-
Notifications
You must be signed in to change notification settings - Fork 1
/
privrequest.C
666 lines (524 loc) · 12.3 KB
/
privrequest.C
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/*
** Copyright 2022 Double Precision, Inc.
** See COPYING for distribution information.
*/
#include "config.h"
#include "privrequest.H"
#include "proc_loader.H"
#include "proc_container_group.H"
#include "messages.H"
#include <sys/socket.h>
#include <sys/stat.h>
#include <sstream>
#include <fstream>
#include <fcntl.h>
#include <stdio.h>
#include <locale>
#include <algorithm>
#include <string_view>
#include <stdio.h>
#include <string.h>
void send_start(const external_filedesc &efd, std::string name)
{
efd->write_all(std::string{"start\n"} + name + "\n");
}
std::string get_start_status(const external_filedesc &efd)
{
return efd->readln();
}
bool get_start_result(const external_filedesc &efd)
{
return efd->readln() == START_RESULT_OK;
}
void send_stop(const external_filedesc &efd, std::string name)
{
efd->write_all(std::string{"stop\n"} + name + "\n");
}
std::string get_stop_status(const external_filedesc &efd)
{
return efd->readln();
}
void wait_stop(const external_filedesc &efd)
{
efd->readln();
}
void send_restart(const external_filedesc &efd, std::string name)
{
efd->write_all(std::string{"restart\n"} + name + "\n");
}
std::string get_restart_status(const external_filedesc &efd)
{
return efd->readln();
}
int wait_restart(const external_filedesc &efd)
{
auto s=efd->readln();
if (s.empty())
return -1;
std::istringstream i{s};
i.imbue(std::locale{"C"});
int n;
if (!(i >> n))
return -1;
return n;
}
void send_reload(const external_filedesc &efd, std::string name)
{
efd->write_all(std::string{"reload\n"} + name + "\n");
}
std::string get_reload_status(const external_filedesc &efd)
{
return efd->readln();
}
void send_sysdown(const external_filedesc &efd,
std::string runlevel,
std::string command)
{
efd->write_all(std::string{"sysdown\n"} + runlevel + "\n" +
command + "\n");
}
std::string get_sysdown_status(const external_filedesc &efd)
{
return efd->readln();
}
int wait_runlevel(const external_filedesc &efd)
{
efd->readln();
return 0;
}
int wait_reload(const external_filedesc &efd)
{
auto s=efd->readln();
if (s.empty())
return -1;
std::istringstream i{s};
i.imbue(std::locale{"C"});
int n;
if (!(i >> n))
return -1;
return n;
}
void request_reexec(const external_filedesc &efd)
{
efd->write_all("reexec\n");
}
void request_runlevel(const external_filedesc &efd,
const std::string &runlevel)
{
efd->write_all("setrunlevel\n" + runlevel + "\n");
}
std::string get_runlevel_status(const external_filedesc &efd)
{
return efd->readln();
}
void request_current_runlevel(const external_filedesc &efd)
{
efd->write_all("getrunlevel\n");
}
std::vector<std::string> get_current_runlevel(const external_filedesc &efd)
{
std::vector<std::string> ret;
while (1)
{
auto s=efd->readln();
if (s.empty())
break;
ret.push_back(s);
}
if (ret.size() > 2)
std::sort(ret.begin()+1, ret.end());
return ret;
}
external_filedesc receive_fd(const external_filedesc &efd)
{
int fd;
struct msghdr msg{};
char buf[CMSG_SPACE(sizeof(fd))];
char dummy;
struct iovec iov;
msg.msg_control=buf;
msg.msg_controllen=sizeof(buf);
msg.msg_iov=&iov;
msg.msg_iovlen=1;
iov.iov_base=&dummy;
iov.iov_len=1;
if (recvmsg(efd->fd, &msg, 0) < 0)
return nullptr;
if (msg.msg_controllen < sizeof(buf))
return nullptr;
auto cmsg=CMSG_FIRSTHDR(&msg);
if (cmsg->cmsg_level != SOL_SOCKET ||
cmsg->cmsg_type != SCM_RIGHTS ||
cmsg->cmsg_len != CMSG_LEN(sizeof(fd)))
{
return nullptr;
}
memcpy(&fd, CMSG_DATA(cmsg), sizeof(fd));
auto ret=std::make_shared<external_filedescObj>(fd);
// Make sure it has FD_CLOEXEC set
fcntl(ret->fd, F_SETFD, FD_CLOEXEC);
return ret;
}
external_filedesc request_regfd(const external_filedesc &efd)
{
auto ret=receive_fd(efd);
// This must be a regular file.
struct stat stat_buf;
if (fstat(ret->fd, &stat_buf) < 0 ||
!S_ISREG(stat_buf.st_mode))
return nullptr;
// Make sure it has FD_CLOEXEC set
fcntl(ret->fd, F_SETFD, FD_CLOEXEC);
return ret;
}
void request_fd(const external_filedesc &efd)
{
efd->write_all("\n");
}
void request_fd_wait(const external_filedesc &efd)
{
efd->readln();
}
void request_send_fd(const external_filedesc &efd, int statusfd)
{
struct iovec iov{};
char dummy{0};
struct msghdr msg{};
char buf[CMSG_SPACE(sizeof(statusfd))]={0};
msg.msg_control=buf;
msg.msg_controllen=sizeof(buf);
auto cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level=SOL_SOCKET;
cmsg->cmsg_type=SCM_RIGHTS;
cmsg->cmsg_len=CMSG_LEN(sizeof(statusfd));
memcpy(CMSG_DATA(cmsg), &statusfd, sizeof(statusfd));
msg.msg_iov=&iov;
msg.msg_iovlen=1;
iov.iov_base=&dummy;
iov.iov_len=1;
sendmsg(efd->fd, &msg, MSG_NOSIGNAL);
}
void request_status(const external_filedesc &efd)
{
efd->write_all("status\n");
}
std::unordered_map<std::string, container_state_info> get_status(
const external_filedesc &efd,
int fd)
{
request_fd_wait(efd);
std::unordered_map<std::string, container_state_info> m;
std::stringstream s;
s.imbue(std::locale{"C"});
if (lseek(fd, 0L, SEEK_SET) == 0)
{
char buffer[1024];
ssize_t n;
while ((n=read(fd, buffer, sizeof(buffer))) > 0)
s << std::string{buffer, buffer+n};
}
s.seekg(0);
std::string name;
while (std::getline(s, name))
{
container_state_info info;
// Linear list of processes in the container
std::unordered_map<pid_t,
container_state_info::pid_info> processes;
std::string line;
while (std::getline(s, line))
{
if (line.empty())
break;
auto b=line.begin(), e=line.end(),
p=std::find(b, e, ':');
std::string_view keyword{
&*b,
static_cast<size_t>(p-b)
};
if (keyword == "status" && p != e)
{
info.state=std::string{++p, e};
}
if (keyword == "elapsed" && p != e)
{
std::istringstream elapsed{std::string{++p, e}};
elapsed.imbue(std::locale{"C"});
time_t s;
if (elapsed >> s)
{
if (elapsed.get() == '/')
{
time_t t;
if (elapsed >> t)
{
info.elapsed=
log_elapsed(s)
+ "/" +
log_elapsed(t);
}
}
else
{
info.elapsed=log_elapsed(s)
+ "/"
+ _("unlimited");
}
}
}
if (keyword == "timestamp" && p != e)
{
std::istringstream i{{++p, e}};
i.imbue(std::locale{"C"});
i >> info.timestamp;
}
if (keyword == "requires" && p != e)
{
info.dep_requires.emplace(++p, e);
}
if (keyword == "requires-first" && p != e)
{
info.dep_requires_first.emplace(++p, e);
}
if (keyword == "required-by" && p != e)
{
info.dep_required_by.emplace(++p, e);
}
if (keyword == "starting-first" && p != e)
{
info.dep_starting_first.emplace(++p, e);
}
if (keyword == "stopping-first" && p != e)
{
info.dep_stopping_first.emplace(++p, e);
}
}
get_pid_status(name, processes);
#if 0
if (name == "name2")
{
processes={
{2, {1, {"pid 2"}}},
{3, {2, {"pid 3"}}},
{4, {2, {"pid 4"}}},
{5, {1, {"pid 5"}}},
{6, {5, {"pid 6"}}},
{7, {5, {"pid 7"}}},
{8, {6, {"pid 8"}}},
};
}
#endif
sort_pids(processes, info.processes);
m.emplace(std::move(name), std::move(info));
}
return m;
}
void get_pid_status(const std::string &container_name,
std::unordered_map<pid_t,
container_state_info::pid_info> &processes)
{
for (auto p:proc_container_group::cgroups_getpids(container_name))
{
auto &pid_info=processes[p];
{
std::ostringstream o;
o.imbue(std::locale{"C"});
o << slashprocslash << p << "/exe";
struct stat stat_buf;
if (stat(o.str().c_str(), &stat_buf) == 0)
{
pid_info.exedev=stat_buf.st_dev;
pid_info.exeino=stat_buf.st_ino;
}
}
{
std::ostringstream o;
o.imbue(std::locale{"C"});
o << slashprocslash << p << "/stat";
std::ifstream i{o.str()};
std::string s;
i >> s; // pid
i >> s; // comm
i >> s; // state
i >> pid_info.ppid;
}
{
std::ostringstream o;
o.imbue(std::locale{"C"});
o << slashprocslash
<< p << "/cmdline";
std::ifstream i{o.str()};
std::string s;
while (std::getline(i, s, '\0'))
pid_info.cmdline
.push_back(s);
}
}
}
void sort_pids(std::unordered_map<pid_t,
container_state_info::pid_info> &processes,
container_state_info::hier_pids &pids)
{
std::unordered_map<pid_t,
container_state_info::hier_pid_info *
> parent_pid_lookup;
// Take each pid in the original map
while (!processes.empty())
{
auto b=processes.begin();
// See if its parent also exists in the map, if so look at
// the parent, and proceed recursively.
auto p=processes.find(b->second.ppid);
size_t failsafe=processes.size();
while (p != processes.end() && --failsafe)
{
b=p;
p=processes.find(b->second.ppid);
}
// We maintain a lookup map of pids to their entry in the
// hier_pids list.
auto parent=parent_pid_lookup.find(b->second.ppid);
if (parent != parent_pid_lookup.end())
{
// This pid is a child of a parent pid that's already
// in the pids, so move it there, and update the
// parent_pid_lookup so that the moved pid can be found
// in the hier_pids
parent_pid_lookup[b->first]=&(
parent->second->child_pids[b->first]=
container_state_info::hier_pid_info{
std::move(b->second)
}
);
}
else
{
// Move the pid to the hier pid and update the
// lookup map.
parent_pid_lookup[b->first]=&(
pids[b->first]=
container_state_info::hier_pid_info{
std::move(b->second),
{}
}
);
}
processes.erase(b);
}
}
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)
{
auto overrides=proc_get_overrides(globaldir, localdir, overridedir);
// Masked containers won't be in the status, let's put them there.
for (auto &[name, override_status] : overrides)
{
if (override_status.get_state() ==
proc_override::state_t::masked)
{
status[name].state="masked";
}
}
for (auto &[name, info] : status)
{
std::filesystem::path path{name};
while (!path.empty())
{
auto iter=overrides.find(path);
if (iter != overrides.end())
{
switch (iter->second.get_state()) {
case proc_override::state_t::masked:
break;
case proc_override::state_t::none:
break;
case proc_override::state_t::enabled:
info.enabled=true;
break;
}
info.resources=iter->second.get_resources();
}
path=path.parent_path();
}
}
}
std::tuple<external_filedesc, external_filedesc> create_fake_request()
{
int sockets[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0)
throw "socketpair() failed";
std::tuple<external_filedesc, external_filedesc> efd{
std::make_shared<external_filedescObj>(
sockets[0]
), std::make_shared<external_filedescObj>(
sockets[1]
)
};
if (fcntl(sockets[0], F_SETFD, FD_CLOEXEC) < 0 ||
fcntl(sockets[1], F_SETFD, FD_CLOEXEC) < 0)
{
throw "fnctl failed";
}
return efd;
}
// Have output from started/stopped/restarting/reloading units go to my
// standard output.
external_filedesc create_stdoutcc(const external_filedesc &efd)
{
auto [a, b]=create_fake_request();
efd->write_all("cc\n");
request_fd_wait(efd);
request_send_fd(efd, b->fd);
return a;
}
void send_setenv(const external_filedesc &fd,
std::string name,
std::string value)
{
if (name.find('\n') != name.npos ||
value.find('\n') != value.npos)
{
throw std::runtime_error{
_("Variable name or value cannot contain a newline")
};
}
fd->write_all("setenv\n" + name + "\n" + value + "\n");
}
void send_unsetenv(const external_filedesc &fd,
std::string name)
{
if (name.find('\n') != name.npos)
{
throw std::runtime_error{
_("Variable name cannot contain a newline")
};
}
fd->write_all("unsetenv\n" + name + "\n");
}
int wait_setunsetenv(const external_filedesc &fd)
{
auto error=fd->readln();
if (!error.empty())
{
std::cerr << error << std::endl;
return 1;
}
return 0;
}
void request_freeze(const external_filedesc &efd,
const std::string &name)
{
efd->write_all(std::string{"freeze\n"} + name + "\n");
}
void request_thaw(const external_filedesc &efd,
const std::string &name)
{
efd->write_all(std::string{"thaw\n"} + name + "\n");
}
std::string get_freeze_thaw_status(const external_filedesc &efd)
{
return efd->readln();
}