update from main archive 960927
[glibc.git] / sysdeps / posix / system.c
blobb1a685c29a111d945c1e8e94ea5b7ae7b12122cb
1 /* Copyright (C) 1991, 92, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/wait.h>
23 #include <signal.h>
24 #include <sys/types.h>
25 #include <errno.h>
28 #ifndef HAVE_GNU_LD
29 #define __environ environ
30 #endif
32 #define SHELL_PATH "/bin/sh" /* Path of the shell. */
33 #define SHELL_NAME "sh" /* Name to give it. */
35 /* Execute LINE as a shell command, returning its status. */
36 int
37 __libc_system (const char *line)
39 int status, save;
40 pid_t pid;
41 struct sigaction sa, intr, quit;
42 #ifndef WAITPID_CANNOT_BLOCK_SIGCHLD
43 sigset_t block, omask;
44 #endif
46 if (line == NULL)
47 return 1;
49 sa.sa_handler = SIG_IGN;
50 sa.sa_flags = 0;
51 __sigemptyset (&sa.sa_mask);
53 if (__sigaction (SIGINT, &sa, &intr) < 0)
54 return -1;
55 if (__sigaction (SIGQUIT, &sa, &quit) < 0)
57 save = errno;
58 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
59 __set_errno (save);
60 return -1;
63 #ifndef WAITPID_CANNOT_BLOCK_SIGCHLD
65 /* SCO 3.2v4 has a bug where `waitpid' will never return if SIGCHLD is
66 blocked. This makes it impossible for `system' to be implemented in
67 compliance with POSIX.2-1992. They have acknowledged that this is a bug
68 but I have not seen nor heard of any forthcoming fix. */
70 __sigemptyset (&block);
71 __sigaddset (&block, SIGCHLD);
72 save = errno;
73 if (__sigprocmask (SIG_BLOCK, &block, &omask) < 0)
75 if (errno == ENOSYS)
76 __set_errno (save);
77 else
79 save = errno;
80 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
81 (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
82 __set_errno (save);
83 return -1;
86 #define UNBLOCK __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL)
87 #else
88 #define UNBLOCK 0
89 #endif
91 pid = __vfork ();
92 if (pid == (pid_t) 0)
94 /* Child side. */
95 const char *new_argv[4];
96 new_argv[0] = SHELL_NAME;
97 new_argv[1] = "-c";
98 new_argv[2] = line;
99 new_argv[3] = NULL;
101 /* Restore the signals. */
102 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
103 (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
104 (void) UNBLOCK;
106 /* Exec the shell. */
107 (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
108 _exit (127);
110 else if (pid < (pid_t) 0)
111 /* The fork failed. */
112 status = -1;
113 else
114 /* Parent side. */
115 #ifdef NO_WAITPID
117 pid_t child;
120 child = __wait (&status);
121 if (child <= -1)
123 status = -1;
124 break;
126 } while (child != pid);
128 #else
129 if (__waitpid (pid, &status, 0) != pid)
130 status = -1;
131 #endif
133 save = errno;
134 if ((__sigaction (SIGINT, &intr, (struct sigaction *) NULL) |
135 __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL) |
136 UNBLOCK) != 0)
138 if (errno == ENOSYS)
139 __set_errno (save);
140 else
141 return -1;
144 return status;
146 weak_alias (__libc_system, system)