summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/socket-util.c16
-rw-r--r--src/basic/socket-util.h6
-rw-r--r--src/basic/stat-util.c15
3 files changed, 21 insertions, 16 deletions
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index 59039bea4f..8267988ad9 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -721,6 +721,10 @@ bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
if (isempty(p))
return false;
+ /* A valid ifindex? If so, it's valid iff IFNAME_VALID_NUMERIC is set */
+ if (parse_ifindex(p) >= 0)
+ return flags & IFNAME_VALID_NUMERIC;
+
if (flags & IFNAME_VALID_ALTERNATIVE) {
if (strlen(p) >= ALTIFNAMSIZ)
return false;
@@ -745,14 +749,10 @@ bool ifname_valid_full(const char *p, IfnameValidFlags flags) {
numeric = numeric && (*t >= '0' && *t <= '9');
}
- if (numeric) {
- if (!(flags & IFNAME_VALID_NUMERIC))
- return false;
-
- /* Verify that the number is well-formatted and in range. */
- if (parse_ifindex(p) < 0)
- return false;
- }
+ /* It's fully numeric but didn't parse as valid ifindex above? if so, it must be too large or zero or
+ * so, let's refuse that. */
+ if (numeric)
+ return false;
return true;
}
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
index b741a90be7..2c98283cd3 100644
--- a/src/basic/socket-util.h
+++ b/src/basic/socket-util.h
@@ -133,9 +133,9 @@ int ip_tos_to_string_alloc(int i, char **s);
int ip_tos_from_string(const char *s);
typedef enum {
- IFNAME_VALID_ALTERNATIVE = 1 << 0,
- IFNAME_VALID_NUMERIC = 1 << 1,
- _IFNAME_VALID_ALL = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC,
+ IFNAME_VALID_ALTERNATIVE = 1 << 0,
+ IFNAME_VALID_NUMERIC = 1 << 1,
+ _IFNAME_VALID_ALL = IFNAME_VALID_ALTERNATIVE | IFNAME_VALID_NUMERIC,
} IfnameValidFlags;
bool ifname_valid_full(const char *p, IfnameValidFlags flags);
static inline bool ifname_valid(const char *p) {
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
index f999681636..72a7e4a48b 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -72,12 +72,17 @@ int dir_is_empty_at(int dir_fd, const char *path) {
_cleanup_closedir_ DIR *d = NULL;
struct dirent *de;
- if (path)
+ if (path) {
fd = openat(dir_fd, path, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
- else
- fd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
- if (fd < 0)
- return -errno;
+ if (fd < 0)
+ return -errno;
+ } else {
+ /* Note that DUPing is not enough, as the internal pointer
+ * would still be shared and moved by FOREACH_DIRENT. */
+ fd = fd_reopen(dir_fd, O_CLOEXEC);
+ if (fd < 0)
+ return fd;
+ }
d = take_fdopendir(&fd);
if (!d)