procd: show process's exit code
[oweals/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 <grp.h>
21 #include <net/if.h>
22 #include <unistd.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <fcntl.h>
26 #include <pwd.h>
27 #include <libgen.h>
28 #include <unistd.h>
29 #define SYSLOG_NAMES
30 #include <syslog.h>
31
32 #include <libubox/md5.h>
33
34 #include "../procd.h"
35
36 #include "service.h"
37 #include "instance.h"
38
39 #define UJAIL_BIN_PATH "/sbin/ujail"
40
41 enum {
42         INSTANCE_ATTR_COMMAND,
43         INSTANCE_ATTR_ENV,
44         INSTANCE_ATTR_DATA,
45         INSTANCE_ATTR_NETDEV,
46         INSTANCE_ATTR_FILE,
47         INSTANCE_ATTR_TRIGGER,
48         INSTANCE_ATTR_RESPAWN,
49         INSTANCE_ATTR_NICE,
50         INSTANCE_ATTR_LIMITS,
51         INSTANCE_ATTR_WATCH,
52         INSTANCE_ATTR_ERROR,
53         INSTANCE_ATTR_USER,
54         INSTANCE_ATTR_GROUP,
55         INSTANCE_ATTR_STDOUT,
56         INSTANCE_ATTR_STDERR,
57         INSTANCE_ATTR_NO_NEW_PRIVS,
58         INSTANCE_ATTR_JAIL,
59         INSTANCE_ATTR_TRACE,
60         INSTANCE_ATTR_SECCOMP,
61         INSTANCE_ATTR_PIDFILE,
62         INSTANCE_ATTR_RELOADSIG,
63         INSTANCE_ATTR_TERMTIMEOUT,
64         INSTANCE_ATTR_FACILITY,
65         __INSTANCE_ATTR_MAX
66 };
67
68 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
69         [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
70         [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
71         [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
72         [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
73         [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
74         [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
75         [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
76         [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
77         [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
78         [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
79         [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
80         [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
81         [INSTANCE_ATTR_GROUP] = { "group", BLOBMSG_TYPE_STRING },
82         [INSTANCE_ATTR_STDOUT] = { "stdout", BLOBMSG_TYPE_BOOL },
83         [INSTANCE_ATTR_STDERR] = { "stderr", BLOBMSG_TYPE_BOOL },
84         [INSTANCE_ATTR_NO_NEW_PRIVS] = { "no_new_privs", BLOBMSG_TYPE_BOOL },
85         [INSTANCE_ATTR_JAIL] = { "jail", BLOBMSG_TYPE_TABLE },
86         [INSTANCE_ATTR_TRACE] = { "trace", BLOBMSG_TYPE_BOOL },
87         [INSTANCE_ATTR_SECCOMP] = { "seccomp", BLOBMSG_TYPE_STRING },
88         [INSTANCE_ATTR_PIDFILE] = { "pidfile", BLOBMSG_TYPE_STRING },
89         [INSTANCE_ATTR_RELOADSIG] = { "reload_signal", BLOBMSG_TYPE_INT32 },
90         [INSTANCE_ATTR_TERMTIMEOUT] = { "term_timeout", BLOBMSG_TYPE_INT32 },
91         [INSTANCE_ATTR_FACILITY] = { "facility", BLOBMSG_TYPE_STRING },
92 };
93
94 enum {
95         JAIL_ATTR_NAME,
96         JAIL_ATTR_HOSTNAME,
97         JAIL_ATTR_PROCFS,
98         JAIL_ATTR_SYSFS,
99         JAIL_ATTR_UBUS,
100         JAIL_ATTR_LOG,
101         JAIL_ATTR_RONLY,
102         JAIL_ATTR_MOUNT,
103         JAIL_ATTR_NETNS,
104         __JAIL_ATTR_MAX,
105 };
106
107 static const struct blobmsg_policy jail_attr[__JAIL_ATTR_MAX] = {
108         [JAIL_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
109         [JAIL_ATTR_HOSTNAME] = { "hostname", BLOBMSG_TYPE_STRING },
110         [JAIL_ATTR_PROCFS] = { "procfs", BLOBMSG_TYPE_BOOL },
111         [JAIL_ATTR_SYSFS] = { "sysfs", BLOBMSG_TYPE_BOOL },
112         [JAIL_ATTR_UBUS] = { "ubus", BLOBMSG_TYPE_BOOL },
113         [JAIL_ATTR_LOG] = { "log", BLOBMSG_TYPE_BOOL },
114         [JAIL_ATTR_RONLY] = { "ronly", BLOBMSG_TYPE_BOOL },
115         [JAIL_ATTR_MOUNT] = { "mount", BLOBMSG_TYPE_TABLE },
116         [JAIL_ATTR_NETNS] = { "netns", BLOBMSG_TYPE_BOOL },
117 };
118
119 struct instance_netdev {
120         struct blobmsg_list_node node;
121         int ifindex;
122 };
123
124 struct instance_file {
125         struct blobmsg_list_node node;
126         uint32_t md5[4];
127 };
128
129 struct rlimit_name {
130         const char *name;
131         int resource;
132 };
133
134 static const struct rlimit_name rlimit_names[] = {
135         { "as", RLIMIT_AS },
136         { "core", RLIMIT_CORE },
137         { "cpu", RLIMIT_CPU },
138         { "data", RLIMIT_DATA },
139         { "fsize", RLIMIT_FSIZE },
140         { "memlock", RLIMIT_MEMLOCK },
141         { "nofile", RLIMIT_NOFILE },
142         { "nproc", RLIMIT_NPROC },
143         { "rss", RLIMIT_RSS },
144         { "stack", RLIMIT_STACK },
145 #ifdef linux
146         { "nice", RLIMIT_NICE },
147         { "rtprio", RLIMIT_RTPRIO },
148         { "msgqueue", RLIMIT_MSGQUEUE },
149         { "sigpending", RLIMIT_SIGPENDING },
150 #endif
151         { NULL, 0 }
152 };
153
154 static void closefd(int fd)
155 {
156         if (fd > STDERR_FILENO)
157                 close(fd);
158 }
159
160 /* convert a string into numeric syslog facility or return -1 if no match found */
161 static int
162 syslog_facility_str_to_int(const char *facility)
163 {
164         CODE *p = facilitynames;
165
166         while (p->c_name && strcasecmp(p->c_name, facility))
167                 p++;
168
169         return p->c_val;
170 }
171
172 static void
173 instance_limits(const char *limit, const char *value)
174 {
175         int i;
176         struct rlimit rlim;
177         unsigned long cur, max;
178
179         for (i = 0; rlimit_names[i].name != NULL; i++) {
180                 if (strcmp(rlimit_names[i].name, limit))
181                         continue;
182                 if (!strcmp(value, "unlimited")) {
183                         rlim.rlim_cur = RLIM_INFINITY;
184                         rlim.rlim_max = RLIM_INFINITY;
185                 } else {
186                         if (getrlimit(rlimit_names[i].resource, &rlim))
187                                 return;
188
189                         cur = rlim.rlim_cur;
190                         max = rlim.rlim_max;
191
192                         if (sscanf(value, "%lu %lu", &cur, &max) < 1)
193                                 return;
194
195                         rlim.rlim_cur = cur;
196                         rlim.rlim_max = max;
197                 }
198
199                 setrlimit(rlimit_names[i].resource, &rlim);
200                 return;
201         }
202 }
203
204 static inline int
205 jail_run(struct service_instance *in, char **argv)
206 {
207         struct blobmsg_list_node *var;
208         struct jail *jail = &in->jail;
209         int argc = 0;
210
211         argv[argc++] = UJAIL_BIN_PATH;
212
213         if (jail->name) {
214                 argv[argc++] = "-n";
215                 argv[argc++] = jail->name;
216         }
217
218         if (jail->hostname) {
219                 argv[argc++] = "-h";
220                 argv[argc++] = jail->hostname;
221         }
222
223         if (in->seccomp) {
224                 argv[argc++] = "-S";
225                 argv[argc++] = in->seccomp;
226         }
227
228         if (in->user) {
229                 argv[argc++] = "-U";
230                 argv[argc++] = in->user;
231         }
232
233         if (in->group) {
234                 argv[argc++] = "-G";
235                 argv[argc++] = in->group;
236         }
237
238         if (in->no_new_privs)
239                 argv[argc++] = "-c";
240
241         if (jail->procfs)
242                 argv[argc++] = "-p";
243
244         if (jail->sysfs)
245                 argv[argc++] = "-s";
246
247         if (jail->ubus)
248                 argv[argc++] = "-u";
249
250         if (jail->log)
251                 argv[argc++] = "-l";
252
253         if (jail->ronly)
254                 argv[argc++] = "-o";
255
256         if (jail->netns)
257                 argv[argc++] = "-N";
258
259         blobmsg_list_for_each(&jail->mount, var) {
260                 const char *type = blobmsg_data(var->data);
261
262                 if (*type == '1')
263                         argv[argc++] = "-w";
264                 else
265                         argv[argc++] = "-r";
266                 argv[argc++] = (char *) blobmsg_name(var->data);
267         }
268
269         argv[argc++] = "--";
270
271         return argc;
272 }
273
274 static int
275 instance_removepid(struct service_instance *in) {
276         if (!in->pidfile)
277                 return 0;
278         if (unlink(in->pidfile)) {
279                 ERROR("Failed to remove pidfile: %s: %m\n", in->pidfile);
280                 return 1;
281         }
282         return 0;
283 }
284
285 static int
286 instance_writepid(struct service_instance *in)
287 {
288         FILE *_pidfile;
289
290         if (!in->pidfile) {
291                 return 0;
292         }
293         _pidfile = fopen(in->pidfile, "w");
294         if (_pidfile == NULL) {
295                 ERROR("failed to open pidfile for writing: %s: %m", in->pidfile);
296                 return 1;
297         }
298         if (fprintf(_pidfile, "%d\n", in->proc.pid) < 0) {
299                 ERROR("failed to write pidfile: %s: %m", in->pidfile);
300                 fclose(_pidfile);
301                 return 2;
302         }
303         if (fclose(_pidfile)) {
304                 ERROR("failed to close pidfile: %s: %m", in->pidfile);
305                 return 3;
306         }
307
308         return 0;
309 }
310
311 static void
312 instance_run(struct service_instance *in, int _stdout, int _stderr)
313 {
314         struct blobmsg_list_node *var;
315         struct blob_attr *cur;
316         char **argv;
317         int argc = 1; /* NULL terminated */
318         int rem, _stdin;
319         bool seccomp = !in->trace && !in->has_jail && in->seccomp;
320         bool setlbf = _stdout >= 0;
321
322         if (in->nice)
323                 setpriority(PRIO_PROCESS, 0, in->nice);
324
325         blobmsg_for_each_attr(cur, in->command, rem)
326                 argc++;
327
328         blobmsg_list_for_each(&in->env, var)
329                 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
330
331         if (seccomp)
332                 setenv("SECCOMP_FILE", in->seccomp, 1);
333
334         if (setlbf)
335                 setenv("LD_PRELOAD", "/lib/libsetlbf.so", 1);
336
337         blobmsg_list_for_each(&in->limits, var)
338                 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
339
340         if (in->trace || seccomp)
341                 argc += 1;
342
343         argv = alloca(sizeof(char *) * (argc + in->jail.argc));
344         argc = 0;
345
346 #ifdef SECCOMP_SUPPORT
347         if (in->trace)
348                 argv[argc++] = "/sbin/utrace";
349         else if (seccomp)
350                 argv[argc++] = "/sbin/seccomp-trace";
351 #else
352         if (in->trace || seccomp)
353                 ULOG_WARN("Seccomp support for %s::%s not available\n", in->srv->name, in->name);
354 #endif
355
356         if (in->has_jail) {
357                 argc = jail_run(in, argv);
358                 if (argc != in->jail.argc)
359                         ULOG_WARN("expected %i jail params, used %i for %s::%s\n",
360                                 in->jail.argc, argc, in->srv->name, in->name);
361         }
362
363         blobmsg_for_each_attr(cur, in->command, rem)
364                 argv[argc++] = blobmsg_data(cur);
365
366         argv[argc] = NULL;
367
368         _stdin = open("/dev/null", O_RDONLY);
369
370         if (_stdout == -1)
371                 _stdout = open("/dev/null", O_WRONLY);
372
373         if (_stderr == -1)
374                 _stderr = open("/dev/null", O_WRONLY);
375
376         if (_stdin > -1) {
377                 dup2(_stdin, STDIN_FILENO);
378                 closefd(_stdin);
379         }
380         if (_stdout > -1) {
381                 dup2(_stdout, STDOUT_FILENO);
382                 closefd(_stdout);
383         }
384         if (_stderr > -1) {
385                 dup2(_stderr, STDERR_FILENO);
386                 closefd(_stderr);
387         }
388
389         if (!in->has_jail && in->user && in->pw_gid && initgroups(in->user, in->pw_gid)) {
390                 ERROR("failed to initgroups() for user %s: %m\n", in->user);
391                 exit(127);
392         }
393         if (!in->has_jail && in->gr_gid && setgid(in->gr_gid)) {
394                 ERROR("failed to set group id %d: %m\n", in->gr_gid);
395                 exit(127);
396         }
397         if (!in->has_jail && in->uid && setuid(in->uid)) {
398                 ERROR("failed to set user id %d: %m\n", in->uid);
399                 exit(127);
400         }
401
402         execvp(argv[0], argv);
403         exit(127);
404 }
405
406 static void
407 instance_free_stdio(struct service_instance *in)
408 {
409         if (in->_stdout.fd.fd > -1) {
410                 ustream_free(&in->_stdout.stream);
411                 close(in->_stdout.fd.fd);
412                 in->_stdout.fd.fd = -1;
413         }
414
415         if (in->_stderr.fd.fd > -1) {
416                 ustream_free(&in->_stderr.stream);
417                 close(in->_stderr.fd.fd);
418                 in->_stderr.fd.fd = -1;
419         }
420 }
421
422 void
423 instance_start(struct service_instance *in)
424 {
425         int pid;
426         int opipe[2] = { -1, -1 };
427         int epipe[2] = { -1, -1 };
428
429         if (!avl_is_empty(&in->errors.avl)) {
430                 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
431                 return;
432         }
433
434         if (!in->command) {
435                 LOG("Not starting instance %s::%s, command not set\n", in->srv->name, in->name);
436                 return;
437         }
438
439         if (in->proc.pending) {
440                 if (in->halt)
441                         in->restart = true;
442                 return;
443         }
444
445         instance_free_stdio(in);
446         if (in->_stdout.fd.fd > -2) {
447                 if (pipe(opipe)) {
448                         ULOG_WARN("pipe() failed: %m\n");
449                         opipe[0] = opipe[1] = -1;
450                 }
451         }
452
453         if (in->_stderr.fd.fd > -2) {
454                 if (pipe(epipe)) {
455                         ULOG_WARN("pipe() failed: %m\n");
456                         epipe[0] = epipe[1] = -1;
457                 }
458         }
459
460         in->restart = false;
461         in->halt = false;
462
463         if (!in->valid)
464                 return;
465
466         pid = fork();
467         if (pid < 0)
468                 return;
469
470         if (!pid) {
471                 uloop_done();
472                 closefd(opipe[0]);
473                 closefd(epipe[0]);
474                 instance_run(in, opipe[1], epipe[1]);
475                 return;
476         }
477
478         DEBUG(2, "Started instance %s::%s[%d]\n", in->srv->name, in->name, pid);
479         in->proc.pid = pid;
480         instance_writepid(in);
481         clock_gettime(CLOCK_MONOTONIC, &in->start);
482         uloop_process_add(&in->proc);
483
484         if (opipe[0] > -1) {
485                 ustream_fd_init(&in->_stdout, opipe[0]);
486                 closefd(opipe[1]);
487                 fcntl(opipe[0], F_SETFD, FD_CLOEXEC);
488         }
489
490         if (epipe[0] > -1) {
491                 ustream_fd_init(&in->_stderr, epipe[0]);
492                 closefd(epipe[1]);
493                 fcntl(epipe[0], F_SETFD, FD_CLOEXEC);
494         }
495
496         service_event("instance.start", in->srv->name, in->name);
497 }
498
499 static void
500 instance_stdio(struct ustream *s, int prio, struct service_instance *in)
501 {
502         char *newline, *str, *arg0, ident[32];
503         int len;
504
505         arg0 = basename(blobmsg_data(blobmsg_data(in->command)));
506         snprintf(ident, sizeof(ident), "%s[%d]", arg0, in->proc.pid);
507         ulog_open(ULOG_SYSLOG, in->syslog_facility, ident);
508
509         do {
510                 str = ustream_get_read_buf(s, &len);
511                 if (!str)
512                         break;
513
514                 newline = memchr(str, '\n', len);
515                 if (!newline && (s->r.buffer_len != len))
516                         break;
517
518                 if (newline) {
519                         *newline = 0;
520                         len = newline + 1 - str;
521                 }
522                 ulog(prio, "%s\n", str);
523
524                 ustream_consume(s, len);
525         } while (1);
526
527         ulog_open(ULOG_SYSLOG, LOG_DAEMON, "procd");
528 }
529
530 static void
531 instance_stdout(struct ustream *s, int bytes)
532 {
533         instance_stdio(s, LOG_INFO,
534                        container_of(s, struct service_instance, _stdout.stream));
535 }
536
537 static void
538 instance_stderr(struct ustream *s, int bytes)
539 {
540         instance_stdio(s, LOG_ERR,
541                        container_of(s, struct service_instance, _stderr.stream));
542 }
543
544 static void
545 instance_timeout(struct uloop_timeout *t)
546 {
547         struct service_instance *in;
548
549         in = container_of(t, struct service_instance, timeout);
550
551         if (in->halt) {
552                 LOG("Instance %s::%s pid %d not stopped on SIGTERM, sending SIGKILL instead\n",
553                                 in->srv->name, in->name, in->proc.pid);
554                 kill(in->proc.pid, SIGKILL);
555         } else if (in->restart || in->respawn)
556                 instance_start(in);
557 }
558
559 static void
560 instance_delete(struct service_instance *in)
561 {
562         struct service *s = in->srv;
563
564         avl_delete(&s->instances.avl, &in->node.avl);
565         instance_free(in);
566         service_stopped(s);
567 }
568
569 static int
570 instance_exit_code(int ret)
571 {
572         if (WIFEXITED(ret)) {
573                 return WEXITSTATUS(ret);
574         }
575
576         if (WIFSIGNALED(ret)) {
577                 return SIGNALLED_OFFSET + WTERMSIG(ret);
578         }
579
580         if (WIFSTOPPED(ret)) {
581                 return WSTOPSIG(ret);
582         }
583
584         return 1;
585 }
586
587 static void
588 instance_exit(struct uloop_process *p, int ret)
589 {
590         struct service_instance *in;
591         struct timespec tp;
592         long runtime;
593
594         in = container_of(p, struct service_instance, proc);
595
596         clock_gettime(CLOCK_MONOTONIC, &tp);
597         runtime = tp.tv_sec - in->start.tv_sec;
598
599         DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
600
601         in->exit_code = instance_exit_code(ret);
602         uloop_timeout_cancel(&in->timeout);
603         service_event("instance.stop", in->srv->name, in->name);
604
605         if (in->halt) {
606                 instance_removepid(in);
607                 if (in->restart)
608                         instance_start(in);
609                 else
610                         instance_delete(in);
611         } else if (in->restart) {
612                 instance_start(in);
613         } else if (in->respawn) {
614                 if (runtime < in->respawn_threshold)
615                         in->respawn_count++;
616                 else
617                         in->respawn_count = 0;
618                 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
619                         LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
620                                                                 in->srv->name, in->name, in->respawn_count, runtime);
621                         in->restart = in->respawn = 0;
622                         in->halt = 1;
623                         service_event("instance.fail", in->srv->name, in->name);
624                 } else {
625                         service_event("instance.respawn", in->srv->name, in->name);
626                         uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
627                 }
628         }
629 }
630
631 void
632 instance_stop(struct service_instance *in, bool halt)
633 {
634         if (!in->proc.pending) {
635                 if (halt)
636                         instance_delete(in);
637                 return;
638         }
639         in->halt = halt;
640         in->restart = in->respawn = false;
641         kill(in->proc.pid, SIGTERM);
642         uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
643 }
644
645 static void
646 instance_restart(struct service_instance *in)
647 {
648         if (!in->proc.pending)
649                 return;
650
651         if (in->reload_signal) {
652                 kill(in->proc.pid, in->reload_signal);
653                 return;
654         }
655
656         in->halt = true;
657         in->restart = true;
658         kill(in->proc.pid, SIGTERM);
659         uloop_timeout_set(&in->timeout, in->term_timeout * 1000);
660 }
661
662 static bool string_changed(const char *a, const char *b)
663 {
664         return !((!a && !b) || (a && b && !strcmp(a, b)));
665 }
666
667 static bool
668 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
669 {
670         if (!in->valid)
671                 return true;
672
673         if (!blob_attr_equal(in->command, in_new->command))
674                 return true;
675
676         if (!blobmsg_list_equal(&in->env, &in_new->env))
677                 return true;
678
679         if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
680                 return true;
681
682         if (!blobmsg_list_equal(&in->file, &in_new->file))
683                 return true;
684
685         if (in->nice != in_new->nice)
686                 return true;
687
688         if (in->syslog_facility != in_new->syslog_facility)
689                 return true;
690
691         if (string_changed(in->user, in_new->user))
692                 return true;
693
694         if (string_changed(in->group, in_new->group))
695                 return true;
696
697         if (in->uid != in_new->uid)
698                 return true;
699
700         if (in->pw_gid != in_new->pw_gid)
701                 return true;
702
703         if (string_changed(in->pidfile, in_new->pidfile))
704                 return true;
705
706         if (in->respawn_retry != in_new->respawn_retry)
707                 return true;
708         if (in->respawn_threshold != in_new->respawn_threshold)
709                 return true;
710         if (in->respawn_timeout != in_new->respawn_timeout)
711                 return true;
712
713         if ((!in->seccomp && in_new->seccomp) ||
714             (in->seccomp && !in_new->seccomp) ||
715             (in->seccomp && in_new->seccomp && strcmp(in->seccomp, in_new->seccomp)))
716                 return true;
717
718         if (!blobmsg_list_equal(&in->limits, &in_new->limits))
719                 return true;
720
721         if (!blobmsg_list_equal(&in->jail.mount, &in_new->jail.mount))
722                 return true;
723
724         if (!blobmsg_list_equal(&in->errors, &in_new->errors))
725                 return true;
726
727         return false;
728 }
729
730 static bool
731 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
732 {
733         struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
734         struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
735
736         return n1->ifindex == n2->ifindex;
737 }
738
739 static void
740 instance_netdev_update(struct blobmsg_list_node *l)
741 {
742         struct instance_netdev *n = container_of(l, struct instance_netdev, node);
743
744         n->ifindex = if_nametoindex(n->node.avl.key);
745 }
746
747 static bool
748 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
749 {
750         struct instance_file *f1 = container_of(l1, struct instance_file, node);
751         struct instance_file *f2 = container_of(l2, struct instance_file, node);
752
753         return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
754 }
755
756 static void
757 instance_file_update(struct blobmsg_list_node *l)
758 {
759         struct instance_file *f = container_of(l, struct instance_file, node);
760         md5_ctx_t md5;
761         char buf[256];
762         int len, fd;
763
764         memset(f->md5, 0, sizeof(f->md5));
765
766         fd = open(l->avl.key, O_RDONLY);
767         if (fd < 0)
768                 return;
769
770         md5_begin(&md5);
771         do {
772                 len = read(fd, buf, sizeof(buf));
773                 if (len < 0) {
774                         if (errno == EINTR)
775                                 continue;
776
777                         break;
778                 }
779                 if (!len)
780                         break;
781
782                 md5_hash(buf, len, &md5);
783         } while(1);
784
785         md5_end(f->md5, &md5);
786         close(fd);
787 }
788
789 static void
790 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
791 {
792         if (!cur)
793                 return;
794
795         blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
796 }
797
798 static bool
799 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
800 {
801         struct blobmsg_list_node *node;
802
803         if (!cur)
804                 return true;
805
806         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
807                 return false;
808
809         blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
810         if (cb) {
811                 blobmsg_list_for_each(l, node)
812                         cb(node);
813         }
814         return true;
815 }
816
817 static int
818 instance_jail_parse(struct service_instance *in, struct blob_attr *attr)
819 {
820         struct blob_attr *tb[__JAIL_ATTR_MAX];
821         struct jail *jail = &in->jail;
822         struct stat s;
823         int r;
824
825         r = stat(UJAIL_BIN_PATH, &s);
826         if (r < 0) {
827                 ERROR("unable to find %s: %m (%d)\n", UJAIL_BIN_PATH, r);
828                 return 0;
829         }
830
831         blobmsg_parse(jail_attr, __JAIL_ATTR_MAX, tb,
832                 blobmsg_data(attr), blobmsg_data_len(attr));
833
834         jail->argc = 2;
835
836         if (tb[JAIL_ATTR_NAME]) {
837                 jail->name = strdup(blobmsg_get_string(tb[JAIL_ATTR_NAME]));
838                 jail->argc += 2;
839         }
840         if (tb[JAIL_ATTR_HOSTNAME]) {
841                 jail->hostname = strdup(blobmsg_get_string(tb[JAIL_ATTR_HOSTNAME]));
842                 jail->argc += 2;
843         }
844         if (tb[JAIL_ATTR_PROCFS]) {
845                 jail->procfs = blobmsg_get_bool(tb[JAIL_ATTR_PROCFS]);
846                 jail->argc++;
847         }
848         if (tb[JAIL_ATTR_SYSFS]) {
849                 jail->sysfs = blobmsg_get_bool(tb[JAIL_ATTR_SYSFS]);
850                 jail->argc++;
851         }
852         if (tb[JAIL_ATTR_UBUS]) {
853                 jail->ubus = blobmsg_get_bool(tb[JAIL_ATTR_UBUS]);
854                 jail->argc++;
855         }
856         if (tb[JAIL_ATTR_LOG]) {
857                 jail->log = blobmsg_get_bool(tb[JAIL_ATTR_LOG]);
858                 jail->argc++;
859         }
860         if (tb[JAIL_ATTR_RONLY]) {
861                 jail->ronly = blobmsg_get_bool(tb[JAIL_ATTR_RONLY]);
862                 jail->argc++;
863         }
864         if (tb[JAIL_ATTR_NETNS]) {
865                 jail->netns = blobmsg_get_bool(tb[JAIL_ATTR_NETNS]);
866                 jail->argc++;
867         }
868         if (tb[JAIL_ATTR_MOUNT]) {
869                 struct blob_attr *cur;
870                 int rem;
871
872                 blobmsg_for_each_attr(cur, tb[JAIL_ATTR_MOUNT], rem)
873                         jail->argc += 2;
874                 instance_fill_array(&jail->mount, tb[JAIL_ATTR_MOUNT], NULL, false);
875         }
876         if (in->seccomp)
877                 jail->argc += 2;
878
879         if (in->user)
880                 jail->argc += 2;
881
882         if (in->group)
883                 jail->argc += 2;
884
885         if (in->no_new_privs)
886                 jail->argc++;
887
888         return 1;
889 }
890
891 static bool
892 instance_config_parse_command(struct service_instance *in, struct blob_attr **tb)
893 {
894         struct blob_attr *cur, *cur2;
895         bool ret = false;
896         int rem;
897
898         cur = tb[INSTANCE_ATTR_COMMAND];
899         if (!cur) {
900                 in->command = NULL;
901                 return true;
902         }
903
904         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
905                 return false;
906
907         blobmsg_for_each_attr(cur2, cur, rem) {
908                 ret = true;
909                 break;
910         }
911
912         in->command = cur;
913         return ret;
914 }
915
916 static bool
917 instance_config_parse(struct service_instance *in)
918 {
919         struct blob_attr *tb[__INSTANCE_ATTR_MAX];
920         struct blob_attr *cur, *cur2;
921         int rem;
922
923         blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
924                 blobmsg_data(in->config), blobmsg_data_len(in->config));
925
926         if (!instance_config_parse_command(in, tb))
927                 return false;
928
929         if (tb[INSTANCE_ATTR_TERMTIMEOUT])
930                 in->term_timeout = blobmsg_get_u32(tb[INSTANCE_ATTR_TERMTIMEOUT]);
931         if (tb[INSTANCE_ATTR_RESPAWN]) {
932                 int i = 0;
933                 uint32_t vals[3] = { 3600, 5, 5};
934
935                 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
936                         if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
937                                 continue;
938                         vals[i] = atoi(blobmsg_get_string(cur2));
939                         i++;
940                 }
941                 in->respawn = true;
942                 in->respawn_count = 0;
943                 in->respawn_threshold = vals[0];
944                 in->respawn_timeout = vals[1];
945                 in->respawn_retry = vals[2];
946         }
947         if (tb[INSTANCE_ATTR_TRIGGER]) {
948                 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
949                 trigger_add(in->trigger, in);
950         }
951
952         if (tb[INSTANCE_ATTR_WATCH]) {
953                 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
954                         if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
955                                 continue;
956                         DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
957                         watch_add(blobmsg_get_string(cur2), in);
958                 }
959         }
960
961         if ((cur = tb[INSTANCE_ATTR_NICE])) {
962                 in->nice = (int8_t) blobmsg_get_u32(cur);
963                 if (in->nice < -20 || in->nice > 20)
964                         return false;
965         }
966
967         if (tb[INSTANCE_ATTR_USER]) {
968                 const char *user = blobmsg_get_string(tb[INSTANCE_ATTR_USER]);
969                 struct passwd *p = getpwnam(user);
970                 if (p) {
971                         in->user = strdup(user);
972                         in->uid = p->pw_uid;
973                         in->gr_gid = in->pw_gid = p->pw_gid;
974                 }
975         }
976
977         if (tb[INSTANCE_ATTR_GROUP]) {
978                 const char *group = blobmsg_get_string(tb[INSTANCE_ATTR_GROUP]);
979                 struct group *p = getgrnam(group);
980                 if (p) {
981                         in->group = strdup(group);
982                         in->gr_gid = p->gr_gid;
983                 }
984         }
985
986         if (tb[INSTANCE_ATTR_TRACE])
987                 in->trace = blobmsg_get_bool(tb[INSTANCE_ATTR_TRACE]);
988
989         if (tb[INSTANCE_ATTR_NO_NEW_PRIVS])
990                 in->no_new_privs = blobmsg_get_bool(tb[INSTANCE_ATTR_NO_NEW_PRIVS]);
991
992         if (!in->trace && tb[INSTANCE_ATTR_SECCOMP])
993                 in->seccomp = strdup(blobmsg_get_string(tb[INSTANCE_ATTR_SECCOMP]));
994
995         if (tb[INSTANCE_ATTR_PIDFILE]) {
996                 char *pidfile = blobmsg_get_string(tb[INSTANCE_ATTR_PIDFILE]);
997                 if (pidfile)
998                         in->pidfile = strdup(pidfile);
999         }
1000
1001         if (tb[INSTANCE_ATTR_RELOADSIG])
1002                 in->reload_signal = blobmsg_get_u32(tb[INSTANCE_ATTR_RELOADSIG]);
1003
1004         if (!in->trace && tb[INSTANCE_ATTR_JAIL])
1005                 in->has_jail = instance_jail_parse(in, tb[INSTANCE_ATTR_JAIL]);
1006
1007         if (tb[INSTANCE_ATTR_STDOUT] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDOUT]))
1008                 in->_stdout.fd.fd = -1;
1009
1010         if (tb[INSTANCE_ATTR_STDERR] && blobmsg_get_bool(tb[INSTANCE_ATTR_STDERR]))
1011                 in->_stderr.fd.fd = -1;
1012
1013         instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
1014
1015         if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
1016                 return false;
1017
1018         if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
1019                 return false;
1020
1021         if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
1022                 return false;
1023
1024         if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
1025                 return false;
1026
1027         if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
1028                 return false;
1029
1030         if (tb[INSTANCE_ATTR_FACILITY]) {
1031                 int facility = syslog_facility_str_to_int(blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1032                 if (facility != -1) {
1033                         in->syslog_facility = facility;
1034                         DEBUG(3, "setting facility '%s'\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1035                 } else
1036                         DEBUG(3, "unknown syslog facility '%s' given, using default (LOG_DAEMON)\n", blobmsg_get_string(tb[INSTANCE_ATTR_FACILITY]));
1037         }
1038
1039         return true;
1040 }
1041
1042 static void
1043 instance_config_cleanup(struct service_instance *in)
1044 {
1045         blobmsg_list_free(&in->env);
1046         blobmsg_list_free(&in->data);
1047         blobmsg_list_free(&in->netdev);
1048         blobmsg_list_free(&in->file);
1049         blobmsg_list_free(&in->limits);
1050         blobmsg_list_free(&in->errors);
1051         blobmsg_list_free(&in->jail.mount);
1052 }
1053
1054 static void
1055 instance_config_move_strdup(char **dst, char *src)
1056 {
1057         if (*dst) {
1058                 free(*dst);
1059                 *dst = NULL;
1060         }
1061
1062         if (!src)
1063                 return;
1064
1065         *dst = strdup(src);
1066 }
1067
1068 static void
1069 instance_config_move(struct service_instance *in, struct service_instance *in_src)
1070 {
1071         instance_config_cleanup(in);
1072         blobmsg_list_move(&in->env, &in_src->env);
1073         blobmsg_list_move(&in->data, &in_src->data);
1074         blobmsg_list_move(&in->netdev, &in_src->netdev);
1075         blobmsg_list_move(&in->file, &in_src->file);
1076         blobmsg_list_move(&in->limits, &in_src->limits);
1077         blobmsg_list_move(&in->errors, &in_src->errors);
1078         blobmsg_list_move(&in->jail.mount, &in_src->jail.mount);
1079         in->trigger = in_src->trigger;
1080         in->command = in_src->command;
1081         in->respawn = in_src->respawn;
1082         in->respawn_retry = in_src->respawn_retry;
1083         in->respawn_threshold = in_src->respawn_threshold;
1084         in->respawn_timeout = in_src->respawn_timeout;
1085         in->name = in_src->name;
1086         in->trace = in_src->trace;
1087         in->node.avl.key = in_src->node.avl.key;
1088         in->syslog_facility = in_src->syslog_facility;
1089
1090         instance_config_move_strdup(&in->pidfile, in_src->pidfile);
1091         instance_config_move_strdup(&in->seccomp, in_src->seccomp);
1092         instance_config_move_strdup(&in->jail.name, in_src->jail.name);
1093         instance_config_move_strdup(&in->jail.hostname, in_src->jail.hostname);
1094
1095         free(in->config);
1096         in->config = in_src->config;
1097         in_src->config = NULL;
1098 }
1099
1100 void
1101 instance_update(struct service_instance *in, struct service_instance *in_new)
1102 {
1103         bool changed = instance_config_changed(in, in_new);
1104         bool running = in->proc.pending;
1105         bool stopping = in->halt;
1106
1107         if (!running || stopping) {
1108                 instance_config_move(in, in_new);
1109                 instance_start(in);
1110         } else {
1111                 if (changed)
1112                         instance_restart(in);
1113                 instance_config_move(in, in_new);
1114                 /* restart happens in the child callback handler */
1115         }
1116 }
1117
1118 void
1119 instance_free(struct service_instance *in)
1120 {
1121         instance_free_stdio(in);
1122         uloop_process_delete(&in->proc);
1123         uloop_timeout_cancel(&in->timeout);
1124         trigger_del(in);
1125         watch_del(in);
1126         instance_config_cleanup(in);
1127         free(in->config);
1128         free(in->user);
1129         free(in->group);
1130         free(in->jail.name);
1131         free(in->jail.hostname);
1132         free(in->seccomp);
1133         free(in->pidfile);
1134         free(in);
1135 }
1136
1137 void
1138 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
1139 {
1140         config = blob_memdup(config);
1141         in->srv = s;
1142         in->name = blobmsg_name(config);
1143         in->config = config;
1144         in->timeout.cb = instance_timeout;
1145         in->proc.cb = instance_exit;
1146         in->term_timeout = 5;
1147         in->syslog_facility = LOG_DAEMON;
1148         in->exit_code = 0;
1149
1150         in->_stdout.fd.fd = -2;
1151         in->_stdout.stream.string_data = true;
1152         in->_stdout.stream.notify_read = instance_stdout;
1153
1154         in->_stderr.fd.fd = -2;
1155         in->_stderr.stream.string_data = true;
1156         in->_stderr.stream.notify_read = instance_stderr;
1157
1158         blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
1159         blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
1160         blobmsg_list_simple_init(&in->env);
1161         blobmsg_list_simple_init(&in->data);
1162         blobmsg_list_simple_init(&in->limits);
1163         blobmsg_list_simple_init(&in->errors);
1164         blobmsg_list_simple_init(&in->jail.mount);
1165         in->valid = instance_config_parse(in);
1166 }
1167
1168 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
1169 {
1170         void *i;
1171
1172         if (!in->valid)
1173                 return;
1174
1175         i = blobmsg_open_table(b, in->name);
1176         blobmsg_add_u8(b, "running", in->proc.pending);
1177         if (in->proc.pending)
1178                 blobmsg_add_u32(b, "pid", in->proc.pid);
1179         if (in->command)
1180                 blobmsg_add_blob(b, in->command);
1181         blobmsg_add_u32(b, "term_timeout", in->term_timeout);
1182         if (!in->proc.pending)
1183                 blobmsg_add_u32(b, "exit_code", in->exit_code);
1184
1185         if (!avl_is_empty(&in->errors.avl)) {
1186                 struct blobmsg_list_node *var;
1187                 void *e = blobmsg_open_array(b, "errors");
1188                 blobmsg_list_for_each(&in->errors, var)
1189                         blobmsg_add_string(b, NULL, blobmsg_data(var->data));
1190                 blobmsg_close_table(b, e);
1191         }
1192
1193         if (!avl_is_empty(&in->env.avl)) {
1194                 struct blobmsg_list_node *var;
1195                 void *e = blobmsg_open_table(b, "env");
1196                 blobmsg_list_for_each(&in->env, var)
1197                         blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1198                 blobmsg_close_table(b, e);
1199         }
1200
1201         if (!avl_is_empty(&in->data.avl)) {
1202                 struct blobmsg_list_node *var;
1203                 void *e = blobmsg_open_table(b, "data");
1204                 blobmsg_list_for_each(&in->data, var)
1205                         blobmsg_add_blob(b, var->data);
1206                 blobmsg_close_table(b, e);
1207         }
1208
1209         if (!avl_is_empty(&in->limits.avl)) {
1210                 struct blobmsg_list_node *var;
1211                 void *e = blobmsg_open_table(b, "limits");
1212                 blobmsg_list_for_each(&in->limits, var)
1213                         blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1214                 blobmsg_close_table(b, e);
1215         }
1216
1217         if (in->reload_signal)
1218                 blobmsg_add_u32(b, "reload_signal", in->reload_signal);
1219
1220         if (in->respawn) {
1221                 void *r = blobmsg_open_table(b, "respawn");
1222                 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
1223                 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
1224                 blobmsg_add_u32(b, "retry", in->respawn_retry);
1225                 blobmsg_close_table(b, r);
1226         }
1227
1228         if (in->trace)
1229                 blobmsg_add_u8(b, "trace", true);
1230
1231         if (in->no_new_privs)
1232                 blobmsg_add_u8(b, "no_new_privs", true);
1233
1234         if (in->seccomp)
1235                 blobmsg_add_string(b, "seccomp", in->seccomp);
1236
1237         if (in->pidfile)
1238                 blobmsg_add_string(b, "pidfile", in->pidfile);
1239
1240         if (in->user)
1241                 blobmsg_add_string(b, "user", in->user);
1242
1243         if (in->group)
1244                 blobmsg_add_string(b, "group", in->group);
1245
1246         if (in->has_jail) {
1247                 void *r = blobmsg_open_table(b, "jail");
1248                 if (in->jail.name)
1249                         blobmsg_add_string(b, "name", in->jail.name);
1250                 if (in->jail.hostname)
1251                         blobmsg_add_string(b, "hostname", in->jail.hostname);
1252                 blobmsg_add_u8(b, "procfs", in->jail.procfs);
1253                 blobmsg_add_u8(b, "sysfs", in->jail.sysfs);
1254                 blobmsg_add_u8(b, "ubus", in->jail.ubus);
1255                 blobmsg_add_u8(b, "log", in->jail.log);
1256                 blobmsg_add_u8(b, "ronly", in->jail.ronly);
1257                 blobmsg_add_u8(b, "netns", in->jail.netns);
1258                 blobmsg_close_table(b, r);
1259                 if (!avl_is_empty(&in->jail.mount.avl)) {
1260                         struct blobmsg_list_node *var;
1261                         void *e = blobmsg_open_table(b, "mount");
1262                         blobmsg_list_for_each(&in->jail.mount, var)
1263                                 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
1264                         blobmsg_close_table(b, e);
1265                 }
1266         }
1267
1268         if (verbose && in->trigger)
1269                 blobmsg_add_blob(b, in->trigger);
1270
1271         blobmsg_close_table(b, i);
1272 }