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)
if (S_ISREG(st->st_mode)) {
int fd, ret;
char lpath[32];
char lpath[PSFDS];
/*
* Reopen file locally since it may have no read
* permissions when drained
*/
snprintf(lpath, sizeof(lpath), "/proc/self/fd/%d", _fd);
sprintf(lpath, "/proc/self/fd/%d", _fd);
fd = open(lpath, O_RDONLY);
if (fd < 0) {
pr_perror("Can't open ghost original file");
......
......@@ -940,7 +940,7 @@ out:
static int fchroot(int fd)
{
char fd_path[32];
char fd_path[PSFDS];
/*
* There's no such thing in syscalls. We can emulate
......
......@@ -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,
FhEntry *f_handle, unsigned long i_ino,
unsigned int s_dev, char *buf, size_t size,
int *target)
unsigned int s_dev, char *buf, int *target)
{
char *path = NULL;
int mntfd = -1;
......@@ -225,7 +224,7 @@ static char *get_mark_path(const char *who, struct file_remap *remap,
* 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;
if (log_get_loglevel() >= LOG_DEBUG) {
......@@ -246,11 +245,10 @@ static int restore_one_inotify(int inotify_fd, struct fsnotify_mark_info *info)
{
InotifyWdEntry *iwe = info->iwe;
int ret = -1, target = -1;
char buf[32], *path;
char buf[PSFDS], *path;
path = get_mark_path("inotify", info->remap, iwe->f_handle,
iwe->i_ino, iwe->s_dev, buf, sizeof(buf),
&target);
iwe->i_ino, iwe->s_dev, buf, &target);
if (!path)
goto err;
......@@ -291,7 +289,7 @@ static int restore_one_fanotify(int fd, struct fsnotify_mark_info *mark)
FanotifyMarkEntry *fme = mark->fme;
unsigned int flags = FAN_MARK_ADD;
int ret = -1, target = -1;
char buf[32], *path = NULL;
char buf[PSFDS], *path = NULL;
if (fme->type == MARK_TYPE__MOUNT) {
struct mount_info *m;
......@@ -307,7 +305,7 @@ static int restore_one_fanotify(int fd, struct fsnotify_mark_info *mark)
} else if (fme->type == MARK_TYPE__INODE) {
path = get_mark_path("fanotify", mark->remap,
fme->ie->f_handle, fme->ie->i_ino,
fme->s_dev, buf, sizeof(buf), &target);
fme->s_dev, buf, &target);
if (!path)
goto err;
} else {
......
......@@ -270,6 +270,12 @@ static inline bool dir_dots(struct dirent *de)
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);
#define USEC_PER_SEC 1000000L
......
......@@ -471,7 +471,7 @@ out:
static int tmpfs_dump(struct mount_info *pm)
{
int ret = -1;
char tmpfs_path[PATH_MAX];
char tmpfs_path[PSFDS];
int fd, fd_img = -1;
DIR *fdir = NULL;
......@@ -489,8 +489,7 @@ static int tmpfs_dump(struct mount_info *pm)
if (fd_img < 0)
goto out;
snprintf(tmpfs_path, sizeof(tmpfs_path),
"/proc/self/fd/%d", fd);
sprintf(tmpfs_path, "/proc/self/fd/%d", fd);
ret = cr_system(-1, fd_img, -1, "tar", (char *[])
{ "tar", "--create",
......
......@@ -250,9 +250,9 @@ err:
static int reopen_pipe(int fd, int flags)
{
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);
if (ret < 0)
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