udev: String substitutions can be done in ENV, too
[systemd_ALT.git] / src / basic / origin-id.h
blobc55b0a368a998e444e378dedafd8a260bfc24769
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
4 #include <pthread.h>
6 #include "random-util.h"
8 /* This pattern needs to be repeated exactly in multiple modules, so macro it.
9 * To ensure an object is not passed into a different module (e.g.: when two shared objects statically
10 * linked to libsystemd get loaded in the same process, and the object created by one is passed to the
11 * other, see https://github.com/systemd/systemd/issues/27216), create a random static global random
12 * (mixed with PID, so that we can also check for reuse after fork) that is stored in the object and
13 * checked by public API on use. */
14 #define _DEFINE_ORIGIN_ID_HELPERS(type, name, scope) \
15 static uint64_t origin_id; \
17 static void origin_id_initialize(void) { \
18 origin_id = random_u64(); \
19 } \
21 static uint64_t origin_id_query(void) { \
22 static pthread_once_t once = PTHREAD_ONCE_INIT; \
23 assert_se(pthread_once(&once, origin_id_initialize) == 0); \
24 return origin_id ^ getpid_cached(); \
25 } \
27 scope bool name##_origin_changed(type *p) { \
28 assert(p); \
29 return p->origin_id != origin_id_query(); \
32 #define DEFINE_ORIGIN_ID_HELPERS(type, name) \
33 _DEFINE_ORIGIN_ID_HELPERS(type, name,);
35 #define DEFINE_PRIVATE_ORIGIN_ID_HELPERS(type, name) \
36 _DEFINE_ORIGIN_ID_HELPERS(type, name, static);