/
lxc_monitord.c
457 lines (386 loc) · 10.9 KB
/
lxc_monitord.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
/*
* lxc: linux Container library
*
* Copyright © 2012 Oracle.
*
* Authors:
* Dwight Engen <dwight.engen@oracle.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <netinet/in.h>
#include <setjmp.h>
#include <sys/epoll.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <lxc/lxccontainer.h>
#include "af_unix.h"
#include "log.h"
#include "mainloop.h"
#include "monitor.h"
#include "utils.h"
#define CLIENTFDS_CHUNK 64
lxc_log_define(lxc_monitord, lxc);
sigjmp_buf mark;
static void lxc_monitord_cleanup(void);
/*
* Defines the structure to store the monitor information
* @lxcpath : the path being monitored
* @fifofd : the file descriptor for publishers (containers) to write state
* @listenfd : the file descriptor for subscribers (lxc-monitors) to connect
* @clientfds : accepted client file descriptors
* @clientfds_size : number of file descriptors clientfds can hold
* @clientfds_cnt : the count of valid fds in clientfds
* @descr : the lxc_mainloop state
*/
struct lxc_monitor {
const char *lxcpath;
int fifofd;
int listenfd;
int *clientfds;
int clientfds_size;
int clientfds_cnt;
struct lxc_epoll_descr descr;
};
static struct lxc_monitor mon;
static int quit;
static int lxc_monitord_fifo_create(struct lxc_monitor *mon)
{
struct flock lk;
char fifo_path[PATH_MAX];
int ret;
ret = lxc_monitor_fifo_name(mon->lxcpath, fifo_path, sizeof(fifo_path), 1);
if (ret < 0)
return ret;
ret = mknod(fifo_path, S_IFIFO|S_IRUSR|S_IWUSR, 0);
if (ret < 0 && errno != EEXIST) {
INFO("Failed to mknod monitor fifo %s: %s.", fifo_path, strerror(errno));
return -1;
}
mon->fifofd = open(fifo_path, O_RDWR);
if (mon->fifofd < 0) {
unlink(fifo_path);
ERROR("Failed to open monitor fifo.");
return -1;
}
lk.l_type = F_WRLCK;
lk.l_whence = SEEK_SET;
lk.l_start = 0;
lk.l_len = 0;
if (fcntl(mon->fifofd, F_SETLK, &lk) != 0) {
/* another lxc-monitord is already running, don't start up */
DEBUG("lxc-monitord already running on lxcpath %s.", mon->lxcpath);
close(mon->fifofd);
return -1;
}
return 0;
}
static int lxc_monitord_fifo_delete(struct lxc_monitor *mon)
{
char fifo_path[PATH_MAX];
int ret;
ret = lxc_monitor_fifo_name(mon->lxcpath, fifo_path, sizeof(fifo_path), 0);
if (ret < 0)
return ret;
unlink(fifo_path);
return 0;
}
static void lxc_monitord_sockfd_remove(struct lxc_monitor *mon, int fd) {
int i;
if (lxc_mainloop_del_handler(&mon->descr, fd))
CRIT("File descriptor %d not found in mainloop.", fd);
close(fd);
for (i = 0; i < mon->clientfds_cnt; i++) {
if (mon->clientfds[i] == fd)
break;
}
if (i >= mon->clientfds_cnt) {
CRIT("File descriptor %d not found in clients array.", fd);
lxc_monitord_cleanup();
exit(EXIT_FAILURE);
}
memmove(&mon->clientfds[i], &mon->clientfds[i+1],
(mon->clientfds_cnt - i - 1) * sizeof(mon->clientfds[0]));
mon->clientfds_cnt--;
}
static int lxc_monitord_sock_handler(int fd, uint32_t events, void *data,
struct lxc_epoll_descr *descr)
{
struct lxc_monitor *mon = data;
if (events & EPOLLIN) {
int rc;
char buf[4];
rc = read(fd, buf, sizeof(buf));
if (rc > 0 && !strncmp(buf, "quit", 4))
quit = 1;
}
if (events & EPOLLHUP)
lxc_monitord_sockfd_remove(mon, fd);
return quit;
}
static int lxc_monitord_sock_accept(int fd, uint32_t events, void *data,
struct lxc_epoll_descr *descr)
{
int ret,clientfd;
struct lxc_monitor *mon = data;
struct ucred cred;
socklen_t credsz = sizeof(cred);
ret = -1;
clientfd = accept(fd, NULL, 0);
if (clientfd < 0) {
SYSERROR("Failed to accept connection for client file descriptor %d.", fd);
goto out;
}
if (fcntl(clientfd, F_SETFD, FD_CLOEXEC)) {
SYSERROR("Failed to set FD_CLOEXEC on client socket connection %d.", clientfd);
goto err1;
}
if (getsockopt(clientfd, SOL_SOCKET, SO_PEERCRED, &cred, &credsz))
{
ERROR("Failed to get credentials on client socket connection %d.", clientfd);
goto err1;
}
if (cred.uid && cred.uid != geteuid()) {
WARN("Monitor denied for uid %d on client socket connection %d.", cred.uid, clientfd);
ret = -EACCES;
goto err1;
}
if (mon->clientfds_cnt + 1 > mon->clientfds_size) {
int *clientfds;
clientfds = realloc(mon->clientfds,
(mon->clientfds_size + CLIENTFDS_CHUNK) * sizeof(mon->clientfds[0]));
if (clientfds == NULL) {
ERROR("Failed to realloc memory for %d client file "
"descriptors.",
mon->clientfds_size + CLIENTFDS_CHUNK);
goto err1;
}
mon->clientfds = clientfds;
mon->clientfds_size += CLIENTFDS_CHUNK;
}
ret = lxc_mainloop_add_handler(&mon->descr, clientfd,
lxc_monitord_sock_handler, mon);
if (ret) {
ERROR("Failed to add socket handler.");
goto err1;
}
mon->clientfds[mon->clientfds_cnt++] = clientfd;
INFO("Accepted client file descriptor %d. Number of accepted file descriptors is now %d.", clientfd, mon->clientfds_cnt);
goto out;
err1:
close(clientfd);
out:
return ret;
}
static int lxc_monitord_sock_create(struct lxc_monitor *mon)
{
struct sockaddr_un addr;
int fd;
if (lxc_monitor_sock_name(mon->lxcpath, &addr) < 0)
return -1;
fd = lxc_abstract_unix_open(addr.sun_path, SOCK_STREAM, O_TRUNC);
if (fd < 0) {
ERROR("Failed to open unix socket: %s.", strerror(errno));
return -1;
}
mon->listenfd = fd;
return 0;
}
static int lxc_monitord_sock_delete(struct lxc_monitor *mon)
{
struct sockaddr_un addr;
if (lxc_monitor_sock_name(mon->lxcpath, &addr) < 0)
return -1;
if (addr.sun_path[0])
unlink(addr.sun_path);
return 0;
}
static int lxc_monitord_create(struct lxc_monitor *mon)
{
int ret;
ret = lxc_monitord_fifo_create(mon);
if (ret < 0)
return ret;
ret = lxc_monitord_sock_create(mon);
return ret;
}
static void lxc_monitord_delete(struct lxc_monitor *mon)
{
int i;
lxc_mainloop_del_handler(&mon->descr, mon->listenfd);
close(mon->listenfd);
lxc_monitord_sock_delete(mon);
lxc_mainloop_del_handler(&mon->descr, mon->fifofd);
lxc_monitord_fifo_delete(mon);
close(mon->fifofd);
for (i = 0; i < mon->clientfds_cnt; i++) {
lxc_mainloop_del_handler(&mon->descr, mon->clientfds[i]);
close(mon->clientfds[i]);
}
mon->clientfds_cnt = 0;
}
static int lxc_monitord_fifo_handler(int fd, uint32_t events, void *data,
struct lxc_epoll_descr *descr)
{
int ret,i;
struct lxc_msg msglxc;
struct lxc_monitor *mon = data;
ret = read(fd, &msglxc, sizeof(msglxc));
if (ret != sizeof(msglxc)) {
SYSERROR("Reading from fifo failed: %s.", strerror(errno));
return 1;
}
for (i = 0; i < mon->clientfds_cnt; i++) {
ret = write(mon->clientfds[i], &msglxc, sizeof(msglxc));
if (ret < 0)
ERROR("Failed to send message to client file descriptor %d: %s.",
mon->clientfds[i], strerror(errno));
}
return 0;
}
static int lxc_monitord_mainloop_add(struct lxc_monitor *mon)
{
int ret;
ret = lxc_mainloop_add_handler(&mon->descr, mon->fifofd,
lxc_monitord_fifo_handler, mon);
if (ret < 0) {
ERROR("Failed to add to mainloop monitor handler for fifo.");
return -1;
}
ret = lxc_mainloop_add_handler(&mon->descr, mon->listenfd,
lxc_monitord_sock_accept, mon);
if (ret < 0) {
ERROR("Failed to add to mainloop monitor handler for listen socket.");
return -1;
}
return 0;
}
static void lxc_monitord_cleanup(void)
{
lxc_monitord_delete(&mon);
}
static void lxc_monitord_sig_handler(int sig)
{
siglongjmp(mark, 1);
}
int main(int argc, char *argv[])
{
int ret, pipefd;
char logpath[PATH_MAX];
sigset_t mask;
char *lxcpath = argv[1];
bool mainloop_opened = false;
bool monitord_created = false;
struct lxc_log log;
if (argc != 3) {
fprintf(stderr,
"Usage: lxc-monitord lxcpath sync-pipe-fd\n\n"
"NOTE: lxc-monitord is intended for use by lxc internally\n"
" and does not need to be run by hand\n\n");
exit(EXIT_FAILURE);
}
ret = snprintf(logpath, sizeof(logpath), "%s/lxc-monitord.log",
(strcmp(LXCPATH, lxcpath) ? lxcpath : LOGPATH ));
if (ret < 0 || ret >= sizeof(logpath))
exit(EXIT_FAILURE);
log.name = NULL;
log.file = logpath;
log.level = "DEBUG";
log.prefix = "lxc-monitord";
log.quiet = 0;
log.lxcpath = lxcpath;
ret = lxc_log_init(&log);
if (ret)
INFO("Failed to open log file %s, log will be lost.", lxcpath);
lxc_log_options_no_override();
if (lxc_safe_int(argv[2], &pipefd) < 0)
exit(EXIT_FAILURE);
if (sigfillset(&mask) ||
sigdelset(&mask, SIGILL) ||
sigdelset(&mask, SIGSEGV) ||
sigdelset(&mask, SIGBUS) ||
sigdelset(&mask, SIGTERM) ||
sigprocmask(SIG_BLOCK, &mask, NULL)) {
SYSERROR("Failed to set signal mask.");
exit(EXIT_FAILURE);
}
signal(SIGILL, lxc_monitord_sig_handler);
signal(SIGSEGV, lxc_monitord_sig_handler);
signal(SIGBUS, lxc_monitord_sig_handler);
signal(SIGTERM, lxc_monitord_sig_handler);
if (sigsetjmp(mark, 1) != 0)
goto on_signal;
ret = EXIT_FAILURE;
memset(&mon, 0, sizeof(mon));
mon.lxcpath = lxcpath;
if (lxc_mainloop_open(&mon.descr)) {
ERROR("Failed to create mainloop.");
goto on_error;
}
mainloop_opened = true;
if (lxc_monitord_create(&mon))
goto on_error;
monitord_created = true;
/* sync with parent, we're ignoring the return from write
* because regardless if it works or not, the following
* close will sync us with the parent process. the
* if-empty-statement construct is to quiet the
* warn-unused-result warning.
*/
if (write(pipefd, "S", 1))
;
close(pipefd);
if (lxc_monitord_mainloop_add(&mon)) {
ERROR("Failed to add mainloop handlers.");
goto on_error;
}
NOTICE("lxc-monitord with pid %d is now monitoring lxcpath %s.",
lxc_raw_getpid(), mon.lxcpath);
for (;;) {
ret = lxc_mainloop(&mon.descr, 1000 * 30);
if (ret) {
ERROR("mainloop returned an error");
break;
}
if (mon.clientfds_cnt <= 0) {
NOTICE("No remaining clients. lxc-monitord is exiting.");
break;
}
if (quit == 1) {
NOTICE("got quit command. lxc-monitord is exitting.");
break;
}
}
on_signal:
ret = EXIT_SUCCESS;
on_error:
if (monitord_created)
lxc_monitord_cleanup();
if (mainloop_opened)
lxc_mainloop_close(&mon.descr);
exit(ret);
}