summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMike Yuan <me@yhndnzj.com>2024-03-12 08:10:03 +0100
committerMike Yuan <me@yhndnzj.com>2024-03-12 09:42:48 +0100
commit7799e7d9f0d57966c4d1bf195b6caf04f8e426fa (patch)
tree0df767a55e1b33a1daff216c0d2e030af85a1316 /src
parentfileio,data-fd-util: use U64_* more (diff)
downloadsystemd-7799e7d9f0d57966c4d1bf195b6caf04f8e426fa.tar.xz
systemd-7799e7d9f0d57966c4d1bf195b6caf04f8e426fa.zip
data-fd-util: some modernization
Diffstat (limited to 'src')
-rw-r--r--src/shared/data-fd-util.c29
-rw-r--r--src/shared/data-fd-util.h6
2 files changed, 16 insertions, 19 deletions
diff --git a/src/shared/data-fd-util.c b/src/shared/data-fd-util.c
index 4907e5bc51..5dd27331c3 100644
--- a/src/shared/data-fd-util.c
+++ b/src/shared/data-fd-util.c
@@ -25,11 +25,10 @@
/* If memfd/pipe didn't work out, then let's use a file in /tmp up to a size of 1M. If it's large than that use /var/tmp instead. */
#define DATA_FD_TMP_LIMIT (1U * U64_MB)
-int acquire_data_fd(const void *data, size_t size, unsigned flags) {
- _cleanup_close_pair_ int pipefds[2] = EBADF_PAIR;
+int acquire_data_fd(const void *data, size_t size, DataFDFlags flags) {
_cleanup_close_ int fd = -EBADF;
- int isz = 0, r;
ssize_t n;
+ int r;
assert(data || size == 0);
@@ -52,24 +51,22 @@ int acquire_data_fd(const void *data, size_t size, unsigned flags) {
* It sucks a bit that depending on the situation we return very different objects here, but that's Linux I
* figure. */
- if (size == 0 && ((flags & ACQUIRE_NO_DEV_NULL) == 0))
+ if (size == 0 && !FLAGS_SET(flags, ACQUIRE_NO_DEV_NULL))
/* As a special case, return /dev/null if we have been called for an empty data block */
return RET_NERRNO(open("/dev/null", O_RDONLY|O_CLOEXEC|O_NOCTTY));
- if ((flags & ACQUIRE_NO_MEMFD) == 0) {
+ if (!FLAGS_SET(flags, ACQUIRE_NO_MEMFD)) {
fd = memfd_new_and_seal("data-fd", data, size);
- if (fd < 0) {
- if (ERRNO_IS_NOT_SUPPORTED(fd))
- goto try_pipe;
-
+ if (fd < 0 && !ERRNO_IS_NOT_SUPPORTED(fd))
return fd;
- }
-
- return TAKE_FD(fd);
+ if (fd >= 0)
+ return TAKE_FD(fd);
}
-try_pipe:
- if ((flags & ACQUIRE_NO_PIPE) == 0) {
+ if (!FLAGS_SET(flags, ACQUIRE_NO_PIPE)) {
+ _cleanup_close_pair_ int pipefds[2] = EBADF_PAIR;
+ int isz;
+
if (pipe2(pipefds, O_CLOEXEC|O_NONBLOCK) < 0)
return -errno;
@@ -106,7 +103,7 @@ try_pipe:
}
try_dev_shm:
- if ((flags & ACQUIRE_NO_TMPFILE) == 0) {
+ if (!FLAGS_SET(flags, ACQUIRE_NO_TMPFILE)) {
fd = open("/dev/shm", O_RDWR|O_TMPFILE|O_CLOEXEC, 0500);
if (fd < 0)
goto try_dev_shm_without_o_tmpfile;
@@ -122,7 +119,7 @@ try_dev_shm:
}
try_dev_shm_without_o_tmpfile:
- if ((flags & ACQUIRE_NO_REGULAR) == 0) {
+ if (!FLAGS_SET(flags, ACQUIRE_NO_REGULAR)) {
char pattern[] = "/dev/shm/data-fd-XXXXXX";
fd = mkostemp_safe(pattern);
diff --git a/src/shared/data-fd-util.h b/src/shared/data-fd-util.h
index 6d99209421..2e073ee45e 100644
--- a/src/shared/data-fd-util.h
+++ b/src/shared/data-fd-util.h
@@ -3,14 +3,14 @@
#include <stddef.h>
-enum {
+typedef enum DataFDFlags {
ACQUIRE_NO_DEV_NULL = 1 << 0,
ACQUIRE_NO_MEMFD = 1 << 1,
ACQUIRE_NO_PIPE = 1 << 2,
ACQUIRE_NO_TMPFILE = 1 << 3,
ACQUIRE_NO_REGULAR = 1 << 4,
-};
+} DataFDFlags;
-int acquire_data_fd(const void *data, size_t size, unsigned flags);
+int acquire_data_fd(const void *data, size_t size, DataFDFlags flags);
int copy_data_fd(int fd);
int memfd_clone_fd(int fd, const char *name, int mode);