1 #include <sys/resource.h>
3 #include <sys/socket.h>
12 INSTANCE_ATTR_COMMAND,
20 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
21 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
22 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
23 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
24 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
25 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
28 struct instance_netdev {
29 struct blobmsg_list_node node;
34 instance_run(struct service_instance *in)
36 struct blobmsg_list_node *var;
37 struct blob_attr *cur;
39 int argc = 1; /* NULL terminated */
43 setpriority(PRIO_PROCESS, 0, in->nice);
45 blobmsg_for_each_attr(cur, in->command, rem)
48 blobmsg_list_for_each(&in->env, var)
49 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
51 argv = alloca(sizeof(char *) * argc);
54 blobmsg_for_each_attr(cur, in->command, rem)
55 argv[argc++] = blobmsg_data(cur);
58 execvp(argv[0], argv);
63 instance_start(struct service_instance *in)
83 DPRINTF("Started instance %s::%s\n", in->srv->name, in->name);
85 uloop_process_add(&in->proc);
89 instance_timeout(struct uloop_timeout *t)
91 struct service_instance *in;
93 in = container_of(t, struct service_instance, timeout);
94 kill(in->proc.pid, SIGKILL);
95 uloop_process_delete(&in->proc);
96 in->proc.cb(&in->proc, -1);
100 instance_exit(struct uloop_process *p, int ret)
102 struct service_instance *in;
104 in = container_of(p, struct service_instance, proc);
105 DPRINTF("Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
106 uloop_timeout_cancel(&in->timeout);
112 instance_stop(struct service_instance *in, bool restart)
114 if (!in->proc.pending)
117 kill(in->proc.pid, SIGTERM);
121 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
126 if (!blob_attr_equal(in->command, in_new->command))
129 if (!blobmsg_list_equal(&in->env, &in_new->env))
132 if (!blobmsg_list_equal(&in->data, &in_new->data))
135 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
138 if (in->nice != in_new->nice)
145 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
147 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
148 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
150 return n1->ifindex == n2->ifindex;
154 instance_netdev_update(struct blobmsg_list_node *l)
156 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
158 n->ifindex = if_nametoindex(n->node.avl.key);
162 instance_config_parse(struct service_instance *in)
164 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
165 struct blob_attr *cur, *cur2;
169 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
170 blobmsg_data(in->config), blobmsg_data_len(in->config));
172 cur = tb[INSTANCE_ATTR_COMMAND];
176 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
179 blobmsg_for_each_attr(cur2, cur, rem) {
188 if ((cur = tb[INSTANCE_ATTR_NICE])) {
189 in->nice = (int8_t) blobmsg_get_u32(cur);
190 if (in->nice < -20 || in->nice > 20)
194 if ((cur = tb[INSTANCE_ATTR_ENV])) {
195 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
198 blobmsg_list_fill(&in->env, blobmsg_data(cur), blobmsg_data_len(cur), false);
201 if ((cur = tb[INSTANCE_ATTR_DATA])) {
202 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
205 blobmsg_list_fill(&in->data, blobmsg_data(cur), blobmsg_data_len(cur), false);
208 if ((cur = tb[INSTANCE_ATTR_NETDEV])) {
209 struct blobmsg_list_node *ndev;
211 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
214 blobmsg_list_fill(&in->netdev, blobmsg_data(cur), blobmsg_data_len(cur), true);
215 blobmsg_list_for_each(&in->netdev, ndev)
216 instance_netdev_update(ndev);
223 instance_config_cleanup(struct service_instance *in)
225 blobmsg_list_free(&in->env);
226 blobmsg_list_free(&in->data);
227 blobmsg_list_free(&in->netdev);
231 instance_config_move(struct service_instance *in, struct service_instance *in_src)
233 instance_config_cleanup(in);
234 blobmsg_list_move(&in->env, &in_src->env);
235 blobmsg_list_move(&in->data, &in_src->data);
236 blobmsg_list_move(&in->netdev, &in_src->netdev);
237 in->command = in_src->command;
238 in->name = in_src->name;
239 in->node.avl.key = in_src->node.avl.key;
242 in->config = in_src->config;
243 in_src->config = NULL;
247 instance_update(struct service_instance *in, struct service_instance *in_new)
249 bool changed = instance_config_changed(in, in_new);
255 instance_stop(in, true);
256 instance_config_move(in, in_new);
261 instance_free(struct service_instance *in)
263 uloop_process_delete(&in->proc);
264 uloop_timeout_cancel(&in->timeout);
265 instance_config_cleanup(in);
271 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
273 config = blob_memdup(config);
275 in->name = blobmsg_name(config);
277 in->timeout.cb = instance_timeout;
278 in->proc.cb = instance_exit;
280 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
281 blobmsg_list_simple_init(&in->env);
282 blobmsg_list_simple_init(&in->data);
283 in->valid = instance_config_parse(in);
286 void instance_dump(struct blob_buf *b, struct service_instance *in)
290 i = blobmsg_open_table(b, in->name);
291 blobmsg_add_u8(b, "running", in->proc.pending);
292 if (in->proc.pending)
293 blobmsg_add_u32(b, "pid", in->proc.pid);
294 blobmsg_add_blob(b, in->command);
295 blobmsg_close_table(b, i);