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