service: add various accessors.
[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() noexcept : status(0) { }
33     explicit exit_status(int status_p) noexcept : status(status_p) { }
34
35     bool did_exit() noexcept
36     {
37         return WIFEXITED(status);
38     }
39
40     bool did_exit_clean() noexcept
41     {
42         return status == 0;
43     }
44
45     bool was_signalled() noexcept
46     {
47         return WIFSIGNALED(status);
48     }
49
50     int get_exit_status() noexcept
51     {
52         return WEXITSTATUS(status);
53     }
54
55     int get_term_sig() noexcept
56     {
57         return WTERMSIG(status);
58     }
59
60     int as_int() noexcept
61     {
62         return status;
63     }
64 };
65
66 inline pid_t waitpid(pid_t p, exit_status *statusp, int flags)
67 {
68     return ::waitpid(p, &statusp->status, flags);
69 }
70
71 }
72
73 #endif  // BPSYS_INCLUDED