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