restart stopped instances on update
[project/procd.git] / instance.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
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
8 *
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.
13 */
14
15 #include <sys/resource.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <net/if.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include <fcntl.h>
22
23 #include "procd.h"
24 #include "service.h"
25 #include "instance.h"
26 #include "md5.h"
27
28 enum {
29 INSTANCE_ATTR_COMMAND,
30 INSTANCE_ATTR_ENV,
31 INSTANCE_ATTR_DATA,
32 INSTANCE_ATTR_NETDEV,
33 INSTANCE_ATTR_FILE,
34 INSTANCE_ATTR_NICE,
35 __INSTANCE_ATTR_MAX
36 };
37
38 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
39 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
40 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
41 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
42 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
43 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
44 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
45 };
46
47 struct instance_netdev {
48 struct blobmsg_list_node node;
49 int ifindex;
50 };
51
52 struct instance_file {
53 struct blobmsg_list_node node;
54 uint32_t md5[4];
55 };
56
57 static void
58 instance_run(struct service_instance *in)
59 {
60 struct blobmsg_list_node *var;
61 struct blob_attr *cur;
62 char **argv;
63 int argc = 1; /* NULL terminated */
64 int rem;
65
66 if (in->nice)
67 setpriority(PRIO_PROCESS, 0, in->nice);
68
69 blobmsg_for_each_attr(cur, in->command, rem)
70 argc++;
71
72 blobmsg_list_for_each(&in->env, var)
73 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
74
75 argv = alloca(sizeof(char *) * argc);
76 argc = 0;
77
78 blobmsg_for_each_attr(cur, in->command, rem)
79 argv[argc++] = blobmsg_data(cur);
80
81 argv[argc] = NULL;
82 execvp(argv[0], argv);
83 exit(127);
84 }
85
86 void
87 instance_start(struct service_instance *in)
88 {
89 int pid;
90
91 if (in->proc.pending)
92 return;
93
94 in->restart = false;
95 if (!in->valid)
96 return;
97
98 pid = fork();
99 if (pid < 0)
100 return;
101
102 if (!pid) {
103 instance_run(in);
104 return;
105 }
106
107 DEBUG(1, "Started instance %s::%s\n", in->srv->name, in->name);
108 in->proc.pid = pid;
109 uloop_process_add(&in->proc);
110 }
111
112 static void
113 instance_timeout(struct uloop_timeout *t)
114 {
115 struct service_instance *in;
116
117 in = container_of(t, struct service_instance, timeout);
118 kill(in->proc.pid, SIGKILL);
119 uloop_process_delete(&in->proc);
120 in->proc.cb(&in->proc, -1);
121 }
122
123 static void
124 instance_exit(struct uloop_process *p, int ret)
125 {
126 struct service_instance *in;
127
128 in = container_of(p, struct service_instance, proc);
129 DEBUG(1, "Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
130 uloop_timeout_cancel(&in->timeout);
131 if (in->restart)
132 instance_start(in);
133 }
134
135 void
136 instance_stop(struct service_instance *in, bool restart)
137 {
138 if (!in->proc.pending)
139 return;
140
141 kill(in->proc.pid, SIGTERM);
142 }
143
144 static bool
145 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
146 {
147 if (!in->valid)
148 return true;
149
150 if (!blob_attr_equal(in->command, in_new->command))
151 return true;
152
153 if (!blobmsg_list_equal(&in->env, &in_new->env))
154 return true;
155
156 if (!blobmsg_list_equal(&in->data, &in_new->data))
157 return true;
158
159 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
160 return true;
161
162 if (!blobmsg_list_equal(&in->file, &in_new->file))
163 return true;
164
165 if (in->nice != in_new->nice)
166 return true;
167
168 return false;
169 }
170
171 static bool
172 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
173 {
174 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
175 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
176
177 return n1->ifindex == n2->ifindex;
178 }
179
180 static void
181 instance_netdev_update(struct blobmsg_list_node *l)
182 {
183 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
184
185 n->ifindex = if_nametoindex(n->node.avl.key);
186 }
187
188 static bool
189 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
190 {
191 struct instance_file *f1 = container_of(l1, struct instance_file, node);
192 struct instance_file *f2 = container_of(l2, struct instance_file, node);
193
194 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
195 }
196
197 static void
198 instance_file_update(struct blobmsg_list_node *l)
199 {
200 struct instance_file *f = container_of(l, struct instance_file, node);
201 md5_ctx_t md5;
202 char buf[256];
203 int len, fd;
204
205 memset(f->md5, 0, sizeof(f->md5));
206
207 fd = open(l->avl.key, O_RDONLY);
208 if (fd < 0)
209 return;
210
211 md5_begin(&md5);
212 do {
213 len = read(fd, buf, sizeof(buf));
214 if (len < 0) {
215 if (errno == EINTR)
216 continue;
217
218 break;
219 }
220 if (!len)
221 break;
222
223 md5_hash(buf, len, &md5);
224 } while(1);
225
226 md5_end(f->md5, &md5);
227 close(fd);
228 }
229
230 static bool
231 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
232 {
233 struct blobmsg_list_node *node;
234
235 if (!cur)
236 return true;
237
238 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
239 return false;
240
241 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
242 if (cb) {
243 blobmsg_list_for_each(l, node)
244 cb(node);
245 }
246 return true;
247 }
248
249 static bool
250 instance_config_parse(struct service_instance *in)
251 {
252 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
253 struct blob_attr *cur, *cur2;
254 int argc = 0;
255 int rem;
256
257 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
258 blobmsg_data(in->config), blobmsg_data_len(in->config));
259
260 cur = tb[INSTANCE_ATTR_COMMAND];
261 if (!cur)
262 return false;
263
264 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
265 return false;
266
267 blobmsg_for_each_attr(cur2, cur, rem) {
268 argc++;
269 break;
270 }
271 if (!argc)
272 return false;
273
274 in->command = cur;
275
276 if ((cur = tb[INSTANCE_ATTR_NICE])) {
277 in->nice = (int8_t) blobmsg_get_u32(cur);
278 if (in->nice < -20 || in->nice > 20)
279 return false;
280 }
281
282 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
283 return false;
284
285 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
286 return false;
287
288 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
289 return false;
290
291 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
292 return false;
293
294 return true;
295 }
296
297 static void
298 instance_config_cleanup(struct service_instance *in)
299 {
300 blobmsg_list_free(&in->env);
301 blobmsg_list_free(&in->data);
302 blobmsg_list_free(&in->netdev);
303 }
304
305 static void
306 instance_config_move(struct service_instance *in, struct service_instance *in_src)
307 {
308 instance_config_cleanup(in);
309 blobmsg_list_move(&in->env, &in_src->env);
310 blobmsg_list_move(&in->data, &in_src->data);
311 blobmsg_list_move(&in->netdev, &in_src->netdev);
312 in->command = in_src->command;
313 in->name = in_src->name;
314 in->node.avl.key = in_src->node.avl.key;
315
316 free(in->config);
317 in->config = in_src->config;
318 in_src->config = NULL;
319 }
320
321 bool
322 instance_update(struct service_instance *in, struct service_instance *in_new)
323 {
324 bool changed = instance_config_changed(in, in_new);
325 bool running = in->proc.pending;
326
327 if (!changed && running)
328 return false;
329
330 if (!running) {
331 if (changed)
332 instance_config_move(in, in_new);
333 instance_start(in);
334 } else {
335 in->restart = true;
336 instance_stop(in, true);
337 instance_config_move(in, in_new);
338 }
339 return true;
340 }
341
342 void
343 instance_free(struct service_instance *in)
344 {
345 uloop_process_delete(&in->proc);
346 uloop_timeout_cancel(&in->timeout);
347 instance_config_cleanup(in);
348 free(in->config);
349 free(in);
350 }
351
352 void
353 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
354 {
355 config = blob_memdup(config);
356 in->srv = s;
357 in->name = blobmsg_name(config);
358 in->config = config;
359 in->timeout.cb = instance_timeout;
360 in->proc.cb = instance_exit;
361
362 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
363 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
364 blobmsg_list_simple_init(&in->env);
365 blobmsg_list_simple_init(&in->data);
366 in->valid = instance_config_parse(in);
367 }
368
369 void instance_dump(struct blob_buf *b, struct service_instance *in)
370 {
371 void *i;
372
373 i = blobmsg_open_table(b, in->name);
374 blobmsg_add_u8(b, "running", in->proc.pending);
375 if (in->proc.pending)
376 blobmsg_add_u32(b, "pid", in->proc.pid);
377 blobmsg_add_blob(b, in->command);
378 blobmsg_close_table(b, i);
379 }