From b16df31358a0e412794bf011d38bafcc692c99b3 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 12 Sep 2001 12:13:59 +0000 Subject: Executing programs, without pipe. --- lib/ChangeLog | 7 +++ lib/Makefile.am | 16 ++--- lib/execute.c | 179 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/execute.h | 32 ++++++++++ 4 files changed, 226 insertions(+), 8 deletions(-) create mode 100644 lib/execute.c create mode 100644 lib/execute.h diff --git a/lib/ChangeLog b/lib/ChangeLog index 03e041a..06f1f52 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,12 @@ 2001-09-02 Bruno Haible + * execute.h: New file. + * execute.c: New file. + * Makefile.am (libnlsut_a_SOURCES): Add execute.c. + (noinst_HEADERS): Add execute.h. + +2001-09-02 Bruno Haible + * gcd.h: New file. * gcd.c: New file. * Makefile.am (libnlsut_a_SOURCES): Add gcd.c. diff --git a/lib/Makefile.am b/lib/Makefile.am index 488ac0a..0ad1557 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -27,17 +27,17 @@ strncasecmp.c strstr.c strtol.c strtoul.c vasprintf.c \ stdbool.h.in \ gen-lbrkprop.c 3level.h -libnlsut_a_SOURCES = basename.c c-ctype.c concatpath.c findprog.c fstrcmp.c \ -full-write.c gcd.c getopt.c getopt1.c hash.c linebreak.c localcharset.c \ -mbswidth.c obstack.c pipe-bidi.c pipe-in.c pipe-out.c progname.c safe-read.c \ -wait-process.c xerror.c xgetcwd.c xmalloc.c xstrdup.c +libnlsut_a_SOURCES = basename.c c-ctype.c concatpath.c execute.c findprog.c \ +fstrcmp.c full-write.c gcd.c getopt.c getopt1.c hash.c linebreak.c \ +localcharset.c mbswidth.c obstack.c pipe-bidi.c pipe-in.c pipe-out.c \ +progname.c safe-read.c wait-process.c xerror.c xgetcwd.c xmalloc.c xstrdup.c libnlsut_a_LIBADD = @ALLOCA@ @LIBOBJS@ -noinst_HEADERS = c-ctype.h error.h findprog.h fstrcmp.h full-write.h gcd.h \ -getline.h getopt.h hash.h lbrkprop.h linebreak.h mbswidth.h obstack.h \ -pathmax.h pipe.h progname.h safe-read.h system.h utf8-ucs4.h utf16-ucs4.h \ -wait-process.h xerror.h +noinst_HEADERS = c-ctype.h error.h execute.h findprog.h fstrcmp.h \ +full-write.h gcd.h getline.h getopt.h hash.h lbrkprop.h linebreak.h mbswidth.h \ +obstack.h pathmax.h pipe.h progname.h safe-read.h system.h utf8-ucs4.h \ +utf16-ucs4.h wait-process.h xerror.h DEFS = -DLIBDIR=\"$(libdir)\" @DEFS@ INCLUDES = -I. -I$(srcdir) -I.. -I../intl diff --git a/lib/execute.c b/lib/execute.c new file mode 100644 index 0000000..48cba72 --- /dev/null +++ b/lib/execute.c @@ -0,0 +1,179 @@ +/* Creation of autonomous subprocesses. + Copyright (C) 2001 Free Software Foundation, Inc. + Written by Bruno Haible , 2001. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +/* Specification. */ +#include "execute.h" + +#include +#include +#include +#include + +#ifdef HAVE_UNISTD_H +# include +#endif + +#ifdef HAVE_POSIX_SPAWN +# include +#else +# ifdef HAVE_VFORK_H +# include +# endif +#endif + +#include "error.h" +#include "wait-process.h" +#include "libgettext.h" + +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif +#ifndef STDOUT_FILENO +# define STDOUT_FILENO 1 +#endif +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif + +#define _(str) gettext (str) + + +/* Prototypes for local functions. Needed to ensure compiler checking of + function argument counts despite of K&R C function definition syntax. */ +#ifdef EINTR +static inline int nonintr_close PARAMS ((int fd)); +static inline int nonintr_open PARAMS ((const char *pathname, int oflag, + mode_t mode)); +#endif + + +#ifdef EINTR + +/* EINTR handling for close(), open(). + These functions can return -1/EINTR even though we don't have any + signal handlers set up, namely when we get interrupted via SIGSTOP. */ + +static inline int +nonintr_close (fd) + int fd; +{ + int retval; + + do + retval = close (fd); + while (retval < 0 && errno == EINTR); + + return retval; +} +#define close nonintr_close + +static inline int +nonintr_open (pathname, oflag, mode) + const char *pathname; + int oflag; + mode_t mode; +{ + int retval; + + do + retval = open (pathname, oflag, mode); + while (retval < 0 && errno == EINTR); + + return retval; +} +#define open nonintr_open + +#endif + + +/* Execute a command, optionally redirecting any of the three standard file + descriptors to /dev/null. Exit if it didn't terminate correctly. + Otherwise return its exit code. */ +int +execute (progname, prog_path, prog_argv, null_stdin, null_stdout, null_stderr) + const char *progname; + const char *prog_path; + char **prog_argv; + bool null_stdin; + bool null_stdout; + bool null_stderr; +{ +#if HAVE_POSIX_SPAWN + posix_spawn_file_actions_t actions; + int err; + pid_t child; +#else + int child; +#endif + +#if HAVE_POSIX_SPAWN + if ((err = posix_spawn_file_actions_init (&actions)) != 0 + || (null_stdin + && (err = posix_spawn_file_actions_addopen (&actions, STDIN_FILENO, + "/dev/null", O_RDONLY, 0)) + != 0) + || (null_stdout + && (err = posix_spawn_file_actions_addopen (&actions, STDOUT_FILENO, + "/dev/null", O_RDWR, 0)) + != 0) + || (null_stderr + && (err = posix_spawn_file_actions_addopen (&actions, STDERR_FILENO, + "/dev/null", O_RDWR, 0)) + != 0) + || (err = posix_spawnp (&child, prog_path, &actions, NULL, prog_argv, + environ)) != 0) + error (EXIT_FAILURE, err, _("%s subprocess failed"), progname); + posix_spawn_file_actions_destroy (&actions); +#else + /* 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)))) + execvp (prog_path, prog_argv); + _exit (-1); + } + if (child == -1) + error (EXIT_FAILURE, errno, _("%s subprocess failed"), progname); +#endif + + return wait_subprocess (child, progname); +} diff --git a/lib/execute.h b/lib/execute.h new file mode 100644 index 0000000..7a2cc49 --- /dev/null +++ b/lib/execute.h @@ -0,0 +1,32 @@ +/* Creation of autonomous subprocesses. + Copyright (C) 2001 Free Software Foundation, Inc. + Written by Bruno Haible , 2001. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#ifndef _EXECUTE_H +#define _EXECUTE_H + +#include + +/* Execute a command, optionally redirecting any of the three standard file + descriptors to /dev/null. Exit if it didn't terminate correctly. + Otherwise return its exit code. */ +extern int execute PARAMS ((const char *progname, + const char *prog_path, char **prog_argv, + bool null_stdin, + bool null_stdout, bool null_stderr)); + +#endif /* _EXECUTE_H */ -- cgit v1.1