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