[CRIU] [PATCH 1/2] log: add log_rotate()

Ruslan Kuprieiev kupruser at gmail.com
Mon Feb 10 06:02:07 PST 2014


This function also might be in handy when\if we will add multireq mode to rpc.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 include/log.h |  1 +
 log.c         | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

diff --git a/include/log.h b/include/log.h
index 9948098..01a98fc 100644
--- a/include/log.h
+++ b/include/log.h
@@ -5,6 +5,7 @@
 
 extern int log_init(const char *output);
 extern void log_fini(void);
+extern int log_rotate(void);
 extern int log_init_by_pid(void);
 extern void log_closedir(void);
 
diff --git a/log.c b/log.c
index c5ad472..da944e9 100644
--- a/log.c
+++ b/log.c
@@ -133,6 +133,39 @@ void log_fini(void)
 	close_service_fd(LOG_FD_OFF);
 }
 
+int log_rotate(void)
+{
+	char buf[PATH_MAX];
+	char log_path[PATH_MAX];
+	int len;
+	int log_fd;
+
+	log_fd = log_get_fd();
+	if (log_fd == DEFAULT_LOGFD)
+		return 0;
+
+	snprintf(buf, PATH_MAX, "/proc/self/fd/%d", log_get_fd());
+
+	len = readlink(buf, log_path, PATH_MAX);
+	if (len < 0) {
+		pr_perror("Can't readlink %s", buf);
+		return -1;
+	}
+
+	log_path[len] = '\0';
+
+	snprintf(buf, PATH_MAX, "%s.old", log_path);
+
+	log_fini();
+
+	if (rename(log_path, buf)) {
+		pr_perror("Can't rename logfile");
+		return -1;
+	}
+
+	return log_init(log_path);
+}
+
 void log_set_loglevel(unsigned int level)
 {
 	if (level == LOG_UNSET)
-- 
1.8.3.2



More information about the CRIU mailing list