systemctl: Default suffixes for timer and socket

* use .timer suffix by default for systemctl list-timers
* use .socket suffix by default for systemctl list-sockets
This commit is contained in:
Kevin Kuehler 2019-09-14 01:39:28 -07:00 committed by Zbigniew Jędrzejewski-Szmek
parent 3509e678f8
commit 3a77f9fb4c
2 changed files with 139 additions and 125 deletions

1
TODO
View File

@ -1109,7 +1109,6 @@ External:
* systemctl status foo.service should say that it is trigger by foo.timer
* systemctl status should know about 'systemd-analyze calendar ... --iterations='
* systemctl list-timers foo should use .timer suffix by default
* If timer has just OnInactiveSec=..., it should fire after a specified time
after being started.

View File

@ -749,6 +749,64 @@ static int get_unit_list_recursive(
return c;
}
static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) {
_cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
char **name;
int r, i;
assert(bus);
assert(ret);
STRV_FOREACH(name, names) {
char *t;
UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN);
if (suffix)
r = unit_name_mangle_with_suffix(*name, options, suffix, &t);
else
r = unit_name_mangle(*name, options, &t);
if (r < 0)
return log_error_errno(r, "Failed to mangle name: %m");
if (string_is_glob(t))
r = strv_consume(&globs, t);
else
r = strv_consume(&mangled, t);
if (r < 0)
return log_oom();
}
/* Query the manager only if any of the names are a glob, since
* this is fairly expensive */
if (!strv_isempty(globs)) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL;
size_t allocated, n;
r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
if (r < 0)
return r;
n = strv_length(mangled);
allocated = n + 1;
for (i = 0; i < r; i++) {
if (!GREEDY_REALLOC(mangled, allocated, n+2))
return log_oom();
mangled[n] = strdup(unit_infos[i].id);
if (!mangled[n])
return log_oom();
mangled[++n] = NULL;
}
}
*ret = TAKE_PTR(mangled);
return 0;
}
static int list_units(int argc, char *argv[], void *userdata) {
_cleanup_free_ UnitInfo *unit_infos = NULL;
_cleanup_(message_set_freep) Set *replies = NULL;
@ -959,6 +1017,7 @@ static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
static int list_sockets(int argc, char *argv[], void *userdata) {
_cleanup_(message_set_freep) Set *replies = NULL;
_cleanup_strv_free_ char **machines = NULL;
_cleanup_strv_free_ char **sockets_with_suffix = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL;
_cleanup_free_ struct socket_info *socket_infos = NULL;
const UnitInfo *u;
@ -974,7 +1033,12 @@ static int list_sockets(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_pager_flags);
n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines);
r = expand_names(bus, strv_skip(argv, 1), ".socket", &sockets_with_suffix);
if (r < 0)
return r;
if (argc == 1 || sockets_with_suffix) {
n = get_unit_list_recursive(bus, sockets_with_suffix, &unit_infos, &replies, &machines);
if (n < 0)
return n;
@ -1017,6 +1081,7 @@ static int list_sockets(int argc, char *argv[], void *userdata) {
}
typesafe_qsort(socket_infos, cs, socket_info_compare);
}
output_sockets_list(socket_infos, cs);
@ -1263,6 +1328,7 @@ static usec_t calc_next_elapse(dual_timestamp *nw, dual_timestamp *next) {
static int list_timers(int argc, char *argv[], void *userdata) {
_cleanup_(message_set_freep) Set *replies = NULL;
_cleanup_strv_free_ char **machines = NULL;
_cleanup_strv_free_ char **timers_with_suffix = NULL;
_cleanup_free_ struct timer_info *timer_infos = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL;
struct timer_info *t;
@ -1279,7 +1345,12 @@ static int list_timers(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_pager_flags);
n = get_unit_list_recursive(bus, strv_skip(argv, 1), &unit_infos, &replies, &machines);
r = expand_names(bus, strv_skip(argv, 1), ".timer", &timers_with_suffix);
if (r < 0)
return r;
if (argc == 1 || timers_with_suffix) {
n = get_unit_list_recursive(bus, timers_with_suffix, &unit_infos, &replies, &machines);
if (n < 0)
return n;
@ -1320,6 +1391,7 @@ static int list_timers(int argc, char *argv[], void *userdata) {
}
typesafe_qsort(timer_infos, c, timer_info_compare);
}
output_timers_list(timer_infos, c);
@ -2926,63 +2998,6 @@ fail:
return r;
}
static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***ret) {
_cleanup_strv_free_ char **mangled = NULL, **globs = NULL;
char **name;
int r, i;
assert(bus);
assert(ret);
STRV_FOREACH(name, names) {
char *t;
UnitNameMangle options = UNIT_NAME_MANGLE_GLOB | (arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN);
if (suffix)
r = unit_name_mangle_with_suffix(*name, options, suffix, &t);
else
r = unit_name_mangle(*name, options, &t);
if (r < 0)
return log_error_errno(r, "Failed to mangle name: %m");
if (string_is_glob(t))
r = strv_consume(&globs, t);
else
r = strv_consume(&mangled, t);
if (r < 0)
return log_oom();
}
/* Query the manager only if any of the names are a glob, since
* this is fairly expensive */
if (!strv_isempty(globs)) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_free_ UnitInfo *unit_infos = NULL;
size_t allocated, n;
r = get_unit_list(bus, NULL, globs, &unit_infos, 0, &reply);
if (r < 0)
return r;
n = strv_length(mangled);
allocated = n + 1;
for (i = 0; i < r; i++) {
if (!GREEDY_REALLOC(mangled, allocated, n+2))
return log_oom();
mangled[n] = strdup(unit_infos[i].id);
if (!mangled[n])
return log_oom();
mangled[++n] = NULL;
}
}
*ret = TAKE_PTR(mangled);
return 0;
}
static const struct {
const char *target;
const char *verb;