|
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 |
|
|
9 std::string spawn(char* const argv[], const char* path) |
|
|
10 { |
|
|
11 pid_t pid; |
|
|
12 int status; |
|
|
13 status = posix_spawn(&pid, path, NULL, NULL, const_cast<char* const*>(argv), environ); |
|
|
14 if (status != 0) { |
|
|
15 std::cerr << "posix_spawn failed:" << strerror(status) << std::endl; |
|
|
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)) { |
|
|
26 return std::to_string(WTERMSIG(status)); |
|
|
27 } |
|
|
28 return "success"; |
|
|
29 } |