chiark / gitweb /
logging: Provide lg_exitstatus
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 24 Sep 2014 23:47:47 +0000 (00:47 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 8 Oct 2014 17:25:18 +0000 (18:25 +0100)
This will allow us to remove a bunch of formulaic exit status
handling.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
log.c
secnet.h

diff --git a/log.c b/log.c
index 156f3549df18c23c481cc017f587ea96b475be79..3748f9157d15dc7778847201c0eaed777f39fdc5 100644 (file)
--- a/log.c
+++ b/log.c
@@ -225,6 +225,23 @@ void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
     va_end(al);
 }
 
+void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
+                  int class, int status, const char *progname)
+{
+    if (!status)
+       lg_perror(lg,desc,loc,class,0,"%s exited",progname);
+    else if (WIFEXITED(status))
+       lg_perror(lg,desc,loc,class,0,"%s exited with error exit status %d",
+                 progname,WEXITSTATUS(status));
+    else if (WIFSIGNALED(status))
+       lg_perror(lg,desc,loc,class,0,"%s died due to fatal signal %s (%d)%s",
+                 progname,strsignal(WTERMSIG(status)),WTERMSIG(status),
+                 WCOREDUMP(status)?" (core dumped)":"");
+    else
+       lg_perror(lg,desc,loc,class,0,"%s died with unknown wait status %d",
+                 progname,status);
+}
+
 struct log_if *init_log(list_t *ll)
 {
     int i=0;
index 5d72f956ef4587851acb8b4f8d32943b6b140757..3b54defdf96b69529d04dafcd14d7523554b5075 100644 (file)
--- a/secnet.h
+++ b/secnet.h
@@ -16,6 +16,7 @@
 #include <errno.h>
 #include <sys/poll.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <sys/time.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -597,6 +598,8 @@ void lg_vperror(struct log_if *lg, const char *desc, struct cloc *loc,
 void lg_perror(struct log_if *lg, const char *desc, struct cloc *loc,
               int class, int errnoval, const char *fmt, ...)
     FORMAT(printf,6,7);
+void lg_exitstatus(struct log_if *lg, const char *desc, struct cloc *loc,
+                  int class, int status, const char *progname);
 
 /* The cfgfatal() family of functions require messages that end in '\n' */
 extern NORETURN(cfgfatal(struct cloc loc, cstring_t facility,