Commit b978c6f8 authored by Pavel Emelyanov's avatar Pavel Emelyanov

util: Introduce buffer size for carrying /proc/self/fd/N path

There's ... a number of places where we want to do something
with /proc/self/fd/%d path. Each time we guess buffer size
that is enough for this. Make standard constant for this and
save some space on stack and drop args for some functions.
Signed-off-by: 's avatarPavel Emelyanov <xemul@parallels.com>
parent baa2583a
...@@ -222,13 +222,13 @@ static int dump_ghost_file(int _fd, u32 id, const struct stat *st) ...@@ -222,13 +222,13 @@ static int dump_ghost_file(int _fd, u32 id, const struct stat *st)
if (S_ISREG(st->st_mode)) { if (S_ISREG(st->st_mode)) {
int fd, ret; int fd, ret;
char lpath[32]; char lpath[PSFDS];
/* /*
* Reopen file locally since it may have no read * Reopen file locally since it may have no read
* permissions when drained * permissions when drained
*/ */
snprintf(lpath, sizeof(lpath), "/proc/self/fd/%d", _fd); sprintf(lpath, "/proc/self/fd/%d", _fd);
fd = open(lpath, O_RDONLY); fd = open(lpath, O_RDONLY);
if (fd < 0) { if (fd < 0) {
pr_perror("Can't open ghost original file"); pr_perror("Can't open ghost original file");
......
...@@ -940,7 +940,7 @@ out: ...@@ -940,7 +940,7 @@ out:
static int fchroot(int fd) static int fchroot(int fd)
{ {
char fd_path[32]; char fd_path[PSFDS];
/* /*
* There's no such thing in syscalls. We can emulate * There's no such thing in syscalls. We can emulate
......
...@@ -189,8 +189,7 @@ static void decode_handle(fh_t *handle, FhEntry *img) ...@@ -189,8 +189,7 @@ static void decode_handle(fh_t *handle, FhEntry *img)
static char *get_mark_path(const char *who, struct file_remap *remap, static char *get_mark_path(const char *who, struct file_remap *remap,
FhEntry *f_handle, unsigned long i_ino, FhEntry *f_handle, unsigned long i_ino,
unsigned int s_dev, char *buf, size_t size, unsigned int s_dev, char *buf, int *target)
int *target)
{ {
char *path = NULL; char *path = NULL;
int mntfd = -1; int mntfd = -1;
...@@ -225,7 +224,7 @@ static char *get_mark_path(const char *who, struct file_remap *remap, ...@@ -225,7 +224,7 @@ static char *get_mark_path(const char *who, struct file_remap *remap,
* points to, i.e. -- just what we want. * points to, i.e. -- just what we want.
*/ */
snprintf(buf, size, "/proc/self/fd/%d", *target); sprintf(buf, "/proc/self/fd/%d", *target);
path = buf; path = buf;
if (log_get_loglevel() >= LOG_DEBUG) { if (log_get_loglevel() >= LOG_DEBUG) {
...@@ -246,11 +245,10 @@ static int restore_one_inotify(int inotify_fd, struct fsnotify_mark_info *info) ...@@ -246,11 +245,10 @@ static int restore_one_inotify(int inotify_fd, struct fsnotify_mark_info *info)
{ {
InotifyWdEntry *iwe = info->iwe; InotifyWdEntry *iwe = info->iwe;
int ret = -1, target = -1; int ret = -1, target = -1;
char buf[32], *path; char buf[PSFDS], *path;
path = get_mark_path("inotify", info->remap, iwe->f_handle, path = get_mark_path("inotify", info->remap, iwe->f_handle,
iwe->i_ino, iwe->s_dev, buf, sizeof(buf), iwe->i_ino, iwe->s_dev, buf, &target);
&target);
if (!path) if (!path)
goto err; goto err;
...@@ -291,7 +289,7 @@ static int restore_one_fanotify(int fd, struct fsnotify_mark_info *mark) ...@@ -291,7 +289,7 @@ static int restore_one_fanotify(int fd, struct fsnotify_mark_info *mark)
FanotifyMarkEntry *fme = mark->fme; FanotifyMarkEntry *fme = mark->fme;
unsigned int flags = FAN_MARK_ADD; unsigned int flags = FAN_MARK_ADD;
int ret = -1, target = -1; int ret = -1, target = -1;
char buf[32], *path = NULL; char buf[PSFDS], *path = NULL;
if (fme->type == MARK_TYPE__MOUNT) { if (fme->type == MARK_TYPE__MOUNT) {
struct mount_info *m; struct mount_info *m;
...@@ -307,7 +305,7 @@ static int restore_one_fanotify(int fd, struct fsnotify_mark_info *mark) ...@@ -307,7 +305,7 @@ static int restore_one_fanotify(int fd, struct fsnotify_mark_info *mark)
} else if (fme->type == MARK_TYPE__INODE) { } else if (fme->type == MARK_TYPE__INODE) {
path = get_mark_path("fanotify", mark->remap, path = get_mark_path("fanotify", mark->remap,
fme->ie->f_handle, fme->ie->i_ino, fme->ie->f_handle, fme->ie->i_ino,
fme->s_dev, buf, sizeof(buf), &target); fme->s_dev, buf, &target);
if (!path) if (!path)
goto err; goto err;
} else { } else {
......
...@@ -270,6 +270,12 @@ static inline bool dir_dots(struct dirent *de) ...@@ -270,6 +270,12 @@ static inline bool dir_dots(struct dirent *de)
return !strcmp(de->d_name, ".") || !strcmp(de->d_name, ".."); return !strcmp(de->d_name, ".") || !strcmp(de->d_name, "..");
} }
/*
* Size of buffer to carry the worst case or /proc/self/fd/N
* path. Since fd is an integer, we can easily estimate one :)
*/
#define PSFDS (sizeof("/proc/self/fd/2147483647"))
extern int read_fd_link(int lfd, char *buf, size_t size); extern int read_fd_link(int lfd, char *buf, size_t size);
#define USEC_PER_SEC 1000000L #define USEC_PER_SEC 1000000L
......
...@@ -471,7 +471,7 @@ out: ...@@ -471,7 +471,7 @@ out:
static int tmpfs_dump(struct mount_info *pm) static int tmpfs_dump(struct mount_info *pm)
{ {
int ret = -1; int ret = -1;
char tmpfs_path[PATH_MAX]; char tmpfs_path[PSFDS];
int fd, fd_img = -1; int fd, fd_img = -1;
DIR *fdir = NULL; DIR *fdir = NULL;
...@@ -489,8 +489,7 @@ static int tmpfs_dump(struct mount_info *pm) ...@@ -489,8 +489,7 @@ static int tmpfs_dump(struct mount_info *pm)
if (fd_img < 0) if (fd_img < 0)
goto out; goto out;
snprintf(tmpfs_path, sizeof(tmpfs_path), sprintf(tmpfs_path, "/proc/self/fd/%d", fd);
"/proc/self/fd/%d", fd);
ret = cr_system(-1, fd_img, -1, "tar", (char *[]) ret = cr_system(-1, fd_img, -1, "tar", (char *[])
{ "tar", "--create", { "tar", "--create",
......
...@@ -250,9 +250,9 @@ err: ...@@ -250,9 +250,9 @@ err:
static int reopen_pipe(int fd, int flags) static int reopen_pipe(int fd, int flags)
{ {
int ret; int ret;
char path[32]; char path[PSFDS];
snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); sprintf(path, "/proc/self/fd/%d", fd);
ret = open(path, flags); ret = open(path, flags);
if (ret < 0) if (ret < 0)
pr_perror("Unable to reopen the pipe %s", path); pr_perror("Unable to reopen the pipe %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