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
,
38 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
39 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
40 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
41 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
42 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
43 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
44 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
47 struct instance_netdev
{
48 struct blobmsg_list_node node
;
52 struct instance_file
{
53 struct blobmsg_list_node node
;
58 instance_run(struct service_instance
*in
)
60 struct blobmsg_list_node
*var
;
61 struct blob_attr
*cur
;
63 int argc
= 1; /* NULL terminated */
67 setpriority(PRIO_PROCESS
, 0, in
->nice
);
69 blobmsg_for_each_attr(cur
, in
->command
, rem
)
72 blobmsg_list_for_each(&in
->env
, var
)
73 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
75 argv
= alloca(sizeof(char *) * argc
);
78 blobmsg_for_each_attr(cur
, in
->command
, rem
)
79 argv
[argc
++] = blobmsg_data(cur
);
82 execvp(argv
[0], argv
);
87 instance_start(struct service_instance
*in
)
107 DEBUG(1, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
109 uloop_process_add(&in
->proc
);
113 instance_timeout(struct uloop_timeout
*t
)
115 struct service_instance
*in
;
117 in
= container_of(t
, struct service_instance
, timeout
);
118 kill(in
->proc
.pid
, SIGKILL
);
119 uloop_process_delete(&in
->proc
);
120 in
->proc
.cb(&in
->proc
, -1);
124 instance_exit(struct uloop_process
*p
, int ret
)
126 struct service_instance
*in
;
128 in
= container_of(p
, struct service_instance
, proc
);
129 LOG("Instance %s::%s exit with error code %d\n", in
->srv
->name
, in
->name
, ret
);
130 uloop_timeout_cancel(&in
->timeout
);
136 instance_stop(struct service_instance
*in
, bool restart
)
138 if (!in
->proc
.pending
)
141 kill(in
->proc
.pid
, SIGTERM
);
145 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
150 if (!blob_attr_equal(in
->command
, in_new
->command
))
153 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
156 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
159 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
162 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
165 if (in
->nice
!= in_new
->nice
)
172 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
174 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
175 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
177 return n1
->ifindex
== n2
->ifindex
;
181 instance_netdev_update(struct blobmsg_list_node
*l
)
183 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
185 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
189 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
191 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
192 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
194 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
198 instance_file_update(struct blobmsg_list_node
*l
)
200 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
205 memset(f
->md5
, 0, sizeof(f
->md5
));
207 fd
= open(l
->avl
.key
, O_RDONLY
);
213 len
= read(fd
, buf
, sizeof(buf
));
223 md5_hash(buf
, len
, &md5
);
226 md5_end(f
->md5
, &md5
);
231 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
233 struct blobmsg_list_node
*node
;
238 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
241 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
243 blobmsg_list_for_each(l
, node
)
250 instance_config_parse(struct service_instance
*in
)
252 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
253 struct blob_attr
*cur
, *cur2
;
257 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
258 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
260 cur
= tb
[INSTANCE_ATTR_COMMAND
];
264 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
267 blobmsg_for_each_attr(cur2
, cur
, rem
) {
276 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
277 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
278 if (in
->nice
< -20 || in
->nice
> 20)
282 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
285 if (!instance_fill_array(&in
->data
, tb
[INSTANCE_ATTR_DATA
], NULL
, false))
288 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
291 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
298 instance_config_cleanup(struct service_instance
*in
)
300 blobmsg_list_free(&in
->env
);
301 blobmsg_list_free(&in
->data
);
302 blobmsg_list_free(&in
->netdev
);
306 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
308 instance_config_cleanup(in
);
309 blobmsg_list_move(&in
->env
, &in_src
->env
);
310 blobmsg_list_move(&in
->data
, &in_src
->data
);
311 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
312 in
->command
= in_src
->command
;
313 in
->name
= in_src
->name
;
314 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
317 in
->config
= in_src
->config
;
318 in_src
->config
= NULL
;
322 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
324 bool changed
= instance_config_changed(in
, in_new
);
330 instance_stop(in
, true);
331 instance_config_move(in
, in_new
);
336 instance_free(struct service_instance
*in
)
338 uloop_process_delete(&in
->proc
);
339 uloop_timeout_cancel(&in
->timeout
);
340 instance_config_cleanup(in
);
346 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
348 config
= blob_memdup(config
);
350 in
->name
= blobmsg_name(config
);
352 in
->timeout
.cb
= instance_timeout
;
353 in
->proc
.cb
= instance_exit
;
355 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
356 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
357 blobmsg_list_simple_init(&in
->env
);
358 blobmsg_list_simple_init(&in
->data
);
359 in
->valid
= instance_config_parse(in
);
362 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
)
366 i
= blobmsg_open_table(b
, in
->name
);
367 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
368 if (in
->proc
.pending
)
369 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
370 blobmsg_add_blob(b
, in
->command
);
371 blobmsg_close_table(b
, i
);