File: process.h

package info (click to toggle)
aoflagger 3.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,960 kB
  • sloc: cpp: 83,076; python: 10,187; sh: 260; makefile: 178
file content (43 lines) | stat: -rw-r--r-- 982 bytes parent folder | download | duplicates (2)
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
#include <sys/types.h>
#include <sys/wait.h>

#include <unistd.h>

#include <functional>

class Process {
 public:
  explicit Process(const std::string& cmdLine) {
    _pid = vfork();
    switch (_pid) {
      case -1:  // Error
        throw std::runtime_error(
            "Could not vfork() new process for executing remote client");
      case 0:  // Child
        execl("/bin/sh", "sh", "-c", cmdLine.c_str(), NULL);
        _exit(127);
    }
  }

  void Join() {
    // Wait for process to terminate
    int pStatus;
    do {
      int pidReturn;
      do {
        pidReturn = waitpid(_pid, &pStatus, 0);
      } while (pidReturn == -1 && errno == EINTR);
    } while (!WIFEXITED(pStatus) && !WIFSIGNALED(pStatus));
    if (WIFEXITED(pStatus)) {
      const int exitStatus = WEXITSTATUS(pStatus);
      onFinished(exitStatus != 0, exitStatus);
    } else {
      onFinished(true, 0);
    }
  }

 private:
  std::function<void(bool, int)> onFinished;  // TODO

  int _pid;
};