mirror of
https://github.com/checkpoint-restore/criu
synced 2025-08-31 22:35:33 +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:
69
log.c
Normal file
69
log.c
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user