Commit 4ebc8759 authored by Andrey Vagin's avatar Andrey Vagin Committed by Pavel Emelyanov

util: add ability to change a proc mount point (v2)

v2:  rework this by using openat() and service fds for proc root.
Signed-off-by: 's avatarAndrey Vagin <avagin@openvz.org>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent 0ee0b197
...@@ -981,7 +981,7 @@ static int sigreturn_restore(pid_t pid, struct list_head *tgt_vmas, int nr_vmas) ...@@ -981,7 +981,7 @@ static int sigreturn_restore(pid_t pid, struct list_head *tgt_vmas, int nr_vmas)
restore_thread_vma_len = 0; restore_thread_vma_len = 0;
ret = parse_smaps(pid, &self_vma_list, false); ret = parse_smaps(pid, &self_vma_list, false);
close_pid_proc(); close_proc();
if (ret < 0) if (ret < 0)
goto err; goto err;
......
...@@ -84,6 +84,7 @@ enum { ...@@ -84,6 +84,7 @@ enum {
LOG_FD_OFF = 1, LOG_FD_OFF = 1,
IMG_FD_OFF, IMG_FD_OFF,
SELF_EXE_FD_OFF, SELF_EXE_FD_OFF,
PROC_FD_OFF,
}; };
int get_service_fd(int type); int get_service_fd(int type);
......
...@@ -168,6 +168,8 @@ extern int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool ...@@ -168,6 +168,8 @@ extern int reopen_fd_as_safe(char *file, int line, int new_fd, int old_fd, bool
#define reopen_fd_as(new_fd, old_fd) reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, false) #define reopen_fd_as(new_fd, old_fd) reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, false)
#define reopen_fd_as_nocheck(new_fd, old_fd) reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, true) #define reopen_fd_as_nocheck(new_fd, old_fd) reopen_fd_as_safe(__FILE__, __LINE__, new_fd, old_fd, true)
int set_proc_mountpoint(char *path);
void close_proc(void);
int open_pid_proc(pid_t pid); int open_pid_proc(pid_t pid);
int close_pid_proc(void); int close_pid_proc(void);
......
...@@ -205,6 +205,7 @@ void close_image_dir(void) ...@@ -205,6 +205,7 @@ void close_image_dir(void)
static pid_t open_proc_pid = 0; static pid_t open_proc_pid = 0;
static int open_proc_fd = -1; static int open_proc_fd = -1;
static int proc_dir_fd = -1;
int close_pid_proc(void) int close_pid_proc(void)
{ {
...@@ -219,6 +220,38 @@ int close_pid_proc(void) ...@@ -219,6 +220,38 @@ int close_pid_proc(void)
return ret; return ret;
} }
void close_proc()
{
close_pid_proc();
if (proc_dir_fd > 0)
close(proc_dir_fd);
proc_dir_fd = -1;
}
int set_proc_mountpoint(char *path)
{
int sfd = get_service_fd(PROC_FD_OFF), fd;
close_proc();
fd = open(path, O_DIRECTORY | O_RDONLY);
if (fd == -1) {
pr_err("Can't open %s\n", path);
return -1;
}
sfd = dup2(fd, sfd);
close(fd);
if (sfd < 0) {
pr_err("Can't set proc fd");
return -1;
}
proc_dir_fd = sfd;
return 0;
}
inline int open_pid_proc(pid_t pid) inline int open_pid_proc(pid_t pid)
{ {
char path[18]; char path[18];
...@@ -228,8 +261,15 @@ inline int open_pid_proc(pid_t pid) ...@@ -228,8 +261,15 @@ inline int open_pid_proc(pid_t pid)
return open_proc_fd; return open_proc_fd;
close_pid_proc(); close_pid_proc();
sprintf(path, "/proc/%d", pid);
fd = open(path, O_RDONLY); if (proc_dir_fd == -1) {
fd = set_proc_mountpoint("/proc");
if (fd < 0)
return fd;
}
sprintf(path, "%d", pid);
fd = openat(proc_dir_fd, path, O_RDONLY);
if (fd < 0) if (fd < 0)
pr_perror("Can't open %s", path); pr_perror("Can't open %s", path);
else { else {
......
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