mirror of
https://github.com/systemd/systemd.git
synced 2025-03-09 12:58:26 +03:00
Merge pull request #11985 from poettering/clean-dir
some rafactoring in systemctl/bus-unit-util
This commit is contained in:
commit
94dfd646b3
@ -3904,12 +3904,12 @@ int exec_context_destroy_runtime_directory(const ExecContext *c, const char *run
|
||||
STRV_FOREACH(i, c->directories[EXEC_DIRECTORY_RUNTIME].paths) {
|
||||
_cleanup_free_ char *p;
|
||||
|
||||
p = strjoin(runtime_prefix, "/", *i);
|
||||
p = path_join(runtime_prefix, *i);
|
||||
if (!p)
|
||||
return -ENOMEM;
|
||||
|
||||
/* We execute this synchronously, since we need to be sure this is gone when we start the service
|
||||
* next. */
|
||||
/* We execute this synchronously, since we need to be sure this is gone when we start the
|
||||
* service next. */
|
||||
(void) rm_rf(p, REMOVE_ROOT);
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-unit-procs.h"
|
||||
#include "bus-util.h"
|
||||
#include "cgroup-show.h"
|
||||
#include "cgroup-util.h"
|
||||
|
@ -18,8 +18,10 @@
|
||||
#include "alloc-util.h"
|
||||
#include "bus-common-errors.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-unit-procs.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "bus-wait-for-jobs.h"
|
||||
#include "cgroup-show.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "copy.h"
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include "bus-error.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "bus-wait-for-jobs.h"
|
||||
#include "device-util.h"
|
||||
#include "dirent-util.h"
|
||||
#include "escape.h"
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include "bus-error.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "bus-wait-for-jobs.h"
|
||||
#include "nspawn-register.h"
|
||||
#include "special.h"
|
||||
#include "stat-util.h"
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "bus-error.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "bus-wait-for-jobs.h"
|
||||
#include "calendarspec.h"
|
||||
#include "env-util.h"
|
||||
#include "fd-util.h"
|
||||
|
411
src/shared/bus-unit-procs.c
Normal file
411
src/shared/bus-unit-procs.c
Normal file
@ -0,0 +1,411 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "bus-unit-procs.h"
|
||||
#include "hashmap.h"
|
||||
#include "list.h"
|
||||
#include "locale-util.h"
|
||||
#include "macro.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
#include "sort-util.h"
|
||||
#include "string-util.h"
|
||||
#include "terminal-util.h"
|
||||
|
||||
struct CGroupInfo {
|
||||
char *cgroup_path;
|
||||
bool is_const; /* If false, cgroup_path should be free()'d */
|
||||
|
||||
Hashmap *pids; /* PID → process name */
|
||||
bool done;
|
||||
|
||||
struct CGroupInfo *parent;
|
||||
LIST_FIELDS(struct CGroupInfo, siblings);
|
||||
LIST_HEAD(struct CGroupInfo, children);
|
||||
size_t n_children;
|
||||
};
|
||||
|
||||
static int add_cgroup(Hashmap *cgroups, const char *path, bool is_const, struct CGroupInfo **ret) {
|
||||
struct CGroupInfo *parent = NULL, *cg;
|
||||
int r;
|
||||
|
||||
assert(cgroups);
|
||||
assert(ret);
|
||||
|
||||
if (empty_or_root(path))
|
||||
path = "/";
|
||||
|
||||
cg = hashmap_get(cgroups, path);
|
||||
if (cg) {
|
||||
*ret = cg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!empty_or_root(path)) {
|
||||
const char *e, *pp;
|
||||
|
||||
e = strrchr(path, '/');
|
||||
if (!e)
|
||||
return -EINVAL;
|
||||
|
||||
pp = strndupa(path, e - path);
|
||||
if (!pp)
|
||||
return -ENOMEM;
|
||||
|
||||
r = add_cgroup(cgroups, pp, false, &parent);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
cg = new0(struct CGroupInfo, 1);
|
||||
if (!cg)
|
||||
return -ENOMEM;
|
||||
|
||||
if (is_const)
|
||||
cg->cgroup_path = (char*) path;
|
||||
else {
|
||||
cg->cgroup_path = strdup(path);
|
||||
if (!cg->cgroup_path) {
|
||||
free(cg);
|
||||
return -ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
cg->is_const = is_const;
|
||||
cg->parent = parent;
|
||||
|
||||
r = hashmap_put(cgroups, cg->cgroup_path, cg);
|
||||
if (r < 0) {
|
||||
if (!is_const)
|
||||
free(cg->cgroup_path);
|
||||
free(cg);
|
||||
return r;
|
||||
}
|
||||
|
||||
if (parent) {
|
||||
LIST_PREPEND(siblings, parent->children, cg);
|
||||
parent->n_children++;
|
||||
}
|
||||
|
||||
*ret = cg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int add_process(
|
||||
Hashmap *cgroups,
|
||||
const char *path,
|
||||
pid_t pid,
|
||||
const char *name) {
|
||||
|
||||
struct CGroupInfo *cg;
|
||||
int r;
|
||||
|
||||
assert(cgroups);
|
||||
assert(name);
|
||||
assert(pid > 0);
|
||||
|
||||
r = add_cgroup(cgroups, path, true, &cg);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = hashmap_ensure_allocated(&cg->pids, &trivial_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return hashmap_put(cg->pids, PID_TO_PTR(pid), (void*) name);
|
||||
}
|
||||
|
||||
static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
|
||||
assert(cgroups);
|
||||
assert(cg);
|
||||
|
||||
while (cg->children)
|
||||
remove_cgroup(cgroups, cg->children);
|
||||
|
||||
hashmap_remove(cgroups, cg->cgroup_path);
|
||||
|
||||
if (!cg->is_const)
|
||||
free(cg->cgroup_path);
|
||||
|
||||
hashmap_free(cg->pids);
|
||||
|
||||
if (cg->parent)
|
||||
LIST_REMOVE(siblings, cg->parent->children, cg);
|
||||
|
||||
free(cg);
|
||||
}
|
||||
|
||||
static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
|
||||
return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
|
||||
}
|
||||
|
||||
static int dump_processes(
|
||||
Hashmap *cgroups,
|
||||
const char *cgroup_path,
|
||||
const char *prefix,
|
||||
unsigned n_columns,
|
||||
OutputFlags flags) {
|
||||
|
||||
struct CGroupInfo *cg;
|
||||
int r;
|
||||
|
||||
assert(prefix);
|
||||
|
||||
if (empty_or_root(cgroup_path))
|
||||
cgroup_path = "/";
|
||||
|
||||
cg = hashmap_get(cgroups, cgroup_path);
|
||||
if (!cg)
|
||||
return 0;
|
||||
|
||||
if (!hashmap_isempty(cg->pids)) {
|
||||
const char *name;
|
||||
size_t n = 0, i;
|
||||
pid_t *pids;
|
||||
void *pidp;
|
||||
Iterator j;
|
||||
int width;
|
||||
|
||||
/* Order processes by their PID */
|
||||
pids = newa(pid_t, hashmap_size(cg->pids));
|
||||
|
||||
HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j)
|
||||
pids[n++] = PTR_TO_PID(pidp);
|
||||
|
||||
assert(n == hashmap_size(cg->pids));
|
||||
typesafe_qsort(pids, n, pid_compare_func);
|
||||
|
||||
width = DECIMAL_STR_WIDTH(pids[n-1]);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
const char *special;
|
||||
bool more;
|
||||
|
||||
name = hashmap_get(cg->pids, PID_TO_PTR(pids[i]));
|
||||
assert(name);
|
||||
|
||||
if (n_columns != 0) {
|
||||
unsigned k;
|
||||
|
||||
k = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
|
||||
|
||||
e = ellipsize(name, k, 100);
|
||||
if (e)
|
||||
name = e;
|
||||
}
|
||||
|
||||
more = i+1 < n || cg->children;
|
||||
special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT);
|
||||
|
||||
fprintf(stdout, "%s%s%*"PID_PRI" %s\n",
|
||||
prefix,
|
||||
special,
|
||||
width, pids[i],
|
||||
name);
|
||||
}
|
||||
}
|
||||
|
||||
if (cg->children) {
|
||||
struct CGroupInfo **children, *child;
|
||||
size_t n = 0, i;
|
||||
|
||||
/* Order subcgroups by their name */
|
||||
children = newa(struct CGroupInfo*, cg->n_children);
|
||||
LIST_FOREACH(siblings, child, cg->children)
|
||||
children[n++] = child;
|
||||
assert(n == cg->n_children);
|
||||
typesafe_qsort(children, n, cgroup_info_compare_func);
|
||||
|
||||
if (n_columns != 0)
|
||||
n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
_cleanup_free_ char *pp = NULL;
|
||||
const char *name, *special;
|
||||
bool more;
|
||||
|
||||
child = children[i];
|
||||
|
||||
name = strrchr(child->cgroup_path, '/');
|
||||
if (!name)
|
||||
return -EINVAL;
|
||||
name++;
|
||||
|
||||
more = i+1 < n;
|
||||
special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT);
|
||||
|
||||
fputs(prefix, stdout);
|
||||
fputs(special, stdout);
|
||||
fputs(name, stdout);
|
||||
fputc('\n', stdout);
|
||||
|
||||
special = special_glyph(more ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE);
|
||||
|
||||
pp = strappend(prefix, special);
|
||||
if (!pp)
|
||||
return -ENOMEM;
|
||||
|
||||
r = dump_processes(cgroups, child->cgroup_path, pp, n_columns, flags);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
cg->done = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dump_extra_processes(
|
||||
Hashmap *cgroups,
|
||||
const char *prefix,
|
||||
unsigned n_columns,
|
||||
OutputFlags flags) {
|
||||
|
||||
_cleanup_free_ pid_t *pids = NULL;
|
||||
_cleanup_hashmap_free_ Hashmap *names = NULL;
|
||||
struct CGroupInfo *cg;
|
||||
size_t n_allocated = 0, n = 0, k;
|
||||
Iterator i;
|
||||
int width, r;
|
||||
|
||||
/* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
|
||||
* combined, sorted, linear list. */
|
||||
|
||||
HASHMAP_FOREACH(cg, cgroups, i) {
|
||||
const char *name;
|
||||
void *pidp;
|
||||
Iterator j;
|
||||
|
||||
if (cg->done)
|
||||
continue;
|
||||
|
||||
if (hashmap_isempty(cg->pids))
|
||||
continue;
|
||||
|
||||
r = hashmap_ensure_allocated(&names, &trivial_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!GREEDY_REALLOC(pids, n_allocated, n + hashmap_size(cg->pids)))
|
||||
return -ENOMEM;
|
||||
|
||||
HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j) {
|
||||
pids[n++] = PTR_TO_PID(pidp);
|
||||
|
||||
r = hashmap_put(names, pidp, (void*) name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
typesafe_qsort(pids, n, pid_compare_func);
|
||||
width = DECIMAL_STR_WIDTH(pids[n-1]);
|
||||
|
||||
for (k = 0; k < n; k++) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
const char *name;
|
||||
|
||||
name = hashmap_get(names, PID_TO_PTR(pids[k]));
|
||||
assert(name);
|
||||
|
||||
if (n_columns != 0) {
|
||||
unsigned z;
|
||||
|
||||
z = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
|
||||
|
||||
e = ellipsize(name, z, 100);
|
||||
if (e)
|
||||
name = e;
|
||||
}
|
||||
|
||||
fprintf(stdout, "%s%s %*" PID_PRI " %s\n",
|
||||
prefix,
|
||||
special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET),
|
||||
width, pids[k],
|
||||
name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int unit_show_processes(
|
||||
sd_bus *bus,
|
||||
const char *unit,
|
||||
const char *cgroup_path,
|
||||
const char *prefix,
|
||||
unsigned n_columns,
|
||||
OutputFlags flags,
|
||||
sd_bus_error *error) {
|
||||
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
Hashmap *cgroups = NULL;
|
||||
struct CGroupInfo *cg;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(unit);
|
||||
|
||||
if (flags & OUTPUT_FULL_WIDTH)
|
||||
n_columns = 0;
|
||||
else if (n_columns <= 0)
|
||||
n_columns = columns();
|
||||
|
||||
prefix = strempty(prefix);
|
||||
|
||||
r = sd_bus_call_method(
|
||||
bus,
|
||||
"org.freedesktop.systemd1",
|
||||
"/org/freedesktop/systemd1",
|
||||
"org.freedesktop.systemd1.Manager",
|
||||
"GetUnitProcesses",
|
||||
error,
|
||||
&reply,
|
||||
"s",
|
||||
unit);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
cgroups = hashmap_new(&path_hash_ops);
|
||||
if (!cgroups)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sd_bus_message_enter_container(reply, 'a', "(sus)");
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
for (;;) {
|
||||
const char *path = NULL, *name = NULL;
|
||||
uint32_t pid;
|
||||
|
||||
r = sd_bus_message_read(reply, "(sus)", &path, &pid, &name);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
r = add_process(cgroups, path, pid, name);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = sd_bus_message_exit_container(reply);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = dump_processes(cgroups, cgroup_path, prefix, n_columns, flags);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = dump_extra_processes(cgroups, prefix, n_columns, flags);
|
||||
|
||||
finish:
|
||||
while ((cg = hashmap_first(cgroups)))
|
||||
remove_cgroup(cgroups, cg);
|
||||
|
||||
hashmap_free(cgroups);
|
||||
|
||||
return r;
|
||||
}
|
8
src/shared/bus-unit-procs.h
Normal file
8
src/shared/bus-unit-procs.h
Normal file
@ -0,0 +1,8 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include "sd-bus.h"
|
||||
|
||||
#include "output-mode.h"
|
||||
|
||||
int unit_show_processes(sd_bus *bus, const char *unit, const char *cgroup_path, const char *prefix, unsigned n_columns, OutputFlags flags, sd_bus_error *error);
|
@ -1,32 +1,29 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "bus-internal.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "cap-list.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "condition.h"
|
||||
#include "cpu-set-util.h"
|
||||
#include "env-util.h"
|
||||
#include "errno-list.h"
|
||||
#include "escape.h"
|
||||
#include "hashmap.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "hostname-util.h"
|
||||
#include "in-addr-util.h"
|
||||
#include "ip-protocol-list.h"
|
||||
#include "list.h"
|
||||
#include "locale-util.h"
|
||||
#include "log.h"
|
||||
#include "missing_fs.h"
|
||||
#include "mountpoint-util.h"
|
||||
#include "nsflags.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
#include "rlimit-util.h"
|
||||
#include "securebits-util.h"
|
||||
#include "signal-util.h"
|
||||
#include "socket-util.h"
|
||||
#include "sort-util.h"
|
||||
#include "string-util.h"
|
||||
#include "syslog-util.h"
|
||||
@ -1772,327 +1769,6 @@ int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct BusWaitForJobs {
|
||||
sd_bus *bus;
|
||||
Set *jobs;
|
||||
|
||||
char *name;
|
||||
char *result;
|
||||
|
||||
sd_bus_slot *slot_job_removed;
|
||||
sd_bus_slot *slot_disconnected;
|
||||
} BusWaitForJobs;
|
||||
|
||||
static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *error) {
|
||||
assert(m);
|
||||
|
||||
log_error("Warning! D-Bus connection terminated.");
|
||||
sd_bus_close(sd_bus_message_get_bus(m));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
|
||||
const char *path, *unit, *result;
|
||||
BusWaitForJobs *d = userdata;
|
||||
uint32_t id;
|
||||
char *found;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
assert(d);
|
||||
|
||||
r = sd_bus_message_read(m, "uoss", &id, &path, &unit, &result);
|
||||
if (r < 0) {
|
||||
bus_log_parse_error(r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
found = set_remove(d->jobs, (char*) path);
|
||||
if (!found)
|
||||
return 0;
|
||||
|
||||
free(found);
|
||||
|
||||
if (!isempty(result))
|
||||
d->result = strdup(result);
|
||||
|
||||
if (!isempty(unit))
|
||||
d->name = strdup(unit);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bus_wait_for_jobs_free(BusWaitForJobs *d) {
|
||||
if (!d)
|
||||
return;
|
||||
|
||||
set_free_free(d->jobs);
|
||||
|
||||
sd_bus_slot_unref(d->slot_disconnected);
|
||||
sd_bus_slot_unref(d->slot_job_removed);
|
||||
|
||||
sd_bus_unref(d->bus);
|
||||
|
||||
free(d->name);
|
||||
free(d->result);
|
||||
|
||||
free(d);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret) {
|
||||
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *d = NULL;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(ret);
|
||||
|
||||
d = new0(BusWaitForJobs, 1);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
|
||||
d->bus = sd_bus_ref(bus);
|
||||
|
||||
/* When we are a bus client we match by sender. Direct
|
||||
* connections OTOH have no initialized sender field, and
|
||||
* hence we ignore the sender then */
|
||||
r = sd_bus_match_signal_async(
|
||||
bus,
|
||||
&d->slot_job_removed,
|
||||
bus->bus_client ? "org.freedesktop.systemd1" : NULL,
|
||||
"/org/freedesktop/systemd1",
|
||||
"org.freedesktop.systemd1.Manager",
|
||||
"JobRemoved",
|
||||
match_job_removed, NULL, d);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_bus_match_signal_async(
|
||||
bus,
|
||||
&d->slot_disconnected,
|
||||
"org.freedesktop.DBus.Local",
|
||||
NULL,
|
||||
"org.freedesktop.DBus.Local",
|
||||
"Disconnected",
|
||||
match_disconnected, NULL, d);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = TAKE_PTR(d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bus_process_wait(sd_bus *bus) {
|
||||
int r;
|
||||
|
||||
for (;;) {
|
||||
r = sd_bus_process(bus, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
return 0;
|
||||
|
||||
r = sd_bus_wait(bus, (uint64_t) -1);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
static int bus_job_get_service_result(BusWaitForJobs *d, char **result) {
|
||||
_cleanup_free_ char *dbus_path = NULL;
|
||||
|
||||
assert(d);
|
||||
assert(d->name);
|
||||
assert(result);
|
||||
|
||||
if (!endswith(d->name, ".service"))
|
||||
return -EINVAL;
|
||||
|
||||
dbus_path = unit_dbus_path_from_name(d->name);
|
||||
if (!dbus_path)
|
||||
return -ENOMEM;
|
||||
|
||||
return sd_bus_get_property_string(d->bus,
|
||||
"org.freedesktop.systemd1",
|
||||
dbus_path,
|
||||
"org.freedesktop.systemd1.Service",
|
||||
"Result",
|
||||
NULL,
|
||||
result);
|
||||
}
|
||||
|
||||
static const struct {
|
||||
const char *result, *explanation;
|
||||
} explanations [] = {
|
||||
{ "resources", "of unavailable resources or another system error" },
|
||||
{ "protocol", "the service did not take the steps required by its unit configuration" },
|
||||
{ "timeout", "a timeout was exceeded" },
|
||||
{ "exit-code", "the control process exited with error code" },
|
||||
{ "signal", "a fatal signal was delivered to the control process" },
|
||||
{ "core-dump", "a fatal signal was delivered causing the control process to dump core" },
|
||||
{ "watchdog", "the service failed to send watchdog ping" },
|
||||
{ "start-limit", "start of the service was attempted too often" }
|
||||
};
|
||||
|
||||
static void log_job_error_with_service_result(const char* service, const char *result, const char* const* extra_args) {
|
||||
_cleanup_free_ char *service_shell_quoted = NULL;
|
||||
const char *systemctl = "systemctl", *journalctl = "journalctl";
|
||||
|
||||
assert(service);
|
||||
|
||||
service_shell_quoted = shell_maybe_quote(service, ESCAPE_BACKSLASH);
|
||||
|
||||
if (!strv_isempty((char**) extra_args)) {
|
||||
_cleanup_free_ char *t;
|
||||
|
||||
t = strv_join((char**) extra_args, " ");
|
||||
systemctl = strjoina("systemctl ", t ? : "<args>");
|
||||
journalctl = strjoina("journalctl ", t ? : "<args>");
|
||||
}
|
||||
|
||||
if (!isempty(result)) {
|
||||
unsigned i;
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(explanations); ++i)
|
||||
if (streq(result, explanations[i].result))
|
||||
break;
|
||||
|
||||
if (i < ELEMENTSOF(explanations)) {
|
||||
log_error("Job for %s failed because %s.\n"
|
||||
"See \"%s status %s\" and \"%s -xe\" for details.\n",
|
||||
service,
|
||||
explanations[i].explanation,
|
||||
systemctl,
|
||||
service_shell_quoted ?: "<service>",
|
||||
journalctl);
|
||||
goto finish;
|
||||
}
|
||||
}
|
||||
|
||||
log_error("Job for %s failed.\n"
|
||||
"See \"%s status %s\" and \"%s -xe\" for details.\n",
|
||||
service,
|
||||
systemctl,
|
||||
service_shell_quoted ?: "<service>",
|
||||
journalctl);
|
||||
|
||||
finish:
|
||||
/* For some results maybe additional explanation is required */
|
||||
if (streq_ptr(result, "start-limit"))
|
||||
log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
|
||||
"followed by \"%1$s start %2$s\" again.",
|
||||
systemctl,
|
||||
service_shell_quoted ?: "<service>");
|
||||
}
|
||||
|
||||
static int check_wait_response(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
|
||||
assert(d->result);
|
||||
|
||||
if (!quiet) {
|
||||
if (streq(d->result, "canceled"))
|
||||
log_error("Job for %s canceled.", strna(d->name));
|
||||
else if (streq(d->result, "timeout"))
|
||||
log_error("Job for %s timed out.", strna(d->name));
|
||||
else if (streq(d->result, "dependency"))
|
||||
log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d->name));
|
||||
else if (streq(d->result, "invalid"))
|
||||
log_error("%s is not active, cannot reload.", strna(d->name));
|
||||
else if (streq(d->result, "assert"))
|
||||
log_error("Assertion failed on job for %s.", strna(d->name));
|
||||
else if (streq(d->result, "unsupported"))
|
||||
log_error("Operation on or unit type of %s not supported on this system.", strna(d->name));
|
||||
else if (streq(d->result, "collected"))
|
||||
log_error("Queued job for %s was garbage collected.", strna(d->name));
|
||||
else if (streq(d->result, "once"))
|
||||
log_error("Unit %s was started already once and can't be started again.", strna(d->name));
|
||||
else if (!STR_IN_SET(d->result, "done", "skipped")) {
|
||||
if (d->name) {
|
||||
_cleanup_free_ char *result = NULL;
|
||||
int q;
|
||||
|
||||
q = bus_job_get_service_result(d, &result);
|
||||
if (q < 0)
|
||||
log_debug_errno(q, "Failed to get Result property of unit %s: %m", d->name);
|
||||
|
||||
log_job_error_with_service_result(d->name, result, extra_args);
|
||||
} else
|
||||
log_error("Job failed. See \"journalctl -xe\" for details.");
|
||||
}
|
||||
}
|
||||
|
||||
if (STR_IN_SET(d->result, "canceled", "collected"))
|
||||
return -ECANCELED;
|
||||
else if (streq(d->result, "timeout"))
|
||||
return -ETIME;
|
||||
else if (streq(d->result, "dependency"))
|
||||
return -EIO;
|
||||
else if (streq(d->result, "invalid"))
|
||||
return -ENOEXEC;
|
||||
else if (streq(d->result, "assert"))
|
||||
return -EPROTO;
|
||||
else if (streq(d->result, "unsupported"))
|
||||
return -EOPNOTSUPP;
|
||||
else if (streq(d->result, "once"))
|
||||
return -ESTALE;
|
||||
else if (STR_IN_SET(d->result, "done", "skipped"))
|
||||
return 0;
|
||||
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EIO),
|
||||
"Unexpected job result, assuming server side newer than us: %s", d->result);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
|
||||
int r = 0;
|
||||
|
||||
assert(d);
|
||||
|
||||
while (!set_isempty(d->jobs)) {
|
||||
int q;
|
||||
|
||||
q = bus_process_wait(d->bus);
|
||||
if (q < 0)
|
||||
return log_error_errno(q, "Failed to wait for response: %m");
|
||||
|
||||
if (d->result) {
|
||||
q = check_wait_response(d, quiet, extra_args);
|
||||
/* Return the first error as it is most likely to be
|
||||
* meaningful. */
|
||||
if (q < 0 && r == 0)
|
||||
r = q;
|
||||
|
||||
log_debug_errno(q, "Got result %s/%m for job %s", strna(d->result), strna(d->name));
|
||||
}
|
||||
|
||||
d->name = mfree(d->name);
|
||||
d->result = mfree(d->result);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
|
||||
int r;
|
||||
|
||||
assert(d);
|
||||
|
||||
r = set_ensure_allocated(&d->jobs, &string_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return set_put_strdup(d->jobs, path);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
|
||||
int r;
|
||||
|
||||
r = bus_wait_for_jobs_add(d, path);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
return bus_wait_for_jobs(d, quiet, NULL);
|
||||
}
|
||||
|
||||
int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, size_t *n_changes) {
|
||||
const char *type, *path, *source;
|
||||
int r;
|
||||
@ -2131,409 +1807,6 @@ int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, Un
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct CGroupInfo {
|
||||
char *cgroup_path;
|
||||
bool is_const; /* If false, cgroup_path should be free()'d */
|
||||
|
||||
Hashmap *pids; /* PID → process name */
|
||||
bool done;
|
||||
|
||||
struct CGroupInfo *parent;
|
||||
LIST_FIELDS(struct CGroupInfo, siblings);
|
||||
LIST_HEAD(struct CGroupInfo, children);
|
||||
size_t n_children;
|
||||
};
|
||||
|
||||
static bool IS_ROOT(const char *p) {
|
||||
return isempty(p) || streq(p, "/");
|
||||
}
|
||||
|
||||
static int add_cgroup(Hashmap *cgroups, const char *path, bool is_const, struct CGroupInfo **ret) {
|
||||
struct CGroupInfo *parent = NULL, *cg;
|
||||
int r;
|
||||
|
||||
assert(cgroups);
|
||||
assert(ret);
|
||||
|
||||
if (IS_ROOT(path))
|
||||
path = "/";
|
||||
|
||||
cg = hashmap_get(cgroups, path);
|
||||
if (cg) {
|
||||
*ret = cg;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!IS_ROOT(path)) {
|
||||
const char *e, *pp;
|
||||
|
||||
e = strrchr(path, '/');
|
||||
if (!e)
|
||||
return -EINVAL;
|
||||
|
||||
pp = strndupa(path, e - path);
|
||||
if (!pp)
|
||||
return -ENOMEM;
|
||||
|
||||
r = add_cgroup(cgroups, pp, false, &parent);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
|
||||
cg = new0(struct CGroupInfo, 1);
|
||||
if (!cg)
|
||||
return -ENOMEM;
|
||||
|
||||
if (is_const)
|
||||
cg->cgroup_path = (char*) path;
|
||||
else {
|
||||
cg->cgroup_path = strdup(path);
|
||||
if (!cg->cgroup_path) {
|
||||
free(cg);
|
||||
return -ENOMEM;
|
||||
}
|
||||
}
|
||||
|
||||
cg->is_const = is_const;
|
||||
cg->parent = parent;
|
||||
|
||||
r = hashmap_put(cgroups, cg->cgroup_path, cg);
|
||||
if (r < 0) {
|
||||
if (!is_const)
|
||||
free(cg->cgroup_path);
|
||||
free(cg);
|
||||
return r;
|
||||
}
|
||||
|
||||
if (parent) {
|
||||
LIST_PREPEND(siblings, parent->children, cg);
|
||||
parent->n_children++;
|
||||
}
|
||||
|
||||
*ret = cg;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int add_process(
|
||||
Hashmap *cgroups,
|
||||
const char *path,
|
||||
pid_t pid,
|
||||
const char *name) {
|
||||
|
||||
struct CGroupInfo *cg;
|
||||
int r;
|
||||
|
||||
assert(cgroups);
|
||||
assert(name);
|
||||
assert(pid > 0);
|
||||
|
||||
r = add_cgroup(cgroups, path, true, &cg);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = hashmap_ensure_allocated(&cg->pids, &trivial_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return hashmap_put(cg->pids, PID_TO_PTR(pid), (void*) name);
|
||||
}
|
||||
|
||||
static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
|
||||
assert(cgroups);
|
||||
assert(cg);
|
||||
|
||||
while (cg->children)
|
||||
remove_cgroup(cgroups, cg->children);
|
||||
|
||||
hashmap_remove(cgroups, cg->cgroup_path);
|
||||
|
||||
if (!cg->is_const)
|
||||
free(cg->cgroup_path);
|
||||
|
||||
hashmap_free(cg->pids);
|
||||
|
||||
if (cg->parent)
|
||||
LIST_REMOVE(siblings, cg->parent->children, cg);
|
||||
|
||||
free(cg);
|
||||
}
|
||||
|
||||
static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
|
||||
return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
|
||||
}
|
||||
|
||||
static int dump_processes(
|
||||
Hashmap *cgroups,
|
||||
const char *cgroup_path,
|
||||
const char *prefix,
|
||||
unsigned n_columns,
|
||||
OutputFlags flags) {
|
||||
|
||||
struct CGroupInfo *cg;
|
||||
int r;
|
||||
|
||||
assert(prefix);
|
||||
|
||||
if (IS_ROOT(cgroup_path))
|
||||
cgroup_path = "/";
|
||||
|
||||
cg = hashmap_get(cgroups, cgroup_path);
|
||||
if (!cg)
|
||||
return 0;
|
||||
|
||||
if (!hashmap_isempty(cg->pids)) {
|
||||
const char *name;
|
||||
size_t n = 0, i;
|
||||
pid_t *pids;
|
||||
void *pidp;
|
||||
Iterator j;
|
||||
int width;
|
||||
|
||||
/* Order processes by their PID */
|
||||
pids = newa(pid_t, hashmap_size(cg->pids));
|
||||
|
||||
HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j)
|
||||
pids[n++] = PTR_TO_PID(pidp);
|
||||
|
||||
assert(n == hashmap_size(cg->pids));
|
||||
typesafe_qsort(pids, n, pid_compare_func);
|
||||
|
||||
width = DECIMAL_STR_WIDTH(pids[n-1]);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
const char *special;
|
||||
bool more;
|
||||
|
||||
name = hashmap_get(cg->pids, PID_TO_PTR(pids[i]));
|
||||
assert(name);
|
||||
|
||||
if (n_columns != 0) {
|
||||
unsigned k;
|
||||
|
||||
k = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
|
||||
|
||||
e = ellipsize(name, k, 100);
|
||||
if (e)
|
||||
name = e;
|
||||
}
|
||||
|
||||
more = i+1 < n || cg->children;
|
||||
special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT);
|
||||
|
||||
fprintf(stdout, "%s%s%*"PID_PRI" %s\n",
|
||||
prefix,
|
||||
special,
|
||||
width, pids[i],
|
||||
name);
|
||||
}
|
||||
}
|
||||
|
||||
if (cg->children) {
|
||||
struct CGroupInfo **children, *child;
|
||||
size_t n = 0, i;
|
||||
|
||||
/* Order subcgroups by their name */
|
||||
children = newa(struct CGroupInfo*, cg->n_children);
|
||||
LIST_FOREACH(siblings, child, cg->children)
|
||||
children[n++] = child;
|
||||
assert(n == cg->n_children);
|
||||
typesafe_qsort(children, n, cgroup_info_compare_func);
|
||||
|
||||
if (n_columns != 0)
|
||||
n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
|
||||
|
||||
for (i = 0; i < n; i++) {
|
||||
_cleanup_free_ char *pp = NULL;
|
||||
const char *name, *special;
|
||||
bool more;
|
||||
|
||||
child = children[i];
|
||||
|
||||
name = strrchr(child->cgroup_path, '/');
|
||||
if (!name)
|
||||
return -EINVAL;
|
||||
name++;
|
||||
|
||||
more = i+1 < n;
|
||||
special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT);
|
||||
|
||||
fputs(prefix, stdout);
|
||||
fputs(special, stdout);
|
||||
fputs(name, stdout);
|
||||
fputc('\n', stdout);
|
||||
|
||||
special = special_glyph(more ? SPECIAL_GLYPH_TREE_VERTICAL : SPECIAL_GLYPH_TREE_SPACE);
|
||||
|
||||
pp = strappend(prefix, special);
|
||||
if (!pp)
|
||||
return -ENOMEM;
|
||||
|
||||
r = dump_processes(cgroups, child->cgroup_path, pp, n_columns, flags);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
cg->done = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dump_extra_processes(
|
||||
Hashmap *cgroups,
|
||||
const char *prefix,
|
||||
unsigned n_columns,
|
||||
OutputFlags flags) {
|
||||
|
||||
_cleanup_free_ pid_t *pids = NULL;
|
||||
_cleanup_hashmap_free_ Hashmap *names = NULL;
|
||||
struct CGroupInfo *cg;
|
||||
size_t n_allocated = 0, n = 0, k;
|
||||
Iterator i;
|
||||
int width, r;
|
||||
|
||||
/* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
|
||||
* combined, sorted, linear list. */
|
||||
|
||||
HASHMAP_FOREACH(cg, cgroups, i) {
|
||||
const char *name;
|
||||
void *pidp;
|
||||
Iterator j;
|
||||
|
||||
if (cg->done)
|
||||
continue;
|
||||
|
||||
if (hashmap_isempty(cg->pids))
|
||||
continue;
|
||||
|
||||
r = hashmap_ensure_allocated(&names, &trivial_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (!GREEDY_REALLOC(pids, n_allocated, n + hashmap_size(cg->pids)))
|
||||
return -ENOMEM;
|
||||
|
||||
HASHMAP_FOREACH_KEY(name, pidp, cg->pids, j) {
|
||||
pids[n++] = PTR_TO_PID(pidp);
|
||||
|
||||
r = hashmap_put(names, pidp, (void*) name);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
if (n == 0)
|
||||
return 0;
|
||||
|
||||
typesafe_qsort(pids, n, pid_compare_func);
|
||||
width = DECIMAL_STR_WIDTH(pids[n-1]);
|
||||
|
||||
for (k = 0; k < n; k++) {
|
||||
_cleanup_free_ char *e = NULL;
|
||||
const char *name;
|
||||
|
||||
name = hashmap_get(names, PID_TO_PTR(pids[k]));
|
||||
assert(name);
|
||||
|
||||
if (n_columns != 0) {
|
||||
unsigned z;
|
||||
|
||||
z = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
|
||||
|
||||
e = ellipsize(name, z, 100);
|
||||
if (e)
|
||||
name = e;
|
||||
}
|
||||
|
||||
fprintf(stdout, "%s%s %*" PID_PRI " %s\n",
|
||||
prefix,
|
||||
special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET),
|
||||
width, pids[k],
|
||||
name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int unit_show_processes(
|
||||
sd_bus *bus,
|
||||
const char *unit,
|
||||
const char *cgroup_path,
|
||||
const char *prefix,
|
||||
unsigned n_columns,
|
||||
OutputFlags flags,
|
||||
sd_bus_error *error) {
|
||||
|
||||
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
|
||||
Hashmap *cgroups = NULL;
|
||||
struct CGroupInfo *cg;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(unit);
|
||||
|
||||
if (flags & OUTPUT_FULL_WIDTH)
|
||||
n_columns = 0;
|
||||
else if (n_columns <= 0)
|
||||
n_columns = columns();
|
||||
|
||||
prefix = strempty(prefix);
|
||||
|
||||
r = sd_bus_call_method(
|
||||
bus,
|
||||
"org.freedesktop.systemd1",
|
||||
"/org/freedesktop/systemd1",
|
||||
"org.freedesktop.systemd1.Manager",
|
||||
"GetUnitProcesses",
|
||||
error,
|
||||
&reply,
|
||||
"s",
|
||||
unit);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
cgroups = hashmap_new(&path_hash_ops);
|
||||
if (!cgroups)
|
||||
return -ENOMEM;
|
||||
|
||||
r = sd_bus_message_enter_container(reply, 'a', "(sus)");
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
for (;;) {
|
||||
const char *path = NULL, *name = NULL;
|
||||
uint32_t pid;
|
||||
|
||||
r = sd_bus_message_read(reply, "(sus)", &path, &pid, &name);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
r = add_process(cgroups, path, pid, name);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
}
|
||||
|
||||
r = sd_bus_message_exit_container(reply);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = dump_processes(cgroups, cgroup_path, prefix, n_columns, flags);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
r = dump_extra_processes(cgroups, prefix, n_columns, flags);
|
||||
|
||||
finish:
|
||||
while ((cg = hashmap_first(cgroups)))
|
||||
remove_cgroup(cgroups, cg);
|
||||
|
||||
hashmap_free(cgroups);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int unit_load_state(sd_bus *bus, const char *name, char **load_state) {
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_free_ char *path = NULL;
|
||||
|
@ -1,9 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include "install.h"
|
||||
#include "output-mode.h"
|
||||
#include "sd-bus.h"
|
||||
|
||||
#include "install.h"
|
||||
#include "unit-def.h"
|
||||
|
||||
typedef struct UnitInfo {
|
||||
@ -25,18 +25,6 @@ int bus_parse_unit_info(sd_bus_message *message, UnitInfo *u);
|
||||
int bus_append_unit_property_assignment(sd_bus_message *m, UnitType t, const char *assignment);
|
||||
int bus_append_unit_property_assignment_many(sd_bus_message *m, UnitType t, char **l);
|
||||
|
||||
typedef struct BusWaitForJobs BusWaitForJobs;
|
||||
|
||||
int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret);
|
||||
void bus_wait_for_jobs_free(BusWaitForJobs *d);
|
||||
int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path);
|
||||
int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args);
|
||||
int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(BusWaitForJobs*, bus_wait_for_jobs_free);
|
||||
|
||||
int bus_deserialize_and_dump_unit_file_changes(sd_bus_message *m, bool quiet, UnitFileChange **changes, size_t *n_changes);
|
||||
|
||||
int unit_show_processes(sd_bus *bus, const char *unit, const char *cgroup_path, const char *prefix, unsigned n_columns, OutputFlags flags, sd_bus_error *error);
|
||||
|
||||
int unit_load_state(sd_bus *bus, const char *name, char **load_state);
|
||||
|
333
src/shared/bus-wait-for-jobs.c
Normal file
333
src/shared/bus-wait-for-jobs.c
Normal file
@ -0,0 +1,333 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "bus-wait-for-jobs.h"
|
||||
#include "set.h"
|
||||
#include "bus-util.h"
|
||||
#include "bus-internal.h"
|
||||
#include "unit-def.h"
|
||||
#include "escape.h"
|
||||
#include "strv.h"
|
||||
|
||||
typedef struct BusWaitForJobs {
|
||||
sd_bus *bus;
|
||||
Set *jobs;
|
||||
|
||||
char *name;
|
||||
char *result;
|
||||
|
||||
sd_bus_slot *slot_job_removed;
|
||||
sd_bus_slot *slot_disconnected;
|
||||
} BusWaitForJobs;
|
||||
|
||||
static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *error) {
|
||||
assert(m);
|
||||
|
||||
log_error("Warning! D-Bus connection terminated.");
|
||||
sd_bus_close(sd_bus_message_get_bus(m));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int match_job_removed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
|
||||
const char *path, *unit, *result;
|
||||
BusWaitForJobs *d = userdata;
|
||||
uint32_t id;
|
||||
char *found;
|
||||
int r;
|
||||
|
||||
assert(m);
|
||||
assert(d);
|
||||
|
||||
r = sd_bus_message_read(m, "uoss", &id, &path, &unit, &result);
|
||||
if (r < 0) {
|
||||
bus_log_parse_error(r);
|
||||
return 0;
|
||||
}
|
||||
|
||||
found = set_remove(d->jobs, (char*) path);
|
||||
if (!found)
|
||||
return 0;
|
||||
|
||||
free(found);
|
||||
|
||||
(void) free_and_strdup(&d->result, empty_to_null(result));
|
||||
(void) free_and_strdup(&d->name, empty_to_null(unit));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bus_wait_for_jobs_free(BusWaitForJobs *d) {
|
||||
if (!d)
|
||||
return;
|
||||
|
||||
set_free_free(d->jobs);
|
||||
|
||||
sd_bus_slot_unref(d->slot_disconnected);
|
||||
sd_bus_slot_unref(d->slot_job_removed);
|
||||
|
||||
sd_bus_unref(d->bus);
|
||||
|
||||
free(d->name);
|
||||
free(d->result);
|
||||
|
||||
free(d);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret) {
|
||||
_cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *d = NULL;
|
||||
int r;
|
||||
|
||||
assert(bus);
|
||||
assert(ret);
|
||||
|
||||
d = new(BusWaitForJobs, 1);
|
||||
if (!d)
|
||||
return -ENOMEM;
|
||||
|
||||
*d = (BusWaitForJobs) {
|
||||
.bus = sd_bus_ref(bus),
|
||||
};
|
||||
|
||||
/* When we are a bus client we match by sender. Direct
|
||||
* connections OTOH have no initialized sender field, and
|
||||
* hence we ignore the sender then */
|
||||
r = sd_bus_match_signal_async(
|
||||
bus,
|
||||
&d->slot_job_removed,
|
||||
bus->bus_client ? "org.freedesktop.systemd1" : NULL,
|
||||
"/org/freedesktop/systemd1",
|
||||
"org.freedesktop.systemd1.Manager",
|
||||
"JobRemoved",
|
||||
match_job_removed, NULL, d);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = sd_bus_match_signal_async(
|
||||
bus,
|
||||
&d->slot_disconnected,
|
||||
"org.freedesktop.DBus.Local",
|
||||
NULL,
|
||||
"org.freedesktop.DBus.Local",
|
||||
"Disconnected",
|
||||
match_disconnected, NULL, d);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
*ret = TAKE_PTR(d);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bus_process_wait(sd_bus *bus) {
|
||||
int r;
|
||||
|
||||
for (;;) {
|
||||
r = sd_bus_process(bus, NULL);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r > 0)
|
||||
return 0;
|
||||
|
||||
r = sd_bus_wait(bus, (uint64_t) -1);
|
||||
if (r < 0)
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
static int bus_job_get_service_result(BusWaitForJobs *d, char **result) {
|
||||
_cleanup_free_ char *dbus_path = NULL;
|
||||
|
||||
assert(d);
|
||||
assert(d->name);
|
||||
assert(result);
|
||||
|
||||
if (!endswith(d->name, ".service"))
|
||||
return -EINVAL;
|
||||
|
||||
dbus_path = unit_dbus_path_from_name(d->name);
|
||||
if (!dbus_path)
|
||||
return -ENOMEM;
|
||||
|
||||
return sd_bus_get_property_string(d->bus,
|
||||
"org.freedesktop.systemd1",
|
||||
dbus_path,
|
||||
"org.freedesktop.systemd1.Service",
|
||||
"Result",
|
||||
NULL,
|
||||
result);
|
||||
}
|
||||
|
||||
static void log_job_error_with_service_result(const char* service, const char *result, const char* const* extra_args) {
|
||||
_cleanup_free_ char *service_shell_quoted = NULL;
|
||||
const char *systemctl = "systemctl", *journalctl = "journalctl";
|
||||
|
||||
static const struct {
|
||||
const char *result, *explanation;
|
||||
} explanations[] = {
|
||||
{ "resources", "of unavailable resources or another system error" },
|
||||
{ "protocol", "the service did not take the steps required by its unit configuration" },
|
||||
{ "timeout", "a timeout was exceeded" },
|
||||
{ "exit-code", "the control process exited with error code" },
|
||||
{ "signal", "a fatal signal was delivered to the control process" },
|
||||
{ "core-dump", "a fatal signal was delivered causing the control process to dump core" },
|
||||
{ "watchdog", "the service failed to send watchdog ping" },
|
||||
{ "start-limit", "start of the service was attempted too often" }
|
||||
};
|
||||
|
||||
assert(service);
|
||||
|
||||
service_shell_quoted = shell_maybe_quote(service, ESCAPE_BACKSLASH);
|
||||
|
||||
if (!strv_isempty((char**) extra_args)) {
|
||||
_cleanup_free_ char *t;
|
||||
|
||||
t = strv_join((char**) extra_args, " ");
|
||||
systemctl = strjoina("systemctl ", t ? : "<args>");
|
||||
journalctl = strjoina("journalctl ", t ? : "<args>");
|
||||
}
|
||||
|
||||
if (!isempty(result)) {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < ELEMENTSOF(explanations); ++i)
|
||||
if (streq(result, explanations[i].result))
|
||||
break;
|
||||
|
||||
if (i < ELEMENTSOF(explanations)) {
|
||||
log_error("Job for %s failed because %s.\n"
|
||||
"See \"%s status %s\" and \"%s -xe\" for details.\n",
|
||||
service,
|
||||
explanations[i].explanation,
|
||||
systemctl,
|
||||
service_shell_quoted ?: "<service>",
|
||||
journalctl);
|
||||
goto finish;
|
||||
}
|
||||
}
|
||||
|
||||
log_error("Job for %s failed.\n"
|
||||
"See \"%s status %s\" and \"%s -xe\" for details.\n",
|
||||
service,
|
||||
systemctl,
|
||||
service_shell_quoted ?: "<service>",
|
||||
journalctl);
|
||||
|
||||
finish:
|
||||
/* For some results maybe additional explanation is required */
|
||||
if (streq_ptr(result, "start-limit"))
|
||||
log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
|
||||
"followed by \"%1$s start %2$s\" again.",
|
||||
systemctl,
|
||||
service_shell_quoted ?: "<service>");
|
||||
}
|
||||
|
||||
static int check_wait_response(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
|
||||
assert(d);
|
||||
assert(d->name);
|
||||
assert(d->result);
|
||||
|
||||
if (!quiet) {
|
||||
if (streq(d->result, "canceled"))
|
||||
log_error("Job for %s canceled.", strna(d->name));
|
||||
else if (streq(d->result, "timeout"))
|
||||
log_error("Job for %s timed out.", strna(d->name));
|
||||
else if (streq(d->result, "dependency"))
|
||||
log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d->name));
|
||||
else if (streq(d->result, "invalid"))
|
||||
log_error("%s is not active, cannot reload.", strna(d->name));
|
||||
else if (streq(d->result, "assert"))
|
||||
log_error("Assertion failed on job for %s.", strna(d->name));
|
||||
else if (streq(d->result, "unsupported"))
|
||||
log_error("Operation on or unit type of %s not supported on this system.", strna(d->name));
|
||||
else if (streq(d->result, "collected"))
|
||||
log_error("Queued job for %s was garbage collected.", strna(d->name));
|
||||
else if (streq(d->result, "once"))
|
||||
log_error("Unit %s was started already once and can't be started again.", strna(d->name));
|
||||
else if (!STR_IN_SET(d->result, "done", "skipped")) {
|
||||
|
||||
if (d->name && endswith(d->name, ".service")) {
|
||||
_cleanup_free_ char *result = NULL;
|
||||
int q;
|
||||
|
||||
q = bus_job_get_service_result(d, &result);
|
||||
if (q < 0)
|
||||
log_debug_errno(q, "Failed to get Result property of unit %s: %m", d->name);
|
||||
|
||||
log_job_error_with_service_result(d->name, result, extra_args);
|
||||
} else
|
||||
log_error("Job failed. See \"journalctl -xe\" for details.");
|
||||
}
|
||||
}
|
||||
|
||||
if (STR_IN_SET(d->result, "canceled", "collected"))
|
||||
return -ECANCELED;
|
||||
else if (streq(d->result, "timeout"))
|
||||
return -ETIME;
|
||||
else if (streq(d->result, "dependency"))
|
||||
return -EIO;
|
||||
else if (streq(d->result, "invalid"))
|
||||
return -ENOEXEC;
|
||||
else if (streq(d->result, "assert"))
|
||||
return -EPROTO;
|
||||
else if (streq(d->result, "unsupported"))
|
||||
return -EOPNOTSUPP;
|
||||
else if (streq(d->result, "once"))
|
||||
return -ESTALE;
|
||||
else if (STR_IN_SET(d->result, "done", "skipped"))
|
||||
return 0;
|
||||
|
||||
return log_debug_errno(SYNTHETIC_ERRNO(EIO),
|
||||
"Unexpected job result, assuming server side newer than us: %s", d->result);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args) {
|
||||
int r = 0;
|
||||
|
||||
assert(d);
|
||||
|
||||
while (!set_isempty(d->jobs)) {
|
||||
int q;
|
||||
|
||||
q = bus_process_wait(d->bus);
|
||||
if (q < 0)
|
||||
return log_error_errno(q, "Failed to wait for response: %m");
|
||||
|
||||
if (d->name && d->result) {
|
||||
q = check_wait_response(d, quiet, extra_args);
|
||||
/* Return the first error as it is most likely to be
|
||||
* meaningful. */
|
||||
if (q < 0 && r == 0)
|
||||
r = q;
|
||||
|
||||
log_debug_errno(q, "Got result %s/%m for job %s", d->result, d->name);
|
||||
}
|
||||
|
||||
d->name = mfree(d->name);
|
||||
d->result = mfree(d->result);
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path) {
|
||||
int r;
|
||||
|
||||
assert(d);
|
||||
|
||||
r = set_ensure_allocated(&d->jobs, &string_hash_ops);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return set_put_strdup(d->jobs, path);
|
||||
}
|
||||
|
||||
int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet) {
|
||||
int r;
|
||||
|
||||
r = bus_wait_for_jobs_add(d, path);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
return bus_wait_for_jobs(d, quiet, NULL);
|
||||
}
|
15
src/shared/bus-wait-for-jobs.h
Normal file
15
src/shared/bus-wait-for-jobs.h
Normal file
@ -0,0 +1,15 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include "sd-bus.h"
|
||||
#include "macro.h"
|
||||
|
||||
typedef struct BusWaitForJobs BusWaitForJobs;
|
||||
|
||||
int bus_wait_for_jobs_new(sd_bus *bus, BusWaitForJobs **ret);
|
||||
void bus_wait_for_jobs_free(BusWaitForJobs *d);
|
||||
int bus_wait_for_jobs_add(BusWaitForJobs *d, const char *path);
|
||||
int bus_wait_for_jobs(BusWaitForJobs *d, bool quiet, const char* const* extra_args);
|
||||
int bus_wait_for_jobs_one(BusWaitForJobs *d, const char *path, bool quiet);
|
||||
|
||||
DEFINE_TRIVIAL_CLEANUP_FUNC(BusWaitForJobs*, bus_wait_for_jobs_free);
|
@ -25,6 +25,10 @@ shared_sources = files('''
|
||||
bus-unit-util.h
|
||||
bus-util.c
|
||||
bus-util.h
|
||||
bus-wait-for-jobs.c
|
||||
bus-wait-for-jobs.h
|
||||
bus-unit-procs.c
|
||||
bus-unit-procs.h
|
||||
calendarspec.c
|
||||
calendarspec.h
|
||||
cgroup-show.c
|
||||
|
@ -24,8 +24,10 @@
|
||||
#include "bus-common-errors.h"
|
||||
#include "bus-error.h"
|
||||
#include "bus-message.h"
|
||||
#include "bus-unit-procs.h"
|
||||
#include "bus-unit-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "bus-wait-for-jobs.h"
|
||||
#include "cgroup-show.h"
|
||||
#include "cgroup-util.h"
|
||||
#include "copy.h"
|
||||
@ -2893,6 +2895,56 @@ static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int wait_context_watch(
|
||||
WaitContext *wait_context,
|
||||
sd_bus *bus,
|
||||
const char *name) {
|
||||
|
||||
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
||||
_cleanup_free_ char *unit_path = NULL;
|
||||
int r;
|
||||
|
||||
assert(wait_context);
|
||||
assert(name);
|
||||
|
||||
log_debug("Watching for property changes of %s", name);
|
||||
r = sd_bus_call_method(
|
||||
bus,
|
||||
"org.freedesktop.systemd1",
|
||||
"/org/freedesktop/systemd1",
|
||||
"org.freedesktop.systemd1.Manager",
|
||||
"RefUnit",
|
||||
&error,
|
||||
NULL,
|
||||
"s", name);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to add reference to unit %s: %s", name, bus_error_message(&error, r));
|
||||
|
||||
unit_path = unit_dbus_path_from_name(name);
|
||||
if (!unit_path)
|
||||
return log_oom();
|
||||
|
||||
r = set_ensure_allocated(&wait_context->unit_paths, &string_hash_ops);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
r = set_put_strdup(wait_context->unit_paths, unit_path);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path);
|
||||
|
||||
r = sd_bus_match_signal_async(bus,
|
||||
&wait_context->match,
|
||||
NULL,
|
||||
unit_path,
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"PropertiesChanged",
|
||||
on_properties_changed, NULL, wait_context);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int start_unit_one(
|
||||
sd_bus *bus,
|
||||
const char *method,
|
||||
@ -2912,38 +2964,9 @@ static int start_unit_one(
|
||||
assert(error);
|
||||
|
||||
if (wait_context) {
|
||||
_cleanup_free_ char *unit_path = NULL;
|
||||
|
||||
log_debug("Watching for property changes of %s", name);
|
||||
r = sd_bus_call_method(
|
||||
bus,
|
||||
"org.freedesktop.systemd1",
|
||||
"/org/freedesktop/systemd1",
|
||||
"org.freedesktop.systemd1.Manager",
|
||||
"RefUnit",
|
||||
error,
|
||||
NULL,
|
||||
"s", name);
|
||||
r = wait_context_watch(wait_context, bus, name);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to RefUnit %s: %s", name, bus_error_message(error, r));
|
||||
|
||||
unit_path = unit_dbus_path_from_name(name);
|
||||
if (!unit_path)
|
||||
return log_oom();
|
||||
|
||||
r = set_put_strdup(wait_context->unit_paths, unit_path);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to add unit path %s to set: %m", unit_path);
|
||||
|
||||
r = sd_bus_match_signal_async(bus,
|
||||
&wait_context->match,
|
||||
NULL,
|
||||
unit_path,
|
||||
"org.freedesktop.DBus.Properties",
|
||||
"PropertiesChanged",
|
||||
on_properties_changed, NULL, wait_context);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to request match for PropertiesChanged signal: %m");
|
||||
return r;
|
||||
}
|
||||
|
||||
log_debug("%s dbus call org.freedesktop.systemd1.Manager %s(%s, %s)",
|
||||
@ -3164,10 +3187,6 @@ static int start_unit(int argc, char *argv[], void *userdata) {
|
||||
}
|
||||
|
||||
if (arg_wait) {
|
||||
wait_context.unit_paths = set_new(&string_hash_ops);
|
||||
if (!wait_context.unit_paths)
|
||||
return log_oom();
|
||||
|
||||
r = sd_bus_call_method_async(
|
||||
bus,
|
||||
NULL,
|
||||
@ -7966,7 +7985,6 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
{}
|
||||
};
|
||||
|
||||
const char *p;
|
||||
int c, r;
|
||||
|
||||
assert(argc >= 0);
|
||||
@ -7986,6 +8004,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
return version();
|
||||
|
||||
case 't': {
|
||||
const char *p;
|
||||
|
||||
if (isempty(optarg))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"--type= requires arguments.");
|
||||
@ -8005,9 +8025,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
}
|
||||
|
||||
if (unit_type_from_string(type) >= 0) {
|
||||
if (strv_push(&arg_types, type) < 0)
|
||||
if (strv_consume(&arg_types, TAKE_PTR(type)) < 0)
|
||||
return log_oom();
|
||||
type = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -8016,9 +8035,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
* in --types= too for compatibility
|
||||
* with old versions */
|
||||
if (unit_load_state_from_string(type) >= 0) {
|
||||
if (strv_push(&arg_states, type) < 0)
|
||||
if (strv_consume(&arg_states, TAKE_PTR(type)) < 0)
|
||||
return log_oom();
|
||||
type = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -8030,14 +8048,16 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
break;
|
||||
}
|
||||
|
||||
case 'p': {
|
||||
/* Make sure that if the empty property list
|
||||
was specified, we won't show any properties. */
|
||||
case 'p':
|
||||
/* Make sure that if the empty property list was specified, we won't show any
|
||||
properties. */
|
||||
if (isempty(optarg) && !arg_properties) {
|
||||
arg_properties = new0(char*, 1);
|
||||
if (!arg_properties)
|
||||
return log_oom();
|
||||
} else
|
||||
} else {
|
||||
const char *p;
|
||||
|
||||
for (p = optarg;;) {
|
||||
_cleanup_free_ char *prop = NULL;
|
||||
|
||||
@ -8052,6 +8072,7 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
|
||||
prop = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* If the user asked for a particular
|
||||
* property, show it to him, even if it is
|
||||
@ -8059,7 +8080,6 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
arg_all = true;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 'a':
|
||||
arg_all = true;
|
||||
@ -8256,6 +8276,8 @@ static int systemctl_parse_argv(int argc, char *argv[]) {
|
||||
break;
|
||||
|
||||
case ARG_STATE: {
|
||||
const char *p;
|
||||
|
||||
if (isempty(optarg))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
"--state= requires arguments.");
|
||||
|
Loading…
x
Reference in New Issue
Block a user