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