debloat and reorganize code
[project/procd.git] / service / 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
25 #include "service.h"
26 #include "instance.h"
27
28 #include "../utils/md5.h"
29
30 enum {
31 INSTANCE_ATTR_COMMAND,
32 INSTANCE_ATTR_ENV,
33 INSTANCE_ATTR_DATA,
34 INSTANCE_ATTR_NETDEV,
35 INSTANCE_ATTR_FILE,
36 INSTANCE_ATTR_TRIGGER,
37 INSTANCE_ATTR_RESPAWN,
38 INSTANCE_ATTR_NICE,
39 __INSTANCE_ATTR_MAX
40 };
41
42 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
43 [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
44 [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
45 [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
46 [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
47 [INSTANCE_ATTR_FILE] = { "file", BLOBMSG_TYPE_ARRAY },
48 [INSTANCE_ATTR_TRIGGER] = { "triggers", BLOBMSG_TYPE_ARRAY },
49 [INSTANCE_ATTR_RESPAWN] = { "respawn", BLOBMSG_TYPE_ARRAY },
50 [INSTANCE_ATTR_NICE] = { "nice", BLOBMSG_TYPE_INT32 },
51 };
52
53 struct instance_netdev {
54 struct blobmsg_list_node node;
55 int ifindex;
56 };
57
58 struct instance_file {
59 struct blobmsg_list_node node;
60 uint32_t md5[4];
61 };
62
63 static void
64 instance_run(struct service_instance *in)
65 {
66 struct blobmsg_list_node *var;
67 struct blob_attr *cur;
68 char **argv;
69 int argc = 1; /* NULL terminated */
70 int rem, fd;
71
72 if (in->nice)
73 setpriority(PRIO_PROCESS, 0, in->nice);
74
75 blobmsg_for_each_attr(cur, in->command, rem)
76 argc++;
77
78 blobmsg_list_for_each(&in->env, var)
79 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
80
81 argv = alloca(sizeof(char *) * argc);
82 argc = 0;
83
84 blobmsg_for_each_attr(cur, in->command, rem)
85 argv[argc++] = blobmsg_data(cur);
86
87 argv[argc] = NULL;
88 fd = open("/dev/null", O_RDWR);
89 if (fd > -1) {
90 dup2(fd, STDIN_FILENO);
91 dup2(fd, STDOUT_FILENO);
92 dup2(fd, STDERR_FILENO);
93 if (fd > STDERR_FILENO)
94 close(fd);
95 }
96 execvp(argv[0], argv);
97 exit(127);
98 }
99
100 void
101 instance_start(struct service_instance *in)
102 {
103 int pid;
104
105 if (in->proc.pending)
106 return;
107
108 in->restart = false;
109 in->halt = !in->respawn;
110
111 if (!in->valid)
112 return;
113
114 pid = fork();
115 if (pid < 0)
116 return;
117
118 if (!pid) {
119 uloop_done();
120 instance_run(in);
121 return;
122 }
123
124 DEBUG(2, "Started instance %s::%s\n", in->srv->name, in->name);
125 in->proc.pid = pid;
126 clock_gettime(CLOCK_MONOTONIC, &in->start);
127 uloop_process_add(&in->proc);
128 }
129
130 static void
131 instance_timeout(struct uloop_timeout *t)
132 {
133 struct service_instance *in;
134
135 in = container_of(t, struct service_instance, timeout);
136
137 if (!in->halt && (in->restart || in->respawn))
138 instance_start(in);
139 }
140
141 static void
142 instance_exit(struct uloop_process *p, int ret)
143 {
144 struct service_instance *in;
145 struct timespec tp;
146 long runtime;
147
148 in = container_of(p, struct service_instance, proc);
149
150 clock_gettime(CLOCK_MONOTONIC, &tp);
151 runtime = tp.tv_sec - in->start.tv_sec;
152
153 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
154 if (upgrade_running)
155 return;
156
157 uloop_timeout_cancel(&in->timeout);
158 if (in->halt) {
159 /* no action */
160 } else if (in->restart) {
161 instance_start(in);
162 } else if (in->respawn) {
163 if (runtime < in->respawn_threshold)
164 in->respawn_count++;
165 else
166 in->respawn_count = 0;
167 if (in->respawn_count > in->respawn_retry) {
168 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
169 in->srv->name, in->name, in->respawn_count, runtime);
170 in->restart = in->respawn = 0;
171 in->halt = 1;
172 } else {
173 uloop_timeout_set(&in->timeout, in->respawn_timeout * 1000);
174 }
175 }
176 }
177
178 void
179 instance_stop(struct service_instance *in)
180 {
181 if (!in->proc.pending)
182 return;
183 in->halt = true;
184 in->restart = in->respawn = false;
185 kill(in->proc.pid, SIGTERM);
186 }
187
188 static void
189 instance_restart(struct service_instance *in)
190 {
191 if (!in->proc.pending)
192 return;
193 in->halt = false;
194 in->restart = true;
195 kill(in->proc.pid, SIGTERM);
196 }
197
198 static bool
199 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
200 {
201 if (!in->valid)
202 return true;
203
204 if (!blob_attr_equal(in->command, in_new->command))
205 return true;
206
207 if (!blobmsg_list_equal(&in->env, &in_new->env))
208 return true;
209
210 if (!blobmsg_list_equal(&in->data, &in_new->data))
211 return true;
212
213 if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
214 return true;
215
216 if (!blobmsg_list_equal(&in->file, &in_new->file))
217 return true;
218
219 if (in->nice != in_new->nice)
220 return true;
221
222 return false;
223 }
224
225 static bool
226 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
227 {
228 struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
229 struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
230
231 return n1->ifindex == n2->ifindex;
232 }
233
234 static void
235 instance_netdev_update(struct blobmsg_list_node *l)
236 {
237 struct instance_netdev *n = container_of(l, struct instance_netdev, node);
238
239 n->ifindex = if_nametoindex(n->node.avl.key);
240 }
241
242 static bool
243 instance_file_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
244 {
245 struct instance_file *f1 = container_of(l1, struct instance_file, node);
246 struct instance_file *f2 = container_of(l2, struct instance_file, node);
247
248 return !memcmp(f1->md5, f2->md5, sizeof(f1->md5));
249 }
250
251 static void
252 instance_file_update(struct blobmsg_list_node *l)
253 {
254 struct instance_file *f = container_of(l, struct instance_file, node);
255 md5_ctx_t md5;
256 char buf[256];
257 int len, fd;
258
259 memset(f->md5, 0, sizeof(f->md5));
260
261 fd = open(l->avl.key, O_RDONLY);
262 if (fd < 0)
263 return;
264
265 md5_begin(&md5);
266 do {
267 len = read(fd, buf, sizeof(buf));
268 if (len < 0) {
269 if (errno == EINTR)
270 continue;
271
272 break;
273 }
274 if (!len)
275 break;
276
277 md5_hash(buf, len, &md5);
278 } while(1);
279
280 md5_end(f->md5, &md5);
281 close(fd);
282 }
283
284 static bool
285 instance_fill_array(struct blobmsg_list *l, struct blob_attr *cur, blobmsg_update_cb cb, bool array)
286 {
287 struct blobmsg_list_node *node;
288
289 if (!cur)
290 return true;
291
292 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
293 return false;
294
295 blobmsg_list_fill(l, blobmsg_data(cur), blobmsg_data_len(cur), array);
296 if (cb) {
297 blobmsg_list_for_each(l, node)
298 cb(node);
299 }
300 return true;
301 }
302
303 static bool
304 instance_config_parse(struct service_instance *in)
305 {
306 struct blob_attr *tb[__INSTANCE_ATTR_MAX];
307 struct blob_attr *cur, *cur2;
308 int argc = 0;
309 int rem;
310
311 blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
312 blobmsg_data(in->config), blobmsg_data_len(in->config));
313
314 cur = tb[INSTANCE_ATTR_COMMAND];
315 if (!cur)
316 return false;
317
318 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
319 return false;
320
321 blobmsg_for_each_attr(cur2, cur, rem) {
322 argc++;
323 break;
324 }
325 if (!argc)
326 return false;
327
328 in->command = cur;
329
330 if (tb[INSTANCE_ATTR_RESPAWN]) {
331 int i = 0;
332 uint32_t vals[3] = { 3600, 5, 5};
333
334 blobmsg_for_each_attr(cur2, tb[INSTANCE_ATTR_RESPAWN], rem) {
335 if ((i >= 3) && (blobmsg_type(cur2) == BLOBMSG_TYPE_STRING))
336 continue;
337 vals[i] = atoi(blobmsg_get_string(cur2));
338 i++;
339 }
340 in->respawn = true;
341 in->respawn_count = 0;
342 in->respawn_threshold = vals[0];
343 in->respawn_timeout = vals[1];
344 in->respawn_retry = vals[2];
345 }
346 if (tb[INSTANCE_ATTR_TRIGGER]) {
347 in->trigger = malloc(blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
348 if (!in->trigger)
349 return -1;
350 memcpy(in->trigger, tb[INSTANCE_ATTR_TRIGGER], blob_pad_len(tb[INSTANCE_ATTR_TRIGGER]));
351 trigger_add(in->trigger, in);
352 }
353
354 if ((cur = tb[INSTANCE_ATTR_NICE])) {
355 in->nice = (int8_t) blobmsg_get_u32(cur);
356 if (in->nice < -20 || in->nice > 20)
357 return false;
358 }
359
360 if (!instance_fill_array(&in->env, tb[INSTANCE_ATTR_ENV], NULL, false))
361 return false;
362
363 if (!instance_fill_array(&in->data, tb[INSTANCE_ATTR_DATA], NULL, false))
364 return false;
365
366 if (!instance_fill_array(&in->netdev, tb[INSTANCE_ATTR_NETDEV], instance_netdev_update, true))
367 return false;
368
369 if (!instance_fill_array(&in->file, tb[INSTANCE_ATTR_FILE], instance_file_update, true))
370 return false;
371
372 return true;
373 }
374
375 static void
376 instance_config_cleanup(struct service_instance *in)
377 {
378 blobmsg_list_free(&in->env);
379 blobmsg_list_free(&in->data);
380 blobmsg_list_free(&in->netdev);
381 }
382
383 static void
384 instance_config_move(struct service_instance *in, struct service_instance *in_src)
385 {
386 instance_config_cleanup(in);
387 blobmsg_list_move(&in->env, &in_src->env);
388 blobmsg_list_move(&in->data, &in_src->data);
389 blobmsg_list_move(&in->netdev, &in_src->netdev);
390 in->trigger = in_src->trigger;
391 in->command = in_src->command;
392 in->name = in_src->name;
393 in->node.avl.key = in_src->node.avl.key;
394
395 free(in->config);
396 in->config = in_src->config;
397 in_src->config = NULL;
398 }
399
400 bool
401 instance_update(struct service_instance *in, struct service_instance *in_new)
402 {
403 bool changed = instance_config_changed(in, in_new);
404 bool running = in->proc.pending;
405
406 if (!changed && running)
407 return false;
408
409 if (!running) {
410 if (changed)
411 instance_config_move(in, in_new);
412 instance_start(in);
413 } else {
414 instance_restart(in);
415 instance_config_move(in, in_new);
416 /* restart happens in the child callback handler */
417 }
418 return true;
419 }
420
421 void
422 instance_free(struct service_instance *in)
423 {
424 uloop_process_delete(&in->proc);
425 uloop_timeout_cancel(&in->timeout);
426 trigger_del(in);
427 free(in->trigger);
428 instance_config_cleanup(in);
429 free(in->config);
430 free(in);
431 }
432
433 void
434 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
435 {
436 config = blob_memdup(config);
437 in->srv = s;
438 in->name = blobmsg_name(config);
439 in->config = config;
440 in->timeout.cb = instance_timeout;
441 in->proc.cb = instance_exit;
442
443 blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
444 blobmsg_list_init(&in->file, struct instance_file, node, instance_file_cmp);
445 blobmsg_list_simple_init(&in->env);
446 blobmsg_list_simple_init(&in->data);
447 in->valid = instance_config_parse(in);
448 }
449
450 void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
451 {
452 void *i;
453
454 i = blobmsg_open_table(b, in->name);
455 blobmsg_add_u8(b, "running", in->proc.pending);
456 if (in->proc.pending)
457 blobmsg_add_u32(b, "pid", in->proc.pid);
458 blobmsg_add_blob(b, in->command);
459
460 if (!avl_is_empty(&in->env.avl)) {
461 struct blobmsg_list_node *var;
462 void *e = blobmsg_open_table(b, "env");
463 blobmsg_list_for_each(&in->env, var)
464 blobmsg_add_string(b, blobmsg_name(var->data), blobmsg_data(var->data));
465 blobmsg_close_table(b, e);
466 }
467
468 if (in->respawn) {
469 void *r = blobmsg_open_table(b, "respawn");
470 blobmsg_add_u32(b, "timeout", in->respawn_timeout);
471 blobmsg_add_u32(b, "threshold", in->respawn_threshold);
472 blobmsg_add_u32(b, "retry", in->respawn_retry);
473 blobmsg_close_table(b, r);
474 }
475
476 if (verbose && in->trigger)
477 blobmsg_add_blob(b, in->trigger);
478
479 blobmsg_close_table(b, i);
480 }