add code for adding instances
[project/procd.git] / instance.c
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <net/if.h>
4 #include <unistd.h>
5
6 #include "procd.h"
7 #include "service.h"
8 #include "instance.h"
9
10 enum {
11         INSTANCE_ATTR_COMMAND,
12         INSTANCE_ATTR_ENV,
13         INSTANCE_ATTR_DATA,
14         INSTANCE_ATTR_NETDEV,
15         __INSTANCE_ATTR_MAX
16 };
17
18 static const struct blobmsg_policy instance_attr[__INSTANCE_ATTR_MAX] = {
19         [INSTANCE_ATTR_COMMAND] = { "command", BLOBMSG_TYPE_ARRAY },
20         [INSTANCE_ATTR_ENV] = { "env", BLOBMSG_TYPE_TABLE },
21         [INSTANCE_ATTR_DATA] = { "data", BLOBMSG_TYPE_TABLE },
22         [INSTANCE_ATTR_NETDEV] = { "netdev", BLOBMSG_TYPE_ARRAY },
23 };
24
25 struct instance_netdev {
26         struct blobmsg_list_node node;
27         int ifindex;
28 };
29
30 static void
31 instance_run(struct service_instance *in)
32 {
33         struct blobmsg_list_node *var;
34         struct blob_attr *cur;
35         char **argv;
36         int argc = 1; /* NULL terminated */
37         int rem;
38
39         blobmsg_for_each_attr(cur, in->command, rem)
40                 argc++;
41
42         blobmsg_list_for_each(&in->env, var)
43                 setenv(blobmsg_name(var->data), blobmsg_data(var->data), 1);
44
45         argv = alloca(sizeof(char *) * argc);
46         argc = 0;
47
48         blobmsg_for_each_attr(cur, in->command, rem)
49                 argv[argc++] = blobmsg_data(cur);
50
51         argv[argc] = NULL;
52         execvp(argv[0], argv);
53         exit(127);
54 }
55
56 void
57 instance_start(struct service_instance *in)
58 {
59         int pid;
60
61         if (in->proc.pending)
62                 return;
63
64         in->restart = false;
65         if (!in->valid)
66                 return;
67
68         pid = fork();
69         if (pid < 0)
70                 return;
71
72         if (!pid) {
73                 instance_run(in);
74                 return;
75         }
76
77         DPRINTF("Started instance %s::%s\n", in->srv->name, in->name);
78         in->proc.pid = pid;
79         uloop_process_add(&in->proc);
80 }
81
82 static void
83 instance_timeout(struct uloop_timeout *t)
84 {
85         struct service_instance *in;
86
87         in = container_of(t, struct service_instance, timeout);
88         kill(in->proc.pid, SIGKILL);
89         uloop_process_delete(&in->proc);
90         in->proc.cb(&in->proc, -1);
91 }
92
93 static void
94 instance_exit(struct uloop_process *p, int ret)
95 {
96         struct service_instance *in;
97
98         in = container_of(p, struct service_instance, proc);
99         DPRINTF("Instance %s::%s exit with error code %d\n", in->srv->name, in->name, ret);
100         uloop_timeout_cancel(&in->timeout);
101         if (in->restart)
102                 instance_start(in);
103 }
104
105 void
106 instance_stop(struct service_instance *in, bool restart)
107 {
108         if (!in->proc.pending)
109                 return;
110
111         kill(in->proc.pid, SIGTERM);
112 }
113
114 static bool
115 instance_config_changed(struct service_instance *in, struct service_instance *in_new)
116 {
117         if (!in->valid)
118                 return true;
119
120         if (!blob_attr_equal(in->command, in_new->command))
121                 return true;
122
123         if (!blobmsg_list_equal(&in->env, &in_new->env))
124                 return true;
125
126         if (!blobmsg_list_equal(&in->data, &in_new->data))
127                 return true;
128
129         if (!blobmsg_list_equal(&in->netdev, &in_new->netdev))
130                 return true;
131
132         return false;
133 }
134
135 static bool
136 instance_netdev_cmp(struct blobmsg_list_node *l1, struct blobmsg_list_node *l2)
137 {
138         struct instance_netdev *n1 = container_of(l1, struct instance_netdev, node);
139         struct instance_netdev *n2 = container_of(l2, struct instance_netdev, node);
140
141         return n1->ifindex == n2->ifindex;
142 }
143
144 static void
145 instance_netdev_update(struct blobmsg_list_node *l)
146 {
147         struct instance_netdev *n = container_of(l, struct instance_netdev, node);
148
149         n->ifindex = if_nametoindex(n->node.avl.key);
150 }
151
152 static bool
153 instance_config_parse(struct service_instance *in)
154 {
155         struct blob_attr *tb[__INSTANCE_ATTR_MAX];
156         struct blob_attr *cur, *cur2;
157         int argc = 0;
158         int rem;
159
160         blobmsg_parse(instance_attr, __INSTANCE_ATTR_MAX, tb,
161                 blobmsg_data(in->config), blobmsg_data_len(in->config));
162
163         cur = tb[INSTANCE_ATTR_COMMAND];
164         if (!cur)
165                 return false;
166
167         if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
168                 return false;
169
170         blobmsg_for_each_attr(cur2, cur, rem) {
171                 argc++;
172                 break;
173         }
174         if (!argc)
175                 return false;
176
177         in->command = cur;
178
179         if ((cur = tb[INSTANCE_ATTR_ENV])) {
180                 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
181                         return false;
182
183                 blobmsg_list_fill(&in->env, blobmsg_data(cur), blobmsg_data_len(cur), false);
184         }
185
186         if ((cur = tb[INSTANCE_ATTR_DATA])) {
187                 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
188                         return false;
189
190                 blobmsg_list_fill(&in->data, blobmsg_data(cur), blobmsg_data_len(cur), false);
191         }
192
193         if ((cur = tb[INSTANCE_ATTR_NETDEV])) {
194                 struct blobmsg_list_node *ndev;
195
196                 if (!blobmsg_check_attr_list(cur, BLOBMSG_TYPE_STRING))
197                         return false;
198
199                 blobmsg_list_fill(&in->netdev, blobmsg_data(cur), blobmsg_data_len(cur), true);
200                 blobmsg_list_for_each(&in->netdev, ndev)
201                         instance_netdev_update(ndev);
202         }
203
204         return true;
205 }
206
207 static void
208 instance_config_cleanup(struct service_instance *in)
209 {
210         blobmsg_list_free(&in->env);
211         blobmsg_list_free(&in->data);
212         blobmsg_list_free(&in->netdev);
213 }
214
215 static void
216 instance_config_move(struct service_instance *in, struct service_instance *in_src)
217 {
218         instance_config_cleanup(in);
219         blobmsg_list_move(&in->env, &in_src->env);
220         blobmsg_list_move(&in->data, &in_src->data);
221         blobmsg_list_move(&in->netdev, &in_src->netdev);
222         in->command = in_src->command;
223         in->name = in_src->name;
224         in->node.avl.key = in_src->node.avl.key;
225
226         free(in->config);
227         in->config = in_src->config;
228         in_src->config = NULL;
229 }
230
231 bool
232 instance_update(struct service_instance *in, struct service_instance *in_new)
233 {
234         bool changed = instance_config_changed(in, in_new);
235
236         if (!changed)
237                 return false;
238
239         in->restart = true;
240         instance_stop(in, true);
241         instance_config_move(in, in_new);
242         return true;
243 }
244
245 void
246 instance_free(struct service_instance *in)
247 {
248         uloop_process_delete(&in->proc);
249         uloop_timeout_cancel(&in->timeout);
250         instance_config_cleanup(in);
251         free(in->config);
252         free(in);
253 }
254
255 void
256 instance_init(struct service_instance *in, struct service *s, struct blob_attr *config)
257 {
258         config = blob_memdup(config);
259         in->srv = s;
260         in->name = blobmsg_name(config);
261         in->config = config;
262         in->timeout.cb = instance_timeout;
263         in->proc.cb = instance_exit;
264
265         blobmsg_list_init(&in->netdev, struct instance_netdev, node, instance_netdev_cmp);
266         blobmsg_list_simple_init(&in->env);
267         blobmsg_list_simple_init(&in->data);
268         in->valid = instance_config_parse(in);
269 }
270
271 void instance_dump(struct blob_buf *b, struct service_instance *in)
272 {
273         void *i;
274
275         i = blobmsg_open_table(b, in->name);
276         blobmsg_add_u8(b, "running", in->proc.pending);
277         if (in->proc.pending)
278                 blobmsg_add_u32(b, "pid", in->proc.pid);
279         blobmsg_add_blob(b, in->command);
280         blobmsg_close_table(b, i);
281 }