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