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