Coverage Report

Created: 2025-08-29 06:14

/src/tinysparql/subprojects/glib-2.80.3/glib/gspawn.c
Line
Count
Source (jump to first uncovered line)
1
/* gspawn.c - Process launching
2
 *
3
 *  Copyright 2000 Red Hat, Inc.
4
 *  g_execvpe implementation based on GNU libc execvp:
5
 *   Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
6
 *
7
 * SPDX-License-Identifier: LGPL-2.1-or-later
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public License
20
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "config.h"
24
25
#include <sys/time.h>
26
#include <sys/types.h>
27
#include <sys/wait.h>
28
#include <unistd.h>
29
#include <errno.h>
30
#include <fcntl.h>
31
#include <signal.h>
32
#include <string.h>
33
#include <stdlib.h>   /* for fdwalk */
34
#include <dirent.h>
35
#include <unistd.h>
36
37
#ifdef HAVE_SPAWN_H
38
#include <spawn.h>
39
#endif /* HAVE_SPAWN_H */
40
41
#ifdef HAVE_CRT_EXTERNS_H
42
#include <crt_externs.h> /* for _NSGetEnviron */
43
#endif
44
45
#ifdef HAVE_SYS_SELECT_H
46
#include <sys/select.h>
47
#endif /* HAVE_SYS_SELECT_H */
48
49
#ifdef HAVE_SYS_RESOURCE_H
50
#include <sys/resource.h>
51
#endif /* HAVE_SYS_RESOURCE_H */
52
53
#if defined(__linux__) || defined(__DragonFly__)
54
#include <sys/syscall.h>  /* for syscall and SYS_getdents64 */
55
#endif
56
57
#include "gspawn.h"
58
#include "gspawn-private.h"
59
#include "gthread.h"
60
#include "gtrace-private.h"
61
#include "glib/gstdio.h"
62
63
#include "genviron.h"
64
#include "gmem.h"
65
#include "gshell.h"
66
#include "gstring.h"
67
#include "gstrfuncs.h"
68
#include "gtestutils.h"
69
#include "gutils.h"
70
#include "glibintl.h"
71
#include "glib-unix.h"
72
73
#if defined(__APPLE__) && defined(HAVE_LIBPROC_H)
74
#include <libproc.h>
75
#include <sys/proc_info.h>
76
#endif
77
78
0
#define INHERITS_OR_NULL_STDIN  (G_SPAWN_STDIN_FROM_DEV_NULL | G_SPAWN_CHILD_INHERITS_STDIN)
79
0
#define INHERITS_OR_NULL_STDOUT (G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_CHILD_INHERITS_STDOUT)
80
0
#define INHERITS_OR_NULL_STDERR (G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_CHILD_INHERITS_STDERR)
81
82
0
#define IS_STD_FILENO(_fd) ((_fd >= STDIN_FILENO) && (_fd <= STDERR_FILENO))
83
0
#define IS_VALID_FILENO(_fd) (_fd >= 0)
84
85
/* posix_spawn() is assumed the fastest way to spawn, but glibc's
86
 * implementation was buggy before glibc 2.24, so avoid it on old versions.
87
 */
88
#ifdef HAVE_POSIX_SPAWN
89
#ifdef __GLIBC__
90
91
#if __GLIBC_PREREQ(2,24)
92
#define POSIX_SPAWN_AVAILABLE
93
#endif
94
95
#else /* !__GLIBC__ */
96
/* Assume that all non-glibc posix_spawn implementations are fine. */
97
#define POSIX_SPAWN_AVAILABLE
98
#endif /* __GLIBC__ */
99
#endif /* HAVE_POSIX_SPAWN */
100
101
#ifdef HAVE__NSGETENVIRON
102
#define environ (*_NSGetEnviron())
103
#else
104
extern char **environ;
105
#endif
106
107
#ifndef O_CLOEXEC
108
#define O_CLOEXEC 0
109
#else
110
#define HAVE_O_CLOEXEC 1
111
#endif
112
113
static gint g_execute (const gchar  *file,
114
                       gchar       **argv,
115
                       gchar       **argv_buffer,
116
                       gsize         argv_buffer_len,
117
                       gchar       **envp,
118
                       const gchar  *search_path,
119
                       gchar        *search_path_buffer,
120
                       gsize         search_path_buffer_len);
121
122
static gboolean fork_exec (gboolean              intermediate_child,
123
                           const gchar          *working_directory,
124
                           const gchar * const  *argv,
125
                           const gchar * const  *envp,
126
                           gboolean              close_descriptors,
127
                           gboolean              search_path,
128
                           gboolean              search_path_from_envp,
129
                           gboolean              stdout_to_null,
130
                           gboolean              stderr_to_null,
131
                           gboolean              child_inherits_stdin,
132
                           gboolean              file_and_argv_zero,
133
                           gboolean              cloexec_pipes,
134
                           GSpawnChildSetupFunc  child_setup,
135
                           gpointer              user_data,
136
                           GPid                 *child_pid,
137
                           gint                 *stdin_pipe_out,
138
                           gint                 *stdout_pipe_out,
139
                           gint                 *stderr_pipe_out,
140
                           gint                  stdin_fd,
141
                           gint                  stdout_fd,
142
                           gint                  stderr_fd,
143
                           const gint           *source_fds,
144
                           const gint           *target_fds,
145
                           gsize                 n_fds,
146
                           GError              **error);
147
148
G_DEFINE_QUARK (g-exec-error-quark, g_spawn_error)
149
G_DEFINE_QUARK (g-spawn-exit-error-quark, g_spawn_exit_error)
150
151
/**
152
 * g_spawn_async:
153
 * @working_directory: (type filename) (nullable): child's current working
154
 *     directory, or %NULL to inherit parent's
155
 * @argv: (array zero-terminated=1) (element-type filename):
156
 *     child's argument vector
157
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
158
 *     child's environment, or %NULL to inherit parent's
159
 * @flags: flags from #GSpawnFlags
160
 * @child_setup: (scope async) (closure user_data) (nullable): function to run
161
 *     in the child just before `exec()`
162
 * @user_data: user data for @child_setup
163
 * @child_pid: (out) (optional): return location for child process reference, or %NULL
164
 * @error: return location for error
165
 *
166
 * Executes a child program asynchronously.
167
 * 
168
 * See g_spawn_async_with_pipes() for a full description; this function
169
 * simply calls the g_spawn_async_with_pipes() without any pipes.
170
 *
171
 * You should call g_spawn_close_pid() on the returned child process
172
 * reference when you don't need it any more.
173
 * 
174
 * If you are writing a GTK application, and the program you are spawning is a
175
 * graphical application too, then to ensure that the spawned program opens its
176
 * windows on the right screen, you may want to use #GdkAppLaunchContext,
177
 * #GAppLaunchContext, or set the %DISPLAY environment variable.
178
 *
179
 * Note that the returned @child_pid on Windows is a handle to the child
180
 * process and not its identifier. Process handles and process identifiers
181
 * are different concepts on Windows.
182
 *
183
 * Returns: %TRUE on success, %FALSE if error is set
184
 **/
185
gboolean
186
g_spawn_async (const gchar          *working_directory,
187
               gchar               **argv,
188
               gchar               **envp,
189
               GSpawnFlags           flags,
190
               GSpawnChildSetupFunc  child_setup,
191
               gpointer              user_data,
192
               GPid                 *child_pid,
193
               GError              **error)
194
0
{
195
0
  return g_spawn_async_with_pipes (working_directory,
196
0
                                   argv, envp,
197
0
                                   flags,
198
0
                                   child_setup,
199
0
                                   user_data,
200
0
                                   child_pid,
201
0
                                   NULL, NULL, NULL,
202
0
                                   error);
203
0
}
204
205
/* Some versions of OS X define READ_OK in public headers */
206
#undef READ_OK
207
208
typedef enum
209
{
210
  READ_FAILED = 0, /* FALSE */
211
  READ_OK,
212
  READ_EOF
213
} ReadResult;
214
215
static ReadResult
216
read_data (GString *str,
217
           gint     fd,
218
           GError **error)
219
0
{
220
0
  gssize bytes;
221
0
  gchar buf[4096];
222
223
0
 again:
224
0
  bytes = read (fd, buf, 4096);
225
226
0
  if (bytes == 0)
227
0
    return READ_EOF;
228
0
  else if (bytes > 0)
229
0
    {
230
0
      g_string_append_len (str, buf, bytes);
231
0
      return READ_OK;
232
0
    }
233
0
  else if (errno == EINTR)
234
0
    goto again;
235
0
  else
236
0
    {
237
0
      int errsv = errno;
238
239
0
      g_set_error (error,
240
0
                   G_SPAWN_ERROR,
241
0
                   G_SPAWN_ERROR_READ,
242
0
                   _("Failed to read data from child process (%s)"),
243
0
                   g_strerror (errsv));
244
245
0
      return READ_FAILED;
246
0
    }
247
0
}
248
249
/**
250
 * g_spawn_sync:
251
 * @working_directory: (type filename) (nullable): child's current working
252
 *     directory, or %NULL to inherit parent's
253
 * @argv: (array zero-terminated=1) (element-type filename):
254
 *     child's argument vector, which must be non-empty and %NULL-terminated
255
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
256
 *     child's environment, or %NULL to inherit parent's
257
 * @flags: flags from #GSpawnFlags
258
 * @child_setup: (scope call) (closure user_data) (nullable): function to run
259
 *     in the child just before `exec()`
260
 * @user_data: user data for @child_setup
261
 * @standard_output: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child output, or %NULL
262
 * @standard_error: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child error messages, or %NULL
263
 * @wait_status: (out) (optional): return location for child wait status, as returned by waitpid(), or %NULL
264
 * @error: return location for error, or %NULL
265
 *
266
 * Executes a child synchronously (waits for the child to exit before returning).
267
 *
268
 * All output from the child is stored in @standard_output and @standard_error,
269
 * if those parameters are non-%NULL. Note that you must set the  
270
 * %G_SPAWN_STDOUT_TO_DEV_NULL and %G_SPAWN_STDERR_TO_DEV_NULL flags when
271
 * passing %NULL for @standard_output and @standard_error.
272
 *
273
 * If @wait_status is non-%NULL, the platform-specific status of
274
 * the child is stored there; see the documentation of
275
 * g_spawn_check_wait_status() for how to use and interpret this.
276
 * On Unix platforms, note that it is usually not equal
277
 * to the integer passed to `exit()` or returned from `main()`.
278
 *
279
 * Note that it is invalid to pass %G_SPAWN_DO_NOT_REAP_CHILD in
280
 * @flags, and on POSIX platforms, the same restrictions as for
281
 * g_child_watch_source_new() apply.
282
 *
283
 * If an error occurs, no data is returned in @standard_output,
284
 * @standard_error, or @wait_status.
285
 *
286
 * This function calls g_spawn_async_with_pipes() internally; see that
287
 * function for full details on the other parameters and details on
288
 * how these functions work on Windows.
289
 * 
290
 * Returns: %TRUE on success, %FALSE if an error was set
291
 */
292
gboolean
293
g_spawn_sync (const gchar          *working_directory,
294
              gchar               **argv,
295
              gchar               **envp,
296
              GSpawnFlags           flags,
297
              GSpawnChildSetupFunc  child_setup,
298
              gpointer              user_data,
299
              gchar               **standard_output,
300
              gchar               **standard_error,
301
              gint                 *wait_status,
302
              GError              **error)     
303
0
{
304
0
  gint outpipe = -1;
305
0
  gint errpipe = -1;
306
0
  GPid pid;
307
0
  gint ret;
308
0
  GString *outstr = NULL;
309
0
  GString *errstr = NULL;
310
0
  gboolean failed;
311
0
  gint status;
312
  
313
0
  g_return_val_if_fail (argv != NULL, FALSE);
314
0
  g_return_val_if_fail (argv[0] != NULL, FALSE);
315
0
  g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
316
0
  g_return_val_if_fail (standard_output == NULL ||
317
0
                        !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
318
0
  g_return_val_if_fail (standard_error == NULL ||
319
0
                        !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
320
  
321
  /* Just to ensure segfaults if callers try to use
322
   * these when an error is reported.
323
   */
324
0
  if (standard_output)
325
0
    *standard_output = NULL;
326
327
0
  if (standard_error)
328
0
    *standard_error = NULL;
329
  
330
0
  if (!fork_exec (FALSE,
331
0
                  working_directory,
332
0
                  (const gchar * const *) argv,
333
0
                  (const gchar * const *) envp,
334
0
                  !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
335
0
                  (flags & G_SPAWN_SEARCH_PATH) != 0,
336
0
                  (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
337
0
                  (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
338
0
                  (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
339
0
                  (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
340
0
                  (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
341
0
                  (flags & G_SPAWN_CLOEXEC_PIPES) != 0,
342
0
                  child_setup,
343
0
                  user_data,
344
0
                  &pid,
345
0
                  NULL,
346
0
                  standard_output ? &outpipe : NULL,
347
0
                  standard_error ? &errpipe : NULL,
348
0
                  -1, -1, -1,
349
0
                  NULL, NULL, 0,
350
0
                  error))
351
0
    return FALSE;
352
353
  /* Read data from child. */
354
  
355
0
  failed = FALSE;
356
357
0
  if (outpipe >= 0)
358
0
    {
359
0
      outstr = g_string_new (NULL);
360
0
    }
361
      
362
0
  if (errpipe >= 0)
363
0
    {
364
0
      errstr = g_string_new (NULL);
365
0
    }
366
367
  /* Read data until we get EOF on both pipes. */
368
0
  while (!failed &&
369
0
         (outpipe >= 0 ||
370
0
          errpipe >= 0))
371
0
    {
372
      /* Any negative FD in the array is ignored, so we can use a fixed length.
373
       * We can use UNIX FDs here without worrying about Windows HANDLEs because
374
       * the Windows implementation is entirely in gspawn-win32.c. */
375
0
      GPollFD fds[] =
376
0
        {
377
0
          { outpipe, G_IO_IN | G_IO_HUP | G_IO_ERR, 0 },
378
0
          { errpipe, G_IO_IN | G_IO_HUP | G_IO_ERR, 0 },
379
0
        };
380
381
0
      ret = g_poll (fds, G_N_ELEMENTS (fds), -1  /* no timeout */);
382
383
0
      if (ret < 0)
384
0
        {
385
0
          int errsv = errno;
386
387
0
    if (errno == EINTR)
388
0
      continue;
389
390
0
          failed = TRUE;
391
392
0
          g_set_error (error,
393
0
                       G_SPAWN_ERROR,
394
0
                       G_SPAWN_ERROR_READ,
395
0
                       _("Unexpected error in reading data from a child process (%s)"),
396
0
                       g_strerror (errsv));
397
              
398
0
          break;
399
0
        }
400
401
0
      if (outpipe >= 0 && fds[0].revents != 0)
402
0
        {
403
0
          switch (read_data (outstr, outpipe, error))
404
0
            {
405
0
            case READ_FAILED:
406
0
              failed = TRUE;
407
0
              break;
408
0
            case READ_EOF:
409
0
              g_clear_fd (&outpipe, NULL);
410
0
              break;
411
0
            default:
412
0
              break;
413
0
            }
414
415
0
          if (failed)
416
0
            break;
417
0
        }
418
419
0
      if (errpipe >= 0 && fds[1].revents != 0)
420
0
        {
421
0
          switch (read_data (errstr, errpipe, error))
422
0
            {
423
0
            case READ_FAILED:
424
0
              failed = TRUE;
425
0
              break;
426
0
            case READ_EOF:
427
0
              g_clear_fd (&errpipe, NULL);
428
0
              break;
429
0
            default:
430
0
              break;
431
0
            }
432
433
0
          if (failed)
434
0
            break;
435
0
        }
436
0
    }
437
438
  /* These should only be open still if we had an error.  */
439
0
  g_clear_fd (&outpipe, NULL);
440
0
  g_clear_fd (&errpipe, NULL);
441
442
  /* Wait for child to exit, even if we have
443
   * an error pending.
444
   */
445
0
 again:
446
      
447
0
  ret = waitpid (pid, &status, 0);
448
449
0
  if (ret < 0)
450
0
    {
451
0
      if (errno == EINTR)
452
0
        goto again;
453
0
      else if (errno == ECHILD)
454
0
        {
455
0
          if (wait_status)
456
0
            {
457
0
              g_warning ("In call to g_spawn_sync(), wait status of a child process was requested but ECHILD was received by waitpid(). See the documentation of g_child_watch_source_new() for possible causes.");
458
0
            }
459
0
          else
460
0
            {
461
              /* We don't need the wait status. */
462
0
            }
463
0
        }
464
0
      else
465
0
        {
466
0
          if (!failed) /* avoid error pileups */
467
0
            {
468
0
              int errsv = errno;
469
470
0
              failed = TRUE;
471
                  
472
0
              g_set_error (error,
473
0
                           G_SPAWN_ERROR,
474
0
                           G_SPAWN_ERROR_READ,
475
0
                           _("Unexpected error in waitpid() (%s)"),
476
0
                           g_strerror (errsv));
477
0
            }
478
0
        }
479
0
    }
480
  
481
0
  if (failed)
482
0
    {
483
0
      if (outstr)
484
0
        g_string_free (outstr, TRUE);
485
0
      if (errstr)
486
0
        g_string_free (errstr, TRUE);
487
488
0
      return FALSE;
489
0
    }
490
0
  else
491
0
    {
492
0
      if (wait_status)
493
0
        *wait_status = status;
494
      
495
0
      if (standard_output)        
496
0
        *standard_output = g_string_free (outstr, FALSE);
497
498
0
      if (standard_error)
499
0
        *standard_error = g_string_free (errstr, FALSE);
500
501
0
      return TRUE;
502
0
    }
503
0
}
504
505
/**
506
 * g_spawn_async_with_pipes:
507
 * @working_directory: (type filename) (nullable): child's current working
508
 *     directory, or %NULL to inherit parent's, in the GLib file name encoding
509
 * @argv: (array zero-terminated=1) (element-type filename): child's argument
510
 *     vector, in the GLib file name encoding; it must be non-empty and %NULL-terminated
511
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
512
 *     child's environment, or %NULL to inherit parent's, in the GLib file
513
 *     name encoding
514
 * @flags: flags from #GSpawnFlags
515
 * @child_setup: (scope async) (closure user_data) (nullable): function to run
516
 *     in the child just before `exec()`
517
 * @user_data: user data for @child_setup
518
 * @child_pid: (out) (optional): return location for child process ID, or %NULL
519
 * @standard_input: (out) (optional): return location for file descriptor to write to child's stdin, or %NULL
520
 * @standard_output: (out) (optional): return location for file descriptor to read child's stdout, or %NULL
521
 * @standard_error: (out) (optional): return location for file descriptor to read child's stderr, or %NULL
522
 * @error: return location for error
523
 *
524
 * Identical to g_spawn_async_with_pipes_and_fds() but with `n_fds` set to zero,
525
 * so no FD assignments are used.
526
 *
527
 * Returns: %TRUE on success, %FALSE if an error was set
528
 */
529
gboolean
530
g_spawn_async_with_pipes (const gchar          *working_directory,
531
                          gchar               **argv,
532
                          gchar               **envp,
533
                          GSpawnFlags           flags,
534
                          GSpawnChildSetupFunc  child_setup,
535
                          gpointer              user_data,
536
                          GPid                 *child_pid,
537
                          gint                 *standard_input,
538
                          gint                 *standard_output,
539
                          gint                 *standard_error,
540
                          GError              **error)
541
0
{
542
0
  return g_spawn_async_with_pipes_and_fds (working_directory,
543
0
                                           (const gchar * const *) argv,
544
0
                                           (const gchar * const *) envp,
545
0
                                           flags,
546
0
                                           child_setup, user_data,
547
0
                                           -1, -1, -1,
548
0
                                           NULL, NULL, 0,
549
0
                                           child_pid,
550
0
                                           standard_input,
551
0
                                           standard_output,
552
0
                                           standard_error,
553
0
                                           error);
554
0
}
555
556
/**
557
 * g_spawn_async_with_pipes_and_fds:
558
 * @working_directory: (type filename) (nullable): child's current working
559
 *     directory, or %NULL to inherit parent's, in the GLib file name encoding
560
 * @argv: (array zero-terminated=1) (element-type filename): child's argument
561
 *     vector, in the GLib file name encoding; it must be non-empty and %NULL-terminated
562
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
563
 *     child's environment, or %NULL to inherit parent's, in the GLib file
564
 *     name encoding
565
 * @flags: flags from #GSpawnFlags
566
 * @child_setup: (scope async) (closure user_data) (nullable): function to run
567
 *     in the child just before `exec()`
568
 * @user_data: user data for @child_setup
569
 * @stdin_fd: file descriptor to use for child's stdin, or `-1`
570
 * @stdout_fd: file descriptor to use for child's stdout, or `-1`
571
 * @stderr_fd: file descriptor to use for child's stderr, or `-1`
572
 * @source_fds: (array length=n_fds) (nullable): array of FDs from the parent
573
 *    process to make available in the child process
574
 * @target_fds: (array length=n_fds) (nullable): array of FDs to remap
575
 *    @source_fds to in the child process
576
 * @n_fds: number of FDs in @source_fds and @target_fds
577
 * @child_pid_out: (out) (optional): return location for child process ID, or %NULL
578
 * @stdin_pipe_out: (out) (optional): return location for file descriptor to write to child's stdin, or %NULL
579
 * @stdout_pipe_out: (out) (optional): return location for file descriptor to read child's stdout, or %NULL
580
 * @stderr_pipe_out: (out) (optional): return location for file descriptor to read child's stderr, or %NULL
581
 * @error: return location for error
582
 *
583
 * Executes a child program asynchronously (your program will not
584
 * block waiting for the child to exit).
585
 *
586
 * The child program is specified by the only argument that must be
587
 * provided, @argv. @argv should be a %NULL-terminated array of strings,
588
 * to be passed as the argument vector for the child. The first string
589
 * in @argv is of course the name of the program to execute. By default,
590
 * the name of the program must be a full path. If @flags contains the
591
 * %G_SPAWN_SEARCH_PATH flag, the `PATH` environment variable is used to
592
 * search for the executable. If @flags contains the
593
 * %G_SPAWN_SEARCH_PATH_FROM_ENVP flag, the `PATH` variable from @envp
594
 * is used to search for the executable. If both the
595
 * %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP flags are
596
 * set, the `PATH` variable from @envp takes precedence over the
597
 * environment variable.
598
 *
599
 * If the program name is not a full path and %G_SPAWN_SEARCH_PATH flag
600
 * is not used, then the program will be run from the current directory
601
 * (or @working_directory, if specified); this might be unexpected or even
602
 * dangerous in some cases when the current directory is world-writable.
603
 *
604
 * On Windows, note that all the string or string vector arguments to
605
 * this function and the other `g_spawn*()` functions are in UTF-8, the
606
 * GLib file name encoding. Unicode characters that are not part of
607
 * the system codepage passed in these arguments will be correctly
608
 * available in the spawned program only if it uses wide character API
609
 * to retrieve its command line. For C programs built with Microsoft's
610
 * tools it is enough to make the program have a `wmain()` instead of
611
 * `main()`. `wmain()` has a wide character argument vector as parameter.
612
 *
613
 * At least currently, mingw doesn't support `wmain()`, so if you use
614
 * mingw to develop the spawned program, it should call
615
 * g_win32_get_command_line() to get arguments in UTF-8.
616
 *
617
 * On Windows the low-level child process creation API `CreateProcess()`
618
 * doesn't use argument vectors, but a command line. The C runtime
619
 * library's `spawn*()` family of functions (which g_spawn_async_with_pipes()
620
 * eventually calls) paste the argument vector elements together into
621
 * a command line, and the C runtime startup code does a corresponding
622
 * reconstruction of an argument vector from the command line, to be
623
 * passed to `main()`. Complications arise when you have argument vector
624
 * elements that contain spaces or double quotes. The `spawn*()` functions
625
 * don't do any quoting or escaping, but on the other hand the startup
626
 * code does do unquoting and unescaping in order to enable receiving
627
 * arguments with embedded spaces or double quotes. To work around this
628
 * asymmetry, g_spawn_async_with_pipes() will do quoting and escaping on
629
 * argument vector elements that need it before calling the C runtime
630
 * `spawn()` function.
631
 *
632
 * The returned @child_pid on Windows is a handle to the child
633
 * process, not its identifier. Process handles and process
634
 * identifiers are different concepts on Windows.
635
 *
636
 * @envp is a %NULL-terminated array of strings, where each string
637
 * has the form `KEY=VALUE`. This will become the child's environment.
638
 * If @envp is %NULL, the child inherits its parent's environment.
639
 *
640
 * @flags should be the bitwise OR of any flags you want to affect the
641
 * function's behaviour. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
642
 * child will not automatically be reaped; you must use a child watch
643
 * (g_child_watch_add()) to be notified about the death of the child process,
644
 * otherwise it will stay around as a zombie process until this process exits.
645
 * Eventually you must call g_spawn_close_pid() on the @child_pid, in order to
646
 * free resources which may be associated with the child process. (On Unix,
647
 * using a child watch is equivalent to calling waitpid() or handling
648
 * the `SIGCHLD` signal manually. On Windows, calling g_spawn_close_pid()
649
 * is equivalent to calling `CloseHandle()` on the process handle returned
650
 * in @child_pid). See g_child_watch_add().
651
 *
652
 * Open UNIX file descriptors marked as `FD_CLOEXEC` will be automatically
653
 * closed in the child process. %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that
654
 * other open file descriptors will be inherited by the child; otherwise all
655
 * descriptors except stdin/stdout/stderr will be closed before calling `exec()`
656
 * in the child. %G_SPAWN_SEARCH_PATH means that @argv[0] need not be an
657
 * absolute path, it will be looked for in the `PATH` environment
658
 * variable. %G_SPAWN_SEARCH_PATH_FROM_ENVP means need not be an
659
 * absolute path, it will be looked for in the `PATH` variable from
660
 * @envp. If both %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP
661
 * are used, the value from @envp takes precedence over the environment.
662
 *
663
 * %G_SPAWN_CHILD_INHERITS_STDIN means that the child will inherit the parent's
664
 * standard input (by default, the child's standard input is attached to
665
 * `/dev/null`). %G_SPAWN_STDIN_FROM_DEV_NULL explicitly imposes the default
666
 * behavior. Both flags cannot be enabled at the same time and, in both cases,
667
 * the @stdin_pipe_out argument is ignored.
668
 *
669
 * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output
670
 * will be discarded (by default, it goes to the same location as the parent's
671
 * standard output). %G_SPAWN_CHILD_INHERITS_STDOUT explicitly imposes the
672
 * default behavior. Both flags cannot be enabled at the same time and, in
673
 * both cases, the @stdout_pipe_out argument is ignored.
674
 *
675
 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
676
 * will be discarded (by default, it goes to the same location as the parent's
677
 * standard error). %G_SPAWN_CHILD_INHERITS_STDERR explicitly imposes the
678
 * default behavior. Both flags cannot be enabled at the same time and, in
679
 * both cases, the @stderr_pipe_out argument is ignored.
680
 *
681
 * It is valid to pass the same FD in multiple parameters (e.g. you can pass
682
 * a single FD for both @stdout_fd and @stderr_fd, and include it in
683
 * @source_fds too).
684
 *
685
 * @source_fds and @target_fds allow zero or more FDs from this process to be
686
 * remapped to different FDs in the spawned process. If @n_fds is greater than
687
 * zero, @source_fds and @target_fds must both be non-%NULL and the same length.
688
 * Each FD in @source_fds is remapped to the FD number at the same index in
689
 * @target_fds. The source and target FD may be equal to simply propagate an FD
690
 * to the spawned process. FD remappings are processed after standard FDs, so
691
 * any target FDs which equal @stdin_fd, @stdout_fd or @stderr_fd will overwrite
692
 * them in the spawned process.
693
 *
694
 * @source_fds is supported on Windows since 2.72.
695
 *
696
 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
697
 * the file to execute, while the remaining elements are the actual
698
 * argument vector to pass to the file. Normally g_spawn_async_with_pipes()
699
 * uses @argv[0] as the file to execute, and passes all of @argv to the child.
700
 *
701
 * @child_setup and @user_data are a function and user data. On POSIX
702
 * platforms, the function is called in the child after GLib has
703
 * performed all the setup it plans to perform (including creating
704
 * pipes, closing file descriptors, etc.) but before calling `exec()`.
705
 * That is, @child_setup is called just before calling `exec()` in the
706
 * child. Obviously actions taken in this function will only affect
707
 * the child, not the parent.
708
 *
709
 * On Windows, there is no separate `fork()` and `exec()` functionality.
710
 * Child processes are created and run with a single API call,
711
 * `CreateProcess()`. There is no sensible thing @child_setup
712
 * could be used for on Windows so it is ignored and not called.
713
 *
714
 * If non-%NULL, @child_pid will on Unix be filled with the child's
715
 * process ID. You can use the process ID to send signals to the child,
716
 * or to use g_child_watch_add() (or `waitpid()`) if you specified the
717
 * %G_SPAWN_DO_NOT_REAP_CHILD flag. On Windows, @child_pid will be
718
 * filled with a handle to the child process only if you specified the
719
 * %G_SPAWN_DO_NOT_REAP_CHILD flag. You can then access the child
720
 * process using the Win32 API, for example wait for its termination
721
 * with the `WaitFor*()` functions, or examine its exit code with
722
 * `GetExitCodeProcess()`. You should close the handle with `CloseHandle()`
723
 * or g_spawn_close_pid() when you no longer need it.
724
 *
725
 * If non-%NULL, the @stdin_pipe_out, @stdout_pipe_out, @stderr_pipe_out
726
 * locations will be filled with file descriptors for writing to the child's
727
 * standard input or reading from its standard output or standard error.
728
 * The caller of g_spawn_async_with_pipes() must close these file descriptors
729
 * when they are no longer in use. If these parameters are %NULL, the
730
 * corresponding pipe won't be created.
731
 *
732
 * If @stdin_pipe_out is %NULL, the child's standard input is attached to
733
 * `/dev/null` unless %G_SPAWN_CHILD_INHERITS_STDIN is set.
734
 *
735
 * If @stderr_pipe_out is NULL, the child's standard error goes to the same
736
 * location as the parent's standard error unless %G_SPAWN_STDERR_TO_DEV_NULL
737
 * is set.
738
 *
739
 * If @stdout_pipe_out is NULL, the child's standard output goes to the same
740
 * location as the parent's standard output unless %G_SPAWN_STDOUT_TO_DEV_NULL
741
 * is set.
742
 *
743
 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
744
 * If an error is set, the function returns %FALSE. Errors are reported
745
 * even if they occur in the child (for example if the executable in
746
 * `@argv[0]` is not found). Typically the `message` field of returned
747
 * errors should be displayed to users. Possible errors are those from
748
 * the %G_SPAWN_ERROR domain.
749
 *
750
 * If an error occurs, @child_pid, @stdin_pipe_out, @stdout_pipe_out,
751
 * and @stderr_pipe_out will not be filled with valid values.
752
 *
753
 * If @child_pid is not %NULL and an error does not occur then the returned
754
 * process reference must be closed using g_spawn_close_pid().
755
 *
756
 * On modern UNIX platforms, GLib can use an efficient process launching
757
 * codepath driven internally by `posix_spawn()`. This has the advantage of
758
 * avoiding the fork-time performance costs of cloning the parent process
759
 * address space, and avoiding associated memory overcommit checks that are
760
 * not relevant in the context of immediately executing a distinct process.
761
 * This optimized codepath will be used provided that the following conditions
762
 * are met:
763
 *
764
 * 1. %G_SPAWN_DO_NOT_REAP_CHILD is set
765
 * 2. %G_SPAWN_LEAVE_DESCRIPTORS_OPEN is set
766
 * 3. %G_SPAWN_SEARCH_PATH_FROM_ENVP is not set
767
 * 4. @working_directory is %NULL
768
 * 5. @child_setup is %NULL
769
 * 6. The program is of a recognised binary format, or has a shebang.
770
 *    Otherwise, GLib will have to execute the program through the
771
 *    shell, which is not done using the optimized codepath.
772
 *
773
 * If you are writing a GTK application, and the program you are spawning is a
774
 * graphical application too, then to ensure that the spawned program opens its
775
 * windows on the right screen, you may want to use #GdkAppLaunchContext,
776
 * #GAppLaunchContext, or set the `DISPLAY` environment variable.
777
 *
778
 * Returns: %TRUE on success, %FALSE if an error was set
779
 *
780
 * Since: 2.68
781
 */
782
gboolean
783
g_spawn_async_with_pipes_and_fds (const gchar           *working_directory,
784
                                  const gchar * const   *argv,
785
                                  const gchar * const   *envp,
786
                                  GSpawnFlags            flags,
787
                                  GSpawnChildSetupFunc   child_setup,
788
                                  gpointer               user_data,
789
                                  gint                   stdin_fd,
790
                                  gint                   stdout_fd,
791
                                  gint                   stderr_fd,
792
                                  const gint            *source_fds,
793
                                  const gint            *target_fds,
794
                                  gsize                  n_fds,
795
                                  GPid                  *child_pid_out,
796
                                  gint                  *stdin_pipe_out,
797
                                  gint                  *stdout_pipe_out,
798
                                  gint                  *stderr_pipe_out,
799
                                  GError               **error)
800
0
{
801
0
  g_return_val_if_fail (argv != NULL, FALSE);
802
0
  g_return_val_if_fail (argv[0] != NULL, FALSE);
803
  /* can’t both inherit and set pipes to /dev/null */
804
0
  g_return_val_if_fail ((flags & INHERITS_OR_NULL_STDIN) != INHERITS_OR_NULL_STDIN, FALSE);
805
0
  g_return_val_if_fail ((flags & INHERITS_OR_NULL_STDOUT) != INHERITS_OR_NULL_STDOUT, FALSE);
806
0
  g_return_val_if_fail ((flags & INHERITS_OR_NULL_STDERR) != INHERITS_OR_NULL_STDERR, FALSE);
807
  /* can’t use pipes and stdin/stdout/stderr FDs */
808
0
  g_return_val_if_fail (stdin_pipe_out == NULL || stdin_fd < 0, FALSE);
809
0
  g_return_val_if_fail (stdout_pipe_out == NULL || stdout_fd < 0, FALSE);
810
0
  g_return_val_if_fail (stderr_pipe_out == NULL || stderr_fd < 0, FALSE);
811
812
0
  if ((flags & INHERITS_OR_NULL_STDIN) != 0)
813
0
    stdin_pipe_out = NULL;
814
0
  if ((flags & INHERITS_OR_NULL_STDOUT) != 0)
815
0
    stdout_pipe_out = NULL;
816
0
  if ((flags & INHERITS_OR_NULL_STDERR) != 0)
817
0
    stderr_pipe_out = NULL;
818
819
0
  return fork_exec (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
820
0
                    working_directory,
821
0
                    (const gchar * const *) argv,
822
0
                    (const gchar * const *) envp,
823
0
                    !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
824
0
                    (flags & G_SPAWN_SEARCH_PATH) != 0,
825
0
                    (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
826
0
                    (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
827
0
                    (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
828
0
                    (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
829
0
                    (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
830
0
                    (flags & G_SPAWN_CLOEXEC_PIPES) != 0,
831
0
                    child_setup,
832
0
                    user_data,
833
0
                    child_pid_out,
834
0
                    stdin_pipe_out,
835
0
                    stdout_pipe_out,
836
0
                    stderr_pipe_out,
837
0
                    stdin_fd,
838
0
                    stdout_fd,
839
0
                    stderr_fd,
840
0
                    source_fds,
841
0
                    target_fds,
842
0
                    n_fds,
843
0
                    error);
844
0
}
845
846
/**
847
 * g_spawn_async_with_fds:
848
 * @working_directory: (type filename) (nullable): child's current working directory, or %NULL to inherit parent's, in the GLib file name encoding
849
 * @argv: (array zero-terminated=1): child's argument vector, in the GLib file name encoding;
850
 *   it must be non-empty and %NULL-terminated
851
 * @envp: (array zero-terminated=1) (nullable): child's environment, or %NULL to inherit parent's, in the GLib file name encoding
852
 * @flags: flags from #GSpawnFlags
853
 * @child_setup: (scope async) (closure user_data) (nullable): function to run
854
 *   in the child just before `exec()`
855
 * @user_data: user data for @child_setup
856
 * @child_pid: (out) (optional): return location for child process ID, or %NULL
857
 * @stdin_fd: file descriptor to use for child's stdin, or `-1`
858
 * @stdout_fd: file descriptor to use for child's stdout, or `-1`
859
 * @stderr_fd: file descriptor to use for child's stderr, or `-1`
860
 * @error: return location for error
861
 *
862
 * Executes a child program asynchronously.
863
 *
864
 * Identical to g_spawn_async_with_pipes_and_fds() but with `n_fds` set to zero,
865
 * so no FD assignments are used.
866
 *
867
 * Returns: %TRUE on success, %FALSE if an error was set
868
 *
869
 * Since: 2.58
870
 */
871
gboolean
872
g_spawn_async_with_fds (const gchar          *working_directory,
873
                        gchar               **argv,
874
                        gchar               **envp,
875
                        GSpawnFlags           flags,
876
                        GSpawnChildSetupFunc  child_setup,
877
                        gpointer              user_data,
878
                        GPid                 *child_pid,
879
                        gint                  stdin_fd,
880
                        gint                  stdout_fd,
881
                        gint                  stderr_fd,
882
                        GError              **error)
883
0
{
884
0
  g_return_val_if_fail (argv != NULL, FALSE);
885
0
  g_return_val_if_fail (argv[0] != NULL, FALSE);
886
0
  g_return_val_if_fail (stdout_fd < 0 ||
887
0
                        !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
888
0
  g_return_val_if_fail (stderr_fd < 0 ||
889
0
                        !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
890
  /* can't inherit stdin if we have an input pipe. */
891
0
  g_return_val_if_fail (stdin_fd < 0 ||
892
0
                        !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
893
894
0
  return fork_exec (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
895
0
                    working_directory,
896
0
                    (const gchar * const *) argv,
897
0
                    (const gchar * const *) envp,
898
0
                    !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
899
0
                    (flags & G_SPAWN_SEARCH_PATH) != 0,
900
0
                    (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
901
0
                    (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
902
0
                    (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
903
0
                    (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
904
0
                    (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
905
0
                    (flags & G_SPAWN_CLOEXEC_PIPES) != 0,
906
0
                    child_setup,
907
0
                    user_data,
908
0
                    child_pid,
909
0
                    NULL, NULL, NULL,
910
0
                    stdin_fd,
911
0
                    stdout_fd,
912
0
                    stderr_fd,
913
0
                    NULL, NULL, 0,
914
0
                    error);
915
0
}
916
917
/**
918
 * g_spawn_command_line_sync:
919
 * @command_line: (type filename): a command line
920
 * @standard_output: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child output
921
 * @standard_error: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child errors
922
 * @wait_status: (out) (optional): return location for child wait status, as returned by waitpid()
923
 * @error: return location for errors
924
 *
925
 * A simple version of g_spawn_sync() with little-used parameters
926
 * removed, taking a command line instead of an argument vector.
927
 *
928
 * See g_spawn_sync() for full details.
929
 *
930
 * The @command_line argument will be parsed by g_shell_parse_argv().
931
 *
932
 * Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag is enabled.
933
 * Note that %G_SPAWN_SEARCH_PATH can have security implications, so
934
 * consider using g_spawn_sync() directly if appropriate.
935
 *
936
 * Possible errors are those from g_spawn_sync() and those
937
 * from g_shell_parse_argv().
938
 *
939
 * If @wait_status is non-%NULL, the platform-specific status of
940
 * the child is stored there; see the documentation of
941
 * g_spawn_check_wait_status() for how to use and interpret this.
942
 * On Unix platforms, note that it is usually not equal
943
 * to the integer passed to `exit()` or returned from `main()`.
944
 * 
945
 * On Windows, please note the implications of g_shell_parse_argv()
946
 * parsing @command_line. Parsing is done according to Unix shell rules, not 
947
 * Windows command interpreter rules.
948
 * Space is a separator, and backslashes are
949
 * special. Thus you cannot simply pass a @command_line containing
950
 * canonical Windows paths, like "c:\\program files\\app\\app.exe", as
951
 * the backslashes will be eaten, and the space will act as a
952
 * separator. You need to enclose such paths with single quotes, like
953
 * "'c:\\program files\\app\\app.exe' 'e:\\folder\\argument.txt'".
954
 *
955
 * Returns: %TRUE on success, %FALSE if an error was set
956
 **/
957
gboolean
958
g_spawn_command_line_sync (const gchar  *command_line,
959
                           gchar       **standard_output,
960
                           gchar       **standard_error,
961
                           gint         *wait_status,
962
                           GError      **error)
963
0
{
964
0
  gboolean retval;
965
0
  gchar **argv = NULL;
966
967
0
  g_return_val_if_fail (command_line != NULL, FALSE);
968
  
969
  /* This will return a runtime error if @command_line is the empty string. */
970
0
  if (!g_shell_parse_argv (command_line,
971
0
                           NULL, &argv,
972
0
                           error))
973
0
    return FALSE;
974
  
975
0
  retval = g_spawn_sync (NULL,
976
0
                         argv,
977
0
                         NULL,
978
0
                         G_SPAWN_SEARCH_PATH,
979
0
                         NULL,
980
0
                         NULL,
981
0
                         standard_output,
982
0
                         standard_error,
983
0
                         wait_status,
984
0
                         error);
985
0
  g_strfreev (argv);
986
987
0
  return retval;
988
0
}
989
990
/**
991
 * g_spawn_command_line_async:
992
 * @command_line: (type filename): a command line
993
 * @error: return location for errors
994
 * 
995
 * A simple version of g_spawn_async() that parses a command line with
996
 * g_shell_parse_argv() and passes it to g_spawn_async().
997
 *
998
 * Runs a command line in the background. Unlike g_spawn_async(), the
999
 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
1000
 * that %G_SPAWN_SEARCH_PATH can have security implications, so
1001
 * consider using g_spawn_async() directly if appropriate. Possible
1002
 * errors are those from g_shell_parse_argv() and g_spawn_async().
1003
 * 
1004
 * The same concerns on Windows apply as for g_spawn_command_line_sync().
1005
 *
1006
 * Returns: %TRUE on success, %FALSE if error is set
1007
 **/
1008
gboolean
1009
g_spawn_command_line_async (const gchar *command_line,
1010
                            GError     **error)
1011
0
{
1012
0
  gboolean retval;
1013
0
  gchar **argv = NULL;
1014
1015
0
  g_return_val_if_fail (command_line != NULL, FALSE);
1016
1017
  /* This will return a runtime error if @command_line is the empty string. */
1018
0
  if (!g_shell_parse_argv (command_line,
1019
0
                           NULL, &argv,
1020
0
                           error))
1021
0
    return FALSE;
1022
  
1023
0
  retval = g_spawn_async (NULL,
1024
0
                          argv,
1025
0
                          NULL,
1026
0
                          G_SPAWN_SEARCH_PATH,
1027
0
                          NULL,
1028
0
                          NULL,
1029
0
                          NULL,
1030
0
                          error);
1031
0
  g_strfreev (argv);
1032
1033
0
  return retval;
1034
0
}
1035
1036
/**
1037
 * g_spawn_check_wait_status:
1038
 * @wait_status: A platform-specific wait status as returned from g_spawn_sync()
1039
 * @error: a #GError
1040
 *
1041
 * Set @error if @wait_status indicates the child exited abnormally
1042
 * (e.g. with a nonzero exit code, or via a fatal signal).
1043
 *
1044
 * The g_spawn_sync() and g_child_watch_add() family of APIs return the
1045
 * status of subprocesses encoded in a platform-specific way.
1046
 * On Unix, this is guaranteed to be in the same format waitpid() returns,
1047
 * and on Windows it is guaranteed to be the result of GetExitCodeProcess().
1048
 *
1049
 * Prior to the introduction of this function in GLib 2.34, interpreting
1050
 * @wait_status required use of platform-specific APIs, which is problematic
1051
 * for software using GLib as a cross-platform layer.
1052
 *
1053
 * Additionally, many programs simply want to determine whether or not
1054
 * the child exited successfully, and either propagate a #GError or
1055
 * print a message to standard error. In that common case, this function
1056
 * can be used. Note that the error message in @error will contain
1057
 * human-readable information about the wait status.
1058
 *
1059
 * The @domain and @code of @error have special semantics in the case
1060
 * where the process has an "exit code", as opposed to being killed by
1061
 * a signal. On Unix, this happens if WIFEXITED() would be true of
1062
 * @wait_status. On Windows, it is always the case.
1063
 *
1064
 * The special semantics are that the actual exit code will be the
1065
 * code set in @error, and the domain will be %G_SPAWN_EXIT_ERROR.
1066
 * This allows you to differentiate between different exit codes.
1067
 *
1068
 * If the process was terminated by some means other than an exit
1069
 * status (for example if it was killed by a signal), the domain will be
1070
 * %G_SPAWN_ERROR and the code will be %G_SPAWN_ERROR_FAILED.
1071
 *
1072
 * This function just offers convenience; you can of course also check
1073
 * the available platform via a macro such as %G_OS_UNIX, and use
1074
 * WIFEXITED() and WEXITSTATUS() on @wait_status directly. Do not attempt
1075
 * to scan or parse the error message string; it may be translated and/or
1076
 * change in future versions of GLib.
1077
 *
1078
 * Prior to version 2.70, g_spawn_check_exit_status() provides the same
1079
 * functionality, although under a misleading name.
1080
 *
1081
 * Returns: %TRUE if child exited successfully, %FALSE otherwise (and
1082
 *   @error will be set)
1083
 *
1084
 * Since: 2.70
1085
 */
1086
gboolean
1087
g_spawn_check_wait_status (gint      wait_status,
1088
         GError  **error)
1089
0
{
1090
0
  gboolean ret = FALSE;
1091
1092
0
  if (WIFEXITED (wait_status))
1093
0
    {
1094
0
      if (WEXITSTATUS (wait_status) != 0)
1095
0
  {
1096
0
    g_set_error (error, G_SPAWN_EXIT_ERROR, WEXITSTATUS (wait_status),
1097
0
           _("Child process exited with code %ld"),
1098
0
           (long) WEXITSTATUS (wait_status));
1099
0
    goto out;
1100
0
  }
1101
0
    }
1102
0
  else if (WIFSIGNALED (wait_status))
1103
0
    {
1104
0
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1105
0
       _("Child process killed by signal %ld"),
1106
0
       (long) WTERMSIG (wait_status));
1107
0
      goto out;
1108
0
    }
1109
0
  else if (WIFSTOPPED (wait_status))
1110
0
    {
1111
0
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1112
0
       _("Child process stopped by signal %ld"),
1113
0
       (long) WSTOPSIG (wait_status));
1114
0
      goto out;
1115
0
    }
1116
0
  else
1117
0
    {
1118
0
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1119
0
       _("Child process exited abnormally"));
1120
0
      goto out;
1121
0
    }
1122
1123
0
  ret = TRUE;
1124
0
 out:
1125
0
  return ret;
1126
0
}
1127
1128
/**
1129
 * g_spawn_check_exit_status:
1130
 * @wait_status: A status as returned from g_spawn_sync()
1131
 * @error: a #GError
1132
 *
1133
 * An old name for g_spawn_check_wait_status(), deprecated because its
1134
 * name is misleading.
1135
 *
1136
 * Despite the name of the function, @wait_status must be the wait status
1137
 * as returned by g_spawn_sync(), g_subprocess_get_status(), `waitpid()`,
1138
 * etc. On Unix platforms, it is incorrect for it to be the exit status
1139
 * as passed to `exit()` or returned by g_subprocess_get_exit_status() or
1140
 * `WEXITSTATUS()`.
1141
 *
1142
 * Returns: %TRUE if child exited successfully, %FALSE otherwise (and
1143
 *     @error will be set)
1144
 *
1145
 * Since: 2.34
1146
 *
1147
 * Deprecated: 2.70: Use g_spawn_check_wait_status() instead, and check whether your code is conflating wait and exit statuses.
1148
 */
1149
gboolean
1150
g_spawn_check_exit_status (gint      wait_status,
1151
                           GError  **error)
1152
0
{
1153
0
  return g_spawn_check_wait_status (wait_status, error);
1154
0
}
1155
1156
/* This function is called between fork() and exec() and hence must be
1157
 * async-signal-safe (see signal-safety(7)). */
1158
static gssize
1159
write_all (gint fd, gconstpointer vbuf, gsize to_write)
1160
0
{
1161
0
  gchar *buf = (gchar *) vbuf;
1162
  
1163
0
  while (to_write > 0)
1164
0
    {
1165
0
      gssize count = write (fd, buf, to_write);
1166
0
      if (count < 0)
1167
0
        {
1168
0
          if (errno != EINTR)
1169
0
            return FALSE;
1170
0
        }
1171
0
      else
1172
0
        {
1173
0
          to_write -= count;
1174
0
          buf += count;
1175
0
        }
1176
0
    }
1177
  
1178
0
  return TRUE;
1179
0
}
1180
1181
/* This function is called between fork() and exec() and hence must be
1182
 * async-signal-safe (see signal-safety(7)). */
1183
G_NORETURN
1184
static void
1185
write_err_and_exit (gint fd, gint msg)
1186
0
{
1187
0
  gint en = errno;
1188
  
1189
0
  write_all (fd, &msg, sizeof(msg));
1190
0
  write_all (fd, &en, sizeof(en));
1191
  
1192
0
  _exit (1);
1193
0
}
1194
1195
/* This function is called between fork() and exec() and hence must be
1196
 * async-signal-safe (see signal-safety(7)). */
1197
static void
1198
set_cloexec (int fd)
1199
0
{
1200
0
  fcntl (fd, F_SETFD, FD_CLOEXEC);
1201
0
}
1202
1203
/* This function is called between fork() and exec() and hence must be
1204
 * async-signal-safe (see signal-safety(7)). */
1205
static void
1206
unset_cloexec (int fd)
1207
0
{
1208
0
  int flags;
1209
0
  int result;
1210
1211
0
  flags = fcntl (fd, F_GETFD, 0);
1212
1213
0
  if (flags != -1)
1214
0
    {
1215
0
      int errsv;
1216
0
      flags &= (~FD_CLOEXEC);
1217
0
      do
1218
0
        {
1219
0
          result = fcntl (fd, F_SETFD, flags);
1220
0
          errsv = errno;
1221
0
        }
1222
0
      while (result == -1 && errsv == EINTR);
1223
0
    }
1224
0
}
1225
1226
/* This function is called between fork() and exec() and hence must be
1227
 * async-signal-safe (see signal-safety(7)). */
1228
static int
1229
dupfd_cloexec (int old_fd, int new_fd_min)
1230
0
{
1231
0
  int fd, errsv;
1232
0
#ifdef F_DUPFD_CLOEXEC
1233
0
  do
1234
0
    {
1235
0
      fd = fcntl (old_fd, F_DUPFD_CLOEXEC, new_fd_min);
1236
0
      errsv = errno;
1237
0
    }
1238
0
  while (fd == -1 && errsv == EINTR);
1239
#else
1240
  /* OS X Snow Lion and earlier don't have F_DUPFD_CLOEXEC:
1241
   * https://bugzilla.gnome.org/show_bug.cgi?id=710962
1242
   */
1243
  int result, flags;
1244
  do
1245
    {
1246
      fd = fcntl (old_fd, F_DUPFD, new_fd_min);
1247
      errsv = errno;
1248
    }
1249
  while (fd == -1 && errsv == EINTR);
1250
  flags = fcntl (fd, F_GETFD, 0);
1251
  if (flags != -1)
1252
    {
1253
      flags |= FD_CLOEXEC;
1254
      do
1255
        {
1256
          result = fcntl (fd, F_SETFD, flags);
1257
          errsv = errno;
1258
        }
1259
      while (result == -1 && errsv == EINTR);
1260
    }
1261
#endif
1262
0
  return fd;
1263
0
}
1264
1265
/* This function is called between fork() and exec() and hence must be
1266
 * async-signal-safe (see signal-safety(7)). */
1267
static gint
1268
safe_dup2 (gint fd1, gint fd2)
1269
0
{
1270
0
  gint ret;
1271
1272
0
  do
1273
0
    ret = dup2 (fd1, fd2);
1274
0
  while (ret < 0 && (errno == EINTR || errno == EBUSY));
1275
1276
0
  return ret;
1277
0
}
1278
1279
/* This function is called between fork() and exec() and hence must be
1280
 * async-signal-safe (see signal-safety(7)). */
1281
static gboolean
1282
relocate_fd_out_of_standard_range (gint *fd)
1283
0
{
1284
0
  gint ret = -1;
1285
0
  const int min_fileno = STDERR_FILENO + 1;
1286
1287
0
  do
1288
0
    ret = fcntl (*fd, F_DUPFD, min_fileno);
1289
0
  while (ret < 0 && errno == EINTR);
1290
1291
  /* Note we don't need to close the old fd, because the caller is expected
1292
   * to close fds in the standard range itself.
1293
   */
1294
0
  if (ret >= min_fileno)
1295
0
    {
1296
0
      *fd = ret;
1297
0
      return TRUE;
1298
0
    }
1299
1300
0
  return FALSE;
1301
0
}
1302
1303
/* This function is called between fork() and exec() and hence must be
1304
 * async-signal-safe (see signal-safety(7)). */
1305
static gint
1306
safe_open (const char *path, gint mode)
1307
0
{
1308
0
  gint ret;
1309
1310
0
  do
1311
0
    ret = open (path, mode);
1312
0
  while (ret < 0 && errno == EINTR);
1313
1314
0
  return ret;
1315
0
}
1316
1317
enum
1318
{
1319
  CHILD_CHDIR_FAILED,
1320
  CHILD_EXEC_FAILED,
1321
  CHILD_OPEN_FAILED,
1322
  CHILD_DUPFD_FAILED,
1323
  CHILD_FORK_FAILED,
1324
  CHILD_CLOSE_FAILED,
1325
};
1326
1327
/* This function is called between fork() and exec() and hence must be
1328
 * async-signal-safe (see signal-safety(7)) until it calls exec().
1329
 *
1330
 * All callers must guarantee that @argv and @argv[0] are non-NULL. */
1331
static void
1332
do_exec (gint                  child_err_report_fd,
1333
         gint                  stdin_fd,
1334
         gint                  stdout_fd,
1335
         gint                  stderr_fd,
1336
         gint                 *source_fds,
1337
         const gint           *target_fds,
1338
         gsize                 n_fds,
1339
         const gchar          *working_directory,
1340
         const gchar * const  *argv,
1341
         gchar               **argv_buffer,
1342
         gsize                 argv_buffer_len,
1343
         const gchar * const  *envp,
1344
         gboolean              close_descriptors,
1345
         const gchar          *search_path,
1346
         gchar                *search_path_buffer,
1347
         gsize                 search_path_buffer_len,
1348
         gboolean              stdout_to_null,
1349
         gboolean              stderr_to_null,
1350
         gboolean              child_inherits_stdin,
1351
         gboolean              file_and_argv_zero,
1352
         GSpawnChildSetupFunc  child_setup,
1353
         gpointer              user_data)
1354
0
{
1355
0
  gsize i;
1356
0
  gint max_target_fd = 0;
1357
1358
0
  if (working_directory && chdir (working_directory) < 0)
1359
0
    write_err_and_exit (child_err_report_fd,
1360
0
                        CHILD_CHDIR_FAILED);
1361
1362
  /* It's possible the caller assigned stdin to an fd with a
1363
   * file number that is supposed to be reserved for
1364
   * stdout or stderr.
1365
   *
1366
   * If so, move it up out of the standard range, so it doesn't
1367
   * cause a conflict.
1368
   */
1369
0
  if (IS_STD_FILENO (stdin_fd) && stdin_fd != STDIN_FILENO)
1370
0
    {
1371
0
      int old_fd = stdin_fd;
1372
1373
0
      if (!relocate_fd_out_of_standard_range (&stdin_fd))
1374
0
        write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1375
1376
0
      if (stdout_fd == old_fd)
1377
0
        stdout_fd = stdin_fd;
1378
1379
0
      if (stderr_fd == old_fd)
1380
0
        stderr_fd = stdin_fd;
1381
0
    }
1382
1383
  /* Redirect pipes as required
1384
   *
1385
   * There are two cases where we don't need to do the redirection
1386
   * 1. Where the associated file descriptor is cleared/invalid
1387
   * 2. When the associated file descriptor is already given the
1388
   * correct file number.
1389
   */
1390
0
  if (IS_VALID_FILENO (stdin_fd) && stdin_fd != STDIN_FILENO)
1391
0
    {
1392
0
      if (safe_dup2 (stdin_fd, 0) < 0)
1393
0
        write_err_and_exit (child_err_report_fd,
1394
0
                            CHILD_DUPFD_FAILED);
1395
1396
0
      set_cloexec (stdin_fd);
1397
0
    }
1398
0
  else if (!child_inherits_stdin)
1399
0
    {
1400
      /* Keep process from blocking on a read of stdin */
1401
0
      gint read_null = safe_open ("/dev/null", O_RDONLY);
1402
0
      if (read_null < 0)
1403
0
        write_err_and_exit (child_err_report_fd,
1404
0
                            CHILD_OPEN_FAILED);
1405
0
      if (safe_dup2 (read_null, 0) < 0)
1406
0
        write_err_and_exit (child_err_report_fd,
1407
0
                            CHILD_DUPFD_FAILED);
1408
0
      g_clear_fd (&read_null, NULL);
1409
0
    }
1410
1411
  /* Like with stdin above, it's possible the caller assigned
1412
   * stdout to an fd with a file number that's intruding on the
1413
   * standard range.
1414
   *
1415
   * If so, move it out of the way, too.
1416
   */
1417
0
  if (IS_STD_FILENO (stdout_fd) && stdout_fd != STDOUT_FILENO)
1418
0
    {
1419
0
      int old_fd = stdout_fd;
1420
1421
0
      if (!relocate_fd_out_of_standard_range (&stdout_fd))
1422
0
        write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1423
1424
0
      if (stderr_fd == old_fd)
1425
0
        stderr_fd = stdout_fd;
1426
0
    }
1427
1428
0
  if (IS_VALID_FILENO (stdout_fd) && stdout_fd != STDOUT_FILENO)
1429
0
    {
1430
0
      if (safe_dup2 (stdout_fd, 1) < 0)
1431
0
        write_err_and_exit (child_err_report_fd,
1432
0
                            CHILD_DUPFD_FAILED);
1433
1434
0
      set_cloexec (stdout_fd);
1435
0
    }
1436
0
  else if (stdout_to_null)
1437
0
    {
1438
0
      gint write_null = safe_open ("/dev/null", O_WRONLY);
1439
0
      if (write_null < 0)
1440
0
        write_err_and_exit (child_err_report_fd,
1441
0
                            CHILD_OPEN_FAILED);
1442
0
      if (safe_dup2 (write_null, 1) < 0)
1443
0
        write_err_and_exit (child_err_report_fd,
1444
0
                            CHILD_DUPFD_FAILED);
1445
0
      g_clear_fd (&write_null, NULL);
1446
0
    }
1447
1448
0
  if (IS_STD_FILENO (stderr_fd) && stderr_fd != STDERR_FILENO)
1449
0
    {
1450
0
      if (!relocate_fd_out_of_standard_range (&stderr_fd))
1451
0
        write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1452
0
    }
1453
1454
  /* Like with stdin/stdout above, it's possible the caller assigned
1455
   * stderr to an fd with a file number that's intruding on the
1456
   * standard range.
1457
   *
1458
   * Make sure it's out of the way, also.
1459
   */
1460
0
  if (IS_VALID_FILENO (stderr_fd) && stderr_fd != STDERR_FILENO)
1461
0
    {
1462
0
      if (safe_dup2 (stderr_fd, 2) < 0)
1463
0
        write_err_and_exit (child_err_report_fd,
1464
0
                            CHILD_DUPFD_FAILED);
1465
1466
0
      set_cloexec (stderr_fd);
1467
0
    }
1468
0
  else if (stderr_to_null)
1469
0
    {
1470
0
      gint write_null = safe_open ("/dev/null", O_WRONLY);
1471
0
      if (write_null < 0)
1472
0
        write_err_and_exit (child_err_report_fd,
1473
0
                            CHILD_OPEN_FAILED);
1474
0
      if (safe_dup2 (write_null, 2) < 0)
1475
0
        write_err_and_exit (child_err_report_fd,
1476
0
                            CHILD_DUPFD_FAILED);
1477
0
      g_clear_fd (&write_null, NULL);
1478
0
    }
1479
1480
  /* Close all file descriptors but stdin, stdout and stderr, and any of source_fds,
1481
   * before we exec. Note that this includes
1482
   * child_err_report_fd, which keeps the parent from blocking
1483
   * forever on the other end of that pipe.
1484
   */
1485
0
  if (close_descriptors)
1486
0
    {
1487
0
      if (child_setup == NULL && n_fds == 0)
1488
0
        {
1489
0
          if (safe_dup2 (child_err_report_fd, 3) < 0)
1490
0
            write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1491
0
          set_cloexec (3);
1492
0
          if (g_closefrom (4) < 0)
1493
0
            write_err_and_exit (child_err_report_fd, CHILD_CLOSE_FAILED);
1494
0
          child_err_report_fd = 3;
1495
0
        }
1496
0
      else
1497
0
        {
1498
0
          if (g_fdwalk_set_cloexec (3) < 0)
1499
0
            write_err_and_exit (child_err_report_fd, CHILD_CLOSE_FAILED);
1500
0
        }
1501
0
    }
1502
0
  else
1503
0
    {
1504
      /* We need to do child_err_report_fd anyway */
1505
0
      set_cloexec (child_err_report_fd);
1506
0
    }
1507
1508
  /*
1509
   * Work through the @source_fds and @target_fds mapping.
1510
   *
1511
   * Based on code originally derived from
1512
   * gnome-terminal:src/terminal-screen.c:terminal_screen_child_setup(),
1513
   * used under the LGPLv2+ with permission from author. (The code has
1514
   * since migrated to vte:src/spawn.cc:SpawnContext::exec and is no longer
1515
   * terribly similar to what we have here.)
1516
   */
1517
1518
0
  if (n_fds > 0)
1519
0
    {
1520
0
      for (i = 0; i < n_fds; i++)
1521
0
        max_target_fd = MAX (max_target_fd, target_fds[i]);
1522
1523
0
      if (max_target_fd == G_MAXINT)
1524
0
        {
1525
0
          errno = EINVAL;
1526
0
          write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1527
0
        }
1528
1529
      /* If we're doing remapping fd assignments, we need to handle
1530
       * the case where the user has specified e.g. 5 -> 4, 4 -> 6.
1531
       * We do this by duping all source fds, taking care to ensure the new
1532
       * fds are larger than any target fd to avoid introducing new conflicts.
1533
       */
1534
0
      for (i = 0; i < n_fds; i++)
1535
0
        {
1536
0
          if (source_fds[i] != target_fds[i])
1537
0
            {
1538
0
              source_fds[i] = dupfd_cloexec (source_fds[i], max_target_fd + 1);
1539
0
              if (source_fds[i] < 0)
1540
0
                write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1541
0
            }
1542
0
        }
1543
1544
0
      for (i = 0; i < n_fds; i++)
1545
0
        {
1546
          /* For basic fd assignments (where source == target), we can just
1547
           * unset FD_CLOEXEC.
1548
           */
1549
0
          if (source_fds[i] == target_fds[i])
1550
0
            {
1551
0
              unset_cloexec (source_fds[i]);
1552
0
            }
1553
0
          else
1554
0
            {
1555
              /* If any of the @target_fds conflict with @child_err_report_fd,
1556
               * dup it so it doesn’t get conflated.
1557
               */
1558
0
              if (target_fds[i] == child_err_report_fd)
1559
0
                {
1560
0
                  child_err_report_fd = dupfd_cloexec (child_err_report_fd, max_target_fd + 1);
1561
0
                  if (child_err_report_fd < 0)
1562
0
                    write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1563
0
                }
1564
1565
0
              if (safe_dup2 (source_fds[i], target_fds[i]) < 0)
1566
0
                write_err_and_exit (child_err_report_fd, CHILD_DUPFD_FAILED);
1567
1568
0
              g_clear_fd (&source_fds[i], NULL);
1569
0
            }
1570
0
        }
1571
0
    }
1572
1573
  /* Call user function just before we exec */
1574
0
  if (child_setup)
1575
0
    {
1576
0
      (* child_setup) (user_data);
1577
0
    }
1578
1579
0
  g_execute (argv[0],
1580
0
             (gchar **) (file_and_argv_zero ? argv + 1 : argv),
1581
0
             argv_buffer, argv_buffer_len,
1582
0
             (gchar **) envp, search_path, search_path_buffer, search_path_buffer_len);
1583
1584
  /* Exec failed */
1585
0
  write_err_and_exit (child_err_report_fd,
1586
0
                      CHILD_EXEC_FAILED);
1587
0
}
1588
1589
static gboolean
1590
read_ints (int      fd,
1591
           gint*    buf,
1592
           gint     n_ints_in_buf,    
1593
           gint    *n_ints_read,      
1594
           GError **error)
1595
0
{
1596
0
  gsize bytes = 0;    
1597
  
1598
0
  while (TRUE)
1599
0
    {
1600
0
      gssize chunk;    
1601
1602
0
      if (bytes >= sizeof(gint)*2)
1603
0
        break; /* give up, who knows what happened, should not be
1604
                * possible.
1605
                */
1606
          
1607
0
    again:
1608
0
      chunk = read (fd,
1609
0
                    ((gchar*)buf) + bytes,
1610
0
                    sizeof(gint) * n_ints_in_buf - bytes);
1611
0
      if (chunk < 0 && errno == EINTR)
1612
0
        goto again;
1613
          
1614
0
      if (chunk < 0)
1615
0
        {
1616
0
          int errsv = errno;
1617
1618
          /* Some weird shit happened, bail out */
1619
0
          g_set_error (error,
1620
0
                       G_SPAWN_ERROR,
1621
0
                       G_SPAWN_ERROR_FAILED,
1622
0
                       _("Failed to read from child pipe (%s)"),
1623
0
                       g_strerror (errsv));
1624
1625
0
          return FALSE;
1626
0
        }
1627
0
      else if (chunk == 0)
1628
0
        break; /* EOF */
1629
0
      else /* chunk > 0 */
1630
0
  bytes += chunk;
1631
0
    }
1632
1633
0
  *n_ints_read = (gint)(bytes / sizeof(gint));
1634
1635
0
  return TRUE;
1636
0
}
1637
1638
#ifdef POSIX_SPAWN_AVAILABLE
1639
static gboolean
1640
do_posix_spawn (const gchar * const *argv,
1641
                const gchar * const *envp,
1642
                gboolean    search_path,
1643
                gboolean    stdout_to_null,
1644
                gboolean    stderr_to_null,
1645
                gboolean    child_inherits_stdin,
1646
                gboolean    file_and_argv_zero,
1647
                GPid       *child_pid,
1648
                gint       *child_close_fds,
1649
                gint        stdin_fd,
1650
                gint        stdout_fd,
1651
                gint        stderr_fd,
1652
                const gint *source_fds,
1653
                const gint *target_fds,
1654
                gsize       n_fds)
1655
0
{
1656
0
  pid_t pid;
1657
0
  gint *duped_source_fds = NULL;
1658
0
  gint max_target_fd = 0;
1659
0
  const gchar * const *argv_pass;
1660
0
  posix_spawnattr_t attr;
1661
0
  posix_spawn_file_actions_t file_actions;
1662
0
  gint parent_close_fds[3];
1663
0
  gsize num_parent_close_fds = 0;
1664
0
  GSList *child_close = NULL;
1665
0
  GSList *elem;
1666
0
  sigset_t mask;
1667
0
  gsize i;
1668
0
  int r;
1669
1670
0
  g_assert (argv != NULL && argv[0] != NULL);
1671
1672
0
  if (*argv[0] == '\0')
1673
0
    {
1674
      /* We check the simple case first. */
1675
0
      return ENOENT;
1676
0
    }
1677
1678
0
  r = posix_spawnattr_init (&attr);
1679
0
  if (r != 0)
1680
0
    return r;
1681
1682
0
  if (child_close_fds)
1683
0
    {
1684
0
      int i = -1;
1685
0
      while (child_close_fds[++i] != -1)
1686
0
        child_close = g_slist_prepend (child_close,
1687
0
                                       GINT_TO_POINTER (child_close_fds[i]));
1688
0
    }
1689
1690
0
  r = posix_spawnattr_setflags (&attr, POSIX_SPAWN_SETSIGDEF);
1691
0
  if (r != 0)
1692
0
    goto out_free_spawnattr;
1693
1694
  /* Reset some signal handlers that we may use */
1695
0
  sigemptyset (&mask);
1696
0
  sigaddset (&mask, SIGCHLD);
1697
0
  sigaddset (&mask, SIGINT);
1698
0
  sigaddset (&mask, SIGTERM);
1699
0
  sigaddset (&mask, SIGHUP);
1700
1701
0
  r = posix_spawnattr_setsigdefault (&attr, &mask);
1702
0
  if (r != 0)
1703
0
    goto out_free_spawnattr;
1704
1705
0
  r = posix_spawn_file_actions_init (&file_actions);
1706
0
  if (r != 0)
1707
0
    goto out_free_spawnattr;
1708
1709
  /* Redirect pipes as required */
1710
1711
0
  if (stdin_fd >= 0)
1712
0
    {
1713
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, stdin_fd, 0);
1714
0
      if (r != 0)
1715
0
        goto out_close_fds;
1716
1717
0
      if (!g_slist_find (child_close, GINT_TO_POINTER (stdin_fd)))
1718
0
        child_close = g_slist_prepend (child_close, GINT_TO_POINTER (stdin_fd));
1719
0
    }
1720
0
  else if (!child_inherits_stdin)
1721
0
    {
1722
      /* Keep process from blocking on a read of stdin */
1723
0
      gint read_null = safe_open ("/dev/null", O_RDONLY | O_CLOEXEC);
1724
0
      g_assert (read_null != -1);
1725
0
      parent_close_fds[num_parent_close_fds++] = read_null;
1726
1727
#ifndef HAVE_O_CLOEXEC
1728
      fcntl (read_null, F_SETFD, FD_CLOEXEC);
1729
#endif
1730
1731
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, read_null, 0);
1732
0
      if (r != 0)
1733
0
        goto out_close_fds;
1734
0
    }
1735
1736
0
  if (stdout_fd >= 0)
1737
0
    {
1738
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, stdout_fd, 1);
1739
0
      if (r != 0)
1740
0
        goto out_close_fds;
1741
1742
0
      if (!g_slist_find (child_close, GINT_TO_POINTER (stdout_fd)))
1743
0
        child_close = g_slist_prepend (child_close, GINT_TO_POINTER (stdout_fd));
1744
0
    }
1745
0
  else if (stdout_to_null)
1746
0
    {
1747
0
      gint write_null = safe_open ("/dev/null", O_WRONLY | O_CLOEXEC);
1748
0
      g_assert (write_null != -1);
1749
0
      parent_close_fds[num_parent_close_fds++] = write_null;
1750
1751
#ifndef HAVE_O_CLOEXEC
1752
      fcntl (write_null, F_SETFD, FD_CLOEXEC);
1753
#endif
1754
1755
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, write_null, 1);
1756
0
      if (r != 0)
1757
0
        goto out_close_fds;
1758
0
    }
1759
1760
0
  if (stderr_fd >= 0)
1761
0
    {
1762
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, stderr_fd, 2);
1763
0
      if (r != 0)
1764
0
        goto out_close_fds;
1765
1766
0
      if (!g_slist_find (child_close, GINT_TO_POINTER (stderr_fd)))
1767
0
        child_close = g_slist_prepend (child_close, GINT_TO_POINTER (stderr_fd));
1768
0
    }
1769
0
  else if (stderr_to_null)
1770
0
    {
1771
0
      gint write_null = safe_open ("/dev/null", O_WRONLY | O_CLOEXEC);
1772
0
      g_assert (write_null != -1);
1773
0
      parent_close_fds[num_parent_close_fds++] = write_null;
1774
1775
#ifndef HAVE_O_CLOEXEC
1776
      fcntl (write_null, F_SETFD, FD_CLOEXEC);
1777
#endif
1778
1779
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, write_null, 2);
1780
0
      if (r != 0)
1781
0
        goto out_close_fds;
1782
0
    }
1783
1784
  /* If source_fds[i] != target_fds[i], we need to handle the case
1785
   * where the user has specified, e.g., 5 -> 4, 4 -> 6. We do this
1786
   * by duping the source fds, taking care to ensure the new fds are
1787
   * larger than any target fd to avoid introducing new conflicts.
1788
   *
1789
   * If source_fds[i] == target_fds[i], then we just need to leak
1790
   * the fd into the child process, which we *could* do by temporarily
1791
   * unsetting CLOEXEC and then setting it again after we spawn if
1792
   * it was originally set. POSIX requires that the addup2 action unset
1793
   * CLOEXEC if source and target are identical, so you'd think doing it
1794
   * manually wouldn't be needed, but unfortunately as of 2021 many
1795
   * libcs still don't do so. Example nonconforming libcs:
1796
   *  Bionic: https://android.googlesource.com/platform/bionic/+/f6e5b582604715729b09db3e36a7aeb8c24b36a4/libc/bionic/spawn.cpp#71
1797
   *  uclibc-ng: https://cgit.uclibc-ng.org/cgi/cgit/uclibc-ng.git/tree/librt/spawn.c?id=7c36bcae09d66bbaa35cbb02253ae0556f42677e#n88
1798
   *
1799
   * Anyway, unsetting CLOEXEC ourselves would open a small race window
1800
   * where the fd could be inherited into a child process if another
1801
   * thread spawns something at the same time, because we have not
1802
   * called fork() and are multithreaded here. This race is avoidable by
1803
   * using dupfd_cloexec, which we already have to do to handle the
1804
   * source_fds[i] != target_fds[i] case. So let's always do it!
1805
   */
1806
1807
0
  for (i = 0; i < n_fds; i++)
1808
0
    max_target_fd = MAX (max_target_fd, target_fds[i]);
1809
1810
0
  if (max_target_fd == G_MAXINT)
1811
0
    goto out_close_fds;
1812
1813
0
  duped_source_fds = g_new (gint, n_fds);
1814
0
  for (i = 0; i < n_fds; i++)
1815
0
    duped_source_fds[i] = -1;  /* initialise in case dupfd_cloexec() fails below */
1816
0
  for (i = 0; i < n_fds; i++)
1817
0
    {
1818
0
      duped_source_fds[i] = dupfd_cloexec (source_fds[i], max_target_fd + 1);
1819
0
      if (duped_source_fds[i] < 0)
1820
0
        goto out_close_fds;
1821
0
    }
1822
1823
0
  for (i = 0; i < n_fds; i++)
1824
0
    {
1825
0
      r = posix_spawn_file_actions_adddup2 (&file_actions, duped_source_fds[i], target_fds[i]);
1826
0
      if (r != 0)
1827
0
        goto out_close_fds;
1828
0
    }
1829
1830
  /* Intentionally close the fds in the child as the last file action,
1831
   * having been careful not to add the same fd to this list twice.
1832
   *
1833
   * This is important to allow (e.g.) for the same fd to be passed as stdout
1834
   * and stderr (we must not close it before we have dupped it in both places,
1835
   * and we must not attempt to close it twice).
1836
   */
1837
0
  for (elem = child_close; elem != NULL; elem = elem->next)
1838
0
    {
1839
0
      r = posix_spawn_file_actions_addclose (&file_actions,
1840
0
                                             GPOINTER_TO_INT (elem->data));
1841
0
      if (r != 0)
1842
0
        goto out_close_fds;
1843
0
    }
1844
1845
0
  argv_pass = file_and_argv_zero ? argv + 1 : argv;
1846
0
  if (envp == NULL)
1847
0
    envp = (const gchar * const *) environ;
1848
1849
  /* Don't search when it contains a slash. */
1850
0
  if (!search_path || strchr (argv[0], '/') != NULL)
1851
0
    r = posix_spawn (&pid, argv[0], &file_actions, &attr, (char * const *) argv_pass, (char * const *) envp);
1852
0
  else
1853
0
    r = posix_spawnp (&pid, argv[0], &file_actions, &attr, (char * const *) argv_pass, (char * const *) envp);
1854
1855
0
  if (r == 0 && child_pid != NULL)
1856
0
    *child_pid = pid;
1857
1858
0
out_close_fds:
1859
0
  for (i = 0; i < num_parent_close_fds; i++)
1860
0
    g_clear_fd (&parent_close_fds[i], NULL);
1861
1862
0
  if (duped_source_fds != NULL)
1863
0
    {
1864
0
      for (i = 0; i < n_fds; i++)
1865
0
        g_clear_fd (&duped_source_fds[i], NULL);
1866
0
      g_free (duped_source_fds);
1867
0
    }
1868
1869
0
  posix_spawn_file_actions_destroy (&file_actions);
1870
0
out_free_spawnattr:
1871
0
  posix_spawnattr_destroy (&attr);
1872
0
  g_slist_free (child_close);
1873
1874
0
  return r;
1875
0
}
1876
#endif /* POSIX_SPAWN_AVAILABLE */
1877
1878
static gboolean
1879
source_fds_collide_with_pipe (const GUnixPipe  *pipefd,
1880
                              const int        *source_fds,
1881
                              gsize             n_fds,
1882
                              GError          **error)
1883
0
{
1884
0
  return (_g_spawn_invalid_source_fd (pipefd->fds[G_UNIX_PIPE_END_READ], source_fds, n_fds, error) ||
1885
0
          _g_spawn_invalid_source_fd (pipefd->fds[G_UNIX_PIPE_END_WRITE], source_fds, n_fds, error));
1886
0
}
1887
1888
static gboolean
1889
fork_exec (gboolean              intermediate_child,
1890
           const gchar          *working_directory,
1891
           const gchar * const  *argv,
1892
           const gchar * const  *envp,
1893
           gboolean              close_descriptors,
1894
           gboolean              search_path,
1895
           gboolean              search_path_from_envp,
1896
           gboolean              stdout_to_null,
1897
           gboolean              stderr_to_null,
1898
           gboolean              child_inherits_stdin,
1899
           gboolean              file_and_argv_zero,
1900
           gboolean              cloexec_pipes,
1901
           GSpawnChildSetupFunc  child_setup,
1902
           gpointer              user_data,
1903
           GPid                 *child_pid,
1904
           gint                 *stdin_pipe_out,
1905
           gint                 *stdout_pipe_out,
1906
           gint                 *stderr_pipe_out,
1907
           gint                  stdin_fd,
1908
           gint                  stdout_fd,
1909
           gint                  stderr_fd,
1910
           const gint           *source_fds,
1911
           const gint           *target_fds,
1912
           gsize                 n_fds,
1913
           GError              **error)
1914
0
{
1915
0
  GPid pid = -1;
1916
0
  GUnixPipe child_err_report_pipe = G_UNIX_PIPE_INIT;
1917
0
  GUnixPipe child_pid_report_pipe = G_UNIX_PIPE_INIT;
1918
0
  guint pipe_flags = cloexec_pipes ? O_CLOEXEC : 0;
1919
0
  gint status;
1920
0
  const gchar *chosen_search_path;
1921
0
  gchar *search_path_buffer = NULL;
1922
0
  gchar *search_path_buffer_heap = NULL;
1923
0
  gsize search_path_buffer_len = 0;
1924
0
  gchar **argv_buffer = NULL;
1925
0
  gchar **argv_buffer_heap = NULL;
1926
0
  gsize argv_buffer_len = 0;
1927
0
  GUnixPipe stdin_pipe = G_UNIX_PIPE_INIT;
1928
0
  GUnixPipe stdout_pipe = G_UNIX_PIPE_INIT;
1929
0
  GUnixPipe stderr_pipe = G_UNIX_PIPE_INIT;
1930
0
  gint child_close_fds[4] = { -1, -1, -1, -1 };
1931
0
  gint n_child_close_fds = 0;
1932
0
  gint *source_fds_copy = NULL;
1933
1934
0
  g_assert (argv != NULL && argv[0] != NULL);
1935
0
  g_assert (stdin_pipe_out == NULL || stdin_fd < 0);
1936
0
  g_assert (stdout_pipe_out == NULL || stdout_fd < 0);
1937
0
  g_assert (stderr_pipe_out == NULL || stderr_fd < 0);
1938
1939
  /* If pipes have been requested, open them */
1940
0
  if (stdin_pipe_out != NULL)
1941
0
    {
1942
0
      if (!g_unix_pipe_open (&stdin_pipe, pipe_flags, error))
1943
0
        goto cleanup_and_fail;
1944
0
      if (source_fds_collide_with_pipe (&stdin_pipe, source_fds, n_fds, error))
1945
0
        goto cleanup_and_fail;
1946
0
      child_close_fds[n_child_close_fds++] = g_unix_pipe_get (&stdin_pipe, G_UNIX_PIPE_END_WRITE);
1947
0
      stdin_fd = g_unix_pipe_get (&stdin_pipe, G_UNIX_PIPE_END_READ);
1948
0
    }
1949
1950
0
  if (stdout_pipe_out != NULL)
1951
0
    {
1952
0
      if (!g_unix_pipe_open (&stdout_pipe, pipe_flags, error))
1953
0
        goto cleanup_and_fail;
1954
0
      if (source_fds_collide_with_pipe (&stdout_pipe, source_fds, n_fds, error))
1955
0
        goto cleanup_and_fail;
1956
0
      child_close_fds[n_child_close_fds++] = g_unix_pipe_get (&stdout_pipe, G_UNIX_PIPE_END_READ);
1957
0
      stdout_fd = g_unix_pipe_get (&stdout_pipe, G_UNIX_PIPE_END_WRITE);
1958
0
    }
1959
1960
0
  if (stderr_pipe_out != NULL)
1961
0
    {
1962
0
      if (!g_unix_pipe_open (&stderr_pipe, pipe_flags, error))
1963
0
        goto cleanup_and_fail;
1964
0
      if (source_fds_collide_with_pipe (&stderr_pipe, source_fds, n_fds, error))
1965
0
        goto cleanup_and_fail;
1966
0
      child_close_fds[n_child_close_fds++] = g_unix_pipe_get (&stderr_pipe, G_UNIX_PIPE_END_READ);
1967
0
      stderr_fd = g_unix_pipe_get (&stderr_pipe, G_UNIX_PIPE_END_WRITE);
1968
0
    }
1969
1970
0
  child_close_fds[n_child_close_fds++] = -1;
1971
1972
0
#ifdef POSIX_SPAWN_AVAILABLE
1973
0
  if (!intermediate_child && working_directory == NULL && !close_descriptors &&
1974
0
      !search_path_from_envp && child_setup == NULL)
1975
0
    {
1976
0
      g_trace_mark (G_TRACE_CURRENT_TIME, 0,
1977
0
                    "GLib", "posix_spawn",
1978
0
                    "%s", argv[0]);
1979
1980
0
      status = do_posix_spawn (argv,
1981
0
                               envp,
1982
0
                               search_path,
1983
0
                               stdout_to_null,
1984
0
                               stderr_to_null,
1985
0
                               child_inherits_stdin,
1986
0
                               file_and_argv_zero,
1987
0
                               child_pid,
1988
0
                               child_close_fds,
1989
0
                               stdin_fd,
1990
0
                               stdout_fd,
1991
0
                               stderr_fd,
1992
0
                               source_fds,
1993
0
                               target_fds,
1994
0
                               n_fds);
1995
0
      if (status == 0)
1996
0
        goto success;
1997
1998
0
      if (status != ENOEXEC)
1999
0
        {
2000
0
          g_set_error (error,
2001
0
                       G_SPAWN_ERROR,
2002
0
                       G_SPAWN_ERROR_FAILED,
2003
0
                       _("Failed to spawn child process ā€œ%sā€ (%s)"),
2004
0
                       argv[0],
2005
0
                       g_strerror (status));
2006
0
          goto cleanup_and_fail;
2007
0
       }
2008
2009
      /* posix_spawn is not intended to support script execution. It does in
2010
       * some situations on some glibc versions, but that will be fixed.
2011
       * So if it fails with ENOEXEC, we fall through to the regular
2012
       * gspawn codepath so that script execution can be attempted,
2013
       * per standard gspawn behaviour. */
2014
0
      g_debug ("posix_spawn failed (ENOEXEC), fall back to regular gspawn");
2015
0
    }
2016
0
  else
2017
0
    {
2018
0
      g_trace_mark (G_TRACE_CURRENT_TIME, 0,
2019
0
                    "GLib", "fork",
2020
0
                    "posix_spawn avoided %s%s%s%s%s",
2021
0
                    !intermediate_child ? "" : "(automatic reaping requested) ",
2022
0
                    working_directory == NULL ? "" : "(workdir specified) ",
2023
0
                    !close_descriptors ? "" : "(fd close requested) ",
2024
0
                    !search_path_from_envp ? "" : "(using envp for search path) ",
2025
0
                    child_setup == NULL ? "" : "(child_setup specified) ");
2026
0
    }
2027
0
#endif /* POSIX_SPAWN_AVAILABLE */
2028
2029
  /* Choose a search path. This has to be done before calling fork()
2030
   * as getenv() isn’t async-signal-safe (see `man 7 signal-safety`). */
2031
0
  chosen_search_path = NULL;
2032
0
  if (search_path_from_envp)
2033
0
    chosen_search_path = g_environ_getenv ((gchar **) envp, "PATH");
2034
0
  if (search_path && chosen_search_path == NULL)
2035
0
    chosen_search_path = g_getenv ("PATH");
2036
2037
0
  if ((search_path || search_path_from_envp) && chosen_search_path == NULL)
2038
0
    {
2039
      /* There is no 'PATH' in the environment.  The default
2040
       * * search path in libc is the current directory followed by
2041
       * * the path 'confstr' returns for '_CS_PATH'.
2042
       * */
2043
2044
      /* In GLib we put . last, for security, and don't use the
2045
       * * unportable confstr(); UNIX98 does not actually specify
2046
       * * what to search if PATH is unset. POSIX may, dunno.
2047
       * */
2048
2049
0
      chosen_search_path = "/bin:/usr/bin:.";
2050
0
    }
2051
2052
0
  if (search_path || search_path_from_envp)
2053
0
    g_assert (chosen_search_path != NULL);
2054
0
  else
2055
0
    g_assert (chosen_search_path == NULL);
2056
2057
  /* Allocate a buffer which the fork()ed child can use to assemble potential
2058
   * paths for the binary to exec(), combining the argv[0] and elements from
2059
   * the chosen_search_path. This can’t be done in the child because malloc()
2060
   * (or alloca()) are not async-signal-safe (see `man 7 signal-safety`).
2061
   *
2062
   * Add 2 for the nul terminator and a leading `/`. */
2063
0
  if (chosen_search_path != NULL)
2064
0
    {
2065
0
      search_path_buffer_len = strlen (chosen_search_path) + strlen (argv[0]) + 2;
2066
0
      if (search_path_buffer_len < 4000)
2067
0
        {
2068
          /* Prefer small stack allocations to avoid valgrind leak warnings
2069
           * in forked child. The 4000B cutoff is arbitrary. */
2070
0
          search_path_buffer = g_alloca (search_path_buffer_len);
2071
0
        }
2072
0
      else
2073
0
        {
2074
0
          search_path_buffer_heap = g_malloc (search_path_buffer_len);
2075
0
          search_path_buffer = search_path_buffer_heap;
2076
0
        }
2077
0
    }
2078
2079
0
  if (search_path || search_path_from_envp)
2080
0
    g_assert (search_path_buffer != NULL);
2081
0
  else
2082
0
    g_assert (search_path_buffer == NULL);
2083
2084
  /* And allocate a buffer which is 2 elements longer than @argv, so that if
2085
   * script_execute() has to be called later on, it can build a wrapper argv
2086
   * array in this buffer. */
2087
0
  argv_buffer_len = g_strv_length ((gchar **) argv) + 2;
2088
0
  if (argv_buffer_len < 4000 / sizeof (gchar *))
2089
0
    {
2090
      /* Prefer small stack allocations to avoid valgrind leak warnings
2091
       * in forked child. The 4000B cutoff is arbitrary. */
2092
0
      argv_buffer = g_newa (gchar *, argv_buffer_len);
2093
0
    }
2094
0
  else
2095
0
    {
2096
0
      argv_buffer_heap = g_new (gchar *, argv_buffer_len);
2097
0
      argv_buffer = argv_buffer_heap;
2098
0
    }
2099
2100
  /* And one to hold a copy of @source_fds for later manipulation in do_exec(). */
2101
0
  source_fds_copy = g_new (int, n_fds);
2102
0
  if (n_fds > 0)
2103
0
    memcpy (source_fds_copy, source_fds, sizeof (*source_fds) * n_fds);
2104
2105
0
  if (!g_unix_pipe_open (&child_err_report_pipe, pipe_flags, error))
2106
0
    goto cleanup_and_fail;
2107
0
  if (source_fds_collide_with_pipe (&child_err_report_pipe, source_fds, n_fds, error))
2108
0
    goto cleanup_and_fail;
2109
2110
0
  if (intermediate_child)
2111
0
    {
2112
0
      if (!g_unix_pipe_open (&child_pid_report_pipe, pipe_flags, error))
2113
0
        goto cleanup_and_fail;
2114
0
      if (source_fds_collide_with_pipe (&child_pid_report_pipe, source_fds, n_fds, error))
2115
0
        goto cleanup_and_fail;
2116
0
    }
2117
  
2118
0
  pid = fork ();
2119
2120
0
  if (pid < 0)
2121
0
    {
2122
0
      int errsv = errno;
2123
2124
0
      g_set_error (error,
2125
0
                   G_SPAWN_ERROR,
2126
0
                   G_SPAWN_ERROR_FORK,
2127
0
                   _("Failed to fork (%s)"),
2128
0
                   g_strerror (errsv));
2129
2130
0
      goto cleanup_and_fail;
2131
0
    }
2132
0
  else if (pid == 0)
2133
0
    {
2134
      /* Immediate child. This may or may not be the child that
2135
       * actually execs the new process.
2136
       */
2137
2138
      /* Reset some signal handlers that we may use */
2139
0
      signal (SIGCHLD, SIG_DFL);
2140
0
      signal (SIGINT, SIG_DFL);
2141
0
      signal (SIGTERM, SIG_DFL);
2142
0
      signal (SIGHUP, SIG_DFL);
2143
      
2144
      /* Be sure we crash if the parent exits
2145
       * and we write to the err_report_pipe
2146
       */
2147
0
      signal (SIGPIPE, SIG_DFL);
2148
2149
      /* Close the parent's end of the pipes;
2150
       * not needed in the close_descriptors case,
2151
       * though
2152
       */
2153
0
      g_unix_pipe_close (&child_err_report_pipe, G_UNIX_PIPE_END_READ, NULL);
2154
0
      g_unix_pipe_close (&child_pid_report_pipe, G_UNIX_PIPE_END_READ, NULL);
2155
0
      if (child_close_fds[0] != -1)
2156
0
        {
2157
0
           int i = -1;
2158
0
           while (child_close_fds[++i] != -1)
2159
0
             g_clear_fd (&child_close_fds[i], NULL);
2160
0
        }
2161
      
2162
0
      if (intermediate_child)
2163
0
        {
2164
          /* We need to fork an intermediate child that launches the
2165
           * final child. The purpose of the intermediate child
2166
           * is to exit, so we can waitpid() it immediately.
2167
           * Then the grandchild will not become a zombie.
2168
           */
2169
0
          GPid grandchild_pid;
2170
2171
0
          grandchild_pid = fork ();
2172
2173
0
          if (grandchild_pid < 0)
2174
0
            {
2175
              /* report -1 as child PID */
2176
0
              write_all (g_unix_pipe_get (&child_pid_report_pipe, G_UNIX_PIPE_END_WRITE),
2177
0
                         &grandchild_pid, sizeof(grandchild_pid));
2178
              
2179
0
              write_err_and_exit (g_unix_pipe_get (&child_err_report_pipe, G_UNIX_PIPE_END_WRITE),
2180
0
                                  CHILD_FORK_FAILED);              
2181
0
            }
2182
0
          else if (grandchild_pid == 0)
2183
0
            {
2184
0
              g_unix_pipe_close (&child_pid_report_pipe, G_UNIX_PIPE_END_WRITE, NULL);
2185
0
              do_exec (g_unix_pipe_get (&child_err_report_pipe, G_UNIX_PIPE_END_WRITE),
2186
0
                       stdin_fd,
2187
0
                       stdout_fd,
2188
0
                       stderr_fd,
2189
0
                       source_fds_copy,
2190
0
                       target_fds,
2191
0
                       n_fds,
2192
0
                       working_directory,
2193
0
                       argv,
2194
0
                       argv_buffer,
2195
0
                       argv_buffer_len,
2196
0
                       envp,
2197
0
                       close_descriptors,
2198
0
                       chosen_search_path,
2199
0
                       search_path_buffer,
2200
0
                       search_path_buffer_len,
2201
0
                       stdout_to_null,
2202
0
                       stderr_to_null,
2203
0
                       child_inherits_stdin,
2204
0
                       file_and_argv_zero,
2205
0
                       child_setup,
2206
0
                       user_data);
2207
0
            }
2208
0
          else
2209
0
            {
2210
0
              write_all (g_unix_pipe_get (&child_pid_report_pipe, G_UNIX_PIPE_END_WRITE),
2211
0
                         &grandchild_pid, sizeof(grandchild_pid));
2212
0
              g_unix_pipe_close (&child_pid_report_pipe, G_UNIX_PIPE_END_WRITE, NULL);
2213
              
2214
0
              _exit (0);
2215
0
            }
2216
0
        }
2217
0
      else
2218
0
        {
2219
          /* Just run the child.
2220
           */
2221
2222
0
          do_exec (g_unix_pipe_get (&child_err_report_pipe, G_UNIX_PIPE_END_WRITE),
2223
0
                   stdin_fd,
2224
0
                   stdout_fd,
2225
0
                   stderr_fd,
2226
0
                   source_fds_copy,
2227
0
                   target_fds,
2228
0
                   n_fds,
2229
0
                   working_directory,
2230
0
                   argv,
2231
0
                   argv_buffer,
2232
0
                   argv_buffer_len,
2233
0
                   envp,
2234
0
                   close_descriptors,
2235
0
                   chosen_search_path,
2236
0
                   search_path_buffer,
2237
0
                   search_path_buffer_len,
2238
0
                   stdout_to_null,
2239
0
                   stderr_to_null,
2240
0
                   child_inherits_stdin,
2241
0
                   file_and_argv_zero,
2242
0
                   child_setup,
2243
0
                   user_data);
2244
0
        }
2245
0
    }
2246
0
  else
2247
0
    {
2248
      /* Parent */
2249
      
2250
0
      gint buf[2];
2251
0
      gint n_ints = 0;    
2252
2253
      /* Close the uncared-about ends of the pipes */
2254
0
      g_unix_pipe_close (&child_err_report_pipe, G_UNIX_PIPE_END_WRITE, NULL);
2255
0
      g_unix_pipe_close (&child_pid_report_pipe, G_UNIX_PIPE_END_WRITE, NULL);
2256
2257
      /* If we had an intermediate child, reap it */
2258
0
      if (intermediate_child)
2259
0
        {
2260
0
        wait_again:
2261
0
          if (waitpid (pid, &status, 0) < 0)
2262
0
            {
2263
0
              if (errno == EINTR)
2264
0
                goto wait_again;
2265
0
              else if (errno == ECHILD)
2266
0
                ; /* do nothing, child already reaped */
2267
0
              else
2268
0
                g_warning ("waitpid() should not fail in 'fork_exec'");
2269
0
            }
2270
0
        }
2271
      
2272
2273
0
      if (!read_ints (g_unix_pipe_get (&child_err_report_pipe, G_UNIX_PIPE_END_READ),
2274
0
                      buf, 2, &n_ints,
2275
0
                      error))
2276
0
        goto cleanup_and_fail;
2277
        
2278
0
      if (n_ints >= 2)
2279
0
        {
2280
          /* Error from the child. */
2281
2282
0
          switch (buf[0])
2283
0
            {
2284
0
            case CHILD_CHDIR_FAILED:
2285
0
              g_set_error (error,
2286
0
                           G_SPAWN_ERROR,
2287
0
                           G_SPAWN_ERROR_CHDIR,
2288
0
                           _("Failed to change to directory ā€œ%sā€ (%s)"),
2289
0
                           working_directory,
2290
0
                           g_strerror (buf[1]));
2291
2292
0
              break;
2293
              
2294
0
            case CHILD_EXEC_FAILED:
2295
0
              g_set_error (error,
2296
0
                           G_SPAWN_ERROR,
2297
0
                           _g_spawn_exec_err_to_g_error (buf[1]),
2298
0
                           _("Failed to execute child process ā€œ%sā€ (%s)"),
2299
0
                           argv[0],
2300
0
                           g_strerror (buf[1]));
2301
2302
0
              break;
2303
2304
0
            case CHILD_OPEN_FAILED:
2305
0
              g_set_error (error,
2306
0
                           G_SPAWN_ERROR,
2307
0
                           G_SPAWN_ERROR_FAILED,
2308
0
                           _("Failed to open file to remap file descriptor (%s)"),
2309
0
                           g_strerror (buf[1]));
2310
0
              break;
2311
2312
0
            case CHILD_DUPFD_FAILED:
2313
0
              g_set_error (error,
2314
0
                           G_SPAWN_ERROR,
2315
0
                           G_SPAWN_ERROR_FAILED,
2316
0
                           _("Failed to duplicate file descriptor for child process (%s)"),
2317
0
                           g_strerror (buf[1]));
2318
2319
0
              break;
2320
2321
0
            case CHILD_FORK_FAILED:
2322
0
              g_set_error (error,
2323
0
                           G_SPAWN_ERROR,
2324
0
                           G_SPAWN_ERROR_FORK,
2325
0
                           _("Failed to fork child process (%s)"),
2326
0
                           g_strerror (buf[1]));
2327
0
              break;
2328
2329
0
            case CHILD_CLOSE_FAILED:
2330
0
              g_set_error (error,
2331
0
                           G_SPAWN_ERROR,
2332
0
                           G_SPAWN_ERROR_FAILED,
2333
0
                           _("Failed to close file descriptor for child process (%s)"),
2334
0
                           g_strerror (buf[1]));
2335
0
              break;
2336
2337
0
            default:
2338
0
              g_set_error (error,
2339
0
                           G_SPAWN_ERROR,
2340
0
                           G_SPAWN_ERROR_FAILED,
2341
0
                           _("Unknown error executing child process ā€œ%sā€"),
2342
0
                           argv[0]);
2343
0
              break;
2344
0
            }
2345
2346
0
          goto cleanup_and_fail;
2347
0
        }
2348
2349
      /* Get child pid from intermediate child pipe. */
2350
0
      if (intermediate_child)
2351
0
        {
2352
0
          n_ints = 0;
2353
          
2354
0
          if (!read_ints (g_unix_pipe_get (&child_pid_report_pipe, G_UNIX_PIPE_END_READ),
2355
0
                          buf, 1, &n_ints, error))
2356
0
            goto cleanup_and_fail;
2357
2358
0
          if (n_ints < 1)
2359
0
            {
2360
0
              int errsv = errno;
2361
2362
0
              g_set_error (error,
2363
0
                           G_SPAWN_ERROR,
2364
0
                           G_SPAWN_ERROR_FAILED,
2365
0
                           _("Failed to read enough data from child pid pipe (%s)"),
2366
0
                           g_strerror (errsv));
2367
0
              goto cleanup_and_fail;
2368
0
            }
2369
0
          else
2370
0
            {
2371
              /* we have the child pid */
2372
0
              pid = buf[0];
2373
0
            }
2374
0
        }
2375
      
2376
      /* Success against all odds! return the information */
2377
0
      g_unix_pipe_close (&child_err_report_pipe, G_UNIX_PIPE_END_READ, NULL);
2378
0
      g_unix_pipe_close (&child_pid_report_pipe, G_UNIX_PIPE_END_READ, NULL);
2379
2380
0
      g_free (search_path_buffer_heap);
2381
0
      g_free (argv_buffer_heap);
2382
0
      g_free (source_fds_copy);
2383
2384
0
      if (child_pid)
2385
0
        *child_pid = pid;
2386
2387
0
      goto success;
2388
0
    }
2389
2390
0
success:
2391
  /* Close the uncared-about ends of the pipes */
2392
0
  g_unix_pipe_close (&stdin_pipe, G_UNIX_PIPE_END_READ, NULL);
2393
0
  g_unix_pipe_close (&stdout_pipe, G_UNIX_PIPE_END_WRITE, NULL);
2394
0
  g_unix_pipe_close (&stderr_pipe, G_UNIX_PIPE_END_WRITE, NULL);
2395
2396
0
  if (stdin_pipe_out != NULL)
2397
0
    *stdin_pipe_out = g_unix_pipe_steal (&stdin_pipe, G_UNIX_PIPE_END_WRITE);
2398
2399
0
  if (stdout_pipe_out != NULL)
2400
0
    *stdout_pipe_out = g_unix_pipe_steal (&stdout_pipe, G_UNIX_PIPE_END_READ);
2401
2402
0
  if (stderr_pipe_out != NULL)
2403
0
    *stderr_pipe_out = g_unix_pipe_steal (&stderr_pipe, G_UNIX_PIPE_END_READ);
2404
2405
0
  return TRUE;
2406
2407
0
 cleanup_and_fail:
2408
2409
  /* There was an error from the Child, reap the child to avoid it being
2410
     a zombie.
2411
   */
2412
2413
0
  if (pid > 0)
2414
0
  {
2415
0
    wait_failed:
2416
0
     if (waitpid (pid, NULL, 0) < 0)
2417
0
       {
2418
0
          if (errno == EINTR)
2419
0
            goto wait_failed;
2420
0
          else if (errno == ECHILD)
2421
0
            ; /* do nothing, child already reaped */
2422
0
          else
2423
0
            g_warning ("waitpid() should not fail in 'fork_exec'");
2424
0
       }
2425
0
   }
2426
2427
0
  g_unix_pipe_clear (&stdin_pipe);
2428
0
  g_unix_pipe_clear (&stdout_pipe);
2429
0
  g_unix_pipe_clear (&stderr_pipe);
2430
0
  g_unix_pipe_clear (&child_err_report_pipe);
2431
0
  g_unix_pipe_clear (&child_pid_report_pipe);
2432
2433
0
  g_clear_pointer (&search_path_buffer_heap, g_free);
2434
0
  g_clear_pointer (&argv_buffer_heap, g_free);
2435
0
  g_clear_pointer (&source_fds_copy, g_free);
2436
2437
0
  return FALSE;
2438
0
}
2439
2440
/* Based on execvp from GNU C Library */
2441
2442
/* This function is called between fork() and exec() and hence must be
2443
 * async-signal-safe (see signal-safety(7)) until it calls exec(). */
2444
static gboolean
2445
script_execute (const gchar *file,
2446
                gchar      **argv,
2447
                gchar      **argv_buffer,
2448
                gsize        argv_buffer_len,
2449
                gchar      **envp)
2450
0
{
2451
  /* Count the arguments.  */
2452
0
  gsize argc = 0;
2453
0
  while (argv[argc])
2454
0
    ++argc;
2455
2456
  /* Construct an argument list for the shell. */
2457
0
  if (argc + 2 > argv_buffer_len)
2458
0
    return FALSE;
2459
2460
0
  argv_buffer[0] = (char *) "/bin/sh";
2461
0
  argv_buffer[1] = (char *) file;
2462
0
  while (argc > 0)
2463
0
    {
2464
0
      argv_buffer[argc + 1] = argv[argc];
2465
0
      --argc;
2466
0
    }
2467
2468
  /* Execute the shell. */
2469
0
  if (envp)
2470
0
    execve (argv_buffer[0], argv_buffer, envp);
2471
0
  else
2472
0
    execv (argv_buffer[0], argv_buffer);
2473
2474
0
  return TRUE;
2475
0
}
2476
2477
/* This function is called between fork() and exec() and hence must be
2478
 * async-signal-safe (see signal-safety(7)). */
2479
static gchar*
2480
my_strchrnul (const gchar *str, gchar c)
2481
0
{
2482
0
  gchar *p = (gchar*) str;
2483
0
  while (*p && (*p != c))
2484
0
    ++p;
2485
2486
0
  return p;
2487
0
}
2488
2489
/* This function is called between fork() and exec() and hence must be
2490
 * async-signal-safe (see signal-safety(7)) until it calls exec(). */
2491
static gint
2492
g_execute (const gchar  *file,
2493
           gchar       **argv,
2494
           gchar       **argv_buffer,
2495
           gsize         argv_buffer_len,
2496
           gchar       **envp,
2497
           const gchar  *search_path,
2498
           gchar        *search_path_buffer,
2499
           gsize         search_path_buffer_len)
2500
0
{
2501
0
  if (file == NULL || *file == '\0')
2502
0
    {
2503
      /* We check the simple case first. */
2504
0
      errno = ENOENT;
2505
0
      return -1;
2506
0
    }
2507
2508
0
  if (search_path == NULL || strchr (file, '/') != NULL)
2509
0
    {
2510
      /* Don't search when it contains a slash. */
2511
0
      if (envp)
2512
0
        execve (file, argv, envp);
2513
0
      else
2514
0
        execv (file, argv);
2515
      
2516
0
      if (errno == ENOEXEC &&
2517
0
          !script_execute (file, argv, argv_buffer, argv_buffer_len, envp))
2518
0
        {
2519
0
          errno = ENOMEM;
2520
0
          return -1;
2521
0
        }
2522
0
    }
2523
0
  else
2524
0
    {
2525
0
      gboolean got_eacces = 0;
2526
0
      const gchar *path, *p;
2527
0
      gchar *name;
2528
0
      gsize len;
2529
0
      gsize pathlen;
2530
2531
0
      path = search_path;
2532
0
      len = strlen (file) + 1;
2533
0
      pathlen = strlen (path);
2534
0
      name = search_path_buffer;
2535
2536
0
      if (search_path_buffer_len < pathlen + len + 1)
2537
0
        {
2538
0
          errno = ENOMEM;
2539
0
          return -1;
2540
0
        }
2541
2542
      /* Copy the file name at the top, including '\0'  */
2543
0
      memcpy (name + pathlen + 1, file, len);
2544
0
      name = name + pathlen;
2545
      /* And add the slash before the filename  */
2546
0
      *name = '/';
2547
2548
0
      p = path;
2549
0
      do
2550
0
  {
2551
0
    char *startp;
2552
2553
0
    path = p;
2554
0
    p = my_strchrnul (path, ':');
2555
2556
0
    if (p == path)
2557
      /* Two adjacent colons, or a colon at the beginning or the end
2558
             * of 'PATH' means to search the current directory.
2559
             */
2560
0
      startp = name + 1;
2561
0
    else
2562
0
      startp = memcpy (name - (p - path), path, p - path);
2563
2564
    /* Try to execute this name.  If it works, execv will not return.  */
2565
0
          if (envp)
2566
0
            execve (startp, argv, envp);
2567
0
          else
2568
0
            execv (startp, argv);
2569
          
2570
0
          if (errno == ENOEXEC &&
2571
0
              !script_execute (startp, argv, argv_buffer, argv_buffer_len, envp))
2572
0
            {
2573
0
              errno = ENOMEM;
2574
0
              return -1;
2575
0
            }
2576
2577
0
    switch (errno)
2578
0
      {
2579
0
      case EACCES:
2580
        /* Record the we got a 'Permission denied' error.  If we end
2581
               * up finding no executable we can use, we want to diagnose
2582
               * that we did find one but were denied access.
2583
               */
2584
0
        got_eacces = TRUE;
2585
2586
0
              G_GNUC_FALLTHROUGH;
2587
0
      case ENOENT:
2588
0
#ifdef ESTALE
2589
0
      case ESTALE:
2590
0
#endif
2591
0
#ifdef ENOTDIR
2592
0
      case ENOTDIR:
2593
0
#endif
2594
        /* Those errors indicate the file is missing or not executable
2595
               * by us, in which case we want to just try the next path
2596
               * directory.
2597
               */
2598
0
        break;
2599
2600
0
      case ENODEV:
2601
0
      case ETIMEDOUT:
2602
        /* Some strange filesystems like AFS return even
2603
         * stranger error numbers.  They cannot reasonably mean anything
2604
         * else so ignore those, too.
2605
         */
2606
0
        break;
2607
2608
0
      default:
2609
        /* Some other error means we found an executable file, but
2610
               * something went wrong executing it; return the error to our
2611
               * caller.
2612
               */
2613
0
        return -1;
2614
0
      }
2615
0
  }
2616
0
      while (*p++ != '\0');
2617
2618
      /* We tried every element and none of them worked.  */
2619
0
      if (got_eacces)
2620
  /* At least one failure was due to permissions, so report that
2621
         * error.
2622
         */
2623
0
        errno = EACCES;
2624
0
    }
2625
2626
  /* Return the error from the last attempt (probably ENOENT).  */
2627
0
  return -1;
2628
0
}
2629
2630
/**
2631
 * g_spawn_close_pid:
2632
 * @pid: The process reference to close
2633
 *
2634
 * On some platforms, notably Windows, the #GPid type represents a resource
2635
 * which must be closed to prevent resource leaking. g_spawn_close_pid()
2636
 * is provided for this purpose. It should be used on all platforms, even
2637
 * though it doesn't do anything under UNIX.
2638
 **/
2639
void
2640
g_spawn_close_pid (GPid pid)
2641
0
{
2642
0
}