1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-02-27 13:57:26 +03:00

Merge pull request #10523 from poettering/fd-get-path-optimize

fd_get_path() optimization
This commit is contained in:
Yu Watanabe 2018-10-26 09:34:07 +09:00 committed by GitHub
commit bf1d6be5b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View File

@ -353,22 +353,22 @@ bool fdname_is_valid(const char *s) {
}
int fd_get_path(int fd, char **ret) {
_cleanup_close_ int dir = -1;
char fdname[DECIMAL_STR_MAX(int)];
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
int r;
dir = open("/proc/self/fd/", O_CLOEXEC | O_DIRECTORY | O_PATH);
if (dir < 0)
/* /proc is not available or not set up properly, we're most likely
* in some chroot environment. */
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
r = readlink_malloc(procfs_path, ret);
if (r == -ENOENT) {
/* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's make
* things debuggable and distuingish the two. */
if (access("/proc/self/fd/", F_OK) < 0)
/* /proc is not available or not set up properly, we're most likely in some chroot
* environment. */
return errno == ENOENT ? -EOPNOTSUPP : -errno;
xsprintf(fdname, "%i", fd);
r = readlinkat_malloc(dir, fdname, ret);
if (r == -ENOENT)
/* If the file doesn't exist the fd is invalid */
return -EBADF;
return -EBADF; /* The directory exists, hence it's the fd that doesn't. */
}
return r;
}

View File

@ -132,7 +132,7 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
}
int readlinkat_malloc(int fd, const char *p, char **ret) {
size_t l = 100;
size_t l = FILENAME_MAX+1;
int r;
assert(p);