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

systemd / systemd / 19020191358

02 Nov 2025 05:04PM UTC coverage: 72.222% (-0.02%) from 72.241%
19020191358

push

github

web-flow
Enhance docs for ukify and direct kernel boots (#39516)

305246 of 422650 relevant lines covered (72.22%)

1085243.28 hits per line

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

72.84
/src/systemctl/systemctl-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fnmatch.h>
4
#include <unistd.h>
5

6
#include "sd-bus.h"
7
#include "sd-daemon.h"
8

9
#include "ask-password-agent.h"
10
#include "bus-common-errors.h"
11
#include "bus-locator.h"
12
#include "bus-map-properties.h"
13
#include "bus-unit-util.h"
14
#include "bus-util.h"
15
#include "chase.h"
16
#include "dropin.h"
17
#include "env-util.h"
18
#include "exit-status.h"
19
#include "format-table.h"
20
#include "format-util.h"
21
#include "glob-util.h"
22
#include "install.h"
23
#include "output-mode.h"
24
#include "path-lookup.h"
25
#include "path-util.h"
26
#include "pidref.h"
27
#include "polkit-agent.h"
28
#include "process-util.h"
29
#include "reboot-util.h"
30
#include "runtime-scope.h"
31
#include "set.h"
32
#include "string-util.h"
33
#include "strv.h"
34
#include "systemctl.h"
35
#include "systemctl-util.h"
36
#include "unit-file.h"
37
#include "unit-name.h"
38
#include "verbs.h"
39

40
static sd_bus *buses[_BUS_FOCUS_MAX] = {};
41

42
int acquire_bus_full(BusFocus focus, bool graceful, sd_bus **ret) {
8,482✔
43
        int r;
8,482✔
44

45
        assert(focus < _BUS_FOCUS_MAX);
8,482✔
46
        assert(ret);
8,482✔
47

48
        if (!IN_SET(arg_runtime_scope, RUNTIME_SCOPE_SYSTEM, RUNTIME_SCOPE_USER))
8,482✔
49
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "--global is not supported for this operation.");
×
50

51
        /* We only go directly to the manager, if we are using a local transport */
52
        if (!IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_CAPSULE))
8,482✔
53
                focus = BUS_FULL;
22✔
54

55
        if (getenv_bool("SYSTEMCTL_FORCE_BUS") > 0)
8,482✔
56
                focus = BUS_FULL;
×
57

58
        if (!buses[focus]) {
8,482✔
59
                if (focus == BUS_MANAGER)
8,367✔
60
                        r = bus_connect_transport_systemd(arg_transport, arg_host, arg_runtime_scope, &buses[focus]);
8,257✔
61
                else
62
                        r = bus_connect_transport(arg_transport, arg_host, arg_runtime_scope, &buses[focus]);
110✔
63
                if (r < 0)
8,367✔
64
                        return bus_log_connect_full(graceful && focus == BUS_FULL && r == -ECONNREFUSED ? LOG_DEBUG : LOG_ERR,
×
65
                                                    r, arg_transport, arg_runtime_scope);
66

67
                (void) sd_bus_set_allow_interactive_authorization(buses[focus], arg_ask_password);
8,367✔
68
        }
69

70
        *ret = buses[focus];
8,482✔
71
        return 0;
8,482✔
72
}
73

74
void release_busses(void) {
8,425✔
75
        FOREACH_ARRAY(w, buses, _BUS_FOCUS_MAX)
25,275✔
76
                *w = sd_bus_flush_close_unref(*w);
16,850✔
77
}
8,425✔
78

79
void ask_password_agent_open_maybe(void) {
2,621✔
80
        /* Open the password agent as a child process if necessary */
81

82
        if (arg_dry_run)
2,621✔
83
                return;
84

85
        if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
2,617✔
86
                return;
87

88
        ask_password_agent_open_if_enabled(arg_transport, arg_ask_password);
2,607✔
89
}
90

91
void polkit_agent_open_maybe(void) {
3,496✔
92
        /* Open the polkit agent as a child process if necessary */
93

94
        if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
3,496✔
95
                return;
96

97
        (void) polkit_agent_open_if_enabled(arg_transport, arg_ask_password);
3,298✔
98
}
99

100
int translate_bus_error_to_exit_status(int r, const sd_bus_error *error) {
68✔
101
        assert(error);
68✔
102

103
        if (!sd_bus_error_is_set(error))
68✔
104
                return r;
105

106
        if (sd_bus_error_has_names(error, SD_BUS_ERROR_ACCESS_DENIED,
68✔
107
                                          BUS_ERROR_ONLY_BY_DEPENDENCY,
108
                                          BUS_ERROR_NO_ISOLATION,
109
                                          BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE))
110
                return EXIT_NOPERMISSION;
111

112
        if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT))
67✔
113
                return EXIT_NOTINSTALLED;
114

115
        if (sd_bus_error_has_names(error, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE,
30✔
116
                                          SD_BUS_ERROR_NOT_SUPPORTED))
117
                return EXIT_NOTIMPLEMENTED;
118

119
        if (sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED))
30✔
120
                return EXIT_NOTCONFIGURED;
121

122
        if (r != 0)
30✔
123
                return r;
30✔
124

125
        return EXIT_FAILURE;
126
}
127

128
int get_state_one_unit(sd_bus *bus, const char *unit, UnitActiveState *ret_active_state) {
4,032✔
129
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
130
        _cleanup_free_ char *buf = NULL, *dbus_path = NULL;
4,032✔
131
        UnitActiveState state;
4,032✔
132
        int r;
4,032✔
133

134
        assert(bus);
4,032✔
135
        assert(unit);
4,032✔
136
        assert(ret_active_state);
4,032✔
137

138
        dbus_path = unit_dbus_path_from_name(unit);
4,032✔
139
        if (!dbus_path)
4,032✔
140
                return log_oom();
×
141

142
        r = sd_bus_get_property_string(
4,032✔
143
                        bus,
144
                        "org.freedesktop.systemd1",
145
                        dbus_path,
146
                        "org.freedesktop.systemd1.Unit",
147
                        "ActiveState",
148
                        &error,
149
                        &buf);
150
        if (r < 0)
4,032✔
151
                return log_error_errno(r, "Failed to retrieve unit state: %s", bus_error_message(&error, r));
×
152

153
        state = unit_active_state_from_string(buf);
4,032✔
154
        if (state < 0)
4,032✔
155
                return log_error_errno(state, "Invalid unit state '%s' for: %s", buf, unit);
×
156

157
        *ret_active_state = state;
4,032✔
158
        return 0;
4,032✔
159
}
160

161
int get_sub_state_one_unit(sd_bus *bus, const char *unit, char **ret_sub_state) {
171✔
162
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
163
        _cleanup_free_ char *sub_state = NULL, *dbus_path = NULL;
171✔
164
        int r;
171✔
165

166
        assert(bus);
171✔
167
        assert(unit);
171✔
168
        assert(ret_sub_state);
171✔
169

170
        dbus_path = unit_dbus_path_from_name(unit);
171✔
171
        if (!dbus_path)
171✔
172
                return log_oom();
×
173

174
        r = sd_bus_get_property_string(
171✔
175
                        bus,
176
                        "org.freedesktop.systemd1",
177
                        dbus_path,
178
                        "org.freedesktop.systemd1.Unit",
179
                        "SubState",
180
                        &error,
181
                        &sub_state);
182
        if (r < 0)
171✔
183
                return log_error_errno(r, "Failed to retrieve unit sub state: %s", bus_error_message(&error, r));
×
184

185
        *ret_sub_state = TAKE_PTR(sub_state);
171✔
186
        return 0;
171✔
187
}
188

189
int get_unit_list(
106✔
190
                sd_bus *bus,
191
                const char *machine,
192
                char **patterns,
193
                UnitInfo **unit_infos,
194
                int c,
195
                sd_bus_message **ret_reply) {
196

197
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
106✔
198
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
199
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
106✔
200
        int r;
106✔
201
        bool fallback = false;
106✔
202

203
        assert(bus);
106✔
204
        assert(unit_infos);
106✔
205
        assert(ret_reply);
106✔
206

207
        r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsByPatterns");
106✔
208
        if (r < 0)
106✔
209
                return bus_log_create_error(r);
×
210

211
        r = sd_bus_message_append_strv(m, arg_states);
106✔
212
        if (r < 0)
106✔
213
                return bus_log_create_error(r);
×
214

215
        r = sd_bus_message_append_strv(m, patterns);
106✔
216
        if (r < 0)
106✔
217
                return bus_log_create_error(r);
×
218

219
        r = sd_bus_call(bus, m, 0, &error, &reply);
106✔
220
        if (r < 0 && (sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_METHOD,
106✔
221
                                                     SD_BUS_ERROR_ACCESS_DENIED))) {
222
                /* Fallback to legacy ListUnitsFiltered method */
223
                fallback = true;
×
224
                log_debug_errno(r, "Failed to list units: %s Falling back to ListUnitsFiltered method.", bus_error_message(&error, r));
×
225
                m = sd_bus_message_unref(m);
×
226
                sd_bus_error_free(&error);
×
227

228
                r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitsFiltered");
×
229
                if (r < 0)
×
230
                        return bus_log_create_error(r);
×
231

232
                r = sd_bus_message_append_strv(m, arg_states);
×
233
                if (r < 0)
×
234
                        return bus_log_create_error(r);
×
235

236
                r = sd_bus_call(bus, m, 0, &error, &reply);
×
237
        }
238
        if (r < 0)
×
239
                return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
×
240

241
        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
106✔
242
        if (r < 0)
106✔
243
                return bus_log_parse_error(r);
×
244

245
        for (;;) {
10,238✔
246
                UnitInfo u;
10,238✔
247

248
                r = bus_parse_unit_info(reply, &u);
10,238✔
249
                if (r < 0)
10,238✔
250
                        return bus_log_parse_error(r);
×
251
                if (r == 0)
10,238✔
252
                        break;
253

254
                u.machine = machine;
10,132✔
255

256
                if (!output_show_unit(&u, fallback ? patterns : NULL))
20,264✔
257
                        continue;
1,560✔
258

259
                if (!GREEDY_REALLOC(*unit_infos, c+1))
8,572✔
260
                        return log_oom();
×
261

262
                (*unit_infos)[c++] = u;
8,572✔
263
        }
264

265
        r = sd_bus_message_exit_container(reply);
106✔
266
        if (r < 0)
106✔
267
                return bus_log_parse_error(r);
×
268

269
        *ret_reply = TAKE_PTR(reply);
106✔
270
        return c;
106✔
271
}
272

273
int expand_unit_names(
7,054✔
274
                sd_bus *bus,
275
                char * const *names,
276
                const char *suffix,
277
                char ***ret,
278
                bool *ret_expanded) {
279

280
        _cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
7,054✔
281
        int r;
7,054✔
282

283
        assert(bus);
7,054✔
284
        assert(ret);
7,054✔
285

286
        STRV_FOREACH(name, names) {
14,198✔
287
                UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN);
7,144✔
288
                char *t;
7,144✔
289

290
                r = unit_name_mangle_with_suffix(*name, NULL, options, suffix ?: ".service", &t);
14,286✔
291
                if (r < 0)
7,144✔
292
                        return log_error_errno(r, "Failed to mangle name: %m");
×
293

294
                if (string_is_glob(t))
7,144✔
295
                        r = strv_consume(&globs, t);
29✔
296
                else
297
                        r = strv_consume(&mangled, t);
7,115✔
298
                if (r < 0)
7,144✔
299
                        return log_oom();
×
300
        }
301

302
        /* Query the manager only if any of the names are a glob, since this is fairly expensive */
303
        bool expanded = !strv_isempty(globs);
7,054✔
304
        if (expanded) {
29✔
305
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
29✔
306
                _cleanup_free_ UnitInfo *unit_infos = NULL;
29✔
307

308
                r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
29✔
309
                if (r < 0)
29✔
310
                        return r;
311

312
                FOREACH_ARRAY(info, unit_infos, r)
144✔
313
                        if (strv_extend(&mangled, info->id) < 0)
115✔
314
                                return log_oom();
×
315
        }
316

317
        *ret = TAKE_PTR(mangled);
7,054✔
318
        if (ret_expanded)
7,054✔
319
                *ret_expanded = expanded;
2,615✔
320

321
        return 0;
322
}
323

324
int get_active_triggering_units(sd_bus *bus, const char *unit, bool ignore_masked, char ***ret) {
1,546✔
325
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1,546✔
326
        _cleanup_strv_free_ char **triggered_by = NULL, **active = NULL;
1,546✔
327
        _cleanup_free_ char *name = NULL, *dbus_path = NULL;
1,546✔
328
        int r;
1,546✔
329

330
        assert(bus);
1,546✔
331
        assert(unit);
1,546✔
332
        assert(ret);
1,546✔
333

334
        r = unit_name_mangle(unit, 0, &name);
1,546✔
335
        if (r < 0)
1,546✔
336
                return r;
337

338
        if (unit_name_is_valid(name, UNIT_NAME_TEMPLATE))
1,546✔
339
                goto skip;
1✔
340

341
        if (ignore_masked) {
1,545✔
342
                r = unit_is_masked(bus, name);
1,534✔
343
                if (r < 0)
1,534✔
344
                        return r;
345
                if (r > 0)
1,534✔
346
                        goto skip;
6✔
347
        }
348

349
        dbus_path = unit_dbus_path_from_name(name);
1,539✔
350
        if (!dbus_path)
1,539✔
351
                return -ENOMEM;
352

353
        r = sd_bus_get_property_strv(
1,539✔
354
                        bus,
355
                        "org.freedesktop.systemd1",
356
                        dbus_path,
357
                        "org.freedesktop.systemd1.Unit",
358
                        "TriggeredBy",
359
                        &error,
360
                        &triggered_by);
361
        if (r < 0)
1,539✔
362
                return log_debug_errno(r, "Failed to get TriggeredBy property of unit '%s': %s",
×
363
                                       name, bus_error_message(&error, r));
364

365
        STRV_FOREACH(i, triggered_by) {
2,314✔
366
                UnitActiveState active_state;
775✔
367

368
                r = get_state_one_unit(bus, *i, &active_state);
775✔
369
                if (r < 0)
775✔
370
                        return r;
×
371

372
                if (!IN_SET(active_state, UNIT_ACTIVE, UNIT_RELOADING, UNIT_REFRESHING))
775✔
373
                        continue;
752✔
374

375
                r = strv_extend(&active, *i);
23✔
376
                if (r < 0)
23✔
377
                        return r;
378
        }
379

380
        *ret = TAKE_PTR(active);
1,539✔
381
        return 0;
1,539✔
382

383
skip:
7✔
384
        *ret = NULL;
7✔
385
        return 0;
7✔
386
}
387

388
void warn_triggering_units(sd_bus *bus, const char *unit, const char *operation, bool ignore_masked) {
1,546✔
389
        _cleanup_strv_free_ char **triggered_by = NULL;
×
390
        _cleanup_free_ char *joined = NULL;
1,546✔
391
        int r;
1,546✔
392

393
        assert(bus);
1,546✔
394
        assert(unit);
1,546✔
395
        assert(operation);
1,546✔
396

397
        r = get_active_triggering_units(bus, unit, ignore_masked, &triggered_by);
1,546✔
398
        if (r < 0) {
1,546✔
399
                if (r != -ENOENT) /* A linked unit might have disappeared after disabling */
×
400
                        log_warning_errno(r, "Failed to get triggering units for '%s', ignoring: %m", unit);
×
401
                return;
×
402
        }
403

404
        if (strv_isempty(triggered_by))
1,546✔
405
                return;
406

407
        joined = strv_join(triggered_by, ", ");
18✔
408
        if (!joined)
18✔
409
                return (void) log_oom();
×
410

411
        log_warning("%s '%s', but its triggering units are still active:\n"
18✔
412
                    "%s",
413
                    operation, unit, joined);
414
}
415

416
int need_daemon_reload(sd_bus *bus, const char *unit) {
2,729✔
417
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
2,729✔
418
        const char *path;
2,729✔
419
        int b, r;
2,729✔
420

421
        /* We ignore all errors here, since this is used to show a
422
         * warning only */
423

424
        /* We don't use unit_dbus_path_from_name() directly since we
425
         * don't want to load the unit if it isn't loaded. */
426

427
        r = bus_call_method(bus, bus_systemd_mgr, "GetUnit", NULL, &reply, "s", unit);
2,729✔
428
        if (r < 0)
2,729✔
429
                return r;
430

431
        r = sd_bus_message_read(reply, "o", &path);
2,582✔
432
        if (r < 0)
2,582✔
433
                return r;
434

435
        r = sd_bus_get_property_trivial(
2,582✔
436
                        bus,
437
                        "org.freedesktop.systemd1",
438
                        path,
439
                        "org.freedesktop.systemd1.Unit",
440
                        "NeedDaemonReload",
441
                        NULL,
442
                        'b', &b);
443
        if (r < 0)
2,582✔
444
                return r;
445

446
        return b;
2,582✔
447
}
448

449
void warn_unit_file_changed(const char *unit) {
2✔
450
        assert(unit);
2✔
451

452
        if (arg_no_warn)
2✔
453
                return;
454

455
        log_warning("Warning: The unit file, source configuration file or drop-ins of %s changed on disk. Run 'systemctl%s daemon-reload' to reload units.",
2✔
456
                    unit,
457
                    arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? "" : " --user");
458
}
459

460
int unit_file_find_path(LookupPaths *lp, const char *unit_name, char **ret_unit_path) {
×
461
        assert(lp);
×
462
        assert(unit_name);
×
463

464
        STRV_FOREACH(p, lp->search_path) {
×
465
                _cleanup_free_ char *path = NULL, *lpath = NULL;
×
466
                int r;
×
467

468
                path = path_join(*p, unit_name);
×
469
                if (!path)
×
470
                        return log_oom();
×
471

472
                r = chase(path, arg_root, 0, &lpath, NULL);
×
473
                if (r == -ENOENT)
×
474
                        continue;
×
475
                if (r == -ENOMEM)
×
476
                        return log_oom();
×
477
                if (r < 0)
×
478
                        return log_error_errno(r, "Failed to access path \"%s\": %m", path);
×
479

480
                if (ret_unit_path)
×
481
                        *ret_unit_path = TAKE_PTR(lpath);
×
482

483
                return 1;
484
        }
485

486
        if (ret_unit_path)
×
487
                *ret_unit_path = NULL;
×
488

489
        return 0;
490
}
491

492
int unit_find_paths(
136✔
493
                sd_bus *bus,
494
                const char *unit_name,
495
                LookupPaths *lp,
496
                bool force_client_side,
497
                Hashmap **cached_id_map,
498
                Hashmap **cached_name_map,
499
                char **ret_fragment_path,
500
                char ***ret_dropin_paths) {
501

502
        _cleanup_strv_free_ char **dropins = NULL;
×
503
        _cleanup_free_ char *path = NULL;
136✔
504
        int r;
136✔
505

506
        /**
507
         * Finds where the unit is defined on disk. Returns 0 if the unit is not found. Returns 1 if it is
508
         * found, and sets:
509
         *
510
         * - the path to the unit in *ret_frament_path, if it exists on disk,
511
         *
512
         * - and a strv of existing drop-ins in *ret_dropin_paths, if the arg is not NULL and any dropins
513
         *   were found.
514
         *
515
         * Returns -ERFKILL if the unit is masked, and -EKEYREJECTED if the unit file could not be loaded for
516
         * some reason (the latter only applies if we are going through the service manager). As special
517
         * exception it won't log for these two error cases.
518
         */
519

520
        assert(unit_name);
136✔
521
        assert(ret_fragment_path);
136✔
522
        assert(lp);
136✔
523

524
        /* Go via the bus to acquire the path, unless we are explicitly told not to, or when the unit name is a template */
525
        if (!force_client_side &&
272✔
526
            install_client_side() == INSTALL_CLIENT_SIDE_NO &&
272✔
527
            !unit_name_is_valid(unit_name, UNIT_NAME_TEMPLATE)) {
261✔
528
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
529
                _cleanup_free_ char *load_state = NULL, *dbus_path = NULL;
131✔
530

531
                dbus_path = unit_dbus_path_from_name(unit_name);
131✔
532
                if (!dbus_path)
131✔
533
                        return log_oom();
×
534

535
                r = sd_bus_get_property_string(
131✔
536
                                bus,
537
                                "org.freedesktop.systemd1",
538
                                dbus_path,
539
                                "org.freedesktop.systemd1.Unit",
540
                                "LoadState",
541
                                &error,
542
                                &load_state);
543
                if (r < 0)
131✔
544
                        return log_error_errno(r, "Failed to get LoadState: %s", bus_error_message(&error, r));
×
545

546
                if (streq(load_state, "masked"))
131✔
547
                        return -ERFKILL; /* special case: no logging */
548
                if (streq(load_state, "not-found")) {
131✔
549
                        r = 0;
6✔
550
                        goto finish;
6✔
551
                }
552
                if (!STR_IN_SET(load_state, "loaded", "bad-setting"))
125✔
553
                        return -EKEYREJECTED; /* special case: no logging */
×
554

555
                r = sd_bus_get_property_string(
125✔
556
                                bus,
557
                                "org.freedesktop.systemd1",
558
                                dbus_path,
559
                                "org.freedesktop.systemd1.Unit",
560
                                "FragmentPath",
561
                                &error,
562
                                &path);
563
                if (r < 0)
125✔
564
                        return log_error_errno(r, "Failed to get FragmentPath: %s", bus_error_message(&error, r));
×
565

566
                if (ret_dropin_paths) {
125✔
567
                        r = sd_bus_get_property_strv(
125✔
568
                                        bus,
569
                                        "org.freedesktop.systemd1",
570
                                        dbus_path,
571
                                        "org.freedesktop.systemd1.Unit",
572
                                        "DropInPaths",
573
                                        &error,
574
                                        &dropins);
575
                        if (r < 0)
125✔
576
                                return log_error_errno(r, "Failed to get DropInPaths: %s", bus_error_message(&error, r));
×
577
                }
578
        } else {
579
                if (!*cached_name_map) {
5✔
580
                        r = unit_file_build_name_map(lp, NULL, cached_id_map, cached_name_map, NULL);
5✔
581
                        if (r < 0)
5✔
582
                                return r;
×
583
                }
584

585
                const char *_path;
5✔
586
                _cleanup_set_free_ Set *names = NULL;
5✔
587
                r = unit_file_find_fragment(*cached_id_map, *cached_name_map, unit_name, &_path, &names);
5✔
588
                if (r < 0)
5✔
589
                        return log_error_errno(r, "Failed to find fragment for '%s': %m", unit_name);
×
590

591
                if (_path) {
5✔
592
                        path = strdup(_path);
2✔
593
                        if (!path)
2✔
594
                                return log_oom();
×
595
                }
596

597
                if (ret_dropin_paths) {
5✔
598
                        r = unit_file_find_dropin_paths(arg_root, lp->search_path, NULL,
5✔
599
                                                        ".d", ".conf",
600
                                                        NULL, names, &dropins);
601
                        if (r < 0)
5✔
602
                                return r;
603
                }
604
        }
605

606
 finish:
136✔
607
        if (isempty(path)) {
136✔
608
                *ret_fragment_path = NULL;
10✔
609
                r = 0;
10✔
610
        } else {
611
                *ret_fragment_path = TAKE_PTR(path);
126✔
612
                r = 1;
126✔
613
        }
614

615
        if (ret_dropin_paths) {
136✔
616
                if (!strv_isempty(dropins)) {
136✔
617
                        *ret_dropin_paths = TAKE_PTR(dropins);
120✔
618
                        r = 1;
120✔
619
                } else
620
                        *ret_dropin_paths = NULL;
16✔
621
        }
622

623
        if (r == 0 && !arg_force)
136✔
624
                log_error("No files found for %s.", unit_name);
5✔
625

626
        return r;
627
}
628

629
static int unit_find_template_path(
×
630
                const char *unit_name,
631
                LookupPaths *lp,
632
                char **ret_fragment_path,
633
                char **ret_template) {
634

635
        _cleanup_free_ char *t = NULL, *f = NULL;
×
636
        int r;
×
637

638
        /* Returns 1 if a fragment was found, 0 if not found, negative on error. */
639

640
        r = unit_file_find_path(lp, unit_name, &f);
×
641
        if (r < 0)
×
642
                return r;
643
        if (r > 0) {
×
644
                if (ret_fragment_path)
×
645
                        *ret_fragment_path = TAKE_PTR(f);
×
646
                if (ret_template)
×
647
                        *ret_template = NULL;
×
648
                return r; /* found a real unit */
×
649
        }
650

651
        r = unit_name_template(unit_name, &t);
×
652
        if (r == -EINVAL) {
×
653
                if (ret_fragment_path)
×
654
                        *ret_fragment_path = NULL;
×
655
                if (ret_template)
×
656
                        *ret_template = NULL;
×
657

658
                return 0; /* not a template, does not exist */
×
659
        }
660
        if (r < 0)
×
661
                return log_error_errno(r, "Failed to determine template name: %m");
×
662

663
        r = unit_file_find_path(lp, t, ret_fragment_path);
×
664
        if (r < 0)
×
665
                return r;
666

667
        if (ret_template)
×
668
                *ret_template = r > 0 ? TAKE_PTR(t) : NULL;
×
669

670
        return r;
671
}
672

673
int unit_is_masked(sd_bus *bus, const char *unit) {
1,549✔
674
        _cleanup_free_ char *load_state = NULL;
1,549✔
675
        int r;
1,549✔
676

677
        assert(bus);
1,549✔
678
        assert(unit);
1,549✔
679

680
        if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE)) {
1,549✔
681
                _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
682
                _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
3✔
683
                const char *state;
3✔
684

685
                r = bus_call_method(bus, bus_systemd_mgr, "GetUnitFileState", &error, &reply, "s", unit);
3✔
686
                if (r < 0)
3✔
687
                        return log_debug_errno(r, "Failed to get UnitFileState for '%s': %s",
1✔
688
                                               unit, bus_error_message(&error, r));
689

690
                r = sd_bus_message_read(reply, "s", &state);
2✔
691
                if (r < 0)
2✔
692
                        return bus_log_parse_error_debug(r);
×
693

694
                return STR_IN_SET(state, "masked", "masked-runtime");
2✔
695
        }
696

697
        r = unit_load_state(bus, unit, &load_state);
1,546✔
698
        if (r < 0)
1,546✔
699
                return r;
700

701
        return streq(load_state, "masked");
1,546✔
702
}
703

704
int unit_exists(LookupPaths *lp, const char *unit) {
26✔
705
        typedef struct UnitStateInfo {
26✔
706
                const char *load_state;
707
                const char *active_state;
708
        } UnitStateInfo;
709

710
        static const struct bus_properties_map property_map[] = {
26✔
711
                { "LoadState",   "s", NULL, offsetof(UnitStateInfo, load_state)   },
712
                { "ActiveState", "s", NULL, offsetof(UnitStateInfo, active_state) },
713
                {},
714
        };
715

716
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
717
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
26✔
718
        _cleanup_free_ char *path = NULL;
26✔
719
        UnitStateInfo info = {};
26✔
720
        sd_bus *bus;
26✔
721
        int r;
26✔
722

723
        if (unit_name_is_valid(unit, UNIT_NAME_TEMPLATE))
26✔
724
                return unit_find_template_path(unit, lp, NULL, NULL);
×
725

726
        path = unit_dbus_path_from_name(unit);
26✔
727
        if (!path)
26✔
728
                return log_oom();
×
729

730
        r = acquire_bus(BUS_MANAGER, &bus);
26✔
731
        if (r < 0)
26✔
732
                return r;
733

734
        r = bus_map_all_properties(bus, "org.freedesktop.systemd1", path, property_map, 0, &error, &m, &info);
26✔
735
        if (r < 0)
26✔
736
                return log_error_errno(r, "Failed to get properties: %s", bus_error_message(&error, r));
×
737

738
        return !streq_ptr(info.load_state, "not-found") || !streq_ptr(info.active_state, "inactive");
26✔
739
}
740

741
int append_unit_dependencies(sd_bus *bus, char **names, char ***ret) {
3✔
742
        _cleanup_strv_free_ char **with_deps = NULL;
3✔
743

744
        assert(bus);
3✔
745
        assert(ret);
3✔
746

747
        STRV_FOREACH(name, names) {
6✔
748
                char **deps;
3✔
749

750
                if (strv_extend(&with_deps, *name) < 0)
3✔
751
                        return log_oom();
×
752

753
                (void) unit_get_dependencies(bus, *name, &deps);
3✔
754

755
                if (strv_extend_strv_consume(&with_deps, deps, /* filter_duplicates = */ true) < 0)
3✔
756
                        return log_oom();
×
757
        }
758

759
        *ret = TAKE_PTR(with_deps);
3✔
760

761
        return 0;
3✔
762
}
763

764
int maybe_extend_with_unit_dependencies(sd_bus *bus, char ***list) {
1,622✔
765
        _cleanup_strv_free_ char **list_with_deps = NULL;
1,622✔
766
        int r;
1,622✔
767

768
        assert(bus);
1,622✔
769
        assert(list);
1,622✔
770

771
        if (!arg_with_dependencies)
1,622✔
772
                return 0;
773

774
        r = append_unit_dependencies(bus, *list, &list_with_deps);
×
775
        if (r < 0)
×
776
                return log_error_errno(r, "Failed to append unit dependencies: %m");
×
777

778
        return strv_free_and_replace(*list, list_with_deps);
×
779
}
780

781
int unit_get_dependencies(sd_bus *bus, const char *name, char ***ret) {
70✔
782
        _cleanup_strv_free_ char **deps = NULL;
70✔
783

784
        static const struct bus_properties_map map[_DEPENDENCY_MAX][7] = {
70✔
785
                [DEPENDENCY_FORWARD] = {
786
                        { "Requires",    "as", NULL, 0 },
787
                        { "Requisite",   "as", NULL, 0 },
788
                        { "Wants",       "as", NULL, 0 },
789
                        { "ConsistsOf",  "as", NULL, 0 },
790
                        { "BindsTo",     "as", NULL, 0 },
791
                        { "Upholds",     "as", NULL, 0 },
792
                        {}
793
                },
794
                [DEPENDENCY_REVERSE] = {
795
                        { "RequiredBy",  "as", NULL, 0 },
796
                        { "RequisiteOf", "as", NULL, 0 },
797
                        { "WantedBy",    "as", NULL, 0 },
798
                        { "PartOf",      "as", NULL, 0 },
799
                        { "BoundBy",     "as", NULL, 0 },
800
                        { "UpheldBy",    "as", NULL, 0 },
801
                        {}
802
                },
803
                [DEPENDENCY_AFTER] = {
804
                        { "After",       "as", NULL, 0 },
805
                        {}
806
                },
807
                [DEPENDENCY_BEFORE] = {
808
                        { "Before",      "as", NULL, 0 },
809
                        {}
810
                },
811
        };
812

813
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
814
        _cleanup_free_ char *dbus_path = NULL;
70✔
815
        int r;
70✔
816

817
        assert(bus);
70✔
818
        assert(name);
70✔
819
        assert(ret);
70✔
820

821
        dbus_path = unit_dbus_path_from_name(name);
70✔
822
        if (!dbus_path)
70✔
823
                return log_oom();
×
824

825
        r = bus_map_all_properties(bus,
140✔
826
                                   "org.freedesktop.systemd1",
827
                                   dbus_path,
828
                                   map[arg_dependency],
70✔
829
                                   BUS_MAP_STRDUP,
830
                                   &error,
831
                                   NULL,
832
                                   &deps);
833
        if (r < 0)
70✔
834
                return log_error_errno(r, "Failed to get properties of %s: %s", name, bus_error_message(&error, r));
×
835

836
        strv_uniq(deps); /* Sometimes a unit might have multiple deps on the other unit,
70✔
837
                          * but we still want to show it just once. */
838
        *ret = TAKE_PTR(deps);
70✔
839

840
        return 0;
70✔
841
}
842

843
const char* unit_type_suffix(const char *unit) {
24,266✔
844
        const char *dot;
24,266✔
845

846
        dot = strrchr(unit, '.');
24,266✔
847
        if (!dot)
24,266✔
848
                return "";
849

850
        return dot + 1;
24,266✔
851
}
852

853
bool output_show_unit(const UnitInfo *u, char **patterns) {
10,132✔
854
        assert(u);
10,132✔
855

856
        if (!strv_fnmatch_or_empty(patterns, u->id, FNM_NOESCAPE))
10,132✔
857
                return false;
858

859
        if (arg_types && !strv_contains(arg_types, unit_type_suffix(u->id)))
10,132✔
860
                return false;
861

862
        if (arg_all)
9,272✔
863
                return true;
864

865
        /* Note that '--all' is not purely a state filter, but also a filter that hides units that "follow"
866
         * other units (which is used for device units that appear under different names). */
867
        if (!isempty(u->following))
1,890✔
868
                return false;
869

870
        if (!strv_isempty(arg_states))
1,890✔
871
                return true;
872

873
        /* By default show all units except the ones in inactive state and with no pending job */
874
        if (u->job_id > 0)
1,477✔
875
                return true;
876

877
        if (streq(u->active_state, "inactive"))
1,464✔
878
                return false;
700✔
879

880
        return true;
881
}
882

883
InstallClientSide install_client_side(void) {
723✔
884
        /* Decides whether to execute enable/disable/… client-side offline operation rather than
885
         * server-side. */
886

887
        /* Unsupported environment variable, mostly for debugging purposes */
888
        if (getenv_bool("SYSTEMCTL_INSTALL_CLIENT_SIDE") > 0)
723✔
889
                return INSTALL_CLIENT_SIDE_OVERRIDE;
890

891
        if (!isempty(arg_root))
723✔
892
                return INSTALL_CLIENT_SIDE_ARG_ROOT;
893

894
        if (running_in_chroot_or_offline())
680✔
895
                return INSTALL_CLIENT_SIDE_OFFLINE;
896

897
        if (sd_booted() <= 0)
680✔
898
                return INSTALL_CLIENT_SIDE_NOT_BOOTED;
899

900
        if (arg_runtime_scope == RUNTIME_SCOPE_GLOBAL)
680✔
901
                return INSTALL_CLIENT_SIDE_GLOBAL_SCOPE;
×
902

903
        return INSTALL_CLIENT_SIDE_NO;
904
}
905

906
int output_table(Table *table) {
105✔
907
        int r;
105✔
908

909
        assert(table);
105✔
910

911
        if (OUTPUT_MODE_IS_JSON(arg_output))
105✔
912
                r = table_print_json(table, NULL, output_mode_to_json_format_flags(arg_output) | SD_JSON_FORMAT_COLOR_AUTO);
×
913
        else
914
                r = table_print(table, NULL);
105✔
915
        if (r < 0)
105✔
916
                return table_log_print_error(r);
2✔
917

918
        return 0;
919
}
920

921
bool show_preset_for_state(UnitFileState state) {
1,456✔
922
        /* Don't show preset state in those unit file states, it'll only confuse users. */
923
        return !IN_SET(state,
1,456✔
924
                       UNIT_FILE_ALIAS,
925
                       UNIT_FILE_STATIC,
926
                       UNIT_FILE_GENERATED,
927
                       UNIT_FILE_TRANSIENT);
928
}
929

930
UnitFileFlags unit_file_flags_from_args(void) {
17✔
931
        return (arg_runtime ? UNIT_FILE_RUNTIME : 0) |
34✔
932
               (arg_force   ? UNIT_FILE_FORCE   : 0);
17✔
933
}
934

935
int mangle_names(const char *operation, char * const *original_names, char ***ret) {
517✔
936
        _cleanup_strv_free_ char **l = NULL;
517✔
937
        int r;
517✔
938

939
        assert(operation);
517✔
940
        assert(ret);
517✔
941

942
        STRV_FOREACH(name, original_names) {
1,041✔
943
                char *mangled;
524✔
944

945
                if (is_path(*name))
524✔
946
                        /* When enabling units qualified path names are OK, too, hence allow them explicitly. */
947
                        r = path_make_absolute_cwd(*name, &mangled);
×
948
                else
949
                        r = unit_name_mangle_with_suffix(*name, operation,
524✔
950
                                                         arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
524✔
951
                                                         ".service", &mangled);
952
                if (r < 0)
524✔
953
                        return log_error_errno(r, "Failed to mangle unit name or path '%s': %m", *name);
×
954

955
                if (strv_consume(&l, mangled) < 0)
524✔
956
                        return log_oom();
×
957
        }
958

959
        *ret = TAKE_PTR(l);
517✔
960

961
        return 0;
517✔
962
}
963

964
int halt_now(enum action a) {
3✔
965
        /* The kernel will automatically flush ATA disks and suchlike on reboot(), but the file systems need
966
         * to be synced explicitly in advance. */
967
        if (!arg_no_sync && !arg_dry_run)
3✔
968
                sync();
×
969

970
        /* Make sure C-A-D is handled by the kernel from this point on... */
971
        if (!arg_dry_run)
3✔
972
                (void) reboot(RB_ENABLE_CAD);
×
973

974
        switch (a) {
3✔
975

976
        case ACTION_HALT:
1✔
977
                if (!arg_quiet)
1✔
978
                        log_info("Halting.");
1✔
979
                if (arg_dry_run)
1✔
980
                        return 0;
981
                (void) reboot(RB_HALT_SYSTEM);
×
982
                return -errno;
×
983

984
        case ACTION_POWEROFF:
1✔
985
                if (!arg_quiet)
1✔
986
                        log_info("Powering off.");
1✔
987
                if (arg_dry_run)
1✔
988
                        return 0;
989
                (void) reboot(RB_POWER_OFF);
×
990
                return -errno;
×
991

992
        case ACTION_KEXEC:
1✔
993
        case ACTION_REBOOT:
994
                return reboot_with_parameter(REBOOT_FALLBACK |
1✔
995
                                             (arg_quiet ? 0 : REBOOT_LOG) |
1✔
996
                                             (arg_dry_run ? REBOOT_DRY_RUN : 0));
1✔
997

998
        default:
×
999
                assert_not_reached();
×
1000
        }
1001
}
1002

1003
int get_unit_by_pid(sd_bus *bus, pid_t pid, char **ret_unit, char **ret_path) {
1✔
1004
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1005
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1✔
1006
        int r;
1✔
1007

1008
        assert(bus);
1✔
1009
        assert(pid >= 0); /* 0 is accepted by GetUnitByPID for querying our own process. */
1✔
1010

1011
        r = bus_call_method(bus, bus_systemd_mgr, "GetUnitByPID", &error, &reply, "u", (uint32_t) pid);
1✔
1012
        if (r < 0) {
1✔
1013
                if (sd_bus_error_has_name(&error, BUS_ERROR_NO_UNIT_FOR_PID))
×
1014
                        return log_error_errno(r, "%s", bus_error_message(&error, r));
×
1015

1016
                return log_error_errno(r,
×
1017
                                       "Failed to get unit that PID " PID_FMT " belongs to: %s",
1018
                                       pid > 0 ? pid : getpid_cached(),
1019
                                       bus_error_message(&error, r));
1020
        }
1021

1022
        _cleanup_free_ char *u = NULL, *p = NULL;
1✔
1023
        const char *path;
1✔
1024

1025
        r = sd_bus_message_read_basic(reply, 'o', &path);
1✔
1026
        if (r < 0)
1✔
1027
                return bus_log_parse_error(r);
×
1028

1029
        if (ret_unit) {
1✔
1030
                r = unit_name_from_dbus_path(path, &u);
1✔
1031
                if (r < 0)
1✔
1032
                        return log_error_errno(r,
×
1033
                                               "Failed to extract unit name from D-Bus object path '%s': %m",
1034
                                               path);
1035
        }
1036

1037
        if (ret_path) {
1✔
1038
                p = strdup(path);
×
1039
                if (!p)
×
1040
                        return log_oom();
×
1041
        }
1042

1043
        if (ret_unit)
1✔
1044
                *ret_unit = TAKE_PTR(u);
1✔
1045
        if (ret_path)
1✔
1046
                *ret_path = TAKE_PTR(p);
×
1047

1048
        return 0;
1049
}
1050

1051
static int get_unit_by_pidfd(sd_bus *bus, const PidRef *pid, char **ret_unit, char **ret_path) {
5✔
1052
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1053
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
5✔
1054
        int r;
5✔
1055

1056
        assert(bus);
5✔
1057
        assert(pidref_is_set(pid));
5✔
1058

1059
        if (pid->fd < 0)
5✔
1060
                return -EOPNOTSUPP;
1061

1062
        r = bus_call_method(bus, bus_systemd_mgr, "GetUnitByPIDFD", &error, &reply, "h", pid->fd);
5✔
1063
        if (r < 0) {
5✔
1064
                if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD))
×
1065
                        return -EOPNOTSUPP;
1066

1067
                if (sd_bus_error_has_names(&error, BUS_ERROR_NO_UNIT_FOR_PID, BUS_ERROR_NO_SUCH_PROCESS))
×
1068
                        return log_error_errno(r, "%s", bus_error_message(&error, r));
×
1069

1070
                return log_error_errno(r,
×
1071
                                       "Failed to get unit that PID " PID_FMT " belongs to: %s",
1072
                                       pid->pid, bus_error_message(&error, r));
1073
        }
1074

1075
        _cleanup_free_ char *u = NULL, *p = NULL;
5✔
1076
        const char *path, *unit;
5✔
1077

1078
        r = sd_bus_message_read(reply, "os", &path, &unit);
5✔
1079
        if (r < 0)
5✔
1080
                return bus_log_parse_error(r);
×
1081

1082
        if (ret_unit) {
5✔
1083
                u = strdup(unit);
5✔
1084
                if (!u)
5✔
1085
                        return log_oom();
×
1086
        }
1087

1088
        if (ret_path) {
5✔
1089
                p = strdup(path);
1✔
1090
                if (!p)
1✔
1091
                        return log_oom();
×
1092
        }
1093

1094
        if (ret_unit)
5✔
1095
                *ret_unit = TAKE_PTR(u);
5✔
1096
        if (ret_path)
5✔
1097
                *ret_path = TAKE_PTR(p);
1✔
1098

1099
        return 0;
1100
}
1101

1102
int lookup_unit_by_pidref(sd_bus *bus, pid_t pid, char **ret_unit, char **ret_path) {
5✔
1103
        int r;
5✔
1104

1105
        assert(bus);
5✔
1106
        assert(pid >= 0); /* 0 means our own process */
5✔
1107

1108
        if (arg_transport != BUS_TRANSPORT_LOCAL)
5✔
1109
                return get_unit_by_pid(bus, pid, ret_unit, ret_path);
5✔
1110

1111
        static bool use_pidfd = true;
5✔
1112
        _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL;
5✔
1113

1114
        r = pidref_set_pid(&pidref, pid);
5✔
1115
        if (r < 0)
5✔
1116
                return log_error_errno(r,
×
1117
                                       r == -ESRCH ?
1118
                                       "PID " PID_FMT " doesn't exist or is already gone." :
1119
                                       "Failed to create reference to PID " PID_FMT ": %m",
1120
                                       pid);
1121

1122
        if (use_pidfd) {
5✔
1123
                r = get_unit_by_pidfd(bus, &pidref, ret_unit, ret_path);
5✔
1124
                if (r != -EOPNOTSUPP)
5✔
1125
                        return r;
1126

1127
                use_pidfd = false;
×
1128
                log_debug_errno(r, "Unable to look up process using pidfd, falling back to pid.");
×
1129
        }
1130

1131
        _cleanup_free_ char *u = NULL, *p = NULL;
×
1132

1133
        r = get_unit_by_pid(bus, pidref.pid, ret_unit ? &u : NULL, ret_path ? &p : NULL);
×
1134
        if (r < 0)
×
1135
                return r;
1136

1137
        r = pidref_verify(&pidref);
×
1138
        if (r < 0)
×
1139
                return log_error_errno(r, "Failed to verify our reference to PID " PID_FMT ": %m", pidref.pid);
×
1140

1141
        if (ret_unit)
×
1142
                *ret_unit = TAKE_PTR(u);
×
1143
        if (ret_path)
×
1144
                *ret_path = TAKE_PTR(p);
×
1145

1146
        return 0;
1147
}
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