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>
28 #include "../utils/md5.h"
31 INSTANCE_ATTR_COMMAND
,
36 INSTANCE_ATTR_TRIGGER
,
37 INSTANCE_ATTR_RESPAWN
,
42 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
43 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
44 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
45 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
46 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
47 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
48 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
49 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
50 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
53 struct instance_netdev
{
54 struct blobmsg_list_node node
;
58 struct instance_file
{
59 struct blobmsg_list_node node
;
64 instance_run(struct service_instance
*in
)
66 struct blobmsg_list_node
*var
;
67 struct blob_attr
*cur
;
69 int argc
= 1; /* NULL terminated */
73 setpriority(PRIO_PROCESS
, 0, in
->nice
);
75 blobmsg_for_each_attr(cur
, in
->command
, rem
)
78 blobmsg_list_for_each(&in
->env
, var
)
79 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
81 argv
= alloca(sizeof(char *) * argc
);
84 blobmsg_for_each_attr(cur
, in
->command
, rem
)
85 argv
[argc
++] = blobmsg_data(cur
);
88 fd
= open("/dev/null", O_RDWR
);
90 dup2(fd
, STDIN_FILENO
);
91 dup2(fd
, STDOUT_FILENO
);
92 dup2(fd
, STDERR_FILENO
);
93 if (fd
> STDERR_FILENO
)
96 execvp(argv
[0], argv
);
101 instance_start(struct service_instance
*in
)
105 if (in
->proc
.pending
)
109 in
->halt
= !in
->respawn
;
124 DEBUG(2, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
126 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
127 uloop_process_add(&in
->proc
);
131 instance_timeout(struct uloop_timeout
*t
)
133 struct service_instance
*in
;
135 in
= container_of(t
, struct service_instance
, timeout
);
137 if (!in
->halt
&& (in
->restart
|| in
->respawn
))
142 instance_exit(struct uloop_process
*p
, int ret
)
144 struct service_instance
*in
;
148 in
= container_of(p
, struct service_instance
, proc
);
150 clock_gettime(CLOCK_MONOTONIC
, &tp
);
151 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
153 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
157 uloop_timeout_cancel(&in
->timeout
);
160 } else if (in
->restart
) {
162 } else if (in
->respawn
) {
163 if (runtime
< in
->respawn_threshold
)
166 in
->respawn_count
= 0;
167 if (in
->respawn_count
> in
->respawn_retry
) {
168 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
169 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
170 in
->restart
= in
->respawn
= 0;
173 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
179 instance_stop(struct service_instance
*in
)
181 if (!in
->proc
.pending
)
184 in
->restart
= in
->respawn
= false;
185 kill(in
->proc
.pid
, SIGTERM
);
189 instance_restart(struct service_instance
*in
)
191 if (!in
->proc
.pending
)
195 kill(in
->proc
.pid
, SIGTERM
);
199 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
204 if (!blob_attr_equal(in
->command
, in_new
->command
))
207 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
210 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
213 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
216 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
219 if (in
->nice
!= in_new
->nice
)
226 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
228 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
229 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
231 return n1
->ifindex
== n2
->ifindex
;
235 instance_netdev_update(struct blobmsg_list_node
*l
)
237 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
239 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
243 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
245 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
246 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
248 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
252 instance_file_update(struct blobmsg_list_node
*l
)
254 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
259 memset(f
->md5
, 0, sizeof(f
->md5
));
261 fd
= open(l
->avl
.key
, O_RDONLY
);
267 len
= read(fd
, buf
, sizeof(buf
));
277 md5_hash(buf
, len
, &md5
);
280 md5_end(f
->md5
, &md5
);
285 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
287 struct blobmsg_list_node
*node
;
292 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
295 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
297 blobmsg_list_for_each(l
, node
)
304 instance_config_parse(struct service_instance
*in
)
306 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
307 struct blob_attr
*cur
, *cur2
;
311 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
312 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
314 cur
= tb
[INSTANCE_ATTR_COMMAND
];
318 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
321 blobmsg_for_each_attr(cur2
, cur
, rem
) {
330 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
332 uint32_t vals
[3] = { 3600, 5, 5};
334 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
335 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
337 vals
[i
] = atoi(blobmsg_get_string(cur2
));
341 in
->respawn_count
= 0;
342 in
->respawn_threshold
= vals
[0];
343 in
->respawn_timeout
= vals
[1];
344 in
->respawn_retry
= vals
[2];
346 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
347 in
->trigger
= malloc(blob_pad_len(tb
[INSTANCE_ATTR_TRIGGER
]));
350 memcpy(in
->trigger
, tb
[INSTANCE_ATTR_TRIGGER
], blob_pad_len(tb
[INSTANCE_ATTR_TRIGGER
]));
351 trigger_add(in
->trigger
, in
);
354 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
355 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
356 if (in
->nice
< -20 || in
->nice
> 20)
360 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
363 if (!instance_fill_array(&in
->data
, tb
[INSTANCE_ATTR_DATA
], NULL
, false))
366 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
369 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
376 instance_config_cleanup(struct service_instance
*in
)
378 blobmsg_list_free(&in
->env
);
379 blobmsg_list_free(&in
->data
);
380 blobmsg_list_free(&in
->netdev
);
384 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
386 instance_config_cleanup(in
);
387 blobmsg_list_move(&in
->env
, &in_src
->env
);
388 blobmsg_list_move(&in
->data
, &in_src
->data
);
389 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
390 in
->trigger
= in_src
->trigger
;
391 in
->command
= in_src
->command
;
392 in
->name
= in_src
->name
;
393 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
396 in
->config
= in_src
->config
;
397 in_src
->config
= NULL
;
401 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
403 bool changed
= instance_config_changed(in
, in_new
);
404 bool running
= in
->proc
.pending
;
406 if (!changed
&& running
)
411 instance_config_move(in
, in_new
);
414 instance_restart(in
);
415 instance_config_move(in
, in_new
);
416 /* restart happens in the child callback handler */
422 instance_free(struct service_instance
*in
)
424 uloop_process_delete(&in
->proc
);
425 uloop_timeout_cancel(&in
->timeout
);
428 instance_config_cleanup(in
);
434 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
436 config
= blob_memdup(config
);
438 in
->name
= blobmsg_name(config
);
440 in
->timeout
.cb
= instance_timeout
;
441 in
->proc
.cb
= instance_exit
;
443 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
444 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
445 blobmsg_list_simple_init(&in
->env
);
446 blobmsg_list_simple_init(&in
->data
);
447 in
->valid
= instance_config_parse(in
);
450 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
454 i
= blobmsg_open_table(b
, in
->name
);
455 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
456 if (in
->proc
.pending
)
457 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
458 blobmsg_add_blob(b
, in
->command
);
460 if (!avl_is_empty(&in
->env
.avl
)) {
461 struct blobmsg_list_node
*var
;
462 void *e
= blobmsg_open_table(b
, "env");
463 blobmsg_list_for_each(&in
->env
, var
)
464 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
465 blobmsg_close_table(b
, e
);
469 void *r
= blobmsg_open_table(b
, "respawn");
470 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
471 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
472 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
473 blobmsg_close_table(b
, r
);
476 if (verbose
&& in
->trigger
)
477 blobmsg_add_blob(b
, in
->trigger
);
479 blobmsg_close_table(b
, i
);