Debian Codegraph @

src/system.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010-2015 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <config.h>
22
23 #ifdef __GNUC__
24
25 #define _BSD_SOURCE
26 #define _POSIX_SOURCE
27 #define _DEFAULT_SOURCE
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31 #include <signal.h>
32 #include "libfakechroot.h"
33
34
35 /* #include <sys/types.h> */
36 /* #include <stdlib.h> */
37 /* #include <unistd.h> */
38 /* #include <sys/wait.h> */
39 wrapper(system, int, (const char * command))
40 {
41 pid_t pid;
42 int pstat;
43 sigset_t mask, omask;
44 struct sigaction new_action_ign, old_action_int, old_action_quit;
45
46 debug("system(\"%s\")", command);
47 if (command == 0)
48 return 1;
49
50 sigemptyset(&mask);
51 sigaddset(&mask, SIGCHLD);
52 sigprocmask(SIG_BLOCK, &mask, &omask);
53
54 if ((pid = vfork()) < 0) {
55 sigprocmask(SIG_SETMASK, &omask, NULL);
56 return -1;
57 }
58 if (pid == 0) {
59 sigprocmask(SIG_SETMASK, &omask, NULL);
60
61 execl("/bin/sh", "sh", "-c", command, (char *) 0);
62 _exit(127);
63 }
64
65 new_action_ign.sa_handler = SIG_IGN;
66 sigemptyset(&new_action_ign.sa_mask);
67 new_action_ign.sa_flags = 0;
68
69 sigaction(SIGINT, &new_action_ign, &old_action_int);
70 sigaction(SIGQUIT, &new_action_ign, &old_action_quit);
71
72 pid = waitpid(pid, (int *)&pstat, 0);
73
74 sigprocmask(SIG_SETMASK, &omask, NULL);
75
76 sigaction(SIGINT, &old_action_int, NULL);
77 sigaction(SIGQUIT, &old_action_quit, NULL);
78
79 return (pid == -1 ? -1 : pstat);
80 }
81
82 #else
83 typedef int empty_translation_unit;
84 #endif