Update.
[glibc.git] / sysdeps / posix / system.c
blob1111646bbda518bf3606df7cc3932b876a1e872e
1 /* Copyright (C) 1991, 92, 94, 95, 96, 97 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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, 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 /* This signals that we have a command processor available. */
48 return 1;
50 sa.sa_handler = SIG_IGN;
51 sa.sa_flags = 0;
52 __sigemptyset (&sa.sa_mask);
54 if (__sigaction (SIGINT, &sa, &intr) < 0)
55 return -1;
56 if (__sigaction (SIGQUIT, &sa, &quit) < 0)
58 save = errno;
59 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
60 __set_errno (save);
61 return -1;
64 #ifndef WAITPID_CANNOT_BLOCK_SIGCHLD
66 /* SCO 3.2v4 has a bug where `waitpid' will never return if SIGCHLD is
67 blocked. This makes it impossible for `system' to be implemented in
68 compliance with POSIX.2-1992. They have acknowledged that this is a bug
69 but I have not seen nor heard of any forthcoming fix. */
71 __sigemptyset (&block);
72 __sigaddset (&block, SIGCHLD);
73 save = errno;
74 if (__sigprocmask (SIG_BLOCK, &block, &omask) < 0)
76 if (errno == ENOSYS)
77 __set_errno (save);
78 else
80 save = errno;
81 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
82 (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
83 __set_errno (save);
84 return -1;
87 #define UNBLOCK __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL)
88 #else
89 #define UNBLOCK 0
90 #endif
92 pid = __vfork ();
93 if (pid == (pid_t) 0)
95 /* Child side. */
96 const char *new_argv[4];
97 new_argv[0] = SHELL_NAME;
98 new_argv[1] = "-c";
99 new_argv[2] = line;
100 new_argv[3] = NULL;
102 /* Restore the signals. */
103 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
104 (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
105 (void) UNBLOCK;
107 /* Exec the shell. */
108 (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
109 _exit (127);
111 else if (pid < (pid_t) 0)
112 /* The fork failed. */
113 status = -1;
114 else
115 /* Parent side. */
116 #ifdef NO_WAITPID
118 pid_t child;
121 child = __wait (&status);
122 if (child <= -1)
124 status = -1;
125 break;
127 } while (child != pid);
129 #else
130 if (__waitpid (pid, &status, 0) != pid)
131 status = -1;
132 #endif
134 save = errno;
135 if ((__sigaction (SIGINT, &intr, (struct sigaction *) NULL) |
136 __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL) |
137 UNBLOCK) != 0)
139 if (errno == ENOSYS)
140 __set_errno (save);
141 else
142 return -1;
145 return status;
147 weak_alias (__libc_system, system)