df7ab0e048828927eabda523169ca4554ff64636
[platform/upstream/glib.git] / gio / gsubprocesslauncher.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2012 Red Hat, Inc.
4  * Copyright © 2012-2013 Canonical Limited
5  *
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.
10  *
11  * See the included COPYING file for more information.
12  *
13  * Authors: Colin Walters <walters@verbum.org>
14  *          Ryan Lortie <desrt@desrt.ca>
15  */
16
17 /**
18  * SECTION:gsubprocess
19  * @title: GSubprocess Launcher
20  * @short_description: Environment options for launching a child process
21  *
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.
25  *
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.
30  *
31  * Since: 2.40
32  */
33
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)
41
42 #include "config.h"
43
44 #include "gsubprocesslauncher-private.h"
45 #include "gioenumtypes.h"
46 #include "gsubprocess.h"
47 #include "ginitable.h"
48
49 #ifdef G_OS_UNIX
50 #include <unistd.h>
51 #include <fcntl.h>
52 #endif
53
54 typedef GObjectClass GSubprocessLauncherClass;
55
56 G_DEFINE_TYPE (GSubprocessLauncher, g_subprocess_launcher, G_TYPE_OBJECT);
57
58 static gboolean
59 verify_disposition (const gchar      *stream_name,
60                     GSubprocessFlags  filtered_flags,
61                     gint              fd,
62                     const gchar      *filename)
63 {
64   guint n_bits;
65
66   if (!filtered_flags)
67     n_bits = 0;
68   else if (((filtered_flags - 1) & filtered_flags) == 0)
69     n_bits = 1;
70   else
71     n_bits = 2; /* ...or more */
72
73   if (n_bits + (fd >= 0) + (filename != NULL) > 1)
74     {
75       GString *err;
76
77       err = g_string_new (NULL);
78       if (n_bits)
79         {
80           GFlagsClass *class;
81           GFlagsValue *value;
82
83           class = g_type_class_peek (G_TYPE_SUBPROCESS_FLAGS);
84           while ((value = g_flags_get_first_value (class, filtered_flags)))
85             {
86               g_string_append_printf (err, " %s", value->value_name);
87               filtered_flags &= value->value;
88             }
89
90           g_type_class_unref (class);
91         }
92
93       if (fd >= 0)
94         g_string_append_printf (err, " g_subprocess_launcher_take_%s_fd()", stream_name);
95
96       if (filename)
97         g_string_append_printf (err, " g_subprocess_launcher_set_%s_file_path()", stream_name);
98
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);
102
103       return FALSE;
104     }
105
106   return TRUE;
107 }
108
109 static gboolean
110 verify_flags (GSubprocessFlags flags)
111 {
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);
115 }
116
117 static void
118 g_subprocess_launcher_set_property (GObject *object, guint prop_id,
119                                     const GValue *value, GParamSpec *pspec)
120 {
121   GSubprocessLauncher *launcher = G_SUBPROCESS_LAUNCHER (object);
122
123   g_assert (prop_id == 1);
124
125   if (verify_flags (g_value_get_flags (value)))
126     launcher->flags = g_value_get_flags (value);
127 }
128
129 static void
130 g_subprocess_launcher_finalize (GObject *object)
131 {
132   GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object);
133   guint i;
134
135   g_strfreev (self->envp);
136   g_free (self->cwd);
137
138 #ifdef G_OS_UNIX
139   g_free (self->stdin_path);
140   g_free (self->stdout_path);
141   g_free (self->stderr_path);
142
143   if (self->stdin_fd != -1)
144     close (self->stdin_fd);
145
146   if (self->stdout_fd != -1)
147     close (self->stdout_fd);
148
149   if (self->stderr_fd != -1)
150     close (self->stderr_fd);
151
152   if (self->basic_fd_assignments)
153     {
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);
157     }
158   if (self->needdup_fd_assignments)
159     {
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);
163     }
164 #endif
165
166   if (self->child_setup_destroy_notify)
167     (* self->child_setup_destroy_notify) (self->child_setup_user_data);
168
169   G_OBJECT_CLASS (g_subprocess_launcher_parent_class)->finalize (object);
170 }
171
172 static void
173 g_subprocess_launcher_init (GSubprocessLauncher  *self)
174 {
175   self->envp = g_listenv ();
176
177   self->stdin_fd = -1;
178   self->stdout_fd = -1;
179   self->stderr_fd = -1;
180 #ifdef G_OS_UNIX
181   self->basic_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
182   self->needdup_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
183 #endif
184 }
185
186 static void
187 g_subprocess_launcher_class_init (GSubprocessLauncherClass *class)
188 {
189   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
190
191   gobject_class->set_property = g_subprocess_launcher_set_property;
192   gobject_class->finalize = g_subprocess_launcher_finalize;
193
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));
198 }
199
200 /**
201  * g_subprocess_launcher_new:
202  *
203  * Creates a new #GSubprocessLauncher.
204  *
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.
208  *
209  * Since: 2.40
210  **/
211 GSubprocessLauncher *
212 g_subprocess_launcher_new (GSubprocessFlags flags)
213 {
214   if (!verify_flags (flags))
215     return NULL;
216
217   return g_object_new (G_TYPE_SUBPROCESS_LAUNCHER,
218                        "flags", flags,
219                        NULL);
220 }
221
222 /**
223  * g_subprocess_launcher_set_environ:
224  * @self: a #GSubprocess
225  * @environ: the replacement environment
226  *
227  * Replace the entire environment of processes launched from this
228  * launcher with the given 'environ' variable.
229  *
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.
233  *
234  * As an alternative, you can use g_subprocess_launcher_setenv(),
235  * g_subprocess_launcher_unsetenv(), etc.
236  *
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.
240  *
241  * Since: 2.40
242  **/
243 void
244 g_subprocess_launcher_set_environ (GSubprocessLauncher  *self,
245                                    gchar               **environ)
246 {
247   g_strfreev (self->envp);
248   self->envp = g_strdupv (environ);
249 }
250
251 /**
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
257  *
258  * Sets the environment variable @variable in the environment of
259  * processes launched from this launcher.
260  *
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.
264  *
265  *
266  * Since: 2.40
267  **/
268 void
269 g_subprocess_launcher_setenv (GSubprocessLauncher *self,
270                               const gchar         *variable,
271                               const gchar         *value,
272                               gboolean             overwrite)
273 {
274   self->envp = g_environ_setenv (self->envp, variable, value, overwrite);
275 }
276
277 /**
278  * g_subprocess_launcher_unsetsenv:
279  * @self: a #GSubprocess
280  * @variable: the environment variable to unset, must not contain '='
281  *
282  * Removes the environment variable @variable from the environment of
283  * processes launched from this launcher.
284  *
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.
288  *
289  * Since: 2.40
290  **/
291 void
292 g_subprocess_launcher_unsetenv (GSubprocessLauncher *self,
293                                 const gchar         *variable)
294 {
295   self->envp = g_environ_unsetenv (self->envp, variable);
296 }
297
298 /**
299  * g_subprocess_launcher_getenv:
300  * @self: a #GSubprocess
301  * @variable: the environment variable to get
302  *
303  * Returns the value of the environment variable @variable in the
304  * environment of processes launched from this launcher.
305  *
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
308  * be UTF-8.
309  *
310  * Returns: the value of the environment variable, %NULL if unset
311  *
312  * Since: 2.40
313  **/
314 const gchar *
315 g_subprocess_launcher_getenv (GSubprocessLauncher *self,
316                               const gchar         *variable)
317 {
318   return g_environ_getenv (self->envp, variable);
319 }
320
321 /**
322  * g_subprocess_launcher_set_cwd:
323  * @self: a #GSubprocess
324  * @cwd: the cwd for launched processes
325  *
326  * Sets the current working directory that processes will be launched
327  * with.
328  *
329  * By default processes are launched with the current working directory
330  * of the launching process at the time of launch.
331  *
332  * Since: 2.40
333  **/
334 void
335 g_subprocess_launcher_set_cwd (GSubprocessLauncher *self,
336                                const gchar         *cwd)
337 {
338   g_free (self->cwd);
339   self->cwd = g_strdup (cwd);
340 }
341
342 /**
343  * g_subprocess_launcher_set_flags:
344  * @self: a #GSubprocessLauncher
345  * @flags: #GSubprocessFlags
346  *
347  * Sets the flags on the launcher.
348  *
349  * The default flags are %G_SUBPROCESS_FLAGS_NONE.
350  *
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).
355  *
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().
359  *
360  * Since: 2.40
361  **/
362 void
363 g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
364                                  GSubprocessFlags     flags)
365 {
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))
369     self->flags = flags;
370 }
371
372 #ifdef G_OS_UNIX
373 static void
374 assign_fd (gint *fd_ptr, gint fd)
375 {
376   gint flags;
377
378   if (*fd_ptr != -1)
379     close (*fd_ptr);
380
381   *fd_ptr = fd;
382
383   if (fd != -1)
384     {
385       /* best effort */
386       flags = fcntl (fd, F_GETFD);
387       if (~flags & FD_CLOEXEC)
388         fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
389     }
390 }
391
392 /**
393  * g_subprocess_launcher_set_stdin_file_path:
394  * @self: a #GSubprocessLauncher
395  * @path: a filename or %NULL
396  *
397  * Sets the file path to use as the stdin for spawned processes.
398  *
399  * If @path is %NULL then any previously given path is unset.
400  *
401  * The file must exist or spawning the process will fail.
402  *
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.
405  *
406  * This feature is only available on UNIX.
407  *
408  * Since: 2.40
409  **/
410 void
411 g_subprocess_launcher_set_stdin_file_path (GSubprocessLauncher *self,
412                                            const gchar         *path)
413 {
414   if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, self->stdin_fd, path))
415     {
416       g_free (self->stdin_path);
417       self->stdin_path = g_strdup (path);
418     }
419 }
420
421 /**
422  * g_subprocess_launcher_take_stdin_fd:
423  * @self: a #GSubprocessLauncher
424  * @fd: a file descriptor, or -1
425  *
426  * Sets the file descriptor to use as the stdin for spawned processes.
427  *
428  * If @fd is -1 then any previously given fd is unset.
429  *
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.
433  *
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.
439  *
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.
442  *
443  * This feature is only available on UNIX.
444  *
445  * Since: 2.40
446  **/
447 void
448 g_subprocess_launcher_take_stdin_fd (GSubprocessLauncher *self,
449                                      gint                 fd)
450 {
451   if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, fd, self->stdin_path))
452     assign_fd (&self->stdin_fd, fd);
453 }
454
455 /**
456  * g_subprocess_launcher_set_stdout_file_path:
457  * @self: a #GSubprocessLauncher
458  * @path: a filename or %NULL
459  *
460  * Sets the file path to use as the stdout for spawned processes.
461  *
462  * If @path is %NULL then any previously given path is unset.
463  *
464  * The file will be created or truncated when the process is spawned, as
465  * would be the case if using '>' at the shell.
466  *
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.
469  *
470  * This feature is only available on UNIX.
471  *
472  * Since: 2.40
473  **/
474 void
475 g_subprocess_launcher_set_stdout_file_path (GSubprocessLauncher *self,
476                                             const gchar         *path)
477 {
478   if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, self->stdout_fd, path))
479     {
480       g_free (self->stdout_path);
481       self->stdout_path = g_strdup (path);
482     }
483 }
484
485 /**
486  * g_subprocess_launcher_take_stdout_fd:
487  * @self: a #GSubprocessLauncher
488  * @fd: a file descriptor, or -1
489  *
490  * Sets the file descriptor to use as the stdout for spawned processes.
491  *
492  * If @fd is -1 then any previously given fd is unset.
493  *
494  * Note that the default behaviour is to pass stdout through to the
495  * stdout of the parent process.
496  *
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.
502  *
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.
505  *
506  * This feature is only available on UNIX.
507  *
508  * Since: 2.40
509  **/
510 void
511 g_subprocess_launcher_take_stdout_fd (GSubprocessLauncher *self,
512                                       gint                 fd)
513 {
514   if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, fd, self->stdout_path))
515     assign_fd (&self->stdout_fd, fd);
516 }
517
518 /**
519  * g_subprocess_launcher_set_stderr_file_path:
520  * @self: a #GSubprocessLauncher
521  * @path: a filename or %NULL
522  *
523  * Sets the file path to use as the stderr for spawned processes.
524  *
525  * If @path is %NULL then any previously given path is unset.
526  *
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.
529  *
530  * If you want to send both stdout and stderr to the same file then use
531  * %G_SUBPROCESS_FLAGS_STDERR_MERGE.
532  *
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.
535  *
536  * This feature is only available on UNIX.
537  *
538  * Since: 2.40
539  **/
540 void
541 g_subprocess_launcher_set_stderr_file_path (GSubprocessLauncher *self,
542                                             const gchar         *path)
543 {
544   if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, self->stderr_fd, path))
545     {
546       g_free (self->stderr_path);
547       self->stderr_path = g_strdup (path);
548     }
549 }
550
551 /**
552  * g_subprocess_launcher_take_stderr_fd:
553  * @self: a #GSubprocessLauncher
554  * @fd: a file descriptor, or -1
555  *
556  * Sets the file descriptor to use as the stderr for spawned processes.
557  *
558  * If @fd is -1 then any previously given fd is unset.
559  *
560  * Note that the default behaviour is to pass stderr through to the
561  * stderr of the parent process.
562  *
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
566  * spawned process.
567  *
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.
570  *
571  * This feature is only available on UNIX.
572  *
573  * Since: 2.40
574  **/
575 void
576 g_subprocess_launcher_take_stderr_fd (GSubprocessLauncher *self,
577                                      gint                 fd)
578 {
579   if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, fd, self->stderr_path))
580     assign_fd (&self->stderr_fd, fd);
581 }
582
583 /**
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
588  *
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.
592  *
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.
597  *
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.
601  */
602 void
603 g_subprocess_launcher_take_fd (GSubprocessLauncher   *self,
604                                gint                   source_fd,
605                                gint                   target_fd)
606 {
607   if (source_fd == target_fd)
608     {
609       g_array_append_val (self->basic_fd_assignments, source_fd);
610     }
611   else
612     {
613       g_array_append_val (self->needdup_fd_assignments, source_fd);
614       g_array_append_val (self->needdup_fd_assignments, target_fd);
615     }
616 }
617
618 /**
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
624  *
625  * Sets up a child setup function.
626  *
627  * The child setup function will be called after fork() but before
628  * exec() on the child's side.
629  *
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
633  * given.
634  *
635  * %NULL can be given as @child_setup to disable the functionality.
636  *
637  * Child setup functions are only available on UNIX.
638  *
639  * Since: 2.40
640  **/
641 void
642 g_subprocess_launcher_set_child_setup (GSubprocessLauncher  *self,
643                                        GSpawnChildSetupFunc  child_setup,
644                                        gpointer              user_data,
645                                        GDestroyNotify        destroy_notify)
646 {
647   if (self->child_setup_destroy_notify)
648     (* self->child_setup_destroy_notify) (self->child_setup_user_data);
649
650   self->child_setup_func = child_setup;
651   self->child_setup_user_data = user_data;
652   self->child_setup_destroy_notify = destroy_notify;
653 }
654 #endif
655
656 /**
657  * g_subprocess_launcher_spawn:
658  * @self: a #GSubprocessLauncher
659  * @error: Error
660  * @argv0: Command line arguments
661  * @...: Continued arguments, %NULL terminated
662  *
663  * A convenience helper for creating a #GSubprocess given a provided
664  * varargs list of arguments.
665  *
666  * Since: 2.40
667  * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
668  **/
669 GSubprocess *
670 g_subprocess_launcher_spawn (GSubprocessLauncher  *launcher,
671                              GError              **error,
672                              const gchar          *argv0,
673                              ...)
674 {
675   GSubprocess *result;
676   GPtrArray *args;
677   const gchar *arg;
678   va_list ap;
679
680   g_return_val_if_fail (argv0 != NULL && argv0[0] != '\0', NULL);
681   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
682
683   args = g_ptr_array_new ();
684
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);
689
690   result = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
691
692   g_ptr_array_free (args, TRUE);
693
694   return result;
695
696 }
697
698 /**
699  * g_subprocess_launcher_spawnv:
700  * @self: a #GSubprocessLauncher
701  * @argv: Command line arguments
702  * @error: Error
703  *
704  * A convenience helper for creating a #GSubprocess given a provided
705  * array of arguments.
706  *
707  * Since: 2.40
708  * Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
709  **/
710 GSubprocess *
711 g_subprocess_launcher_spawnv (GSubprocessLauncher  *launcher,
712                               const gchar * const  *argv,
713                               GError              **error)
714 {
715   GSubprocess *subprocess;
716
717   g_return_val_if_fail (argv != NULL && argv[0] != NULL && argv[0][0] != '\0', NULL);
718
719   subprocess = g_object_new (G_TYPE_SUBPROCESS,
720                              "argv", argv,
721                              "flags", launcher->flags,
722                              NULL);
723   g_subprocess_set_launcher (subprocess, launcher);
724
725   if (!g_initable_init (G_INITABLE (subprocess), NULL, error))
726     {
727       g_object_unref (subprocess);
728       return NULL;
729     }
730
731   return subprocess;
732 }