2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/resource.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
23 #include <libubox/md5.h>
32 INSTANCE_ATTR_COMMAND,
37 INSTANCE_ATTR_TRIGGER,
38 INSTANCE_ATTR_RESPAWN,
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 },
58 struct instance_netdev {
59 struct blobmsg_list_node node;
63 struct instance_file {
64 struct blobmsg_list_node node;
73 static const struct rlimit_name rlimit_names[] = {
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 },
92 instance_limits(const char *limit, const char *value)
96 unsigned long cur, max;
98 for (i = 0; rlimit_names[i].name != NULL; i++) {
99 if (strcmp(rlimit_names[i].name, limit))
101 if (!strcmp(value, "unlimited")) {
102 rlim.rlim_cur = RLIM_INFINITY;
103 rlim.rlim_max = RLIM_INFINITY;
105 if (getrlimit(rlimit_names[i].resource, &rlim))
111 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
118 setrlimit(rlimit_names[i].resource, &rlim);
124 instance_run(struct service_instance *in)
126 struct blobmsg_list_node *var;
127 struct blob_attr *cur;
129 int argc = 1; /* NULL terminated */
133 setpriority(PRIO_PROCESS, 0, in->nice);
135 blobmsg_for_each_attr(cur, in->command, rem)
138 blobmsg_list_for_each(&in->env, var)
139 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
141 blobmsg_list_for_each(&in->limits, var)
142 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
144 argv = alloca(sizeof(char *) * argc);
147 blobmsg_for_each_attr(cur, in->command, rem)
148 argv[argc++] = blobmsg_data(cur);
151 fd = open("/dev/null", O_RDWR);
153 dup2(fd, STDIN_FILENO);
154 dup2(fd, STDOUT_FILENO);
155 dup2(fd, STDERR_FILENO);
156 if (fd > STDERR_FILENO)
159 execvp(argv[0], argv);
164 instance_start(struct service_instance *in)
168 if (in->proc.pending)
172 in->halt = !in->respawn;
187 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
189 clock_gettime(CLOCK_MONOTONIC, &in->start);
190 uloop_process_add(&in->proc);
191 service_event("instance.start", in->srv->name, in->name);
195 instance_timeout(struct uloop_timeout *t)
197 struct service_instance *in;
199 in = container_of(t, struct service_instance, timeout);
201 if (!in->halt && (in->restart || in->respawn))
206 instance_exit(struct uloop_process *p, int ret)
208 struct service_instance *in;
212 in = container_of(p, struct service_instance, proc);
214 clock_gettime(CLOCK_MONOTONIC, &tp);
215 runtime = tp.tv_sec - in->start.tv_sec;
217 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
221 uloop_timeout_cancel(&in->timeout);
224 } else if (in->restart) {
226 } else if (in->respawn) {
227 if (runtime < in->respawn_threshold)
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;
237 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
240 service_event("instance.stop", in->srv->name, in->name);
244 instance_stop(struct service_instance *in)
246 if (!in->proc.pending)
249 in->restart = in->respawn = false;
250 kill(in->proc.pid, SIGTERM);
254 instance_restart(struct service_instance *in)
256 if (!in->proc.pending)
260 kill(in->proc.pid, SIGTERM);
264 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
269 if (!blob_attr_equal(in->command, in_new->command))
272 if (!blobmsg_list_equal(&in->env, &in_new->env))
275 if (!blobmsg_list_equal(&in->data, &in_new->data))
278 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
281 if (!blobmsg_list_equal(&in->file, &in_new->file))
284 if (in->nice != in_new->nice)
287 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
294 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
296 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
297 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
299 return n1->ifindex == n2->ifindex;
303 instance_netdev_update(struct blobmsg_list_node *l)
305 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
307 n->ifindex = if_nametoindex(n->node.avl.key);
311 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
313 struct instance_file *f1 = container_of(l1, struct instance_file, node);
314 struct instance_file *f2 = container_of(l2, struct instance_file, node);
316 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
320 instance_file_update(struct blobmsg_list_node *l)
322 struct instance_file *f = container_of(l, struct instance_file, node);
327 memset(f->md5, 0, sizeof(f->md5));
329 fd = open(l->avl.key, O_RDONLY);
335 len = read(fd, buf, sizeof(buf));
345 md5_hash(buf, len, &md5);
348 md5_end(f->md5, &md5);
353 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
355 struct blobmsg_list_node *node;
360 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
363 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
365 blobmsg_list_for_each(l, node)
372 instance_config_parse(struct service_instance *in)
374 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
375 struct blob_attr *cur, *cur2;
379 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
380 blobmsg_data(in->config), blobmsg_data_len(in->config));
382 cur = tb[INSTANCE_ATTR_COMMAND];
386 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
389 blobmsg_for_each_attr(cur2, cur, rem) {
398 if (tb[INSTANCE_ATTR_RESPAWN]) {
400 uint32_t vals[3] = { 3600, 5, 5};
402 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
403 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
405 vals[i] = atoi(blobmsg_get_string(cur2));
409 in->respawn_count = 0;
410 in->respawn_threshold = vals[0];
411 in->respawn_timeout = vals[1];
412 in->respawn_retry = vals[2];
414 if (tb[INSTANCE_ATTR_TRIGGER]) {
415 in->trigger = blob_memdup(tb[INSTANCE_ATTR_TRIGGER]);
418 trigger_add(in->trigger, in);
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)
425 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
426 watch_add(blobmsg_get_string(cur2), in);
430 if ((cur = tb[INSTANCE_ATTR_NICE])) {
431 in->nice = (int8_t) blobmsg_get_u32(cur);
432 if (in->nice < -20 || in->nice > 20)
436 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
439 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
442 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
445 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
448 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
455 instance_config_cleanup(struct service_instance *in)
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);
465 instance_config_move(struct service_instance *in, struct service_instance *in_src)
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;
479 in->config = in_src->config;
480 in_src->config = NULL;
484 instance_update(struct service_instance *in, struct service_instance *in_new)
486 bool changed = instance_config_changed(in, in_new);
487 bool running = in->proc.pending;
489 if (!changed && running)
494 instance_config_move(in, in_new);
497 instance_restart(in);
498 instance_config_move(in, in_new);
499 /* restart happens in the child callback handler */
505 instance_free(struct service_instance *in)
507 uloop_process_delete(&in->proc);
508 uloop_timeout_cancel(&in->timeout);
512 instance_config_cleanup(in);
518 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
520 config = blob_memdup(config);
522 in->name = blobmsg_name(config);
524 in->timeout.cb = instance_timeout;
525 in->proc.cb = instance_exit;
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);
535 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
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);
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);
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);
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);
569 if (verbose && in->trigger)
570 blobmsg_add_blob(b, in->trigger);
572 blobmsg_close_table(b, i);