e410bc43a493eb4904054f2b09a654a2f3391716
[project/procd.git] / service / instance.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <sys/resource.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <net/if.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include <fcntl.h>
22
23 #include <libubox/md5.h>
24
25 #include "../procd.h"
26
27 #include "service.h"
28 #include "instance.h"
29
30
31 enum {
32 INSTANCE_ATTR_COMMAND,
33 INSTANCE_ATTR_ENV,
34 INSTANCE_ATTR_DATA,
35 INSTANCE_ATTR_NETDEV,
36 INSTANCE_ATTR_FILE,
37 INSTANCE_ATTR_TRIGGER,
38 INSTANCE_ATTR_RESPAWN,
39 INSTANCE_ATTR_NICE,
40 INSTANCE_ATTR_LIMITS,
41 INSTANCE_ATTR_WATCH,
42 __INSTANCE_ATTR_MAX
43 };
44
45 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
46 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
47 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
48 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
49 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
50 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
51 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
52 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
53 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
54 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
55 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
56 };
57
58 struct instance_netdev {
59 struct blobmsg_list_node node;
60 int ifindex;
61 };
62
63 struct instance_file {
64 struct blobmsg_list_node node;
65 uint32_t md5[4];
66 };
67
68 struct rlimit_name {
69 const char *name;
70 int resource;
71 };
72
73 static const struct rlimit_name rlimit_names[] = {
74 { "as", RLIMIT_AS },
75 { "core", RLIMIT_CORE },
76 { "cpu", RLIMIT_CPU },
77 { "data", RLIMIT_DATA },
78 { "fsize", RLIMIT_FSIZE },
79 { "memlock", RLIMIT_MEMLOCK },
80 { "msgqueue", RLIMIT_MSGQUEUE },
81 { "nice", RLIMIT_NICE },
82 { "nofile", RLIMIT_NOFILE },
83 { "nproc", RLIMIT_NPROC },
84 { "rss", RLIMIT_RSS },
85 { "rtprio", RLIMIT_RTPRIO },
86 { "sigpending", RLIMIT_SIGPENDING },
87 { "stack", RLIMIT_STACK },
88 { NULL, 0 }
89 };
90
91 static void
92 instance_limits(const char *limit, const char *value)
93 {
94 int i;
95 struct rlimit rlim;
96 unsigned long cur, max;
97
98 for (i = 0; rlimit_names[i].name != NULL; i++) {
99 if (strcmp(rlimit_names[i].name, limit))
100 continue;
101 if (!strcmp(value, "unlimited")) {
102 rlim.rlim_cur = RLIM_INFINITY;
103 rlim.rlim_max = RLIM_INFINITY;
104 } else {
105 if (getrlimit(rlimit_names[i].resource, &rlim))
106 return;
107
108 cur = rlim.rlim_cur;
109 max = rlim.rlim_max;
110
111 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
112 return;
113
114 rlim.rlim_cur = cur;
115 rlim.rlim_max = max;
116 }
117
118 setrlimit(rlimit_names[i].resource, &rlim);
119 return;
120 }
121 }
122
123 static void
124 instance_run(struct service_instance *in)
125 {
126 struct blobmsg_list_node *var;
127 struct blob_attr *cur;
128 char **argv;
129 int argc = 1; /* NULL terminated */
130 int rem, fd;
131
132 if (in->nice)
133 setpriority(PRIO_PROCESS, 0, in->nice);
134
135 blobmsg_for_each_attr(cur, in->command, rem)
136 argc++;
137
138 blobmsg_list_for_each(&in->env, var)
139 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
140
141 blobmsg_list_for_each(&in->limits, var)
142 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
143
144 argv = alloca(sizeof(char *) * argc);
145 argc = 0;
146
147 blobmsg_for_each_attr(cur, in->command, rem)
148 argv[argc++] = blobmsg_data(cur);
149
150 argv[argc] = NULL;
151 fd = open("/dev/null", O_RDWR);
152 if (fd > -1) {
153 dup2(fd, STDIN_FILENO);
154 dup2(fd, STDOUT_FILENO);
155 dup2(fd, STDERR_FILENO);
156 if (fd > STDERR_FILENO)
157 close(fd);
158 }
159 execvp(argv[0], argv);
160 exit(127);
161 }
162
163 void
164 instance_start(struct service_instance *in)
165 {
166 int pid;
167
168 if (in->proc.pending)
169 return;
170
171 in->restart = false;
172 in->halt = !in->respawn;
173
174 if (!in->valid)
175 return;
176
177 pid = fork();
178 if (pid < 0)
179 return;
180
181 if (!pid) {
182 uloop_done();
183 instance_run(in);
184 return;
185 }
186
187 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
188 in->proc.pid = pid;
189 clock_gettime(CLOCK_MONOTONIC, &in->start);
190 uloop_process_add(&in->proc);
191 service_event("instance.start", in->srv->name, in->name);
192 }
193
194 static void
195 instance_timeout(struct uloop_timeout *t)
196 {
197 struct service_instance *in;
198
199 in = container_of(t, struct service_instance, timeout);
200
201 if (!in->halt && (in->restart || in->respawn))
202 instance_start(in);
203 }
204
205 static void
206 instance_exit(struct uloop_process *p, int ret)
207 {
208 struct service_instance *in;
209 struct timespec tp;
210 long runtime;
211
212 in = container_of(p, struct service_instance, proc);
213
214 clock_gettime(CLOCK_MONOTONIC, &tp);
215 runtime = tp.tv_sec - in->start.tv_sec;
216
217 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
218 if (upgrade_running)
219 return;
220
221 uloop_timeout_cancel(&in->timeout);
222 if (in->halt) {
223 /* no action */
224 } else if (in->restart) {
225 instance_start(in);
226 } else if (in->respawn) {
227 if (runtime < in->respawn_threshold)
228 in->respawn_count++;
229 else
230 in->respawn_count = 0;
231 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
232 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
233 in->srv->name, in->name, in->respawn_count, runtime);
234 in->restart = in->respawn = 0;
235 in->halt = 1;
236 } else {
237 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
238 }
239 }
240 service_event("instance.stop", in->srv->name, in->name);
241 }
242
243 void
244 instance_stop(struct service_instance *in)
245 {
246 if (!in->proc.pending)
247 return;
248 in->halt = true;
249 in->restart = in->respawn = false;
250 kill(in->proc.pid, SIGTERM);
251 }
252
253 static void
254 instance_restart(struct service_instance *in)
255 {
256 if (!in->proc.pending)
257 return;
258 in->halt = false;
259 in->restart = true;
260 kill(in->proc.pid, SIGTERM);
261 }
262
263 static bool
264 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
265 {
266 if (!in->valid)
267 return true;
268
269 if (!blob_attr_equal(in->command, in_new->command))
270 return true;
271
272 if (!blobmsg_list_equal(&in->env, &in_new->env))
273 return true;
274
275 if (!blobmsg_list_equal(&in->data, &in_new->data))
276 return true;
277
278 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
279 return true;
280
281 if (!blobmsg_list_equal(&in->file, &in_new->file))
282 return true;
283
284 if (in->nice != in_new->nice)
285 return true;
286
287 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
288 return true;
289
290 return false;
291 }
292
293 static bool
294 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
295 {
296 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
297 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
298
299 return n1->ifindex == n2->ifindex;
300 }
301
302 static void
303 instance_netdev_update(struct blobmsg_list_node *l)
304 {
305 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
306
307 n->ifindex = if_nametoindex(n->node.avl.key);
308 }
309
310 static bool
311 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
312 {
313 struct instance_file *f1 = container_of(l1, struct instance_file, node);
314 struct instance_file *f2 = container_of(l2, struct instance_file, node);
315
316 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
317 }
318
319 static void
320 instance_file_update(struct blobmsg_list_node *l)
321 {
322 struct instance_file *f = container_of(l, struct instance_file, node);
323 md5_ctx_t md5;
324 char buf[256];
325 int len, fd;
326
327 memset(f->md5, 0, sizeof(f->md5));
328
329 fd = open(l->avl.key, O_RDONLY);
330 if (fd < 0)
331 return;
332
333 md5_begin(&md5);
334 do {
335 len = read(fd, buf, sizeof(buf));
336 if (len < 0) {
337 if (errno == EINTR)
338 continue;
339
340 break;
341 }
342 if (!len)
343 break;
344
345 md5_hash(buf, len, &md5);
346 } while(1);
347
348 md5_end(f->md5, &md5);
349 close(fd);
350 }
351
352 static bool
353 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
354 {
355 struct blobmsg_list_node *node;
356
357 if (!cur)
358 return true;
359
360 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
361 return false;
362
363 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
364 if (cb) {
365 blobmsg_list_for_each(l, node)
366 cb(node);
367 }
368 return true;
369 }
370
371 static bool
372 instance_config_parse(struct service_instance *in)
373 {
374 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
375 struct blob_attr *cur, *cur2;
376 int argc = 0;
377 int rem;
378
379 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
380 blobmsg_data(in->config), blobmsg_data_len(in->config));
381
382 cur = tb[INSTANCE_ATTR_COMMAND];
383 if (!cur)
384 return false;
385
386 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
387 return false;
388
389 blobmsg_for_each_attr(cur2, cur, rem) {
390 argc++;
391 break;
392 }
393 if (!argc)
394 return false;
395
396 in->command = cur;
397
398 if (tb[INSTANCE_ATTR_RESPAWN]) {
399 int i = 0;
400 uint32_t vals[3] = { 3600, 5, 5};
401
402 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
403 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
404 continue;
405 vals[i] = atoi(blobmsg_get_string(cur2));
406 i++;
407 }
408 in->respawn = true;
409 in->respawn_count = 0;
410 in->respawn_threshold = vals[0];
411 in->respawn_timeout = vals[1];
412 in->respawn_retry = vals[2];
413 }
414 if (tb[INSTANCE_ATTR_TRIGGER]) {
415 in->trigger = blob_memdup(tb[INSTANCE_ATTR_TRIGGER]);
416 if (!in->trigger)
417 return -1;
418 trigger_add(in->trigger, in);
419 }
420
421 if (tb[INSTANCE_ATTR_WATCH]) {
422 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
423 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
424 continue;
425 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
426 watch_add(blobmsg_get_string(cur2), in);
427 }
428 }
429
430 if ((cur = tb[INSTANCE_ATTR_NICE])) {
431 in->nice = (int8_t) blobmsg_get_u32(cur);
432 if (in->nice < -20 || in->nice > 20)
433 return false;
434 }
435
436 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
437 return false;
438
439 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
440 return false;
441
442 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
443 return false;
444
445 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
446 return false;
447
448 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
449 return false;
450
451 return true;
452 }
453
454 static void
455 instance_config_cleanup(struct service_instance *in)
456 {
457 blobmsg_list_free(&in->env);
458 blobmsg_list_free(&in->data);
459 blobmsg_list_free(&in->netdev);
460 blobmsg_list_free(&in->file);
461 blobmsg_list_free(&in->limits);
462 }
463
464 static void
465 instance_config_move(struct service_instance *in, struct service_instance *in_src)
466 {
467 instance_config_cleanup(in);
468 blobmsg_list_move(&in->env, &in_src->env);
469 blobmsg_list_move(&in->data, &in_src->data);
470 blobmsg_list_move(&in->netdev, &in_src->netdev);
471 blobmsg_list_move(&in->file, &in_src->file);
472 blobmsg_list_move(&in->limits, &in_src->limits);
473 in->trigger = in_src->trigger;
474 in->command = in_src->command;
475 in->name = in_src->name;
476 in->node.avl.key = in_src->node.avl.key;
477
478 free(in->config);
479 in->config = in_src->config;
480 in_src->config = NULL;
481 }
482
483 bool
484 instance_update(struct service_instance *in, struct service_instance *in_new)
485 {
486 bool changed = instance_config_changed(in, in_new);
487 bool running = in->proc.pending;
488
489 if (!changed && running)
490 return false;
491
492 if (!running) {
493 if (changed)
494 instance_config_move(in, in_new);
495 instance_start(in);
496 } else {
497 instance_restart(in);
498 instance_config_move(in, in_new);
499 /* restart happens in the child callback handler */
500 }
501 return true;
502 }
503
504 void
505 instance_free(struct service_instance *in)
506 {
507 uloop_process_delete(&in->proc);
508 uloop_timeout_cancel(&in->timeout);
509 trigger_del(in);
510 watch_del(in);
511 free(in->trigger);
512 instance_config_cleanup(in);
513 free(in->config);
514 free(in);
515 }
516
517 void
518 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
519 {
520 config = blob_memdup(config);
521 in->srv = s;
522 in->name = blobmsg_name(config);
523 in->config = config;
524 in->timeout.cb = instance_timeout;
525 in->proc.cb = instance_exit;
526
527 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
528 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
529 blobmsg_list_simple_init(&in->env);
530 blobmsg_list_simple_init(&in->data);
531 blobmsg_list_simple_init(&in->limits);
532 in->valid = instance_config_parse(in);
533 }
534
535 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
536 {
537 void *i;
538
539 i = blobmsg_open_table(b, in->name);
540 blobmsg_add_u8(b, "running", in->proc.pending);
541 if (in->proc.pending)
542 blobmsg_add_u32(b, "pid", in->proc.pid);
543 blobmsg_add_blob(b, in->command);
544
545 if (!avl_is_empty(&in->env.avl)) {
546 struct blobmsg_list_node *var;
547 void *e = blobmsg_open_table(b, "env");
548 blobmsg_list_for_each(&in->env, var)
549 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
550 blobmsg_close_table(b, e);
551 }
552
553 if (!avl_is_empty(&in->limits.avl)) {
554 struct blobmsg_list_node *var;
555 void *e = blobmsg_open_table(b, "limits");
556 blobmsg_list_for_each(&in->limits, var)
557 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
558 blobmsg_close_table(b, e);
559 }
560
561 if (in->respawn) {
562 void *r = blobmsg_open_table(b, "respawn");
563 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
564 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
565 blobmsg_add_u32(b, "retry", in->respawn_retry);
566 blobmsg_close_table(b, r);
567 }
568
569 if (verbose && in->trigger)
570 blobmsg_add_blob(b, in->trigger);
571
572 blobmsg_close_table(b, i);
573 }