• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14939761809

09 May 2025 06:22PM UTC coverage: 72.251% (-0.001%) from 72.252%
14939761809

push

github

web-flow
mount-tool: honor arg_canonicalize for ACTION_UMOUNT path_is_absolute() check too (#37398)

Split out from #36337

4 of 4 new or added lines in 2 files covered. (100.0%)

3591 existing lines in 114 files now uncovered.

297546 of 411820 relevant lines covered (72.25%)

704170.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/basic/static-destruct.h
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#pragma once
4

5
#include <errno.h>
6

7
#include "macro.h"
8
#include "memory-util.h"
9

10
typedef void (*free_func_t)(void *p);
11

12
/* A framework for registering static variables that shall be freed on shutdown of a process. It's a bit like gcc's
13
 * destructor attribute, but allows us to precisely schedule when we want to free the variables. This is supposed to
14
 * feel a bit like the gcc cleanup attribute, but for static variables. Note that this does not work for static
15
 * variables declared in .so's, as the list is private to the same linking unit. But maybe that's a good thing. */
16

17
#define _common_static_destruct_attrs_                                  \
18
        /* Older compilers don't know "retain" attribute. */            \
19
        _Pragma("GCC diagnostic ignored \"-Wattributes\"")              \
20
        /* The actual destructor structure we place in a special section to find it. */ \
21
        _section_("SYSTEMD_STATIC_DESTRUCT")                            \
22
        /* Use pointer alignment, since that is apparently what gcc does for static variables. */ \
23
        _alignptr_                                                      \
24
        /* Make sure this is not dropped from the image despite not being explicitly referenced. */ \
25
        _used_                                                          \
26
        /* Prevent garbage collection by the linker. */                 \
27
        _retain_                                                        \
28
        /* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section
29
         * packed next to each other so that we can enumerate it. */    \
30
        _variable_no_sanitize_address_
31

32
typedef enum StaticDestructorType {
33
        STATIC_DESTRUCTOR_SIMPLE,
34
        STATIC_DESTRUCTOR_ARRAY,
35
        _STATIC_DESTRUCTOR_TYPE_MAX,
36
        _STATIC_DESTRUCTOR_INVALID = -EINVAL,
37
} StaticDestructorType;
38

39
typedef struct SimpleCleanup {
40
        void *data;
41
        free_func_t destroy;
42
} SimpleCleanup;
43

44
typedef struct StaticDestructor {
45
        StaticDestructorType type;
46
        union {
47
                SimpleCleanup simple;
48
                ArrayCleanup array;
49
        };
50
} StaticDestructor;
51

52
#define STATIC_DESTRUCTOR_REGISTER(variable, func) \
53
        _STATIC_DESTRUCTOR_REGISTER(UNIQ, variable, func)
54

55
#define _STATIC_DESTRUCTOR_REGISTER(uq, variable, func)                 \
56
        /* Type-safe destructor */                                      \
57
        static void UNIQ_T(static_destructor_wrapper, uq)(void *p) {    \
58
                typeof(variable) *q = p;                                \
59
                func(q);                                                \
60
        }                                                               \
61
        _common_static_destruct_attrs_                                  \
62
        static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
63
                .type = STATIC_DESTRUCTOR_SIMPLE,                       \
64
                .simple.data = &(variable),                             \
65
                .simple.destroy = UNIQ_T(static_destructor_wrapper, uq), \
66
        }
67

68
#define STATIC_ARRAY_DESTRUCTOR_REGISTER(a, n, func)            \
69
        _STATIC_ARRAY_DESTRUCTOR_REGISTER(UNIQ, a, n, func)
70

71
#define _STATIC_ARRAY_DESTRUCTOR_REGISTER(uq, a, n, func)               \
72
        /* Type-safety check */                                         \
73
        _unused_ static void (* UNIQ_T(static_destructor_wrapper, uq))(typeof(a[0]) *x, size_t y) = (func); \
74
        _common_static_destruct_attrs_                                  \
75
        static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
76
                .type = STATIC_DESTRUCTOR_ARRAY,                        \
77
                .array.parray = (void**) &(a),                          \
78
                .array.pn = &(n),                                       \
79
                .array.pfunc = (free_array_func_t) (func),              \
80
        };
81

82
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work
83
 * even if no destructors are defined and the section is missing. */
84
extern const StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[];
85
extern const StaticDestructor _weak_ __stop_SYSTEMD_STATIC_DESTRUCT[];
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) {
73,158✔
90
        if (!__start_SYSTEMD_STATIC_DESTRUCT)
73,158✔
91
                return;
92

93
        for (const StaticDestructor *d = ALIGN_PTR(__start_SYSTEMD_STATIC_DESTRUCT);
59,191✔
94
             d < __stop_SYSTEMD_STATIC_DESTRUCT;
1,364,655✔
95
             d = ALIGN_PTR(d + 1))
1,305,464✔
96
                switch (d->type) {
1,305,464✔
97
                case STATIC_DESTRUCTOR_SIMPLE:
1,305,415✔
98
                        d->simple.destroy(d->simple.data);
1,305,415✔
99
                        break;
1,305,415✔
100

101
                case STATIC_DESTRUCTOR_ARRAY:
49✔
102
                        array_cleanup(&d->array);
49✔
103
                        break;
49✔
104

UNCOV
105
                default:
×
UNCOV
106
                        assert_not_reached();
×
107
                }
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc