Implement "env-file" service setting for service-specific environment.
[oweals/dinit.git] / src / tests / test-includes / dinit.h
1 #ifndef DINIT_H_INCLUDED
2 #define DINIT_H_INCLUDED 1
3
4 // dummy dinit.h
5
6 #include <unordered_set>
7 #include <map>
8 #include <string>
9 #include <cassert>
10
11 #include "dasynq.h"
12
13 using clock_type = dasynq::clock_type;
14 using rearm = dasynq::rearm;
15 using time_val = dasynq::time_val;
16
17 namespace bp_sys {
18     extern pid_t last_forked_pid;
19 }
20
21 class eventloop_t
22 {
23     time_val current_time {0, 0};
24
25     public:
26     void get_time(time_val &tv, dasynq::clock_type clock) noexcept
27     {
28         tv = current_time;
29     }
30
31     // Advance the simulated current time by the given amount, and call timer callbacks.
32     void advance_time(time_val amount)
33     {
34         current_time += amount;
35         auto active_copy = active_timers;
36         for (timer * t : active_copy) {
37             if (t->expiry_time >= current_time) {
38                 t->stop_timer(*this);
39                 rearm r = t->expired(*this, 1);
40                 assert(r == rearm::NOOP); // others not handled
41             }
42         }
43     }
44
45     class child_proc_watcher
46     {
47         public:
48         pid_t fork(eventloop_t &loop, bool reserved_child_watcher, int priority = dasynq::DEFAULT_PRIORITY)
49         {
50             bp_sys::last_forked_pid++;
51             return bp_sys::last_forked_pid;
52         }
53
54         void add_reserved(eventloop_t &eloop, pid_t child, int prio = dasynq::DEFAULT_PRIORITY) noexcept
55         {
56
57         }
58
59         void stop_watch(eventloop_t &eloop) noexcept
60         {
61
62         }
63
64         void deregister(eventloop_t &loop, pid_t pid) noexcept
65         {
66
67         }
68
69         void unreserve(eventloop_t &loop) noexcept
70         {
71
72         }
73     };
74
75     template <typename Derived> class child_proc_watcher_impl : public child_proc_watcher
76     {
77
78     };
79
80     class fd_watcher
81     {
82         int watched_fd;
83
84         public:
85         void add_watch(eventloop_t &loop, int fd, int events, bool enable = true)
86         {
87             if (loop.regd_fd_watchers.find(fd) != loop.regd_fd_watchers.end()
88                     || loop.regd_bidi_watchers.find(fd) != loop.regd_bidi_watchers.end()) {
89                 throw std::string("must not add_watch when already active");
90             }
91             watched_fd = fd;
92             loop.regd_fd_watchers[fd] = this;
93         }
94
95         int get_watched_fd() noexcept
96         {
97             return watched_fd;
98         }
99
100         void set_enabled(eventloop_t &loop, bool enable) noexcept
101         {
102
103         }
104
105         void deregister(eventloop_t &loop) noexcept
106         {
107             loop.regd_fd_watchers.erase(watched_fd);
108             watched_fd = -1;
109         }
110
111         virtual rearm fd_event(eventloop_t & loop, int fd, int flags) = 0;
112     };
113
114     template <typename Derived> class fd_watcher_impl : public fd_watcher
115     {
116
117     };
118
119     class bidi_fd_watcher
120     {
121         int watched_fd = -1;
122
123         public:
124         void set_watches(eventloop_t &eloop, int newFlags) noexcept
125         {
126
127         }
128
129         void add_watch(eventloop_t &eloop, int fd, int flags, int inprio = dasynq::DEFAULT_PRIORITY,
130                         int outprio = dasynq::DEFAULT_PRIORITY)
131         {
132                 if (eloop.regd_bidi_watchers.find(fd) != eloop.regd_bidi_watchers.end()) {
133                         throw std::string("must not add_watch when already active");
134                 }
135                 eloop.regd_bidi_watchers[fd] = this;
136                 watched_fd = fd;
137         }
138
139         int get_watched_fd() noexcept
140         {
141                 return watched_fd;
142         }
143
144         void deregister(eventloop_t &eloop) noexcept
145         {
146                 eloop.regd_bidi_watchers.erase(watched_fd);
147                 watched_fd = -1;
148         }
149
150         // In the real implementation these are not virtual, but it is easier for testing if they are:
151         virtual rearm read_ready(eventloop_t &loop, int fd) noexcept = 0;
152         virtual rearm write_ready(eventloop_t &loop, int fd) noexcept = 0;
153     };
154
155     template <typename Derived> class bidi_fd_watcher_impl : public bidi_fd_watcher
156         {
157
158         };
159
160     class timer
161     {
162         friend class eventloop_t;
163
164         private:
165         time_val expiry_time;
166
167         protected:
168         virtual rearm expired(eventloop_t &loop, int expiry_count)
169         {
170             return rearm::NOOP;
171         }
172
173         public:
174         void add_timer(eventloop_t &loop)
175         {
176
177         }
178
179         void arm_timer_rel(eventloop_t &loop, time_val timeout) noexcept
180         {
181             expiry_time = loop.current_time + timeout;
182             loop.active_timers.insert(this);
183         }
184
185         void stop_timer(eventloop_t &loop) noexcept
186         {
187             loop.active_timers.erase(this);
188         }
189
190         void deregister(eventloop_t &loop) noexcept
191         {
192
193         }
194     };
195
196     template <typename Derived> class timer_impl : public timer
197     {
198         protected:
199         virtual rearm expired(eventloop_t &loop, int expiry_count) override
200         {
201             return static_cast<Derived *>(this)->timer_expiry(loop, expiry_count);
202         }
203     };
204
205     std::unordered_set<timer *> active_timers;
206         std::map<int, bidi_fd_watcher *> regd_bidi_watchers;
207         std::map<int, fd_watcher *> regd_fd_watchers;
208 };
209
210 inline void rootfs_is_rw() noexcept
211 {
212 }
213
214 inline void setup_external_log() noexcept
215 {
216 }
217
218 inline void read_env_file(const char *env_file_path)
219 {
220 }
221
222 extern eventloop_t event_loop;
223
224 #endif