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