instance, ujail: wire remount / read only option (-o)
[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 <net/if.h>
21 #include <unistd.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <fcntl.h>
25 #include <pwd.h>
26 #include <libgen.h>
27 #include <unistd.h>
28
29 #include <libubox/md5.h>
30
31 #include "../procd.h"
32
33 #include "service.h"
34 #include "instance.h"
35
36
37 enum {
38 INSTANCE_ATTR_COMMAND,
39 INSTANCE_ATTR_ENV,
40 INSTANCE_ATTR_DATA,
41 INSTANCE_ATTR_NETDEV,
42 INSTANCE_ATTR_FILE,
43 INSTANCE_ATTR_TRIGGER,
44 INSTANCE_ATTR_RESPAWN,
45 INSTANCE_ATTR_NICE,
46 INSTANCE_ATTR_LIMITS,
47 INSTANCE_ATTR_WATCH,
48 INSTANCE_ATTR_ERROR,
49 INSTANCE_ATTR_USER,
50 INSTANCE_ATTR_STDOUT,
51 INSTANCE_ATTR_STDERR,
52 INSTANCE_ATTR_JAIL,
53 INSTANCE_ATTR_TRACE,
54 INSTANCE_ATTR_SECCOMP,
55 __INSTANCE_ATTR_MAX
56 };
57
58 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
59 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
60 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
61 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
62 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
63 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
64 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
65 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
66 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
67 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
68 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
69 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
70 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
71 [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
72 [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
73 [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
74 [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
75 [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
76 };
77
78 enum {
79 JAIL_ATTR_NAME,
80 JAIL_ATTR_PROCFS,
81 JAIL_ATTR_SYSFS,
82 JAIL_ATTR_UBUS,
83 JAIL_ATTR_LOG,
84 JAIL_ATTR_RONLY,
85 JAIL_ATTR_MOUNT,
86 __JAIL_ATTR_MAX,
87 };
88
89 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
90 [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
91 [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
92 [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
93 [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
94 [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
95 [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
96 [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
97 };
98
99 struct instance_netdev {
100 struct blobmsg_list_node node;
101 int ifindex;
102 };
103
104 struct instance_file {
105 struct blobmsg_list_node node;
106 uint32_t md5[4];
107 };
108
109 struct rlimit_name {
110 const char *name;
111 int resource;
112 };
113
114 static const struct rlimit_name rlimit_names[] = {
115 { "as", RLIMIT_AS },
116 { "core", RLIMIT_CORE },
117 { "cpu", RLIMIT_CPU },
118 { "data", RLIMIT_DATA },
119 { "fsize", RLIMIT_FSIZE },
120 { "memlock", RLIMIT_MEMLOCK },
121 { "msgqueue", RLIMIT_MSGQUEUE },
122 { "nice", RLIMIT_NICE },
123 { "nofile", RLIMIT_NOFILE },
124 { "nproc", RLIMIT_NPROC },
125 { "rss", RLIMIT_RSS },
126 { "rtprio", RLIMIT_RTPRIO },
127 { "sigpending", RLIMIT_SIGPENDING },
128 { "stack", RLIMIT_STACK },
129 { NULL, 0 }
130 };
131
132 static char trace[] = "/sbin/utrace";
133
134 static void closefd(int fd)
135 {
136 if (fd > STDERR_FILENO)
137 close(fd);
138 }
139
140 static void
141 instance_limits(const char *limit, const char *value)
142 {
143 int i;
144 struct rlimit rlim;
145 unsigned long cur, max;
146
147 for (i = 0; rlimit_names[i].name != NULL; i++) {
148 if (strcmp(rlimit_names[i].name, limit))
149 continue;
150 if (!strcmp(value, "unlimited")) {
151 rlim.rlim_cur = RLIM_INFINITY;
152 rlim.rlim_max = RLIM_INFINITY;
153 } else {
154 if (getrlimit(rlimit_names[i].resource, &rlim))
155 return;
156
157 cur = rlim.rlim_cur;
158 max = rlim.rlim_max;
159
160 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
161 return;
162
163 rlim.rlim_cur = cur;
164 rlim.rlim_max = max;
165 }
166
167 setrlimit(rlimit_names[i].resource, &rlim);
168 return;
169 }
170 }
171
172 static inline int
173 jail_run(struct service_instance *in, char **argv)
174 {
175 struct blobmsg_list_node *var;
176 struct jail *jail = &in->jail;
177 int argc = 0;
178
179 argv[argc++] = "/sbin/ujail";
180
181 if (jail->name) {
182 argv[argc++] = "-n";
183 argv[argc++] = jail->name;
184 }
185
186 if (in->seccomp) {
187 argv[argc++] = "-S";
188 argv[argc++] = in->seccomp;
189 }
190
191 if (jail->procfs)
192 argv[argc++] = "-p";
193
194 if (jail->sysfs)
195 argv[argc++] = "-s";
196
197 if (jail->ubus)
198 argv[argc++] = "-u";
199
200 if (jail->log)
201 argv[argc++] = "-l";
202
203 if (jail->ronly)
204 argv[argc++] = "-o";
205
206 blobmsg_list_for_each(&jail->mount, var) {
207 const char *type = blobmsg_data(var->data);
208
209 if (*type == '1')
210 argv[argc++] = "-w";
211 else
212 argv[argc++] = "-r";
213 argv[argc++] = (char *) blobmsg_name(var->data);
214 }
215
216 argv[argc++] = "--";
217
218 return argc;
219 }
220
221 static void
222 instance_run(struct service_instance *in, int _stdout, int _stderr)
223 {
224 struct blobmsg_list_node *var;
225 struct blob_attr *cur;
226 char **argv;
227 char *ld_preload;
228 int argc = 1; /* NULL terminated */
229 int rem, _stdin;
230 bool seccomp = !in->trace && !in->has_jail && in->seccomp;
231 bool setlbf = _stdout >= 0;
232
233 if (in->nice)
234 setpriority(PRIO_PROCESS, 0, in->nice);
235
236 blobmsg_for_each_attr(cur, in->command, rem)
237 argc++;
238
239 blobmsg_list_for_each(&in->env, var)
240 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
241
242 if (seccomp)
243 setenv("SECCOMP_FILE", in->seccomp, 1);
244
245 if ((seccomp || setlbf) && asprintf(&ld_preload, "LD_PRELOAD=%s%s%s",
246 seccomp ? "/lib/libpreload-seccomp.so" : "",
247 seccomp && setlbf ? ":" : "",
248 setlbf ? "/lib/libsetlbf.so" : "") > 0)
249 putenv(ld_preload);
250
251 blobmsg_list_for_each(&in->limits, var)
252 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
253
254 if (in->trace)
255 argc += 1;
256
257 argv = alloca(sizeof(char *) * (argc + in->jail.argc));
258 argc = 0;
259
260 if (in->trace)
261 argv[argc++] = trace;
262
263 if (in->has_jail)
264 argc = jail_run(in, argv);
265
266 blobmsg_for_each_attr(cur, in->command, rem)
267 argv[argc++] = blobmsg_data(cur);
268
269 argv[argc] = NULL;
270
271 _stdin = open("/dev/null", O_RDONLY);
272
273 if (_stdout == -1)
274 _stdout = open("/dev/null", O_WRONLY);
275
276 if (_stderr == -1)
277 _stderr = open("/dev/null", O_WRONLY);
278
279 if (_stdin > -1) {
280 dup2(_stdin, STDIN_FILENO);
281 closefd(_stdin);
282 }
283 if (_stdout > -1) {
284 dup2(_stdout, STDOUT_FILENO);
285 closefd(_stdout);
286 }
287 if (_stderr > -1) {
288 dup2(_stderr, STDERR_FILENO);
289 closefd(_stderr);
290 }
291
292 if (in->gid && setgid(in->gid)) {
293 ERROR("failed to set group id %d: %d (%s)\n", in->gid, errno, strerror(errno));
294 exit(127);
295 }
296 if (in->uid && setuid(in->uid)) {
297 ERROR("failed to set user id %d: %d (%s)\n", in->uid, errno, strerror(errno));
298 exit(127);
299 }
300
301 execvp(argv[0], argv);
302 exit(127);
303 }
304
305 static void
306 instance_free_stdio(struct service_instance *in)
307 {
308 if (in->_stdout.fd.fd > -1) {
309 ustream_free(&in->_stdout.stream);
310 close(in->_stdout.fd.fd);
311 in->_stdout.fd.fd = -1;
312 }
313
314 if (in->_stderr.fd.fd > -1) {
315 ustream_free(&in->_stderr.stream);
316 close(in->_stderr.fd.fd);
317 in->_stderr.fd.fd = -1;
318 }
319 }
320
321 void
322 instance_start(struct service_instance *in)
323 {
324 int pid;
325 int opipe[2] = { -1, -1 };
326 int epipe[2] = { -1, -1 };
327
328 if (!avl_is_empty(&in->errors.avl)) {
329 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
330 return;
331 }
332
333 if (in->proc.pending)
334 return;
335
336 instance_free_stdio(in);
337 if (in->_stdout.fd.fd > -2) {
338 if (pipe(opipe)) {
339 ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
340 opipe[0] = opipe[1] = -1;
341 }
342 }
343
344 if (in->_stderr.fd.fd > -2) {
345 if (pipe(epipe)) {
346 ULOG_WARN("pipe() failed: %d (%s)\n", errno, strerror(errno));
347 epipe[0] = epipe[1] = -1;
348 }
349 }
350
351 in->restart = false;
352 in->halt = !in->respawn;
353
354 if (!in->valid)
355 return;
356
357 pid = fork();
358 if (pid < 0)
359 return;
360
361 if (!pid) {
362 uloop_done();
363 closefd(opipe[0]);
364 closefd(epipe[0]);
365 instance_run(in, opipe[1], epipe[1]);
366 return;
367 }
368
369 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
370 in->proc.pid = pid;
371 clock_gettime(CLOCK_MONOTONIC, &in->start);
372 uloop_process_add(&in->proc);
373
374 if (opipe[0] > -1) {
375 ustream_fd_init(&in->_stdout, opipe[0]);
376 closefd(opipe[1]);
377 }
378
379 if (epipe[0] > -1) {
380 ustream_fd_init(&in->_stderr, epipe[0]);
381 closefd(epipe[1]);
382 }
383
384 service_event("instance.start", in->srv->name, in->name);
385 }
386
387 static void
388 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
389 {
390 char *newline, *str, *arg0, ident[32];
391 int len;
392
393 arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
394 snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
395 ulog_open(ULOG_SYSLOG, LOG_DAEMON, ident);
396
397 do {
398 str = ustream_get_read_buf(s, NULL);
399 if (!str)
400 break;
401
402 newline = strchr(str, '\n');
403 if (!newline)
404 break;
405
406 *newline = 0;
407 ulog(prio, "%s\n", str);
408
409 len = newline + 1 - str;
410 ustream_consume(s, len);
411 } while (1);
412
413 ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
414 }
415
416 static void
417 instance_stdout(struct ustream *s, int bytes)
418 {
419 instance_stdio(s, LOG_INFO,
420 container_of(s, struct service_instance, _stdout.stream));
421 }
422
423 static void
424 instance_stderr(struct ustream *s, int bytes)
425 {
426 instance_stdio(s, LOG_ERR,
427 container_of(s, struct service_instance, _stderr.stream));
428 }
429
430 static void
431 instance_timeout(struct uloop_timeout *t)
432 {
433 struct service_instance *in;
434
435 in = container_of(t, struct service_instance, timeout);
436
437 if (!in->halt && (in->restart || in->respawn))
438 instance_start(in);
439 }
440
441 static void
442 instance_exit(struct uloop_process *p, int ret)
443 {
444 struct service_instance *in;
445 struct timespec tp;
446 long runtime;
447
448 in = container_of(p, struct service_instance, proc);
449
450 clock_gettime(CLOCK_MONOTONIC, &tp);
451 runtime = tp.tv_sec - in->start.tv_sec;
452
453 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
454 if (upgrade_running)
455 return;
456
457 uloop_timeout_cancel(&in->timeout);
458 if (in->halt) {
459 /* no action */
460 } else if (in->restart) {
461 instance_start(in);
462 } else if (in->respawn) {
463 if (runtime < in->respawn_threshold)
464 in->respawn_count++;
465 else
466 in->respawn_count = 0;
467 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
468 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
469 in->srv->name, in->name, in->respawn_count, runtime);
470 in->restart = in->respawn = 0;
471 in->halt = 1;
472 } else {
473 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
474 }
475 }
476 service_event("instance.stop", in->srv->name, in->name);
477 }
478
479 void
480 instance_stop(struct service_instance *in)
481 {
482 if (!in->proc.pending)
483 return;
484 in->halt = true;
485 in->restart = in->respawn = false;
486 kill(in->proc.pid, SIGTERM);
487 }
488
489 static void
490 instance_restart(struct service_instance *in)
491 {
492 if (!in->proc.pending)
493 return;
494 in->halt = false;
495 in->restart = true;
496 kill(in->proc.pid, SIGTERM);
497 }
498
499 static bool
500 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
501 {
502 if (!in->valid)
503 return true;
504
505 if (!blob_attr_equal(in->command, in_new->command))
506 return true;
507
508 if (!blobmsg_list_equal(&in->env, &in_new->env))
509 return true;
510
511 if (!blobmsg_list_equal(&in->data, &in_new->data))
512 return true;
513
514 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
515 return true;
516
517 if (!blobmsg_list_equal(&in->file, &in_new->file))
518 return true;
519
520 if (in->nice != in_new->nice)
521 return true;
522
523 if (in->uid != in_new->uid)
524 return true;
525
526 if (in->gid != in_new->gid)
527 return true;
528
529 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
530 return true;
531
532 if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
533 return true;
534
535 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
536 return true;
537
538 return false;
539 }
540
541 static bool
542 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
543 {
544 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
545 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
546
547 return n1->ifindex == n2->ifindex;
548 }
549
550 static void
551 instance_netdev_update(struct blobmsg_list_node *l)
552 {
553 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
554
555 n->ifindex = if_nametoindex(n->node.avl.key);
556 }
557
558 static bool
559 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
560 {
561 struct instance_file *f1 = container_of(l1, struct instance_file, node);
562 struct instance_file *f2 = container_of(l2, struct instance_file, node);
563
564 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
565 }
566
567 static void
568 instance_file_update(struct blobmsg_list_node *l)
569 {
570 struct instance_file *f = container_of(l, struct instance_file, node);
571 md5_ctx_t md5;
572 char buf[256];
573 int len, fd;
574
575 memset(f->md5, 0, sizeof(f->md5));
576
577 fd = open(l->avl.key, O_RDONLY);
578 if (fd < 0)
579 return;
580
581 md5_begin(&md5);
582 do {
583 len = read(fd, buf, sizeof(buf));
584 if (len < 0) {
585 if (errno == EINTR)
586 continue;
587
588 break;
589 }
590 if (!len)
591 break;
592
593 md5_hash(buf, len, &md5);
594 } while(1);
595
596 md5_end(f->md5, &md5);
597 close(fd);
598 }
599
600 static void
601 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
602 {
603 if (!cur)
604 return;
605
606 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
607 }
608
609 static bool
610 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
611 {
612 struct blobmsg_list_node *node;
613
614 if (!cur)
615 return true;
616
617 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
618 return false;
619
620 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
621 if (cb) {
622 blobmsg_list_for_each(l, node)
623 cb(node);
624 }
625 return true;
626 }
627
628 static int
629 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
630 {
631 struct blob_attr *tb[__JAIL_ATTR_MAX];
632 struct jail *jail = &in->jail;
633 struct stat s;
634
635 if (stat("/sbin/ujail", &s))
636 return 0;
637
638 blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
639 blobmsg_data(attr), blobmsg_data_len(attr));
640
641 jail->argc = 2;
642
643 if (tb[JAIL_ATTR_NAME]) {
644 jail->name = blobmsg_get_string(tb[JAIL_ATTR_NAME]);
645 jail->argc += 2;
646 }
647 if (tb[JAIL_ATTR_PROCFS]) {
648 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
649 jail->argc++;
650 }
651 if (tb[JAIL_ATTR_SYSFS]) {
652 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
653 jail->argc++;
654 }
655 if (tb[JAIL_ATTR_UBUS]) {
656 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
657 jail->argc++;
658 }
659 if (tb[JAIL_ATTR_LOG]) {
660 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
661 jail->argc++;
662 }
663 if (tb[JAIL_ATTR_RONLY]) {
664 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
665 jail->argc++;
666 }
667 if (tb[JAIL_ATTR_MOUNT]) {
668 struct blob_attr *cur;
669 int rem;
670
671 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
672 jail->argc += 2;
673 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
674 }
675 if (in->seccomp)
676 jail->argc += 2;
677
678 return 1;
679 }
680
681 static bool
682 instance_config_parse(struct service_instance *in)
683 {
684 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
685 struct blob_attr *cur, *cur2;
686 int argc = 0;
687 int rem;
688
689 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
690 blobmsg_data(in->config), blobmsg_data_len(in->config));
691
692 cur = tb[INSTANCE_ATTR_COMMAND];
693 if (!cur)
694 return false;
695
696 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
697 return false;
698
699 blobmsg_for_each_attr(cur2, cur, rem) {
700 argc++;
701 break;
702 }
703 if (!argc)
704 return false;
705
706 in->command = cur;
707
708 if (tb[INSTANCE_ATTR_RESPAWN]) {
709 int i = 0;
710 uint32_t vals[3] = { 3600, 5, 5};
711
712 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
713 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
714 continue;
715 vals[i] = atoi(blobmsg_get_string(cur2));
716 i++;
717 }
718 in->respawn = true;
719 in->respawn_count = 0;
720 in->respawn_threshold = vals[0];
721 in->respawn_timeout = vals[1];
722 in->respawn_retry = vals[2];
723 }
724 if (tb[INSTANCE_ATTR_TRIGGER]) {
725 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
726 trigger_add(in->trigger, in);
727 }
728
729 if (tb[INSTANCE_ATTR_WATCH]) {
730 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
731 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
732 continue;
733 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
734 watch_add(blobmsg_get_string(cur2), in);
735 }
736 }
737
738 if ((cur = tb[INSTANCE_ATTR_NICE])) {
739 in->nice = (int8_t) blobmsg_get_u32(cur);
740 if (in->nice < -20 || in->nice > 20)
741 return false;
742 }
743
744 if (tb[INSTANCE_ATTR_USER]) {
745 struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
746 if (p) {
747 in->uid = p->pw_uid;
748 in->gid = p->pw_gid;
749 }
750 }
751
752 if (tb[INSTANCE_ATTR_TRACE])
753 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
754
755 if (!in->trace && tb[INSTANCE_ATTR_SECCOMP]) {
756 char *seccomp = blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]);
757 struct stat s;
758
759 if (stat(seccomp, &s))
760 ERROR("%s: not starting seccomp as %s is missing\n", in->name, seccomp);
761 else
762 in->seccomp = seccomp;
763 }
764 if (!in->trace && tb[INSTANCE_ATTR_JAIL])
765 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
766
767 if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
768 in->_stdout.fd.fd = -1;
769
770 if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
771 in->_stderr.fd.fd = -1;
772
773 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
774
775 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
776 return false;
777
778 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
779 return false;
780
781 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
782 return false;
783
784 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
785 return false;
786
787 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
788 return false;
789
790 return true;
791 }
792
793 static void
794 instance_config_cleanup(struct service_instance *in)
795 {
796 blobmsg_list_free(&in->env);
797 blobmsg_list_free(&in->data);
798 blobmsg_list_free(&in->netdev);
799 blobmsg_list_free(&in->file);
800 blobmsg_list_free(&in->limits);
801 blobmsg_list_free(&in->errors);
802 blobmsg_list_free(&in->jail.mount);
803 }
804
805 static void
806 instance_config_move(struct service_instance *in, struct service_instance *in_src)
807 {
808 instance_config_cleanup(in);
809 blobmsg_list_move(&in->env, &in_src->env);
810 blobmsg_list_move(&in->data, &in_src->data);
811 blobmsg_list_move(&in->netdev, &in_src->netdev);
812 blobmsg_list_move(&in->file, &in_src->file);
813 blobmsg_list_move(&in->limits, &in_src->limits);
814 blobmsg_list_move(&in->errors, &in_src->errors);
815 blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
816 in->trigger = in_src->trigger;
817 in->command = in_src->command;
818 in->name = in_src->name;
819 in->node.avl.key = in_src->node.avl.key;
820
821 free(in->config);
822 in->config = in_src->config;
823 in_src->config = NULL;
824 }
825
826 bool
827 instance_update(struct service_instance *in, struct service_instance *in_new)
828 {
829 bool changed = instance_config_changed(in, in_new);
830 bool running = in->proc.pending;
831
832 if (!changed && running)
833 return false;
834
835 if (!running) {
836 if (changed)
837 instance_config_move(in, in_new);
838 instance_start(in);
839 } else {
840 instance_restart(in);
841 instance_config_move(in, in_new);
842 /* restart happens in the child callback handler */
843 }
844 return true;
845 }
846
847 void
848 instance_free(struct service_instance *in)
849 {
850 instance_free_stdio(in);
851 uloop_process_delete(&in->proc);
852 uloop_timeout_cancel(&in->timeout);
853 trigger_del(in);
854 watch_del(in);
855 instance_config_cleanup(in);
856 free(in->config);
857 free(in);
858 }
859
860 void
861 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
862 {
863 config = blob_memdup(config);
864 in->srv = s;
865 in->name = blobmsg_name(config);
866 in->config = config;
867 in->timeout.cb = instance_timeout;
868 in->proc.cb = instance_exit;
869
870 in->_stdout.fd.fd = -2;
871 in->_stdout.stream.string_data = true;
872 in->_stdout.stream.notify_read = instance_stdout;
873
874 in->_stderr.fd.fd = -2;
875 in->_stderr.stream.string_data = true;
876 in->_stderr.stream.notify_read = instance_stderr;
877
878 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
879 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
880 blobmsg_list_simple_init(&in->env);
881 blobmsg_list_simple_init(&in->data);
882 blobmsg_list_simple_init(&in->limits);
883 blobmsg_list_simple_init(&in->errors);
884 blobmsg_list_simple_init(&in->jail.mount);
885 in->valid = instance_config_parse(in);
886 }
887
888 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
889 {
890 void *i;
891
892 if (!in->valid)
893 return;
894
895 i = blobmsg_open_table(b, in->name);
896 blobmsg_add_u8(b, "running", in->proc.pending);
897 if (in->proc.pending)
898 blobmsg_add_u32(b, "pid", in->proc.pid);
899 blobmsg_add_blob(b, in->command);
900
901 if (!avl_is_empty(&in->errors.avl)) {
902 struct blobmsg_list_node *var;
903 void *e = blobmsg_open_array(b, "errors");
904 blobmsg_list_for_each(&in->errors, var)
905 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
906 blobmsg_close_table(b, e);
907 }
908
909 if (!avl_is_empty(&in->env.avl)) {
910 struct blobmsg_list_node *var;
911 void *e = blobmsg_open_table(b, "env");
912 blobmsg_list_for_each(&in->env, var)
913 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
914 blobmsg_close_table(b, e);
915 }
916
917 if (!avl_is_empty(&in->data.avl)) {
918 struct blobmsg_list_node *var;
919 void *e = blobmsg_open_table(b, "data");
920 blobmsg_list_for_each(&in->data, var)
921 blobmsg_add_blob(b, var->data);
922 blobmsg_close_table(b, e);
923 }
924
925 if (!avl_is_empty(&in->limits.avl)) {
926 struct blobmsg_list_node *var;
927 void *e = blobmsg_open_table(b, "limits");
928 blobmsg_list_for_each(&in->limits, var)
929 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
930 blobmsg_close_table(b, e);
931 }
932
933 if (in->respawn) {
934 void *r = blobmsg_open_table(b, "respawn");
935 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
936 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
937 blobmsg_add_u32(b, "retry", in->respawn_retry);
938 blobmsg_close_table(b, r);
939 }
940
941 if (in->trace)
942 blobmsg_add_u8(b, "trace", true);
943
944 if (in->seccomp)
945 blobmsg_add_string(b, "seccomp", in->seccomp);
946
947 if (in->has_jail) {
948 void *r = blobmsg_open_table(b, "jail");
949 if (in->jail.name)
950 blobmsg_add_string(b, "name", in->jail.name);
951 blobmsg_add_u8(b, "procfs", in->jail.procfs);
952 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
953 blobmsg_add_u8(b, "ubus", in->jail.ubus);
954 blobmsg_add_u8(b, "log", in->jail.log);
955 blobmsg_add_u8(b, "ronly", in->jail.ronly);
956 blobmsg_close_table(b, r);
957 if (!avl_is_empty(&in->jail.mount.avl)) {
958 struct blobmsg_list_node *var;
959 void *e = blobmsg_open_table(b, "mount");
960 blobmsg_list_for_each(&in->jail.mount, var)
961 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
962 blobmsg_close_table(b, e);
963 }
964 }
965
966 if (verbose && in->trigger)
967 blobmsg_add_blob(b, in->trigger);
968
969 blobmsg_close_table(b, i);
970 }