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,
44 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
45 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
46 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
47 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
48 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
49 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
50 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
51 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
52 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
53 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
56 struct instance_netdev {
57 struct blobmsg_list_node node;
61 struct instance_file {
62 struct blobmsg_list_node node;
71 static const struct rlimit_name rlimit_names[] = {
73 { "core", RLIMIT_CORE },
74 { "cpu", RLIMIT_CPU },
75 { "data", RLIMIT_DATA },
76 { "fsize", RLIMIT_FSIZE },
77 { "memlock", RLIMIT_MEMLOCK },
78 { "msgqueue", RLIMIT_MSGQUEUE },
79 { "nice", RLIMIT_NICE },
80 { "nofile", RLIMIT_NOFILE },
81 { "nproc", RLIMIT_NPROC },
82 { "rss", RLIMIT_RSS },
83 { "rtprio", RLIMIT_RTPRIO },
84 { "sigpending", RLIMIT_SIGPENDING },
85 { "stack", RLIMIT_STACK },
90 instance_limits(const char *limit, const char *value)
94 unsigned long cur, max;
96 for (i = 0; rlimit_names[i].name != NULL; i++) {
97 if (strcmp(rlimit_names[i].name, limit))
99 if (!strcmp(value, "unlimited")) {
100 rlim.rlim_cur = RLIM_INFINITY;
101 rlim.rlim_max = RLIM_INFINITY;
103 if (getrlimit(rlimit_names[i].resource, &rlim))
109 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
116 setrlimit(rlimit_names[i].resource, &rlim);
122 instance_run(struct service_instance *in)
124 struct blobmsg_list_node *var;
125 struct blob_attr *cur;
127 int argc = 1; /* NULL terminated */
131 setpriority(PRIO_PROCESS, 0, in->nice);
133 blobmsg_for_each_attr(cur, in->command, rem)
136 blobmsg_list_for_each(&in->env, var)
137 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
139 blobmsg_list_for_each(&in->limits, var)
140 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
142 argv = alloca(sizeof(char *) * argc);
145 blobmsg_for_each_attr(cur, in->command, rem)
146 argv[argc++] = blobmsg_data(cur);
149 fd = open("/dev/null", O_RDWR);
151 dup2(fd, STDIN_FILENO);
152 dup2(fd, STDOUT_FILENO);
153 dup2(fd, STDERR_FILENO);
154 if (fd > STDERR_FILENO)
157 execvp(argv[0], argv);
162 instance_start(struct service_instance *in)
166 if (in->proc.pending)
170 in->halt = !in->respawn;
185 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
187 clock_gettime(CLOCK_MONOTONIC, &in->start);
188 uloop_process_add(&in->proc);
192 instance_timeout(struct uloop_timeout *t)
194 struct service_instance *in;
196 in = container_of(t, struct service_instance, timeout);
198 if (!in->halt && (in->restart || in->respawn))
203 instance_exit(struct uloop_process *p, int ret)
205 struct service_instance *in;
209 in = container_of(p, struct service_instance, proc);
211 clock_gettime(CLOCK_MONOTONIC, &tp);
212 runtime = tp.tv_sec - in->start.tv_sec;
214 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
218 uloop_timeout_cancel(&in->timeout);
221 } else if (in->restart) {
223 } else if (in->respawn) {
224 if (runtime < in->respawn_threshold)
227 in->respawn_count = 0;
228 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
229 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
230 in->srv->name, in->name, in->respawn_count, runtime);
231 in->restart = in->respawn = 0;
234 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
240 instance_stop(struct service_instance *in)
242 if (!in->proc.pending)
245 in->restart = in->respawn = false;
246 kill(in->proc.pid, SIGTERM);
250 instance_restart(struct service_instance *in)
252 if (!in->proc.pending)
256 kill(in->proc.pid, SIGTERM);
260 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
265 if (!blob_attr_equal(in->command, in_new->command))
268 if (!blobmsg_list_equal(&in->env, &in_new->env))
271 if (!blobmsg_list_equal(&in->data, &in_new->data))
274 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
277 if (!blobmsg_list_equal(&in->file, &in_new->file))
280 if (in->nice != in_new->nice)
283 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
290 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
292 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
293 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
295 return n1->ifindex == n2->ifindex;
299 instance_netdev_update(struct blobmsg_list_node *l)
301 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
303 n->ifindex = if_nametoindex(n->node.avl.key);
307 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
309 struct instance_file *f1 = container_of(l1, struct instance_file, node);
310 struct instance_file *f2 = container_of(l2, struct instance_file, node);
312 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
316 instance_file_update(struct blobmsg_list_node *l)
318 struct instance_file *f = container_of(l, struct instance_file, node);
323 memset(f->md5, 0, sizeof(f->md5));
325 fd = open(l->avl.key, O_RDONLY);
331 len = read(fd, buf, sizeof(buf));
341 md5_hash(buf, len, &md5);
344 md5_end(f->md5, &md5);
349 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
351 struct blobmsg_list_node *node;
356 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
359 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
361 blobmsg_list_for_each(l, node)
368 instance_config_parse(struct service_instance *in)
370 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
371 struct blob_attr *cur, *cur2;
375 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
376 blobmsg_data(in->config), blobmsg_data_len(in->config));
378 cur = tb[INSTANCE_ATTR_COMMAND];
382 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
385 blobmsg_for_each_attr(cur2, cur, rem) {
394 if (tb[INSTANCE_ATTR_RESPAWN]) {
396 uint32_t vals[3] = { 3600, 5, 5};
398 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
399 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
401 vals[i] = atoi(blobmsg_get_string(cur2));
405 in->respawn_count = 0;
406 in->respawn_threshold = vals[0];
407 in->respawn_timeout = vals[1];
408 in->respawn_retry = vals[2];
410 if (tb[INSTANCE_ATTR_TRIGGER]) {
411 in->trigger = blob_memdup(tb[INSTANCE_ATTR_TRIGGER]);
414 trigger_add(in->trigger, in);
417 if ((cur = tb[INSTANCE_ATTR_NICE])) {
418 in->nice = (int8_t) blobmsg_get_u32(cur);
419 if (in->nice < -20 || in->nice > 20)
423 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
426 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
429 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
432 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
435 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
442 instance_config_cleanup(struct service_instance *in)
444 blobmsg_list_free(&in->env);
445 blobmsg_list_free(&in->data);
446 blobmsg_list_free(&in->netdev);
447 blobmsg_list_free(&in->file);
448 blobmsg_list_free(&in->limits);
452 instance_config_move(struct service_instance *in, struct service_instance *in_src)
454 instance_config_cleanup(in);
455 blobmsg_list_move(&in->env, &in_src->env);
456 blobmsg_list_move(&in->data, &in_src->data);
457 blobmsg_list_move(&in->netdev, &in_src->netdev);
458 blobmsg_list_move(&in->file, &in_src->file);
459 blobmsg_list_move(&in->limits, &in_src->limits);
460 in->trigger = in_src->trigger;
461 in->command = in_src->command;
462 in->name = in_src->name;
463 in->node.avl.key = in_src->node.avl.key;
466 in->config = in_src->config;
467 in_src->config = NULL;
471 instance_update(struct service_instance *in, struct service_instance *in_new)
473 bool changed = instance_config_changed(in, in_new);
474 bool running = in->proc.pending;
476 if (!changed && running)
481 instance_config_move(in, in_new);
484 instance_restart(in);
485 instance_config_move(in, in_new);
486 /* restart happens in the child callback handler */
492 instance_free(struct service_instance *in)
494 uloop_process_delete(&in->proc);
495 uloop_timeout_cancel(&in->timeout);
498 instance_config_cleanup(in);
504 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
506 config = blob_memdup(config);
508 in->name = blobmsg_name(config);
510 in->timeout.cb = instance_timeout;
511 in->proc.cb = instance_exit;
513 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
514 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
515 blobmsg_list_simple_init(&in->env);
516 blobmsg_list_simple_init(&in->data);
517 blobmsg_list_simple_init(&in->limits);
518 in->valid = instance_config_parse(in);
521 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
525 i = blobmsg_open_table(b, in->name);
526 blobmsg_add_u8(b, "running", in->proc.pending);
527 if (in->proc.pending)
528 blobmsg_add_u32(b, "pid", in->proc.pid);
529 blobmsg_add_blob(b, in->command);
531 if (!avl_is_empty(&in->env.avl)) {
532 struct blobmsg_list_node *var;
533 void *e = blobmsg_open_table(b, "env");
534 blobmsg_list_for_each(&in->env, var)
535 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
536 blobmsg_close_table(b, e);
539 if (!avl_is_empty(&in->limits.avl)) {
540 struct blobmsg_list_node *var;
541 void *e = blobmsg_open_table(b, "limits");
542 blobmsg_list_for_each(&in->limits, var)
543 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
544 blobmsg_close_table(b, e);
548 void *r = blobmsg_open_table(b, "respawn");
549 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
550 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
551 blobmsg_add_u32(b, "retry", in->respawn_retry);
552 blobmsg_close_table(b, r);
555 if (verbose && in->trigger)
556 blobmsg_add_blob(b, in->trigger);
558 blobmsg_close_table(b, i);