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