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