bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

execute, pipe: use posix_spawnp


From: Bruno Haible
Subject: execute, pipe: use posix_spawnp
Date: Mon, 26 Jan 2009 00:19:18 +0100
User-agent: KMail/1.9.9

The replacements for the posix_spawn* family of functions appear to work fine
on all Unix platforms (at least w.r.t. the gettext test suite). I'm therefore
removing the old vfork/fork based alternative code.

Sam Steingold would call this a "dependency creep". But it removes duplicated
code.


2009-01-25  Bruno Haible  <address@hidden>

        * lib/pipe.c: On Unix, assume HAVE_POSIX_SPAWN and use posix_spawnp.
        * m4/pipe.m4 (gl_PIPE): Remove tests for vfork() based code.
        * modules/pipe (Files): Remove m4/posix_spawn.m4.
        (Depends-on): Add spawn, posix_spawnp, posix_spawn_file_actions_init,
        posix_spawn_file_actions_addclose, posix_spawn_file_actions_adddup2,
        posix_spawn_file_actions_addopen, posix_spawn_file_actions_destroy,
        posix_spawnattr_init, posix_spawnattr_setsigmask,
        posix_spawnattr_setflags, posix_spawnattr_destroy.

        * lib/execute.c: On Unix, assume HAVE_POSIX_SPAWN and use posix_spawnp.
        * m4/execute.m4 (gl_EXECUTE): Remove tests for vfork() based code.
        * modules/execute (Files): Remove m4/posix_spawn.m4.
        (Depends-on): Add spawn, posix_spawnp, posix_spawn_file_actions_init,
        posix_spawn_file_actions_addopen, posix_spawn_file_actions_destroy,
        posix_spawnattr_init, posix_spawnattr_setsigmask,
        posix_spawnattr_setflags, posix_spawnattr_destroy.

--- lib/execute.c.orig  2009-01-26 00:02:25.000000000 +0100
+++ lib/execute.c       2009-01-25 11:59:01.000000000 +0100
@@ -44,13 +44,7 @@
 #else
 
 /* Unix API.  */
-# if HAVE_POSIX_SPAWN
-#  include <spawn.h>
-# else
-#  if HAVE_VFORK_H
-#   include <vfork.h>
-#  endif
-# endif
+# include <spawn.h>
 
 #endif
 
@@ -204,7 +198,6 @@
      subprocess to exit with return code 127.  It is implementation
      dependent which error is reported which way.  We treat both cases as
      equivalent.  */
-#if HAVE_POSIX_SPAWN
   sigset_t blocked_signals;
   posix_spawn_file_actions_t actions;
   bool actions_allocated;
@@ -212,11 +205,7 @@
   bool attrs_allocated;
   int err;
   pid_t child;
-#else
-  int child;
-#endif
 
-#if HAVE_POSIX_SPAWN
   if (slave_process)
     {
       sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
@@ -274,48 +263,6 @@
   posix_spawn_file_actions_destroy (&actions);
   if (attrs_allocated)
     posix_spawnattr_destroy (&attrs);
-#else
-  if (slave_process)
-    block_fatal_signals ();
-  /* Use vfork() instead of fork() for efficiency.  */
-  if ((child = vfork ()) == 0)
-    {
-      /* Child process code.  */
-      int nullinfd;
-      int nulloutfd;
-
-      if ((!null_stdin
-          || ((nullinfd = open ("/dev/null", O_RDONLY, 0)) >= 0
-              && (nullinfd == STDIN_FILENO
-                  || (dup2 (nullinfd, STDIN_FILENO) >= 0
-                      && close (nullinfd) >= 0))))
-         && (!(null_stdout || null_stderr)
-             || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
-                 && (!null_stdout
-                     || nulloutfd == STDOUT_FILENO
-                     || dup2 (nulloutfd, STDOUT_FILENO) >= 0)
-                 && (!null_stderr
-                     || nulloutfd == STDERR_FILENO
-                     || dup2 (nulloutfd, STDERR_FILENO) >= 0)
-                 && ((null_stdout && nulloutfd == STDOUT_FILENO)
-                     || (null_stderr && nulloutfd == STDERR_FILENO)
-                     || close (nulloutfd) >= 0)))
-         && (!slave_process || (unblock_fatal_signals (), true)))
-       execvp (prog_path, prog_argv);
-      _exit (127);
-    }
-  if (child == -1)
-    {
-      if (slave_process)
-       unblock_fatal_signals ();
-      if (termsigp != NULL)
-       *termsigp = 0;
-      if (exit_on_error || !null_stderr)
-       error (exit_on_error ? EXIT_FAILURE : 0, errno,
-              _("%s subprocess failed"), progname);
-      return 127;
-    }
-#endif
   if (slave_process)
     {
       register_slave_subprocess (child);
--- lib/pipe.c.orig     2009-01-26 00:02:26.000000000 +0100
+++ lib/pipe.c  2009-01-25 11:59:01.000000000 +0100
@@ -44,13 +44,7 @@
 #else
 
 /* Unix API.  */
-# if HAVE_POSIX_SPAWN
-#  include <spawn.h>
-# else
-#  if HAVE_VFORK_H
-#   include <vfork.h>
-#  endif
-# endif
+# include <spawn.h>
 
 #endif
 
@@ -253,7 +247,6 @@
   /* Unix API.  */
   int ifd[2];
   int ofd[2];
-# if HAVE_POSIX_SPAWN
   sigset_t blocked_signals;
   posix_spawn_file_actions_t actions;
   bool actions_allocated;
@@ -261,9 +254,6 @@
   bool attrs_allocated;
   int err;
   pid_t child;
-# else
-  int child;
-# endif
 
   if (pipe_stdout)
     if (pipe (ifd) < 0
@@ -282,7 +272,6 @@
  *
  */
 
-# if HAVE_POSIX_SPAWN
   if (slave_process)
     {
       sigprocmask (SIG_SETMASK, NULL, &blocked_signals);
@@ -370,64 +359,6 @@
   posix_spawn_file_actions_destroy (&actions);
   if (attrs_allocated)
     posix_spawnattr_destroy (&attrs);
-# else
-  if (slave_process)
-    block_fatal_signals ();
-  /* Use vfork() instead of fork() for efficiency.  */
-  if ((child = vfork ()) == 0)
-    {
-      /* Child process code.  */
-      int nulloutfd;
-      int stdinfd;
-      int stdoutfd;
-
-      if ((!pipe_stdin || dup2 (ofd[0], STDIN_FILENO) >= 0)
-         && (!pipe_stdout || dup2 (ifd[1], STDOUT_FILENO) >= 0)
-         && (!pipe_stdin || close (ofd[0]) >= 0)
-         && (!pipe_stdout || close (ifd[1]) >= 0)
-         && (!pipe_stdin || close (ofd[1]) >= 0)
-         && (!pipe_stdout || close (ifd[0]) >= 0)
-         && (!null_stderr
-             || ((nulloutfd = open ("/dev/null", O_RDWR, 0)) >= 0
-                 && (nulloutfd == STDERR_FILENO
-                     || (dup2 (nulloutfd, STDERR_FILENO) >= 0
-                         && close (nulloutfd) >= 0))))
-         && (pipe_stdin
-             || prog_stdin == NULL
-             || ((stdinfd = open (prog_stdin, O_RDONLY, 0)) >= 0
-                 && (stdinfd == STDIN_FILENO
-                     || (dup2 (stdinfd, STDIN_FILENO) >= 0
-                         && close (stdinfd) >= 0))))
-         && (pipe_stdout
-             || prog_stdout == NULL
-             || ((stdoutfd = open (prog_stdout, O_WRONLY, 0)) >= 0
-                 && (stdoutfd == STDOUT_FILENO
-                     || (dup2 (stdoutfd, STDOUT_FILENO) >= 0
-                         && close (stdoutfd) >= 0))))
-         && (!slave_process || (unblock_fatal_signals (), true)))
-       execvp (prog_path, prog_argv);
-      _exit (127);
-    }
-  if (child == -1)
-    {
-      if (slave_process)
-       unblock_fatal_signals ();
-      if (exit_on_error || !null_stderr)
-       error (exit_on_error ? EXIT_FAILURE : 0, errno,
-              _("%s subprocess failed"), progname);
-      if (pipe_stdout)
-       {
-         close (ifd[0]);
-         close (ifd[1]);
-       }
-      if (pipe_stdin)
-       {
-         close (ofd[0]);
-         close (ofd[1]);
-       }
-      return -1;
-    }
-# endif
   if (slave_process)
     {
       register_slave_subprocess (child);
--- m4/execute.m4.orig  2009-01-26 00:02:26.000000000 +0100
+++ m4/execute.m4       2009-01-25 11:59:01.000000000 +0100
@@ -1,4 +1,4 @@
-# execute.m4 serial 3
+# execute.m4 serial 4
 dnl Copyright (C) 2003, 2008, 2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -9,15 +9,4 @@
   dnl Prerequisites of lib/execute.c.
   AC_REQUIRE([AC_C_INLINE])
   AC_REQUIRE([AC_TYPE_MODE_T])
-  AC_CHECK_HEADERS_ONCE([unistd.h])
-  AC_REQUIRE([AC_FUNC_FORK])
-  AC_CHECK_FUNC([posix_spawn],
-    [gl_POSIX_SPAWN_WORKS
-     case "$gl_cv_func_posix_spawn_works" in
-       *yes)
-         AC_DEFINE([HAVE_POSIX_SPAWN], [1],
-           [Define if you have the posix_spawn() function and it works.])
-         ;;
-     esac
-    ])
 ])
--- m4/pipe.m4.orig     2009-01-26 00:02:26.000000000 +0100
+++ m4/pipe.m4  2009-01-25 11:59:01.000000000 +0100
@@ -1,4 +1,4 @@
-# pipe.m4 serial 3
+# pipe.m4 serial 4
 dnl Copyright (C) 2004, 2008, 2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -6,19 +6,7 @@
 
 AC_DEFUN([gl_PIPE],
 [
-  dnl Prerequisites of lib/pipe.h.
-  AC_CHECK_HEADERS_ONCE([unistd.h])
   dnl Prerequisites of lib/pipe.c.
   AC_REQUIRE([AC_C_INLINE])
   AC_REQUIRE([AC_TYPE_MODE_T])
-  AC_REQUIRE([AC_FUNC_FORK])
-  AC_CHECK_FUNC([posix_spawn],
-    [gl_POSIX_SPAWN_WORKS
-     case "$gl_cv_func_posix_spawn_works" in
-       *yes)
-         AC_DEFINE([HAVE_POSIX_SPAWN], [1],
-           [Define if you have the posix_spawn() function and it works.])
-         ;;
-     esac
-    ])
 ])
--- modules/execute.orig        2009-01-26 00:02:26.000000000 +0100
+++ modules/execute     2009-01-25 11:59:01.000000000 +0100
@@ -6,7 +6,6 @@
 lib/execute.c
 lib/w32spawn.h
 m4/execute.m4
-m4/posix_spawn.m4
 
 Depends-on:
 error
@@ -14,6 +13,15 @@
 fatal-signal
 wait-process
 gettext-h
+spawn
+posix_spawnp
+posix_spawn_file_actions_init
+posix_spawn_file_actions_addopen
+posix_spawn_file_actions_destroy
+posix_spawnattr_init
+posix_spawnattr_setsigmask
+posix_spawnattr_setflags
+posix_spawnattr_destroy
 stdbool
 strpbrk
 unistd
--- modules/pipe.orig   2009-01-26 00:02:26.000000000 +0100
+++ modules/pipe        2009-01-25 11:59:01.000000000 +0100
@@ -6,7 +6,6 @@
 lib/pipe.c
 lib/w32spawn.h
 m4/pipe.m4
-m4/posix_spawn.m4
 
 Depends-on:
 environ
@@ -15,6 +14,17 @@
 fatal-signal
 gettext-h
 open
+spawn
+posix_spawnp
+posix_spawn_file_actions_init
+posix_spawn_file_actions_addclose
+posix_spawn_file_actions_adddup2
+posix_spawn_file_actions_addopen
+posix_spawn_file_actions_destroy
+posix_spawnattr_init
+posix_spawnattr_setsigmask
+posix_spawnattr_setflags
+posix_spawnattr_destroy
 stdbool
 strpbrk
 unistd




reply via email to

[Prev in Thread] Current Thread [Next in Thread]