|
0
|
1 #include <iostream> |
|
|
2 #include <unistd.h> |
|
|
3 #include <sys/wait.h> |
|
|
4 #include <spawn.h> |
|
|
5 #include <cstring> |
|
|
6 #include <string> |
|
|
7 #include "process.h" |
|
|
8 extern char** environ; // idk what this does |
|
2
|
9 std::string spawn(char* const argv[]) |
|
0
|
10 { |
|
|
11 pid_t pid; |
|
|
12 int status; |
|
2
|
13 status = posix_spawnp(&pid, argv[0], NULL, NULL, const_cast<char* const*>(argv), environ); |
|
0
|
14 if (status != 0) { |
|
2
|
15 std::cerr << "posix_spawnp failed for " << argv[0] << ": " << strerror(status) << std::endl; |
|
0
|
16 return "error"; |
|
|
17 } |
|
|
18 if (waitpid(pid, &status, 0) == -1) { |
|
|
19 perror("waitpid"); |
|
|
20 return "error"; |
|
|
21 } |
|
|
22 if (WIFEXITED(status)) { |
|
|
23 return std::to_string(WEXITSTATUS(status)); |
|
|
24 } |
|
|
25 if (WIFSIGNALED(status)) { |
|
2
|
26 return "sig:" + std::to_string(WTERMSIG(status)); |
|
0
|
27 } |
|
|
28 return "success"; |
|
|
29 } |