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:
commit
bf1d6be5b8
@ -353,22 +353,22 @@ bool fdname_is_valid(const char *s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int fd_get_path(int fd, char **ret) {
|
int fd_get_path(int fd, char **ret) {
|
||||||
_cleanup_close_ int dir = -1;
|
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
|
||||||
char fdname[DECIMAL_STR_MAX(int)];
|
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
dir = open("/proc/self/fd/", O_CLOEXEC | O_DIRECTORY | O_PATH);
|
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
|
||||||
if (dir < 0)
|
r = readlink_malloc(procfs_path, ret);
|
||||||
/* /proc is not available or not set up properly, we're most likely
|
if (r == -ENOENT) {
|
||||||
* in some chroot environment. */
|
/* 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;
|
return errno == ENOENT ? -EOPNOTSUPP : -errno;
|
||||||
|
|
||||||
xsprintf(fdname, "%i", fd);
|
return -EBADF; /* The directory exists, hence it's the fd that doesn't. */
|
||||||
|
}
|
||||||
r = readlinkat_malloc(dir, fdname, ret);
|
|
||||||
if (r == -ENOENT)
|
|
||||||
/* If the file doesn't exist the fd is invalid */
|
|
||||||
return -EBADF;
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
int readlinkat_malloc(int fd, const char *p, char **ret) {
|
||||||
size_t l = 100;
|
size_t l = FILENAME_MAX+1;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(p);
|
assert(p);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user