1 #include <sys/resource.h>
3 #include <sys/socket.h>
15 INSTANCE_ATTR_COMMAND,
24 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
25 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
26 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
27 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
28 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
29 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
30 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
33 struct instance_netdev {
34 struct blobmsg_list_node node;
38 struct instance_file {
39 struct blobmsg_list_node node;
44 instance_run(struct service_instance *in)
46 struct blobmsg_list_node *var;
47 struct blob_attr *cur;
49 int argc = 1; /* NULL terminated */
53 setpriority(PRIO_PROCESS, 0, in->nice);
55 blobmsg_for_each_attr(cur, in->command, rem)
58 blobmsg_list_for_each(&in->env, var)
59 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
61 argv = alloca(sizeof(char *) * argc);
64 blobmsg_for_each_attr(cur, in->command, rem)
65 argv[argc++] = blobmsg_data(cur);
68 execvp(argv[0], argv);
73 instance_start(struct service_instance *in)
93 DPRINTF("Started instance %s::%s\n", in->srv->name, in->name);
95 uloop_process_add(&in->proc);
99 instance_timeout(struct uloop_timeout *t)
101 struct service_instance *in;
103 in = container_of(t, struct service_instance, timeout);
104 kill(in->proc.pid, SIGKILL);
105 uloop_process_delete(&in->proc);
106 in->proc.cb(&in->proc, -1);
110 instance_exit(struct uloop_process *p, int ret)
112 struct service_instance *in;
114 in = container_of(p, struct service_instance, proc);
115 DPRINTF("Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
116 uloop_timeout_cancel(&in->timeout);
122 instance_stop(struct service_instance *in, bool restart)
124 if (!in->proc.pending)
127 kill(in->proc.pid, SIGTERM);
131 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
136 if (!blob_attr_equal(in->command, in_new->command))
139 if (!blobmsg_list_equal(&in->env, &in_new->env))
142 if (!blobmsg_list_equal(&in->data, &in_new->data))
145 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
148 if (!blobmsg_list_equal(&in->file, &in_new->file))
151 if (in->nice != in_new->nice)
158 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
160 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
161 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
163 return n1->ifindex == n2->ifindex;
167 instance_netdev_update(struct blobmsg_list_node *l)
169 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
171 n->ifindex = if_nametoindex(n->node.avl.key);
175 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
177 struct instance_file *f1 = container_of(l1, struct instance_file, node);
178 struct instance_file *f2 = container_of(l2, struct instance_file, node);
180 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
184 instance_file_update(struct blobmsg_list_node *l)
186 struct instance_file *f = container_of(l, struct instance_file, node);
191 memset(f->md5, 0, sizeof(f->md5));
193 fd = open(l->avl.key, O_RDONLY);
199 len = read(fd, buf, sizeof(buf));
209 md5_hash(buf, len, &md5);
212 md5_end(f->md5, &md5);
217 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
219 struct blobmsg_list_node *node;
224 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
227 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
229 blobmsg_list_for_each(l, node)
236 instance_config_parse(struct service_instance *in)
238 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
239 struct blob_attr *cur, *cur2;
243 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
244 blobmsg_data(in->config), blobmsg_data_len(in->config));
246 cur = tb[INSTANCE_ATTR_COMMAND];
250 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
253 blobmsg_for_each_attr(cur2, cur, rem) {
262 if ((cur = tb[INSTANCE_ATTR_NICE])) {
263 in->nice = (int8_t) blobmsg_get_u32(cur);
264 if (in->nice < -20 || in->nice > 20)
268 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
271 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
274 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
277 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
284 instance_config_cleanup(struct service_instance *in)
286 blobmsg_list_free(&in->env);
287 blobmsg_list_free(&in->data);
288 blobmsg_list_free(&in->netdev);
292 instance_config_move(struct service_instance *in, struct service_instance *in_src)
294 instance_config_cleanup(in);
295 blobmsg_list_move(&in->env, &in_src->env);
296 blobmsg_list_move(&in->data, &in_src->data);
297 blobmsg_list_move(&in->netdev, &in_src->netdev);
298 in->command = in_src->command;
299 in->name = in_src->name;
300 in->node.avl.key = in_src->node.avl.key;
303 in->config = in_src->config;
304 in_src->config = NULL;
308 instance_update(struct service_instance *in, struct service_instance *in_new)
310 bool changed = instance_config_changed(in, in_new);
316 instance_stop(in, true);
317 instance_config_move(in, in_new);
322 instance_free(struct service_instance *in)
324 uloop_process_delete(&in->proc);
325 uloop_timeout_cancel(&in->timeout);
326 instance_config_cleanup(in);
332 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
334 config = blob_memdup(config);
336 in->name = blobmsg_name(config);
338 in->timeout.cb = instance_timeout;
339 in->proc.cb = instance_exit;
341 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
342 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
343 blobmsg_list_simple_init(&in->env);
344 blobmsg_list_simple_init(&in->data);
345 in->valid = instance_config_parse(in);
348 void instance_dump(struct blob_buf *b, struct service_instance *in)
352 i = blobmsg_open_table(b, in->name);
353 blobmsg_add_u8(b, "running", in->proc.pending);
354 if (in->proc.pending)
355 blobmsg_add_u32(b, "pid", in->proc.pid);
356 blobmsg_add_blob(b, in->command);
357 blobmsg_close_table(b, i);