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