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

systemd / systemd / 29132483780

10 Jul 2026 08:43PM UTC coverage: 72.912% (+0.2%) from 72.702%
29132483780

push

github

bluca
man: run forgotten 'update-man-rules'

344600 of 472622 relevant lines covered (72.91%)

1365091.83 hits per line

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

61.78
/src/debug-generator/debug-generator.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <unistd.h>
4

5
#include "alloc-util.h"
6
#include "bitfield.h"
7
#include "creds-util.h"
8
#include "dlopen-note.h"
9
#include "dropin.h"
10
#include "errno-util.h"
11
#include "extract-word.h"
12
#include "fileio.h"
13
#include "log.h"
14
#include "generator.h"
15
#include "initrd-util.h"
16
#include "parse-util.h"
17
#include "path-util.h"
18
#include "proc-cmdline.h"
19
#include "recurse-dir.h"
20
#include "special.h"
21
#include "string-util.h"
22
#include "strv.h"
23
#include "unit-file.h"
24
#include "unit-name.h"
25

26
static const char *arg_dest = NULL;
27
static char *arg_default_unit = NULL;
28
static char **arg_mask = NULL;
29
static char **arg_wants = NULL;
30
static bool arg_debug_shell = false;
31
static char *arg_debug_tty = NULL;
32
static char *arg_default_debug_tty = NULL;
33
static uint32_t arg_breakpoints = 0;
34

35
STATIC_DESTRUCTOR_REGISTER(arg_default_unit, freep);
20✔
36
STATIC_DESTRUCTOR_REGISTER(arg_mask, strv_freep);
20✔
37
STATIC_DESTRUCTOR_REGISTER(arg_wants, strv_freep);
20✔
38
STATIC_DESTRUCTOR_REGISTER(arg_debug_tty, freep);
20✔
39
STATIC_DESTRUCTOR_REGISTER(arg_default_debug_tty, freep);
20✔
40

41
typedef enum BreakpointType {
42
        BREAKPOINT_PRE_UDEV,
43
        BREAKPOINT_PRE_BASIC,
44
        BREAKPOINT_PRE_SYSROOT_MOUNT,
45
        BREAKPOINT_PRE_SWITCH_ROOT,
46
        _BREAKPOINT_TYPE_MAX,
47
        _BREAKPOINT_TYPE_INVALID = -EINVAL,
48
} BreakpointType;
49

50
typedef enum BreakpointValidity {
51
        BREAKPOINT_DEFAULT   = 1 << 0,
52
        BREAKPOINT_IN_INITRD = 1 << 1,
53
        BREAKPOINT_ON_HOST   = 1 << 2,
54
} BreakpointValidity;
55

56
typedef struct BreakpointInfo {
57
        BreakpointType type;
58
        const char *name;
59
        const char *unit;
60
        BreakpointValidity validity;
61
} BreakpointInfo;
62

63
static const struct BreakpointInfo breakpoint_info_table[_BREAKPOINT_TYPE_MAX] = {
64
        { BREAKPOINT_PRE_UDEV,          "pre-udev",        "breakpoint-pre-udev.service",        BREAKPOINT_IN_INITRD | BREAKPOINT_ON_HOST },
65
        { BREAKPOINT_PRE_BASIC,         "pre-basic",       "breakpoint-pre-basic.service",       BREAKPOINT_IN_INITRD | BREAKPOINT_ON_HOST },
66
        { BREAKPOINT_PRE_SYSROOT_MOUNT, "pre-mount",       "breakpoint-pre-mount.service",       BREAKPOINT_IN_INITRD                      },
67
        { BREAKPOINT_PRE_SWITCH_ROOT,   "pre-switch-root", "breakpoint-pre-switch-root.service", BREAKPOINT_IN_INITRD | BREAKPOINT_DEFAULT },
68
};
69

70
static bool breakpoint_applies(const BreakpointInfo *info, int log_level) {
66✔
71
        assert(info);
66✔
72

73
        if (in_initrd() && !FLAGS_SET(info->validity, BREAKPOINT_IN_INITRD))
66✔
74
                log_full(log_level, "Breakpoint '%s' not valid in the initrd, ignoring.", info->name);
×
75
        else if (!in_initrd() && !FLAGS_SET(info->validity, BREAKPOINT_ON_HOST))
66✔
76
                log_full(log_level, "Breakpoint '%s' not valid on the host, ignoring.", info->name);
22✔
77
        else
78
                return true;
79

80
        return false;
81
}
82

83
static BreakpointType parse_breakpoint_from_string_one(const char *s) {
52✔
84
        assert(s);
52✔
85

86
        FOREACH_ELEMENT(i, breakpoint_info_table)
130✔
87
                if (streq(i->name, s))
130✔
88
                        return i->type;
52✔
89

90
        return _BREAKPOINT_TYPE_INVALID;
91
}
92

93
static int parse_breakpoint_from_string(const char *s, uint32_t *ret_breakpoints) {
54✔
94
        uint32_t breakpoints = 0;
54✔
95
        int r;
54✔
96

97
        assert(ret_breakpoints);
54✔
98

99
        /* Empty value? set default breakpoint */
100
        if (isempty(s)) {
94✔
101
                bool found_default = false;
63✔
102

103
                FOREACH_ELEMENT(i, breakpoint_info_table)
63✔
104
                        if (FLAGS_SET(i->validity, BREAKPOINT_DEFAULT) && breakpoint_applies(i, INT_MAX)) {
56✔
105
                                assert(i->type >= 0 && i->type < _BREAKPOINT_TYPE_MAX); /* silence coverity */
7✔
106
                                breakpoints |= UINT32_C(1) << i->type;
7✔
107
                                found_default = true;
7✔
108
                                break;
7✔
109
                        }
110

111
                if (!found_default)
7✔
112
                        log_warning("No default breakpoint defined %s, ignoring.",
14✔
113
                                    in_initrd() ? "in the initrd" : "on the host");
114
        } else
115
                for (;;) {
92✔
116
                        _cleanup_free_ char *t = NULL;
92✔
117
                        BreakpointType tt;
92✔
118

119
                        r = extract_first_word(&s, &t, ",", EXTRACT_DONT_COALESCE_SEPARATORS);
92✔
120
                        if (r < 0)
92✔
121
                                return r;
×
122
                        if (r == 0)
92✔
123
                                break;
124

125
                        tt = parse_breakpoint_from_string_one(t);
52✔
126
                        if (tt < 0) {
52✔
127
                                log_warning("Invalid breakpoint value '%s', ignoring.", t);
×
128
                                continue;
×
129
                        }
130

131
                        if (breakpoint_applies(&breakpoint_info_table[tt], LOG_WARNING))
52✔
132
                                breakpoints |= UINT32_C(1) << tt;
37✔
133
                }
134

135
        *ret_breakpoints = breakpoints;
54✔
136

137
        return 0;
54✔
138
}
139

140
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
205✔
141
        int r;
205✔
142

143
        assert(key);
205✔
144

145
        if (streq(key, "systemd.mask")) {
205✔
146
                char *n;
42✔
147

148
                if (proc_cmdline_value_missing(key, value))
42✔
149
                        return 0;
×
150

151
                r = unit_name_mangle(value, UNIT_NAME_MANGLE_WARN, &n);
42✔
152
                if (r < 0)
42✔
153
                        return log_error_errno(r, "Failed to glob unit name: %m");
×
154

155
                if (strv_consume(&arg_mask, n) < 0)
42✔
156
                        return log_oom();
×
157

158
        } else if (streq(key, "systemd.wants")) {
163✔
159
                char *n;
42✔
160

161
                if (proc_cmdline_value_missing(key, value))
42✔
162
                        return 0;
×
163

164
                r = unit_name_mangle(value, UNIT_NAME_MANGLE_WARN, &n);
42✔
165
                if (r < 0)
42✔
166
                        return log_error_errno(r, "Failed to glob unit name: %m");
×
167

168
                if (strv_consume(&arg_wants, n) < 0)
42✔
169
                        return log_oom();
×
170

171
        } else if (proc_cmdline_key_streq(key, "systemd.debug_shell")) {
121✔
172

173
                r = value ? parse_boolean(value) : 1;
35✔
174
                arg_debug_shell = r != 0;
35✔
175
                if (r >= 0)
35✔
176
                        return 0;
177

178
                return free_and_strdup_warn(&arg_debug_tty, skip_dev_prefix(value));
9✔
179

180
        } else if (proc_cmdline_key_streq(key, "systemd.default_debug_tty")) {
86✔
181

182
                if (proc_cmdline_value_missing(key, value))
8✔
183
                        return 0;
184

185
                return free_and_strdup_warn(&arg_default_debug_tty, skip_dev_prefix(value));
8✔
186

187
        } else if (streq(key, "systemd.unit")) {
78✔
188

189
                if (proc_cmdline_value_missing(key, value))
2✔
190
                        return 0;
191

192
                return free_and_strdup_warn(&arg_default_unit, value);
2✔
193

194
        } else if (streq(key, "systemd.break")) {
76✔
195
                uint32_t breakpoints = 0;
54✔
196

197
                r = parse_breakpoint_from_string(value, &breakpoints);
54✔
198
                if (r < 0)
54✔
199
                        return log_warning_errno(r, "Failed to parse breakpoint value '%s': %m", value);
×
200

201
                arg_breakpoints |= breakpoints;
54✔
202

203
        } else if (!value) {
22✔
204
                const char *target;
11✔
205

206
                target = runlevel_to_target(key);
11✔
207
                if (target)
11✔
208
                        return free_and_strdup_warn(&arg_default_unit, target);
×
209
        }
210

211
        return 0;
212
}
213

214
static int generate_mask_symlinks(void) {
20✔
215
        int r = 0;
20✔
216

217
        STRV_FOREACH(u, arg_mask) {
62✔
218
                _cleanup_free_ char *p = NULL;
42✔
219

220
                p = path_join(arg_dest, *u);
42✔
221
                if (!p)
42✔
222
                        return log_oom();
×
223

224
                if (symlink("/dev/null", p) < 0)
42✔
225
                        RET_GATHER(r, log_error_errno(errno, "Failed to create mask symlink '%s': %m", p));
×
226
        }
227

228
        return r;
229
}
230

231
static int generate_wants_symlinks(void) {
20✔
232
        int r = 0;
20✔
233

234
        STRV_FOREACH(u, arg_wants) {
109✔
235
                _cleanup_free_ char *f = NULL;
89✔
236
                const char *target;
89✔
237

238
                /* This should match what do_queue_default_job() in core/main.c does. */
239
                if (arg_default_unit)
89✔
240
                        target = arg_default_unit;
241
                else if (in_initrd())
77✔
242
                        target = SPECIAL_INITRD_TARGET;
243
                else
244
                        target = SPECIAL_DEFAULT_TARGET;
44✔
245

246
                f = path_join(SYSTEM_DATA_UNIT_DIR, *u);
89✔
247
                if (!f)
89✔
248
                        return log_oom();
×
249

250
                RET_GATHER(r, generator_add_symlink(arg_dest, target, "wants", f));
89✔
251
        }
252

253
        return r;
254
}
255

256
static int install_debug_shell_dropin(void) {
18✔
257
        const char *tty = arg_debug_tty ?: arg_default_debug_tty;
18✔
258
        int r;
9✔
259

260
        if (!tty || path_equal(tty, skip_dev_prefix(DEBUGTTY)))
18✔
261
                return 0;
262

263
        r = write_drop_in_format(arg_dest, "debug-shell.service", 50, "tty",
9✔
264
                                 "# Automatically generated by systemd-debug-generator\n\n"
265
                                 "[Unit]\n"
266
                                 "Description=Early root shell on /dev/%s FOR DEBUGGING ONLY\n"
267
                                 "ConditionPathExists=\n"
268
                                 "\n[Service]\n"
269
                                 "TTYPath=/dev/%s\n",
270
                                 tty, tty);
271
        if (r < 0)
9✔
272
                return log_warning_errno(r, "Failed to write drop-in for debug-shell.service: %m");
×
273

274
        return 1;
275
}
276

277
static int process_unit_credentials(const char *credentials_dir) {
×
278
        _cleanup_free_ DirectoryEntries *des = NULL;
×
279
        int r;
×
280

281
        assert(credentials_dir);
×
282

283
        r = readdir_all_at(AT_FDCWD, credentials_dir, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_MUST_BE_REGULAR, &des);
×
284
        if (r < 0)
×
285
                return log_error_errno(r, "Failed to enumerate credentials from credentials directory '%s': %m", credentials_dir);
×
286

287
        FOREACH_ARRAY(i, des->entries, des->n_entries) {
×
288
                struct dirent *de = *i;
×
289
                const char *unit, *dropin;
×
290

291
                unit = startswith(de->d_name, "systemd.extra-unit.");
×
292
                dropin = startswith(de->d_name, "systemd.unit-dropin.");
×
293

294
                if (!unit && !dropin)
×
295
                        continue;
×
296

297
                _cleanup_free_ char *d = NULL;
×
298

299
                r = read_credential_with_decryption(de->d_name, (void**) &d, NULL);
×
300
                if (r < 0) {
×
301
                        log_warning_errno(r, "Failed to read credential '%s', ignoring: %m", de->d_name);
×
302
                        continue;
×
303
                }
304

305
                if (unit) {
×
306
                        _cleanup_free_ char *p = NULL;
×
307

308
                        if (!unit_name_is_valid(unit, UNIT_NAME_ANY)) {
×
309
                                log_warning("Invalid unit name '%s' in credential '%s', ignoring.",
×
310
                                            unit, de->d_name);
311
                                continue;
×
312
                        }
313

314
                        p = path_join(arg_dest, unit);
×
315
                        if (!p)
×
316
                                return log_oom();
×
317

318
                        r = write_string_file(p, d, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_MKDIR_0755|WRITE_STRING_FILE_LABEL);
×
319
                        if (r < 0) {
×
320
                                log_warning_errno(r, "Failed to write unit file '%s' from credential '%s', ignoring: %m",
×
321
                                                  unit, de->d_name);
322
                                continue;
×
323
                        }
324

325
                        log_debug("Wrote unit file '%s' from credential '%s'", unit, de->d_name);
×
326

327
                } else if (dropin) {
×
328
                        _cleanup_free_ char *dropin_unit = NULL;
×
329
                        const char *tilde, *dropin_name;
×
330

331
                        tilde = strchrnul(dropin, '~');
×
332
                        dropin_unit = strndup(dropin, tilde - dropin);
×
333
                        if (!dropin_unit)
×
334
                                return log_oom();
×
335

336
                        if (!unit_name_is_valid(dropin_unit, UNIT_NAME_ANY)) {
×
337
                                log_warning("Invalid unit name '%s' in credential '%s', ignoring.",
×
338
                                            dropin_unit, de->d_name);
339
                                continue;
×
340
                        }
341

342
                        dropin_name = isempty(tilde) ? "50-credential" : tilde + 1;
×
343
                        if (isempty(dropin_name)) {
×
344
                                log_warning("Empty drop-in name for unit '%s' in credential '%s', ignoring.",
×
345
                                            dropin_unit, de->d_name);
346
                                continue;
×
347
                        }
348

349
                        r = write_drop_in(arg_dest, dropin_unit, /* level= */ UINT_MAX, dropin_name, d);
×
350
                        if (r < 0) {
×
351
                                log_warning_errno(r, "Failed to write drop-in '%s' for unit '%s' from credential '%s', ignoring: %m",
×
352
                                                  dropin_name, dropin_unit, de->d_name);
353
                                continue;
×
354
                        }
355

356
                        log_debug("Wrote drop-in '%s' for unit '%s' from credential '%s'", dropin_name, dropin_unit, de->d_name);
×
357
                } else
358
                        assert_not_reached();
×
359
        }
360

361
        return 0;
362
}
363

364
static int run(const char *dest, const char *dest_early, const char *dest_late) {
20✔
365
        const char *credentials_dir;
20✔
366
        int r;
20✔
367

368
        LIBCRYPTO_NOTE(suggested);
20✔
369
        TPM2_NOTE(suggested);
20✔
370

371
        assert_se(arg_dest = dest_early);
20✔
372

373
        r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_RD_STRICT | PROC_CMDLINE_STRIP_RD_PREFIX);
20✔
374
        if (r < 0)
20✔
375
                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
×
376

377
        if (arg_debug_shell) {
20✔
378
                if (strv_extend(&arg_wants, "debug-shell.service") < 0)
18✔
379
                        return log_oom();
×
380

381
                RET_GATHER(r, install_debug_shell_dropin());
18✔
382
        }
383

384
        BIT_FOREACH(i, arg_breakpoints)
49✔
385
                if (strv_extend(&arg_wants, breakpoint_info_table[i].unit) < 0)
29✔
386
                        return log_oom();
×
387

388
        if (get_credentials_dir(&credentials_dir) >= 0)
20✔
389
                RET_GATHER(r, process_unit_credentials(credentials_dir));
×
390

391
        if (get_encrypted_credentials_dir(&credentials_dir) >= 0)
20✔
392
                RET_GATHER(r, process_unit_credentials(credentials_dir));
×
393

394
        RET_GATHER(r, generate_mask_symlinks());
20✔
395
        RET_GATHER(r, generate_wants_symlinks());
20✔
396

397
        return r;
398
}
399

400
DEFINE_MAIN_GENERATOR_FUNCTION(run);
20✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc