1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2012 Red Hat, Inc.
4 * Copyright © 2012-2013 Canonical Limited
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2 of the licence or (at
9 * your option) any later version.
11 * See the included COPYING file for more information.
13 * Authors: Colin Walters <walters@verbum.org>
14 * Ryan Lortie <desrt@desrt.ca>
18 * SECTION:gsubprocesslauncher
19 * @title: GSubprocess Launcher
20 * @short_description: Environment options for launching a child process
22 * This class contains a set of options for launching child processes,
23 * such as where its standard input and output will be directed, the
24 * argument list, the environment, and more.
26 * While the #GSubprocess class has high level functions covering
27 * popular cases, use of this class allows access to more advanced
28 * options. It can also be used to launch multiple subprocesses with
29 * a similar configuration.
34 #define ALL_STDIN_FLAGS (G_SUBPROCESS_FLAGS_STDIN_PIPE | \
35 G_SUBPROCESS_FLAGS_STDIN_INHERIT)
36 #define ALL_STDOUT_FLAGS (G_SUBPROCESS_FLAGS_STDOUT_PIPE | \
37 G_SUBPROCESS_FLAGS_STDOUT_SILENCE)
38 #define ALL_STDERR_FLAGS (G_SUBPROCESS_FLAGS_STDERR_PIPE | \
39 G_SUBPROCESS_FLAGS_STDERR_SILENCE | \
40 G_SUBPROCESS_FLAGS_STDERR_MERGE)
44 #include "gsubprocesslauncher-private.h"
45 #include "gioenumtypes.h"
46 #include "gsubprocess.h"
47 #include "ginitable.h"
54 typedef GObjectClass GSubprocessLauncherClass;
56 G_DEFINE_TYPE (GSubprocessLauncher, g_subprocess_launcher, G_TYPE_OBJECT);
59 verify_disposition (const gchar *stream_name,
60 GSubprocessFlags filtered_flags,
62 const gchar *filename)
68 else if (((filtered_flags - 1) & filtered_flags) == 0)
71 n_bits = 2; /* ...or more */
73 if (n_bits + (fd >= 0) + (filename != NULL) > 1)
77 err = g_string_new (NULL);
83 class = g_type_class_peek (G_TYPE_SUBPROCESS_FLAGS);
84 while ((value = g_flags_get_first_value (class, filtered_flags)))
86 g_string_append_printf (err, " %s", value->value_name);
87 filtered_flags &= value->value;
90 g_type_class_unref (class);
94 g_string_append_printf (err, " g_subprocess_launcher_take_%s_fd()", stream_name);
97 g_string_append_printf (err, " g_subprocess_launcher_set_%s_file_path()", stream_name);
99 g_critical ("You may specify at most one disposition for the %s stream, but you specified:%s.",
100 stream_name, err->str);
101 g_string_free (err, TRUE);
110 verify_flags (GSubprocessFlags flags)
112 return verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, -1, NULL) &&
113 verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, -1, NULL) &&
114 verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, -1, NULL);
118 g_subprocess_launcher_set_property (GObject *object, guint prop_id,
119 const GValue *value, GParamSpec *pspec)
121 GSubprocessLauncher *launcher = G_SUBPROCESS_LAUNCHER (object);
123 g_assert (prop_id == 1);
125 if (verify_flags (g_value_get_flags (value)))
126 launcher->flags = g_value_get_flags (value);
130 g_subprocess_launcher_finalize (GObject *object)
132 GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object);
135 g_strfreev (self->envp);
139 g_free (self->stdin_path);
140 g_free (self->stdout_path);
141 g_free (self->stderr_path);
143 if (self->stdin_fd != -1)
144 close (self->stdin_fd);
146 if (self->stdout_fd != -1)
147 close (self->stdout_fd);
149 if (self->stderr_fd != -1)
150 close (self->stderr_fd);
152 if (self->basic_fd_assignments)
154 for (i = 0; i < self->basic_fd_assignments->len; i++)
155 (void) close (g_array_index (self->basic_fd_assignments, int, i));
156 g_array_unref (self->basic_fd_assignments);
158 if (self->needdup_fd_assignments)
160 for (i = 0; i < self->needdup_fd_assignments->len; i += 2)
161 (void) close (g_array_index (self->needdup_fd_assignments, int, i));
162 g_array_unref (self->needdup_fd_assignments);
166 if (self->child_setup_destroy_notify)
167 (* self->child_setup_destroy_notify) (self->child_setup_user_data);
169 G_OBJECT_CLASS (g_subprocess_launcher_parent_class)->finalize (object);
173 g_subprocess_launcher_init (GSubprocessLauncher *self)
175 self->envp = g_listenv ();
178 self->stdout_fd = -1;
179 self->stderr_fd = -1;
181 self->basic_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
182 self->needdup_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
187 g_subprocess_launcher_class_init (GSubprocessLauncherClass *class)
189 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
191 gobject_class->set_property = g_subprocess_launcher_set_property;
192 gobject_class->finalize = g_subprocess_launcher_finalize;
194 g_object_class_install_property (gobject_class, 1,
195 g_param_spec_flags ("flags", "Flags", "GSubprocessFlags for launched processes",
196 G_TYPE_SUBPROCESS_FLAGS, 0, G_PARAM_WRITABLE |
197 G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
201 * g_subprocess_launcher_new:
202 * @flags: #GSubprocessFlags
204 * Creates a new #GSubprocessLauncher.
206 * The launcher is created with the default options. A copy of the
207 * environment of the calling process is made at the time of this call
208 * and will be used as the environment that the process is launched in.
212 GSubprocessLauncher *
213 g_subprocess_launcher_new (GSubprocessFlags flags)
215 if (!verify_flags (flags))
218 return g_object_new (G_TYPE_SUBPROCESS_LAUNCHER,
224 * g_subprocess_launcher_set_environ:
225 * @self: a #GSubprocess
226 * @environ: the replacement environment
228 * Replace the entire environment of processes launched from this
229 * launcher with the given 'environ' variable.
231 * Typically you will build this variable by using g_listenv() to copy
232 * the process 'environ' and using the functions g_environ_setenv(),
233 * g_environ_unsetenv(), etc.
235 * As an alternative, you can use g_subprocess_launcher_setenv(),
236 * g_subprocess_launcher_unsetenv(), etc.
238 * All strings in this array are expected to be in the GLib file name
239 * encoding. On UNIX, this means that they can be arbitrary byte
240 * strings. On Windows, they should be in UTF-8.
245 g_subprocess_launcher_set_environ (GSubprocessLauncher *self,
248 g_strfreev (self->envp);
249 self->envp = g_strdupv (environ);
253 * g_subprocess_launcher_setenv:
254 * @self: a #GSubprocess
255 * @variable: the environment variable to set, must not contain '='
256 * @value: the new value for the variable
257 * @overwrite: whether to change the variable if it already exists
259 * Sets the environment variable @variable in the environment of
260 * processes launched from this launcher.
262 * Both the variable's name and value should be in the GLib file name
263 * encoding. On UNIX, this means that they can be arbitrary byte
264 * strings. On Windows, they should be in UTF-8.
270 g_subprocess_launcher_setenv (GSubprocessLauncher *self,
271 const gchar *variable,
275 self->envp = g_environ_setenv (self->envp, variable, value, overwrite);
279 * g_subprocess_launcher_unsetenv:
280 * @self: a #GSubprocess
281 * @variable: the environment variable to unset, must not contain '='
283 * Removes the environment variable @variable from the environment of
284 * processes launched from this launcher.
286 * The variable name should be in the GLib file name encoding. On UNIX,
287 * this means that they can be arbitrary byte strings. On Windows, they
288 * should be in UTF-8.
293 g_subprocess_launcher_unsetenv (GSubprocessLauncher *self,
294 const gchar *variable)
296 self->envp = g_environ_unsetenv (self->envp, variable);
300 * g_subprocess_launcher_getenv:
301 * @self: a #GSubprocess
302 * @variable: the environment variable to get
304 * Returns the value of the environment variable @variable in the
305 * environment of processes launched from this launcher.
307 * The returned string is in the GLib file name encoding. On UNIX, this
308 * means that it can be an arbitrary byte string. On Windows, it will
311 * Returns: the value of the environment variable, %NULL if unset
316 g_subprocess_launcher_getenv (GSubprocessLauncher *self,
317 const gchar *variable)
319 return g_environ_getenv (self->envp, variable);
323 * g_subprocess_launcher_set_cwd:
324 * @self: a #GSubprocess
325 * @cwd: the cwd for launched processes
327 * Sets the current working directory that processes will be launched
330 * By default processes are launched with the current working directory
331 * of the launching process at the time of launch.
336 g_subprocess_launcher_set_cwd (GSubprocessLauncher *self,
340 self->cwd = g_strdup (cwd);
344 * g_subprocess_launcher_set_flags:
345 * @self: a #GSubprocessLauncher
346 * @flags: #GSubprocessFlags
348 * Sets the flags on the launcher.
350 * The default flags are %G_SUBPROCESS_FLAGS_NONE.
352 * You may not set flags that specify conflicting options for how to
353 * handle a particular stdio stream (eg: specifying both
354 * %G_SUBPROCESS_FLAGS_STDIN_PIPE and
355 * %G_SUBPROCESS_FLAGS_STDIN_INHERIT).
357 * You may also not set a flag that conflicts with a previous call to a
358 * function like g_subprocess_launcher_set_stdin_file_path() or
359 * g_subprocess_launcher_take_stdout_fd().
364 g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
365 GSubprocessFlags flags)
367 if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, self->stdin_fd, self->stdin_path) &&
368 verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, self->stdout_fd, self->stdout_path) &&
369 verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, self->stderr_fd, self->stderr_path))
375 assign_fd (gint *fd_ptr, gint fd)
387 flags = fcntl (fd, F_GETFD);
388 if (~flags & FD_CLOEXEC)
389 fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
394 * g_subprocess_launcher_set_stdin_file_path:
395 * @self: a #GSubprocessLauncher
396 * @path: a filename or %NULL
398 * Sets the file path to use as the stdin for spawned processes.
400 * If @path is %NULL then any previously given path is unset.
402 * The file must exist or spawning the process will fail.
404 * You may not set a stdin file path if a stdin fd is already set or if
405 * the launcher flags contain any flags directing stdin elsewhere.
407 * This feature is only available on UNIX.
412 g_subprocess_launcher_set_stdin_file_path (GSubprocessLauncher *self,
415 if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, self->stdin_fd, path))
417 g_free (self->stdin_path);
418 self->stdin_path = g_strdup (path);
423 * g_subprocess_launcher_take_stdin_fd:
424 * @self: a #GSubprocessLauncher
425 * @fd: a file descriptor, or -1
427 * Sets the file descriptor to use as the stdin for spawned processes.
429 * If @fd is -1 then any previously given fd is unset.
431 * Note that if your intention is to have the stdin of the calling
432 * process inherited by the child then %G_SUBPROCESS_FLAGS_STDIN_INHERIT
433 * is a better way to go about doing that.
435 * The passed @fd is noted but will not be touched in the current
436 * process. It is therefore necessary that it be kept open by the
437 * caller until the subprocess is spawned. The file descriptor will
438 * also not be explicitly closed on the child side, so it must be marked
439 * O_CLOEXEC if that's what you want.
441 * You may not set a stdin fd if a stdin file path is already set or if
442 * the launcher flags contain any flags directing stdin elsewhere.
444 * This feature is only available on UNIX.
449 g_subprocess_launcher_take_stdin_fd (GSubprocessLauncher *self,
452 if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, fd, self->stdin_path))
453 assign_fd (&self->stdin_fd, fd);
457 * g_subprocess_launcher_set_stdout_file_path:
458 * @self: a #GSubprocessLauncher
459 * @path: a filename or %NULL
461 * Sets the file path to use as the stdout for spawned processes.
463 * If @path is %NULL then any previously given path is unset.
465 * The file will be created or truncated when the process is spawned, as
466 * would be the case if using '>' at the shell.
468 * You may not set a stdout file path if a stdout fd is already set or
469 * if the launcher flags contain any flags directing stdout elsewhere.
471 * This feature is only available on UNIX.
476 g_subprocess_launcher_set_stdout_file_path (GSubprocessLauncher *self,
479 if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, self->stdout_fd, path))
481 g_free (self->stdout_path);
482 self->stdout_path = g_strdup (path);
487 * g_subprocess_launcher_take_stdout_fd:
488 * @self: a #GSubprocessLauncher
489 * @fd: a file descriptor, or -1
491 * Sets the file descriptor to use as the stdout for spawned processes.
493 * If @fd is -1 then any previously given fd is unset.
495 * Note that the default behaviour is to pass stdout through to the
496 * stdout of the parent process.
498 * The passed @fd is noted but will not be touched in the current
499 * process. It is therefore necessary that it be kept open by the
500 * caller until the subprocess is spawned. The file descriptor will
501 * also not be explicitly closed on the child side, so it must be marked
502 * O_CLOEXEC if that's what you want.
504 * You may not set a stdout fd if a stdout file path is already set or
505 * if the launcher flags contain any flags directing stdout elsewhere.
507 * This feature is only available on UNIX.
512 g_subprocess_launcher_take_stdout_fd (GSubprocessLauncher *self,
515 if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, fd, self->stdout_path))
516 assign_fd (&self->stdout_fd, fd);
520 * g_subprocess_launcher_set_stderr_file_path:
521 * @self: a #GSubprocessLauncher
522 * @path: a filename or %NULL
524 * Sets the file path to use as the stderr for spawned processes.
526 * If @path is %NULL then any previously given path is unset.
528 * The file will be created or truncated when the process is spawned, as
529 * would be the case if using '2>' at the shell.
531 * If you want to send both stdout and stderr to the same file then use
532 * %G_SUBPROCESS_FLAGS_STDERR_MERGE.
534 * You may not set a stderr file path if a stderr fd is already set or
535 * if the launcher flags contain any flags directing stderr elsewhere.
537 * This feature is only available on UNIX.
542 g_subprocess_launcher_set_stderr_file_path (GSubprocessLauncher *self,
545 if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, self->stderr_fd, path))
547 g_free (self->stderr_path);
548 self->stderr_path = g_strdup (path);
553 * g_subprocess_launcher_take_stderr_fd:
554 * @self: a #GSubprocessLauncher
555 * @fd: a file descriptor, or -1
557 * Sets the file descriptor to use as the stderr for spawned processes.
559 * If @fd is -1 then any previously given fd is unset.
561 * Note that the default behaviour is to pass stderr through to the
562 * stderr of the parent process.
564 * The passed @fd belongs to the #GSubprocessLauncher. It will be
565 * automatically closed when the launcher is finalized. The file
566 * descriptor will also be closed on the child side when executing the
569 * You may not set a stderr fd if a stderr file path is already set or
570 * if the launcher flags contain any flags directing stderr elsewhere.
572 * This feature is only available on UNIX.
577 g_subprocess_launcher_take_stderr_fd (GSubprocessLauncher *self,
580 if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, fd, self->stderr_path))
581 assign_fd (&self->stderr_fd, fd);
585 * g_subprocess_launcher_take_fd:
586 * @self: a #GSubprocessLauncher
587 * @source_fd: File descriptor in parent process
588 * @target_fd: Target descriptor for child process
590 * Transfer an arbitrary file descriptor from parent process to the
591 * child. This function takes "ownership" of the fd; it will be closed
592 * in the parent when @self is freed.
594 * By default, all file descriptors from the parent will be closed.
595 * This function allows you to create (for example) a custom pipe() or
596 * socketpair() before launching the process, and choose the target
597 * descriptor in the child.
599 * An example use case is GNUPG, which has a command line argument
600 * --passphrase-fd providing a file descriptor number where it expects
601 * the passphrase to be written.
604 g_subprocess_launcher_take_fd (GSubprocessLauncher *self,
608 if (source_fd == target_fd)
610 g_array_append_val (self->basic_fd_assignments, source_fd);
614 g_array_append_val (self->needdup_fd_assignments, source_fd);
615 g_array_append_val (self->needdup_fd_assignments, target_fd);
620 * g_subprocess_launcher_set_child_setup:
621 * @self: a #GSubprocessLauncher
622 * @child_setup: a #GSpawnChildSetupFunc to use as the child setup function
623 * @user_data: user data for @child_setup
624 * @destroy_notify: a #GDestroyNotify for @user_data
626 * Sets up a child setup function.
628 * The child setup function will be called after fork() but before
629 * exec() on the child's side.
631 * @destroy_notify will not be automatically called on the child's side
632 * of the fork(). It will only be called when the last reference on the
633 * #GSubprocessLauncher is dropped or when a new child setup function is
636 * %NULL can be given as @child_setup to disable the functionality.
638 * Child setup functions are only available on UNIX.
643 g_subprocess_launcher_set_child_setup (GSubprocessLauncher *self,
644 GSpawnChildSetupFunc child_setup,
646 GDestroyNotify destroy_notify)
648 if (self->child_setup_destroy_notify)
649 (* self->child_setup_destroy_notify) (self->child_setup_user_data);
651 self->child_setup_func = child_setup;
652 self->child_setup_user_data = user_data;
653 self->child_setup_destroy_notify = destroy_notify;
658 * g_subprocess_launcher_spawn:
659 * @self: a #GSubprocessLauncher
661 * @argv0: Command line arguments
662 * @...: Continued arguments, %NULL terminated
664 * A convenience helper for creating a #GSubprocess given a provided
665 * varargs list of arguments.
668 * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
671 g_subprocess_launcher_spawn (GSubprocessLauncher *launcher,
681 g_return_val_if_fail (argv0 != NULL && argv0[0] != '\0', NULL);
682 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
684 args = g_ptr_array_new ();
686 va_start (ap, argv0);
687 g_ptr_array_add (args, (gchar *) argv0);
688 while ((arg = va_arg (ap, const gchar *)))
689 g_ptr_array_add (args, (gchar *) arg);
691 result = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
693 g_ptr_array_free (args, TRUE);
700 * g_subprocess_launcher_spawnv:
701 * @self: a #GSubprocessLauncher
702 * @argv: Command line arguments
705 * A convenience helper for creating a #GSubprocess given a provided
706 * array of arguments.
709 * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
712 g_subprocess_launcher_spawnv (GSubprocessLauncher *launcher,
713 const gchar * const *argv,
716 GSubprocess *subprocess;
718 g_return_val_if_fail (argv != NULL && argv[0] != NULL && argv[0][0] != '\0', NULL);
720 subprocess = g_object_new (G_TYPE_SUBPROCESS,
722 "flags", launcher->flags,
724 g_subprocess_set_launcher (subprocess, launcher);
726 if (!g_initable_init (G_INITABLE (subprocess), NULL, error))
728 g_object_unref (subprocess);