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