]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Report container exit status to monitord 325/head
authorJean-Tiare LE BIGOT <jean-tiare.le-bigot@ovh.net>
Thu, 28 Aug 2014 09:32:49 +0000 (11:32 +0200)
committerJean-Tiare LE BIGOT <jean-tiare.le-bigot@ovh.net>
Thu, 4 Sep 2014 13:22:04 +0000 (15:22 +0200)
When managing containers, I need to take action based on container
exit status. For instance, if it exited abnormally (status!=0), I
sometime want to respawn it automatically. Or, when invoking
`lxc-stop` I want to know if it terminated gracefully (ie on `SIGTERM`)
or on `SIGKILL` after a timeout.

This patch adds a new message type `lxc_msg_exit_code,` to preserve
ABI. It sends the raw status code as returned by `waitpid` so that
listening application may want to apply `WEXITSTATUS` before. This is
what `lxc-monitor` does.

Signed-off-by: Jean-Tiare LE BIGOT <jean-tiare.le-bigot@ovh.net>
src/lxc/lxc_monitor.c
src/lxc/monitor.c
src/lxc/monitor.h
src/lxc/start.c

index 85993fc2fe5e853c0eb7e7190b909b3b0fd5d32d..ede19ff920545e285c208fce04d79fb67affdb9a 100644 (file)
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <regex.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <errno.h>
 
 #include "lxc.h"
@@ -172,6 +173,10 @@ int main(int argc, char *argv[])
                        printf("'%s' changed state to [%s]\n",
                               msg.name, lxc_state2str(msg.value));
                        break;
+               case lxc_msg_exit_code:
+                       printf("'%s' exited with status [%d]\n",
+                              msg.name, WEXITSTATUS(msg.value));
+                       break;
                default:
                        /* ignore garbage */
                        break;
index 59b02b3b90a28661d56dbfff5aea4a7344837994..f6d36a96bb840a82dbdad39a982859728542cb77 100644 (file)
@@ -131,6 +131,16 @@ void lxc_monitor_send_state(const char *name, lxc_state_t state, const char *lxc
        lxc_monitor_fifo_send(&msg, lxcpath);
 }
 
+void lxc_monitor_send_exit_code(const char *name, int exit_code, const char *lxcpath)
+{
+       struct lxc_msg msg = { .type = lxc_msg_exit_code,
+                              .value = exit_code };
+       strncpy(msg.name, name, sizeof(msg.name));
+       msg.name[sizeof(msg.name) - 1] = 0;
+
+       lxc_monitor_fifo_send(&msg, lxcpath);
+}
+
 
 /* routines used by monitor subscribers (lxc-monitor) */
 int lxc_monitor_close(int fd)
index 229696bd74136ad2265795ee6e2cb09aa8de8ef7..500a9f250c27a79e16de745228e3147f7d25db13 100644 (file)
@@ -32,6 +32,7 @@
 typedef enum {
        lxc_msg_state,
        lxc_msg_priority,
+       lxc_msg_exit_code,
 } lxc_msg_type_t;
 
 struct lxc_msg {
@@ -46,6 +47,8 @@ extern int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path,
                                 size_t fifo_path_sz, int do_mkdirp);
 extern void lxc_monitor_send_state(const char *name, lxc_state_t state,
                            const char *lxcpath);
+extern void lxc_monitor_send_exit_code(const char *name, int exit_code,
+                           const char *lxcpath);
 extern int lxc_monitord_spawn(const char *lxcpath);
 
 #endif
index 98849e10e721e9a5e2842f60ad7902dcc38f56c7..d9fbc8c40d974abdb9d3b28f82b24ffa03ebad64 100644 (file)
@@ -1139,6 +1139,7 @@ int __lxc_start(const char *name, struct lxc_conf *conf,
                handler->pinfd = -1;
        }
 
+       lxc_monitor_send_exit_code(name, status, handler->lxcpath);
        err =  lxc_error_set_and_log(handler->pid, status);
 out_fini:
        lxc_delete_network(handler);