From d6eab944e3be68d244b5cf36f8e89c024cfe2da6 Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Mon, 19 Dec 2011 18:52:50 +0400 Subject: [PATCH] Move logging functions to log.c Instead of keeping all unrelated to C/R procedure helpers in util.c move logging related helpers to log.c. Signed-off-by: Cyrill Gorcunov --- Makefile | 1 + cr-restore.c | 3 ++- crtools.c | 5 ++-- include/log.h | 8 ++++++ include/util.h | 2 -- log.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ util.c | 38 ++------------------------- 7 files changed, 85 insertions(+), 41 deletions(-) create mode 100644 include/log.h create mode 100644 log.c diff --git a/Makefile b/Makefile index 1f968602b..4eb2de2f1 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,7 @@ OBJS += cr-show.o OBJS += util.o OBJS += seize.o OBJS += restorer.o +OBJS += log.o DEPS := $(patsubst %.o,%.d,$(OBJS)) diff --git a/cr-restore.c b/cr-restore.c index 8bc0914f3..9d363151e 100644 --- a/cr-restore.c +++ b/cr-restore.c @@ -27,6 +27,7 @@ #include "image.h" #include "util.h" +#include "log.h" #include "syscall.h" #include "restorer.h" @@ -1759,7 +1760,7 @@ static void sigreturn_restore(pid_t pstree_pid, pid_t pid) task_args->thread_args); close_safe(&fd_pstree); - deinit_logging(); + fini_log(); /* * An indirect call to task_restore, note it never resturns diff --git a/crtools.c b/crtools.c index a7ce61bc3..ff218549b 100644 --- a/crtools.c +++ b/crtools.c @@ -25,6 +25,7 @@ #include "compiler.h" #include "crtools.h" #include "util.h" +#include "log.h" static struct cr_options opts; struct page_entry zero_page_entry; @@ -310,7 +311,7 @@ int main(int argc, char *argv[]) } break; case 'o': - if (init_logging(optarg)) + if (init_log(optarg)) return -1; log_inited = 1; break; @@ -321,7 +322,7 @@ int main(int argc, char *argv[]) } if (!log_inited) { - ret = init_logging(NULL); + ret = init_log(NULL); if (ret) return ret; } diff --git a/include/log.h b/include/log.h new file mode 100644 index 000000000..828e631a1 --- /dev/null +++ b/include/log.h @@ -0,0 +1,8 @@ +#ifndef LOG_H__ +#define LOG_H__ + +extern int init_log(const char *name); +extern void fini_log(void); +extern int get_logfd(void); + +#endif /* LOG_H__ */ diff --git a/include/util.h b/include/util.h index c6dd7e50b..b4099f87f 100644 --- a/include/util.h +++ b/include/util.h @@ -15,8 +15,6 @@ #include "compiler.h" #include "types.h" -extern int init_logging(const char *file_name); -extern void deinit_logging(void); extern void printk(const char *format, ...); #define PREF_SHIFT_OP(pref, op, size) ((size) op (pref ##BYTES_SHIFT)) diff --git a/log.c b/log.c new file mode 100644 index 000000000..98ba570fa --- /dev/null +++ b/log.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "compiler.h" +#include "types.h" +#include "util.h" + +/* + * Note pr_ helpers rely on this + * descriptor! + */ +static int logfd = STDERR_FILENO; + +int get_logfd(void) +{ + return logfd; +} + +int init_log(const char *name) +{ + struct rlimit rlimit; + int fd = STDERR_FILENO; + + if (name) { + fd = open(name, O_CREAT | O_WRONLY); + if (fd == -1) { + pr_perror("Can't create log file %s\n", name); + return -1; + } + } + + if (getrlimit(RLIMIT_NOFILE, &rlimit)) { + pr_err("can't get rlimit: %m\n"); + return -1; + } + + logfd = rlimit.rlim_cur - 1; + if (dup2(fd, logfd) < 0) { + pr_err("can't duplicate descriptor %d->%d: %m\n", + fd, logfd); + logfd = STDERR_FILENO; + return -1; + } + + return 0; +} + +void fini_log(void) +{ + if (logfd != STDERR_FILENO && + logfd != STDIN_FILENO && + logfd != STDERR_FILENO) + close(logfd); + + logfd = STDERR_FILENO; +} diff --git a/util.c b/util.c index 9d8b3fc08..b44ccf79d 100644 --- a/util.c +++ b/util.c @@ -37,50 +37,16 @@ #include "types.h" #include "list.h" #include "util.h" +#include "log.h" #include "crtools.h" -static int logfd = STDERR_FILENO; - -int init_logging(const char *name) -{ - struct rlimit rlimit; - int fd = STDERR_FILENO; - - if (name) { - fd = open(name, O_CREAT | O_WRONLY); - if (fd == -1) { - pr_perror("Can't create log file %s\n", name); - return -1; - } - } - - if (getrlimit(RLIMIT_NOFILE, &rlimit)) { - pr_err("can't get rlimit: %m\n"); - return -1; - } - - logfd = rlimit.rlim_cur - 1; - if (dup2(fd, logfd) < 0) { - pr_err("can't duplicate descriptor 2->%d: %m\n", logfd); - return -1; - } - - return 0; -} - -void deinit_logging(void) -{ - close(logfd); - logfd = -1; -} - void printk(const char *format, ...) { va_list params; va_start(params, format); - vdprintf(logfd, format, params); + vdprintf(get_logfd(), format, params); va_end(params); }