1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/* foundry-command-line-remote.c
*
* Copyright 2024 Christian Hergert <chergert@redhat.com>
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <glib/gstdio.h>
#include "foundry-command-line-remote-private.h"
#include "foundry-inhibitor-private.h"
#include "foundry-ipc.h"
struct _FoundryCommandLineRemote
{
FoundryCommandLine parent_instance;
DexCancellable *cancellable;
FoundryContext *context;
FoundryInhibitor *inhibitor;
DexFuture *proxy;
GDBusConnection *connection;
char *object_path;
char *directory;
char **env;
int stdin_fd;
int stdout_fd;
int stderr_fd;
};
enum {
PROP_0,
PROP_CONNECTION,
PROP_OBJECT_PATH,
N_PROPS
};
G_DEFINE_FINAL_TYPE (FoundryCommandLineRemote, foundry_command_line_remote, FOUNDRY_TYPE_COMMAND_LINE)
static GParamSpec *properties[N_PROPS];
static DexCancellable *
foundry_command_line_remote_dup_cancellable (FoundryCommandLine *command_line)
{
return dex_ref (FOUNDRY_COMMAND_LINE_REMOTE (command_line)->cancellable);
}
static const char *
foundry_command_line_remote_getenv (FoundryCommandLine *command_line,
const char *name)
{
FoundryCommandLineRemote *self = FOUNDRY_COMMAND_LINE_REMOTE (command_line);
return g_environ_getenv (self->env, name);
}
static char **
foundry_command_line_remote_get_environ (FoundryCommandLine *command_line)
{
FoundryCommandLineRemote *self = FOUNDRY_COMMAND_LINE_REMOTE (command_line);
return g_strdupv (self->env);
}
static void
get_proxy_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
g_autoptr(FoundryIpcCommandLine) proxy = NULL;
g_autoptr(DexPromise) promise = user_data;
g_autoptr(GError) error = NULL;
g_assert (!object || G_IS_OBJECT (object));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (DEX_IS_PROMISE (promise));
if (!(proxy = foundry_ipc_command_line_proxy_new_finish (result, &error)))
dex_promise_reject (promise, g_steal_pointer (&error));
else
dex_promise_resolve_object (promise, g_steal_pointer (&proxy));
}
static DexFuture *
get_proxy (FoundryCommandLineRemote *self)
{
g_assert (FOUNDRY_IS_COMMAND_LINE_REMOTE (self));
if (self->proxy == NULL)
{
self->proxy = DEX_FUTURE (dex_promise_new ());
foundry_ipc_command_line_proxy_new (self->connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
self->object_path,
NULL,
get_proxy_cb,
dex_ref (self->proxy));
}
return dex_ref (self->proxy);
}
static void
call_open_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
FoundryIpcCommandLine *proxy = (FoundryIpcCommandLine *)object;
g_autoptr(DexPromise) promise = user_data;
g_autoptr(GError) error = NULL;
g_autoptr(GUnixFDList) fd_list = NULL;
g_autoptr(GVariant) handle = NULL;
g_autofd int fd = -1;
g_assert (FOUNDRY_IPC_IS_COMMAND_LINE (proxy));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (DEX_IS_PROMISE (promise));
if (!foundry_ipc_command_line_call_open_finish (proxy, &handle, &fd_list, result, &error))
dex_promise_reject (promise, g_steal_pointer (&error));
else if (-1 == (fd = g_unix_fd_list_get (fd_list, g_variant_get_handle (handle), &error)))
dex_promise_reject (promise, g_steal_pointer (&error));
else
dex_promise_resolve_fd (promise, g_steal_fd (&fd));
}
static DexFuture *
call_open (DexFuture *completed,
gpointer user_data)
{
g_autoptr(FoundryIpcCommandLine) proxy = NULL;
DexPromise *promise;
int fd_number = GPOINTER_TO_INT (user_data);
g_assert (DEX_IS_FUTURE (completed));
g_assert (fd_number >= 0);
proxy = dex_await_object (dex_ref (completed), NULL);
promise = dex_promise_new_cancellable ();
foundry_ipc_command_line_call_open (proxy,
fd_number,
NULL,
dex_promise_get_cancellable (promise),
call_open_cb,
dex_ref (promise));
return DEX_FUTURE (promise);
}
static DexFuture *
foundry_command_line_remote_open (FoundryCommandLine *command_line,
int fd_number)
{
FoundryCommandLineRemote *self = (FoundryCommandLineRemote *)command_line;
DexFuture *future;
g_assert (FOUNDRY_IS_COMMAND_LINE_REMOTE (self));
g_assert (fd_number >= 0);
future = get_proxy (self);
future = dex_future_then (future, call_open, GINT_TO_POINTER (fd_number), NULL);
return future;
}
static gboolean
foundry_command_line_remote_isatty (FoundryCommandLine *command_line)
{
FoundryCommandLineRemote *self = (FoundryCommandLineRemote *)command_line;
g_assert (FOUNDRY_IS_COMMAND_LINE_REMOTE (self));
return isatty (self->stdin_fd);
}
static void
write_all (int fd,
const char *message,
gssize to_write)
{
const char *data = message;
if (fd < 0)
return;
if (to_write < 0)
to_write = strlen (message);
while (to_write > 0)
{
gssize n_written;
errno = 0;
n_written = write (fd, data, to_write);
if (n_written < 0)
return;
if (n_written == 0 && errno == EINTR)
continue;
to_write -= n_written;
data += (gsize)n_written;
}
}
static void
foundry_command_line_remote_print (FoundryCommandLine *command_line,
const char *message)
{
write_all (FOUNDRY_COMMAND_LINE_REMOTE (command_line)->stdout_fd, message, -1);
}
static void
foundry_command_line_remote_printerr (FoundryCommandLine *command_line,
const char *message)
{
write_all (FOUNDRY_COMMAND_LINE_REMOTE (command_line)->stderr_fd, message, -1);
}
static char *
foundry_command_line_remote_get_directory (FoundryCommandLine *command_line)
{
return g_strdup (FOUNDRY_COMMAND_LINE_REMOTE (command_line)->directory);
}
static int
foundry_command_line_remote_get_stdin (FoundryCommandLine *self)
{
return FOUNDRY_COMMAND_LINE_REMOTE (self)->stdin_fd;
}
static int
foundry_command_line_remote_get_stdout (FoundryCommandLine *self)
{
return FOUNDRY_COMMAND_LINE_REMOTE (self)->stdout_fd;
}
static int
foundry_command_line_remote_get_stderr (FoundryCommandLine *self)
{
return FOUNDRY_COMMAND_LINE_REMOTE (self)->stderr_fd;
}
static void
foundry_command_line_remote_finalize (GObject *object)
{
FoundryCommandLineRemote *self = (FoundryCommandLineRemote *)object;
dex_clear (&self->proxy);
dex_clear (&self->cancellable);
g_clear_object (&self->connection);
g_clear_object (&self->context);
g_clear_object (&self->inhibitor);
g_clear_pointer (&self->object_path, g_free);
g_clear_pointer (&self->directory, g_free);
g_clear_pointer (&self->env, g_strfreev);
g_clear_fd (&self->stdin_fd, NULL);
g_clear_fd (&self->stdout_fd, NULL);
g_clear_fd (&self->stderr_fd, NULL);
G_OBJECT_CLASS (foundry_command_line_remote_parent_class)->finalize (object);
}
static void
foundry_command_line_remote_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FoundryCommandLineRemote *self = FOUNDRY_COMMAND_LINE_REMOTE (object);
switch (prop_id)
{
case PROP_CONNECTION:
g_value_set_object (value, self->connection);
break;
case PROP_OBJECT_PATH:
g_value_set_string (value, self->object_path);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
foundry_command_line_remote_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FoundryCommandLineRemote *self = FOUNDRY_COMMAND_LINE_REMOTE (object);
switch (prop_id)
{
case PROP_CONNECTION:
self->connection = g_value_dup_object (value);
break;
case PROP_OBJECT_PATH:
self->object_path = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
foundry_command_line_remote_class_init (FoundryCommandLineRemoteClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
FoundryCommandLineClass *command_line_class = FOUNDRY_COMMAND_LINE_CLASS (klass);
object_class->finalize = foundry_command_line_remote_finalize;
object_class->get_property = foundry_command_line_remote_get_property;
object_class->set_property = foundry_command_line_remote_set_property;
command_line_class->get_directory = foundry_command_line_remote_get_directory;
command_line_class->getenv = foundry_command_line_remote_getenv;
command_line_class->get_environ = foundry_command_line_remote_get_environ;
command_line_class->open = foundry_command_line_remote_open;
command_line_class->isatty = foundry_command_line_remote_isatty;
command_line_class->print = foundry_command_line_remote_print;
command_line_class->printerr = foundry_command_line_remote_printerr;
command_line_class->get_stdin = foundry_command_line_remote_get_stdin;
command_line_class->get_stdout = foundry_command_line_remote_get_stdout;
command_line_class->get_stderr = foundry_command_line_remote_get_stderr;
command_line_class->dup_cancellable = foundry_command_line_remote_dup_cancellable;
properties[PROP_CONNECTION] =
g_param_spec_object ("connection", NULL, NULL,
G_TYPE_DBUS_CONNECTION,
(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
properties[PROP_OBJECT_PATH] =
g_param_spec_string ("object-path", NULL, NULL,
NULL,
(G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
foundry_command_line_remote_init (FoundryCommandLineRemote *self)
{
self->stdin_fd = -1;
self->stdout_fd = -1;
self->stderr_fd = -1;
self->cancellable = dex_cancellable_new ();
}
static void
foundry_command_line_remote_connection_closed (FoundryCommandLineRemote *self,
gboolean remote_peer_vanished,
const GError *error,
GDBusConnection *connection)
{
g_assert (FOUNDRY_IS_COMMAND_LINE_REMOTE (self));
g_assert (G_IS_DBUS_CONNECTION (connection));
if (self->cancellable != NULL)
dex_cancellable_cancel (self->cancellable);
}
FoundryCommandLine *
foundry_command_line_remote_new (FoundryContext *context,
const char *directory,
const char * const *env,
int stdin_fd,
int stdout_fd,
int stderr_fd,
GDBusConnection *connection,
const char *object_path)
{
FoundryCommandLineRemote *self;
g_return_val_if_fail (!context || FOUNDRY_IS_CONTEXT (context), NULL);
g_return_val_if_fail (directory != NULL, NULL);
g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
self = g_object_new (FOUNDRY_TYPE_COMMAND_LINE_REMOTE,
"connection", connection,
"object-path", object_path,
NULL);
g_signal_connect_object (connection,
"closed",
G_CALLBACK (foundry_command_line_remote_connection_closed),
self,
G_CONNECT_SWAPPED);
if (g_set_object (&self->context, context))
self->inhibitor = foundry_inhibitor_new (context, NULL);
self->stdin_fd = g_steal_fd (&stdin_fd);
self->stdout_fd = g_steal_fd (&stdout_fd);
self->stderr_fd = g_steal_fd (&stderr_fd);
self->directory = g_strdup (directory);
self->env = g_strdupv ((char **)env);
return FOUNDRY_COMMAND_LINE (self);
}
FoundryContext *
foundry_command_line_remote_get_context (FoundryCommandLineRemote *self)
{
g_return_val_if_fail (FOUNDRY_IS_COMMAND_LINE_REMOTE (self), NULL);
return self->context;
}
|