Commit 2c237b69 authored by Andrey Vagin's avatar Andrey Vagin Committed by Cyrill Gorcunov

Don't use standard descriptors for logging

The standard descriptors may be redirected.

crtool dumplicates stderr in rlimit.maxfileno-1 and this descriptor
is inherited by all children and will be closed before sigreturn.

Known issues:

 - The logging descriptor may be used by a target process and
   a resume will fail.
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Acked-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Signed-off-by: 's avatarCyrill Gorcunov <gorcunov@openvz.org>
parent e57a4c29
......@@ -1623,6 +1623,7 @@ static void sigreturn_restore(pid_t pstree_pid, pid_t pid)
task_args->thread_args);
close_safe(&fd_pstree);
deinit_logging();
/*
* An indirect call to task_restore, note it never resturns
......
......@@ -241,6 +241,9 @@ int main(int argc, char *argv[])
{ NULL, no_argument, NULL, 0 }
};
if (init_logging())
return 1;
BUILD_BUG_ON(PAGE_SIZE != PAGE_IMAGE_SIZE);
if (argc < 3)
......
......@@ -15,6 +15,8 @@
#include "compiler.h"
#include "types.h"
extern int init_logging(void);
extern void deinit_logging(void);
extern void printk(const char *format, ...);
#define PREF_SHIFT_OP(pref, op, size) ((size) op (pref ##BYTES_SHIFT))
......
......@@ -31,6 +31,7 @@
#include <sys/ptrace.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include "compiler.h"
#include "types.h"
......@@ -39,12 +40,38 @@
#include "crtools.h"
static int logfd = STDERR_FILENO;
int init_logging(void)
{
struct rlimit rlimit;
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
pr_err("can't get rlimit: %m\n");
return 1;
}
logfd = rlimit.rlim_cur - 1;
if (dup2(2, 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);
vfprintf(stdout, format, params);
vdprintf(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