fd-util: add wildcard_fd_is_valid() helper and use it tree-wide
Many *at()-style helpers accept a directory fd that may be either a regular, valid fd (>= 0) or one of the special AT_FDCWD/XAT_FDROOT wildcard values, and open-code that check in their assertions. Add a wildcard_fd_is_valid() helper for it and use it tree-wide.
This commit is contained in:
committed by
Luca Boccassi
parent
d35fa257eb
commit
eb3799a373
@@ -48,7 +48,7 @@ int btrfs_subvol_make(int dir_fd, const char *path) {
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(!isempty(path));
|
||||
|
||||
r = extract_subvolume_name(path, &subvolume);
|
||||
|
||||
+3
-3
@@ -62,7 +62,7 @@ static int chase_openat2(int root_fd, int dir_fd, const char *path, ChaseFlags c
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
|
||||
if (!can_openat2)
|
||||
return -EOPNOTSUPP;
|
||||
@@ -241,8 +241,8 @@ int chaseat(int root_fd, int dir_fd, const char *path, ChaseFlags flags, char **
|
||||
assert(!FLAGS_SET(flags, CHASE_PREFIX_ROOT));
|
||||
assert(!FLAGS_SET(flags, CHASE_STEP|CHASE_EXTRACT_FILENAME));
|
||||
assert(!FLAGS_SET(flags, CHASE_NO_AUTOFS|CHASE_TRIGGER_AUTOFS));
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(root_fd >= 0 || IN_SET(root_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(wildcard_fd_is_valid(root_fd));
|
||||
/* AT_FDCWD for dir_fd is only allowed when there is no chroot boundary: otherwise the current
|
||||
* working directory might live outside root_fd's subtree. */
|
||||
assert(dir_fd != AT_FDCWD || IN_SET(root_fd, AT_FDCWD, XAT_FDROOT));
|
||||
|
||||
@@ -155,7 +155,7 @@ static int conf_file_chase_and_verify(
|
||||
struct stat st = {};
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(original_path);
|
||||
assert(path);
|
||||
assert(name);
|
||||
@@ -274,7 +274,7 @@ int conf_file_new_at(
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(ret);
|
||||
|
||||
int log_level = conf_files_log_level(flags);
|
||||
@@ -390,7 +390,7 @@ static int files_add(
|
||||
assert(dir);
|
||||
assert(original_dirpath);
|
||||
assert(resolved_dirpath);
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(files);
|
||||
assert(masked);
|
||||
|
||||
@@ -622,7 +622,7 @@ static int conf_files_list_impl(
|
||||
const ConfFile *inserted = NULL;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(ret);
|
||||
|
||||
root = empty_to_root(root);
|
||||
@@ -736,7 +736,7 @@ int conf_files_list_strv_at(
|
||||
_cleanup_free_ char *root = NULL;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(ret);
|
||||
|
||||
if (DEBUG_LOGGING)
|
||||
@@ -761,7 +761,7 @@ int conf_files_list_strv_at_full(
|
||||
_cleanup_free_ char *root = NULL;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(ret_files);
|
||||
assert(ret_n_files);
|
||||
|
||||
|
||||
+5
-5
@@ -581,7 +581,7 @@ bool fdname_is_valid(const char *s) {
|
||||
int fd_get_path(int fd, char **ret) {
|
||||
int r;
|
||||
|
||||
assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(fd));
|
||||
|
||||
if (fd == AT_FDCWD)
|
||||
return safe_getcwd(ret);
|
||||
@@ -781,7 +781,7 @@ finish:
|
||||
}
|
||||
|
||||
int fd_reopen(int fd, int flags) {
|
||||
assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(fd));
|
||||
assert(!FLAGS_SET(flags, O_CREAT));
|
||||
|
||||
/* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to
|
||||
@@ -1052,7 +1052,7 @@ static bool is_literal_root(const char *p) {
|
||||
}
|
||||
|
||||
int path_is_root_at(int dir_fd, const char *path) {
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
|
||||
if (dir_fd == XAT_FDROOT && isempty(path))
|
||||
return true;
|
||||
@@ -1086,8 +1086,8 @@ int fds_inode_and_mount_same(int fd1, int fd2) {
|
||||
struct statx sx1, sx2;
|
||||
int r;
|
||||
|
||||
assert(fd1 >= 0 || IN_SET(fd1, AT_FDCWD, XAT_FDROOT));
|
||||
assert(fd2 >= 0 || IN_SET(fd2, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(fd1));
|
||||
assert(wildcard_fd_is_valid(fd2));
|
||||
|
||||
r = xstatx(fd1, /* path = */ NULL, AT_EMPTY_PATH,
|
||||
STATX_TYPE|STATX_INO|STATX_MNT_ID,
|
||||
|
||||
@@ -64,6 +64,12 @@
|
||||
assert_cc(XAT_FDROOT != AT_FDCWD);
|
||||
assert_cc(XAT_FDROOT < -ERRNO_MAX);
|
||||
|
||||
/* Checks whether the specified fd is acceptable as a *at() directory fd that supports the two "wildcard"
|
||||
* values: it's either a regular, valid fd (i.e. >= 0), or one of the special AT_FDCWD/XAT_FDROOT values. */
|
||||
static inline bool wildcard_fd_is_valid(int fd) {
|
||||
return fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT);
|
||||
}
|
||||
|
||||
int close_nointr(int fd);
|
||||
int safe_close(int fd);
|
||||
void safe_close_pair(int p[static 2]);
|
||||
|
||||
+6
-6
@@ -420,7 +420,7 @@ int read_one_line_file_at(int dir_fd, const char *filename, char **ret) {
|
||||
_cleanup_fclose_ FILE *f = NULL;
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(filename);
|
||||
assert(ret);
|
||||
|
||||
@@ -435,7 +435,7 @@ int read_boolean_file_at(int dir_fd, const char *filename) {
|
||||
_cleanup_free_ char *s = NULL;
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(filename);
|
||||
|
||||
r = read_one_line_file_at(dir_fd, filename, &s);
|
||||
@@ -1024,7 +1024,7 @@ static int xfopenat_regular(int dir_fd, const char *path, const char *mode, int
|
||||
|
||||
/* A combination of fopen() with openat() */
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(mode);
|
||||
assert(ret);
|
||||
|
||||
@@ -1071,7 +1071,7 @@ static int xfopenat_unix_socket(int dir_fd, const char *path, const char *bind_n
|
||||
FILE *f;
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(ret);
|
||||
|
||||
sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
|
||||
@@ -1119,7 +1119,7 @@ int xfopenat_full(
|
||||
FILE *f = NULL; /* avoid false maybe-uninitialized warning */
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(mode);
|
||||
assert(ret);
|
||||
|
||||
@@ -1689,7 +1689,7 @@ int write_data_file_atomic_at(
|
||||
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
|
||||
/* This is a cousin of write_string_file_atomic(), but operates with arbitrary struct iovec binary
|
||||
* data (rather than strings), works without FILE* streams, and does direct syscalls instead. */
|
||||
|
||||
+1
-1
@@ -1176,7 +1176,7 @@ int xopenat_full(int dir_fd, const char *path, int open_flags, XOpenFlags xopen_
|
||||
bool made_dir = false, made_file = false;
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
|
||||
/* An inode can only be one of a directory, a regular file or a socket at the same time. */
|
||||
assert(FLAGS_SET(open_flags, O_DIRECTORY) + FLAGS_SET(xopen_flags, XO_REGULAR) + FLAGS_SET(xopen_flags, XO_SOCKET) <= 1);
|
||||
|
||||
@@ -242,7 +242,7 @@ struct file_handle* file_handle_dup(const struct file_handle *fh) {
|
||||
int is_mount_point_at(int dir_fd, const char *path, int flags) {
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert((flags & ~AT_SYMLINK_FOLLOW) == 0);
|
||||
|
||||
if (path_equal(path, "/"))
|
||||
@@ -306,7 +306,7 @@ static int path_get_mnt_id_at_internal(int dir_fd, const char *path, bool unique
|
||||
struct statx sx;
|
||||
int r;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(ret);
|
||||
|
||||
r = xstatx(dir_fd, path,
|
||||
|
||||
+4
-4
@@ -170,7 +170,7 @@ int open_os_release_at(int rfd, char **ret_path, int *ret_fd) {
|
||||
const char *e;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
|
||||
e = secure_getenv("SYSTEMD_OS_RELEASE");
|
||||
if (e)
|
||||
@@ -227,7 +227,7 @@ int open_extension_release_at(
|
||||
const char *p;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
assert(!extension || (image_class >= 0 && image_class < _IMAGE_CLASS_MAX));
|
||||
|
||||
if (!extension)
|
||||
@@ -372,7 +372,7 @@ static int parse_extension_release_atv(
|
||||
_cleanup_free_ char *p = NULL;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
|
||||
r = open_extension_release_at(rfd, image_class, extension, relax_extension_release_check, &p, &fd);
|
||||
if (r < 0)
|
||||
@@ -391,7 +391,7 @@ int parse_extension_release_at_sentinel(
|
||||
va_list ap;
|
||||
int r;
|
||||
|
||||
assert(rfd >= 0 || IN_SET(rfd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(rfd));
|
||||
|
||||
va_start(ap, extension);
|
||||
r = parse_extension_release_atv(rfd, image_class, extension, relax_extension_release_check, ap);
|
||||
|
||||
@@ -1621,7 +1621,7 @@ int connect_unix_path(int fd, int dir_fd, const char *path) {
|
||||
_cleanup_close_ int inode_fd = -EBADF;
|
||||
|
||||
assert(fd >= 0);
|
||||
assert(IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT) || dir_fd >= 0);
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
|
||||
/* Connects to the specified AF_UNIX socket in the file system. Works around the 108 byte size limit
|
||||
* in sockaddr_un, by going via O_PATH if needed. This hence works for any kind of path. */
|
||||
|
||||
@@ -32,7 +32,7 @@ static int verify_stat_at(
|
||||
struct stat st;
|
||||
int r;
|
||||
|
||||
assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(fd));
|
||||
assert(!isempty(path) || !follow);
|
||||
assert(verify_func);
|
||||
|
||||
@@ -418,7 +418,7 @@ int xstatx_full(int fd,
|
||||
* STATX_MNT_ID if not.
|
||||
*/
|
||||
|
||||
assert(fd >= 0 || IN_SET(fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(fd));
|
||||
assert((mandatory_mask & optional_mask) == 0);
|
||||
assert(!FLAGS_SET(xstatx_flags, XSTATX_MNT_ID_BEST) || !((mandatory_mask|optional_mask) & (STATX_MNT_ID|STATX_MNT_ID_UNIQUE)));
|
||||
assert(ret);
|
||||
@@ -480,7 +480,7 @@ static int xfstatfs(int fd, struct statfs *ret) {
|
||||
int xstatfsat(int dir_fd, const char *path, struct statfs *ret) {
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
|
||||
assert(dir_fd >= 0 || IN_SET(dir_fd, AT_FDCWD, XAT_FDROOT));
|
||||
assert(wildcard_fd_is_valid(dir_fd));
|
||||
assert(ret);
|
||||
|
||||
if (!isempty(path)) {
|
||||
|
||||
Reference in New Issue
Block a user