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