systemctl: move functions in systemctl-sysv-compat.[ch]
- parse_shutdown_time_spec() is used only by systemctl-compat-shutdown.c, - talk_initctl() and action_to_runlevel() are used only by systemctl-compat-telinit.c, - the exit code enum is widely used in systemctl, hence moved to systemctl-util.h. No functional change, preparation for later changes.
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -4,13 +4,16 @@
|
||||
#include <signal.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "initreq.h"
|
||||
#include "io-util.h"
|
||||
#include "log.h"
|
||||
#include "pretty-print.h"
|
||||
#include "strv.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;
|
||||
@@ -123,6 +126,61 @@ int telinit_parse_argv(int argc, char *argv[]) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
#if HAVE_SYSV_COMPAT
|
||||
static int talk_initctl(char rl) {
|
||||
_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;
|
||||
}
|
||||
|
||||
static int action_to_runlevel(void) {
|
||||
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];
|
||||
}
|
||||
#endif
|
||||
|
||||
int start_with_fallback(void) {
|
||||
int r;
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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_;
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user