-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.c
48 lines (32 loc) · 792 Bytes
/
runner.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "runner.h"
#include "headers.h"
int runCommand(int background, char** args) {
int pid = fork();
if (pid < 0) {
perror("Error");
return -1;
} else if (pid) {
if (background) {
fprintf(stderr, "%s started with PID %d\n", args[0], pid);
return pid;
} else {
int status;
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(0, pid);
waitpid(pid, &status, WUNTRACED);
tcsetpgrp(0, getpgid(0));
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
if (WIFSTOPPED(status)) return pid;
if (WEXITSTATUS(status) == EXIT_FAILURE) return -1;
}
} else {
setpgid(0, 0);
if (execvp(args[0], args) < 0) {
perror("Error");
exit(1);
}
}
return 0;
}