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