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