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>
29 INSTANCE_ATTR_COMMAND,
34 INSTANCE_ATTR_TRIGGER,
35 INSTANCE_ATTR_RESPAWN,
40 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
41 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
42 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
43 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
44 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
45 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
46 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
47 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
48 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
51 struct instance_netdev {
52 struct blobmsg_list_node node;
56 struct instance_file {
57 struct blobmsg_list_node node;
62 instance_run(struct service_instance *in)
64 struct blobmsg_list_node *var;
65 struct blob_attr *cur;
67 int argc = 1; /* NULL terminated */
71 setpriority(PRIO_PROCESS, 0, in->nice);
73 blobmsg_for_each_attr(cur, in->command, rem)
76 blobmsg_list_for_each(&in->env, var)
77 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
79 argv = alloca(sizeof(char *) * argc);
82 blobmsg_for_each_attr(cur, in->command, rem)
83 argv[argc++] = blobmsg_data(cur);
86 fd = open("/dev/null", O_RDWR);
88 dup2(fd, STDIN_FILENO);
89 dup2(fd, STDOUT_FILENO);
90 dup2(fd, STDERR_FILENO);
91 if (fd > STDERR_FILENO)
94 execvp(argv[0], argv);
99 instance_start(struct service_instance *in)
103 if (in->proc.pending)
107 in->halt = !in->respawn;
122 DEBUG(1, "Started instance %s::%s\n", in->srv->name, in->name);
124 clock_gettime(CLOCK_MONOTONIC, &in->start);
125 uloop_process_add(&in->proc);
129 instance_timeout(struct uloop_timeout *t)
131 struct service_instance *in;
133 in = container_of(t, struct service_instance, timeout);
135 if (!in->halt && (in->restart || in->respawn))
140 instance_exit(struct uloop_process *p, int ret)
142 struct service_instance *in;
146 in = container_of(p, struct service_instance, proc);
148 clock_gettime(CLOCK_MONOTONIC, &tp);
149 runtime = tp.tv_sec - in->start.tv_sec;
151 DEBUG(1, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
152 uloop_timeout_cancel(&in->timeout);
155 } else if (in->restart) {
157 } else if (in->respawn) {
158 if (runtime < in->respawn_threshold)
161 in->respawn_count = 0;
162 if (in->respawn_count > in->respawn_retry) {
163 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
164 in->srv->name, in->name, in->respawn_count, runtime);
165 in->restart = in->respawn = 0;
168 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
174 instance_stop(struct service_instance *in)
176 if (!in->proc.pending)
179 in->restart = in->respawn = false;
180 kill(in->proc.pid, SIGTERM);
184 instance_restart(struct service_instance *in)
186 if (!in->proc.pending)
190 kill(in->proc.pid, SIGTERM);
194 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
199 if (!blob_attr_equal(in->command, in_new->command))
202 if (!blobmsg_list_equal(&in->env, &in_new->env))
205 if (!blobmsg_list_equal(&in->data, &in_new->data))
208 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
211 if (!blobmsg_list_equal(&in->file, &in_new->file))
214 if (in->nice != in_new->nice)
221 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
223 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
224 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
226 return n1->ifindex == n2->ifindex;
230 instance_netdev_update(struct blobmsg_list_node *l)
232 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
234 n->ifindex = if_nametoindex(n->node.avl.key);
238 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
240 struct instance_file *f1 = container_of(l1, struct instance_file, node);
241 struct instance_file *f2 = container_of(l2, struct instance_file, node);
243 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
247 instance_file_update(struct blobmsg_list_node *l)
249 struct instance_file *f = container_of(l, struct instance_file, node);
254 memset(f->md5, 0, sizeof(f->md5));
256 fd = open(l->avl.key, O_RDONLY);
262 len = read(fd, buf, sizeof(buf));
272 md5_hash(buf, len, &md5);
275 md5_end(f->md5, &md5);
280 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
282 struct blobmsg_list_node *node;
287 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
290 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
292 blobmsg_list_for_each(l, node)
299 instance_config_parse(struct service_instance *in)
301 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
302 struct blob_attr *cur, *cur2;
306 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
307 blobmsg_data(in->config), blobmsg_data_len(in->config));
309 cur = tb[INSTANCE_ATTR_COMMAND];
313 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
316 blobmsg_for_each_attr(cur2, cur, rem) {
325 if (tb[INSTANCE_ATTR_RESPAWN]) {
327 uint32_t vals[3] = { 3600, 5, 5};
329 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
330 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
332 vals[i] = atoi(blobmsg_get_string(cur2));
336 in->respawn_count = 0;
337 in->respawn_threshold = vals[0];
338 in->respawn_timeout = vals[1];
339 in->respawn_retry = vals[2];
341 if (tb[INSTANCE_ATTR_TRIGGER]) {
342 in->trigger = malloc(blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
345 memcpy(in->trigger, tb[INSTANCE_ATTR_TRIGGER], blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
346 trigger_add(in->trigger, in);
349 if ((cur = tb[INSTANCE_ATTR_NICE])) {
350 in->nice = (int8_t) blobmsg_get_u32(cur);
351 if (in->nice < -20 || in->nice > 20)
355 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
358 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
361 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
364 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
371 instance_config_cleanup(struct service_instance *in)
373 blobmsg_list_free(&in->env);
374 blobmsg_list_free(&in->data);
375 blobmsg_list_free(&in->netdev);
379 instance_config_move(struct service_instance *in, struct service_instance *in_src)
381 instance_config_cleanup(in);
382 blobmsg_list_move(&in->env, &in_src->env);
383 blobmsg_list_move(&in->data, &in_src->data);
384 blobmsg_list_move(&in->netdev, &in_src->netdev);
385 in->trigger = in_src->trigger;
386 in->command = in_src->command;
387 in->name = in_src->name;
388 in->node.avl.key = in_src->node.avl.key;
391 in->config = in_src->config;
392 in_src->config = NULL;
396 instance_update(struct service_instance *in, struct service_instance *in_new)
398 bool changed = instance_config_changed(in, in_new);
399 bool running = in->proc.pending;
401 if (!changed && running)
406 instance_config_move(in, in_new);
409 instance_restart(in);
410 instance_config_move(in, in_new);
411 /* restart happens in the child callback handler */
417 instance_free(struct service_instance *in)
419 uloop_process_delete(&in->proc);
420 uloop_timeout_cancel(&in->timeout);
423 instance_config_cleanup(in);
429 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
431 config = blob_memdup(config);
433 in->name = blobmsg_name(config);
435 in->timeout.cb = instance_timeout;
436 in->proc.cb = instance_exit;
438 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
439 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
440 blobmsg_list_simple_init(&in->env);
441 blobmsg_list_simple_init(&in->data);
442 in->valid = instance_config_parse(in);
445 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
450 i = blobmsg_open_table(b, in->name);
451 blobmsg_add_u8(b, "running", in->proc.pending);
452 if (in->proc.pending)
453 blobmsg_add_u32(b, "pid", in->proc.pid);
454 blobmsg_add_blob(b, in->command);
456 if (!avl_is_empty(&in->env.avl)) {
457 struct blobmsg_list_node *var;
458 void *e = blobmsg_open_table(b, "env");
459 blobmsg_list_for_each(&in->env, var)
460 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
461 blobmsg_close_table(b, e);
465 void *r = blobmsg_open_table(b, "respawn");
466 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
467 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
468 blobmsg_add_u32(b, "retry", in->respawn_retry);
469 blobmsg_close_table(b, r);
472 if (verbose && in->trigger)
473 blobmsg_add_blob(b, in->trigger);
474 if (!measure_process(in->proc.pid, &pi)) {
478 clock_gettime(CLOCK_MONOTONIC, &tp);
479 uptime = tp.tv_sec - in->start.tv_sec;
481 blobmsg_add_u8(b, "ppid", pi.ppid);
482 blobmsg_add_u16(b, "uid", pi.uid);
483 blobmsg_add_u32(b, "fdcount", pi.fdcount);
484 blobmsg_add_u32(b, "vmsize", pi.vmsize);
485 blobmsg_add_u32(b, "uptime", uptime);
487 blobmsg_close_table(b, i);