forked from svarshavchik/vera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprivrequest.H
290 lines (188 loc) · 7.53 KB
/
privrequest.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
** Copyright 2022 Double Precision, Inc.
** See COPYING for distribution information.
*/
#ifndef privrequest_h
#define privrequest_h
#include "external_filedesc.H"
#include "proc_loaderfwd.H"
#include <sys/stat.h>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <optional>
#include <variant>
#include <time.h>
#include <signal.h>
std::string environconfig();
// Send requests and process responses over a privileged (root) sockets.
// Send a start request
void send_start(const external_filedesc &efd, std::string name);
// Initial start attempt, did it work?
// Returns an empty string if it did, or an error message.
std::string get_start_status(const external_filedesc &efd);
// Wait for the start request to finish.
bool get_start_result(const external_filedesc &efd);
#define START_RESULT_OK "0"
// Send a stop request
void send_stop(const external_filedesc &efd, std::string name);
// Initial stop attempt, did it work?
// Returns an empty string if it did, or an error message.
std::string get_stop_status(const external_filedesc &efd);
// Wait for the stop request to finish.
void wait_stop(const external_filedesc &efd);
// Send a restart request
void send_restart(const external_filedesc &efd, std::string name);
// Initial restart attempt, did it work?
// Returns an empty string if it did, or an error message.
std::string get_restart_status(const external_filedesc &efd);
// Wait for the restart request to finish.
int wait_restart(const external_filedesc &efd);
// Send a reload request
void send_reload(const external_filedesc &efd, std::string name);
// Initial reload attempt, did it work?
// Returns an empty string if it did, or an error message.
std::string get_reload_status(const external_filedesc &efd);
// Wait for the reload request to finish.
int wait_reload(const external_filedesc &efd);
// Request vera to terminate and replace itself with another process.
// This is used to execute rc.0 or rc.6, which ends in a reboot.
void send_sysdown(const external_filedesc &efd,
std::string runlevel,
std::string command);
// Acknowledge that system down is in progress.
std::string get_sysdown_status(const external_filedesc &efd);
// Request a re-exec
void request_reexec(const external_filedesc &efd);
// Request a new runlevel
void request_runlevel(const external_filedesc &efd,
const std::string &runlevel);
// Wait for the runlevel request to be validated.
// Returns an empty string if the request is processing, or an error message.
std::string get_runlevel_status(const external_filedesc &efd);
// Wait for the new runlevel to come up.
int wait_runlevel(const external_filedesc &efd);
// Request the current runlevel and aliases
void request_current_runlevel(const external_filedesc &efd);
// Returns the current runlevel and its aliases
//
// First string is the full runlevel name, followed by aliases (3, 4, etc...)
std::vector<std::string> get_current_runlevel(const external_filedesc &efd);
// Create a pair of sockets for the fake requests.
// This is used mostly for unit tests, but we also use this to queue up
// a synthesized request to switch to the default run level, at startup.
std::tuple<external_filedesc, external_filedesc> create_fake_request();
// Send a status request to the daemon
void request_status(const external_filedesc &efd);
// Helper function used to receive the file descriptor for a plain file.
// Parameter is the connection to the requester.
//
// Receives and returns the file descriptor.
external_filedesc receive_fd(const external_filedesc &efd);
// Helper function used to receive the file descriptor for a plain file.
// Parameter is the connection to the requester.
//
// Receives and returns the file descriptor, verifying that it is IS_REG.
external_filedesc request_regfd(const external_filedesc &efd);
// Handshake: daemon: request a file descriptor
void request_fd(const external_filedesc &efd);
// Handshake: requester: wait for the daemon to request a file descriptor
void request_fd_wait(const external_filedesc &efd);
// Send a file descriptor to the requesting daemon
void request_send_fd(const external_filedesc &efd, int statusfd);
external_filedesc create_stdoutcc(const external_filedesc &efd);
struct container_state_info {
std::string state;
proc_override::resources_t resources;
std::string elapsed;
time_t timestamp=0;
bool enabled=false;
// Bare-bones information about a process in the container.
struct pid_info {
// Its parent process id
pid_t ppid{1};
// exe's device/inode
dev_t exedev{};
ino_t exeino{};
// Its command line
std::vector<std::string> cmdline;
bool operator==(const pid_info &) const=default;
bool operator<=>(const pid_info &) const=default;
};
struct hier_pid_info;
// Processes arranged in a parent-child hierarchy
typedef std::map<pid_t, hier_pid_info> hier_pids;
struct hier_pid_info {
// This is actually this process's pid. Thsi is the parent
// of all the child_pids
pid_info parent_pid;
// parent_pid's child processes.
hier_pids child_pids;
bool operator==(const hier_pid_info &) const=default;
bool operator<=>(const hier_pid_info &) const=default;
};
// Processes in this container.
hier_pids processes;
// The dependencies
std::unordered_set<std::string> dep_requires, dep_requires_first,
dep_required_by, dep_starting_first, dep_stopping_first;
bool operator==(const container_state_info &) const=default;
};
/*! Receive and parse the container state.
This is the end result of the following sequence: request_status, followed
by request_fd_wait, followed by request_send_fd(). get_status() gets
called with the daemon connection and the temporary file descriptor that
was passed to request_sendfd().
Returns a map, the key is the container name.
container_state_info does not reflect overrides.
*/
std::unordered_map<std::string, container_state_info> get_status(
const external_filedesc &efd,
int statusfd
);
/*! "/proc/"
Used to read process information. Overridden in unit tests.
*/
extern const char slashprocslash[];
/*! Populate container_state_info::pid_info
This reads the pids in the container, then reads /proc and populates
a pid_info map.
*/
void get_pid_status(const std::string &container_name,
std::unordered_map<pid_t,
container_state_info::pid_info> &processes);
/*! Sort container pids into a hierarchical tree
This removes each pid from the map populated by get_pid_status and puts them
into a hierarchical parent/children tree.
*/
void sort_pids(std::unordered_map<pid_t,
container_state_info::pid_info> &processes,
container_state_info::hier_pids &pids);
// Update the container_state_info map to reflect the current overrides
void update_status_overrides(
std::unordered_map<std::string, container_state_info> &status,
const std::string &globaldir,
const std::string &localdir,
const std::string &overridedir);
// Send environment variable set
void send_setenv(const external_filedesc &fd,
std::string name,
std::string value);
// Send environment variable unset
void send_unsetenv(const external_filedesc &fd,
std::string name);
// Wait for the setenv or unsetenv to be processed
//
// Returns: 0 - success, 1 - error.
int wait_setunsetenv(const external_filedesc &fd);
// Request a freeze
void request_freeze(const external_filedesc &efd,
const std::string &name);
// Request a thaw
void request_thaw(const external_filedesc &efd,
const std::string &name);
// Status of the thaw command
std::string get_freeze_thaw_status(const external_filedesc &efd);
#endif