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)
95 for (i = 0; rlimit_names[i].name != NULL; i++) {
96 if (strcmp(rlimit_names[i].name, limit))
98 if (!strcmp(value, "unlimited")) {
99 rlim.rlim_cur = RLIM_INFINITY;
100 rlim.rlim_max = RLIM_INFINITY;
102 else if (getrlimit(rlimit_names[i].resource, &rlim) ||
103 sscanf(value, "%lu %lu", &rlim.rlim_cur, &rlim.rlim_max) == 0)
105 setrlimit(rlimit_names[i].resource, &rlim);
111 instance_run(struct service_instance *in)
113 struct blobmsg_list_node *var;
114 struct blob_attr *cur;
116 int argc = 1; /* NULL terminated */
120 setpriority(PRIO_PROCESS, 0, in->nice);
122 blobmsg_for_each_attr(cur, in->command, rem)
125 blobmsg_list_for_each(&in->env, var)
126 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
128 blobmsg_list_for_each(&in->limits, var)
129 instance_limits(blobmsg_name(var->data), blobmsg_data(var->data));
131 argv = alloca(sizeof(char *) * argc);
134 blobmsg_for_each_attr(cur, in->command, rem)
135 argv[argc++] = blobmsg_data(cur);
138 fd = open("/dev/null", O_RDWR);
140 dup2(fd, STDIN_FILENO);
141 dup2(fd, STDOUT_FILENO);
142 dup2(fd, STDERR_FILENO);
143 if (fd > STDERR_FILENO)
146 execvp(argv[0], argv);
151 instance_start(struct service_instance *in)
155 if (in->proc.pending)
159 in->halt = !in->respawn;
174 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
176 clock_gettime(CLOCK_MONOTONIC, &in->start);
177 uloop_process_add(&in->proc);
181 instance_timeout(struct uloop_timeout *t)
183 struct service_instance *in;
185 in = container_of(t, struct service_instance, timeout);
187 if (!in->halt && (in->restart || in->respawn))
192 instance_exit(struct uloop_process *p, int ret)
194 struct service_instance *in;
198 in = container_of(p, struct service_instance, proc);
200 clock_gettime(CLOCK_MONOTONIC, &tp);
201 runtime = tp.tv_sec - in->start.tv_sec;
203 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
207 uloop_timeout_cancel(&in->timeout);
210 } else if (in->restart) {
212 } else if (in->respawn) {
213 if (runtime < in->respawn_threshold)
216 in->respawn_count = 0;
217 if (in->respawn_count > in->respawn_retry && in->respawn_retry > 0 ) {
218 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
219 in->srv->name, in->name, in->respawn_count, runtime);
220 in->restart = in->respawn = 0;
223 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
229 instance_stop(struct service_instance *in)
231 if (!in->proc.pending)
234 in->restart = in->respawn = false;
235 kill(in->proc.pid, SIGTERM);
239 instance_restart(struct service_instance *in)
241 if (!in->proc.pending)
245 kill(in->proc.pid, SIGTERM);
249 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
254 if (!blob_attr_equal(in->command, in_new->command))
257 if (!blobmsg_list_equal(&in->env, &in_new->env))
260 if (!blobmsg_list_equal(&in->data, &in_new->data))
263 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
266 if (!blobmsg_list_equal(&in->file, &in_new->file))
269 if (in->nice != in_new->nice)
272 if (!blobmsg_list_equal(&in->limits, &in_new->limits))
279 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
281 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
282 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
284 return n1->ifindex == n2->ifindex;
288 instance_netdev_update(struct blobmsg_list_node *l)
290 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
292 n->ifindex = if_nametoindex(n->node.avl.key);
296 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
298 struct instance_file *f1 = container_of(l1, struct instance_file, node);
299 struct instance_file *f2 = container_of(l2, struct instance_file, node);
301 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
305 instance_file_update(struct blobmsg_list_node *l)
307 struct instance_file *f = container_of(l, struct instance_file, node);
312 memset(f->md5, 0, sizeof(f->md5));
314 fd = open(l->avl.key, O_RDONLY);
320 len = read(fd, buf, sizeof(buf));
330 md5_hash(buf, len, &md5);
333 md5_end(f->md5, &md5);
338 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
340 struct blobmsg_list_node *node;
345 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
348 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
350 blobmsg_list_for_each(l, node)
357 instance_config_parse(struct service_instance *in)
359 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
360 struct blob_attr *cur, *cur2;
364 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
365 blobmsg_data(in->config), blobmsg_data_len(in->config));
367 cur = tb[INSTANCE_ATTR_COMMAND];
371 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
374 blobmsg_for_each_attr(cur2, cur, rem) {
383 if (tb[INSTANCE_ATTR_RESPAWN]) {
385 uint32_t vals[3] = { 3600, 5, 5};
387 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
388 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
390 vals[i] = atoi(blobmsg_get_string(cur2));
394 in->respawn_count = 0;
395 in->respawn_threshold = vals[0];
396 in->respawn_timeout = vals[1];
397 in->respawn_retry = vals[2];
399 if (tb[INSTANCE_ATTR_TRIGGER]) {
400 in->trigger = malloc(blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
403 memcpy(in->trigger, tb[INSTANCE_ATTR_TRIGGER], blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
404 trigger_add(in->trigger, in);
407 if ((cur = tb[INSTANCE_ATTR_NICE])) {
408 in->nice = (int8_t) blobmsg_get_u32(cur);
409 if (in->nice < -20 || in->nice > 20)
413 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
416 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
419 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
422 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
425 if (!instance_fill_array(&in->limits, tb[INSTANCE_ATTR_LIMITS], NULL, false))
432 instance_config_cleanup(struct service_instance *in)
434 blobmsg_list_free(&in->env);
435 blobmsg_list_free(&in->data);
436 blobmsg_list_free(&in->netdev);
437 blobmsg_list_free(&in->limits);
441 instance_config_move(struct service_instance *in, struct service_instance *in_src)
443 instance_config_cleanup(in);
444 blobmsg_list_move(&in->env, &in_src->env);
445 blobmsg_list_move(&in->data, &in_src->data);
446 blobmsg_list_move(&in->netdev, &in_src->netdev);
447 blobmsg_list_move(&in->limits, &in_src->limits);
448 in->trigger = in_src->trigger;
449 in->command = in_src->command;
450 in->name = in_src->name;
451 in->node.avl.key = in_src->node.avl.key;
454 in->config = in_src->config;
455 in_src->config = NULL;
459 instance_update(struct service_instance *in, struct service_instance *in_new)
461 bool changed = instance_config_changed(in, in_new);
462 bool running = in->proc.pending;
464 if (!changed && running)
469 instance_config_move(in, in_new);
472 instance_restart(in);
473 instance_config_move(in, in_new);
474 /* restart happens in the child callback handler */
480 instance_free(struct service_instance *in)
482 uloop_process_delete(&in->proc);
483 uloop_timeout_cancel(&in->timeout);
486 instance_config_cleanup(in);
492 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
494 config = blob_memdup(config);
496 in->name = blobmsg_name(config);
498 in->timeout.cb = instance_timeout;
499 in->proc.cb = instance_exit;
501 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
502 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
503 blobmsg_list_simple_init(&in->env);
504 blobmsg_list_simple_init(&in->data);
505 blobmsg_list_simple_init(&in->limits);
506 in->valid = instance_config_parse(in);
509 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
513 i = blobmsg_open_table(b, in->name);
514 blobmsg_add_u8(b, "running", in->proc.pending);
515 if (in->proc.pending)
516 blobmsg_add_u32(b, "pid", in->proc.pid);
517 blobmsg_add_blob(b, in->command);
519 if (!avl_is_empty(&in->env.avl)) {
520 struct blobmsg_list_node *var;
521 void *e = blobmsg_open_table(b, "env");
522 blobmsg_list_for_each(&in->env, var)
523 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
524 blobmsg_close_table(b, e);
527 if (!avl_is_empty(&in->limits.avl)) {
528 struct blobmsg_list_node *var;
529 void *e = blobmsg_open_table(b, "limits");
530 blobmsg_list_for_each(&in->limits, var)
531 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
532 blobmsg_close_table(b, e);
536 void *r = blobmsg_open_table(b, "respawn");
537 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
538 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
539 blobmsg_add_u32(b, "retry", in->respawn_retry);
540 blobmsg_close_table(b, r);
543 if (verbose && in->trigger)
544 blobmsg_add_blob(b, in->trigger);
546 blobmsg_close_table(b, i);