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

systemd / systemd / 15479777183

05 Jun 2025 09:07PM UTC coverage: 72.092% (+0.02%) from 72.075%
15479777183

push

github

web-flow
core: adding CGroup for io.systemd.Unit.List (second PR) (#37646)

This PR adds CGroup context/runtime for io.systemd.Unit.List method.

This is follow up for https://github.com/systemd/systemd/pull/37432.

235 of 295 new or added lines in 2 files covered. (79.66%)

3638 existing lines in 66 files now uncovered.

300163 of 416359 relevant lines covered (72.09%)

704675.28 hits per line

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

96.08
/src/core/unit-printf.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "sd-path.h"
4

5
#include "alloc-util.h"
6
#include "creds-util.h"
7
#include "env-util.h"
8
#include "fd-util.h"
9
#include "fileio.h"
10
#include "manager.h"
11
#include "specifier.h"
12
#include "string-util.h"
13
#include "unit.h"
14
#include "unit-name.h"
15
#include "unit-printf.h"
16

17
static int specifier_prefix_and_instance(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
153✔
18
        const Unit *u = ASSERT_PTR(userdata);
153✔
19

20
        return unit_name_to_prefix_and_instance(u->id, ret);
153✔
21
}
22

23
static int specifier_prefix(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
24
        const Unit *u = ASSERT_PTR(userdata);
3✔
25

26
        return unit_name_to_prefix(u->id, ret);
3✔
27
}
28

29
static int specifier_prefix_unescaped(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
30
        _cleanup_free_ char *p = NULL;
3✔
31
        const Unit *u = ASSERT_PTR(userdata);
3✔
32
        int r;
3✔
33

34
        r = unit_name_to_prefix(u->id, &p);
3✔
35
        if (r < 0)
3✔
36
                return r;
37

38
        return unit_name_unescape(p, ret);
3✔
39
}
40

41
static int specifier_instance_unescaped(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
561✔
42
        const Unit *u = ASSERT_PTR(userdata);
561✔
43

44
        return unit_name_unescape(strempty(u->instance), ret);
563✔
45
}
46

47
static int specifier_last_component(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
132✔
48
        const Unit *u = ASSERT_PTR(userdata);
132✔
49
        _cleanup_free_ char *prefix = NULL;
132✔
50
        char *dash;
132✔
51
        int r;
132✔
52

53
        r = unit_name_to_prefix(u->id, &prefix);
132✔
54
        if (r < 0)
132✔
55
                return r;
56

57
        dash = strrchr(prefix, '-');
132✔
58
        if (dash)
132✔
59
                return specifier_string(specifier, dash + 1, root, userdata, ret);
128✔
60

61
        *ret = TAKE_PTR(prefix);
4✔
62
        return 0;
4✔
63
}
64

65
static int specifier_last_component_unescaped(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
66
        _cleanup_free_ char *p = NULL;
3✔
67
        int r;
3✔
68

69
        r = specifier_last_component(specifier, data, root, userdata, &p);
3✔
70
        if (r < 0)
3✔
71
                return r;
72

73
        return unit_name_unescape(p, ret);
3✔
74
}
75

76
static int specifier_filename(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3,438✔
77
        const Unit *u = ASSERT_PTR(userdata);
3,438✔
78

79
        if (u->instance)
3,438✔
80
                return unit_name_path_unescape(u->instance, ret);
3,436✔
81
        else
82
                return unit_name_to_path(u->id, ret);
2✔
83
}
84

85
static void bad_specifier(const Unit *u, char specifier) {
9✔
86
        log_unit_warning(u, "Specifier '%%%c' used in unit configuration, which is deprecated. Please update your unit file, as it does not work as intended.", specifier);
9✔
87
}
9✔
88

89
static int specifier_cgroup(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
90
        const Unit *u = ASSERT_PTR(userdata);
3✔
91

92
        bad_specifier(u, specifier);
3✔
93

94
        return unit_get_cgroup_path_with_fallback(u, ret);
3✔
95
}
96

97
static int specifier_cgroup_root(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
98
        const Unit *u = ASSERT_PTR(userdata);
3✔
99

100
        bad_specifier(u, specifier);
3✔
101

102
        return strdup_to(ret, u->manager->cgroup_root);
3✔
103
}
104

105
static int specifier_cgroup_slice(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
106
        const Unit *u = ASSERT_PTR(userdata), *slice;
3✔
107

108
        bad_specifier(u, specifier);
3✔
109

110
        slice = UNIT_GET_SLICE(u);
3✔
111
        if (slice)
3✔
112
                return unit_get_cgroup_path_with_fallback(slice, ret);
2✔
113

114
        return strdup_to(ret, u->manager->cgroup_root);
1✔
115
}
116

117
static int specifier_special_directory(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
2,123✔
118
        const Unit *u = ASSERT_PTR(userdata);
2,123✔
119

120
        return strdup_to(ret, u->manager->prefix[PTR_TO_UINT(data)]);
2,123✔
121
}
122

123
static int specifier_credentials_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
3✔
124
        const Unit *u = ASSERT_PTR(userdata);
3✔
125
        char *d;
3✔
126

127
        assert(ret);
3✔
128

129
        d = strjoin(u->manager->prefix[EXEC_DIRECTORY_RUNTIME], "/credentials/", u->id);
3✔
130
        if (!d)
3✔
131
                return -ENOMEM;
132

133
        *ret = d;
3✔
134
        return 0;
3✔
135
}
136

UNCOV
137
static int specifier_shared_data_dir(char specifier, const void *data, const char *root, const void *userdata, char **ret) {
×
UNCOV
138
        const Unit *u = ASSERT_PTR(userdata);
×
139

UNCOV
140
        assert(ret);
×
141

UNCOV
142
        return sd_path_lookup(MANAGER_IS_SYSTEM(u->manager) ? SD_PATH_SYSTEM_SHARED : SD_PATH_USER_SHARED, NULL, ret);
×
143
}
144

145
int unit_name_printf(const Unit *u, const char* format, char **ret) {
86,286✔
146
        /*
147
         * This will use the passed string as format string and replace the following specifiers (which should all be
148
         * safe for inclusion in unit names):
149
         *
150
         * %n: the full id of the unit                 (foo-aaa@bar.waldo)
151
         * %N: the id of the unit without the suffix   (foo-aaa@bar)
152
         * %p: the prefix                              (foo-aaa)
153
         * %i: the instance                            (bar)
154
         * %j: the last component of the prefix        (aaa)
155
         */
156

157
        const Specifier table[] = {
86,286✔
158
                { 'i', specifier_string,              u->instance },
86,286✔
159
                { 'j', specifier_last_component,      NULL },
160
                { 'n', specifier_string,              u->id },
86,286✔
161
                { 'N', specifier_prefix_and_instance, NULL },
162
                { 'p', specifier_prefix,              NULL },
163

164
                COMMON_SYSTEM_SPECIFIERS,
165

166
                COMMON_CREDS_SPECIFIERS(u->manager->runtime_scope),
86,286✔
167
                {}
168
        };
169

170
        assert(u);
86,286✔
171
        assert(format);
86,286✔
172
        assert(ret);
86,286✔
173

174
        return specifier_printf(format, UNIT_NAME_MAX, table, NULL, u, ret);
86,286✔
175
}
176

177
int unit_full_printf_full(const Unit *u, const char *format, size_t max_length, char **ret) {
152,584✔
178
        /* This is similar to unit_name_printf() but also supports unescaping. Also, adds a couple of
179
         * additional codes (which are likely not suitable for unescaped inclusion in unit names):
180
         *
181
         * %f: the unescaped instance if set, otherwise the id unescaped as path
182
         *
183
         * %c: cgroup path of unit (deprecated)
184
         * %r: where units in this slice are placed in the cgroup tree (deprecated)
185
         * %R: the root of this systemd's instance tree (deprecated)
186
         *
187
         * %C: the cache directory root (e.g. /var/cache or $XDG_CACHE_HOME)
188
         * %d: the credentials directory ($CREDENTIALS_DIRECTORY)
189
         * %D: the shared data root (e.g. /usr/share or $XDG_DATA_HOME)
190
         * %E: the configuration directory root (e.g. /etc or $XDG_CONFIG_HOME)
191
         * %L: the log directory root (e.g. /var/log or $XDG_STATE_HOME/log)
192
         * %S: the state directory root (e.g. /var/lib or $XDG_STATE_HOME)
193
         * %t: the runtime directory root (e.g. /run or $XDG_RUNTIME_DIR)
194
         *
195
         * %h: the homedir of the running user
196
         * %s: the shell of the running user
197
         *
198
         * NOTICE: When you add new entries here, please be careful: specifiers which depend on settings of
199
         * the unit file itself are broken by design, as they would resolve differently depending on whether
200
         * they are used before or after the relevant configuration setting. Hence: don't add them.
201
         */
202

203
        assert(u);
152,584✔
204
        assert(format);
152,584✔
205
        assert(ret);
152,584✔
206

207
        const Specifier table[] = {
152,584✔
208
                { 'i', specifier_string,                   u->instance },
152,584✔
209
                { 'I', specifier_instance_unescaped,       NULL },
210
                { 'j', specifier_last_component,           NULL },
211
                { 'J', specifier_last_component_unescaped, NULL },
212
                { 'n', specifier_string,                   u->id },
152,584✔
213
                { 'N', specifier_prefix_and_instance,      NULL },
214
                { 'p', specifier_prefix,                   NULL },
215
                { 'P', specifier_prefix_unescaped,         NULL },
216

217
                { 'f', specifier_filename,                 NULL },
218
                { 'y', specifier_real_path,                u->fragment_path },
152,584✔
219
                { 'Y', specifier_real_directory,           u->fragment_path },
152,584✔
220

221
                { 'c', specifier_cgroup,                   NULL },  /* deprecated, see 1b89b0c499cd4bf0ff389caab4ecaae6e75f9d4e */
222
                { 'r', specifier_cgroup_slice,             NULL },  /* deprecated, see 1b89b0c499cd4bf0ff389caab4ecaae6e75f9d4e */
223
                { 'R', specifier_cgroup_root,              NULL },  /* deprecated, see 1b89b0c499cd4bf0ff389caab4ecaae6e75f9d4e */
224

225
                { 'C', specifier_special_directory,        UINT_TO_PTR(EXEC_DIRECTORY_CACHE) },
226
                { 'd', specifier_credentials_dir,          NULL },
227
                { 'D', specifier_shared_data_dir,          NULL },
228
                { 'E', specifier_special_directory,        UINT_TO_PTR(EXEC_DIRECTORY_CONFIGURATION) },
229
                { 'L', specifier_special_directory,        UINT_TO_PTR(EXEC_DIRECTORY_LOGS) },
230
                { 'S', specifier_special_directory,        UINT_TO_PTR(EXEC_DIRECTORY_STATE) },
231
                { 't', specifier_special_directory,        UINT_TO_PTR(EXEC_DIRECTORY_RUNTIME) },
232

233
                { 'h', specifier_user_home,                NULL },
234
                { 's', specifier_user_shell,               NULL },
235

236
                COMMON_SYSTEM_SPECIFIERS,
237

238
                COMMON_CREDS_SPECIFIERS(u->manager->runtime_scope),
152,584✔
239

240
                COMMON_TMP_SPECIFIERS,
241
                {}
242
        };
243

244
        return specifier_printf(format, max_length, table, NULL, u, ret);
152,584✔
245
}
246

247
int unit_full_printf(const Unit *u, const char *text, char **ret) {
97,256✔
248
        return unit_full_printf_full(u, text, LONG_LINE_MAX, ret);
97,256✔
249
}
250

251
int unit_path_printf(const Unit *u, const char *text, char **ret) {
44,564✔
252
        return unit_full_printf_full(u, text, PATH_MAX-1, ret);
44,564✔
253
}
254

255
int unit_fd_printf(const Unit *u, const char *text, char **ret) {
2,962✔
256
        return unit_full_printf_full(u, text, FDNAME_MAX, ret);
2,962✔
257
}
258

259
int unit_cred_printf(const Unit *u, const char *text, char **ret) {
4,042✔
260
        return unit_full_printf_full(u, text, CREDENTIAL_NAME_MAX, ret);
4,042✔
261
}
262

263
int unit_env_printf(const Unit *u, const char *text, char **ret) {
2,794✔
264
        return unit_full_printf_full(u, text, sc_arg_max(), ret);
2,794✔
265
}
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