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>
23 #include <libubox/md5.h>
32 INSTANCE_ATTR_COMMAND
,
37 INSTANCE_ATTR_TRIGGER
,
38 INSTANCE_ATTR_RESPAWN
,
43 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
44 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
45 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
46 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
47 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
48 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
49 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
50 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
51 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
54 struct instance_netdev
{
55 struct blobmsg_list_node node
;
59 struct instance_file
{
60 struct blobmsg_list_node node
;
65 instance_run(struct service_instance
*in
)
67 struct blobmsg_list_node
*var
;
68 struct blob_attr
*cur
;
70 int argc
= 1; /* NULL terminated */
74 setpriority(PRIO_PROCESS
, 0, in
->nice
);
76 blobmsg_for_each_attr(cur
, in
->command
, rem
)
79 blobmsg_list_for_each(&in
->env
, var
)
80 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
82 argv
= alloca(sizeof(char *) * argc
);
85 blobmsg_for_each_attr(cur
, in
->command
, rem
)
86 argv
[argc
++] = blobmsg_data(cur
);
89 fd
= open("/dev/null", O_RDWR
);
91 dup2(fd
, STDIN_FILENO
);
92 dup2(fd
, STDOUT_FILENO
);
93 dup2(fd
, STDERR_FILENO
);
94 if (fd
> STDERR_FILENO
)
97 execvp(argv
[0], argv
);
102 instance_start(struct service_instance
*in
)
106 if (in
->proc
.pending
)
110 in
->halt
= !in
->respawn
;
125 DEBUG(2, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
127 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
128 uloop_process_add(&in
->proc
);
132 instance_timeout(struct uloop_timeout
*t
)
134 struct service_instance
*in
;
136 in
= container_of(t
, struct service_instance
, timeout
);
138 if (!in
->halt
&& (in
->restart
|| in
->respawn
))
143 instance_exit(struct uloop_process
*p
, int ret
)
145 struct service_instance
*in
;
149 in
= container_of(p
, struct service_instance
, proc
);
151 clock_gettime(CLOCK_MONOTONIC
, &tp
);
152 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
154 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
158 uloop_timeout_cancel(&in
->timeout
);
161 } else if (in
->restart
) {
163 } else if (in
->respawn
) {
164 if (runtime
< in
->respawn_threshold
)
167 in
->respawn_count
= 0;
168 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
169 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
170 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
171 in
->restart
= in
->respawn
= 0;
174 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
180 instance_stop(struct service_instance
*in
)
182 if (!in
->proc
.pending
)
185 in
->restart
= in
->respawn
= false;
186 kill(in
->proc
.pid
, SIGTERM
);
190 instance_restart(struct service_instance
*in
)
192 if (!in
->proc
.pending
)
196 kill(in
->proc
.pid
, SIGTERM
);
200 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
205 if (!blob_attr_equal(in
->command
, in_new
->command
))
208 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
211 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
214 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
217 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
220 if (in
->nice
!= in_new
->nice
)
227 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
229 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
230 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
232 return n1
->ifindex
== n2
->ifindex
;
236 instance_netdev_update(struct blobmsg_list_node
*l
)
238 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
240 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
244 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
246 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
247 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
249 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
253 instance_file_update(struct blobmsg_list_node
*l
)
255 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
260 memset(f
->md5
, 0, sizeof(f
->md5
));
262 fd
= open(l
->avl
.key
, O_RDONLY
);
268 len
= read(fd
, buf
, sizeof(buf
));
278 md5_hash(buf
, len
, &md5
);
281 md5_end(f
->md5
, &md5
);
286 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
288 struct blobmsg_list_node
*node
;
293 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
296 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
298 blobmsg_list_for_each(l
, node
)
305 instance_config_parse(struct service_instance
*in
)
307 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
308 struct blob_attr
*cur
, *cur2
;
312 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
313 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
315 cur
= tb
[INSTANCE_ATTR_COMMAND
];
319 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
322 blobmsg_for_each_attr(cur2
, cur
, rem
) {
331 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
333 uint32_t vals
[3] = { 3600, 5, 5};
335 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
336 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
338 vals
[i
] = atoi(blobmsg_get_string(cur2
));
342 in
->respawn_count
= 0;
343 in
->respawn_threshold
= vals
[0];
344 in
->respawn_timeout
= vals
[1];
345 in
->respawn_retry
= vals
[2];
347 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
348 in
->trigger
= malloc(blob_pad_len(tb
[INSTANCE_ATTR_TRIGGER
]));
351 memcpy(in
->trigger
, tb
[INSTANCE_ATTR_TRIGGER
], blob_pad_len(tb
[INSTANCE_ATTR_TRIGGER
]));
352 trigger_add(in
->trigger
, in
);
355 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
356 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
357 if (in
->nice
< -20 || in
->nice
> 20)
361 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
364 if (!instance_fill_array(&in
->data
, tb
[INSTANCE_ATTR_DATA
], NULL
, false))
367 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
370 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
377 instance_config_cleanup(struct service_instance
*in
)
379 blobmsg_list_free(&in
->env
);
380 blobmsg_list_free(&in
->data
);
381 blobmsg_list_free(&in
->netdev
);
385 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
387 instance_config_cleanup(in
);
388 blobmsg_list_move(&in
->env
, &in_src
->env
);
389 blobmsg_list_move(&in
->data
, &in_src
->data
);
390 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
391 in
->trigger
= in_src
->trigger
;
392 in
->command
= in_src
->command
;
393 in
->name
= in_src
->name
;
394 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
397 in
->config
= in_src
->config
;
398 in_src
->config
= NULL
;
402 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
404 bool changed
= instance_config_changed(in
, in_new
);
405 bool running
= in
->proc
.pending
;
407 if (!changed
&& running
)
412 instance_config_move(in
, in_new
);
415 instance_restart(in
);
416 instance_config_move(in
, in_new
);
417 /* restart happens in the child callback handler */
423 instance_free(struct service_instance
*in
)
425 uloop_process_delete(&in
->proc
);
426 uloop_timeout_cancel(&in
->timeout
);
429 instance_config_cleanup(in
);
435 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
437 config
= blob_memdup(config
);
439 in
->name
= blobmsg_name(config
);
441 in
->timeout
.cb
= instance_timeout
;
442 in
->proc
.cb
= instance_exit
;
444 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
445 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
446 blobmsg_list_simple_init(&in
->env
);
447 blobmsg_list_simple_init(&in
->data
);
448 in
->valid
= instance_config_parse(in
);
451 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
455 i
= blobmsg_open_table(b
, in
->name
);
456 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
457 if (in
->proc
.pending
)
458 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
459 blobmsg_add_blob(b
, in
->command
);
461 if (!avl_is_empty(&in
->env
.avl
)) {
462 struct blobmsg_list_node
*var
;
463 void *e
= blobmsg_open_table(b
, "env");
464 blobmsg_list_for_each(&in
->env
, var
)
465 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
466 blobmsg_close_table(b
, e
);
470 void *r
= blobmsg_open_table(b
, "respawn");
471 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
472 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
473 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
474 blobmsg_close_table(b
, r
);
477 if (verbose
&& in
->trigger
)
478 blobmsg_add_blob(b
, in
->trigger
);
480 blobmsg_close_table(b
, i
);