Fix signal unsafety in testdriver.c.
This commit is contained in:
parent
90547157b4
commit
b7de58e34e
|
@ -10,9 +10,9 @@
|
||||||
|
|
||||||
static bool timed_out = false;
|
static bool timed_out = false;
|
||||||
static bool child_exited = false;
|
static bool child_exited = false;
|
||||||
static char** command = NULL;
|
static char* const* command = NULL;
|
||||||
static int timeout = 0;
|
static int timeout = 0;
|
||||||
static jmp_buf exit_jmp;
|
static int pid = 0;
|
||||||
|
|
||||||
static void parse_arguments(int argc, char* const argv[])
|
static void parse_arguments(int argc, char* const argv[])
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,8 @@ static void parse_arguments(int argc, char* const argv[])
|
||||||
static void sigalrm_cb(int sigraised)
|
static void sigalrm_cb(int sigraised)
|
||||||
{
|
{
|
||||||
timed_out = true;
|
timed_out = true;
|
||||||
longjmp(exit_jmp, 1);
|
if (pid)
|
||||||
|
kill(pid, SIGKILL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* const argv[])
|
int main(int argc, char* const argv[])
|
||||||
|
@ -53,20 +54,12 @@ int main(int argc, char* const argv[])
|
||||||
const int WRITE = 1;
|
const int WRITE = 1;
|
||||||
|
|
||||||
int fds[2];
|
int fds[2];
|
||||||
int pid;
|
|
||||||
FILE* childin;
|
FILE* childin;
|
||||||
int wstatus;
|
int wstatus;
|
||||||
char buffer[4096];
|
char buffer[4096];
|
||||||
|
|
||||||
parse_arguments(argc, argv);
|
parse_arguments(argc, argv);
|
||||||
|
|
||||||
if (setjmp(exit_jmp) == 0)
|
|
||||||
{
|
|
||||||
/* First time through. */
|
|
||||||
|
|
||||||
signal(SIGALRM, sigalrm_cb);
|
|
||||||
alarm(timeout);
|
|
||||||
|
|
||||||
pipe(fds);
|
pipe(fds);
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
|
@ -79,8 +72,18 @@ int main(int argc, char* const argv[])
|
||||||
execvp(command[0], command);
|
execvp(command[0], command);
|
||||||
_exit(1);
|
_exit(1);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Parent */
|
||||||
|
close(fds[WRITE]);
|
||||||
|
signal(SIGALRM, sigalrm_cb);
|
||||||
|
alarm(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
childin = fdopen(fds[READ], "r");
|
childin = fdopen(fds[READ], "r");
|
||||||
|
if (!childin)
|
||||||
|
fatal("cannot open pipe");
|
||||||
|
|
||||||
while (!timed_out)
|
while (!timed_out)
|
||||||
{
|
{
|
||||||
if (!fgets(buffer, sizeof(buffer), childin))
|
if (!fgets(buffer, sizeof(buffer), childin))
|
||||||
|
@ -92,9 +95,10 @@ int main(int argc, char* const argv[])
|
||||||
if (strcmp(buffer, "@@FINISHED\r\n") == 0)
|
if (strcmp(buffer, "@@FINISHED\r\n") == 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* Reached via longjmp, EOF, or seeing a @@FINISHED. */
|
/* Reached via EOF or seeing a @@FINISHED. */
|
||||||
|
|
||||||
|
alarm(0);
|
||||||
|
|
||||||
kill(pid, SIGKILL);
|
kill(pid, SIGKILL);
|
||||||
waitpid(pid, &wstatus, 0);
|
waitpid(pid, &wstatus, 0);
|
||||||
|
|
Loading…
Reference in a new issue