2011-12-19 18:52:50 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2012-02-17 12:17:00 +04:00
|
|
|
/* Note pr_ helpers rely on this descriptor! */
|
2011-12-19 18:52:50 +04:00
|
|
|
static int logfd = STDERR_FILENO;
|
|
|
|
|
|
|
|
int get_logfd(void)
|
|
|
|
{
|
|
|
|
return logfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int init_log(const char *name)
|
|
|
|
{
|
|
|
|
struct rlimit rlimit;
|
|
|
|
int fd = STDERR_FILENO;
|
|
|
|
|
2012-01-10 16:39:00 +04:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
|
|
|
|
pr_err("can't get rlimit: %m\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-19 18:52:50 +04:00
|
|
|
if (name) {
|
|
|
|
fd = open(name, O_CREAT | O_WRONLY);
|
|
|
|
if (fd == -1) {
|
2012-01-31 15:13:05 +04:00
|
|
|
pr_perror("Can't create log file %s", name);
|
2011-12-19 18:52:50 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
logfd = rlimit.rlim_cur - 1;
|
2012-01-10 16:39:00 +04:00
|
|
|
if (reopen_fd_as(logfd, fd) < 0) {
|
2011-12-19 18:52:50 +04:00
|
|
|
pr_err("can't duplicate descriptor %d->%d: %m\n",
|
|
|
|
fd, logfd);
|
|
|
|
logfd = STDERR_FILENO;
|
2012-01-10 16:39:00 +04:00
|
|
|
goto err;
|
2011-12-19 18:52:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-01-10 16:39:00 +04:00
|
|
|
err:
|
|
|
|
if (name)
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
2011-12-19 18:52:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fini_log(void)
|
|
|
|
{
|
|
|
|
if (logfd != STDERR_FILENO &&
|
|
|
|
logfd != STDIN_FILENO &&
|
|
|
|
logfd != STDERR_FILENO)
|
|
|
|
close(logfd);
|
|
|
|
|
|
|
|
logfd = STDERR_FILENO;
|
|
|
|
}
|
2012-02-17 12:17:00 +04:00
|
|
|
|
|
|
|
void printk(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
|
|
|
|
va_start(params, format);
|
|
|
|
vdprintf(get_logfd(), format, params);
|
|
|
|
va_end(params);
|
|
|
|
}
|