Debian Codegraph @

src/basic/origin-id.h

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