2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-29 13:28:27 +00:00

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 <gorcunov@openvz.org>
This commit is contained in:
Cyrill Gorcunov 2011-12-19 18:52:50 +04:00
parent 3023837da9
commit d6eab944e3
7 changed files with 85 additions and 41 deletions

View File

@ -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))

View File

@ -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

View File

@ -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;
}

8
include/log.h Normal file
View File

@ -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__ */

View File

@ -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))

69
log.c Normal file
View File

@ -0,0 +1,69 @@
#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;
}

38
util.c
View File

@ -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);
}