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