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