memdup instance config
[project/procd.git] / instance.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <net/if.h>
4 #include <unistd.h>
5
6 #include "procd.h"
7 #include "service.h"
8 #include "instance.h"
9
10 enum {
11 INSTANCE_ATTR_COMMAND,
12 INSTANCE_ATTR_ENV,
13 INSTANCE_ATTR_DATA,
14 INSTANCE_ATTR_NETDEV,
15 __INSTANCE_ATTR_MAX
16 };
17
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 },
23 };
24
25 struct instance_netdev {
26 struct blobmsg_list_node node;
27 int ifindex;
28 };
29
30 static void
31 instance_run(struct service_instance *in)
32 {
33 struct blobmsg_list_node *var;
34 struct blob_attr *cur;
35 char **argv;
36 int argc = 1; /* NULL terminated */
37 int rem;
38
39 blobmsg_for_each_attr(cur, in->command, rem)
40 argc++;
41
42 blobmsg_list_for_each(&in->env, var)
43 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
44
45 argv = alloca(sizeof(char *) * argc);
46 argc = 0;
47
48 blobmsg_for_each_attr(cur, in->command, rem)
49 argv[argc++] = blobmsg_data(cur);
50
51 argv[argc] = NULL;
52 execvp(argv[0], argv);
53 exit(127);
54 }
55
56 void
57 instance_start(struct service_instance *in)
58 {
59 int pid;
60
61 if (in->proc.pending)
62 return;
63
64 in->restart = false;
65 if (!in->valid)
66 return;
67
68 pid = fork();
69 if (pid < 0)
70 return;
71
72 if (!pid) {
73 instance_run(in);
74 return;
75 }
76
77 DPRINTF("Started instance %s::%s\n", in->srv->name, in->name);
78 in->proc.pid = pid;
79 uloop_process_add(&in->proc);
80 }
81
82 static void
83 instance_timeout(struct uloop_timeout *t)
84 {
85 struct service_instance *in;
86
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);
91 }
92
93 static void
94 instance_exit(struct uloop_process *p, int ret)
95 {
96 struct service_instance *in;
97
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);
101 if (in->restart)
102 instance_start(in);
103 }
104
105 void
106 instance_stop(struct service_instance *in, bool restart)
107 {
108 if (!in->proc.pending)
109 return;
110
111 kill(in->proc.pid, SIGTERM);
112 }
113
114 static bool
115 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
116 {
117 if (!in->valid)
118 return true;
119
120 if (!blob_attr_equal(in->command, in_new->command))
121 return true;
122
123 if (!blobmsg_list_equal(&in->env, &in_new->env))
124 return true;
125
126 if (!blobmsg_list_equal(&in->data, &in_new->data))
127 return true;
128
129 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
130 return true;
131
132 return false;
133 }
134
135 static bool
136 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
137 {
138 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
139 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
140
141 return n1->ifindex == n2->ifindex;
142 }
143
144 static void
145 instance_netdev_update(struct blobmsg_list_node *l)
146 {
147 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
148
149 n->ifindex = if_nametoindex(n->node.avl.key);
150 }
151
152 static bool
153 instance_config_parse(struct service_instance *in)
154 {
155 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
156 struct blob_attr *cur, *cur2;
157 int argc = 0;
158 int rem;
159
160 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
161 blobmsg_data(in->config), blobmsg_data_len(in->config));
162
163 cur = tb[INSTANCE_ATTR_COMMAND];
164 if (!cur)
165 return false;
166
167 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
168 return false;
169
170 blobmsg_for_each_attr(cur2, cur, rem) {
171 argc++;
172 break;
173 }
174 if (!argc)
175 return false;
176
177 in->command = cur;
178
179 if ((cur = tb[INSTANCE_ATTR_ENV])) {
180 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
181 return false;
182
183 blobmsg_list_fill(&in->env, blobmsg_data(cur), blobmsg_data_len(cur), false);
184 }
185
186 if ((cur = tb[INSTANCE_ATTR_DATA])) {
187 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
188 return false;
189
190 blobmsg_list_fill(&in->data, blobmsg_data(cur), blobmsg_data_len(cur), false);
191 }
192
193 if ((cur = tb[INSTANCE_ATTR_NETDEV])) {
194 struct blobmsg_list_node *ndev;
195
196 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
197 return false;
198
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);
202 }
203
204 return true;
205 }
206
207 static void
208 instance_config_cleanup(struct service_instance *in)
209 {
210 blobmsg_list_free(&in->env);
211 blobmsg_list_free(&in->data);
212 blobmsg_list_free(&in->netdev);
213 }
214
215 static void
216 instance_config_move(struct service_instance *in, struct service_instance *in_src)
217 {
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;
227 }
228
229 bool
230 instance_update(struct service_instance *in, struct service_instance *in_new)
231 {
232 bool changed = instance_config_changed(in, in_new);
233
234 if (!changed)
235 return false;
236
237 in->restart = true;
238 instance_stop(in, true);
239 instance_config_move(in, in_new);
240 return true;
241 }
242
243 void
244 instance_free(struct service_instance *in)
245 {
246 uloop_process_delete(&in->proc);
247 uloop_timeout_cancel(&in->timeout);
248 instance_config_cleanup(in);
249 free(in->config);
250 free(in);
251 }
252
253 void
254 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
255 {
256 config = blob_memdup(config);
257 in->srv = s;
258 in->name = blobmsg_name(config);
259 in->config = config;
260 in->timeout.cb = instance_timeout;
261 in->proc.cb = instance_exit;
262
263 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
264 blobmsg_list_simple_init(&in->env);
265 blobmsg_list_simple_init(&in->data);
266 in->valid = instance_config_parse(in);
267 }
268
269 void instance_dump(struct blob_buf *b, struct service_instance *in)
270 {
271 void *i;
272
273 i = blobmsg_open_table(b, in->name);
274 blobmsg_add_u8(b, "running", in->proc.pending);
275 if (in->proc.pending)
276 blobmsg_add_u32(b, "pid", in->proc.pid);
277 blobmsg_add_blob(b, in->command);
278 blobmsg_close_table(b, i);
279 }