~ubuntu-branches/debian/sid/glib2.0/sid

« back to all changes in this revision

Viewing changes to glib/gspawn.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 06:25:57 UTC
  • mfrom: (1.27.14) (3.1.181 experimental)
  • Revision ID: package-import@ubuntu.com-20130508062557-i7gbku66mls70gi2
Tags: 2.36.1-2
Merge experimental branch, upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#endif /* HAVE_SYS_RESOURCE_H */
43
43
 
44
44
#include "gspawn.h"
 
45
#include "gthread.h"
 
46
#include "glib/gstdio.h"
45
47
 
46
48
#include "genviron.h"
47
49
#include "gmem.h"
 
50
#include "gmain.h"
48
51
#include "gshell.h"
49
52
#include "gstring.h"
50
53
#include "gstrfuncs.h"
64
67
static gint g_execute (const gchar  *file,
65
68
                       gchar **argv,
66
69
                       gchar **envp,
67
 
                       gboolean search_path);
 
70
                       gboolean search_path,
 
71
                       gboolean search_path_from_envp);
68
72
 
69
73
static gboolean make_pipe            (gint                  p[2],
70
74
                                      GError              **error);
74
78
                                      gchar               **envp,
75
79
                                      gboolean              close_descriptors,
76
80
                                      gboolean              search_path,
 
81
                                      gboolean              search_path_from_envp,
77
82
                                      gboolean              stdout_to_null,
78
83
                                      gboolean              stderr_to_null,
79
84
                                      gboolean              child_inherits_stdin,
86
91
                                      gint                 *standard_error,
87
92
                                      GError              **error);
88
93
 
89
 
GQuark
90
 
g_spawn_error_quark (void)
91
 
{
92
 
  return g_quark_from_static_string ("g-exec-error-quark");
93
 
}
 
94
G_DEFINE_QUARK (g-exec-error-quark, g_spawn_error)
 
95
G_DEFINE_QUARK (g-spawn-exit-error-quark, g_spawn_exit_error)
94
96
 
95
97
/**
96
98
 * g_spawn_async:
149
151
 * on a file descriptor twice, and another thread has
150
152
 * re-opened it since the first close)
151
153
 */
152
 
static gint
 
154
static void
153
155
close_and_invalidate (gint *fd)
154
156
{
155
 
  gint ret;
156
 
 
157
157
  if (*fd < 0)
158
 
    return -1;
 
158
    return;
159
159
  else
160
160
    {
161
 
    again:
162
 
      ret = close (*fd);
163
 
      if (ret == -1 && errno == EINTR)
164
 
        goto again;
 
161
      (void) g_close (*fd, NULL);
165
162
      *fd = -1;
166
163
    }
167
 
 
168
 
  return ret;
169
164
}
170
165
 
171
166
/* Some versions of OS X define READ_OK in public headers */
212
207
    }
213
208
}
214
209
 
 
210
typedef struct {
 
211
  GMainLoop *loop;
 
212
  gint *status_p;
 
213
} SyncWaitpidData;
 
214
 
 
215
static void
 
216
on_sync_waitpid (GPid     pid,
 
217
                 gint     status,
 
218
                 gpointer user_data)
 
219
{
 
220
  SyncWaitpidData *data = user_data;
 
221
  *(data->status_p) = status;
 
222
  g_main_loop_quit (data->loop);
 
223
}
 
224
 
215
225
/**
216
226
 * g_spawn_sync:
217
227
 * @working_directory: (allow-none): child's current working directory, or %NULL to inherit parent's
230
240
 * if those parameters are non-%NULL. Note that you must set the  
231
241
 * %G_SPAWN_STDOUT_TO_DEV_NULL and %G_SPAWN_STDERR_TO_DEV_NULL flags when
232
242
 * passing %NULL for @standard_output and @standard_error.
233
 
 * If @exit_status is non-%NULL, the exit status of the child is stored
234
 
 * there as it would be returned by waitpid(); standard UNIX macros such 
235
 
 * as WIFEXITED() and WEXITSTATUS() must be used to evaluate the exit status.
236
 
 * Note that this function call waitpid() even if @exit_status is %NULL, and
237
 
 * does not accept the %G_SPAWN_DO_NOT_REAP_CHILD flag.
238
 
 * If an error occurs, no data is returned in @standard_output, 
239
 
 * @standard_error, or @exit_status. 
 
243
 *
 
244
 * If @exit_status is non-%NULL, the platform-specific exit status of
 
245
 * the child is stored there; see the doucumentation of
 
246
 * g_spawn_check_exit_status() for how to use and interpret this.
 
247
 * Note that it is invalid to pass %G_SPAWN_DO_NOT_REAP_CHILD in
 
248
 * @flags.
 
249
 *
 
250
 * If an error occurs, no data is returned in @standard_output,
 
251
 * @standard_error, or @exit_status.
240
252
 *
241
253
 * This function calls g_spawn_async_with_pipes() internally; see that
242
254
 * function for full details on the other parameters and details on
265
277
  GString *errstr = NULL;
266
278
  gboolean failed;
267
279
  gint status;
 
280
  SyncWaitpidData waitpid_data;
268
281
  
269
282
  g_return_val_if_fail (argv != NULL, FALSE);
270
283
  g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
288
301
                             envp,
289
302
                             !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
290
303
                             (flags & G_SPAWN_SEARCH_PATH) != 0,
 
304
                             (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
291
305
                             (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
292
306
                             (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
293
307
                             (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
396
410
    close_and_invalidate (&outpipe);
397
411
  if (errpipe >= 0)
398
412
    close_and_invalidate (&errpipe);
399
 
  
400
 
  /* Wait for child to exit, even if we have
401
 
   * an error pending.
 
413
 
 
414
  /* Now create a temporary main context and loop, with just one
 
415
   * waitpid source.  We used to invoke waitpid() directly here, but
 
416
   * this way we unify with the worker thread in gmain.c.
402
417
   */
403
 
 again:
404
 
      
405
 
  ret = waitpid (pid, &status, 0);
406
 
 
407
 
  if (ret < 0)
408
 
    {
409
 
      if (errno == EINTR)
410
 
        goto again;
411
 
      else if (errno == ECHILD)
412
 
        {
413
 
          if (exit_status)
414
 
            {
415
 
              g_warning ("In call to g_spawn_sync(), exit status of a child process was requested but SIGCHLD action was set to SIG_IGN and ECHILD was received by waitpid(), so exit status can't be returned. This is a bug in the program calling g_spawn_sync(); either don't request the exit status, or don't set the SIGCHLD action.");
416
 
            }
417
 
          else
418
 
            {
419
 
              /* We don't need the exit status. */
420
 
            }
421
 
        }
422
 
      else
423
 
        {
424
 
          if (!failed) /* avoid error pileups */
425
 
            {
426
 
              int errsv = errno;
427
 
 
428
 
              failed = TRUE;
429
 
                  
430
 
              g_set_error (error,
431
 
                           G_SPAWN_ERROR,
432
 
                           G_SPAWN_ERROR_READ,
433
 
                           _("Unexpected error in waitpid() (%s)"),
434
 
                           g_strerror (errsv));
435
 
            }
436
 
        }
437
 
    }
 
418
  {
 
419
    GMainContext *context;
 
420
    GMainLoop *loop;
 
421
    GSource *source;
 
422
 
 
423
    context = g_main_context_new ();
 
424
    loop = g_main_loop_new (context, TRUE);
 
425
 
 
426
    waitpid_data.loop = loop;
 
427
    waitpid_data.status_p = &status;
 
428
    
 
429
    source = g_child_watch_source_new (pid);
 
430
    g_source_set_callback (source, (GSourceFunc)on_sync_waitpid, &waitpid_data, NULL);
 
431
    g_source_attach (source, context);
 
432
    g_source_unref (source);
 
433
    
 
434
    g_main_loop_run (loop);
 
435
 
 
436
    g_main_context_unref (context);
 
437
    g_main_loop_unref (loop);
 
438
  }
438
439
  
439
440
  if (failed)
440
441
    {
480
481
 * should be a %NULL-terminated array of strings, to be passed as the
481
482
 * argument vector for the child. The first string in @argv is of
482
483
 * course the name of the program to execute. By default, the name of
483
 
 * the program must be a full path; the <envar>PATH</envar> shell variable 
484
 
 * will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
 
484
 * the program must be a full path. If @flags contains the 
 
485
 * %G_SPAWN_SEARCH_PATH flag, the <envar>PATH</envar> environment variable 
 
486
 * is used to search for the executable. If @flags contains the
 
487
 * %G_SPAWN_SEARCH_PATH_FROM_ENVP flag, the <envar>PATH</envar> variable from 
 
488
 * @envp is used to search for the executable.
 
489
 * If both the %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP
 
490
 * flags are set, the <envar>PATH</envar> variable from @envp takes precedence 
 
491
 * over the environment variable.
 
492
 *
485
493
 * If the program name is not a full path and %G_SPAWN_SEARCH_PATH flag is not
486
494
 * used, then the program will be run from the current directory (or
487
495
 * @working_directory, if specified); this might be unexpected or even
547
555
 * descriptors except stdin/stdout/stderr will be closed before
548
556
 * calling exec() in the child. %G_SPAWN_SEARCH_PATH 
549
557
 * means that <literal>argv[0]</literal> need not be an absolute path, it
550
 
 * will be looked for in the user's <envar>PATH</envar>. 
 
558
 * will be looked for in the <envar>PATH</envar> environment variable.
 
559
 * %G_SPAWN_SEARCH_PATH_FROM_ENVP means need not be an absolute path, it
 
560
 * will be looked for in the <envar>PATH</envar> variable from @envp. If
 
561
 * both %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP are used,
 
562
 * the value from @envp takes precedence over the environment.
551
563
 * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output will 
552
564
 * be discarded, instead of going to the same location as the parent's 
553
565
 * standard output. If you use this flag, @standard_output must be %NULL.
656
668
                               envp,
657
669
                               !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
658
670
                               (flags & G_SPAWN_SEARCH_PATH) != 0,
 
671
                               (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
659
672
                               (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
660
673
                               (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
661
674
                               (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
686
699
 * appropriate. Possible errors are those from g_spawn_sync() and those
687
700
 * from g_shell_parse_argv().
688
701
 *
689
 
 * If @exit_status is non-%NULL, the exit status of the child is stored there as
690
 
 * it would be returned by waitpid(); standard UNIX macros such as WIFEXITED()
691
 
 * and WEXITSTATUS() must be used to evaluate the exit status.
 
702
 * If @exit_status is non-%NULL, the platform-specific exit status of
 
703
 * the child is stored there; see the documentation of
 
704
 * g_spawn_check_exit_status() for how to use and interpret this.
692
705
 * 
693
706
 * On Windows, please note the implications of g_shell_parse_argv()
694
707
 * parsing @command_line. Parsing is done according to Unix shell rules, not 
778
791
  return retval;
779
792
}
780
793
 
 
794
/**
 
795
 * g_spawn_check_exit_status:
 
796
 * @exit_status: An exit code as returned from g_spawn_sync()
 
797
 * @error: a #GError
 
798
 *
 
799
 * Set @error if @exit_status indicates the child exited abnormally
 
800
 * (e.g. with a nonzero exit code, or via a fatal signal).
 
801
 *
 
802
 * The g_spawn_sync() and g_child_watch_add() family of APIs return an
 
803
 * exit status for subprocesses encoded in a platform-specific way.
 
804
 * On Unix, this is guaranteed to be in the same format
 
805
 * <literal>waitpid(2)</literal> returns, and on Windows it is
 
806
 * guaranteed to be the result of
 
807
 * <literal>GetExitCodeProcess()</literal>.  Prior to the introduction
 
808
 * of this function in GLib 2.34, interpreting @exit_status required
 
809
 * use of platform-specific APIs, which is problematic for software
 
810
 * using GLib as a cross-platform layer.
 
811
 *
 
812
 * Additionally, many programs simply want to determine whether or not
 
813
 * the child exited successfully, and either propagate a #GError or
 
814
 * print a message to standard error.  In that common case, this
 
815
 * function can be used.  Note that the error message in @error will
 
816
 * contain human-readable information about the exit status.
 
817
 *
 
818
 * The <literal>domain</literal> and <literal>code</literal> of @error
 
819
 * have special semantics in the case where the process has an "exit
 
820
 * code", as opposed to being killed by a signal.  On Unix, this
 
821
 * happens if <literal>WIFEXITED</literal> would be true of
 
822
 * @exit_status.  On Windows, it is always the case.
 
823
 *
 
824
 * The special semantics are that the actual exit code will be the
 
825
 * code set in @error, and the domain will be %G_SPAWN_EXIT_ERROR.
 
826
 * This allows you to differentiate between different exit codes.
 
827
 *
 
828
 * If the process was terminated by some means other than an exit
 
829
 * status, the domain will be %G_SPAWN_ERROR, and the code will be
 
830
 * %G_SPAWN_ERROR_FAILED.
 
831
 *
 
832
 * This function just offers convenience; you can of course also check
 
833
 * the available platform via a macro such as %G_OS_UNIX, and use
 
834
 * <literal>WIFEXITED()</literal> and <literal>WEXITSTATUS()</literal>
 
835
 * on @exit_status directly.  Do not attempt to scan or parse the
 
836
 * error message string; it may be translated and/or change in future
 
837
 * versions of GLib.
 
838
 *
 
839
 * Returns: %TRUE if child exited successfully, %FALSE otherwise (and @error will be set)
 
840
 * Since: 2.34
 
841
 */
 
842
gboolean
 
843
g_spawn_check_exit_status (gint      exit_status,
 
844
                           GError  **error)
 
845
{
 
846
  gboolean ret = FALSE;
 
847
 
 
848
  if (WIFEXITED (exit_status))
 
849
    {
 
850
      if (WEXITSTATUS (exit_status) != 0)
 
851
        {
 
852
          g_set_error (error, G_SPAWN_EXIT_ERROR, WEXITSTATUS (exit_status),
 
853
                       _("Child process exited with code %ld"),
 
854
                       (long) WEXITSTATUS (exit_status));
 
855
          goto out;
 
856
        }
 
857
    }
 
858
  else if (WIFSIGNALED (exit_status))
 
859
    {
 
860
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
861
                   _("Child process killed by signal %ld"),
 
862
                   (long) WTERMSIG (exit_status));
 
863
      goto out;
 
864
    }
 
865
  else if (WIFSTOPPED (exit_status))
 
866
    {
 
867
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
868
                   _("Child process stopped by signal %ld"),
 
869
                   (long) WSTOPSIG (exit_status));
 
870
      goto out;
 
871
    }
 
872
  else
 
873
    {
 
874
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
 
875
                   _("Child process exited abnormally"));
 
876
      goto out;
 
877
    }
 
878
 
 
879
  ret = TRUE;
 
880
 out:
 
881
  return ret;
 
882
}
 
883
 
781
884
static gint
782
885
exec_err_to_g_error (gint en)
783
886
{
1040
1143
         gchar               **envp,
1041
1144
         gboolean              close_descriptors,
1042
1145
         gboolean              search_path,
 
1146
         gboolean              search_path_from_envp,
1043
1147
         gboolean              stdout_to_null,
1044
1148
         gboolean              stderr_to_null,
1045
1149
         gboolean              child_inherits_stdin,
1133
1237
 
1134
1238
  g_execute (argv[0],
1135
1239
             file_and_argv_zero ? argv + 1 : argv,
1136
 
             envp, search_path);
 
1240
             envp, search_path, search_path_from_envp);
1137
1241
 
1138
1242
  /* Exec failed */
1139
1243
  write_err_and_exit (child_err_report_fd,
1196
1300
                      gchar               **envp,
1197
1301
                      gboolean              close_descriptors,
1198
1302
                      gboolean              search_path,
 
1303
                      gboolean              search_path_from_envp,
1199
1304
                      gboolean              stdout_to_null,
1200
1305
                      gboolean              stderr_to_null,
1201
1306
                      gboolean              child_inherits_stdin,
1303
1408
                       envp,
1304
1409
                       close_descriptors,
1305
1410
                       search_path,
 
1411
                       search_path_from_envp,
1306
1412
                       stdout_to_null,
1307
1413
                       stderr_to_null,
1308
1414
                       child_inherits_stdin,
1332
1438
                   envp,
1333
1439
                   close_descriptors,
1334
1440
                   search_path,
 
1441
                   search_path_from_envp,
1335
1442
                   stdout_to_null,
1336
1443
                   stderr_to_null,
1337
1444
                   child_inherits_stdin,
1533
1640
static void
1534
1641
script_execute (const gchar *file,
1535
1642
                gchar      **argv,
1536
 
                gchar      **envp,
1537
 
                gboolean     search_path)
 
1643
                gchar      **envp)
1538
1644
{
1539
1645
  /* Count the arguments.  */
1540
1646
  int argc = 0;
1579
1685
g_execute (const gchar *file,
1580
1686
           gchar      **argv,
1581
1687
           gchar      **envp,
1582
 
           gboolean     search_path)
 
1688
           gboolean     search_path,
 
1689
           gboolean     search_path_from_envp)
1583
1690
{
1584
1691
  if (*file == '\0')
1585
1692
    {
1588
1695
      return -1;
1589
1696
    }
1590
1697
 
1591
 
  if (!search_path || strchr (file, '/') != NULL)
 
1698
  if (!(search_path || search_path_from_envp) || strchr (file, '/') != NULL)
1592
1699
    {
1593
1700
      /* Don't search when it contains a slash. */
1594
1701
      if (envp)
1597
1704
        execv (file, argv);
1598
1705
      
1599
1706
      if (errno == ENOEXEC)
1600
 
        script_execute (file, argv, envp, FALSE);
 
1707
        script_execute (file, argv, envp);
1601
1708
    }
1602
1709
  else
1603
1710
    {
1607
1714
      gsize len;
1608
1715
      gsize pathlen;
1609
1716
 
1610
 
      path = g_getenv ("PATH");
 
1717
      path = NULL;
 
1718
      if (search_path_from_envp)
 
1719
        path = g_environ_getenv (envp, "PATH");
 
1720
      if (search_path && path == NULL)
 
1721
        path = g_getenv ("PATH");
 
1722
 
1611
1723
      if (path == NULL)
1612
1724
        {
1613
1725
          /* There is no `PATH' in the environment.  The default
1656
1768
            execv (startp, argv);
1657
1769
          
1658
1770
          if (errno == ENOEXEC)
1659
 
            script_execute (startp, argv, envp, search_path);
 
1771
            script_execute (startp, argv, envp);
1660
1772
 
1661
1773
          switch (errno)
1662
1774
            {