Kill several SysV compat functionalities (v258) (#38178)

This commit is contained in:
Luca Boccassi
2025-07-15 01:21:13 +01:00
committed by GitHub
44 changed files with 115 additions and 1669 deletions
+1 -4
View File
@@ -32,9 +32,7 @@ systemctl_sources = files(
)
systemctl_extract_sources = files(
'systemctl-compat-halt.c',
'systemctl-compat-runlevel.c',
'systemctl-compat-shutdown.c',
'systemctl-compat-telinit.c',
'systemctl-daemon-reload.c',
'systemctl-logind.c',
'systemctl-start-unit.c',
@@ -74,8 +72,7 @@ executables += [
},
]
foreach alias : (['halt', 'poweroff', 'reboot', 'shutdown'] +
(conf.get('HAVE_SYSV_COMPAT') == 1 ? ['runlevel', 'telinit'] : []))
foreach alias : ['halt', 'poweroff', 'reboot', 'shutdown']
install_symlink(alias,
pointing_to : sbin_to_bin + 'systemctl',
install_dir : sbindir)
+2 -8
View File
@@ -11,7 +11,6 @@
#include "reboot-util.h"
#include "systemctl.h"
#include "systemctl-compat-halt.h"
#include "systemctl-compat-telinit.h"
#include "systemctl-logind.h"
#include "systemctl-start-unit.h"
#include "systemctl-util.h"
@@ -77,16 +76,11 @@ int halt_parse_argv(int argc, char *argv[]) {
{}
};
int c, r, runlevel;
int c, r;
assert(argc >= 0);
assert(argv);
/* called in sysvinit system as last command in shutdown/reboot so this is always forceful */
if (utmp_get_runlevel(&runlevel, NULL) >= 0)
if (IN_SET(runlevel, '0', '6'))
arg_force = 2;
while ((c = getopt_long(argc, argv, "pfwdnih", options, NULL)) >= 0)
switch (c) {
@@ -176,7 +170,7 @@ int halt_main(void) {
arg_no_block = true;
if (!arg_dry_run)
return start_with_fallback();
return verb_start(0, NULL, NULL);
}
r = must_be_root();
-81
View File
@@ -1,81 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <getopt.h>
#include "alloc-util.h"
#include "log.h"
#include "pretty-print.h"
#include "systemctl-compat-runlevel.h"
#include "utmp-wtmp.h"
static int runlevel_help(void) {
_cleanup_free_ char *link = NULL;
int r;
r = terminal_urlify_man("runlevel", "8", &link);
if (r < 0)
return log_oom();
printf("%s [OPTIONS...]\n"
"\n%sPrints the previous and current runlevel of the init system.%s\n"
"\nOptions:\n"
" --help Show this help\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
ansi_normal(),
link);
return 0;
}
int runlevel_parse_argv(int argc, char *argv[]) {
enum {
ARG_HELP = 0x100,
};
static const struct option options[] = {
{ "help", no_argument, NULL, ARG_HELP },
{}
};
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
switch (c) {
case ARG_HELP:
return runlevel_help();
case '?':
return -EINVAL;
default:
assert_not_reached();
}
if (optind < argc)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Too many arguments.");
return 1;
}
int runlevel_main(void) {
int r, runlevel, previous;
r = utmp_get_runlevel(&runlevel, &previous);
if (r < 0) {
puts("unknown");
return r;
}
printf("%c %c\n",
previous <= 0 ? 'N' : previous,
runlevel <= 0 ? 'N' : runlevel);
return 0;
}
@@ -1,6 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
int runlevel_parse_argv(int argc, char *argv[]);
int runlevel_main(void);
+59 -1
View File
@@ -4,12 +4,15 @@
#include "alloc-util.h"
#include "log.h"
#include "parse-util.h"
#include "pretty-print.h"
#include "reboot-util.h"
#include "string-util.h"
#include "strv.h"
#include "systemctl.h"
#include "systemctl-compat-shutdown.h"
#include "systemctl-sysv-compat.h"
#include "systemctl-logind.h"
#include "time-util.h"
static int shutdown_help(void) {
_cleanup_free_ char *link = NULL;
@@ -47,6 +50,61 @@ static int shutdown_help(void) {
return 0;
}
static int parse_shutdown_time_spec(const char *t, usec_t *ret) {
int r;
assert(t);
assert(ret);
/* This parses SysV compat time spec. */
if (streq(t, "now"))
*ret = 0;
else if (!strchr(t, ':')) {
uint64_t u;
if (safe_atou64(t, &u) < 0)
return -EINVAL;
*ret = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
} else {
char *e = NULL;
long hour, minute;
errno = 0;
hour = strtol(t, &e, 10);
if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
return -EINVAL;
minute = strtol(e+1, &e, 10);
if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
return -EINVAL;
usec_t n = now(CLOCK_REALTIME);
struct tm tm = {};
r = localtime_or_gmtime_usec(n, /* utc= */ false, &tm);
if (r < 0)
return r;
tm.tm_hour = (int) hour;
tm.tm_min = (int) minute;
tm.tm_sec = 0;
usec_t s;
r = mktime_or_timegm_usec(&tm, /* utc= */ false, &s);
if (r < 0)
return r;
while (s <= n)
s += USEC_PER_DAY;
*ret = s;
}
return 0;
}
int shutdown_parse_argv(int argc, char *argv[]) {
enum {
ARG_HELP = 0x100,
-156
View File
@@ -1,156 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <getopt.h>
#include <signal.h>
#include "alloc-util.h"
#include "log.h"
#include "pretty-print.h"
#include "systemctl.h"
#include "systemctl-compat-telinit.h"
#include "systemctl-daemon-reload.h"
#include "systemctl-start-unit.h"
#include "systemctl-sysv-compat.h"
static int telinit_help(void) {
_cleanup_free_ char *link = NULL;
int r;
r = terminal_urlify_man("telinit", "8", &link);
if (r < 0)
return log_oom();
printf("%s [OPTIONS...] COMMAND\n\n"
"%sSend control commands to the init daemon.%s\n"
"\nCommands:\n"
" 0 Power-off the machine\n"
" 6 Reboot the machine\n"
" 2, 3, 4, 5 Start runlevelX.target unit\n"
" 1, s, S Enter rescue mode\n"
" q, Q Reload init daemon configuration\n"
" u, U Reexecute init daemon\n"
"\nOptions:\n"
" --help Show this help\n"
" --no-wall Don't send wall message before halt/power-off/reboot\n"
"\nSee the %s for details.\n",
program_invocation_short_name,
ansi_highlight(),
ansi_normal(),
link);
return 0;
}
int telinit_parse_argv(int argc, char *argv[]) {
enum {
ARG_HELP = 0x100,
ARG_NO_WALL
};
static const struct option options[] = {
{ "help", no_argument, NULL, ARG_HELP },
{ "no-wall", no_argument, NULL, ARG_NO_WALL },
{}
};
static const struct {
char from;
enum action to;
} table[] = {
{ '0', ACTION_POWEROFF },
{ '6', ACTION_REBOOT },
{ '1', ACTION_RESCUE },
{ '2', ACTION_RUNLEVEL2 },
{ '3', ACTION_RUNLEVEL3 },
{ '4', ACTION_RUNLEVEL4 },
{ '5', ACTION_RUNLEVEL5 },
{ 's', ACTION_RESCUE },
{ 'S', ACTION_RESCUE },
{ 'q', ACTION_RELOAD },
{ 'Q', ACTION_RELOAD },
{ 'u', ACTION_REEXEC },
{ 'U', ACTION_REEXEC }
};
unsigned i;
int c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
switch (c) {
case ARG_HELP:
return telinit_help();
case ARG_NO_WALL:
arg_no_wall = true;
break;
case '?':
return -EINVAL;
default:
assert_not_reached();
}
if (optind >= argc)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"%s: required argument missing.",
program_invocation_short_name);
if (optind + 1 < argc)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Too many arguments.");
if (strlen(argv[optind]) != 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Expected single character argument.");
for (i = 0; i < ELEMENTSOF(table); i++)
if (table[i].from == argv[optind][0])
break;
if (i >= ELEMENTSOF(table))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Unknown command '%s'.", argv[optind]);
arg_action = table[i].to;
optind++;
return 1;
}
int start_with_fallback(void) {
int r;
/* First, try systemd via D-Bus. */
r = verb_start(0, NULL, NULL);
if (r == 0)
return 0;
#if HAVE_SYSV_COMPAT
/* Nothing else worked, so let's try /dev/initctl */
if (talk_initctl(action_to_runlevel()) > 0)
return 0;
#endif
return log_error_errno(r, "Failed to talk to init daemon: %m");
}
int reload_with_fallback(void) {
assert(IN_SET(arg_action, ACTION_RELOAD, ACTION_REEXEC));
/* First, try systemd via D-Bus */
if (daemon_reload(arg_action, /* graceful= */ true) > 0)
return 0;
/* That didn't work, so let's try signals */
if (kill(1, arg_action == ACTION_RELOAD ? SIGHUP : SIGTERM) < 0)
return log_error_errno(errno, "kill() failed: %m");
return 0;
}
-6
View File
@@ -1,6 +0,0 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
int telinit_parse_argv(int argc, char *argv[]);
int start_with_fallback(void);
int reload_with_fallback(void);
-1
View File
@@ -11,7 +11,6 @@
#include "strv.h"
#include "systemctl.h"
#include "systemctl-is-active.h"
#include "systemctl-sysv-compat.h"
#include "systemctl-util.h"
#include "unit-def.h"
+3 -19
View File
@@ -16,8 +16,6 @@
#include "systemctl-cancel-job.h"
#include "systemctl-clean-or-freeze.h"
#include "systemctl-compat-halt.h"
#include "systemctl-compat-runlevel.h"
#include "systemctl-compat-telinit.h"
#include "systemctl-daemon-reload.h"
#include "systemctl-edit.h"
#include "systemctl-enable.h"
@@ -214,19 +212,6 @@ static int run(int argc, char *argv[]) {
r = halt_main();
break;
case ACTION_RUNLEVEL2:
case ACTION_RUNLEVEL3:
case ACTION_RUNLEVEL4:
case ACTION_RUNLEVEL5:
case ACTION_RESCUE:
r = start_with_fallback();
break;
case ACTION_RELOAD:
case ACTION_REEXEC:
r = reload_with_fallback();
break;
case ACTION_CANCEL_SHUTDOWN:
r = logind_cancel_shutdown();
break;
@@ -236,10 +221,9 @@ static int run(int argc, char *argv[]) {
r = logind_show_shutdown();
break;
case ACTION_RUNLEVEL:
r = runlevel_main();
break;
case ACTION_RESCUE:
case ACTION_RELOAD:
case ACTION_REEXEC:
case ACTION_EXIT:
case ACTION_SLEEP:
case ACTION_SUSPEND:
-1
View File
@@ -48,7 +48,6 @@
#include "systemctl.h"
#include "systemctl-list-machines.h"
#include "systemctl-show.h"
#include "systemctl-sysv-compat.h"
#include "systemctl-util.h"
#include "terminal-util.h"
#include "utf8.h"
-4
View File
@@ -232,10 +232,6 @@ const struct action_metadata action_table[_ACTION_MAX] = {
[ACTION_REBOOT] = { SPECIAL_REBOOT_TARGET, "reboot", "replace-irreversibly" },
[ACTION_KEXEC] = { SPECIAL_KEXEC_TARGET, "kexec", "replace-irreversibly" },
[ACTION_SOFT_REBOOT] = { SPECIAL_SOFT_REBOOT_TARGET, "soft-reboot", "replace-irreversibly" },
[ACTION_RUNLEVEL2] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" },
[ACTION_RUNLEVEL3] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" },
[ACTION_RUNLEVEL4] = { SPECIAL_MULTI_USER_TARGET, NULL, "isolate" },
[ACTION_RUNLEVEL5] = { SPECIAL_GRAPHICAL_TARGET, NULL, "isolate" },
[ACTION_RESCUE] = { SPECIAL_RESCUE_TARGET, "rescue", "isolate" },
[ACTION_EMERGENCY] = { SPECIAL_EMERGENCY_TARGET, "emergency", "isolate" },
[ACTION_DEFAULT] = { SPECIAL_DEFAULT_TARGET, "default", "isolate" },
-119
View File
@@ -5,12 +5,8 @@
#include <unistd.h>
#include "env-util.h"
#include "fd-util.h"
#include "initreq.h"
#include "install.h"
#include "io-util.h"
#include "log.h"
#include "parse-util.h"
#include "path-lookup.h"
#include "path-util.h"
#include "process-util.h"
@@ -18,101 +14,6 @@
#include "strv.h"
#include "systemctl.h"
#include "systemctl-sysv-compat.h"
#include "time-util.h"
int talk_initctl(char rl) {
#if HAVE_SYSV_COMPAT
_cleanup_close_ int fd = -EBADF;
const char *path;
int r;
/* Try to switch to the specified SysV runlevel. Returns == 0 if the operation does not apply on this
* system, and > 0 on success. */
if (rl == 0)
return 0;
FOREACH_STRING(_path, "/run/initctl", "/dev/initctl") {
path = _path;
fd = open(path, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
if (fd < 0 && errno != ENOENT)
return log_error_errno(errno, "Failed to open %s: %m", path);
if (fd >= 0)
break;
}
if (fd < 0)
return 0;
struct init_request request = {
.magic = INIT_MAGIC,
.sleeptime = 0,
.cmd = INIT_CMD_RUNLVL,
.runlevel = rl,
};
r = loop_write(fd, &request, sizeof(request));
if (r < 0)
return log_error_errno(r, "Failed to write to %s: %m", path);
return 1;
#else
return -EOPNOTSUPP;
#endif
}
int parse_shutdown_time_spec(const char *t, usec_t *ret) {
int r;
assert(t);
assert(ret);
if (streq(t, "now"))
*ret = 0;
else if (!strchr(t, ':')) {
uint64_t u;
if (safe_atou64(t, &u) < 0)
return -EINVAL;
*ret = now(CLOCK_REALTIME) + USEC_PER_MINUTE * u;
} else {
char *e = NULL;
long hour, minute;
errno = 0;
hour = strtol(t, &e, 10);
if (errno > 0 || *e != ':' || hour < 0 || hour > 23)
return -EINVAL;
minute = strtol(e+1, &e, 10);
if (errno > 0 || *e != 0 || minute < 0 || minute > 59)
return -EINVAL;
usec_t n = now(CLOCK_REALTIME);
struct tm tm = {};
r = localtime_or_gmtime_usec(n, /* utc= */ false, &tm);
if (r < 0)
return r;
tm.tm_hour = (int) hour;
tm.tm_min = (int) minute;
tm.tm_sec = 0;
usec_t s;
r = mktime_or_timegm_usec(&tm, /* utc= */ false, &s);
if (r < 0)
return r;
while (s <= n)
s += USEC_PER_DAY;
*ret = s;
}
return 0;
}
int enable_sysv_units(const char *verb, char **args) {
int r = 0;
@@ -264,23 +165,3 @@ int enable_sysv_units(const char *verb, char **args) {
#endif
return r;
}
int action_to_runlevel(void) {
#if HAVE_SYSV_COMPAT
static const char table[_ACTION_MAX] = {
[ACTION_HALT] = '0',
[ACTION_POWEROFF] = '0',
[ACTION_REBOOT] = '6',
[ACTION_RUNLEVEL2] = '2',
[ACTION_RUNLEVEL3] = '3',
[ACTION_RUNLEVEL4] = '4',
[ACTION_RUNLEVEL5] = '5',
[ACTION_RESCUE] = '1'
};
assert(arg_action >= 0 && arg_action < _ACTION_MAX);
return table[arg_action];
#else
return -EOPNOTSUPP;
#endif
}
-31
View File
@@ -1,35 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "forward.h"
int talk_initctl(char runlevel);
int parse_shutdown_time_spec(const char *t, usec_t *ret);
/* The init script exit codes for the LSB 'status' verb. (This is different from the 'start' verb, whose exit
codes are defined in exit-status.h.)
0 program is running or service is OK
1 program is dead and /var/run pid file exists
2 program is dead and /var/lock lock file exists
3 program is not running
4 program or service status is unknown
5-99 reserved for future LSB use
100-149 reserved for distribution use
150-199 reserved for application use
200-254 reserved
https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
*/
enum {
EXIT_PROGRAM_RUNNING_OR_SERVICE_OK = 0,
EXIT_PROGRAM_DEAD_AND_PID_EXISTS = 1,
EXIT_PROGRAM_DEAD_AND_LOCK_FILE_EXISTS = 2,
EXIT_PROGRAM_NOT_RUNNING = 3,
EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN = 4,
};
typedef enum SysVUnitEnableState {
SYSV_UNIT_NOT_FOUND = 0,
SYSV_UNIT_DISABLED,
@@ -37,5 +8,3 @@ typedef enum SysVUnitEnableState {
} SysVUnitEnableState;
int enable_sysv_units(const char *verb, char **args);
int action_to_runlevel(void) _pure_;
+23
View File
@@ -3,6 +3,29 @@
#include "systemctl.h"
/* The init script exit codes for the LSB 'status' verb. (This is different from the 'start' verb, whose exit
codes are defined in exit-status.h.)
0 program is running or service is OK
1 program is dead and /var/run pid file exists
2 program is dead and /var/lock lock file exists
3 program is not running
4 program or service status is unknown
5-99 reserved for future LSB use
100-149 reserved for distribution use
150-199 reserved for application use
200-254 reserved
https://refspecs.linuxbase.org/LSB_5.0.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
*/
enum {
EXIT_PROGRAM_RUNNING_OR_SERVICE_OK = 0,
EXIT_PROGRAM_DEAD_AND_PID_EXISTS = 1,
EXIT_PROGRAM_DEAD_AND_LOCK_FILE_EXISTS = 2,
EXIT_PROGRAM_NOT_RUNNING = 3,
EXIT_PROGRAM_OR_SERVICES_STATUS_UNKNOWN = 4,
};
typedef enum BusFocus {
BUS_FULL, /* The full bus indicated via --system or --user */
BUS_MANAGER, /* The manager itself, possibly directly, possibly via the bus */
-24
View File
@@ -24,9 +24,7 @@
#include "strv.h"
#include "systemctl.h"
#include "systemctl-compat-halt.h"
#include "systemctl-compat-runlevel.h"
#include "systemctl-compat-shutdown.h"
#include "systemctl-compat-telinit.h"
#include "systemctl-logind.h"
#include "time-util.h"
@@ -1124,28 +1122,6 @@ int systemctl_dispatch_parse_argv(int argc, char *argv[]) {
} else if (invoked_as(argv, "shutdown")) {
arg_action = ACTION_POWEROFF;
return shutdown_parse_argv(argc, argv);
} else if (invoked_as(argv, "init")) {
/* Matches invocations as "init" as well as "telinit", which are synonymous when run
* as PID != 1 on SysV.
*
* On SysV "telinit" was the official command to communicate with PID 1, but "init" would
* redirect itself to "telinit" if called with PID != 1. We follow the same logic here still,
* though we add one level of indirection, as we implement "telinit" in "systemctl". Hence,
* for us if you invoke "init" you get "systemd", but it will execve() "systemctl"
* immediately with argv[] unmodified if PID is != 1. If you invoke "telinit" you directly
* get "systemctl". In both cases we shall do the same thing, which is why we do
* invoked_as(argv, "init") here, as a quick way to match both.
*
* Also see redirect_telinit() in src/core/main.c. */
arg_action = _ACTION_INVALID; /* telinit_parse_argv() will figure out the actual action we'll execute */
return telinit_parse_argv(argc, argv);
} else if (invoked_as(argv, "runlevel")) {
arg_action = ACTION_RUNLEVEL;
return runlevel_parse_argv(argc, argv);
}
arg_action = ACTION_SYSTEMCTL;
-5
View File
@@ -16,16 +16,11 @@ enum action {
ACTION_HIBERNATE,
ACTION_HYBRID_SLEEP,
ACTION_SUSPEND_THEN_HIBERNATE,
ACTION_RUNLEVEL2,
ACTION_RUNLEVEL3,
ACTION_RUNLEVEL4,
ACTION_RUNLEVEL5,
ACTION_RESCUE,
ACTION_EMERGENCY,
ACTION_DEFAULT,
ACTION_RELOAD,
ACTION_REEXEC,
ACTION_RUNLEVEL,
ACTION_CANCEL_SHUTDOWN,
ACTION_SHOW_SHUTDOWN,
ACTION_SYSTEMCTL_SHOW_SHUTDOWN,