Fix mistakes in cptest asserts
[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
5  * mocks/stubs 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" // for pipe2
12
13 #include <sys/uio.h> // writev
14 #include <unistd.h>
15 #include <fcntl.h>
16
17 namespace bp_sys {
18
19 using dasynq::pipe2;
20
21 using ::fcntl;
22 using ::open;
23 using ::close;
24 using ::kill;
25 using ::getpgid;
26 using ::tcsetpgrp;
27 using ::getpgrp;
28 using ::read;
29 using ::write;
30 using ::writev;
31
32 // Wrapper around a POSIX exit status
33 class exit_status
34 {
35     friend pid_t waitpid(pid_t, exit_status *, int);
36
37     int status;
38
39     public:
40     exit_status() noexcept : status(0) { }
41     explicit exit_status(int status_p) noexcept : status(status_p) { }
42
43     bool did_exit() noexcept
44     {
45         return WIFEXITED(status);
46     }
47
48     bool did_exit_clean() noexcept
49     {
50         return status == 0;
51     }
52
53     bool was_signalled() noexcept
54     {
55         return WIFSIGNALED(status);
56     }
57
58     int get_exit_status() noexcept
59     {
60         return WEXITSTATUS(status);
61     }
62
63     int get_term_sig() noexcept
64     {
65         return WTERMSIG(status);
66     }
67
68     int as_int() noexcept
69     {
70         return status;
71     }
72 };
73
74 inline pid_t waitpid(pid_t p, exit_status *statusp, int flags)
75 {
76     return ::waitpid(p, &statusp->status, flags);
77 }
78
79 }
80
81 #endif  // BPSYS_INCLUDED