Commit 8644ce96 authored by Pavel Emelyanov's avatar Pavel Emelyanov

util: Prepare proc opening helpers to open any files

We have a set of routines that open /proc/$pid files via proc service
descriptor. Teach them to accept non-pids as pids to open /proc/self/*
and /proc/* files via the same engine.
Signed-f-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
Acked-by: 's avatarAndrew Vagin <avagin@parallels.com>
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent d9e7a5f1
...@@ -130,6 +130,17 @@ extern int open_pid_proc(pid_t pid); ...@@ -130,6 +130,17 @@ extern int open_pid_proc(pid_t pid);
extern int close_pid_proc(void); extern int close_pid_proc(void);
extern int set_proc_fd(int fd); extern int set_proc_fd(int fd);
/*
* Values for pid argument of the proc opening routines below.
* SELF would open file under /proc/self
* GEN would open a file under /proc itself
* NONE is internal, don't use it ;)
*/
#define PROC_SELF 0
#define PROC_GEN -1
#define PROC_NONE -2
extern int do_open_proc(pid_t pid, int flags, const char *fmt, ...); extern int do_open_proc(pid_t pid, int flags, const char *fmt, ...);
#define __open_proc(pid, flags, fmt, ...) \ #define __open_proc(pid, flags, fmt, ...) \
......
...@@ -160,7 +160,7 @@ int move_img_fd(int *img_fd, int want_fd) ...@@ -160,7 +160,7 @@ int move_img_fd(int *img_fd, int want_fd)
return 0; return 0;
} }
static pid_t open_proc_pid = 0; static pid_t open_proc_pid = PROC_NONE;
static int open_proc_fd = -1; static int open_proc_fd = -1;
int close_pid_proc(void) int close_pid_proc(void)
...@@ -171,7 +171,7 @@ int close_pid_proc(void) ...@@ -171,7 +171,7 @@ int close_pid_proc(void)
ret = close(open_proc_fd); ret = close(open_proc_fd);
open_proc_fd = -1; open_proc_fd = -1;
open_proc_pid = 0; open_proc_pid = PROC_NONE;
return ret; return ret;
} }
...@@ -228,7 +228,18 @@ inline int open_pid_proc(pid_t pid) ...@@ -228,7 +228,18 @@ inline int open_pid_proc(pid_t pid)
dfd = get_service_fd(PROC_FD_OFF); dfd = get_service_fd(PROC_FD_OFF);
} }
snprintf(path, sizeof(path), "%d", pid); if (pid == PROC_GEN)
/*
* Don't cache it, close_pid_proc() would
* close service descriptor otherwise.
*/
return dfd;
if (pid == PROC_SELF)
snprintf(path, sizeof(path), "self");
else
snprintf(path, sizeof(path), "%d", pid);
fd = openat(dfd, path, O_RDONLY); fd = openat(dfd, path, O_RDONLY);
if (fd < 0) if (fd < 0)
pr_perror("Can't open %s", path); pr_perror("Can't open %s", path);
......
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