]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/posix/system.c
Update.
[thirdparty/glibc.git] / sysdeps / posix / system.c
1 /* Copyright (C) 1991, 92, 94, 95, 96, 97 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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. */
18
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>
26
27
28 #ifndef HAVE_GNU_LD
29 #define __environ environ
30 #endif
31
32 #define SHELL_PATH "/bin/sh" /* Path of the shell. */
33 #define SHELL_NAME "sh" /* Name to give it. */
34
35 /* Execute LINE as a shell command, returning its status. */
36 int
37 __libc_system (const char *line)
38 {
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
45
46 if (line == NULL)
47 /* This signals that we have a command processor available. */
48 return 1;
49
50 sa.sa_handler = SIG_IGN;
51 sa.sa_flags = 0;
52 __sigemptyset (&sa.sa_mask);
53
54 if (__sigaction (SIGINT, &sa, &intr) < 0)
55 return -1;
56 if (__sigaction (SIGQUIT, &sa, &quit) < 0)
57 {
58 save = errno;
59 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
60 __set_errno (save);
61 return -1;
62 }
63
64 #ifndef WAITPID_CANNOT_BLOCK_SIGCHLD
65
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. */
70
71 __sigemptyset (&block);
72 __sigaddset (&block, SIGCHLD);
73 save = errno;
74 if (__sigprocmask (SIG_BLOCK, &block, &omask) < 0)
75 {
76 if (errno == ENOSYS)
77 __set_errno (save);
78 else
79 {
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;
85 }
86 }
87 #define UNBLOCK __sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL)
88 #else
89 #define UNBLOCK 0
90 #endif
91
92 pid = __vfork ();
93 if (pid == (pid_t) 0)
94 {
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;
101
102 /* Restore the signals. */
103 (void) __sigaction (SIGINT, &intr, (struct sigaction *) NULL);
104 (void) __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
105 (void) UNBLOCK;
106
107 /* Exec the shell. */
108 (void) __execve (SHELL_PATH, (char *const *) new_argv, __environ);
109 _exit (127);
110 }
111 else if (pid < (pid_t) 0)
112 /* The fork failed. */
113 status = -1;
114 else
115 /* Parent side. */
116 #ifdef NO_WAITPID
117 {
118 pid_t child;
119 do
120 {
121 child = __wait (&status);
122 if (child <= -1)
123 {
124 status = -1;
125 break;
126 }
127 } while (child != pid);
128 }
129 #else
130 if (__waitpid (pid, &status, 0) != pid)
131 status = -1;
132 #endif
133
134 save = errno;
135 if ((__sigaction (SIGINT, &intr, (struct sigaction *) NULL) |
136 __sigaction (SIGQUIT, &quit, (struct sigaction *) NULL) |
137 UNBLOCK) != 0)
138 {
139 if (errno == ENOSYS)
140 __set_errno (save);
141 else
142 return -1;
143 }
144
145 return status;
146 }
147 weak_alias (__libc_system, system)