OSDN Git Service

b15d601aa94dfcfc2861674be6881c1552457995
[uclinux-h8/uClibc.git] / libc / stdlib / system.c
1 #include <stdio.h>
2 #include <stddef.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <sys/wait.h>
6
7 /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
8 #include <sys/syscall.h>
9 #if ! defined __NR_vfork
10 #define vfork fork      
11 #endif
12
13 int __libc_system(command)
14 char *command;
15 {
16         int wait_val, pid;
17         __sighandler_t save_quit, save_int, save_chld;
18
19         if (command == 0)
20                 return 1;
21
22         save_quit = signal(SIGQUIT, SIG_IGN);
23         save_int = signal(SIGINT, SIG_IGN);
24         save_chld = signal(SIGCHLD, SIG_DFL);
25
26         if ((pid = vfork()) < 0) {
27                 signal(SIGQUIT, save_quit);
28                 signal(SIGINT, save_int);
29                 signal(SIGCHLD, save_chld);
30                 return -1;
31         }
32         if (pid == 0) {
33                 signal(SIGQUIT, SIG_DFL);
34                 signal(SIGINT, SIG_DFL);
35                 signal(SIGCHLD, SIG_DFL);
36
37                 execl("/bin/sh", "sh", "-c", command, (char *) 0);
38                 _exit(127);
39         }
40         /* Signals are not absolutly guarenteed with vfork */
41         signal(SIGQUIT, SIG_IGN);
42         signal(SIGINT, SIG_IGN);
43
44 #if 0
45         printf("Waiting for child %d\n", pid);
46 #endif
47
48         if (wait4(pid, &wait_val, 0, 0) == -1)
49                 wait_val = -1;
50
51         signal(SIGQUIT, save_quit);
52         signal(SIGINT, save_int);
53         signal(SIGCHLD, save_chld);
54         return wait_val;
55 }
56 weak_alias(__libc_system, system)