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