]> git.proxmox.com Git - mirror_lxc.git/commitdiff
commands: rotate console log file
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 15 Nov 2017 12:29:49 +0000 (13:29 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Fri, 17 Nov 2017 23:20:56 +0000 (00:20 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/commands.c
src/lxc/confile.c
src/lxc/console.c
src/lxc/console.h

index 133b6e6c1e1584ef89773a64da27d848ee58adc8..f0f3c076ac2676a487d7da97d076f0f3098924f8 100644 (file)
@@ -1064,6 +1064,10 @@ static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req,
 
        rsp.ret = 0;
        if (log->clear) {
+               int ret;
+               size_t len;
+               char *tmp;
+
                /* clear the ringbuffer */
                lxc_ringbuf_clear(buf);
 
@@ -1086,6 +1090,31 @@ static int lxc_cmd_console_log_callback(int fd, struct lxc_cmd_req *req,
                                goto out;
                        }
                }
+
+               /* rotate the console log file */
+               if (!console->log_path || console->log_rotate == 0)
+                       goto out;
+
+               /* be very certain things are kosher */
+               rsp.ret = -EBADF;
+               if (console->log_fd < 0)
+                       goto out;
+
+               len = strlen(console->log_path) + sizeof(".1");
+               tmp = alloca(len);
+
+               rsp.ret = -EFBIG;
+               ret = snprintf(tmp, len, "%s.1", console->log_path);
+               if (ret < 0 || (size_t)ret >= len)
+                       goto out;
+
+               close(console->log_fd);
+               console->log_fd = -1;
+               rsp.ret = lxc_unpriv(rename(console->log_path, tmp));
+               if (rsp.ret < 0)
+                       goto out;
+
+               rsp.ret = lxc_console_create_log_file(console);
        } else if (rsp.datalen > 0) {
                lxc_ringbuf_move_read_addr(buf, rsp.datalen);
        }
index 24e74adffd36d6ad3dd1d7121907e131d15c1e72..bff51f6d77ade3fbb4d35391018cea392ce0e851 100644 (file)
@@ -1803,8 +1803,11 @@ static int set_config_console_rotate(const char *key, const char *value,
        if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
                return -1;
 
-       if (lxc_conf->console.log_rotate > 1)
+       if (lxc_conf->console.log_rotate > 1) {
+               ERROR("The \"lxc.console.rotate\" config key can only be set "
+                     "to 0 or 1");
                return -1;
+       }
 
        return 0;
 }
index 14fe4024020c1e93c1286c4801ed2b241e539a4f..c8775de6c0b1e642d0bc5691aad0007e1178d946 100644 (file)
@@ -674,7 +674,7 @@ static int lxc_console_create_ringbuf(struct lxc_console *console)
  * This is the console log file. Please note that the console log file is
  * (implementation wise not content wise) independent of the console ringbuffer.
  */
-static int lxc_console_create_log_file(struct lxc_console *console)
+int lxc_console_create_log_file(struct lxc_console *console)
 {
        if (!console->log_path)
                return 0;
index 389914ab5c8560512a9b9e5cf2bdcfc5c4695188..60b299631908b946e3edd1b6c876a749549cd35e 100644 (file)
@@ -222,5 +222,6 @@ extern int lxc_console_cb_signal_fd(int fd, uint32_t events, void *cbdata,
 extern void lxc_console_signal_fini(struct lxc_tty_state *ts);
 
 extern int lxc_console_write_ringbuffer(struct lxc_console *console);
+extern int lxc_console_create_log_file(struct lxc_console *console);
 
 #endif