X Tutup
Skip to content

Commit daf71ef

Browse files
committed
systemctl: split up humungous systemctl.c file
This is just some refactoring: shifting around of code, not change in codeflow. This splits up the way too huge systemctl.c in multiple more easily digestable files. It roughly follows the rule that each family of verbs gets its own .c/.h file pair, and so do all the compat executable names we support. Plus three extra files for sysv compat (which existed before already, but I renamed slightly, to get the systemctl- prefix lik everything else), a -util file with generic stuff everything uses, and a -logind file with everything that talks directly to logind instead of PID1. systemctl is still a bit too complex for my taste, but I think this way itc omes in a more digestable bits at least. No change of behaviour, just reshuffling of some code.
1 parent 4dcc065 commit daf71ef

File tree

72 files changed

+9864
-9146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+9864
-9146
lines changed

meson.build

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,9 +2105,77 @@ endif
21052105

21062106
public_programs += executable(
21072107
'systemctl',
2108+
'src/systemctl/systemctl-add-dependency.c',
2109+
'src/systemctl/systemctl-add-dependency.h',
2110+
'src/systemctl/systemctl-cancel-job.c',
2111+
'src/systemctl/systemctl-cancel-job.h',
2112+
'src/systemctl/systemctl-clean-or-freeze.c',
2113+
'src/systemctl/systemctl-clean-or-freeze.h',
2114+
'src/systemctl/systemctl-compat-halt.c',
2115+
'src/systemctl/systemctl-compat-halt.h',
2116+
'src/systemctl/systemctl-compat-runlevel.c',
2117+
'src/systemctl/systemctl-compat-runlevel.h',
2118+
'src/systemctl/systemctl-compat-shutdown.c',
2119+
'src/systemctl/systemctl-compat-shutdown.h',
2120+
'src/systemctl/systemctl-compat-telinit.c',
2121+
'src/systemctl/systemctl-compat-telinit.h',
2122+
'src/systemctl/systemctl-daemon-reload.c',
2123+
'src/systemctl/systemctl-daemon-reload.h',
2124+
'src/systemctl/systemctl-edit.c',
2125+
'src/systemctl/systemctl-edit.h',
2126+
'src/systemctl/systemctl-enable.c',
2127+
'src/systemctl/systemctl-enable.h',
2128+
'src/systemctl/systemctl-is-active.c',
2129+
'src/systemctl/systemctl-is-active.h',
2130+
'src/systemctl/systemctl-is-enabled.c',
2131+
'src/systemctl/systemctl-is-enabled.h',
2132+
'src/systemctl/systemctl-is-system-running.c',
2133+
'src/systemctl/systemctl-is-system-running.h',
2134+
'src/systemctl/systemctl-kill.c',
2135+
'src/systemctl/systemctl-kill.h',
2136+
'src/systemctl/systemctl-list-dependencies.c',
2137+
'src/systemctl/systemctl-list-dependencies.h',
2138+
'src/systemctl/systemctl-list-jobs.c',
2139+
'src/systemctl/systemctl-list-jobs.h',
2140+
'src/systemctl/systemctl-list-machines.c',
2141+
'src/systemctl/systemctl-list-machines.h',
2142+
'src/systemctl/systemctl-list-unit-files.c',
2143+
'src/systemctl/systemctl-list-unit-files.h',
2144+
'src/systemctl/systemctl-list-units.c',
2145+
'src/systemctl/systemctl-list-units.h',
2146+
'src/systemctl/systemctl-log-setting.c',
2147+
'src/systemctl/systemctl-log-setting.h',
2148+
'src/systemctl/systemctl-logind.c',
2149+
'src/systemctl/systemctl-logind.h',
2150+
'src/systemctl/systemctl-preset-all.c',
2151+
'src/systemctl/systemctl-preset-all.h',
2152+
'src/systemctl/systemctl-reset-failed.c',
2153+
'src/systemctl/systemctl-reset-failed.h',
2154+
'src/systemctl/systemctl-service-watchdogs.c',
2155+
'src/systemctl/systemctl-service-watchdogs.h',
2156+
'src/systemctl/systemctl-set-default.c',
2157+
'src/systemctl/systemctl-set-default.h',
2158+
'src/systemctl/systemctl-set-environment.c',
2159+
'src/systemctl/systemctl-set-environment.h',
2160+
'src/systemctl/systemctl-set-property.c',
2161+
'src/systemctl/systemctl-set-property.h',
2162+
'src/systemctl/systemctl-show.c',
2163+
'src/systemctl/systemctl-show.h',
2164+
'src/systemctl/systemctl-start-special.c',
2165+
'src/systemctl/systemctl-start-special.h',
2166+
'src/systemctl/systemctl-start-unit.c',
2167+
'src/systemctl/systemctl-start-unit.h',
2168+
'src/systemctl/systemctl-switch-root.c',
2169+
'src/systemctl/systemctl-switch-root.h',
2170+
'src/systemctl/systemctl-sysv-compat.c',
2171+
'src/systemctl/systemctl-sysv-compat.h',
2172+
'src/systemctl/systemctl-trivial-method.c',
2173+
'src/systemctl/systemctl-trivial-method.h',
2174+
'src/systemctl/systemctl-util.c',
2175+
'src/systemctl/systemctl-util.c',
2176+
'src/systemctl/systemctl-util.h',
21082177
'src/systemctl/systemctl.c',
2109-
'src/systemctl/sysv-compat.h',
2110-
'src/systemctl/sysv-compat.c',
2178+
'src/systemctl/systemctl.h',
21112179
include_directories : includes,
21122180
link_with : systemctl_link_with,
21132181
dependencies : [threads,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
3+
#include "bus-error.h"
4+
#include "bus-locator.h"
5+
#include "systemctl-add-dependency.h"
6+
#include "systemctl-daemon-reload.h"
7+
#include "systemctl-util.h"
8+
#include "systemctl.h"
9+
10+
int add_dependency(int argc, char *argv[], void *userdata) {
11+
_cleanup_strv_free_ char **names = NULL;
12+
_cleanup_free_ char *target = NULL;
13+
const char *verb = argv[0];
14+
UnitFileChange *changes = NULL;
15+
size_t n_changes = 0;
16+
UnitDependency dep;
17+
int r;
18+
19+
if (!argv[1])
20+
return 0;
21+
22+
r = unit_name_mangle_with_suffix(argv[1], "as target",
23+
arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
24+
".target", &target);
25+
if (r < 0)
26+
return log_error_errno(r, "Failed to mangle unit name: %m");
27+
28+
r = mangle_names("as dependency", strv_skip(argv, 2), &names);
29+
if (r < 0)
30+
return r;
31+
32+
if (streq(verb, "add-wants"))
33+
dep = UNIT_WANTS;
34+
else if (streq(verb, "add-requires"))
35+
dep = UNIT_REQUIRES;
36+
else
37+
assert_not_reached("Unknown verb");
38+
39+
if (install_client_side()) {
40+
r = unit_file_add_dependency(arg_scope, unit_file_flags_from_args(), arg_root, names, target, dep, &changes, &n_changes);
41+
unit_file_dump_changes(r, "add dependency on", changes, n_changes, arg_quiet);
42+
43+
if (r > 0)
44+
r = 0;
45+
} else {
46+
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
47+
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
48+
sd_bus *bus;
49+
50+
r = acquire_bus(BUS_MANAGER, &bus);
51+
if (r < 0)
52+
return r;
53+
54+
polkit_agent_open_maybe();
55+
56+
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "AddDependencyUnitFiles");
57+
if (r < 0)
58+
return bus_log_create_error(r);
59+
60+
r = sd_bus_message_append_strv(m, names);
61+
if (r < 0)
62+
return bus_log_create_error(r);
63+
64+
r = sd_bus_message_append(m, "ssbb", target, unit_dependency_to_string(dep), arg_runtime, arg_force);
65+
if (r < 0)
66+
return bus_log_create_error(r);
67+
68+
r = sd_bus_call(bus, m, 0, &error, &reply);
69+
if (r < 0)
70+
return log_error_errno(r, "Failed to add dependency: %s", bus_error_message(&error, r));
71+
72+
r = bus_deserialize_and_dump_unit_file_changes(reply, arg_quiet, &changes, &n_changes);
73+
if (r < 0)
74+
goto finish;
75+
76+
if (arg_no_reload) {
77+
r = 0;
78+
goto finish;
79+
}
80+
81+
r = daemon_reload(argc, argv, userdata);
82+
}
83+
84+
finish:
85+
unit_file_changes_free(changes, n_changes);
86+
87+
return r;
88+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
int add_dependency(int argc, char *argv[], void *userdata);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
3+
#include "bus-error.h"
4+
#include "bus-locator.h"
5+
#include "parse-util.h"
6+
#include "systemctl-cancel-job.h"
7+
#include "systemctl-trivial-method.h"
8+
#include "systemctl-util.h"
9+
#include "systemctl.h"
10+
11+
int cancel_job(int argc, char *argv[], void *userdata) {
12+
sd_bus *bus;
13+
char **name;
14+
int r;
15+
16+
if (argc <= 1) /* Shortcut to trivial_method() if no argument is given */
17+
return trivial_method(argc, argv, userdata);
18+
19+
r = acquire_bus(BUS_MANAGER, &bus);
20+
if (r < 0)
21+
return r;
22+
23+
polkit_agent_open_maybe();
24+
25+
STRV_FOREACH(name, strv_skip(argv, 1)) {
26+
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
27+
uint32_t id;
28+
int q;
29+
30+
q = safe_atou32(*name, &id);
31+
if (q < 0)
32+
return log_error_errno(q, "Failed to parse job id \"%s\": %m", *name);
33+
34+
q = bus_call_method(bus, bus_systemd_mgr, "CancelJob", &error, NULL, "u", id);
35+
if (q < 0) {
36+
log_error_errno(q, "Failed to cancel job %"PRIu32": %s", id, bus_error_message(&error, q));
37+
if (r == 0)
38+
r = q;
39+
}
40+
}
41+
42+
return r;
43+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
int cancel_job(int argc, char *argv[], void *userdata);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
3+
#include "bus-error.h"
4+
#include "bus-locator.h"
5+
#include "bus-wait-for-units.h"
6+
#include "systemctl-clean-or-freeze.h"
7+
#include "systemctl-util.h"
8+
#include "systemctl.h"
9+
10+
int clean_or_freeze_unit(int argc, char *argv[], void *userdata) {
11+
_cleanup_(bus_wait_for_units_freep) BusWaitForUnits *w = NULL;
12+
_cleanup_strv_free_ char **names = NULL;
13+
int r, ret = EXIT_SUCCESS;
14+
char **name;
15+
const char *method;
16+
sd_bus *bus;
17+
18+
r = acquire_bus(BUS_FULL, &bus);
19+
if (r < 0)
20+
return r;
21+
22+
polkit_agent_open_maybe();
23+
24+
if (!arg_clean_what) {
25+
arg_clean_what = strv_new("cache", "runtime");
26+
if (!arg_clean_what)
27+
return log_oom();
28+
}
29+
30+
r = expand_unit_names(bus, strv_skip(argv, 1), NULL, &names, NULL);
31+
if (r < 0)
32+
return log_error_errno(r, "Failed to expand names: %m");
33+
34+
if (!arg_no_block) {
35+
r = bus_wait_for_units_new(bus, &w);
36+
if (r < 0)
37+
return log_error_errno(r, "Failed to allocate unit waiter: %m");
38+
}
39+
40+
if (streq(argv[0], "clean"))
41+
method = "CleanUnit";
42+
else if (streq(argv[0], "freeze"))
43+
method = "FreezeUnit";
44+
else if (streq(argv[0], "thaw"))
45+
method = "ThawUnit";
46+
else
47+
assert_not_reached("Unhandled method");
48+
49+
STRV_FOREACH(name, names) {
50+
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
51+
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
52+
53+
if (w) {
54+
/* If we shall wait for the cleaning to complete, let's add a ref on the unit first */
55+
r = bus_call_method(bus, bus_systemd_mgr, "RefUnit", &error, NULL, "s", *name);
56+
if (r < 0) {
57+
log_error_errno(r, "Failed to add reference to unit %s: %s", *name, bus_error_message(&error, r));
58+
if (ret == EXIT_SUCCESS)
59+
ret = r;
60+
continue;
61+
}
62+
}
63+
64+
r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, method);
65+
if (r < 0)
66+
return bus_log_create_error(r);
67+
68+
r = sd_bus_message_append(m, "s", *name);
69+
if (r < 0)
70+
return bus_log_create_error(r);
71+
72+
if (streq(method, "CleanUnit")) {
73+
r = sd_bus_message_append_strv(m, arg_clean_what);
74+
if (r < 0)
75+
return bus_log_create_error(r);
76+
}
77+
78+
r = sd_bus_call(bus, m, 0, &error, NULL);
79+
if (r < 0) {
80+
log_error_errno(r, "Failed to %s unit %s: %s", argv[0], *name, bus_error_message(&error, r));
81+
if (ret == EXIT_SUCCESS) {
82+
ret = r;
83+
continue;
84+
}
85+
}
86+
87+
if (w) {
88+
r = bus_wait_for_units_add_unit(w, *name, BUS_WAIT_REFFED|BUS_WAIT_FOR_MAINTENANCE_END, NULL, NULL);
89+
if (r < 0)
90+
return log_error_errno(r, "Failed to watch unit %s: %m", *name);
91+
}
92+
}
93+
94+
r = bus_wait_for_units_run(w);
95+
if (r < 0)
96+
return log_error_errno(r, "Failed to wait for units: %m");
97+
if (r == BUS_WAIT_FAILURE)
98+
ret = EXIT_FAILURE;
99+
100+
return ret;
101+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
#pragma once
3+
4+
int clean_or_freeze_unit(int argc, char *argv[], void *userdata);

0 commit comments

Comments
 (0)
X Tutup