]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
console: add "write_logfile" to console_log struct
authorChristian Brauner <christian.brauner@ubuntu.com>
Thu, 26 Oct 2017 21:49:21 +0000 (23:49 +0200)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 6 Nov 2017 23:54:54 +0000 (00:54 +0100)
If a console log file was specified this flag indicates whether the contents of
the ringbuffer should be written to the logfile when a request is sent to the
ringbuffer.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/commands.c
src/lxc/commands.h
src/lxc/console.c
src/lxc/console.h
src/lxc/lxccontainer.c
src/lxc/lxccontainer.h

index 36570714f4219064338d2f445017a013bcfbd976..880cb28c2f66a02dfb371d83fbfaacf2c9c71b90 100644 (file)
@@ -994,6 +994,7 @@ int lxc_cmd_console_log(const char *name, const char *lxcpath,
        data.clear = log->clear;
        data.read = log->read;
        data.read_max = *log->read_max;
+       data.write_logfile = log->write_logfile;
 
        cmd.req.cmd = LXC_CMD_CONSOLE_LOG;
        cmd.req.data = &data;
@@ -1006,7 +1007,7 @@ int lxc_cmd_console_log(const char *name, const char *lxcpath,
        /* There is nothing to be read from the buffer. So clear any values we
         * where passed to clearly indicate to the user that nothing went wrong.
         */
-       if (cmd.rsp.ret == -ENODATA || cmd.rsp.ret == -EFAULT) {
+       if (cmd.rsp.ret == -ENODATA || cmd.rsp.ret == -EFAULT || cmd.rsp.ret == -ENOENT) {
                *log->read_max = 0;
                log->data = NULL;
        }
@@ -1027,6 +1028,7 @@ static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req,
        struct lxc_cmd_rsp rsp;
        uint64_t logsize = handler->conf->console.log_size;
        const struct lxc_cmd_console_log *log = req->data;
+       struct lxc_console *console = &handler->conf->console;
        struct lxc_ringbuf *buf = &handler->conf->console.ringbuf;
 
        rsp.ret = -EFAULT;
@@ -1043,10 +1045,21 @@ static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req,
                rsp.datalen = log->read_max;
 
        /* there's nothing to read */
+       rsp.ret = -ENODATA;
        if (log->read && (buf->r_off == buf->w_off))
-               rsp.ret = -ENODATA;
-       else
-               rsp.ret = 0;
+               goto out;
+
+       if (log->write_logfile && rsp.datalen > 0) {
+               rsp.ret = -ENOENT;
+               if (!console->log_path)
+                       goto out;
+
+               rsp.ret = lxc_console_write_ringbuffer(console);
+               if (rsp.ret < 0)
+                       goto out;
+       }
+
+       rsp.ret = 0;
 
        if (log->clear)
                lxc_ringbuf_clear(buf);
index 416cfded62a941842e8cfee056fe97ca1e95f22b..7e197b98d9565f0c7bdc4f44b965b0f704724372 100644 (file)
@@ -85,6 +85,7 @@ struct lxc_cmd_console_log {
        bool clear;
        bool read;
        uint64_t read_max;
+       bool write_logfile;
 
 };
 
index 98dda69ffb75ed9c9e000e97c338790c0c1fd926..659f86df507296eb39b0fd113e0d2ee43b5f9152 100644 (file)
@@ -512,7 +512,7 @@ out:
        return ret;
 }
 
-static int lxc_console_write_ringbuffer(struct lxc_console *console)
+int lxc_console_write_ringbuffer(struct lxc_console *console)
 {
        int fd;
        char *r_addr;
@@ -527,10 +527,10 @@ static int lxc_console_write_ringbuffer(struct lxc_console *console)
        if (used == 0)
                return 0;
 
-       fd = lxc_unpriv(open(console->log_path, O_CLOEXEC | O_RDWR | O_CREAT, 0600));
+       fd = lxc_unpriv(open(console->log_path, O_CLOEXEC | O_RDWR | O_CREAT | O_TRUNC, 0600));
        if (fd < 0) {
                SYSERROR("Failed to open console log file \"%s\"", console->log_path);
-               return -1;
+               return -EIO;
        }
        DEBUG("Using \"%s\" as console log file", console->log_path);
 
@@ -538,7 +538,7 @@ static int lxc_console_write_ringbuffer(struct lxc_console *console)
        ret = lxc_write_nointr(fd, r_addr, used);
        close(fd);
        if (ret < 0)
-               return -1;
+               return -EIO;
 
        return 0;
 }
index 2453d60d446132ac05addfa7604216d1bdbc9487..50dfd2e1cb1ebdfcf3f7ceea5eb370451a08fba6 100644 (file)
@@ -215,4 +215,6 @@ extern int lxc_console_cb_sigwinch_fd(int fd, uint32_t events, void *cbdata,
  */
 extern void lxc_console_sigwinch_fini(struct lxc_tty_state *ts);
 
+extern int lxc_console_write_ringbuffer(struct lxc_console *console);
+
 #endif
index 8c0487853c7a65a213ded52107f0b704a1654b8a..7c5b504b60f8faf84944f2b4422060776029b10e 100644 (file)
@@ -523,11 +523,15 @@ static int do_lxcapi_console_log(struct lxc_container *c, struct lxc_console_log
        ret = lxc_cmd_console_log(c->name, do_lxcapi_get_config_path(c), log);
        if (ret < 0) {
                if (ret == -ENODATA)
-                       NOTICE("%s - The console log is empty", strerror(-ret));
+                       NOTICE("The console log is empty");
                else if (ret == -EFAULT)
-                       NOTICE("%s - The container does not have a console log configured", strerror(-ret));
+                       NOTICE("The container does not keep a console log");
+               else if (ret == -ENOENT)
+                       NOTICE("The container does not keep a console log file");
+               else if (ret == -EIO)
+                       NOTICE("Failed to write console log to log file");
                else
-                       ERROR("%s - Failed to retrieve console log", strerror(-ret));
+                       ERROR("Failed to retrieve console log");
        }
 
        return ret;
index 22ab852781ed04272dfcbbe1af1e8c0a257f436e..9ea67df7f1d83b665aa67cad7a017c1a0e2328fa 100644 (file)
@@ -953,6 +953,12 @@ struct lxc_console_log {
         * "data" is invalid.
         */
        char *data;
+
+       /* If a console log file was specified this flag indicates whether the
+        * contents of the ringbuffer should be written to the logfile when a
+        * request is sent to the ringbuffer.
+        */
+       bool write_logfile;
 };
 
 /*!