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

systemd / systemd / 14526975710

17 Apr 2025 09:06PM UTC coverage: 72.13% (+0.01%) from 72.117%
14526975710

push

github

yuwata
rules: Make ADB and fastboot work out-of-the-box

https://android.googlesource.com/platform/packages/modules/adb/+/d0db47dcd/adb.h#199
https://android.googlesource.com/platform/system/core/+/7199051aa/fastboot/fastboot.cpp#244

297093 of 411885 relevant lines covered (72.13%)

687643.53 hits per line

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

81.91
/src/systemctl/systemctl-start-unit.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "sd-bus.h"
4

5
#include "ansi-color.h"
6
#include "bus-common-errors.h"
7
#include "bus-error.h"
8
#include "bus-locator.h"
9
#include "bus-util.h"
10
#include "bus-wait-for-jobs.h"
11
#include "bus-wait-for-units.h"
12
#include "macro.h"
13
#include "special.h"
14
#include "string-util.h"
15
#include "systemctl-start-unit.h"
16
#include "systemctl-util.h"
17
#include "systemctl.h"
18
#include "terminal-util.h"
19

20
static const struct {
21
        const char *verb;      /* systemctl verb */
22
        const char *method;    /* Name of the specific D-Bus method */
23
        const char *job_type;  /* Job type when passing to the generic EnqueueUnitJob() method */
24
} unit_actions[] = {
25
        { "start",                 "StartUnit",              "start"                 },
26
        { "stop",                  "StopUnit",               "stop"                  },
27
        { "condstop",              "StopUnit",               "stop"                  }, /* legacy alias */
28
        { "reload",                "ReloadUnit",             "reload"                },
29
        { "restart",               "RestartUnit",            "restart"               },
30
        { "try-restart",           "TryRestartUnit",         "try-restart"           },
31
        { "condrestart",           "TryRestartUnit",         "try-restart"           }, /* legacy alias */
32
        { "reload-or-restart",     "ReloadOrRestartUnit",    "reload-or-restart"     },
33
        { "try-reload-or-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" },
34
        { "reload-or-try-restart", "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
35
        { "condreload",            "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
36
        { "force-reload",          "ReloadOrTryRestartUnit", "reload-or-try-restart" }, /* legacy alias */
37
};
38

39
static const char* verb_to_method(const char *verb) {
1,909✔
40
        assert(verb);
1,909✔
41

42
        FOREACH_ELEMENT(i, unit_actions)
4,420✔
43
                if (streq(i->verb, verb))
4,420✔
44
                        return i->method;
1,909✔
45

46
        return "StartUnit";
47
}
48

49
static const char* verb_to_job_type(const char *verb) {
1,909✔
50
        assert(verb);
1,909✔
51

52
        FOREACH_ELEMENT(i, unit_actions)
4,420✔
53
                if (streq(i->verb, verb))
4,420✔
54
                        return i->job_type;
1,909✔
55

56
        return "start";
57
}
58

59
static int start_unit_one(
1,970✔
60
                sd_bus *bus,
61
                const char *method,    /* When using classic per-job bus methods */
62
                const char *job_type,  /* When using new-style EnqueueUnitJob() */
63
                const char *name,
64
                const char *mode,
65
                sd_bus_error *error,
66
                BusWaitForJobs *w,
67
                BusWaitForUnits *wu) {
68

69
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1,970✔
70
        const char *path;
1,970✔
71
        bool done = false;
1,970✔
72
        int r;
1,970✔
73

74
        assert(method);
1,970✔
75
        assert(name);
1,970✔
76
        assert(mode);
1,970✔
77
        assert(error);
1,970✔
78

79
        log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)",
2,013✔
80
                  arg_dry_run ? "Would execute" : "Executing",
81
                  method, name, mode);
82

83
        if (arg_dry_run)
1,970✔
84
                return 0;
85

86
        if (arg_show_transaction) {
1,966✔
87
                _cleanup_(sd_bus_error_free) sd_bus_error enqueue_error = SD_BUS_ERROR_NULL;
×
88

89
                /* Use the new, fancy EnqueueUnitJob() API if the user wants us to print the transaction */
90
                r = bus_call_method(
10✔
91
                                bus,
92
                                bus_systemd_mgr,
93
                                "EnqueueUnitJob",
94
                                &enqueue_error,
95
                                &reply,
96
                                "sss",
97
                                name, job_type, mode);
98
                if (r < 0) {
10✔
99
                        if (!sd_bus_error_has_name(&enqueue_error, SD_BUS_ERROR_UNKNOWN_METHOD)) {
×
100
                                (void) sd_bus_error_move(error, &enqueue_error);
×
101
                                goto fail;
×
102
                        }
103

104
                        /* Hmm, the API is not yet available. Let's use the classic API instead (see below). */
105
                        log_notice("--show-transaction not supported by this service manager, proceeding without.");
10✔
106
                } else {
107
                        const char *u, *jt;
10✔
108
                        uint32_t id;
10✔
109

110
                        r = sd_bus_message_read(reply, "uosos", &id, &path, &u, NULL, &jt);
10✔
111
                        if (r < 0)
10✔
112
                                return bus_log_parse_error(r);
×
113

114
                        log_info("Enqueued anchor job %" PRIu32 " %s/%s.", id, u, jt);
10✔
115

116
                        r = sd_bus_message_enter_container(reply, 'a', "(uosos)");
10✔
117
                        if (r < 0)
10✔
118
                                return bus_log_parse_error(r);
×
119
                        for (;;) {
13✔
120
                                r = sd_bus_message_read(reply, "(uosos)", &id, NULL, &u, NULL, &jt);
26✔
121
                                if (r < 0)
13✔
122
                                        return bus_log_parse_error(r);
×
123
                                if (r == 0)
13✔
124
                                        break;
125

126
                                log_info("Enqueued auxiliary job %" PRIu32 " %s/%s.", id, u, jt);
16✔
127
                        }
128

129
                        r = sd_bus_message_exit_container(reply);
10✔
130
                        if (r < 0)
10✔
131
                                return bus_log_parse_error(r);
×
132

133
                        done = true;
10✔
134
                }
135
        }
136

137
        if (!done) {
10✔
138
                r = bus_call_method(bus, bus_systemd_mgr, method, error, &reply, "ss", name, mode);
1,956✔
139
                if (r < 0)
1,956✔
140
                        goto fail;
60✔
141

142
                r = sd_bus_message_read(reply, "o", &path);
1,896✔
143
                if (r < 0)
1,896✔
144
                        return bus_log_parse_error(r);
×
145
        }
146

147
        if (need_daemon_reload(bus, name) > 0)
1,906✔
148
                warn_unit_file_changed(name);
×
149

150
        if (w) {
1,906✔
151
                log_debug("Adding %s to the set", path);
1,809✔
152
                r = bus_wait_for_jobs_add(w, path);
1,809✔
153
                if (r < 0)
1,809✔
154
                        return log_error_errno(r, "Failed to watch job for %s: %m", name);
×
155
        }
156

157
        if (wu) {
1,906✔
158
                r = bus_wait_for_units_add_unit(wu, name, BUS_WAIT_FOR_INACTIVE|BUS_WAIT_NO_JOB, NULL, NULL);
14✔
159
                if (r < 0)
14✔
160
                        return log_error_errno(r, "Failed to watch unit %s: %m", name);
×
161
        }
162

163
        return 0;
164

165
fail:
60✔
166
        /* There's always a fallback possible for legacy actions. */
167
        if (arg_action != ACTION_SYSTEMCTL)
60✔
168
                return r;
169

170
        if (sd_bus_error_has_name(error, BUS_ERROR_UNIT_MASKED) &&
60✔
171
            STR_IN_SET(method, "TryRestartUnit", "ReloadOrTryRestartUnit")) {
×
172
                /* Ignore masked unit if try-* is requested */
173

174
                log_debug_errno(r, "Failed to %s %s, ignoring: %s", job_type, name, bus_error_message(error, r));
×
175
                return 0;
×
176
        }
177

178
        log_error_errno(r, "Failed to %s %s: %s", job_type, name, bus_error_message(error, r));
60✔
179

180
        if (!sd_bus_error_has_names(error, BUS_ERROR_NO_SUCH_UNIT,
60✔
181
                                           BUS_ERROR_UNIT_MASKED,
182
                                           BUS_ERROR_JOB_TYPE_NOT_APPLICABLE))
183
                log_error("See %s logs and 'systemctl%s status%s %s' for details.",
1,999✔
184
                          runtime_scope_to_string(arg_runtime_scope),
185
                          arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? "" : " --user",
186
                          name[0] == '-' ? " --" : "",
187
                          name);
188

189
        return r;
190
}
191

192
static int enqueue_marked_jobs(
1✔
193
                sd_bus *bus,
194
                BusWaitForJobs *w) {
195

196
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1✔
197
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1✔
198
        int r;
1✔
199

200
        log_debug("%s dbus call org.freedesktop.systemd1.Manager EnqueueMarkedJobs()",
1✔
201
                  arg_dry_run ? "Would execute" : "Executing");
202

203
        if (arg_dry_run)
1✔
204
                return 0;
205

206
        r = bus_call_method(bus, bus_systemd_mgr, "EnqueueMarkedJobs", &error, &reply, NULL);
1✔
207
        if (r < 0)
1✔
208
                return log_error_errno(r, "Failed to start jobs: %s", bus_error_message(&error, r));
×
209

210
        _cleanup_strv_free_ char **paths = NULL;
1✔
211
        r = sd_bus_message_read_strv(reply, &paths);
1✔
212
        if (r < 0)
1✔
213
                return bus_log_parse_error(r);
×
214

215
        if (w)
1✔
216
                STRV_FOREACH(path, paths) {
2✔
217
                        log_debug("Adding %s to the set", *path);
1✔
218
                        r = bus_wait_for_jobs_add(w, *path);
1✔
219
                        if (r < 0)
1✔
220
                                return log_error_errno(r, "Failed to watch job %s: %m", *path);
×
221
                }
222

223
        return 0;
224
}
225

226
const struct action_metadata action_table[_ACTION_MAX] = {
227
        [ACTION_HALT]                   = { SPECIAL_HALT_TARGET,                   "halt",                   "replace-irreversibly" },
228
        [ACTION_POWEROFF]               = { SPECIAL_POWEROFF_TARGET,               "poweroff",               "replace-irreversibly" },
229
        [ACTION_REBOOT]                 = { SPECIAL_REBOOT_TARGET,                 "reboot",                 "replace-irreversibly" },
230
        [ACTION_KEXEC]                  = { SPECIAL_KEXEC_TARGET,                  "kexec",                  "replace-irreversibly" },
231
        [ACTION_SOFT_REBOOT]            = { SPECIAL_SOFT_REBOOT_TARGET,            "soft-reboot",            "replace-irreversibly" },
232
        [ACTION_RUNLEVEL2]              = { SPECIAL_MULTI_USER_TARGET,             NULL,                     "isolate"              },
233
        [ACTION_RUNLEVEL3]              = { SPECIAL_MULTI_USER_TARGET,             NULL,                     "isolate"              },
234
        [ACTION_RUNLEVEL4]              = { SPECIAL_MULTI_USER_TARGET,             NULL,                     "isolate"              },
235
        [ACTION_RUNLEVEL5]              = { SPECIAL_GRAPHICAL_TARGET,              NULL,                     "isolate"              },
236
        [ACTION_RESCUE]                 = { SPECIAL_RESCUE_TARGET,                 "rescue",                 "isolate"              },
237
        [ACTION_EMERGENCY]              = { SPECIAL_EMERGENCY_TARGET,              "emergency",              "isolate"              },
238
        [ACTION_DEFAULT]                = { SPECIAL_DEFAULT_TARGET,                "default",                "isolate"              },
239
        [ACTION_EXIT]                   = { SPECIAL_EXIT_TARGET,                   "exit",                   "replace-irreversibly" },
240
        [ACTION_SUSPEND]                = { SPECIAL_SUSPEND_TARGET,                "suspend",                "replace-irreversibly" },
241
        [ACTION_HIBERNATE]              = { SPECIAL_HIBERNATE_TARGET,              "hibernate",              "replace-irreversibly" },
242
        [ACTION_HYBRID_SLEEP]           = { SPECIAL_HYBRID_SLEEP_TARGET,           "hybrid-sleep",           "replace-irreversibly" },
243
        [ACTION_SUSPEND_THEN_HIBERNATE] = { SPECIAL_SUSPEND_THEN_HIBERNATE_TARGET, "suspend-then-hibernate", "replace-irreversibly" },
244
        [ACTION_SLEEP]                  = { NULL, /* handled only by logind */     "sleep",                  NULL                   },
245
};
246

247
enum action verb_to_action(const char *verb) {
1,957✔
248
        assert(verb);
1,957✔
249

250
        for (enum action i = 0; i < _ACTION_MAX; i++)
50,081✔
251
                if (streq_ptr(action_table[i].verb, verb))
48,165✔
252
                        return i;
253

254
        return _ACTION_INVALID;
255
}
256

257
static const char** make_extra_args(const char *extra_args[static 4]) {
1,821✔
258
        size_t n = 0;
1,821✔
259

260
        assert(extra_args);
1,821✔
261

262
        if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM)
1,821✔
263
                extra_args[n++] = "--user";
8✔
264

265
        switch (arg_transport) {
1,821✔
266

267
        case BUS_TRANSPORT_REMOTE:
×
268
                extra_args[n++] = "-H";
×
269
                extra_args[n++] = arg_host;
×
270
                break;
×
271

272
        case BUS_TRANSPORT_MACHINE:
6✔
273
                extra_args[n++] = "-M";
6✔
274
                extra_args[n++] = arg_host;
6✔
275
                break;
6✔
276

277
        case BUS_TRANSPORT_CAPSULE:
1✔
278
                extra_args[n++] = "-C";
1✔
279
                extra_args[n++] = arg_host;
1✔
280
                break;
1✔
281

282
        case BUS_TRANSPORT_LOCAL:
283
                break;
284

285
        default:
×
286
                assert_not_reached();
×
287
        }
288

289
        extra_args[n] = NULL;
1,821✔
290
        return extra_args;
1,821✔
291
}
292

293
int verb_start(int argc, char *argv[], void *userdata) {
1,920✔
294
        _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *wu = NULL;
1,920✔
295
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
296
        const char *method, *job_type, *mode, *one_name, *suffix = NULL;
1,920✔
297
        _cleanup_free_ char **stopped_units = NULL; /* Do not use _cleanup_strv_free_ */
1,920✔
298
        _cleanup_strv_free_ char **names = NULL;
1,920✔
299
        int r, ret = EXIT_SUCCESS;
1,920✔
300
        sd_bus *bus;
1,920✔
301

302
        if (arg_wait && !STR_IN_SET(argv[0], "start", "restart"))
1,920✔
303
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
304
                                       "--wait may only be used with the 'start' or 'restart' commands.");
305

306
        /* We cannot do sender tracking on the private bus, so we need the full one for RefUnit to implement
307
         * --wait */
308
        r = acquire_bus(arg_wait ? BUS_FULL : BUS_MANAGER, &bus);
1,920✔
309
        if (r < 0)
1,920✔
310
                return r;
311

312
        ask_password_agent_open_maybe();
1,920✔
313
        polkit_agent_open_maybe();
1,920✔
314

315
        if (arg_action == ACTION_SYSTEMCTL) {
1,920✔
316
                enum action action;
1,920✔
317

318
                action = verb_to_action(argv[0]);
1,920✔
319

320
                assert(action != ACTION_SLEEP);
1,920✔
321

322
                if (action != _ACTION_INVALID) {
1,920✔
323
                        /* A command in style "systemctl reboot", "systemctl poweroff", … */
324
                        method = "StartUnit";
4✔
325
                        job_type = "start";
4✔
326
                        mode = action_table[action].mode;
4✔
327
                        one_name = action_table[action].target;
4✔
328
                } else {
329
                        if (streq(argv[0], "isolate")) {
1,916✔
330
                                /* A "systemctl isolate <unit1> <unit2> …" command */
331
                                method = "StartUnit";
332
                                job_type = "start";
333
                                mode = "isolate";
334
                                suffix = ".target";
335
                        } else if (!arg_marked) {
1,910✔
336
                                /* A command in style of "systemctl start <unit1> <unit2> …", "systemctl stop <unit1> <unit2> …" and so on */
337
                                method = verb_to_method(argv[0]);
1,909✔
338
                                job_type = verb_to_job_type(argv[0]);
1,909✔
339
                                mode = arg_job_mode();
1,909✔
340
                        } else
341
                                method = job_type = mode = NULL;
342

343
                        one_name = NULL;
344
                }
345
        } else {
346
                /* A SysV legacy command such as "halt", "reboot", "poweroff", … */
347
                assert(arg_action >= 0 && arg_action < _ACTION_MAX);
×
348
                assert(action_table[arg_action].target);
×
349
                assert(action_table[arg_action].mode);
×
350

351
                method = "StartUnit";
352
                job_type = "start";
353
                mode = action_table[arg_action].mode;
354
                one_name = action_table[arg_action].target;
355
        }
356

357
        if (one_name) {
4✔
358
                names = strv_new(one_name);
4✔
359
                if (!names)
4✔
360
                        return log_oom();
×
361
        } else if (!arg_marked) {
1,916✔
362
                bool expanded;
1,915✔
363

364
                r = expand_unit_names(bus, strv_skip(argv, 1), suffix, &names, &expanded);
1,915✔
365
                if (r < 0)
1,915✔
366
                        return log_error_errno(r, "Failed to expand names: %m");
×
367

368
                if (!arg_all && expanded && streq(job_type, "start") && !arg_quiet) {
1,915✔
369
                        log_warning("Warning: %ssystemctl start called with a glob pattern.%s",
×
370
                                    ansi_highlight_red(),
371
                                    ansi_normal());
372
                        log_notice("Hint: unit globs expand to loaded units, so start will usually have no effect.\n"
1,915✔
373
                                   "      Passing --all will also load units which are pulled in by other units.\n"
374
                                   "      See systemctl(1) for more details.");
375
                }
376
        }
377

378
        if (!arg_no_block) {
1,920✔
379
                r = bus_wait_for_jobs_new(bus, &w);
1,821✔
380
                if (r < 0)
1,821✔
381
                        return log_error_errno(r, "Could not watch jobs: %m");
×
382
        }
383

384
        if (arg_wait) {
1,920✔
385
                r = bus_wait_for_units_new(bus, &wu);
13✔
386
                if (r < 0)
13✔
387
                        return log_error_errno(r, "Failed to allocate unit watch context: %m");
×
388
        }
389

390
        if (arg_marked)
1,920✔
391
                ret = enqueue_marked_jobs(bus, w);
1✔
392
        else
393
                STRV_FOREACH(name, names) {
3,889✔
394
                        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1,970✔
395

396
                        r = start_unit_one(bus, method, job_type, *name, mode, &error, w, wu);
1,970✔
397
                        if (ret == EXIT_SUCCESS && r < 0)
1,970✔
398
                                ret = translate_bus_error_to_exit_status(r, &error);
60✔
399

400
                        if (r >= 0 && streq(method, "StopUnit")) {
1,970✔
401
                                r = strv_push(&stopped_units, *name);
999✔
402
                                if (r < 0)
999✔
403
                                        return log_oom();
×
404
                        }
405
                }
406

407
        if (!arg_no_block) {
1,920✔
408
                const char *extra_args[4];
1,821✔
409
                WaitJobsFlags flags = 0;
1,821✔
410

411
                SET_FLAG(flags, BUS_WAIT_JOBS_LOG_ERROR, !arg_quiet);
1,821✔
412
                SET_FLAG(flags, BUS_WAIT_JOBS_LOG_SUCCESS, arg_show_transaction);
1,821✔
413
                r = bus_wait_for_jobs(w, flags, make_extra_args(extra_args));
1,821✔
414
                if (r < 0)
1,821✔
415
                        return r;
17✔
416

417
                /* When stopping units, warn if they can still be triggered by
418
                 * another active unit (socket, path, timer) */
419
                if (!arg_quiet && !arg_no_warn)
1,804✔
420
                        STRV_FOREACH(unit, stopped_units)
2,799✔
421
                                warn_triggering_units(bus, *unit, "Stopping", /* ignore_masked = */ true);
996✔
422
        }
423

424
        if (arg_wait) {
1,903✔
425
                r = bus_wait_for_units_run(wu);
13✔
426
                if (r < 0)
13✔
427
                        return log_error_errno(r, "Failed to wait for units: %m");
×
428
                if (r == BUS_WAIT_FAILURE && ret == EXIT_SUCCESS)
13✔
429
                        ret = EXIT_FAILURE;
1✔
430
        }
431

432
        return ret;
433
}
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