6bd0de4f906d70ad0a4c782c808078285b6fd7a1
[oweals/dinit.git] / src / includes / baseproc-sys.h
1 /*
2  * This header implements a namespace, bp_sys, which wraps various system calls used by baseproc-service.cc.
3  *
4  * When running tests, another header is substituted in place of this one. The substitute provides mocks/stubs
5  * for the functions, to avoid calling the real functions and thus allow for unit-level testing.
6  */
7
8 #ifndef BPSYS_INCLUDED
9 #define BPSYS_INCLUDED
10
11 #include "dasynq.h"
12
13 namespace bp_sys {
14
15 using dasynq::pipe2;
16
17 using ::fcntl;
18 using ::close;
19 using ::kill;
20 using ::getpgid;
21 using ::tcsetpgrp;
22 using ::getpgrp;
23
24 // Wrapper around a POSIX exit status
25 class exit_status
26 {
27     friend pid_t waitpid(pid_t, exit_status *, int);
28
29     int status;
30
31     public:
32     exit_status() : status(0) { }
33     explicit exit_status(int status_p) : status(status_p) { }
34
35     bool did_exit()
36     {
37         return WIFEXITED(status);
38     }
39
40     bool did_exit_clean()
41     {
42         return status == 0;
43     }
44
45     bool was_signalled()
46     {
47         return WIFSIGNALED(status);
48     }
49
50     int get_exit_status()
51     {
52         return WEXITSTATUS(status);
53     }
54
55     int get_term_sig()
56     {
57         return WTERMSIG(status);
58     }
59 };
60
61 inline pid_t waitpid(pid_t p, exit_status *statusp, int flags)
62 {
63     return ::waitpid(p, &statusp->status, flags);
64 }
65
66 }
67
68 #endif  // BPSYS_INCLUDED