diff mbox series

[OpenWrt-Devel] procd: show process's exit code

Message ID 20200123131527.23003-1-ondrej.votava@cvut.cz
State Rejected
Headers show
Series [OpenWrt-Devel] procd: show process's exit code | expand

Commit Message

Ondřej Votava Jan. 23, 2020, 1:15 p.m. UTC
From: Ondřej Votava <ondrej.votava@cvut.cz>

Adds feature to show exit code of processes launched by procd.
The exit code is shown for finished process when ubus's
service list method is called.

The exit code value is computed according to waitpid(2)
and http://tldp.org/LDP/abs/html/exitcodes.html

Signed-off-by: Ondřej Votava <ondrej.votava@cvut.cz>
---
 service/instance.c | 16 ++++++++++++++++
 service/instance.h |  1 +
 2 files changed, 17 insertions(+)

Comments

John Crispin Jan. 23, 2020, 1:17 p.m. UTC | #1
On 23/01/2020 14:15, ondrej.votava@cvut.cz wrote:
>   
> +static int
> +instance_exit_code(int ret)
> +{
> +	if(WIFEXITED(ret)) {
           ^ missing space
> +		return WEXITSTATUS(ret);
> +	}
         no new line here
> +	else if (WIFSIGNALED(ret)) {
> +		return 128 + WTERMSIG(ret);
         is there a define for 128 ?
> +	}
> +	return 1;
> +}
> +

	John
Paul Oranje Jan. 23, 2020, 1:54 p.m. UTC | #2
Op 23 jan. 2020, om 14:15 heeft ondrej.votava@cvut.cz het volgende geschreven:
> 
> +static int
> +instance_exit_code(int ret)
> +{
> +	if(WIFEXITED(ret)) {
> +		return WEXITSTATUS(ret);
> +	}
> +	else if (WIFSIGNALED(ret)) {
The else should be omitted because, when the previous if condition is met, the program flow unconditionally returns from the function.
So just use: if (WIFSIGNALED(ret)) {
> +		return 128 + WTERMSIG(ret);
> +	}
> +	return 1;
> +}
> +
> static void
> instance_exit(struct uloop_process *p, int ret)
> {
> @@ -574,6 +586,7 @@ instance_exit(struct uloop_process *p, int ret)
> 
> 	DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
> 
> +	in->exit_code = instance_exit_code(ret);
> 	uloop_timeout_cancel(&in->timeout);
> 	service_event("instance.stop", in->srv->name, in->name);
> 
> @@ -1091,6 +1104,7 @@ instance_init(struct service_instance *in, struct service *s, struct blob_attr *
> 	in->proc.cb = instance_exit;
> 	in->term_timeout = 5;
> 	in->syslog_facility = LOG_DAEMON;
> +	in->exit_code = 0;
> 
> 	in->_stdout.fd.fd = -2;
> 	in->_stdout.stream.string_data = true;
> @@ -1124,6 +1138,8 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
> 	if (in->command)
> 		blobmsg_add_blob(b, in->command);
> 	blobmsg_add_u32(b, "term_timeout", in->term_timeout);
> +	if (!in->proc.pending)
> +		blobmsg_add_u32(b, "exit_code", in->exit_code);
> 
> 	if (!avl_is_empty(&in->errors.avl)) {
> 		struct blobmsg_list_node *var;
> diff --git a/service/instance.h b/service/instance.h
> index 42cc4be..05a2fc3 100644
> --- a/service/instance.h
> +++ b/service/instance.h
> @@ -62,6 +62,7 @@ struct service_instance {
> 	char *seccomp;
> 	char *pidfile;
> 	int syslog_facility;
> +	int exit_code;
> 
> 	uint32_t term_timeout;
> 	uint32_t respawn_timeout;
> -- 
> 2.21.0 (Apple Git-122.2)
> 
> 
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel@lists.openwrt.org
> https://lists.openwrt.org/mailman/listinfo/openwrt-devel
diff mbox series

Patch

diff --git a/service/instance.c b/service/instance.c
index abd1f34..13e7d0a 100644
--- a/service/instance.c
+++ b/service/instance.c
@@ -560,6 +560,18 @@  instance_delete(struct service_instance *in)
 	service_stopped(s);
 }
 
+static int
+instance_exit_code(int ret)
+{
+	if(WIFEXITED(ret)) {
+		return WEXITSTATUS(ret);
+	}
+	else if (WIFSIGNALED(ret)) {
+		return 128 + WTERMSIG(ret);
+	}
+	return 1;
+}
+
 static void
 instance_exit(struct uloop_process *p, int ret)
 {
@@ -574,6 +586,7 @@  instance_exit(struct uloop_process *p, int ret)
 
 	DEBUG(2, "Instance %s::%s exit with error code %d after %ld seconds\n", in->srv->name, in->name, ret, runtime);
 
+	in->exit_code = instance_exit_code(ret);
 	uloop_timeout_cancel(&in->timeout);
 	service_event("instance.stop", in->srv->name, in->name);
 
@@ -1091,6 +1104,7 @@  instance_init(struct service_instance *in, struct service *s, struct blob_attr *
 	in->proc.cb = instance_exit;
 	in->term_timeout = 5;
 	in->syslog_facility = LOG_DAEMON;
+	in->exit_code = 0;
 
 	in->_stdout.fd.fd = -2;
 	in->_stdout.stream.string_data = true;
@@ -1124,6 +1138,8 @@  void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose)
 	if (in->command)
 		blobmsg_add_blob(b, in->command);
 	blobmsg_add_u32(b, "term_timeout", in->term_timeout);
+	if (!in->proc.pending)
+		blobmsg_add_u32(b, "exit_code", in->exit_code);
 
 	if (!avl_is_empty(&in->errors.avl)) {
 		struct blobmsg_list_node *var;
diff --git a/service/instance.h b/service/instance.h
index 42cc4be..05a2fc3 100644
--- a/service/instance.h
+++ b/service/instance.h
@@ -62,6 +62,7 @@  struct service_instance {
 	char *seccomp;
 	char *pidfile;
 	int syslog_facility;
+	int exit_code;
 
 	uint32_t term_timeout;
 	uint32_t respawn_timeout;