1
0
mirror of https://github.com/systemd/systemd synced 2026-03-30 03:34:49 +02:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Frantisek Sumsal
b98416e100 tree-wide: assorted Coccinelle fixes
It's that time of year again.
2021-10-08 15:03:27 +02:00
Lennart Poettering
c17e8ce9ec
Merge pull request #20962 from poettering/dttoif
Some tweaks to dirent-util.c
2021-10-08 11:14:03 +02:00
Lennart Poettering
49a0931f62 dirent-util: tweak readdir_ensure_type() a bit
So far we ignored if readdir_ensure_type() failed, the .d_type would
then still possibly report DT_UNKNOWN, possibly confusing the caller.

Let's make this safer: if we get an error on readdir_ensure_type() then
report it — except if it is ENOENT which indicates the dirent vanished
by now, which is not a problem and we should just skip to the next
entry.
2021-10-07 23:13:40 +02:00
Lennart Poettering
3214129369 dirent-util: use statx() in readdir_ensure_type()
Let's ask exactly for the one field we actually want to know, i.e.
STATX_TYPE.

(While we are at it, also copy over the inode number, if we have it,
simply to report the most recent info we have)

(Also, see AT_NO_AUTOMOUNT, so that we don't trigger automounts here.
After all, if we want to know the inode type of a dirent here, then
there's not need to trigger the automount, the inode type is not going
to change by that.)
2021-10-07 23:13:40 +02:00
Lennart Poettering
ba24ef86e7 dirent-util: get rid of stat_mode_to_dirent_type()
Apparently glibc already has a helper for this. (Not in the man pages
for Linux, but FreeBSD does document these cryptic helpers, and its
exported by glibc. That should be good enough for us.)
2021-10-07 23:13:03 +02:00
9 changed files with 54 additions and 43 deletions

View File

@ -1412,7 +1412,7 @@ static int verb_log_control(int argc, char *argv[], void *userdata) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r; int r;
assert(argc == 1 || argc == 2); assert(IN_SET(argc, 1, 2));
r = acquire_bus(&bus, NULL); r = acquire_bus(&bus, NULL);
if (r < 0) if (r < 0)
@ -2284,7 +2284,7 @@ static int do_security(int argc, char *argv[], void *userdata) {
if (r < 0 && r != -ENOENT) if (r < 0 && r != -ENOENT)
return r; return r;
if (f != NULL) { if (f) {
r = json_parse_file(f, pp, /*flags=*/ 0, &policy, &line, &column); r = json_parse_file(f, pp, /*flags=*/ 0, &policy, &line, &column);
if (r < 0) if (r < 0)
return log_error_errno(r, "[%s:%u:%u] Failed to parse JSON policy: %m", pp, line, column); return log_error_errno(r, "[%s:%u:%u] Failed to parse JSON policy: %m", pp, line, column);

View File

@ -5,22 +5,12 @@
#include "dirent-util.h" #include "dirent-util.h"
#include "path-util.h" #include "path-util.h"
#include "stat-util.h"
#include "string-util.h" #include "string-util.h"
int stat_mode_to_dirent_type(mode_t mode) {
return
S_ISREG(mode) ? DT_REG :
S_ISDIR(mode) ? DT_DIR :
S_ISLNK(mode) ? DT_LNK :
S_ISFIFO(mode) ? DT_FIFO :
S_ISSOCK(mode) ? DT_SOCK :
S_ISCHR(mode) ? DT_CHR :
S_ISBLK(mode) ? DT_BLK :
DT_UNKNOWN;
}
static int dirent_ensure_type(DIR *d, struct dirent *de) { static int dirent_ensure_type(DIR *d, struct dirent *de) {
struct stat st; STRUCT_STATX_DEFINE(sx);
int r;
assert(d); assert(d);
assert(de); assert(de);
@ -33,10 +23,17 @@ static int dirent_ensure_type(DIR *d, struct dirent *de) {
return 0; return 0;
} }
if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) /* Let's ask only for the type, nothing else. */
return -errno; r = statx_fallback(dirfd(d), de->d_name, AT_SYMLINK_NOFOLLOW|AT_NO_AUTOMOUNT, STATX_TYPE, &sx);
if (r < 0)
return r;
de->d_type = stat_mode_to_dirent_type(st.st_mode); assert(FLAGS_SET(sx.stx_mask, STATX_TYPE));
de->d_type = IFTODT(sx.stx_mode);
/* If the inode is passed too, update the field, i.e. report most recent data */
if (FLAGS_SET(sx.stx_mask, STATX_INO))
de->d_ino = sx.stx_ino;
return 0; return 0;
} }
@ -69,24 +66,40 @@ bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
} }
struct dirent *readdir_ensure_type(DIR *d) { struct dirent *readdir_ensure_type(DIR *d) {
struct dirent *de; int r;
assert(d); assert(d);
errno = 0; /* Like readdir(), but fills in .d_type if it is DT_UNKNOWN */
de = readdir(d);
if (de)
(void) dirent_ensure_type(d, de);
return de;
}
struct dirent *readdir_no_dot(DIR *dirp) {
struct dirent *d;
for (;;) { for (;;) {
d = readdir_ensure_type(dirp); struct dirent *de;
if (d && dot_or_dot_dot(d->d_name))
continue; errno = 0;
return d; de = readdir(d);
if (!de)
return NULL;
r = dirent_ensure_type(d, de);
if (r >= 0)
return de;
if (r != -ENOENT) {
errno = -r; /* We want to be compatible with readdir(), hence propagate error via errno here */
return NULL;
}
/* Vanished by now? Then skip immedately to next */
}
}
struct dirent *readdir_no_dot(DIR *d) {
assert(d);
for (;;) {
struct dirent *de;
de = readdir_ensure_type(d);
if (!de || !dot_or_dot_dot(de->d_name))
return de;
} }
} }

View File

@ -8,8 +8,6 @@
#include "macro.h" #include "macro.h"
#include "path-util.h" #include "path-util.h"
int stat_mode_to_dirent_type(mode_t mode);
bool dirent_is_file(const struct dirent *de) _pure_; bool dirent_is_file(const struct dirent *de) _pure_;
bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_; bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) _pure_;

View File

@ -290,7 +290,7 @@ int recurse_dir(
/* Copy over the data we acquired through statx() if we acquired any */ /* Copy over the data we acquired through statx() if we acquired any */
if (sx.stx_mask & STATX_TYPE) { if (sx.stx_mask & STATX_TYPE) {
assert(!!subdir == !!S_ISDIR(sx.stx_mode)); assert(!!subdir == !!S_ISDIR(sx.stx_mode));
de[i]->d_type = stat_mode_to_dirent_type(sx.stx_mode); de[i]->d_type = IFTODT(sx.stx_mode);
} }
if (sx.stx_mask & STATX_INO) if (sx.stx_mask & STATX_INO)

View File

@ -1004,7 +1004,7 @@ static int attach_luks2_by_pkcs11(
bool headless, bool headless,
uint32_t flags) { uint32_t flags) {
int r = -ENOTSUP; int r = -EOPNOTSUPP;
#if HAVE_LIBCRYPTSETUP_PLUGINS #if HAVE_LIBCRYPTSETUP_PLUGINS
if (!crypt_get_type(cd) || strcmp(crypt_get_type(cd), CRYPT_LUKS2)) if (!crypt_get_type(cd) || strcmp(crypt_get_type(cd), CRYPT_LUKS2))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device."); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Automatic PKCS#11 metadata requires LUKS2 device.");
@ -1198,7 +1198,7 @@ static int attach_luks2_by_tpm2(
.device = arg_tpm2_device .device = arg_tpm2_device
}; };
if (crypt_token_external_path() == NULL) if (!crypt_token_external_path())
return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Libcryptsetup has external plugins support disabled."); "Libcryptsetup has external plugins support disabled.");

View File

@ -2540,7 +2540,7 @@ static int verb_log_level(int argc, char *argv[], void *userdata) {
sd_bus *bus = userdata; sd_bus *bus = userdata;
assert(bus); assert(bus);
assert(argc == 1 || argc == 2); assert(IN_SET(argc, 1, 2));
return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL); return verb_log_control_common(bus, "org.freedesktop.resolve1", argv[0], argc == 2 ? argv[1] : NULL);
} }

View File

@ -1596,7 +1596,7 @@ int dissect_image(
} }
if (m->verity_ready) if (m->verity_ready)
m->verity_sig_ready = !!verity->root_hash_sig; m->verity_sig_ready = verity->root_hash_sig;
} else if (m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR_VERITY_SIG : PARTITION_ROOT_VERITY_SIG].found) { } else if (m->partitions[verity->designator == PARTITION_USR ? PARTITION_USR_VERITY_SIG : PARTITION_ROOT_VERITY_SIG].found) {

View File

@ -529,7 +529,7 @@ int fork_agent(const char *name, int except[], size_t n_except, pid_t *ret_pid,
* stdin around. */ * stdin around. */
fd = open("/dev/tty", O_WRONLY); fd = open("/dev/tty", O_WRONLY);
if (fd < 0) { if (fd < 0) {
if (errno != -ENXIO) { if (errno != ENXIO) {
log_error_errno(errno, "Failed to open /dev/tty: %m"); log_error_errno(errno, "Failed to open /dev/tty: %m");
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }

View File

@ -219,9 +219,9 @@ int pkcs11_token_login_by_pin(
return log_error_errno(SYNTHETIC_ERRNO(EIO), return log_error_errno(SYNTHETIC_ERRNO(EIO),
"Failed to log into security token '%s': %s", token_label, p11_kit_strerror(rv)); "Failed to log into security token '%s': %s", token_label, p11_kit_strerror(rv));
log_notice("PIN for token '%s' is incorrect, please try again.", token_label); return log_notice_errno(SYNTHETIC_ERRNO(ENOLCK),
"PIN for token '%s' is incorrect, please try again.",
return -ENOLCK; token_label);
} }
int pkcs11_token_login( int pkcs11_token_login(