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>
25 #include <libubox/md5.h>
34 INSTANCE_ATTR_COMMAND
,
39 INSTANCE_ATTR_TRIGGER
,
40 INSTANCE_ATTR_RESPAWN
,
51 static const struct blobmsg_policy instance_attr
[__INSTANCE_ATTR_MAX
] = {
52 [INSTANCE_ATTR_COMMAND
] = { "command", BLOBMSG_TYPE_ARRAY
},
53 [INSTANCE_ATTR_ENV
] = { "env", BLOBMSG_TYPE_TABLE
},
54 [INSTANCE_ATTR_DATA
] = { "data", BLOBMSG_TYPE_TABLE
},
55 [INSTANCE_ATTR_NETDEV
] = { "netdev", BLOBMSG_TYPE_ARRAY
},
56 [INSTANCE_ATTR_FILE
] = { "file", BLOBMSG_TYPE_ARRAY
},
57 [INSTANCE_ATTR_TRIGGER
] = { "triggers", BLOBMSG_TYPE_ARRAY
},
58 [INSTANCE_ATTR_RESPAWN
] = { "respawn", BLOBMSG_TYPE_ARRAY
},
59 [INSTANCE_ATTR_NICE
] = { "nice", BLOBMSG_TYPE_INT32
},
60 [INSTANCE_ATTR_LIMITS
] = { "limits", BLOBMSG_TYPE_TABLE
},
61 [INSTANCE_ATTR_WATCH
] = { "watch", BLOBMSG_TYPE_ARRAY
},
62 [INSTANCE_ATTR_ERROR
] = { "error", BLOBMSG_TYPE_ARRAY
},
63 [INSTANCE_ATTR_USER
] = { "user", BLOBMSG_TYPE_STRING
},
64 [INSTANCE_ATTR_STDOUT
] = { "stdout", BLOBMSG_TYPE_BOOL
},
65 [INSTANCE_ATTR_STDERR
] = { "stderr", BLOBMSG_TYPE_BOOL
},
68 struct instance_netdev
{
69 struct blobmsg_list_node node
;
73 struct instance_file
{
74 struct blobmsg_list_node node
;
83 static const struct rlimit_name rlimit_names
[] = {
85 { "core", RLIMIT_CORE
},
86 { "cpu", RLIMIT_CPU
},
87 { "data", RLIMIT_DATA
},
88 { "fsize", RLIMIT_FSIZE
},
89 { "memlock", RLIMIT_MEMLOCK
},
90 { "msgqueue", RLIMIT_MSGQUEUE
},
91 { "nice", RLIMIT_NICE
},
92 { "nofile", RLIMIT_NOFILE
},
93 { "nproc", RLIMIT_NPROC
},
94 { "rss", RLIMIT_RSS
},
95 { "rtprio", RLIMIT_RTPRIO
},
96 { "sigpending", RLIMIT_SIGPENDING
},
97 { "stack", RLIMIT_STACK
},
101 static void closefd(int fd
)
103 if (fd
> STDERR_FILENO
)
108 instance_limits(const char *limit
, const char *value
)
112 unsigned long cur
, max
;
114 for (i
= 0; rlimit_names
[i
].name
!= NULL
; i
++) {
115 if (strcmp(rlimit_names
[i
].name
, limit
))
117 if (!strcmp(value
, "unlimited")) {
118 rlim
.rlim_cur
= RLIM_INFINITY
;
119 rlim
.rlim_max
= RLIM_INFINITY
;
121 if (getrlimit(rlimit_names
[i
].resource
, &rlim
))
127 if (sscanf(value
, "%lu %lu", &cur
, &max
) < 1)
134 setrlimit(rlimit_names
[i
].resource
, &rlim
);
140 instance_run(struct service_instance
*in
, int stdout
, int stderr
)
142 struct blobmsg_list_node
*var
;
143 struct blob_attr
*cur
;
145 int argc
= 1; /* NULL terminated */
149 setpriority(PRIO_PROCESS
, 0, in
->nice
);
151 blobmsg_for_each_attr(cur
, in
->command
, rem
)
154 blobmsg_list_for_each(&in
->env
, var
)
155 setenv(blobmsg_name(var
->data
), blobmsg_data(var
->data
), 1);
157 blobmsg_list_for_each(&in
->limits
, var
)
158 instance_limits(blobmsg_name(var
->data
), blobmsg_data(var
->data
));
160 argv
= alloca(sizeof(char *) * argc
);
163 blobmsg_for_each_attr(cur
, in
->command
, rem
)
164 argv
[argc
++] = blobmsg_data(cur
);
168 stdin
= open("/dev/null", O_RDONLY
);
171 stdout
= open("/dev/null", O_WRONLY
);
174 stderr
= open("/dev/null", O_WRONLY
);
177 dup2(stdin
, STDIN_FILENO
);
181 dup2(stdout
, STDOUT_FILENO
);
185 dup2(stderr
, STDERR_FILENO
);
189 if (in
->uid
|| in
->gid
) {
193 execvp(argv
[0], argv
);
198 instance_start(struct service_instance
*in
)
201 int opipe
[2] = { -1, -1 };
202 int epipe
[2] = { -1, -1 };
204 if (!avl_is_empty(&in
->errors
.avl
)) {
205 LOG("Not starting instance %s::%s, an error was indicated\n", in
->srv
->name
, in
->name
);
209 if (in
->proc
.pending
)
212 if (in
->stdout
.fd
.fd
> -2) {
214 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
215 opipe
[0] = opipe
[1] = -1;
219 if (in
->stderr
.fd
.fd
> -2) {
221 ULOG_WARN("pipe() failed: %d (%s)\n", errno
, strerror(errno
));
222 epipe
[0] = epipe
[1] = -1;
227 in
->halt
= !in
->respawn
;
240 instance_run(in
, opipe
[1], epipe
[1]);
244 DEBUG(2, "Started instance %s::%s\n", in
->srv
->name
, in
->name
);
246 clock_gettime(CLOCK_MONOTONIC
, &in
->start
);
247 uloop_process_add(&in
->proc
);
250 ustream_fd_init(&in
->stdout
, opipe
[0]);
255 ustream_fd_init(&in
->stderr
, epipe
[0]);
259 service_event("instance.start", in
->srv
->name
, in
->name
);
263 instance_stdio(struct ustream
*s
, int prio
, struct service_instance
*in
)
265 char *newline
, *str
, *arg0
, ident
[32];
269 str
= ustream_get_read_buf(s
, NULL
);
273 newline
= strchr(str
, '\n');
278 len
= newline
+ 1 - str
;
280 arg0
= basename(blobmsg_data(blobmsg_data(in
->command
)));
281 snprintf(ident
, sizeof(ident
), "%s[%d]", arg0
, in
->proc
.pid
);
283 ulog_open(ULOG_STDIO
|ULOG_SYSLOG
, LOG_DAEMON
, ident
);
284 ulog(prio
, "%s\n", str
);
285 ulog_open(ULOG_STDIO
|ULOG_SYSLOG
, LOG_DAEMON
, "procd");
287 ustream_consume(s
, len
);
292 instance_stdout(struct ustream
*s
, int bytes
)
294 instance_stdio(s
, LOG_INFO
,
295 container_of(s
, struct service_instance
, stdout
.stream
));
299 instance_stderr(struct ustream
*s
, int bytes
)
301 instance_stdio(s
, LOG_ERR
,
302 container_of(s
, struct service_instance
, stderr
.stream
));
306 instance_timeout(struct uloop_timeout
*t
)
308 struct service_instance
*in
;
310 in
= container_of(t
, struct service_instance
, timeout
);
312 if (!in
->halt
&& (in
->restart
|| in
->respawn
))
317 instance_exit(struct uloop_process
*p
, int ret
)
319 struct service_instance
*in
;
323 in
= container_of(p
, struct service_instance
, proc
);
325 clock_gettime(CLOCK_MONOTONIC
, &tp
);
326 runtime
= tp
.tv_sec
- in
->start
.tv_sec
;
328 DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in
->srv
->name
, in
->name
, ret
, runtime
);
332 uloop_timeout_cancel(&in
->timeout
);
335 } else if (in
->restart
) {
337 } else if (in
->respawn
) {
338 if (runtime
< in
->respawn_threshold
)
341 in
->respawn_count
= 0;
342 if (in
->respawn_count
> in
->respawn_retry
&& in
->respawn_retry
> 0 ) {
343 LOG("Instance %s::%s s in a crash loop %d crashes, %ld seconds since last crash\n",
344 in
->srv
->name
, in
->name
, in
->respawn_count
, runtime
);
345 in
->restart
= in
->respawn
= 0;
348 uloop_timeout_set(&in
->timeout
, in
->respawn_timeout
* 1000);
351 service_event("instance.stop", in
->srv
->name
, in
->name
);
355 instance_stop(struct service_instance
*in
)
357 if (!in
->proc
.pending
)
360 in
->restart
= in
->respawn
= false;
361 kill(in
->proc
.pid
, SIGTERM
);
365 instance_restart(struct service_instance
*in
)
367 if (!in
->proc
.pending
)
371 kill(in
->proc
.pid
, SIGTERM
);
375 instance_config_changed(struct service_instance
*in
, struct service_instance
*in_new
)
380 if (!blob_attr_equal(in
->command
, in_new
->command
))
383 if (!blobmsg_list_equal(&in
->env
, &in_new
->env
))
386 if (!blobmsg_list_equal(&in
->data
, &in_new
->data
))
389 if (!blobmsg_list_equal(&in
->netdev
, &in_new
->netdev
))
392 if (!blobmsg_list_equal(&in
->file
, &in_new
->file
))
395 if (in
->nice
!= in_new
->nice
)
398 if (in
->uid
!= in_new
->uid
)
401 if (in
->gid
!= in_new
->gid
)
404 if (!blobmsg_list_equal(&in
->limits
, &in_new
->limits
))
407 if (!blobmsg_list_equal(&in
->errors
, &in_new
->errors
))
414 instance_netdev_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
416 struct instance_netdev
*n1
= container_of(l1
, struct instance_netdev
, node
);
417 struct instance_netdev
*n2
= container_of(l2
, struct instance_netdev
, node
);
419 return n1
->ifindex
== n2
->ifindex
;
423 instance_netdev_update(struct blobmsg_list_node
*l
)
425 struct instance_netdev
*n
= container_of(l
, struct instance_netdev
, node
);
427 n
->ifindex
= if_nametoindex(n
->node
.avl
.key
);
431 instance_file_cmp(struct blobmsg_list_node
*l1
, struct blobmsg_list_node
*l2
)
433 struct instance_file
*f1
= container_of(l1
, struct instance_file
, node
);
434 struct instance_file
*f2
= container_of(l2
, struct instance_file
, node
);
436 return !memcmp(f1
->md5
, f2
->md5
, sizeof(f1
->md5
));
440 instance_file_update(struct blobmsg_list_node
*l
)
442 struct instance_file
*f
= container_of(l
, struct instance_file
, node
);
447 memset(f
->md5
, 0, sizeof(f
->md5
));
449 fd
= open(l
->avl
.key
, O_RDONLY
);
455 len
= read(fd
, buf
, sizeof(buf
));
465 md5_hash(buf
, len
, &md5
);
468 md5_end(f
->md5
, &md5
);
473 instance_fill_any(struct blobmsg_list
*l
, struct blob_attr
*cur
)
478 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), false);
482 instance_fill_array(struct blobmsg_list
*l
, struct blob_attr
*cur
, blobmsg_update_cb cb
, bool array
)
484 struct blobmsg_list_node
*node
;
489 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
492 blobmsg_list_fill(l
, blobmsg_data(cur
), blobmsg_data_len(cur
), array
);
494 blobmsg_list_for_each(l
, node
)
501 instance_config_parse(struct service_instance
*in
)
503 struct blob_attr
*tb
[__INSTANCE_ATTR_MAX
];
504 struct blob_attr
*cur
, *cur2
;
508 blobmsg_parse(instance_attr
, __INSTANCE_ATTR_MAX
, tb
,
509 blobmsg_data(in
->config
), blobmsg_data_len(in
->config
));
511 cur
= tb
[INSTANCE_ATTR_COMMAND
];
515 if (!blobmsg_check_attr_list(cur
, BLOBMSG_TYPE_STRING
))
518 blobmsg_for_each_attr(cur2
, cur
, rem
) {
527 if (tb
[INSTANCE_ATTR_RESPAWN
]) {
529 uint32_t vals
[3] = { 3600, 5, 5};
531 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_RESPAWN
], rem
) {
532 if ((i
>= 3) && (blobmsg_type(cur2
) == BLOBMSG_TYPE_STRING
))
534 vals
[i
] = atoi(blobmsg_get_string(cur2
));
538 in
->respawn_count
= 0;
539 in
->respawn_threshold
= vals
[0];
540 in
->respawn_timeout
= vals
[1];
541 in
->respawn_retry
= vals
[2];
543 if (tb
[INSTANCE_ATTR_TRIGGER
]) {
544 in
->trigger
= tb
[INSTANCE_ATTR_TRIGGER
];
545 trigger_add(in
->trigger
, in
);
548 if (tb
[INSTANCE_ATTR_WATCH
]) {
549 blobmsg_for_each_attr(cur2
, tb
[INSTANCE_ATTR_WATCH
], rem
) {
550 if (blobmsg_type(cur2
) != BLOBMSG_TYPE_STRING
)
552 DEBUG(3, "watch for %s\n", blobmsg_get_string(cur2
));
553 watch_add(blobmsg_get_string(cur2
), in
);
557 if ((cur
= tb
[INSTANCE_ATTR_NICE
])) {
558 in
->nice
= (int8_t) blobmsg_get_u32(cur
);
559 if (in
->nice
< -20 || in
->nice
> 20)
563 if (tb
[INSTANCE_ATTR_USER
]) {
564 struct passwd
*p
= getpwnam(blobmsg_get_string(tb
[INSTANCE_ATTR_USER
]));
571 if (tb
[INSTANCE_ATTR_STDOUT
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDOUT
]))
572 in
->stdout
.fd
.fd
= -1;
574 if (tb
[INSTANCE_ATTR_STDERR
] && blobmsg_get_bool(tb
[INSTANCE_ATTR_STDERR
]))
575 in
->stderr
.fd
.fd
= -1;
577 instance_fill_any(&in
->data
, tb
[INSTANCE_ATTR_DATA
]);
579 if (!instance_fill_array(&in
->env
, tb
[INSTANCE_ATTR_ENV
], NULL
, false))
582 if (!instance_fill_array(&in
->netdev
, tb
[INSTANCE_ATTR_NETDEV
], instance_netdev_update
, true))
585 if (!instance_fill_array(&in
->file
, tb
[INSTANCE_ATTR_FILE
], instance_file_update
, true))
588 if (!instance_fill_array(&in
->limits
, tb
[INSTANCE_ATTR_LIMITS
], NULL
, false))
591 if (!instance_fill_array(&in
->errors
, tb
[INSTANCE_ATTR_ERROR
], NULL
, true))
598 instance_config_cleanup(struct service_instance
*in
)
600 blobmsg_list_free(&in
->env
);
601 blobmsg_list_free(&in
->data
);
602 blobmsg_list_free(&in
->netdev
);
603 blobmsg_list_free(&in
->file
);
604 blobmsg_list_free(&in
->limits
);
605 blobmsg_list_free(&in
->errors
);
609 instance_config_move(struct service_instance
*in
, struct service_instance
*in_src
)
611 instance_config_cleanup(in
);
612 blobmsg_list_move(&in
->env
, &in_src
->env
);
613 blobmsg_list_move(&in
->data
, &in_src
->data
);
614 blobmsg_list_move(&in
->netdev
, &in_src
->netdev
);
615 blobmsg_list_move(&in
->file
, &in_src
->file
);
616 blobmsg_list_move(&in
->limits
, &in_src
->limits
);
617 blobmsg_list_move(&in
->errors
, &in_src
->errors
);
618 in
->trigger
= in_src
->trigger
;
619 in
->command
= in_src
->command
;
620 in
->name
= in_src
->name
;
621 in
->node
.avl
.key
= in_src
->node
.avl
.key
;
624 in
->config
= in_src
->config
;
625 in_src
->config
= NULL
;
629 instance_update(struct service_instance
*in
, struct service_instance
*in_new
)
631 bool changed
= instance_config_changed(in
, in_new
);
632 bool running
= in
->proc
.pending
;
634 if (!changed
&& running
)
639 instance_config_move(in
, in_new
);
642 instance_restart(in
);
643 instance_config_move(in
, in_new
);
644 /* restart happens in the child callback handler */
650 instance_free(struct service_instance
*in
)
652 if (in
->stdout
.fd
.fd
> -1) {
653 ustream_free(&in
->stdout
.stream
);
654 close(in
->stdout
.fd
.fd
);
657 if (in
->stderr
.fd
.fd
> -1) {
658 ustream_free(&in
->stderr
.stream
);
659 close(in
->stderr
.fd
.fd
);
662 uloop_process_delete(&in
->proc
);
663 uloop_timeout_cancel(&in
->timeout
);
666 instance_config_cleanup(in
);
672 instance_init(struct service_instance
*in
, struct service
*s
, struct blob_attr
*config
)
674 config
= blob_memdup(config
);
676 in
->name
= blobmsg_name(config
);
678 in
->timeout
.cb
= instance_timeout
;
679 in
->proc
.cb
= instance_exit
;
681 in
->stdout
.fd
.fd
= -2;
682 in
->stdout
.stream
.string_data
= true;
683 in
->stdout
.stream
.notify_read
= instance_stdout
;
685 in
->stderr
.fd
.fd
= -2;
686 in
->stderr
.stream
.string_data
= true;
687 in
->stderr
.stream
.notify_read
= instance_stderr
;
689 blobmsg_list_init(&in
->netdev
, struct instance_netdev
, node
, instance_netdev_cmp
);
690 blobmsg_list_init(&in
->file
, struct instance_file
, node
, instance_file_cmp
);
691 blobmsg_list_simple_init(&in
->env
);
692 blobmsg_list_simple_init(&in
->data
);
693 blobmsg_list_simple_init(&in
->limits
);
694 blobmsg_list_simple_init(&in
->errors
);
695 in
->valid
= instance_config_parse(in
);
698 void instance_dump(struct blob_buf
*b
, struct service_instance
*in
, int verbose
)
702 i
= blobmsg_open_table(b
, in
->name
);
703 blobmsg_add_u8(b
, "running", in
->proc
.pending
);
704 if (in
->proc
.pending
)
705 blobmsg_add_u32(b
, "pid", in
->proc
.pid
);
706 blobmsg_add_blob(b
, in
->command
);
708 if (!avl_is_empty(&in
->errors
.avl
)) {
709 struct blobmsg_list_node
*var
;
710 void *e
= blobmsg_open_array(b
, "errors");
711 blobmsg_list_for_each(&in
->errors
, var
)
712 blobmsg_add_string(b
, NULL
, blobmsg_data(var
->data
));
713 blobmsg_close_table(b
, e
);
716 if (!avl_is_empty(&in
->env
.avl
)) {
717 struct blobmsg_list_node
*var
;
718 void *e
= blobmsg_open_table(b
, "env");
719 blobmsg_list_for_each(&in
->env
, var
)
720 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
721 blobmsg_close_table(b
, e
);
724 if (!avl_is_empty(&in
->data
.avl
)) {
725 struct blobmsg_list_node
*var
;
726 void *e
= blobmsg_open_table(b
, "data");
727 blobmsg_list_for_each(&in
->data
, var
)
728 blobmsg_add_blob(b
, var
->data
);
729 blobmsg_close_table(b
, e
);
732 if (!avl_is_empty(&in
->limits
.avl
)) {
733 struct blobmsg_list_node
*var
;
734 void *e
= blobmsg_open_table(b
, "limits");
735 blobmsg_list_for_each(&in
->limits
, var
)
736 blobmsg_add_string(b
, blobmsg_name(var
->data
), blobmsg_data(var
->data
));
737 blobmsg_close_table(b
, e
);
741 void *r
= blobmsg_open_table(b
, "respawn");
742 blobmsg_add_u32(b
, "timeout", in
->respawn_timeout
);
743 blobmsg_add_u32(b
, "threshold", in
->respawn_threshold
);
744 blobmsg_add_u32(b
, "retry", in
->respawn_retry
);
745 blobmsg_close_table(b
, r
);
748 if (verbose
&& in
->trigger
)
749 blobmsg_add_blob(b
, in
->trigger
);
751 blobmsg_close_table(b
, i
);