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