2 #include <sys/socket.h>
11 INSTANCE_ATTR_COMMAND,
18 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
19 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
20 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
21 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
22 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
25 struct instance_netdev {
26 struct blobmsg_list_node node;
31 instance_run(struct service_instance *in)
33 struct blobmsg_list_node *var;
34 struct blob_attr *cur;
36 int argc = 1; /* NULL terminated */
39 blobmsg_for_each_attr(cur, in->command, rem)
42 blobmsg_list_for_each(&in->env, var)
43 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
45 argv = alloca(sizeof(char *) * argc);
48 blobmsg_for_each_attr(cur, in->command, rem)
49 argv[argc++] = blobmsg_data(cur);
52 execvp(argv[0], argv);
57 instance_start(struct service_instance *in)
77 DPRINTF("Started instance %s::%s\n", in->srv->name, in->name);
79 uloop_process_add(&in->proc);
83 instance_timeout(struct uloop_timeout *t)
85 struct service_instance *in;
87 in = container_of(t, struct service_instance, timeout);
88 kill(in->proc.pid, SIGKILL);
89 uloop_process_delete(&in->proc);
90 in->proc.cb(&in->proc, -1);
94 instance_exit(struct uloop_process *p, int ret)
96 struct service_instance *in;
98 in = container_of(p, struct service_instance, proc);
99 DPRINTF("Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
100 uloop_timeout_cancel(&in->timeout);
106 instance_stop(struct service_instance *in, bool restart)
108 if (!in->proc.pending)
111 kill(in->proc.pid, SIGTERM);
115 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
120 if (!blob_attr_equal(in->command, in_new->command))
123 if (!blobmsg_list_equal(&in->env, &in_new->env))
126 if (!blobmsg_list_equal(&in->data, &in_new->data))
129 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
136 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
138 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
139 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
141 return n1->ifindex == n2->ifindex;
145 instance_netdev_update(struct blobmsg_list_node *l)
147 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
149 n->ifindex = if_nametoindex(n->node.avl.key);
153 instance_config_parse(struct service_instance *in)
155 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
156 struct blob_attr *cur, *cur2;
160 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
161 blobmsg_data(in->config), blobmsg_data_len(in->config));
163 cur = tb[INSTANCE_ATTR_COMMAND];
167 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
170 blobmsg_for_each_attr(cur2, cur, rem) {
179 if ((cur = tb[INSTANCE_ATTR_ENV])) {
180 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
183 blobmsg_list_fill(&in->env, blobmsg_data(cur), blobmsg_data_len(cur), false);
186 if ((cur = tb[INSTANCE_ATTR_DATA])) {
187 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
190 blobmsg_list_fill(&in->data, blobmsg_data(cur), blobmsg_data_len(cur), false);
193 if ((cur = tb[INSTANCE_ATTR_NETDEV])) {
194 struct blobmsg_list_node *ndev;
196 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
199 blobmsg_list_fill(&in->netdev, blobmsg_data(cur), blobmsg_data_len(cur), true);
200 blobmsg_list_for_each(&in->netdev, ndev)
201 instance_netdev_update(ndev);
208 instance_config_cleanup(struct service_instance *in)
210 blobmsg_list_free(&in->env);
211 blobmsg_list_free(&in->data);
212 blobmsg_list_free(&in->netdev);
216 instance_config_move(struct service_instance *in, struct service_instance *in_src)
218 instance_config_cleanup(in);
219 blobmsg_list_move(&in->env, &in_src->env);
220 blobmsg_list_move(&in->data, &in_src->data);
221 blobmsg_list_move(&in->netdev, &in_src->netdev);
222 in->command = in_src->command;
223 in->name = in_src->name;
224 in->node.avl.key = in_src->node.avl.key;
225 in->config = in_src->config;
226 in_src->config = NULL;
230 instance_update(struct service_instance *in, struct service_instance *in_new)
232 bool changed = instance_config_changed(in, in_new);
238 instance_stop(in, true);
239 instance_config_move(in, in_new);
244 instance_free(struct service_instance *in)
246 uloop_process_delete(&in->proc);
247 uloop_timeout_cancel(&in->timeout);
248 instance_config_cleanup(in);
253 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
256 in->name = blobmsg_name(config);
258 in->timeout.cb = instance_timeout;
259 in->proc.cb = instance_exit;
261 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
262 blobmsg_list_simple_init(&in->env);
263 blobmsg_list_simple_init(&in->data);
264 in->valid = instance_config_parse(in);
267 void instance_dump(struct blob_buf *b, struct service_instance *in)
271 i = blobmsg_open_table(b, in->name);
272 blobmsg_add_u8(b, "running", in->proc.pending);
273 if (in->proc.pending)
274 blobmsg_add_u32(b, "pid", in->proc.pid);
275 blobmsg_add_blob(b, in->command);
276 blobmsg_close_table(b, i);