From cefec8f6ce66d65a501f19440acd502d950fbede Mon Sep 17 00:00:00 2001 From: DaanDeMeyer Date: Thu, 3 Jul 2025 14:17:31 +0200 Subject: [PATCH 1/2] fd-util: Move RAW_O_LARGEFILE definition to fd-util.h This is not something that comes from glibc, but which we invent ourselves. As such, it should not be part of our overrides of glibc headers, but instead should be part of one of our own headers, so let's move it to fd-util.h. Follow up for 194a690181c4857542a094d19f5808f8f4032ed9 --- src/basic/fd-util.h | 26 ++++++++++++++++++++++++++ src/basic/include/fcntl.h | 26 -------------------------- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 7d5b9f3083..75cc5310c7 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -18,6 +18,32 @@ #define EBADF_PAIR { -EBADF, -EBADF } #define EBADF_TRIPLET { -EBADF, -EBADF, -EBADF } +/* So O_LARGEFILE is generally implied by glibc, and defined to zero hence, because we only build in LFS + * mode. However, when invoking fcntl(F_GETFL) the flag is ORed into the result anyway — glibc does not mask + * it away. Which sucks. Let's define the actual value here, so that we can mask it ourselves. + * + * The precise definition is arch specific, so we use the values defined in the kernel (note that some + * are hexa and others are octal; duplicated as-is from the kernel definitions): + * - alpha, arm, arm64, m68k, mips, parisc, powerpc, sparc: each has a specific value; + * - others: they use the "generic" value (defined in include/uapi/asm-generic/fcntl.h) */ +#if O_LARGEFILE != 0 +# define RAW_O_LARGEFILE O_LARGEFILE +#else +# if defined(__alpha__) || defined(__arm__) || defined(__aarch64__) || defined(__m68k__) +# define RAW_O_LARGEFILE 0400000 +# elif defined(__mips__) +# define RAW_O_LARGEFILE 0x2000 +# elif defined(__parisc__) || defined(__hppa__) +# define RAW_O_LARGEFILE 000004000 +# elif defined(__powerpc__) +# define RAW_O_LARGEFILE 0200000 +# elif defined(__sparc__) +# define RAW_O_LARGEFILE 0x40000 +# else +# define RAW_O_LARGEFILE 00100000 +# endif +#endif + int close_nointr(int fd); int safe_close(int fd); void safe_close_pair(int p[static 2]); diff --git a/src/basic/include/fcntl.h b/src/basic/include/fcntl.h index 86e0c8bbf5..344287f768 100644 --- a/src/basic/include/fcntl.h +++ b/src/basic/include/fcntl.h @@ -13,32 +13,6 @@ #define F_SEAL_EXEC 0x0020 /* prevent chmod modifying exec bits */ #endif -/* So O_LARGEFILE is generally implied by glibc, and defined to zero hence, because we only build in LFS - * mode. However, when invoking fcntl(F_GETFL) the flag is ORed into the result anyway — glibc does not mask - * it away. Which sucks. Let's define the actual value here, so that we can mask it ourselves. - * - * The precise definition is arch specific, so we use the values defined in the kernel (note that some - * are hexa and others are octal; duplicated as-is from the kernel definitions): - * - alpha, arm, arm64, m68k, mips, parisc, powerpc, sparc: each has a specific value; - * - others: they use the "generic" value (defined in include/uapi/asm-generic/fcntl.h) */ -#if O_LARGEFILE != 0 -#define RAW_O_LARGEFILE O_LARGEFILE -#else -#if defined(__alpha__) || defined(__arm__) || defined(__aarch64__) || defined(__m68k__) -#define RAW_O_LARGEFILE 0400000 -#elif defined(__mips__) -#define RAW_O_LARGEFILE 0x2000 -#elif defined(__parisc__) || defined(__hppa__) -#define RAW_O_LARGEFILE 000004000 -#elif defined(__powerpc__) -#define RAW_O_LARGEFILE 0200000 -#elif defined(__sparc__) -#define RAW_O_LARGEFILE 0x40000 -#else -#define RAW_O_LARGEFILE 00100000 -#endif -#endif - /* This is defined since glibc-2.39. */ #ifndef AT_HANDLE_FID #define AT_HANDLE_FID AT_REMOVEDIR From 1543c2385c0a1afbdc59b50c8ecd132a0d60bbef Mon Sep 17 00:00:00 2001 From: DaanDeMeyer Date: Thu, 3 Jul 2025 14:23:16 +0200 Subject: [PATCH 2/2] fd-util: Move O_ACCMODE_STRICT to fd-util.h We're still discussing whether we want to remove this or not, but regardless of whether we end up removing it or not, it's something we add ourselves and as such should not live in our headers that override glibc headers, so let's move it to fd-util.h. --- src/basic/fd-util.h | 6 ++++++ src/basic/include/fcntl.h | 6 ------ src/libsystemd/sd-journal/journal-file.c | 5 +++++ src/libsystemd/sd-journal/journal-file.h | 5 +---- src/shared/journal-file-util.c | 1 + 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 75cc5310c7..25ad3e9361 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -44,6 +44,12 @@ # endif #endif +/* On musl, O_ACCMODE is defined as (03|O_SEARCH), unlike glibc which defines it as + * (O_RDONLY|O_WRONLY|O_RDWR). Additionally, O_SEARCH is simply defined as O_PATH. This changes the behaviour + * of O_ACCMODE in certain situations, which we don't want. This definition is copied from glibc and works + * around the problems with musl's definition. */ +#define O_ACCMODE_STRICT (O_RDONLY|O_WRONLY|O_RDWR) + int close_nointr(int fd); int safe_close(int fd); void safe_close_pair(int p[static 2]); diff --git a/src/basic/include/fcntl.h b/src/basic/include/fcntl.h index 344287f768..5f1d90ad79 100644 --- a/src/basic/include/fcntl.h +++ b/src/basic/include/fcntl.h @@ -17,9 +17,3 @@ #ifndef AT_HANDLE_FID #define AT_HANDLE_FID AT_REMOVEDIR #endif - -/* On musl, O_ACCMODE is defined as (03|O_SEARCH), unlike glibc which defines it as - * (O_RDONLY|O_WRONLY|O_RDWR). Additionally, O_SEARCH is simply defined as O_PATH. This changes the behaviour - * of O_ACCMODE in certain situations, which we don't want. This definition is copied from glibc and works - * around the problems with musl's definition. */ -#define O_ACCMODE_STRICT (O_RDONLY|O_WRONLY|O_RDWR) diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 224c3e23c7..baabb92fd7 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -4694,6 +4694,11 @@ bool journal_file_rotate_suggested(JournalFile *f, usec_t max_file_usec, int log return false; } +bool journal_file_writable(JournalFile *f) { + assert(f); + return (f->open_flags & O_ACCMODE_STRICT) != O_RDONLY; +} + static const char * const journal_object_type_table[] = { [OBJECT_UNUSED] = "unused", [OBJECT_DATA] = "data", diff --git a/src/libsystemd/sd-journal/journal-file.h b/src/libsystemd/sd-journal/journal-file.h index 9598879066..4a5e0d81e2 100644 --- a/src/libsystemd/sd-journal/journal-file.h +++ b/src/libsystemd/sd-journal/journal-file.h @@ -378,7 +378,4 @@ static inline uint32_t COMPRESSION_TO_HEADER_INCOMPATIBLE_FLAG(Compression c) { } } -static inline bool journal_file_writable(JournalFile *f) { - assert(f); - return (f->open_flags & O_ACCMODE_STRICT) != O_RDONLY; -} +bool journal_file_writable(JournalFile *f); diff --git a/src/shared/journal-file-util.c b/src/shared/journal-file-util.c index ce799e9256..990146b624 100644 --- a/src/shared/journal-file-util.c +++ b/src/shared/journal-file-util.c @@ -9,6 +9,7 @@ #include "chattr-util.h" #include "copy.h" #include "errno-util.h" +#include "fd-util.h" #include "journal-authenticate.h" #include "journal-file-util.h" #include "log.h"