OSDN Git Service

- trim any trailing whitespace
[uclinux-h8/uClibc.git] / libc / stdlib / system.c
1 /*
2  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6
7 #include <stdio.h>
8 #include <stddef.h>
9 #include <signal.h>
10 #include <unistd.h>
11 #include <sys/wait.h>
12 #include <stdlib.h>
13
14 libc_hidden_proto(_exit)
15 libc_hidden_proto(wait4)
16 libc_hidden_proto(execl)
17 libc_hidden_proto(signal)
18 libc_hidden_proto(vfork)
19
20 /* uClinux-2.0 has vfork, but Linux 2.0 doesn't */
21 #include <sys/syscall.h>
22 #ifndef __NR_vfork
23 # define vfork fork
24 libc_hidden_proto(fork)
25 #endif
26
27 extern __typeof(system) __libc_system;
28 int __libc_system(const char *command)
29 {
30         int wait_val, pid;
31         __sighandler_t save_quit, save_int, save_chld;
32
33         if (command == 0)
34                 return 1;
35
36         save_quit = signal(SIGQUIT, SIG_IGN);
37         save_int = signal(SIGINT, SIG_IGN);
38         save_chld = signal(SIGCHLD, SIG_DFL);
39
40         if ((pid = vfork()) < 0) {
41                 signal(SIGQUIT, save_quit);
42                 signal(SIGINT, save_int);
43                 signal(SIGCHLD, save_chld);
44                 return -1;
45         }
46         if (pid == 0) {
47                 signal(SIGQUIT, SIG_DFL);
48                 signal(SIGINT, SIG_DFL);
49                 signal(SIGCHLD, SIG_DFL);
50
51                 execl("/bin/sh", "sh", "-c", command, (char *) 0);
52                 _exit(127);
53         }
54         /* Signals are not absolutly guarenteed with vfork */
55         signal(SIGQUIT, SIG_IGN);
56         signal(SIGINT, SIG_IGN);
57
58 #if 0
59         __printf("Waiting for child %d\n", pid);
60 #endif
61
62         if (wait4(pid, &wait_val, 0, 0) == -1)
63                 wait_val = -1;
64
65         signal(SIGQUIT, save_quit);
66         signal(SIGINT, save_int);
67         signal(SIGCHLD, save_chld);
68         return wait_val;
69 }
70 weak_alias(__libc_system,system)