Add initial control protocol test.
[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 using ::read;
24 using ::write;
25
26 // Wrapper around a POSIX exit status
27 class exit_status
28 {
29     friend pid_t waitpid(pid_t, exit_status *, int);
30
31     int status;
32
33     public:
34     exit_status() noexcept : status(0) { }
35     explicit exit_status(int status_p) noexcept : status(status_p) { }
36
37     bool did_exit() noexcept
38     {
39         return WIFEXITED(status);
40     }
41
42     bool did_exit_clean() noexcept
43     {
44         return status == 0;
45     }
46
47     bool was_signalled() noexcept
48     {
49         return WIFSIGNALED(status);
50     }
51
52     int get_exit_status() noexcept
53     {
54         return WEXITSTATUS(status);
55     }
56
57     int get_term_sig() noexcept
58     {
59         return WTERMSIG(status);
60     }
61
62     int as_int() noexcept
63     {
64         return status;
65     }
66 };
67
68 inline pid_t waitpid(pid_t p, exit_status *statusp, int flags)
69 {
70     return ::waitpid(p, &statusp->status, flags);
71 }
72
73 }
74
75 #endif  // BPSYS_INCLUDED