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