John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/util/run_command.h" |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 2 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 3 | #include <fcntl.h> |
| 4 | #include <sys/stat.h> |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/wait.h> |
| 7 | #include <unistd.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 8 | |
| 9 | #include <csignal> |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 10 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 11 | #include "aos/logging/logging.h" |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace util { |
| 15 | namespace { |
| 16 | |
| 17 | // RAII class to block SIGCHLD and then restore it on destruction. |
| 18 | class BlockSIGCHLD { |
| 19 | public: |
| 20 | BlockSIGCHLD() { |
| 21 | sigset_t to_block; |
| 22 | sigemptyset(&to_block); |
| 23 | sigaddset(&to_block, SIGCHLD); |
| 24 | if (sigprocmask(SIG_BLOCK, &to_block, &original_blocked_) == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 25 | AOS_PLOG(FATAL, "sigprocmask(SIG_BLOCK, %p, %p) failed", &to_block, |
| 26 | &original_blocked_); |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | ~BlockSIGCHLD() { |
| 30 | if (sigprocmask(SIG_SETMASK, &original_blocked_, nullptr) == -1) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 31 | AOS_PLOG(FATAL, "sigprocmask(SIG_SETMASK, %p, nullptr) failed", |
| 32 | &original_blocked_); |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
| 36 | private: |
| 37 | sigset_t original_blocked_; |
| 38 | }; |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | int RunCommand(const char *command) { |
| 43 | BlockSIGCHLD blocker; |
| 44 | const pid_t pid = fork(); |
| 45 | switch (pid) { |
| 46 | case 0: // in child |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 47 | { |
| 48 | int new_stdin = open("/dev/null", O_RDONLY); |
| 49 | if (new_stdin == -1) _exit(127); |
| 50 | int new_stdout = open("/dev/null", O_WRONLY); |
| 51 | if (new_stdout == -1) _exit(127); |
| 52 | int new_stderr = open("/dev/null", O_WRONLY); |
| 53 | if (new_stderr == -1) _exit(127); |
| 54 | if (dup2(new_stdin, 0) != 0) _exit(127); |
| 55 | if (dup2(new_stdout, 1) != 1) _exit(127); |
| 56 | if (dup2(new_stderr, 2) != 2) _exit(127); |
| 57 | execl("/bin/sh", "sh", "-c", command, nullptr); |
| 58 | _exit(127); |
| 59 | } |
Brian Silverman | af78486 | 2014-05-13 08:14:55 -0700 | [diff] [blame] | 60 | case -1: |
| 61 | return -1; |
| 62 | default: |
| 63 | int stat; |
| 64 | while (waitpid(pid, &stat, 0) == -1) { |
| 65 | if (errno != EINTR) { |
| 66 | return -1; |
| 67 | } |
| 68 | } |
| 69 | return stat; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | } // namespace util |
| 74 | } // namespace aos |