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>
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:
203 * Creates a new #GSubprocessLauncher.
205 * The launcher is created with the default options. A copy of the
206 * environment of the calling process is made at the time of this call
207 * and will be used as the environment that the process is launched in.
211 GSubprocessLauncher *
212 g_subprocess_launcher_new (GSubprocessFlags flags)
214 if (!verify_flags (flags))
217 return g_object_new (G_TYPE_SUBPROCESS_LAUNCHER,
223 * g_subprocess_launcher_set_environ:
224 * @self: a #GSubprocess
225 * @environ: the replacement environment
227 * Replace the entire environment of processes launched from this
228 * launcher with the given 'environ' variable.
230 * Typically you will build this variable by using g_listenv() to copy
231 * the process 'environ' and using the functions g_environ_setenv(),
232 * g_environ_unsetenv(), etc.
234 * As an alternative, you can use g_subprocess_launcher_setenv(),
235 * g_subprocess_launcher_unsetenv(), etc.
237 * All strings in this array are expected to be in the GLib file name
238 * encoding. On UNIX, this means that they can be arbitrary byte
239 * strings. On Windows, they should be in UTF-8.
244 g_subprocess_launcher_set_environ (GSubprocessLauncher *self,
247 g_strfreev (self->envp);
248 self->envp = g_strdupv (environ);
252 * g_subprocess_launcher_setenv:
253 * @self: a #GSubprocess
254 * @variable: the environment variable to set, must not contain '='
255 * @value: the new value for the variable
256 * @overwrite: whether to change the variable if it already exists
258 * Sets the environment variable @variable in the environment of
259 * processes launched from this launcher.
261 * Both the variable's name and value should be in the GLib file name
262 * encoding. On UNIX, this means that they can be arbitrary byte
263 * strings. On Windows, they should be in UTF-8.
269 g_subprocess_launcher_setenv (GSubprocessLauncher *self,
270 const gchar *variable,
274 self->envp = g_environ_setenv (self->envp, variable, value, overwrite);
278 * g_subprocess_launcher_unsetsenv:
279 * @self: a #GSubprocess
280 * @variable: the environment variable to unset, must not contain '='
282 * Removes the environment variable @variable from the environment of
283 * processes launched from this launcher.
285 * The variable name should be in the GLib file name encoding. On UNIX,
286 * this means that they can be arbitrary byte strings. On Windows, they
287 * should be in UTF-8.
292 g_subprocess_launcher_unsetenv (GSubprocessLauncher *self,
293 const gchar *variable)
295 self->envp = g_environ_unsetenv (self->envp, variable);
299 * g_subprocess_launcher_getenv:
300 * @self: a #GSubprocess
301 * @variable: the environment variable to get
303 * Returns the value of the environment variable @variable in the
304 * environment of processes launched from this launcher.
306 * The returned string is in the GLib file name encoding. On UNIX, this
307 * means that it can be an arbitrary byte string. On Windows, it will
310 * Returns: the value of the environment variable, %NULL if unset
315 g_subprocess_launcher_getenv (GSubprocessLauncher *self,
316 const gchar *variable)
318 return g_environ_getenv (self->envp, variable);
322 * g_subprocess_launcher_set_cwd:
323 * @self: a #GSubprocess
324 * @cwd: the cwd for launched processes
326 * Sets the current working directory that processes will be launched
329 * By default processes are launched with the current working directory
330 * of the launching process at the time of launch.
335 g_subprocess_launcher_set_cwd (GSubprocessLauncher *self,
339 self->cwd = g_strdup (cwd);
343 * g_subprocess_launcher_set_flags:
344 * @self: a #GSubprocessLauncher
345 * @flags: #GSubprocessFlags
347 * Sets the flags on the launcher.
349 * The default flags are %G_SUBPROCESS_FLAGS_NONE.
351 * You may not set flags that specify conflicting options for how to
352 * handle a particular stdio stream (eg: specifying both
353 * %G_SUBPROCESS_FLAGS_STDIN_PIPE and
354 * %G_SUBPROCESS_FLAGS_STDIN_INHERIT).
356 * You may also not set a flag that conflicts with a previous call to a
357 * function like g_subprocess_launcher_set_stdin_file_path() or
358 * g_subprocess_launcher_take_stdout_fd().
363 g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
364 GSubprocessFlags flags)
366 if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, self->stdin_fd, self->stdin_path) &&
367 verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, self->stdout_fd, self->stdout_path) &&
368 verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, self->stderr_fd, self->stderr_path))
374 assign_fd (gint *fd_ptr, gint fd)
386 flags = fcntl (fd, F_GETFD);
387 if (~flags & FD_CLOEXEC)
388 fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
393 * g_subprocess_launcher_set_stdin_file_path:
394 * @self: a #GSubprocessLauncher
395 * @path: a filename or %NULL
397 * Sets the file path to use as the stdin for spawned processes.
399 * If @path is %NULL then any previously given path is unset.
401 * The file must exist or spawning the process will fail.
403 * You may not set a stdin file path if a stdin fd is already set or if
404 * the launcher flags contain any flags directing stdin elsewhere.
406 * This feature is only available on UNIX.
411 g_subprocess_launcher_set_stdin_file_path (GSubprocessLauncher *self,
414 if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, self->stdin_fd, path))
416 g_free (self->stdin_path);
417 self->stdin_path = g_strdup (path);
422 * g_subprocess_launcher_take_stdin_fd:
423 * @self: a #GSubprocessLauncher
424 * @fd: a file descriptor, or -1
426 * Sets the file descriptor to use as the stdin for spawned processes.
428 * If @fd is -1 then any previously given fd is unset.
430 * Note that if your intention is to have the stdin of the calling
431 * process inherited by the child then %G_SUBPROCESS_FLAGS_STDIN_INHERIT
432 * is a better way to go about doing that.
434 * The passed @fd is noted but will not be touched in the current
435 * process. It is therefore necessary that it be kept open by the
436 * caller until the subprocess is spawned. The file descriptor will
437 * also not be explicitly closed on the child side, so it must be marked
438 * O_CLOEXEC if that's what you want.
440 * You may not set a stdin fd if a stdin file path is already set or if
441 * the launcher flags contain any flags directing stdin elsewhere.
443 * This feature is only available on UNIX.
448 g_subprocess_launcher_take_stdin_fd (GSubprocessLauncher *self,
451 if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, fd, self->stdin_path))
452 assign_fd (&self->stdin_fd, fd);
456 * g_subprocess_launcher_set_stdout_file_path:
457 * @self: a #GSubprocessLauncher
458 * @path: a filename or %NULL
460 * Sets the file path to use as the stdout for spawned processes.
462 * If @path is %NULL then any previously given path is unset.
464 * The file will be created or truncated when the process is spawned, as
465 * would be the case if using '>' at the shell.
467 * You may not set a stdout file path if a stdout fd is already set or
468 * if the launcher flags contain any flags directing stdout elsewhere.
470 * This feature is only available on UNIX.
475 g_subprocess_launcher_set_stdout_file_path (GSubprocessLauncher *self,
478 if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, self->stdout_fd, path))
480 g_free (self->stdout_path);
481 self->stdout_path = g_strdup (path);
486 * g_subprocess_launcher_take_stdout_fd:
487 * @self: a #GSubprocessLauncher
488 * @fd: a file descriptor, or -1
490 * Sets the file descriptor to use as the stdout for spawned processes.
492 * If @fd is -1 then any previously given fd is unset.
494 * Note that the default behaviour is to pass stdout through to the
495 * stdout of the parent process.
497 * The passed @fd is noted but will not be touched in the current
498 * process. It is therefore necessary that it be kept open by the
499 * caller until the subprocess is spawned. The file descriptor will
500 * also not be explicitly closed on the child side, so it must be marked
501 * O_CLOEXEC if that's what you want.
503 * You may not set a stdout fd if a stdout file path is already set or
504 * if the launcher flags contain any flags directing stdout elsewhere.
506 * This feature is only available on UNIX.
511 g_subprocess_launcher_take_stdout_fd (GSubprocessLauncher *self,
514 if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, fd, self->stdout_path))
515 assign_fd (&self->stdout_fd, fd);
519 * g_subprocess_launcher_set_stderr_file_path:
520 * @self: a #GSubprocessLauncher
521 * @path: a filename or %NULL
523 * Sets the file path to use as the stderr for spawned processes.
525 * If @path is %NULL then any previously given path is unset.
527 * The file will be created or truncated when the process is spawned, as
528 * would be the case if using '2>' at the shell.
530 * If you want to send both stdout and stderr to the same file then use
531 * %G_SUBPROCESS_FLAGS_STDERR_MERGE.
533 * You may not set a stderr file path if a stderr fd is already set or
534 * if the launcher flags contain any flags directing stderr elsewhere.
536 * This feature is only available on UNIX.
541 g_subprocess_launcher_set_stderr_file_path (GSubprocessLauncher *self,
544 if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, self->stderr_fd, path))
546 g_free (self->stderr_path);
547 self->stderr_path = g_strdup (path);
552 * g_subprocess_launcher_take_stderr_fd:
553 * @self: a #GSubprocessLauncher
554 * @fd: a file descriptor, or -1
556 * Sets the file descriptor to use as the stderr for spawned processes.
558 * If @fd is -1 then any previously given fd is unset.
560 * Note that the default behaviour is to pass stderr through to the
561 * stderr of the parent process.
563 * The passed @fd belongs to the #GSubprocessLauncher. It will be
564 * automatically closed when the launcher is finalized. The file
565 * descriptor will also be closed on the child side when executing the
568 * You may not set a stderr fd if a stderr file path is already set or
569 * if the launcher flags contain any flags directing stderr elsewhere.
571 * This feature is only available on UNIX.
576 g_subprocess_launcher_take_stderr_fd (GSubprocessLauncher *self,
579 if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, fd, self->stderr_path))
580 assign_fd (&self->stderr_fd, fd);
584 * g_subprocess_launcher_take_fd:
585 * @self: a #GSubprocessLauncher
586 * @source_fd: File descriptor in parent process
587 * @target_fd: Target descriptor for child process
589 * Transfer an arbitrary file descriptor from parent process to the
590 * child. This function takes "ownership" of the fd; it will be closed
591 * in the parent when @self is freed.
593 * By default, all file descriptors from the parent will be closed.
594 * This function allows you to create (for example) a custom pipe() or
595 * socketpair() before launching the process, and choose the target
596 * descriptor in the child.
598 * An example use case is GNUPG, which has a command line argument
599 * --passphrase-fd providing a file descriptor number where it expects
600 * the passphrase to be written.
603 g_subprocess_launcher_take_fd (GSubprocessLauncher *self,
607 if (source_fd == target_fd)
609 g_array_append_val (self->basic_fd_assignments, source_fd);
613 g_array_append_val (self->needdup_fd_assignments, source_fd);
614 g_array_append_val (self->needdup_fd_assignments, target_fd);
619 * g_subprocess_launcher_set_child_setup:
620 * @self: a #GSubprocessLauncher
621 * @child_setup: a #GSpawnChildSetupFunc to use as the child setup function
622 * @user_data: user data for @child_setup
623 * @destroy_notify: a #GDestroyNotify for @user_data
625 * Sets up a child setup function.
627 * The child setup function will be called after fork() but before
628 * exec() on the child's side.
630 * @destroy_notify will not be automatically called on the child's side
631 * of the fork(). It will only be called when the last reference on the
632 * #GSubprocessLauncher is dropped or when a new child setup function is
635 * %NULL can be given as @child_setup to disable the functionality.
637 * Child setup functions are only available on UNIX.
642 g_subprocess_launcher_set_child_setup (GSubprocessLauncher *self,
643 GSpawnChildSetupFunc child_setup,
645 GDestroyNotify destroy_notify)
647 if (self->child_setup_destroy_notify)
648 (* self->child_setup_destroy_notify) (self->child_setup_user_data);
650 self->child_setup_func = child_setup;
651 self->child_setup_user_data = user_data;
652 self->child_setup_destroy_notify = destroy_notify;
657 * g_subprocess_launcher_spawn:
658 * @self: a #GSubprocessLauncher
660 * @argv0: Command line arguments
661 * @...: Continued arguments, %NULL terminated
663 * A convenience helper for creating a #GSubprocess given a provided
664 * varargs list of arguments.
667 * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
670 g_subprocess_launcher_spawn (GSubprocessLauncher *launcher,
680 g_return_val_if_fail (argv0 != NULL && argv0[0] != '\0', NULL);
681 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
683 args = g_ptr_array_new ();
685 va_start (ap, argv0);
686 g_ptr_array_add (args, (gchar *) argv0);
687 while ((arg = va_arg (ap, const gchar *)))
688 g_ptr_array_add (args, (gchar *) arg);
690 result = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
692 g_ptr_array_free (args, TRUE);
699 * g_subprocess_launcher_spawnv:
700 * @self: a #GSubprocessLauncher
701 * @argv: Command line arguments
704 * A convenience helper for creating a #GSubprocess given a provided
705 * array of arguments.
708 * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
711 g_subprocess_launcher_spawnv (GSubprocessLauncher *launcher,
712 const gchar * const *argv,
715 GSubprocess *subprocess;
717 g_return_val_if_fail (argv != NULL && argv[0] != NULL && argv[0][0] != '\0', NULL);
719 subprocess = g_object_new (G_TYPE_SUBPROCESS,
721 "flags", launcher->flags,
723 g_subprocess_set_launcher (subprocess, launcher);
725 if (!g_initable_init (G_INITABLE (subprocess), NULL, error))
727 g_object_unref (subprocess);