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