| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#pragma once |
| 4 |
|
| 5 |
#include "basic-forward.h" |
| 6 |
|
| 7 |
typedef void (*free_func_t)(void *p); |
| 8 |
|
| 9 |
/* A framework for registering static variables that shall be freed on shutdown of a process. It's a bit like gcc's |
| 10 |
* destructor attribute, but allows us to precisely schedule when we want to free the variables. This is supposed to |
| 11 |
* feel a bit like the gcc cleanup attribute, but for static variables. Note that this does not work for static |
| 12 |
* variables declared in .so's, as the list is private to the same linking unit. But maybe that's a good thing. */ |
| 13 |
|
| 14 |
#define _common_static_destruct_attrs_ \ |
| 15 |
/* The actual destructor structure we place in a special section to find it. */ \ |
| 16 |
_section_("SYSTEMD_STATIC_DESTRUCT") \ |
| 17 |
/* Use pointer alignment, since that is apparently what gcc does for static variables. */ \ |
| 18 |
_alignptr_ \ |
| 19 |
/* Make sure this is not dropped from the image despite not being explicitly referenced. */ \ |
| 20 |
_used_ \ |
| 21 |
/* Prevent garbage collection by the linker. */ \ |
| 22 |
_retain_ \ |
| 23 |
/* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section |
| 24 |
* packed next to each other so that we can enumerate it. */ \ |
| 25 |
_variable_no_sanitize_address_ |
| 26 |
|
| 27 |
typedef enum StaticDestructorType { |
| 28 |
STATIC_DESTRUCTOR_SIMPLE, |
| 29 |
STATIC_DESTRUCTOR_ARRAY, |
| 30 |
_STATIC_DESTRUCTOR_TYPE_MAX, |
| 31 |
_STATIC_DESTRUCTOR_INVALID = -EINVAL, |
| 32 |
} StaticDestructorType; |
| 33 |
|
| 34 |
typedef struct SimpleCleanup { |
| 35 |
void *data; |
| 36 |
free_func_t destroy; |
| 37 |
} SimpleCleanup; |
| 38 |
|
| 39 |
/* Note: see the comment on struct Option in options.h for why _alignptr_ is required here. */ |
| 40 |
typedef struct _alignptr_ StaticDestructor { |
| 41 |
StaticDestructorType type; |
| 42 |
union { |
| 43 |
SimpleCleanup simple; |
| 44 |
ArrayCleanup array; |
| 45 |
}; |
| 46 |
} StaticDestructor; |
| 47 |
|
| 48 |
assert_cc(sizeof(StaticDestructor) % sizeof(void*) == 0); |
| 49 |
|
| 50 |
#define STATIC_DESTRUCTOR_REGISTER(variable, func) \ |
| 51 |
_STATIC_DESTRUCTOR_REGISTER(UNIQ, variable, func) |
| 52 |
|
| 53 |
#define _STATIC_DESTRUCTOR_REGISTER(uq, variable, func) \ |
| 54 |
/* Type-safe destructor */ \ |
| 55 |
static void UNIQ_T(static_destructor_wrapper, uq)(void *p) { \ |
| 56 |
typeof(variable) *q = p; \ |
| 57 |
func(q); \ |
| 58 |
} \ |
| 59 |
_common_static_destruct_attrs_ \ |
| 60 |
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \ |
| 61 |
.type = STATIC_DESTRUCTOR_SIMPLE, \ |
| 62 |
.simple.data = &(variable), \ |
| 63 |
.simple.destroy = UNIQ_T(static_destructor_wrapper, uq), \ |
| 64 |
} |
| 65 |
|
| 66 |
#define STATIC_ARRAY_DESTRUCTOR_REGISTER(a, n, func) \ |
| 67 |
_STATIC_ARRAY_DESTRUCTOR_REGISTER(UNIQ, a, n, func) |
| 68 |
|
| 69 |
#define _STATIC_ARRAY_DESTRUCTOR_REGISTER(uq, a, n, func) \ |
| 70 |
/* Type-safety check */ \ |
| 71 |
_unused_ static void (* UNIQ_T(static_destructor_wrapper, uq))(typeof(a[0]) *x, size_t y) = (func); \ |
| 72 |
_common_static_destruct_attrs_ \ |
| 73 |
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \ |
| 74 |
.type = STATIC_DESTRUCTOR_ARRAY, \ |
| 75 |
.array.parray = (void**) &(a), \ |
| 76 |
.array.pn = &(n), \ |
| 77 |
.array.pfunc = (free_array_func_t) (func), \ |
| 78 |
}; |
| 79 |
|
| 80 |
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work |
| 81 |
* even if no destructors are defined and the section is missing. */ |
| 82 |
extern const StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[]; |
| 83 |
extern const StaticDestructor _weak_ __stop_SYSTEMD_STATIC_DESTRUCT[]; |
| 84 |
|
| 85 |
void static_destruct_impl(const StaticDestructor *start, const StaticDestructor *end); |
| 86 |
|
| 87 |
/* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in |
| 88 |
* the same linking unit as the variables we want to destroy.) */ |
| 89 |
static inline void static_destruct(void) { |
| 90 |
return static_destruct_impl(__start_SYSTEMD_STATIC_DESTRUCT, __stop_SYSTEMD_STATIC_DESTRUCT); |
| 91 |
} |