2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <sys/resource.h>
16 #include <sys/types.h>
17 #include <sys/socket.h>
29 INSTANCE_ATTR_COMMAND
,
34 INSTANCE_ATTR_TRIGGER
,
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
},
49 struct instance_netdev
{
50 struct blobmsg_list_node node
;
54 struct instance_file
{
55 struct blobmsg_list_node node
;
60 instance_run(struct service_instance
*in
)
62 struct blobmsg_list_node
*var
;
63 struct blob_attr
*cur
;
65 int argc
= 1; /* NULL terminated */
69 setpriority(PRIO_PROCESS
, 0, in
->nice
);
71 blobmsg_for_each_attr(cur
, in
->command
, rem
)
74 blobmsg_list_for_each(&in
->env
, var
)
75 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
77 argv
= alloca(sizeof(char *) * argc
);
80 blobmsg_for_each_attr(cur
, in
->command
, rem
)
81 argv
[argc
++] = blobmsg_data(cur
);
84 execvp(argv
[0], argv
);
89 instance_start(struct service_instance
*in
)
109 DEBUG(1, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
111 uloop_process_add(&in
->proc
);
115 instance_timeout(struct uloop_timeout
*t
)
117 struct service_instance
*in
;
119 in
= container_of(t
, struct service_instance
, timeout
);
120 kill(in
->proc
.pid
, SIGKILL
);
121 uloop_process_delete(&in
->proc
);
122 in
->proc
.cb(&in
->proc
, -1);
126 instance_exit(struct uloop_process
*p
, int ret
)
128 struct service_instance
*in
;
130 in
= container_of(p
, struct service_instance
, proc
);
131 DEBUG(1, "Instance %s::%s exit with error code %d\n", in
->srv
->name
, in
->name
, ret
);
132 uloop_timeout_cancel(&in
->timeout
);
138 instance_stop(struct service_instance
*in
, bool restart
)
140 if (!in
->proc
.pending
)
143 kill(in
->proc
.pid
, SIGTERM
);
147 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
152 if (!blob_attr_equal(in
->command
, in_new
->command
))
155 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
158 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
161 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
164 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
167 if (in
->nice
!= in_new
->nice
)
174 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
176 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
177 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
179 return n1
->ifindex
== n2
->ifindex
;
183 instance_netdev_update(struct blobmsg_list_node
*l
)
185 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
187 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
191 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
193 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
194 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
196 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
200 instance_file_update(struct blobmsg_list_node
*l
)
202 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
207 memset(f
->md5
, 0, sizeof(f
->md5
));
209 fd
= open(l
->avl
.key
, O_RDONLY
);
215 len
= read(fd
, buf
, sizeof(buf
));
225 md5_hash(buf
, len
, &md5
);
228 md5_end(f
->md5
, &md5
);
233 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
235 struct blobmsg_list_node
*node
;
240 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
243 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
245 blobmsg_list_for_each(l
, node
)
252 instance_config_parse(struct service_instance
*in
)
254 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
255 struct blob_attr
*cur
, *cur2
;
259 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
260 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
262 cur
= tb
[INSTANCE_ATTR_COMMAND
];
266 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
269 blobmsg_for_each_attr(cur2
, cur
, rem
) {
277 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
280 trigger_add(in
->trigger
, in
);
282 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
283 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
284 if (in
->nice
< -20 || in
->nice
> 20)
288 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
291 if (!instance_fill_array(&in
->data
, tb
[INSTANCE_ATTR_DATA
], NULL
, false))
294 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
297 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
304 instance_config_cleanup(struct service_instance
*in
)
306 blobmsg_list_free(&in
->env
);
307 blobmsg_list_free(&in
->data
);
308 blobmsg_list_free(&in
->netdev
);
312 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
314 instance_config_cleanup(in
);
315 blobmsg_list_move(&in
->env
, &in_src
->env
);
316 blobmsg_list_move(&in
->data
, &in_src
->data
);
317 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
318 in
->trigger
= in_src
->trigger
;
319 in
->command
= in_src
->command
;
320 in
->name
= in_src
->name
;
321 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
324 in
->config
= in_src
->config
;
325 in_src
->config
= NULL
;
329 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
331 bool changed
= instance_config_changed(in
, in_new
);
332 bool running
= in
->proc
.pending
;
334 if (!changed
&& running
)
339 instance_config_move(in
, in_new
);
343 instance_stop(in
, true);
344 instance_config_move(in
, in_new
);
350 instance_free(struct service_instance
*in
)
352 uloop_process_delete(&in
->proc
);
353 uloop_timeout_cancel(&in
->timeout
);
355 instance_config_cleanup(in
);
361 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
363 config
= blob_memdup(config
);
365 in
->name
= blobmsg_name(config
);
367 in
->timeout
.cb
= instance_timeout
;
368 in
->proc
.cb
= instance_exit
;
370 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
371 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
372 blobmsg_list_simple_init(&in
->env
);
373 blobmsg_list_simple_init(&in
->data
);
374 in
->valid
= instance_config_parse(in
);
377 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
381 i
= blobmsg_open_table(b
, in
->name
);
382 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
383 if (in
->proc
.pending
)
384 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
385 blobmsg_add_blob(b
, in
->command
);
386 if (verbose
&& in
->trigger
)
387 blobmsg_add_blob(b
, in
->trigger
);
388 blobmsg_close_table(b
, i
);