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