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