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