summaryrefslogtreecommitdiffstats
path: root/src/basic/origin-id.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-10 20:49:52 +0000
commit55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch)
tree33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/basic/origin-id.h
parentInitial commit. (diff)
downloadsystemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz
systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/basic/origin-id.h')
-rw-r--r--src/basic/origin-id.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/basic/origin-id.h b/src/basic/origin-id.h
new file mode 100644
index 0000000..c55b0a3
--- /dev/null
+++ b/src/basic/origin-id.h
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <pthread.h>
+
+#include "random-util.h"
+
+/* This pattern needs to be repeated exactly in multiple modules, so macro it.
+ * To ensure an object is not passed into a different module (e.g.: when two shared objects statically
+ * linked to libsystemd get loaded in the same process, and the object created by one is passed to the
+ * other, see https://github.com/systemd/systemd/issues/27216), create a random static global random
+ * (mixed with PID, so that we can also check for reuse after fork) that is stored in the object and
+ * checked by public API on use. */
+#define _DEFINE_ORIGIN_ID_HELPERS(type, name, scope) \
+static uint64_t origin_id; \
+ \
+static void origin_id_initialize(void) { \
+ origin_id = random_u64(); \
+} \
+ \
+static uint64_t origin_id_query(void) { \
+ static pthread_once_t once = PTHREAD_ONCE_INIT; \
+ assert_se(pthread_once(&once, origin_id_initialize) == 0); \
+ return origin_id ^ getpid_cached(); \
+} \
+ \
+scope bool name##_origin_changed(type *p) { \
+ assert(p); \
+ return p->origin_id != origin_id_query(); \
+}
+
+#define DEFINE_ORIGIN_ID_HELPERS(type, name) \
+ _DEFINE_ORIGIN_ID_HELPERS(type, name,);
+
+#define DEFINE_PRIVATE_ORIGIN_ID_HELPERS(type, name) \
+ _DEFINE_ORIGIN_ID_HELPERS(type, name, static);