From c74380cceb6185c4b9d8f01809324522ed298c37 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 4 Mar 2025 17:25:42 +0100 Subject: [PATCH 1/5] missing_fs: drop FS_KEY_DESCRIPTOR_SIZE We now directly import linux/fscrypt.h, so this def is duplicate --- src/basic/missing_fs.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/basic/missing_fs.h b/src/basic/missing_fs.h index dfd79504bc..c3b2746b07 100644 --- a/src/basic/missing_fs.h +++ b/src/basic/missing_fs.h @@ -50,14 +50,7 @@ assert_cc(RWF_DONTCACHE == __RWF_DONTCACHE_SAVED__); #define EXT4_IOC_RESIZE_FS _IOW('f', 16, __u64) #endif -/* linux/fscrypt.h */ -#ifndef FS_KEY_DESCRIPTOR_SIZE -# define FS_KEY_DESCRIPTOR_SIZE 8 -#else -assert_cc(FS_KEY_DESCRIPTOR_SIZE == 8); -#endif - -/* linux/exportfs.h */ +/* linux/exportfs.h (33c5ac9175195c36a0b7005aaf503a2e81f117a1, 5.5) */ #ifndef FILEID_KERNFS #define FILEID_KERNFS 0xfe #endif From 87838420aa192f417d25eb6c80f15b0eb8075a5d Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 4 Mar 2025 18:08:21 +0100 Subject: [PATCH 2/5] basic/sys/mount: sort includes --- src/basic/sys/mount.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/basic/sys/mount.h b/src/basic/sys/mount.h index 13a3ba6851..16cf080ab8 100644 --- a/src/basic/sys/mount.h +++ b/src/basic/sys/mount.h @@ -2,8 +2,8 @@ #pragma once #include -#include #include +#include #include #include #include From f2f9b82724a1f9c0d70068c431a372866523d9ff Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 4 Mar 2025 18:41:23 +0100 Subject: [PATCH 3/5] sd-journal/journal-send: use is_main_thread() where appropriate --- src/libsystemd/sd-journal/journal-send.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsystemd/sd-journal/journal-send.c b/src/libsystemd/sd-journal/journal-send.c index 93f9fc9df0..d55ca86b18 100644 --- a/src/libsystemd/sd-journal/journal-send.c +++ b/src/libsystemd/sd-journal/journal-send.c @@ -88,7 +88,7 @@ void close_journal_fd(void) { if (!RUNNING_ON_VALGRIND) return; - if (getpid_cached() != gettid()) + if (!is_main_thread()) return; if (fd_plus_one <= 0) From c133fcd5c0ebaf72bfea12f165c3b727a0fb5ffb Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 4 Mar 2025 18:42:24 +0100 Subject: [PATCH 4/5] locale-util: modernize is_locale_utf8() a bit --- src/basic/locale-util.c | 66 ++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 41 deletions(-) diff --git a/src/basic/locale-util.c b/src/basic/locale-util.c index 09bdb37300..c7095ece23 100644 --- a/src/basic/locale-util.c +++ b/src/basic/locale-util.c @@ -17,8 +17,8 @@ #include "fileio.h" #include "hashmap.h" #include "locale-util.h" -#include "missing_syscall.h" #include "path-util.h" +#include "process-util.h" #include "set.h" #include "string-table.h" #include "string-util.h" @@ -283,64 +283,48 @@ int locale_is_installed(const char *name) { return true; } -bool is_locale_utf8(void) { - static int cached_answer = -1; +static bool is_locale_utf8_impl(void) { const char *set; int r; - /* Note that we default to 'true' here, since today UTF8 is - * pretty much supported everywhere. */ - - if (cached_answer >= 0) - goto out; + /* Note that we default to 'true' here, since today UTF8 is pretty much supported everywhere. */ r = secure_getenv_bool("SYSTEMD_UTF8"); - if (r >= 0) { - cached_answer = r; - goto out; - } else if (r != -ENXIO) + if (r >= 0) + return r; + if (r != -ENXIO) log_debug_errno(r, "Failed to parse $SYSTEMD_UTF8, ignoring: %m"); /* This function may be called from libsystemd, and setlocale() is not thread safe. Assuming yes. */ - if (gettid() != raw_getpid()) { - cached_answer = true; - goto out; - } + if (!is_main_thread()) + return true; - if (!setlocale(LC_ALL, "")) { - cached_answer = true; - goto out; - } + if (!setlocale(LC_ALL, "")) + return true; set = nl_langinfo(CODESET); - if (!set) { - cached_answer = true; - goto out; - } + if (!set || streq(set, "UTF-8")) + return true; - if (streq(set, "UTF-8")) { - cached_answer = true; - goto out; - } - - /* For LC_CTYPE=="C" return true, because CTYPE is effectively - * unset and everything can do to UTF-8 nowadays. */ set = setlocale(LC_CTYPE, NULL); - if (!set) { - cached_answer = true; - goto out; - } + if (!set) + return true; - /* Check result, but ignore the result if C was set - * explicitly. */ - cached_answer = - STR_IN_SET(set, "C", "POSIX") && + /* Unless LC_CTYPE is explicitly overridden, return true. Because here CTYPE is effectively unset + * and everything can do to UTF-8 nowadays. */ + return STR_IN_SET(set, "C", "POSIX") && !getenv("LC_ALL") && !getenv("LC_CTYPE") && !getenv("LANG"); +} -out: - return (bool) cached_answer; +bool is_locale_utf8(void) { + static int cached = -1; + + if (cached < 0) + cached = is_locale_utf8_impl(); + + return cached; } void locale_variables_free(char *l[_VARIABLE_LC_MAX]) { From 33db9f214b1b21cb9550ff0c4fc5ad7904f9ebcc Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 4 Mar 2025 18:49:04 +0100 Subject: [PATCH 5/5] missing_syscall: drop raw_getpid() This used to be relevant since in old versions of glibc an internal cache is maintained, while we might sidestep their invalidation with raw_clone(). After glibc 2.25 getpid() is a trivial wrapper for the syscall, and hence there's no need to have a separate raw_getpid(). --- src/basic/missing_syscall.h | 10 ---------- src/basic/process-util.c | 4 ++-- src/basic/signal-util.c | 4 ++-- src/test/test-process-util.c | 6 +++--- src/test/test-raw-clone.c | 6 +++--- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/basic/missing_syscall.h b/src/basic/missing_syscall.h index aae07a3f37..296e39b919 100644 --- a/src/basic/missing_syscall.h +++ b/src/basic/missing_syscall.h @@ -63,16 +63,6 @@ static inline int missing_ioprio_set(int which, int who, int ioprio) { /* ======================================================================= */ -static inline pid_t raw_getpid(void) { -#if defined(__alpha__) - return (pid_t) syscall(__NR_getxpid); -#else - return (pid_t) syscall(__NR_getpid); -#endif -} - -/* ======================================================================= */ - #if !HAVE_KCMP static inline int missing_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) { return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2); diff --git a/src/basic/process-util.c b/src/basic/process-util.c index 80eeca6c0a..c19890b7c3 100644 --- a/src/basic/process-util.c +++ b/src/basic/process-util.c @@ -1441,7 +1441,7 @@ pid_t getpid_cached(void) { case CACHED_PID_UNSET: { /* Not initialized yet, then do so now */ pid_t new_pid; - new_pid = raw_getpid(); + new_pid = getpid(); if (!installed) { /* __register_atfork() either returns 0 or -ENOMEM, in its glibc implementation. Since it's @@ -1462,7 +1462,7 @@ pid_t getpid_cached(void) { } case CACHED_PID_BUSY: /* Somebody else is currently initializing */ - return raw_getpid(); + return getpid(); default: /* Properly initialized */ return current_value; diff --git a/src/basic/signal-util.c b/src/basic/signal-util.c index 7abee0d29f..f5afe34307 100644 --- a/src/basic/signal-util.c +++ b/src/basic/signal-util.c @@ -280,13 +280,13 @@ void propagate_signal(int sig, siginfo_t *siginfo) { /* To be called from a signal handler. Will raise the same signal again, in our process + in our threads. * - * Note that we use raw_getpid() instead of getpid_cached(). We might have forked with raw_clone() + * Note that we use getpid() instead of getpid_cached(). We might have forked with raw_clone() * earlier (see PID 1), and hence let's go to the raw syscall here. In particular as this is not * performance sensitive code. * * Note that we use kill() rather than raise() as fallback, for similar reasons. */ - p = raw_getpid(); + p = getpid(); if (rt_tgsigqueueinfo(p, gettid(), sig, siginfo) < 0) assert_se(kill(p, sig) >= 0); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 0f00089eec..e9a461b141 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -581,7 +581,7 @@ TEST(getpid_cached) { siginfo_t si; pid_t a, b, c, d, e, f, child; - a = raw_getpid(); + a = getpid(); b = getpid_cached(); c = getpid(); @@ -593,7 +593,7 @@ TEST(getpid_cached) { if (child == 0) { /* In child */ - a = raw_getpid(); + a = getpid(); b = getpid_cached(); c = getpid(); @@ -602,7 +602,7 @@ TEST(getpid_cached) { _exit(EXIT_SUCCESS); } - d = raw_getpid(); + d = getpid(); e = getpid_cached(); f = getpid(); diff --git a/src/test/test-raw-clone.c b/src/test/test-raw-clone.c index 23ec7d1aa0..a43e2c2c29 100644 --- a/src/test/test-raw-clone.c +++ b/src/test/test-raw-clone.c @@ -14,13 +14,13 @@ TEST(raw_clone) { parent = getpid(); log_info("before clone: getpid()→"PID_FMT, parent); - assert_se(raw_getpid() == parent); + assert_se(getpid() == parent); pid = raw_clone(0); assert_se(pid >= 0); - pid2 = raw_getpid(); - log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT, + pid2 = getpid(); + log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" getpid()→"PID_FMT, pid, getpid(), pid2); if (pid == 0) { assert_se(pid2 != parent);