Skip to content

Commit 18044e2

Browse files
committed
added run_shell; temporarily suspend signals during fork
1 parent f585a44 commit 18044e2

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/util/run.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,38 @@ Date: August 2012
2323
#include <sys/wait.h>
2424
#include <sys/types.h>
2525
#include <fcntl.h>
26+
#include <signal.h>
2627

2728
#endif
2829

2930
#include <util/unicode.h>
31+
#include <util/signal_catcher.h>
3032

3133
#include "run.h"
3234

3335
/*******************************************************************\
3436

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+
3558
Function: run
3659

3760
Inputs:
@@ -89,15 +112,22 @@ int run(
89112
}
90113
}
91114

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);
93119

94120
/* now create new process */
95-
childpid = fork();
121+
pid_t childpid = fork();
96122

97123
if(childpid>=0) /* fork succeeded */
98124
{
99125
if(childpid==0) /* fork() returns 0 to the child process */
100126
{
127+
// resume signals
128+
remove_signal_catcher();
129+
sigprocmask(SIG_SETMASK, &old_mask, NULL);
130+
101131
char **_argv=new char * [argv.size()+1];
102132
for(std::size_t i=0; i<argv.size(); i++)
103133
_argv[i]=strdup(argv[i].c_str());
@@ -106,12 +136,17 @@ int run(
106136

107137
if(stdin_fd!=STDIN_FILENO)
108138
dup2(stdin_fd, STDIN_FILENO);
139+
109140
execvp(what.c_str(), _argv);
141+
110142
/* usually no return */
111143
return 1;
112144
}
113145
else /* fork() returns new pid to the parent process */
114146
{
147+
// resume signals
148+
sigprocmask(SIG_SETMASK, &old_mask, NULL);
149+
115150
int status; /* parent process: child's exit status */
116151

117152
while(waitpid(childpid, &status, 0)==-1) /* wait for child to exit, and store its status */
@@ -133,8 +168,11 @@ int run(
133168
}
134169
else /* fork returns -1 on failure */
135170
{
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);
138176

139177
return 1;
140178
}

src/util/run.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ int run(
1919
const std::vector<std::string> &argv,
2020
const std::string &std_input);
2121

22+
int run_shell(const std::string &command);
23+
2224
#endif // CPROVER_UTIL_RUN_H

0 commit comments

Comments
 (0)