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,
46 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
47 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
48 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
49 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
50 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
51 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
52 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
53 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
54 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
55 [INSTANCE_ATTR_LIMITS] = { "limits", BLOBMSG_TYPE_TABLE },
56 [INSTANCE_ATTR_WATCH] = { "watch", BLOBMSG_TYPE_ARRAY },
57 [INSTANCE_ATTR_ERROR] = { "error", BLOBMSG_TYPE_ARRAY },
60 struct instance_netdev {
61 struct blobmsg_list_node node;
65 struct instance_file {
66 struct blobmsg_list_node node;
75 static const struct rlimit_name rlimit_names[] = {
77 { "core", RLIMIT_CORE },
78 { "cpu", RLIMIT_CPU },
79 { "data", RLIMIT_DATA },
80 { "fsize", RLIMIT_FSIZE },
81 { "memlock", RLIMIT_MEMLOCK },
82 { "msgqueue", RLIMIT_MSGQUEUE },
83 { "nice", RLIMIT_NICE },
84 { "nofile", RLIMIT_NOFILE },
85 { "nproc", RLIMIT_NPROC },
86 { "rss", RLIMIT_RSS },
87 { "rtprio", RLIMIT_RTPRIO },
88 { "sigpending", RLIMIT_SIGPENDING },
89 { "stack", RLIMIT_STACK },
94 instance_limits(const char *limit, const char *value)
98 unsigned long cur, max;
100 for (i = 0; rlimit_names[i].name != NULL; i++) {
101 if (strcmp(rlimit_names[i].name, limit))
103 if (!strcmp(value, "unlimited")) {
104 rlim.rlim_cur = RLIM_INFINITY;
105 rlim.rlim_max = RLIM_INFINITY;
107 if (getrlimit(rlimit_names[i].resource, &rlim))
113 if (sscanf(value, "%lu %lu", &cur, &max) < 1)
120 setrlimit(rlimit_names[i].resource, &rlim);
126 instance_run(struct service_instance *in)
128 struct blobmsg_list_node *var;
129 struct blob_attr *cur;
131 int argc = 1; /* NULL terminated */
135 setpriority(PRIO_PROCESS, 0, in->nice);
137 blobmsg_for_each_attr(cur, in->command, rem)
140 blobmsg_list_for_each(&in->env, var)
141 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
143 blobmsg_list_for_each(&in->limits, var)
144 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
146 argv = alloca(sizeof(char *) * argc);
149 blobmsg_for_each_attr(cur, in->command, rem)
150 argv[argc++] = blobmsg_data(cur);
153 fd = open("/dev/null", O_RDWR);
155 dup2(fd, STDIN_FILENO);
156 dup2(fd, STDOUT_FILENO);
157 dup2(fd, STDERR_FILENO);
158 if (fd > STDERR_FILENO)
161 execvp(argv[0], argv);
166 instance_start(struct service_instance *in)
170 if (!avl_is_empty(&in->errors.avl)) {
171 LOG("Not starting instance %s::%s, an error was indicated\n", in->srv->name, in->name);
175 if (in->proc.pending)
179 in->halt = !in->respawn;
194 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
196 clock_gettime(CLOCK_MONOTONIC, &in->start);
197 uloop_process_add(&in->proc);
198 service_event("instance.start", in->srv->name, in->name);
202 instance_timeout(struct uloop_timeout *t)
204 struct service_instance *in;
206 in = container_of(t, struct service_instance, timeout);
208 if (!in->halt && (in->restart || in->respawn))
213 instance_exit(struct uloop_process *p, int ret)
215 struct service_instance *in;
219 in = container_of(p, struct service_instance, proc);
221 clock_gettime(CLOCK_MONOTONIC, &tp);
222 runtime = tp.tv_sec - in->start.tv_sec;
224 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
228 uloop_timeout_cancel(&in->timeout);
231 } else if (in->restart) {
233 } else if (in->respawn) {
234 if (runtime < in->respawn_threshold)
237 in->respawn_count = 0;
238 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
239 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
240 in->srv->name, in->name, in->respawn_count, runtime);
241 in->restart = in->respawn = 0;
244 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
247 service_event("instance.stop", in->srv->name, in->name);
251 instance_stop(struct service_instance *in)
253 if (!in->proc.pending)
256 in->restart = in->respawn = false;
257 kill(in->proc.pid, SIGTERM);
261 instance_restart(struct service_instance *in)
263 if (!in->proc.pending)
267 kill(in->proc.pid, SIGTERM);
271 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
276 if (!blob_attr_equal(in->command, in_new->command))
279 if (!blobmsg_list_equal(&in->env, &in_new->env))
282 if (!blobmsg_list_equal(&in->data, &in_new->data))
285 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
288 if (!blobmsg_list_equal(&in->file, &in_new->file))
291 if (in->nice != in_new->nice)
294 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
297 if (!blobmsg_list_equal(&in->errors, &in_new->errors))
304 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
306 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
307 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
309 return n1->ifindex == n2->ifindex;
313 instance_netdev_update(struct blobmsg_list_node *l)
315 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
317 n->ifindex = if_nametoindex(n->node.avl.key);
321 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
323 struct instance_file *f1 = container_of(l1, struct instance_file, node);
324 struct instance_file *f2 = container_of(l2, struct instance_file, node);
326 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
330 instance_file_update(struct blobmsg_list_node *l)
332 struct instance_file *f = container_of(l, struct instance_file, node);
337 memset(f->md5, 0, sizeof(f->md5));
339 fd = open(l->avl.key, O_RDONLY);
345 len = read(fd, buf, sizeof(buf));
355 md5_hash(buf, len, &md5);
358 md5_end(f->md5, &md5);
363 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
365 struct blobmsg_list_node *node;
370 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
373 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
375 blobmsg_list_for_each(l, node)
382 instance_config_parse(struct service_instance *in)
384 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
385 struct blob_attr *cur, *cur2;
389 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
390 blobmsg_data(in->config), blobmsg_data_len(in->config));
392 cur = tb[INSTANCE_ATTR_COMMAND];
396 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
399 blobmsg_for_each_attr(cur2, cur, rem) {
408 if (tb[INSTANCE_ATTR_RESPAWN]) {
410 uint32_t vals[3] = { 3600, 5, 5};
412 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
413 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
415 vals[i] = atoi(blobmsg_get_string(cur2));
419 in->respawn_count = 0;
420 in->respawn_threshold = vals[0];
421 in->respawn_timeout = vals[1];
422 in->respawn_retry = vals[2];
424 if (tb[INSTANCE_ATTR_TRIGGER]) {
425 in->trigger = blob_memdup(tb[INSTANCE_ATTR_TRIGGER]);
428 trigger_add(in->trigger, in);
431 if (tb[INSTANCE_ATTR_WATCH]) {
432 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_WATCH], rem) {
433 if (blobmsg_type(cur2) != BLOBMSG_TYPE_STRING)
435 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2));
436 watch_add(blobmsg_get_string(cur2), in);
440 if ((cur = tb[INSTANCE_ATTR_NICE])) {
441 in->nice = (int8_t) blobmsg_get_u32(cur);
442 if (in->nice < -20 || in->nice > 20)
446 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
449 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
452 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
455 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
458 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
461 if (!instance_fill_array(&in->errors, tb[INSTANCE_ATTR_ERROR], NULL, true))
468 instance_config_cleanup(struct service_instance *in)
470 blobmsg_list_free(&in->env);
471 blobmsg_list_free(&in->data);
472 blobmsg_list_free(&in->netdev);
473 blobmsg_list_free(&in->file);
474 blobmsg_list_free(&in->limits);
475 blobmsg_list_free(&in->errors);
479 instance_config_move(struct service_instance *in, struct service_instance *in_src)
481 instance_config_cleanup(in);
482 blobmsg_list_move(&in->env, &in_src->env);
483 blobmsg_list_move(&in->data, &in_src->data);
484 blobmsg_list_move(&in->netdev, &in_src->netdev);
485 blobmsg_list_move(&in->file, &in_src->file);
486 blobmsg_list_move(&in->limits, &in_src->limits);
487 blobmsg_list_move(&in->errors, &in_src->errors);
488 in->trigger = in_src->trigger;
489 in->command = in_src->command;
490 in->name = in_src->name;
491 in->node.avl.key = in_src->node.avl.key;
494 in->config = in_src->config;
495 in_src->config = NULL;
499 instance_update(struct service_instance *in, struct service_instance *in_new)
501 bool changed = instance_config_changed(in, in_new);
502 bool running = in->proc.pending;
504 if (!changed && running)
509 instance_config_move(in, in_new);
512 instance_restart(in);
513 instance_config_move(in, in_new);
514 /* restart happens in the child callback handler */
520 instance_free(struct service_instance *in)
522 uloop_process_delete(&in->proc);
523 uloop_timeout_cancel(&in->timeout);
527 instance_config_cleanup(in);
533 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
535 config = blob_memdup(config);
537 in->name = blobmsg_name(config);
539 in->timeout.cb = instance_timeout;
540 in->proc.cb = instance_exit;
542 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
543 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
544 blobmsg_list_simple_init(&in->env);
545 blobmsg_list_simple_init(&in->data);
546 blobmsg_list_simple_init(&in->limits);
547 blobmsg_list_simple_init(&in->errors);
548 in->valid = instance_config_parse(in);
551 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
555 i = blobmsg_open_table(b, in->name);
556 blobmsg_add_u8(b, "running", in->proc.pending);
557 if (in->proc.pending)
558 blobmsg_add_u32(b, "pid", in->proc.pid);
559 blobmsg_add_blob(b, in->command);
561 if (!avl_is_empty(&in->errors.avl)) {
562 struct blobmsg_list_node *var;
563 void *e = blobmsg_open_array(b, "errors");
564 blobmsg_list_for_each(&in->errors, var)
565 blobmsg_add_string(b, NULL, blobmsg_data(var->data));
566 blobmsg_close_table(b, e);
569 if (!avl_is_empty(&in->env.avl)) {
570 struct blobmsg_list_node *var;
571 void *e = blobmsg_open_table(b, "env");
572 blobmsg_list_for_each(&in->env, var)
573 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
574 blobmsg_close_table(b, e);
577 if (!avl_is_empty(&in->limits.avl)) {
578 struct blobmsg_list_node *var;
579 void *e = blobmsg_open_table(b, "limits");
580 blobmsg_list_for_each(&in->limits, var)
581 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
582 blobmsg_close_table(b, e);
586 void *r = blobmsg_open_table(b, "respawn");
587 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
588 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
589 blobmsg_add_u32(b, "retry", in->respawn_retry);
590 blobmsg_close_table(b, r);
593 if (verbose && in->trigger)
594 blobmsg_add_blob(b, in->trigger);
596 blobmsg_close_table(b, i);