Commit d6eab944 authored by Cyrill Gorcunov's avatar Cyrill Gorcunov

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: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent 3023837d
......@@ -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))
......
......@@ -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
......
......@@ -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;
}
......
#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__ */
......@@ -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))
......
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <stdbool.h>
#include <limits.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <errno.h>
#include <fcntl.h>
#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;
}
......@@ -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);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment