@@ -23,15 +23,38 @@ Date: August 2012
23
23
#include <sys/wait.h>
24
24
#include <sys/types.h>
25
25
#include <fcntl.h>
26
+ #include <signal.h>
26
27
27
28
#endif
28
29
29
30
#include <util/unicode.h>
31
+ #include <util/signal_catcher.h>
30
32
31
33
#include "run.h"
32
34
33
35
/*******************************************************************\
34
36
37
+ Function: run_shell
38
+
39
+ Inputs:
40
+
41
+ Outputs:
42
+
43
+ Purpose:
44
+
45
+ \*******************************************************************/
46
+
47
+ int run_shell(const std::string &command)
48
+ {
49
+ std::string shell="/bin/sh";
50
+ std::vector<std::string> argv;
51
+ argv.push_back(shell);
52
+ argv.push_back(command);
53
+ return run(shell, argv, "");
54
+ }
55
+
56
+ /*******************************************************************\
57
+
35
58
Function: run
36
59
37
60
Inputs:
@@ -89,15 +112,22 @@ int run(
89
112
}
90
113
}
91
114
92
- pid_t childpid; /* variable to store the child's pid */
115
+ // temporarily suspend all signals
116
+ sigset_t new_mask, old_mask;
117
+ sigemptyset(&new_mask);
118
+ sigprocmask(SIG_SETMASK, &new_mask, &old_mask);
93
119
94
120
/* now create new process */
95
- childpid = fork();
121
+ pid_t childpid = fork();
96
122
97
123
if(childpid>=0) /* fork succeeded */
98
124
{
99
125
if(childpid==0) /* fork() returns 0 to the child process */
100
126
{
127
+ // resume signals
128
+ remove_signal_catcher();
129
+ sigprocmask(SIG_SETMASK, &old_mask, NULL);
130
+
101
131
char **_argv=new char * [argv.size()+1];
102
132
for(std::size_t i=0; i<argv.size(); i++)
103
133
_argv[i]=strdup(argv[i].c_str());
@@ -106,12 +136,17 @@ int run(
106
136
107
137
if(stdin_fd!=STDIN_FILENO)
108
138
dup2(stdin_fd, STDIN_FILENO);
139
+
109
140
execvp(what.c_str(), _argv);
141
+
110
142
/* usually no return */
111
143
return 1;
112
144
}
113
145
else /* fork() returns new pid to the parent process */
114
146
{
147
+ // resume signals
148
+ sigprocmask(SIG_SETMASK, &old_mask, NULL);
149
+
115
150
int status; /* parent process: child's exit status */
116
151
117
152
while(waitpid(childpid, &status, 0)==-1) /* wait for child to exit, and store its status */
@@ -133,8 +168,11 @@ int run(
133
168
}
134
169
else /* fork returns -1 on failure */
135
170
{
136
- if(stdin_fd!=STDIN_FILENO)
137
- close(stdin_fd);
171
+ // resume signals
172
+ sigprocmask(SIG_SETMASK, &old_mask, NULL);
173
+
174
+ if(stdin_fd!=STDIN_FILENO)
175
+ close(stdin_fd);
138
176
139
177
return 1;
140
178
}
0 commit comments