1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2012 Red Hat, Inc.
4 * Copyright © 2012-2013 Canonical Limited
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at 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
23 * This class contains a set of options for launching child processes,
24 * such as where its standard input and output will be directed, the
25 * argument list, the environment, and more.
27 * While the #GSubprocess class has high level functions covering
28 * popular cases, use of this class allows access to more advanced
29 * options. It can also be used to launch multiple subprocesses with
30 * a similar configuration.
35 #define ALL_STDIN_FLAGS (G_SUBPROCESS_FLAGS_STDIN_PIPE | \
36 G_SUBPROCESS_FLAGS_STDIN_INHERIT)
37 #define ALL_STDOUT_FLAGS (G_SUBPROCESS_FLAGS_STDOUT_PIPE | \
38 G_SUBPROCESS_FLAGS_STDOUT_SILENCE)
39 #define ALL_STDERR_FLAGS (G_SUBPROCESS_FLAGS_STDERR_PIPE | \
40 G_SUBPROCESS_FLAGS_STDERR_SILENCE | \
41 G_SUBPROCESS_FLAGS_STDERR_MERGE)
45 #include "gsubprocesslauncher-private.h"
46 #include "gioenumtypes.h"
47 #include "gsubprocess.h"
48 #include "ginitable.h"
56 typedef GObjectClass GSubprocessLauncherClass;
58 G_DEFINE_TYPE (GSubprocessLauncher, g_subprocess_launcher, G_TYPE_OBJECT)
61 verify_disposition (const gchar *stream_name,
62 GSubprocessFlags filtered_flags,
64 const gchar *filename)
70 else if (((filtered_flags - 1) & filtered_flags) == 0)
73 n_bits = 2; /* ...or more */
75 if (n_bits + (fd >= 0) + (filename != NULL) > 1)
79 err = g_string_new (NULL);
85 class = g_type_class_peek (G_TYPE_SUBPROCESS_FLAGS);
87 for (i = 0; i < class->n_values; i++)
89 const GFlagsValue *value = &class->values[i];
91 if (filtered_flags & value->value)
92 g_string_append_printf (err, " %s", value->value_name);
95 g_type_class_unref (class);
99 g_string_append_printf (err, " g_subprocess_launcher_take_%s_fd()", stream_name);
102 g_string_append_printf (err, " g_subprocess_launcher_set_%s_file_path()", stream_name);
104 g_critical ("You may specify at most one disposition for the %s stream, but you specified:%s.",
105 stream_name, err->str);
106 g_string_free (err, TRUE);
115 verify_flags (GSubprocessFlags flags)
117 return verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, -1, NULL) &&
118 verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, -1, NULL) &&
119 verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, -1, NULL);
123 g_subprocess_launcher_set_property (GObject *object, guint prop_id,
124 const GValue *value, GParamSpec *pspec)
126 GSubprocessLauncher *launcher = G_SUBPROCESS_LAUNCHER (object);
128 g_assert (prop_id == 1);
130 if (verify_flags (g_value_get_flags (value)))
131 launcher->flags = g_value_get_flags (value);
135 g_subprocess_launcher_dispose (GObject *object)
137 GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object);
140 g_clear_pointer (&self->stdin_path, g_free);
141 g_clear_pointer (&self->stdout_path, g_free);
142 g_clear_pointer (&self->stderr_path, g_free);
144 g_subprocess_launcher_close (self);
146 if (self->child_setup_destroy_notify)
147 (* self->child_setup_destroy_notify) (self->child_setup_user_data);
148 self->child_setup_destroy_notify = NULL;
149 self->child_setup_user_data = NULL;
152 g_clear_pointer (&self->envp, g_strfreev);
153 g_clear_pointer (&self->cwd, g_free);
155 G_OBJECT_CLASS (g_subprocess_launcher_parent_class)->dispose (object);
159 g_subprocess_launcher_init (GSubprocessLauncher *self)
161 self->envp = g_get_environ ();
165 self->stdout_fd = -1;
166 self->stderr_fd = -1;
167 self->source_fds = g_array_new (FALSE, 0, sizeof (int));
168 self->target_fds = g_array_new (FALSE, 0, sizeof (int));
173 g_subprocess_launcher_class_init (GSubprocessLauncherClass *class)
175 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
177 gobject_class->set_property = g_subprocess_launcher_set_property;
178 gobject_class->dispose = g_subprocess_launcher_dispose;
180 g_object_class_install_property (gobject_class, 1,
181 g_param_spec_flags ("flags", "Flags", "GSubprocessFlags for launched processes",
182 G_TYPE_SUBPROCESS_FLAGS, 0, G_PARAM_WRITABLE |
183 G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
187 * g_subprocess_launcher_new:
188 * @flags: #GSubprocessFlags
190 * Creates a new #GSubprocessLauncher.
192 * The launcher is created with the default options. A copy of the
193 * environment of the calling process is made at the time of this call
194 * and will be used as the environment that the process is launched in.
198 GSubprocessLauncher *
199 g_subprocess_launcher_new (GSubprocessFlags flags)
201 if (!verify_flags (flags))
204 return g_object_new (G_TYPE_SUBPROCESS_LAUNCHER,
210 * g_subprocess_launcher_set_environ:
211 * @self: a #GSubprocessLauncher
212 * @env: (array zero-terminated=1) (element-type filename) (transfer none):
213 * the replacement environment
215 * Replace the entire environment of processes launched from this
216 * launcher with the given 'environ' variable.
218 * Typically you will build this variable by using g_listenv() to copy
219 * the process 'environ' and using the functions g_environ_setenv(),
220 * g_environ_unsetenv(), etc.
222 * As an alternative, you can use g_subprocess_launcher_setenv(),
223 * g_subprocess_launcher_unsetenv(), etc.
225 * Pass an empty array to set an empty environment. Pass %NULL to inherit the
226 * parent process’ environment. As of GLib 2.54, the parent process’ environment
227 * will be copied when g_subprocess_launcher_set_environ() is called.
228 * Previously, it was copied when the subprocess was executed. This means the
229 * copied environment may now be modified (using g_subprocess_launcher_setenv(),
230 * etc.) before launching the subprocess.
232 * On UNIX, all strings in this array can be arbitrary byte strings.
233 * On Windows, they should be in UTF-8.
238 g_subprocess_launcher_set_environ (GSubprocessLauncher *self,
241 g_strfreev (self->envp);
242 self->envp = g_strdupv (env);
244 if (self->envp == NULL)
245 self->envp = g_get_environ ();
249 * g_subprocess_launcher_setenv:
250 * @self: a #GSubprocessLauncher
251 * @variable: (type filename): the environment variable to set,
252 * must not contain '='
253 * @value: (type filename): the new value for the variable
254 * @overwrite: whether to change the variable if it already exists
256 * Sets the environment variable @variable in the environment of
257 * processes launched from this launcher.
259 * On UNIX, both the variable's name and value can be arbitrary byte
260 * strings, except that the variable's name cannot contain '='.
261 * On Windows, they should be in UTF-8.
266 g_subprocess_launcher_setenv (GSubprocessLauncher *self,
267 const gchar *variable,
271 self->envp = g_environ_setenv (self->envp, variable, value, overwrite);
275 * g_subprocess_launcher_unsetenv:
276 * @self: a #GSubprocessLauncher
277 * @variable: (type filename): the environment variable to unset,
278 * must not contain '='
280 * Removes the environment variable @variable from the environment of
281 * processes launched from this launcher.
283 * On UNIX, the variable's name can be an arbitrary byte string not
284 * containing '='. On Windows, it should be in UTF-8.
289 g_subprocess_launcher_unsetenv (GSubprocessLauncher *self,
290 const gchar *variable)
292 self->envp = g_environ_unsetenv (self->envp, variable);
296 * g_subprocess_launcher_getenv:
297 * @self: a #GSubprocessLauncher
298 * @variable: (type filename): the environment variable to get
300 * Returns the value of the environment variable @variable in the
301 * environment of processes launched from this launcher.
303 * On UNIX, the returned string can be an arbitrary byte string.
304 * On Windows, it will be UTF-8.
306 * Returns: (nullable) (type filename): the value of the environment variable,
312 g_subprocess_launcher_getenv (GSubprocessLauncher *self,
313 const gchar *variable)
315 return g_environ_getenv (self->envp, variable);
319 * g_subprocess_launcher_set_cwd:
320 * @self: a #GSubprocessLauncher
321 * @cwd: (type filename): the cwd for launched processes
323 * Sets the current working directory that processes will be launched
326 * By default processes are launched with the current working directory
327 * of the launching process at the time of launch.
332 g_subprocess_launcher_set_cwd (GSubprocessLauncher *self,
336 self->cwd = g_strdup (cwd);
340 * g_subprocess_launcher_set_flags:
341 * @self: a #GSubprocessLauncher
342 * @flags: #GSubprocessFlags
344 * Sets the flags on the launcher.
346 * The default flags are %G_SUBPROCESS_FLAGS_NONE.
348 * You may not set flags that specify conflicting options for how to
349 * handle a particular stdio stream (eg: specifying both
350 * %G_SUBPROCESS_FLAGS_STDIN_PIPE and
351 * %G_SUBPROCESS_FLAGS_STDIN_INHERIT).
353 * You may also not set a flag that conflicts with a previous call to a
354 * function like g_subprocess_launcher_set_stdin_file_path() or
355 * g_subprocess_launcher_take_stdout_fd().
360 g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
361 GSubprocessFlags flags)
363 const gchar *stdin_path = NULL, *stdout_path = NULL, *stderr_path = NULL;
364 gint stdin_fd = -1, stdout_fd = -1, stderr_fd = -1;
367 stdin_fd = self->stdin_fd;
368 stdout_fd = self->stdout_fd;
369 stderr_fd = self->stderr_fd;
370 stdin_path = self->stdin_path;
371 stdout_path = self->stdout_path;
372 stderr_path = self->stderr_path;
375 if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, stdin_fd, stdin_path) &&
376 verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, stdout_fd, stdout_path) &&
377 verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, stderr_fd, stderr_path))
383 assign_fd (gint *fd_ptr, gint fd)
395 flags = fcntl (fd, F_GETFD);
396 if (~flags & FD_CLOEXEC)
397 fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
402 * g_subprocess_launcher_set_stdin_file_path:
403 * @self: a #GSubprocessLauncher
404 * @path: (type filename) (nullable: a filename or %NULL
406 * Sets the file path to use as the stdin for spawned processes.
408 * If @path is %NULL then any previously given path is unset.
410 * The file must exist or spawning the process will fail.
412 * You may not set a stdin file path if a stdin fd is already set or if
413 * the launcher flags contain any flags directing stdin elsewhere.
415 * This feature is only available on UNIX.
420 g_subprocess_launcher_set_stdin_file_path (GSubprocessLauncher *self,
423 if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, self->stdin_fd, path))
425 g_free (self->stdin_path);
426 self->stdin_path = g_strdup (path);
431 * g_subprocess_launcher_take_stdin_fd:
432 * @self: a #GSubprocessLauncher
433 * @fd: a file descriptor, or -1
435 * Sets the file descriptor to use as the stdin for spawned processes.
437 * If @fd is -1 then any previously given fd is unset.
439 * Note that if your intention is to have the stdin of the calling
440 * process inherited by the child then %G_SUBPROCESS_FLAGS_STDIN_INHERIT
441 * is a better way to go about doing that.
443 * The passed @fd is noted but will not be touched in the current
444 * process. It is therefore necessary that it be kept open by the
445 * caller until the subprocess is spawned. The file descriptor will
446 * also not be explicitly closed on the child side, so it must be marked
447 * O_CLOEXEC if that's what you want.
449 * You may not set a stdin fd if a stdin file path is already set or if
450 * the launcher flags contain any flags directing stdin elsewhere.
452 * This feature is only available on UNIX.
457 g_subprocess_launcher_take_stdin_fd (GSubprocessLauncher *self,
460 if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, fd, self->stdin_path))
461 assign_fd (&self->stdin_fd, fd);
465 * g_subprocess_launcher_set_stdout_file_path:
466 * @self: a #GSubprocessLauncher
467 * @path: (type filename) (nullable): a filename or %NULL
469 * Sets the file path to use as the stdout for spawned processes.
471 * If @path is %NULL then any previously given path is unset.
473 * The file will be created or truncated when the process is spawned, as
474 * would be the case if using '>' at the shell.
476 * You may not set a stdout file path if a stdout fd is already set or
477 * if the launcher flags contain any flags directing stdout elsewhere.
479 * This feature is only available on UNIX.
484 g_subprocess_launcher_set_stdout_file_path (GSubprocessLauncher *self,
487 if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, self->stdout_fd, path))
489 g_free (self->stdout_path);
490 self->stdout_path = g_strdup (path);
495 * g_subprocess_launcher_take_stdout_fd:
496 * @self: a #GSubprocessLauncher
497 * @fd: a file descriptor, or -1
499 * Sets the file descriptor to use as the stdout for spawned processes.
501 * If @fd is -1 then any previously given fd is unset.
503 * Note that the default behaviour is to pass stdout through to the
504 * stdout of the parent process.
506 * The passed @fd is noted but will not be touched in the current
507 * process. It is therefore necessary that it be kept open by the
508 * caller until the subprocess is spawned. The file descriptor will
509 * also not be explicitly closed on the child side, so it must be marked
510 * O_CLOEXEC if that's what you want.
512 * You may not set a stdout fd if a stdout file path is already set or
513 * if the launcher flags contain any flags directing stdout elsewhere.
515 * This feature is only available on UNIX.
520 g_subprocess_launcher_take_stdout_fd (GSubprocessLauncher *self,
523 if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, fd, self->stdout_path))
524 assign_fd (&self->stdout_fd, fd);
528 * g_subprocess_launcher_set_stderr_file_path:
529 * @self: a #GSubprocessLauncher
530 * @path: (type filename) (nullable): a filename or %NULL
532 * Sets the file path to use as the stderr for spawned processes.
534 * If @path is %NULL then any previously given path is unset.
536 * The file will be created or truncated when the process is spawned, as
537 * would be the case if using '2>' at the shell.
539 * If you want to send both stdout and stderr to the same file then use
540 * %G_SUBPROCESS_FLAGS_STDERR_MERGE.
542 * You may not set a stderr file path if a stderr fd is already set or
543 * if the launcher flags contain any flags directing stderr elsewhere.
545 * This feature is only available on UNIX.
550 g_subprocess_launcher_set_stderr_file_path (GSubprocessLauncher *self,
553 if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, self->stderr_fd, path))
555 g_free (self->stderr_path);
556 self->stderr_path = g_strdup (path);
561 * g_subprocess_launcher_take_stderr_fd:
562 * @self: a #GSubprocessLauncher
563 * @fd: a file descriptor, or -1
565 * Sets the file descriptor to use as the stderr for spawned processes.
567 * If @fd is -1 then any previously given fd is unset.
569 * Note that the default behaviour is to pass stderr through to the
570 * stderr of the parent process.
572 * The passed @fd belongs to the #GSubprocessLauncher. It will be
573 * automatically closed when the launcher is finalized. The file
574 * descriptor will also be closed on the child side when executing the
577 * You may not set a stderr fd if a stderr file path is already set or
578 * if the launcher flags contain any flags directing stderr elsewhere.
580 * This feature is only available on UNIX.
585 g_subprocess_launcher_take_stderr_fd (GSubprocessLauncher *self,
588 if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, fd, self->stderr_path))
589 assign_fd (&self->stderr_fd, fd);
593 * g_subprocess_launcher_take_fd:
594 * @self: a #GSubprocessLauncher
595 * @source_fd: File descriptor in parent process
596 * @target_fd: Target descriptor for child process
598 * Transfer an arbitrary file descriptor from parent process to the
599 * child. This function takes "ownership" of the fd; it will be closed
600 * in the parent when @self is freed.
602 * By default, all file descriptors from the parent will be closed.
603 * This function allows you to create (for example) a custom pipe() or
604 * socketpair() before launching the process, and choose the target
605 * descriptor in the child.
607 * An example use case is GNUPG, which has a command line argument
608 * --passphrase-fd providing a file descriptor number where it expects
609 * the passphrase to be written.
612 g_subprocess_launcher_take_fd (GSubprocessLauncher *self,
616 if (self->source_fds != NULL && self->target_fds != NULL)
618 g_array_append_val (self->source_fds, source_fd);
619 g_array_append_val (self->target_fds, target_fd);
624 * g_subprocess_launcher_close:
625 * @self: a #GSubprocessLauncher
627 * Closes all the file descriptors previously passed to the object with
628 * g_subprocess_launcher_take_fd(), g_subprocess_launcher_take_stderr_fd(), etc.
630 * After calling this method, any subsequent calls to g_subprocess_launcher_spawn() or g_subprocess_launcher_spawnv() will
631 * return %G_IO_ERROR_CLOSED. This method is idempotent if
632 * called more than once.
634 * This function is called automatically when the #GSubprocessLauncher
635 * is disposed, but is provided separately so that garbage collected
636 * language bindings can call it earlier to guarantee when FDs are closed.
641 g_subprocess_launcher_close (GSubprocessLauncher *self)
645 g_return_if_fail (G_IS_SUBPROCESS_LAUNCHER (self));
647 if (self->stdin_fd != -1)
648 close (self->stdin_fd);
651 if (self->stdout_fd != -1)
652 close (self->stdout_fd);
653 self->stdout_fd = -1;
655 if (self->stderr_fd != -1)
656 close (self->stderr_fd);
657 self->stderr_fd = -1;
659 if (self->source_fds)
661 g_assert (self->target_fds != NULL);
662 g_assert (self->source_fds->len == self->target_fds->len);
664 for (i = 0; i < self->source_fds->len; i++)
666 (void) close (g_array_index (self->source_fds, int, i));
667 (void) close (g_array_index (self->target_fds, int, i));
669 g_clear_pointer (&self->source_fds, g_array_unref);
670 g_clear_pointer (&self->target_fds, g_array_unref);
673 self->closed_fd = TRUE;
677 * g_subprocess_launcher_set_child_setup: (skip)
678 * @self: a #GSubprocessLauncher
679 * @child_setup: a #GSpawnChildSetupFunc to use as the child setup function
680 * @user_data: user data for @child_setup
681 * @destroy_notify: a #GDestroyNotify for @user_data
683 * Sets up a child setup function.
685 * The child setup function will be called after fork() but before
686 * exec() on the child's side.
688 * @destroy_notify will not be automatically called on the child's side
689 * of the fork(). It will only be called when the last reference on the
690 * #GSubprocessLauncher is dropped or when a new child setup function is
693 * %NULL can be given as @child_setup to disable the functionality.
695 * Child setup functions are only available on UNIX.
700 g_subprocess_launcher_set_child_setup (GSubprocessLauncher *self,
701 GSpawnChildSetupFunc child_setup,
703 GDestroyNotify destroy_notify)
705 if (self->child_setup_destroy_notify)
706 (* self->child_setup_destroy_notify) (self->child_setup_user_data);
708 self->child_setup_func = child_setup;
709 self->child_setup_user_data = user_data;
710 self->child_setup_destroy_notify = destroy_notify;
715 * g_subprocess_launcher_spawn:
716 * @self: a #GSubprocessLauncher
718 * @argv0: Command line arguments
719 * @...: Continued arguments, %NULL terminated
721 * Creates a #GSubprocess given a provided varargs list of arguments.
724 * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
727 g_subprocess_launcher_spawn (GSubprocessLauncher *launcher,
737 g_return_val_if_fail (argv0 != NULL && argv0[0] != '\0', NULL);
738 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
740 args = g_ptr_array_new ();
742 va_start (ap, argv0);
743 g_ptr_array_add (args, (gchar *) argv0);
744 while ((arg = va_arg (ap, const gchar *)))
745 g_ptr_array_add (args, (gchar *) arg);
747 g_ptr_array_add (args, NULL);
750 result = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
752 g_ptr_array_free (args, TRUE);
759 * g_subprocess_launcher_spawnv:
760 * @self: a #GSubprocessLauncher
761 * @argv: (array zero-terminated=1) (element-type filename): Command line arguments
764 * Creates a #GSubprocess given a provided array of arguments.
767 * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
770 g_subprocess_launcher_spawnv (GSubprocessLauncher *launcher,
771 const gchar * const *argv,
774 GSubprocess *subprocess;
776 g_return_val_if_fail (argv != NULL && argv[0] != NULL && argv[0][0] != '\0', NULL);
779 if (launcher->closed_fd)
784 "Can't spawn a new child because a passed file descriptor has been closed.");
789 subprocess = g_object_new (G_TYPE_SUBPROCESS,
791 "flags", launcher->flags,
793 g_subprocess_set_launcher (subprocess, launcher);
795 if (!g_initable_init (G_INITABLE (subprocess), NULL, error))
797 g_object_unref (subprocess);