Bug Summary

File:service/instance.c
Warning:line 402, column 2
Null pointer passed to 1st parameter expecting 'nonnull'

Annotated Source Code

Press '?' to see keyboard shortcuts

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