procd: show process's exit code
authorOndřej Votava <ondrej.votava@cvut.cz>
Thu, 23 Jan 2020 14:31:31 +0000 (15:31 +0100)
committerPetr Štetiar <ynezz@true.cz>
Fri, 24 Jan 2020 05:24:08 +0000 (06:24 +0100)
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
service/instance.h

index 658c086121850ad4a7faa9c58dc64571369cd71e..e872ba0228026d46f3cc0f07d5ee8ca0612e2bab 100644 (file)
@@ -566,6 +566,24 @@ instance_delete(struct service_instance *in)
        service_stopped(s);
 }
 
+static int
+instance_exit_code(int ret)
+{
+       if (WIFEXITED(ret)) {
+               return WEXITSTATUS(ret);
+       }
+
+       if (WIFSIGNALED(ret)) {
+               return SIGNALLED_OFFSET + WTERMSIG(ret);
+       }
+
+       if (WIFSTOPPED(ret)) {
+               return WSTOPSIG(ret);
+       }
+
+       return 1;
+}
+
 static void
 instance_exit(struct uloop_process *p, int ret)
 {
@@ -580,6 +598,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);
 
@@ -1126,6 +1145,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;
@@ -1159,6 +1179,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;
index e53c6069a3ab434915ca9eecb2e2eed7c106749c..7d91b51a34082a04d0d59afb0c615a780ea707f4 100644 (file)
@@ -21,6 +21,7 @@
 #include "../utils/utils.h"
 
 #define RESPAWN_ERROR  (5 * 60)
+#define SIGNALLED_OFFSET 128
 
 struct jail {
        bool procfs;
@@ -63,6 +64,7 @@ struct service_instance {
        char *seccomp;
        char *pidfile;
        int syslog_facility;
+       int exit_code;
 
        uint32_t term_timeout;
        uint32_t respawn_timeout;