Commit 3dd8f2e6 authored by Pavel Emelyanov's avatar Pavel Emelyanov

fds: Introduce service fd-s

These are the fds that help us to do c/r. We want them not to intersect
with any "real-life" ones and thus store them close the the file rlimit
boundary. For now only the logfd one is such.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 177fbd88
......@@ -56,6 +56,13 @@ struct cr_options {
unsigned int namespaces_flags;
};
enum {
LOG_FD_OFF = 1,
IMG_FD_OFF,
};
int get_service_fd(int type);
/* file descriptors template */
struct cr_fd_desc_tmpl {
const char *fmt; /* format for the name */
......
......@@ -15,6 +15,7 @@
#include "compiler.h"
#include "types.h"
#include "util.h"
#include "crtools.h"
#define DEFAULT_LOGLEVEL LOG_WARN
#define DEFAULT_LOGFD STDERR_FILENO
......@@ -29,25 +30,14 @@ int log_get_fd(void)
int log_init(const char *output)
{
struct rlimit rlimit;
int new_logfd = DEFAULT_LOGFD;
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
pr_perror("Can't get rlimit");
return -1;
current_logfd = get_service_fd(LOG_FD_OFF);
if (current_logfd < 0) {
pr_msg("Can't obtain logfd");
goto err;
}
/*
* We might need to transfer this descriptors
* to another process' address space (and file
* descriptors space) so we try to minimize
* potential conflict between descriptors and
* try to reopen them somewhere near a limit.
*
* Still an explicit output file might be
* requested.
*/
if (output) {
new_logfd = open(output, O_CREAT | O_WRONLY);
if (new_logfd < 0) {
......@@ -56,7 +46,6 @@ int log_init(const char *output)
}
}
current_logfd = rlimit.rlim_cur - 1;
if (reopen_fd_as(current_logfd, new_logfd) < 0)
goto err;
......
......@@ -247,3 +247,20 @@ int do_open_proc(pid_t pid, int flags, const char *fmt, ...)
return openat(dirfd, path, flags);
}
int get_service_fd(int type)
{
struct rlimit rlimit;
/*
* Service FDs are thouse that most likely won't
* conflict with any 'real-life' ones
*/
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
pr_perror("Can't get rlimit");
return -1;
}
return rlimit.rlim_cur - type;
}
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