[CRIU] [PATCH 1/2] log: add log_init_at and define log_init as a macro

Ruslan Kuprieiev kupruser at gmail.com
Mon Nov 18 17:15:53 PST 2013


log_init_at is more flexible and log_init may be useful somewhere too.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 include/log.h |  3 ++-
 log.c         | 13 ++++++++++---
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/include/log.h b/include/log.h
index c9f4412..0073476 100644
--- a/include/log.h
+++ b/include/log.h
@@ -3,7 +3,8 @@
 
 #include "log-levels.h"
 
-extern int log_init(const char *output);
+#define log_init(output) log_init_at(".", output)
+extern int log_init_at(const char *dir, const char *output);
 extern void log_fini(void);
 extern int log_init_by_pid(void);
 extern void log_closedir(void);
diff --git a/log.c b/log.c
index 8a6ed06..1866e76 100644
--- a/log.c
+++ b/log.c
@@ -61,15 +61,21 @@ int log_get_fd(void)
 	return fd < 0 ? DEFAULT_LOGFD : fd;
 }
 
-int log_init(const char *output)
+int log_init_at(const char *dir, const char *output)
 {
-	int new_logfd, fd;
+	int new_logfd, fd, dfd;
 
 	gettimeofday(&start, NULL);
 	buf_off = TS_BUF_OFF;
 
+	dfd = open(dir, O_RDONLY);
+	if (dfd < 0) {
+		pr_perror("Can't open work directory");
+		return -1;
+	}
+
 	if (output) {
-		new_logfd = open(output, O_CREAT|O_TRUNC|O_WRONLY|O_APPEND, 0600);
+		new_logfd = openat(dfd, output, O_CREAT|O_TRUNC|O_WRONLY|O_APPEND, 0600);
 		if (new_logfd < 0) {
 			pr_perror("Can't create log file %s", output);
 			return -1;
@@ -84,6 +90,7 @@ int log_init(const char *output)
 
 	fd = install_service_fd(LOG_FD_OFF, new_logfd);
 	close(new_logfd);
+	close(dfd);
 	if (fd < 0)
 		goto err;
 
-- 
1.8.1.2



More information about the CRIU mailing list