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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 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 "macro.h"
6
#include "memory-util.h"
7

8
typedef void (*free_func_t)(void *p);
9

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

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

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

37
typedef struct SimpleCleanup {
38
        void *data;
39
        free_func_t destroy;
40
} SimpleCleanup;
41

42
typedef struct StaticDestructor {
43
        StaticDestructorType type;
44
        union {
45
                SimpleCleanup simple;
46
                ArrayCleanup array;
47
        };
48
} StaticDestructor;
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
/* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in
86
 * the same linking unit as the variables we want to destroy.) */
87
static inline void static_destruct(void) {
72,796✔
88
        if (!__start_SYSTEMD_STATIC_DESTRUCT)
72,796✔
89
                return;
90

91
        for (const StaticDestructor *d = ALIGN_PTR(__start_SYSTEMD_STATIC_DESTRUCT);
59,106✔
92
             d < __stop_SYSTEMD_STATIC_DESTRUCT;
735,387✔
93
             d = ALIGN_PTR(d + 1))
676,281✔
94
                switch (d->type) {
676,281✔
95
                case STATIC_DESTRUCTOR_SIMPLE:
676,232✔
96
                        d->simple.destroy(d->simple.data);
676,232✔
97
                        break;
676,232✔
98

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

103
                default:
×
UNCOV
104
                        assert_not_reached();
×
105
                }
106
}
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