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>
24 #include <libubox/md5.h>
33 INSTANCE_ATTR_COMMAND,
38 INSTANCE_ATTR_TRIGGER,
39 INSTANCE_ATTR_RESPAWN,
48 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
49 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
50 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
51 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
52 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
53 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
54 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
55 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
56 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
57 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
58 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
59 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
60 [INSTANCE_ATTR_USER] = { "user", BLOBMSG_TYPE_STRING },
63 struct instance_netdev {
64 struct blobmsg_list_node node;
68 struct instance_file {
69 struct blobmsg_list_node node;
78 static const struct rlimit_name rlimit_names[] = {
80 { "core", RLIMIT_CORE },
81 { "cpu", RLIMIT_CPU },
82 { "data", RLIMIT_DATA },
83 { "fsize", RLIMIT_FSIZE },
84 { "memlock", RLIMIT_MEMLOCK },
85 { "msgqueue", RLIMIT_MSGQUEUE },
86 { "nice", RLIMIT_NICE },
87 { "nofile", RLIMIT_NOFILE },
88 { "nproc", RLIMIT_NPROC },
89 { "rss", RLIMIT_RSS },
90 { "rtprio", RLIMIT_RTPRIO },
91 { "sigpending", RLIMIT_SIGPENDING },
92 { "stack", RLIMIT_STACK },
97 instance_limits(const char *limit, const char *value)
101 unsigned long cur, max;
103 for (i = 0; rlimit_names[i].name != NULL; i++) {
104 if (strcmp(rlimit_names[i].name, limit))
106 if (!strcmp(value, "unlimited")) {
107 rlim.rlim_cur = RLIM_INFINITY;
108 rlim.rlim_max = RLIM_INFINITY;
110 if (getrlimit(rlimit_names[i].resource, &rlim))
116 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
123 setrlimit(rlimit_names[i].resource, &rlim);
129 instance_run(struct service_instance *in)
131 struct blobmsg_list_node *var;
132 struct blob_attr *cur;
134 int argc = 1; /* NULL terminated */
138 setpriority(PRIO_PROCESS, 0, in->nice);
140 blobmsg_for_each_attr(cur, in->command, rem)
143 blobmsg_list_for_each(&in->env, var)
144 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
146 blobmsg_list_for_each(&in->limits, var)
147 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
149 argv = alloca(sizeof(char *) * argc);
152 blobmsg_for_each_attr(cur, in->command, rem)
153 argv[argc++] = blobmsg_data(cur);
156 fd = open("/dev/null", O_RDWR);
158 dup2(fd, STDIN_FILENO);
159 dup2(fd, STDOUT_FILENO);
160 dup2(fd, STDERR_FILENO);
161 if (fd > STDERR_FILENO)
164 if (in->uid || in->gid) {
168 execvp(argv[0], argv);
173 instance_start(struct service_instance *in)
177 if (!avl_is_empty(&in->errors.avl)) {
178 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
182 if (in->proc.pending)
186 in->halt = !in->respawn;
201 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
203 clock_gettime(CLOCK_MONOTONIC, &in->start);
204 uloop_process_add(&in->proc);
205 service_event("instance.start", in->srv->name, in->name);
209 instance_timeout(struct uloop_timeout *t)
211 struct service_instance *in;
213 in = container_of(t, struct service_instance, timeout);
215 if (!in->halt && (in->restart || in->respawn))
220 instance_exit(struct uloop_process *p, int ret)
222 struct service_instance *in;
226 in = container_of(p, struct service_instance, proc);
228 clock_gettime(CLOCK_MONOTONIC, &tp);
229 runtime = tp.tv_sec - in->start.tv_sec;
231 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
235 uloop_timeout_cancel(&in->timeout);
238 } else if (in->restart) {
240 } else if (in->respawn) {
241 if (runtime < in->respawn_threshold)
244 in->respawn_count = 0;
245 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
246 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
247 in->srv->name, in->name, in->respawn_count, runtime);
248 in->restart = in->respawn = 0;
251 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
254 service_event("instance.stop", in->srv->name, in->name);
258 instance_stop(struct service_instance *in)
260 if (!in->proc.pending)
263 in->restart = in->respawn = false;
264 kill(in->proc.pid, SIGTERM);
268 instance_restart(struct service_instance *in)
270 if (!in->proc.pending)
274 kill(in->proc.pid, SIGTERM);
278 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
283 if (!blob_attr_equal(in->command, in_new->command))
286 if (!blobmsg_list_equal(&in->env, &in_new->env))
289 if (!blobmsg_list_equal(&in->data, &in_new->data))
292 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
295 if (!blobmsg_list_equal(&in->file, &in_new->file))
298 if (in->nice != in_new->nice)
301 if (in->uid != in_new->uid)
304 if (in->gid != in_new->gid)
307 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
310 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
317 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
319 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
320 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
322 return n1->ifindex == n2->ifindex;
326 instance_netdev_update(struct blobmsg_list_node *l)
328 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
330 n->ifindex = if_nametoindex(n->node.avl.key);
334 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
336 struct instance_file *f1 = container_of(l1, struct instance_file, node);
337 struct instance_file *f2 = container_of(l2, struct instance_file, node);
339 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
343 instance_file_update(struct blobmsg_list_node *l)
345 struct instance_file *f = container_of(l, struct instance_file, node);
350 memset(f->md5, 0, sizeof(f->md5));
352 fd = open(l->avl.key, O_RDONLY);
358 len = read(fd, buf, sizeof(buf));
368 md5_hash(buf, len, &md5);
371 md5_end(f->md5, &md5);
376 instance_fill_any(struct blobmsg_list *l, struct blob_attr *cur)
381 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), false);
385 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
387 struct blobmsg_list_node *node;
392 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
395 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
397 blobmsg_list_for_each(l, node)
404 instance_config_parse(struct service_instance *in)
406 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
407 struct blob_attr *cur, *cur2;
411 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
412 blobmsg_data(in->config), blobmsg_data_len(in->config));
414 cur = tb[INSTANCE_ATTR_COMMAND];
418 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
421 blobmsg_for_each_attr(cur2, cur, rem) {
430 if (tb[INSTANCE_ATTR_RESPAWN]) {
432 uint32_t vals[3] = { 3600, 5, 5};
434 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
435 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
437 vals[i] = atoi(blobmsg_get_string(cur2));
441 in->respawn_count = 0;
442 in->respawn_threshold = vals[0];
443 in->respawn_timeout = vals[1];
444 in->respawn_retry = vals[2];
446 if (tb[INSTANCE_ATTR_TRIGGER]) {
447 in->trigger = tb[INSTANCE_ATTR_TRIGGER];
448 trigger_add(in->trigger, in);
451 if (tb[INSTANCE_ATTR_WATCH]) {
452 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
453 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
455 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
456 watch_add(blobmsg_get_string(cur2), in);
460 if ((cur = tb[INSTANCE_ATTR_NICE])) {
461 in->nice = (int8_t) blobmsg_get_u32(cur);
462 if (in->nice < -20 || in->nice > 20)
466 if (tb[INSTANCE_ATTR_USER]) {
467 struct passwd *p = getpwnam(blobmsg_get_string(tb[INSTANCE_ATTR_USER]));
474 instance_fill_any(&in->data, tb[INSTANCE_ATTR_DATA]);
476 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
479 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
482 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
485 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
488 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
495 instance_config_cleanup(struct service_instance *in)
497 blobmsg_list_free(&in->env);
498 blobmsg_list_free(&in->data);
499 blobmsg_list_free(&in->netdev);
500 blobmsg_list_free(&in->file);
501 blobmsg_list_free(&in->limits);
502 blobmsg_list_free(&in->errors);
506 instance_config_move(struct service_instance *in, struct service_instance *in_src)
508 instance_config_cleanup(in);
509 blobmsg_list_move(&in->env, &in_src->env);
510 blobmsg_list_move(&in->data, &in_src->data);
511 blobmsg_list_move(&in->netdev, &in_src->netdev);
512 blobmsg_list_move(&in->file, &in_src->file);
513 blobmsg_list_move(&in->limits, &in_src->limits);
514 blobmsg_list_move(&in->errors, &in_src->errors);
515 in->trigger = in_src->trigger;
516 in->command = in_src->command;
517 in->name = in_src->name;
518 in->node.avl.key = in_src->node.avl.key;
521 in->config = in_src->config;
522 in_src->config = NULL;
526 instance_update(struct service_instance *in, struct service_instance *in_new)
528 bool changed = instance_config_changed(in, in_new);
529 bool running = in->proc.pending;
531 if (!changed && running)
536 instance_config_move(in, in_new);
539 instance_restart(in);
540 instance_config_move(in, in_new);
541 /* restart happens in the child callback handler */
547 instance_free(struct service_instance *in)
549 uloop_process_delete(&in->proc);
550 uloop_timeout_cancel(&in->timeout);
553 instance_config_cleanup(in);
559 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
561 config = blob_memdup(config);
563 in->name = blobmsg_name(config);
565 in->timeout.cb = instance_timeout;
566 in->proc.cb = instance_exit;
568 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
569 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
570 blobmsg_list_simple_init(&in->env);
571 blobmsg_list_simple_init(&in->data);
572 blobmsg_list_simple_init(&in->limits);
573 blobmsg_list_simple_init(&in->errors);
574 in->valid = instance_config_parse(in);
577 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
581 i = blobmsg_open_table(b, in->name);
582 blobmsg_add_u8(b, "running", in->proc.pending);
583 if (in->proc.pending)
584 blobmsg_add_u32(b, "pid", in->proc.pid);
585 blobmsg_add_blob(b, in->command);
587 if (!avl_is_empty(&in->errors.avl)) {
588 struct blobmsg_list_node *var;
589 void *e = blobmsg_open_array(b, "errors");
590 blobmsg_list_for_each(&in->errors, var)
591 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
592 blobmsg_close_table(b, e);
595 if (!avl_is_empty(&in->env.avl)) {
596 struct blobmsg_list_node *var;
597 void *e = blobmsg_open_table(b, "env");
598 blobmsg_list_for_each(&in->env, var)
599 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
600 blobmsg_close_table(b, e);
603 if (!avl_is_empty(&in->data.avl)) {
604 struct blobmsg_list_node *var;
605 void *e = blobmsg_open_table(b, "data");
606 blobmsg_list_for_each(&in->data, var)
607 blobmsg_add_blob(b, var->data);
608 blobmsg_close_table(b, e);
611 if (!avl_is_empty(&in->limits.avl)) {
612 struct blobmsg_list_node *var;
613 void *e = blobmsg_open_table(b, "limits");
614 blobmsg_list_for_each(&in->limits, var)
615 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
616 blobmsg_close_table(b, e);
620 void *r = blobmsg_open_table(b, "respawn");
621 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
622 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
623 blobmsg_add_u32(b, "retry", in->respawn_retry);
624 blobmsg_close_table(b, r);
627 if (verbose && in->trigger)
628 blobmsg_add_blob(b, in->trigger);
630 blobmsg_close_table(b, i);