trace: use standard POSIX header for basename()
[project/procd.git] / service / instance.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #define _GNU_SOURCE
16 #include <sys/resource.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/stat.h>
20 #include <grp.h>
21 #include <net/if.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <pwd.h>
27 #include <libgen.h>
28 #include <unistd.h>
29 #define SYSLOG_NAMES
30 #include <syslog.h>
31
32 #include <libubox/md5.h>
33
34 #include "../procd.h"
35
36 #include "service.h"
37 #include "instance.h"
38
39 #define UJAIL_BIN_PATH "/sbin/ujail"
40 #define CGROUP_BASEDIR "/sys/fs/cgroup/services"
41
42 enum {
43 INSTANCE_ATTR_COMMAND,
44 INSTANCE_ATTR_ENV,
45 INSTANCE_ATTR_DATA,
46 INSTANCE_ATTR_NETDEV,
47 INSTANCE_ATTR_FILE,
48 INSTANCE_ATTR_TRIGGER,
49 INSTANCE_ATTR_RESPAWN,
50 INSTANCE_ATTR_NICE,
51 INSTANCE_ATTR_LIMITS,
52 INSTANCE_ATTR_WATCH,
53 INSTANCE_ATTR_ERROR,
54 INSTANCE_ATTR_USER,
55 INSTANCE_ATTR_GROUP,
56 INSTANCE_ATTR_STDOUT,
57 INSTANCE_ATTR_STDERR,
58 INSTANCE_ATTR_NO_NEW_PRIVS,
59 INSTANCE_ATTR_JAIL,
60 INSTANCE_ATTR_TRACE,
61 INSTANCE_ATTR_SECCOMP,
62 INSTANCE_ATTR_CAPABILITIES,
63 INSTANCE_ATTR_PIDFILE,
64 INSTANCE_ATTR_RELOADSIG,
65 INSTANCE_ATTR_TERMTIMEOUT,
66 INSTANCE_ATTR_FACILITY,
67 INSTANCE_ATTR_EXTROOT,
68 INSTANCE_ATTR_OVERLAYDIR,
69 INSTANCE_ATTR_TMPOVERLAYSIZE,
70 INSTANCE_ATTR_BUNDLE,
71 INSTANCE_ATTR_WATCHDOG,
72 __INSTANCE_ATTR_MAX
73 };
74
75 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
76 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
77 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
78 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
79 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
80 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
81 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
82 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
83 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
84 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
85 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
86 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
87 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
88 [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
89 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
90 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
91 [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
92 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
93 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
94 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
95 [INSTANCE_ATTR_CAPABILITIES] = { "capabilities", BLOBMSG_TYPE_STRING },
96 [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
97 [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
98 [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
99 [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
100 [INSTANCE_ATTR_EXTROOT] = { "extroot", BLOBMSG_TYPE_STRING },
101 [INSTANCE_ATTR_OVERLAYDIR] = { "overlaydir", BLOBMSG_TYPE_STRING },
102 [INSTANCE_ATTR_TMPOVERLAYSIZE] = { "tmpoverlaysize", BLOBMSG_TYPE_STRING },
103 [INSTANCE_ATTR_BUNDLE] = { "bundle", BLOBMSG_TYPE_STRING },
104 [INSTANCE_ATTR_WATCHDOG] = { "watchdog", BLOBMSG_TYPE_ARRAY },
105 };
106
107 enum {
108 JAIL_ATTR_NAME,
109 JAIL_ATTR_HOSTNAME,
110 JAIL_ATTR_PROCFS,
111 JAIL_ATTR_SYSFS,
112 JAIL_ATTR_UBUS,
113 JAIL_ATTR_LOG,
114 JAIL_ATTR_RONLY,
115 JAIL_ATTR_MOUNT,
116 JAIL_ATTR_NETNS,
117 JAIL_ATTR_USERNS,
118 JAIL_ATTR_CGROUPSNS,
119 JAIL_ATTR_CONSOLE,
120 JAIL_ATTR_REQUIREJAIL,
121 JAIL_ATTR_IMMEDIATELY,
122 JAIL_ATTR_PIDFILE,
123 __JAIL_ATTR_MAX,
124 };
125
126 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
127 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
128 [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
129 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
130 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
131 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
132 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
133 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
134 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
135 [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
136 [JAIL_ATTR_USERNS] = { "userns", BLOBMSG_TYPE_BOOL },
137 [JAIL_ATTR_CGROUPSNS] = { "cgroupsns", BLOBMSG_TYPE_BOOL },
138 [JAIL_ATTR_CONSOLE] = { "console", BLOBMSG_TYPE_BOOL },
139 [JAIL_ATTR_REQUIREJAIL] = { "requirejail", BLOBMSG_TYPE_BOOL },
140 [JAIL_ATTR_IMMEDIATELY] = { "immediately", BLOBMSG_TYPE_BOOL },
141 [JAIL_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
142 };
143
144 struct instance_netdev {
145 struct blobmsg_list_node node;
146 int ifindex;
147 };
148
149 struct instance_file {
150 struct blobmsg_list_node node;
151 uint32_t md5[4];
152 };
153
154 struct rlimit_name {
155 const char *name;
156 int resource;
157 };
158
159 static const struct rlimit_name rlimit_names[] = {
160 { "as", RLIMIT_AS },
161 { "core", RLIMIT_CORE },
162 { "cpu", RLIMIT_CPU },
163 { "data", RLIMIT_DATA },
164 { "fsize", RLIMIT_FSIZE },
165 { "memlock", RLIMIT_MEMLOCK },
166 { "nofile", RLIMIT_NOFILE },
167 { "nproc", RLIMIT_NPROC },
168 { "rss", RLIMIT_RSS },
169 { "stack", RLIMIT_STACK },
170 #ifdef linux
171 { "nice", RLIMIT_NICE },
172 { "rtprio", RLIMIT_RTPRIO },
173 { "msgqueue", RLIMIT_MSGQUEUE },
174 { "sigpending", RLIMIT_SIGPENDING },
175 #endif
176 { NULL, 0 }
177 };
178
179 static void closefd(int fd)
180 {
181 if (fd > STDERR_FILENO)
182 close(fd);
183 }
184
185 /* convert a string into numeric syslog facility or return -1 if no match found */
186 static int
187 syslog_facility_str_to_int(const char *facility)
188 {
189 CODE *p = facilitynames;
190
191 while (p->c_name && strcasecmp(p->c_name, facility))
192 p++;
193
194 return p->c_val;
195 }
196
197 static void
198 instance_limits(const char *limit, const char *value)
199 {
200 int i;
201 struct rlimit rlim;
202 unsigned long cur, max;
203
204 for (i = 0; rlimit_names[i].name != NULL; i++) {
205 if (strcmp(rlimit_names[i].name, limit))
206 continue;
207 if (!strcmp(value, "unlimited")) {
208 rlim.rlim_cur = RLIM_INFINITY;
209 rlim.rlim_max = RLIM_INFINITY;
210 } else {
211 if (getrlimit(rlimit_names[i].resource, &rlim))
212 return;
213
214 cur = rlim.rlim_cur;
215 max = rlim.rlim_max;
216
217 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
218 return;
219
220 rlim.rlim_cur = cur;
221 rlim.rlim_max = max;
222 }
223
224 setrlimit(rlimit_names[i].resource, &rlim);
225 return;
226 }
227 }
228
229 static inline int
230 jail_run(struct service_instance *in, char **argv)
231 {
232 struct blobmsg_list_node *var;
233 struct jail *jail = &in->jail;
234 int argc = 0;
235
236 argv[argc++] = UJAIL_BIN_PATH;
237
238 if (jail->name) {
239 argv[argc++] = "-n";
240 argv[argc++] = jail->name;
241 }
242
243 if (jail->hostname) {
244 argv[argc++] = "-h";
245 argv[argc++] = jail->hostname;
246 }
247
248 if (in->seccomp) {
249 argv[argc++] = "-S";
250 argv[argc++] = in->seccomp;
251 }
252
253 if (in->user) {
254 argv[argc++] = "-U";
255 argv[argc++] = in->user;
256 }
257
258 if (in->group) {
259 argv[argc++] = "-G";
260 argv[argc++] = in->group;
261 }
262
263 if (in->capabilities) {
264 argv[argc++] = "-C";
265 argv[argc++] = in->capabilities;
266 }
267
268 if (in->no_new_privs)
269 argv[argc++] = "-c";
270
271 if (jail->procfs)
272 argv[argc++] = "-p";
273
274 if (jail->sysfs)
275 argv[argc++] = "-s";
276
277 if (jail->ubus)
278 argv[argc++] = "-u";
279
280 if (jail->log)
281 argv[argc++] = "-l";
282
283 if (jail->ronly)
284 argv[argc++] = "-o";
285
286 if (jail->netns)
287 argv[argc++] = "-N";
288
289 if (jail->userns)
290 argv[argc++] = "-f";
291
292 if (jail->cgroupsns)
293 argv[argc++] = "-F";
294
295 if (jail->console)
296 argv[argc++] = "-y";
297
298 if (in->extroot) {
299 argv[argc++] = "-R";
300 argv[argc++] = in->extroot;
301 }
302
303 if (in->overlaydir) {
304 argv[argc++] = "-O";
305 argv[argc++] = in->overlaydir;
306 }
307
308 if (in->tmpoverlaysize) {
309 argv[argc++] = "-T";
310 argv[argc++] = in->tmpoverlaysize;
311 }
312
313 if (in->immediately)
314 argv[argc++] = "-i";
315
316 if (jail->pidfile) {
317 argv[argc++] = "-P";
318 argv[argc++] = jail->pidfile;
319 }
320
321 if (in->bundle) {
322 argv[argc++] = "-J";
323 argv[argc++] = in->bundle;
324 }
325
326 if (in->require_jail)
327 argv[argc++] = "-E";
328
329 blobmsg_list_for_each(&jail->mount, var) {
330 const char *type = blobmsg_data(var->data);
331
332 if (*type == '1')
333 argv[argc++] = "-w";
334 else
335 argv[argc++] = "-r";
336 argv[argc++] = (char *) blobmsg_name(var->data);
337 }
338
339 argv[argc++] = "--";
340
341 return argc;
342 }
343
344 static int
345 instance_removepid(struct service_instance *in) {
346 if (!in->pidfile)
347 return 0;
348 if (unlink(in->pidfile)) {
349 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
350 return 1;
351 }
352 return 0;
353 }
354
355 static int
356 instance_writepid(struct service_instance *in)
357 {
358 FILE *_pidfile;
359
360 if (!in->pidfile) {
361 return 0;
362 }
363 _pidfile = fopen(in->pidfile, "w");
364 if (_pidfile == NULL) {
365 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
366 return 1;
367 }
368 if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
369 ERROR("failed to write pidfile: %s: %m", in->pidfile);
370 fclose(_pidfile);
371 return 2;
372 }
373 if (fclose(_pidfile)) {
374 ERROR("failed to close pidfile: %s: %m", in->pidfile);
375 return 3;
376 }
377
378 return 0;
379 }
380
381 static void
382 instance_run(struct service_instance *in, int _stdout, int _stderr)
383 {
384 struct blobmsg_list_node *var;
385 struct blob_attr *cur;
386 char **argv;
387 int argc = 1; /* NULL terminated */
388 int rem, _stdin;
389 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
390 bool setlbf = _stdout >= 0;
391
392 if (in->nice)
393 setpriority(PRIO_PROCESS, 0, in->nice);
394
395 blobmsg_for_each_attr(cur, in->command, rem)
396 argc++;
397
398 blobmsg_list_for_each(&in->env, var)
399 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
400
401 if (seccomp)
402 setenv("SECCOMP_FILE", in->seccomp, 1);
403
404 if (setlbf)
405 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
406
407 blobmsg_list_for_each(&in->limits, var)
408 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
409
410 if (in->trace || seccomp)
411 argc += 1;
412
413 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
414 argc = 0;
415
416 #ifdef SECCOMP_SUPPORT
417 if (in->trace)
418 argv[argc++] = "/sbin/utrace";
419 else if (seccomp)
420 argv[argc++] = "/sbin/seccomp-trace";
421 #else
422 if (in->trace || seccomp)
423 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
424 #endif
425
426 if (in->has_jail) {
427 argc = jail_run(in, argv);
428 if (argc != in->jail.argc)
429 ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
430 in->jail.argc, argc, in->srv->name, in->name);
431 }
432
433 blobmsg_for_each_attr(cur, in->command, rem)
434 argv[argc++] = blobmsg_data(cur);
435
436 argv[argc] = NULL;
437
438 _stdin = open("/dev/null", O_RDONLY);
439
440 if (_stdout == -1)
441 _stdout = open("/dev/null", O_WRONLY);
442
443 if (_stderr == -1)
444 _stderr = open("/dev/null", O_WRONLY);
445
446 if (_stdin > -1) {
447 dup2(_stdin, STDIN_FILENO);
448 closefd(_stdin);
449 }
450 if (_stdout > -1) {
451 dup2(_stdout, STDOUT_FILENO);
452 closefd(_stdout);
453 }
454 if (_stderr > -1) {
455 dup2(_stderr, STDERR_FILENO);
456 closefd(_stderr);
457 }
458
459 if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
460 ERROR("failed to initgroups() for user %s: %m\n", in->user);
461 exit(127);
462 }
463 if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
464 ERROR("failed to set group id %d: %m\n", in->gr_gid);
465 exit(127);
466 }
467 if (!in->has_jail && in->uid && setuid(in->uid)) {
468 ERROR("failed to set user id %d: %m\n", in->uid);
469 exit(127);
470 }
471
472 execvp(argv[0], argv);
473 exit(127);
474 }
475
476 static void
477 instance_add_cgroup(const char *service, const char *instance)
478 {
479 struct stat sb;
480 char cgnamebuf[256];
481 int fd;
482
483 if (stat("/sys/fs/cgroup/cgroup.subtree_control", &sb))
484 return;
485
486 mkdir(CGROUP_BASEDIR, 0700);
487
488 snprintf(cgnamebuf, sizeof(cgnamebuf), "%s/%s", CGROUP_BASEDIR, service);
489 mkdir(cgnamebuf, 0700);
490 snprintf(cgnamebuf, sizeof(cgnamebuf), "%s/%s/%s", CGROUP_BASEDIR, service, instance);
491 mkdir(cgnamebuf, 0700);
492 strcat(cgnamebuf, "/cgroup.procs");
493
494 fd = open(cgnamebuf, O_WRONLY);
495 if (fd == -1)
496 return;
497
498 dprintf(fd, "%d", getpid());
499 close(fd);
500 }
501
502 static void
503 instance_free_stdio(struct service_instance *in)
504 {
505 if (in->_stdout.fd.fd > -1) {
506 ustream_free(&in->_stdout.stream);
507 close(in->_stdout.fd.fd);
508 in->_stdout.fd.fd = -1;
509 }
510
511 if (in->_stderr.fd.fd > -1) {
512 ustream_free(&in->_stderr.stream);
513 close(in->_stderr.fd.fd);
514 in->_stderr.fd.fd = -1;
515 }
516
517 if (in->console.fd.fd > -1) {
518 ustream_free(&in->console.stream);
519 close(in->console.fd.fd);
520 in->console.fd.fd = -1;
521 }
522
523 if (in->console_client.fd.fd > -1) {
524 ustream_free(&in->console_client.stream);
525 close(in->console_client.fd.fd);
526 in->console_client.fd.fd = -1;
527 }
528 }
529
530 void
531 instance_start(struct service_instance *in)
532 {
533 int pid;
534 int opipe[2] = { -1, -1 };
535 int epipe[2] = { -1, -1 };
536
537 if (!avl_is_empty(&in->errors.avl)) {
538 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
539 return;
540 }
541
542 if (!in->bundle && !in->command) {
543 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
544 return;
545 }
546
547 if (in->proc.pending) {
548 if (in->halt)
549 in->restart = true;
550 return;
551 }
552
553 instance_free_stdio(in);
554 if (in->_stdout.fd.fd > -2) {
555 if (pipe(opipe)) {
556 ULOG_WARN("pipe() failed: %m\n");
557 opipe[0] = opipe[1] = -1;
558 }
559 }
560
561 if (in->_stderr.fd.fd > -2) {
562 if (pipe(epipe)) {
563 ULOG_WARN("pipe() failed: %m\n");
564 epipe[0] = epipe[1] = -1;
565 }
566 }
567
568 in->restart = false;
569 in->halt = false;
570
571 if (!in->valid)
572 return;
573
574 pid = fork();
575 if (pid < 0)
576 return;
577
578 if (!pid) {
579 uloop_done();
580 closefd(opipe[0]);
581 closefd(epipe[0]);
582 instance_add_cgroup(in->srv->name, in->name);
583 instance_run(in, opipe[1], epipe[1]);
584 return;
585 }
586
587 DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
588 in->proc.pid = pid;
589 instance_writepid(in);
590 clock_gettime(CLOCK_MONOTONIC, &in->start);
591 uloop_process_add(&in->proc);
592
593 if (opipe[0] > -1) {
594 ustream_fd_init(&in->_stdout, opipe[0]);
595 closefd(opipe[1]);
596 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
597 }
598
599 if (epipe[0] > -1) {
600 ustream_fd_init(&in->_stderr, epipe[0]);
601 closefd(epipe[1]);
602 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
603 }
604
605 if (in->watchdog.mode != INSTANCE_WATCHDOG_MODE_DISABLED) {
606 uloop_timeout_set(&in->watchdog.timeout, in->watchdog.freq * 1000);
607 DEBUG(2, "Started instance %s::%s watchdog timer : timeout = %d\n", in->srv->name, in->name, in->watchdog.freq);
608 }
609
610 service_event("instance.start", in->srv->name, in->name);
611 }
612
613 static void
614 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
615 {
616 char *newline, *str, *arg0, ident[32];
617 int len;
618
619 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
620 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
621 ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
622
623 do {
624 str = ustream_get_read_buf(s, &len);
625 if (!str)
626 break;
627
628 newline = memchr(str, '\n', len);
629 if (!newline && (s->r.buffer_len != len))
630 break;
631
632 if (newline) {
633 *newline = 0;
634 len = newline + 1 - str;
635 }
636 ulog(prio, "%s\n", str);
637
638 ustream_consume(s, len);
639 } while (1);
640
641 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
642 }
643
644 static void
645 instance_stdout(struct ustream *s, int bytes)
646 {
647 instance_stdio(s, LOG_INFO,
648 container_of(s, struct service_instance, _stdout.stream));
649 }
650
651 static void
652 instance_console(struct ustream *s, int bytes)
653 {
654 struct service_instance *in = container_of(s, struct service_instance, console.stream);
655 char *buf;
656 int len;
657
658 do {
659 buf = ustream_get_read_buf(s, &len);
660 if (!buf)
661 break;
662
663 ulog(LOG_INFO, "out: %s\n", buf);
664
665 /* test if console client is attached */
666 if (in->console_client.fd.fd > -1)
667 ustream_write(&in->console_client.stream, buf, len, false);
668
669 ustream_consume(s, len);
670 } while (1);
671 }
672
673 static void
674 instance_console_client(struct ustream *s, int bytes)
675 {
676 struct service_instance *in = container_of(s, struct service_instance, console_client.stream);
677 char *buf;
678 int len;
679
680 do {
681 buf = ustream_get_read_buf(s, &len);
682 if (!buf)
683 break;
684
685 ulog(LOG_INFO, "in: %s\n", buf);
686 ustream_write(&in->console.stream, buf, len, false);
687 ustream_consume(s, len);
688 } while (1);
689 }
690
691 static void
692 instance_stderr(struct ustream *s, int bytes)
693 {
694 instance_stdio(s, LOG_ERR,
695 container_of(s, struct service_instance, _stderr.stream));
696 }
697
698 static void
699 instance_timeout(struct uloop_timeout *t)
700 {
701 struct service_instance *in;
702
703 in = container_of(t, struct service_instance, timeout);
704
705 if (in->halt) {
706 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
707 in->srv->name, in->name, in->proc.pid);
708 kill(in->proc.pid, SIGKILL);
709 } else if (in->restart || in->respawn)
710 instance_start(in);
711 }
712
713 static void
714 instance_delete(struct service_instance *in)
715 {
716 struct service *s = in->srv;
717
718 avl_delete(&s->instances.avl, &in->node.avl);
719 instance_free(in);
720 service_stopped(s);
721 }
722
723 static int
724 instance_exit_code(int ret)
725 {
726 if (WIFEXITED(ret)) {
727 return WEXITSTATUS(ret);
728 }
729
730 if (WIFSIGNALED(ret)) {
731 return SIGNALLED_OFFSET + WTERMSIG(ret);
732 }
733
734 if (WIFSTOPPED(ret)) {
735 return WSTOPSIG(ret);
736 }
737
738 return 1;
739 }
740
741 static void
742 instance_exit(struct uloop_process *p, int ret)
743 {
744 struct service_instance *in;
745 struct timespec tp;
746 long runtime;
747
748 in = container_of(p, struct service_instance, proc);
749
750 clock_gettime(CLOCK_MONOTONIC, &tp);
751 runtime = tp.tv_sec - in->start.tv_sec;
752
753 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
754
755 in->exit_code = instance_exit_code(ret);
756 uloop_timeout_cancel(&in->timeout);
757 uloop_timeout_cancel(&in->watchdog.timeout);
758 service_event("instance.stop", in->srv->name, in->name);
759
760 if (in->halt) {
761 instance_removepid(in);
762 if (in->restart)
763 instance_start(in);
764 else
765 instance_delete(in);
766 } else if (in->restart) {
767 instance_start(in);
768 } else if (in->respawn) {
769 if (runtime < in->respawn_threshold)
770 in->respawn_count++;
771 else
772 in->respawn_count = 0;
773 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
774 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
775 in->srv->name, in->name, in->respawn_count, runtime);
776 in->restart = in->respawn = 0;
777 in->halt = 1;
778 service_event("instance.fail", in->srv->name, in->name);
779 } else {
780 service_event("instance.respawn", in->srv->name, in->name);
781 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
782 }
783 }
784 }
785
786 void
787 instance_stop(struct service_instance *in, bool halt)
788 {
789 if (!in->proc.pending) {
790 if (halt)
791 instance_delete(in);
792 return;
793 }
794 in->halt = halt;
795 in->restart = in->respawn = false;
796 kill(in->proc.pid, SIGTERM);
797 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
798 }
799
800 static void
801 instance_restart(struct service_instance *in)
802 {
803 if (!in->proc.pending)
804 return;
805
806 if (in->reload_signal) {
807 kill(in->proc.pid, in->reload_signal);
808 return;
809 }
810
811 in->halt = true;
812 in->restart = true;
813 kill(in->proc.pid, SIGTERM);
814 uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
815 }
816
817 static void
818 instance_watchdog(struct uloop_timeout *t)
819 {
820 struct service_instance *in = container_of(t, struct service_instance, watchdog.timeout);
821
822 DEBUG(3, "instance %s::%s watchdog timer expired\n", in->srv->name, in->name);
823
824 if (in->respawn)
825 instance_restart(in);
826 else
827 instance_stop(in, true);
828 }
829
830 static bool string_changed(const char *a, const char *b)
831 {
832 return !((!a && !b) || (a && b && !strcmp(a, b)));
833 }
834
835 static bool
836 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
837 {
838 if (!in->valid)
839 return true;
840
841 if (!blob_attr_equal(in->command, in_new->command))
842 return true;
843
844 if (string_changed(in->bundle, in_new->bundle))
845 return true;
846
847 if (string_changed(in->extroot, in_new->extroot))
848 return true;
849
850 if (string_changed(in->overlaydir, in_new->overlaydir))
851 return true;
852
853 if (string_changed(in->tmpoverlaysize, in_new->tmpoverlaysize))
854 return true;
855
856 if (!blobmsg_list_equal(&in->env, &in_new->env))
857 return true;
858
859 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
860 return true;
861
862 if (!blobmsg_list_equal(&in->file, &in_new->file))
863 return true;
864
865 if (in->nice != in_new->nice)
866 return true;
867
868 if (in->syslog_facility != in_new->syslog_facility)
869 return true;
870
871 if (string_changed(in->user, in_new->user))
872 return true;
873
874 if (string_changed(in->group, in_new->group))
875 return true;
876
877 if (in->uid != in_new->uid)
878 return true;
879
880 if (in->pw_gid != in_new->pw_gid)
881 return true;
882
883 if (in->gr_gid != in_new->gr_gid)
884 return true;
885
886 if (string_changed(in->pidfile, in_new->pidfile))
887 return true;
888
889 if (in->respawn_retry != in_new->respawn_retry)
890 return true;
891 if (in->respawn_threshold != in_new->respawn_threshold)
892 return true;
893 if (in->respawn_timeout != in_new->respawn_timeout)
894 return true;
895
896 if (in->reload_signal != in_new->reload_signal)
897 return true;
898
899 if (in->term_timeout != in_new->term_timeout)
900 return true;
901
902 if (string_changed(in->seccomp, in_new->seccomp))
903 return true;
904
905 if (string_changed(in->capabilities, in_new->capabilities))
906 return true;
907
908 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
909 return true;
910
911 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
912 return true;
913
914 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
915 return true;
916
917 if (in->has_jail != in_new->has_jail)
918 return true;
919
920 if (in->trace != in_new->trace)
921 return true;
922
923 if (in->require_jail != in_new->require_jail)
924 return true;
925
926 if (in->immediately != in_new->immediately)
927 return true;
928
929 if (in->no_new_privs != in_new->no_new_privs)
930 return true;
931
932 if (string_changed(in->jail.name, in_new->jail.name))
933 return true;
934
935 if (string_changed(in->jail.hostname, in_new->jail.hostname))
936 return true;
937
938 if (string_changed(in->jail.pidfile, in_new->jail.pidfile))
939 return true;
940
941 if (in->jail.procfs != in_new->jail.procfs)
942 return true;
943
944 if (in->jail.sysfs != in_new->jail.sysfs)
945 return true;
946
947 if (in->jail.ubus != in_new->jail.ubus)
948 return true;
949
950 if (in->jail.log != in_new->jail.log)
951 return true;
952
953 if (in->jail.ronly != in_new->jail.ronly)
954 return true;
955
956 if (in->jail.netns != in_new->jail.netns)
957 return true;
958
959 if (in->jail.userns != in_new->jail.userns)
960 return true;
961
962 if (in->jail.cgroupsns != in_new->jail.cgroupsns)
963 return true;
964
965 if (in->jail.console != in_new->jail.console)
966 return true;
967
968 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
969 return true;
970
971 if (in->watchdog.mode != in_new->watchdog.mode)
972 return true;
973
974 if (in->watchdog.freq != in_new->watchdog.freq)
975 return true;
976
977 return false;
978 }
979
980 static bool
981 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
982 {
983 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
984 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
985
986 return n1->ifindex == n2->ifindex;
987 }
988
989 static void
990 instance_netdev_update(struct blobmsg_list_node *l)
991 {
992 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
993
994 n->ifindex = if_nametoindex(n->node.avl.key);
995 }
996
997 static bool
998 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
999 {
1000 struct instance_file *f1 = container_of(l1, struct instance_file, node);
1001 struct instance_file *f2 = container_of(l2, struct instance_file, node);
1002
1003 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
1004 }
1005
1006 static void
1007 instance_file_update(struct blobmsg_list_node *l)
1008 {
1009 struct instance_file *f = container_of(l, struct instance_file, node);
1010 md5_ctx_t md5;
1011 char buf[256];
1012 int len, fd;
1013
1014 memset(f->md5, 0, sizeof(f->md5));
1015
1016 fd = open(l->avl.key, O_RDONLY);
1017 if (fd < 0)
1018 return;
1019
1020 md5_begin(&md5);
1021 do {
1022 len = read(fd, buf, sizeof(buf));
1023 if (len < 0) {
1024 if (errno == EINTR)
1025 continue;
1026
1027 break;
1028 }
1029 if (!len)
1030 break;
1031
1032 md5_hash(buf, len, &md5);
1033 } while(1);
1034
1035 md5_end(f->md5, &md5);
1036 close(fd);
1037 }
1038
1039 static void
1040 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
1041 {
1042 if (!cur)
1043 return;
1044
1045 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
1046 }
1047
1048 static bool
1049 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
1050 {
1051 struct blobmsg_list_node *node;
1052
1053 if (!cur)
1054 return true;
1055
1056 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
1057 return false;
1058
1059 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
1060 if (cb) {
1061 blobmsg_list_for_each(l, node)
1062 cb(node);
1063 }
1064 return true;
1065 }
1066
1067 static int
1068 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
1069 {
1070 struct blob_attr *tb[__JAIL_ATTR_MAX];
1071 struct jail *jail = &in->jail;
1072
1073 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
1074 blobmsg_data(attr), blobmsg_data_len(attr));
1075
1076 jail->argc = 2;
1077
1078 if (tb[JAIL_ATTR_REQUIREJAIL] && blobmsg_get_bool(tb[JAIL_ATTR_REQUIREJAIL])) {
1079 in->require_jail = true;
1080 jail->argc++;
1081 }
1082 if (tb[JAIL_ATTR_IMMEDIATELY] && blobmsg_get_bool(tb[JAIL_ATTR_IMMEDIATELY])) {
1083 in->immediately = true;
1084 jail->argc++;
1085 }
1086 if (tb[JAIL_ATTR_NAME]) {
1087 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
1088 jail->argc += 2;
1089 }
1090 if (tb[JAIL_ATTR_HOSTNAME]) {
1091 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
1092 jail->argc += 2;
1093 }
1094 if (tb[JAIL_ATTR_PROCFS] && blobmsg_get_bool(tb[JAIL_ATTR_PROCFS])) {
1095 jail->procfs = true;
1096 jail->argc++;
1097 }
1098 if (tb[JAIL_ATTR_SYSFS] && blobmsg_get_bool(tb[JAIL_ATTR_SYSFS])) {
1099 jail->sysfs = true;
1100 jail->argc++;
1101 }
1102 if (tb[JAIL_ATTR_UBUS] && blobmsg_get_bool(tb[JAIL_ATTR_UBUS])) {
1103 jail->ubus = true;
1104 jail->argc++;
1105 }
1106 if (tb[JAIL_ATTR_LOG] && blobmsg_get_bool(tb[JAIL_ATTR_LOG])) {
1107 jail->log = true;
1108 jail->argc++;
1109 }
1110 if (tb[JAIL_ATTR_RONLY] && blobmsg_get_bool(tb[JAIL_ATTR_RONLY])) {
1111 jail->ronly = true;
1112 jail->argc++;
1113 }
1114 if (tb[JAIL_ATTR_NETNS] && blobmsg_get_bool(tb[JAIL_ATTR_NETNS])) {
1115 jail->netns = true;
1116 jail->argc++;
1117 }
1118 if (tb[JAIL_ATTR_USERNS] && blobmsg_get_bool(tb[JAIL_ATTR_USERNS])) {
1119 jail->userns = true;
1120 jail->argc++;
1121 }
1122 if (tb[JAIL_ATTR_CGROUPSNS] && blobmsg_get_bool(tb[JAIL_ATTR_CGROUPSNS])) {
1123 jail->cgroupsns = true;
1124 jail->argc++;
1125 }
1126 if (tb[JAIL_ATTR_CONSOLE] && blobmsg_get_bool(tb[JAIL_ATTR_CONSOLE])) {
1127 jail->console = true;
1128 jail->argc++;
1129 }
1130 if (tb[JAIL_ATTR_PIDFILE]) {
1131 jail->pidfile = strdup(blobmsg_get_string(tb[JAIL_ATTR_PIDFILE]));
1132 jail->argc += 2;
1133 }
1134
1135 if (tb[JAIL_ATTR_MOUNT]) {
1136 struct blob_attr *cur;
1137 int rem;
1138
1139 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
1140 jail->argc += 2;
1141 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
1142 }
1143 if (in->seccomp)
1144 jail->argc += 2;
1145
1146 if (in->capabilities)
1147 jail->argc += 2;
1148
1149 if (in->user)
1150 jail->argc += 2;
1151
1152 if (in->group)
1153 jail->argc += 2;
1154
1155 if (in->extroot)
1156 jail->argc += 2;
1157
1158 if (in->overlaydir)
1159 jail->argc += 2;
1160
1161 if (in->tmpoverlaysize)
1162 jail->argc += 2;
1163
1164 if (in->no_new_privs)
1165 jail->argc++;
1166
1167 if (in->bundle)
1168 jail->argc += 2;
1169
1170 return true;
1171 }
1172
1173 static bool
1174 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
1175 {
1176 struct blob_attr *cur, *cur2;
1177 bool ret = false;
1178 int rem;
1179
1180 cur = tb[INSTANCE_ATTR_COMMAND];
1181 if (!cur) {
1182 in->command = NULL;
1183 return true;
1184 }
1185
1186 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
1187 return false;
1188
1189 blobmsg_for_each_attr(cur2, cur, rem) {
1190 ret = true;
1191 break;
1192 }
1193
1194 in->command = cur;
1195 return ret;
1196 }
1197
1198 static bool
1199 instance_config_parse(struct service_instance *in)
1200 {
1201 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
1202 struct blob_attr *cur, *cur2;
1203 struct stat s;
1204 int rem, r;
1205
1206 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
1207 blobmsg_data(in->config), blobmsg_data_len(in->config));
1208
1209 if (!tb[INSTANCE_ATTR_BUNDLE] && !instance_config_parse_command(in, tb))
1210 return false;
1211
1212 if (tb[INSTANCE_ATTR_TERMTIMEOUT])
1213 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
1214 if (tb[INSTANCE_ATTR_RESPAWN]) {
1215 int i = 0;
1216 uint32_t vals[3] = { 3600, 5, 5};
1217
1218 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
1219 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
1220 continue;
1221 vals[i] = atoi(blobmsg_get_string(cur2));
1222 i++;
1223 }
1224 in->respawn = true;
1225 in->respawn_count = 0;
1226 in->respawn_threshold = vals[0];
1227 in->respawn_timeout = vals[1];
1228 in->respawn_retry = vals[2];
1229 }
1230 if (tb[INSTANCE_ATTR_TRIGGER]) {
1231 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
1232 trigger_add(in->trigger, in);
1233 }
1234
1235 if (tb[INSTANCE_ATTR_WATCH]) {
1236 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
1237 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
1238 continue;
1239 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
1240 watch_add(blobmsg_get_string(cur2), in);
1241 }
1242 }
1243
1244 if ((cur = tb[INSTANCE_ATTR_NICE])) {
1245 in->nice = (int8_t) blobmsg_get_u32(cur);
1246 if (in->nice < -20 || in->nice > 20)
1247 return false;
1248 }
1249
1250 if (tb[INSTANCE_ATTR_USER]) {
1251 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
1252 struct passwd *p = getpwnam(user);
1253 if (p) {
1254 in->user = strdup(user);
1255 in->uid = p->pw_uid;
1256 in->gr_gid = in->pw_gid = p->pw_gid;
1257 }
1258 }
1259
1260 if (tb[INSTANCE_ATTR_GROUP]) {
1261 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
1262 struct group *p = getgrnam(group);
1263 if (p) {
1264 in->group = strdup(group);
1265 in->gr_gid = p->gr_gid;
1266 }
1267 }
1268
1269 if (tb[INSTANCE_ATTR_TRACE])
1270 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
1271
1272 if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
1273 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
1274
1275 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
1276 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
1277
1278 if (tb[INSTANCE_ATTR_CAPABILITIES])
1279 in->capabilities = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_CAPABILITIES]));
1280
1281 if (tb[INSTANCE_ATTR_EXTROOT])
1282 in->extroot = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_EXTROOT]));
1283
1284 if (tb[INSTANCE_ATTR_OVERLAYDIR])
1285 in->overlaydir = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_OVERLAYDIR]));
1286
1287 if (tb[INSTANCE_ATTR_TMPOVERLAYSIZE])
1288 in->tmpoverlaysize = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_TMPOVERLAYSIZE]));
1289
1290 if (tb[INSTANCE_ATTR_BUNDLE])
1291 in->bundle = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_BUNDLE]));
1292
1293 if (tb[INSTANCE_ATTR_PIDFILE]) {
1294 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
1295 if (pidfile)
1296 in->pidfile = strdup(pidfile);
1297 }
1298
1299 if (tb[INSTANCE_ATTR_RELOADSIG])
1300 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1301
1302 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1303 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1304
1305 if (in->has_jail) {
1306 r = stat(UJAIL_BIN_PATH, &s);
1307 if (r < 0) {
1308 if (in->require_jail) {
1309 ERROR("Cannot jail service %s::%s. %s: %m (%d)\n",
1310 in->srv->name, in->name, UJAIL_BIN_PATH, r);
1311 return false;
1312 }
1313 DEBUG(2, "unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
1314 in->has_jail = false;
1315 }
1316 }
1317
1318 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1319 in->_stdout.fd.fd = -1;
1320
1321 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1322 in->_stderr.fd.fd = -1;
1323
1324 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1325
1326 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1327 return false;
1328
1329 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1330 return false;
1331
1332 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1333 return false;
1334
1335 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1336 return false;
1337
1338 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1339 return false;
1340
1341 if (tb[INSTANCE_ATTR_FACILITY]) {
1342 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1343 if (facility != -1) {
1344 in->syslog_facility = facility;
1345 DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1346 } else
1347 DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1348 }
1349
1350 if (tb[INSTANCE_ATTR_WATCHDOG]) {
1351 int i = 0;
1352 uint32_t vals[2] = { 0, 30 };
1353
1354 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCHDOG], rem) {
1355 if (i >= 2)
1356 break;
1357
1358 vals[i] = atoi(blobmsg_get_string(cur2));
1359 i++;
1360 }
1361
1362 if (vals[0] >= 0 && vals[0] < __INSTANCE_WATCHDOG_MODE_MAX) {
1363 in->watchdog.mode = vals[0];
1364 DEBUG(3, "setting watchdog mode (%d)\n", vals[0]);
1365 } else {
1366 in->watchdog.mode = 0;
1367 DEBUG(3, "unknown watchdog mode (%d) given, using default (0)\n", vals[0]);
1368 }
1369
1370 if (vals[1] > 0) {
1371 in->watchdog.freq = vals[1];
1372 DEBUG(3, "setting watchdog timeout (%d)\n", vals[0]);
1373 } else {
1374 in->watchdog.freq = 30;
1375 DEBUG(3, "invalid watchdog timeout (%d) given, using default (30)\n", vals[1]);
1376 }
1377 }
1378
1379 return true;
1380 }
1381
1382 static void
1383 instance_config_cleanup(struct service_instance *in)
1384 {
1385 blobmsg_list_free(&in->env);
1386 blobmsg_list_free(&in->data);
1387 blobmsg_list_free(&in->netdev);
1388 blobmsg_list_free(&in->file);
1389 blobmsg_list_free(&in->limits);
1390 blobmsg_list_free(&in->errors);
1391 blobmsg_list_free(&in->jail.mount);
1392 }
1393
1394 static void
1395 instance_config_move_strdup(char **dst, char *src)
1396 {
1397 if (*dst) {
1398 free(*dst);
1399 *dst = NULL;
1400 }
1401
1402 if (!src)
1403 return;
1404
1405 *dst = strdup(src);
1406 }
1407
1408 static void
1409 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1410 {
1411 instance_config_cleanup(in);
1412 blobmsg_list_move(&in->env, &in_src->env);
1413 blobmsg_list_move(&in->data, &in_src->data);
1414 blobmsg_list_move(&in->netdev, &in_src->netdev);
1415 blobmsg_list_move(&in->file, &in_src->file);
1416 blobmsg_list_move(&in->limits, &in_src->limits);
1417 blobmsg_list_move(&in->errors, &in_src->errors);
1418 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1419 in->trigger = in_src->trigger;
1420 in->command = in_src->command;
1421 in->respawn = in_src->respawn;
1422 in->respawn_retry = in_src->respawn_retry;
1423 in->respawn_threshold = in_src->respawn_threshold;
1424 in->respawn_timeout = in_src->respawn_timeout;
1425 in->reload_signal = in_src->reload_signal;
1426 in->term_timeout = in_src->term_timeout;
1427 in->watchdog.mode = in_src->watchdog.mode;
1428 in->watchdog.freq = in_src->watchdog.freq;
1429 in->watchdog.timeout = in_src->watchdog.timeout;
1430 in->name = in_src->name;
1431 in->nice = in_src->nice;
1432 in->trace = in_src->trace;
1433 in->node.avl.key = in_src->node.avl.key;
1434 in->syslog_facility = in_src->syslog_facility;
1435 in->require_jail = in_src->require_jail;
1436 in->no_new_privs = in_src->no_new_privs;
1437 in->immediately = in_src->immediately;
1438 in->uid = in_src->uid;
1439 in->pw_gid = in_src->pw_gid;
1440 in->gr_gid = in_src->gr_gid;
1441
1442 in->has_jail = in_src->has_jail;
1443 in->jail.procfs = in_src->jail.procfs;
1444 in->jail.sysfs = in_src->jail.sysfs;
1445 in->jail.ubus = in_src->jail.ubus;
1446 in->jail.log = in_src->jail.log;
1447 in->jail.ronly = in_src->jail.ronly;
1448 in->jail.netns = in_src->jail.netns;
1449 in->jail.cgroupsns = in_src->jail.cgroupsns;
1450 in->jail.console = in_src->jail.console;
1451 in->jail.argc = in_src->jail.argc;
1452
1453 instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1454 instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1455 instance_config_move_strdup(&in->capabilities, in_src->capabilities);
1456 instance_config_move_strdup(&in->bundle, in_src->bundle);
1457 instance_config_move_strdup(&in->extroot, in_src->extroot);
1458 instance_config_move_strdup(&in->overlaydir, in_src->overlaydir);
1459 instance_config_move_strdup(&in->tmpoverlaysize, in_src->tmpoverlaysize);
1460 instance_config_move_strdup(&in->user, in_src->user);
1461 instance_config_move_strdup(&in->group, in_src->group);
1462 instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1463 instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1464 instance_config_move_strdup(&in->jail.pidfile, in_src->jail.pidfile);
1465
1466 free(in->config);
1467 in->config = in_src->config;
1468 in_src->config = NULL;
1469 }
1470
1471 void
1472 instance_update(struct service_instance *in, struct service_instance *in_new)
1473 {
1474 bool changed = instance_config_changed(in, in_new);
1475 bool running = in->proc.pending;
1476 bool stopping = in->halt;
1477
1478 if (!running || stopping) {
1479 instance_config_move(in, in_new);
1480 instance_start(in);
1481 } else {
1482 if (changed)
1483 instance_restart(in);
1484 instance_config_move(in, in_new);
1485 /* restart happens in the child callback handler */
1486 }
1487 }
1488
1489 void
1490 instance_free(struct service_instance *in)
1491 {
1492 instance_free_stdio(in);
1493 uloop_process_delete(&in->proc);
1494 uloop_timeout_cancel(&in->timeout);
1495 uloop_timeout_cancel(&in->watchdog.timeout);
1496 trigger_del(in);
1497 watch_del(in);
1498 instance_config_cleanup(in);
1499 free(in->config);
1500 free(in->user);
1501 free(in->group);
1502 free(in->extroot);
1503 free(in->overlaydir);
1504 free(in->tmpoverlaysize);
1505 free(in->bundle);
1506 free(in->jail.name);
1507 free(in->jail.hostname);
1508 free(in->jail.pidfile);
1509 free(in->seccomp);
1510 free(in->capabilities);
1511 free(in->pidfile);
1512 free(in);
1513 }
1514
1515 void
1516 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1517 {
1518 config = blob_memdup(config);
1519 in->srv = s;
1520 in->name = blobmsg_name(config);
1521 in->config = config;
1522 in->timeout.cb = instance_timeout;
1523 in->proc.cb = instance_exit;
1524 in->term_timeout = 5;
1525 in->syslog_facility = LOG_DAEMON;
1526 in->exit_code = 0;
1527 in->require_jail = false;
1528 in->immediately = false;
1529
1530 in->_stdout.fd.fd = -2;
1531 in->_stdout.stream.string_data = true;
1532 in->_stdout.stream.notify_read = instance_stdout;
1533
1534 in->_stderr.fd.fd = -2;
1535 in->_stderr.stream.string_data = true;
1536 in->_stderr.stream.notify_read = instance_stderr;
1537
1538 in->console.fd.fd = -2;
1539 in->console.stream.string_data = true;
1540 in->console.stream.notify_read = instance_console;
1541
1542 in->console_client.fd.fd = -2;
1543 in->console_client.stream.string_data = true;
1544 in->console_client.stream.notify_read = instance_console_client;
1545
1546 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1547 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1548 blobmsg_list_simple_init(&in->env);
1549 blobmsg_list_simple_init(&in->data);
1550 blobmsg_list_simple_init(&in->limits);
1551 blobmsg_list_simple_init(&in->errors);
1552 blobmsg_list_simple_init(&in->jail.mount);
1553
1554 in->watchdog.timeout.cb = instance_watchdog;
1555
1556 in->valid = instance_config_parse(in);
1557 }
1558
1559 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1560 {
1561 void *i;
1562
1563 if (!in->valid)
1564 return;
1565
1566 i = blobmsg_open_table(b, in->name);
1567 blobmsg_add_u8(b, "running", in->proc.pending);
1568 if (in->proc.pending)
1569 blobmsg_add_u32(b, "pid", in->proc.pid);
1570 if (in->command)
1571 blobmsg_add_blob(b, in->command);
1572 if (in->bundle)
1573 blobmsg_add_string(b, "bundle", in->bundle);
1574 blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1575 if (!in->proc.pending)
1576 blobmsg_add_u32(b, "exit_code", in->exit_code);
1577
1578 if (!avl_is_empty(&in->errors.avl)) {
1579 struct blobmsg_list_node *var;
1580 void *e = blobmsg_open_array(b, "errors");
1581 blobmsg_list_for_each(&in->errors, var)
1582 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1583 blobmsg_close_table(b, e);
1584 }
1585
1586 if (!avl_is_empty(&in->env.avl)) {
1587 struct blobmsg_list_node *var;
1588 void *e = blobmsg_open_table(b, "env");
1589 blobmsg_list_for_each(&in->env, var)
1590 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1591 blobmsg_close_table(b, e);
1592 }
1593
1594 if (!avl_is_empty(&in->data.avl)) {
1595 struct blobmsg_list_node *var;
1596 void *e = blobmsg_open_table(b, "data");
1597 blobmsg_list_for_each(&in->data, var)
1598 blobmsg_add_blob(b, var->data);
1599 blobmsg_close_table(b, e);
1600 }
1601
1602 if (!avl_is_empty(&in->limits.avl)) {
1603 struct blobmsg_list_node *var;
1604 void *e = blobmsg_open_table(b, "limits");
1605 blobmsg_list_for_each(&in->limits, var)
1606 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1607 blobmsg_close_table(b, e);
1608 }
1609
1610 if (in->reload_signal)
1611 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1612
1613 if (in->respawn) {
1614 void *r = blobmsg_open_table(b, "respawn");
1615 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1616 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1617 blobmsg_add_u32(b, "retry", in->respawn_retry);
1618 blobmsg_close_table(b, r);
1619 }
1620
1621 if (in->trace)
1622 blobmsg_add_u8(b, "trace", true);
1623
1624 if (in->no_new_privs)
1625 blobmsg_add_u8(b, "no_new_privs", true);
1626
1627 if (in->seccomp)
1628 blobmsg_add_string(b, "seccomp", in->seccomp);
1629
1630 if (in->capabilities)
1631 blobmsg_add_string(b, "capabilities", in->capabilities);
1632
1633 if (in->pidfile)
1634 blobmsg_add_string(b, "pidfile", in->pidfile);
1635
1636 if (in->user)
1637 blobmsg_add_string(b, "user", in->user);
1638
1639 if (in->group)
1640 blobmsg_add_string(b, "group", in->group);
1641
1642 if (in->has_jail) {
1643 void *r = blobmsg_open_table(b, "jail");
1644 if (in->jail.name)
1645 blobmsg_add_string(b, "name", in->jail.name);
1646 if (!in->bundle) {
1647 if (in->jail.hostname)
1648 blobmsg_add_string(b, "hostname", in->jail.hostname);
1649
1650 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1651 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1652 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1653 blobmsg_add_u8(b, "log", in->jail.log);
1654 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1655 blobmsg_add_u8(b, "netns", in->jail.netns);
1656 blobmsg_add_u8(b, "userns", in->jail.userns);
1657 blobmsg_add_u8(b, "cgroupsns", in->jail.cgroupsns);
1658 } else {
1659 if (in->jail.pidfile)
1660 blobmsg_add_string(b, "pidfile", in->jail.pidfile);
1661
1662 blobmsg_add_u8(b, "immediately", in->immediately);
1663 }
1664 blobmsg_add_u8(b, "console", (in->console.fd.fd > -1));
1665 blobmsg_close_table(b, r);
1666 if (!avl_is_empty(&in->jail.mount.avl)) {
1667 struct blobmsg_list_node *var;
1668 void *e = blobmsg_open_table(b, "mount");
1669 blobmsg_list_for_each(&in->jail.mount, var)
1670 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1671 blobmsg_close_table(b, e);
1672 }
1673 }
1674
1675 if (in->extroot)
1676 blobmsg_add_string(b, "extroot", in->extroot);
1677 if (in->overlaydir)
1678 blobmsg_add_string(b, "overlaydir", in->overlaydir);
1679 if (in->tmpoverlaysize)
1680 blobmsg_add_string(b, "tmpoverlaysize", in->tmpoverlaysize);
1681
1682 if (verbose && in->trigger)
1683 blobmsg_add_blob(b, in->trigger);
1684
1685 if (in->watchdog.mode != INSTANCE_WATCHDOG_MODE_DISABLED) {
1686 void *r = blobmsg_open_table(b, "watchdog");
1687 blobmsg_add_u32(b, "mode", in->watchdog.mode);
1688 blobmsg_add_u32(b, "timeout", in->watchdog.freq);
1689 blobmsg_close_table(b, r);
1690 }
1691
1692 blobmsg_close_table(b, i);
1693 }