1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

shared, systemctl: teach is-enabled to show installation targets

It may be desired by users to know what targets a particular service is
installed into. Improve user friendliness by teaching the is-enabled
command to show such information when used with --full.

This patch makes use of the newly added UnitFileFlags and adds
UNIT_FILE_DRY_RUN flag into it. Since the API had already been modified,
it's now easy to add the new dry-run feature for other commands as
well. As a next step, --dry-run could be added to systemctl, which in
turn might pave the way for a long requested dry-run feature when
running systemctl start.
This commit is contained in:
Jan Synacek 2016-10-20 15:20:11 +02:00
parent b3796dd834
commit 3b3557c410
6 changed files with 145 additions and 19 deletions

View File

@ -233,6 +233,8 @@
of <command>status</command>, <command>list-units</command>,
<command>list-jobs</command>, and
<command>list-timers</command>.</para>
<para>Also, show installation targets in the output of
<command>is-enabled</command>.</para>
</listitem>
</varlistentry>
@ -1136,6 +1138,7 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
exit code of 0 if at least one is enabled, non-zero
otherwise. Prints the current enable status (see table).
To suppress this output, use <option>--quiet</option>.
To show installation targets, use <option>--full</option>.
</para>
<table>

View File

@ -2242,6 +2242,49 @@ static int method_add_dependency_unit_files(sd_bus_message *message, void *userd
return reply_unit_file_changes_and_free(m, message, -1, changes, n_changes);
}
static int method_get_unit_file_links(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
UnitFileChange *changes = NULL;
unsigned n_changes = 0, i;
UnitFileFlags flags;
const char *name;
char **p;
int runtime, r;
r = sd_bus_message_read(message, "sb", &name, &runtime);
if (r < 0)
return r;
r = sd_bus_message_new_method_return(message, &reply);
if (r < 0)
return r;
r = sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "s");
if (r < 0)
return r;
p = STRV_MAKE(name);
flags = UNIT_FILE_DRY_RUN |
(runtime ? UNIT_FILE_RUNTIME : 0);
r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes);
if (r < 0)
return log_error_errno(r, "Failed to get file links for %s: %m", name);
for (i = 0; i < n_changes; i++)
if (changes[i].type == UNIT_FILE_UNLINK) {
r = sd_bus_message_append(reply, "s", changes[i].path);
if (r < 0)
return r;
}
r = sd_bus_message_close_container(reply);
if (r < 0)
return r;
return sd_bus_send(NULL, reply, NULL);
}
const sd_bus_vtable bus_manager_vtable[] = {
SD_BUS_VTABLE_START(0),
@ -2387,6 +2430,7 @@ const sd_bus_vtable bus_manager_vtable[] = {
SD_BUS_METHOD("GetDefaultTarget", NULL, "s", method_get_default_target, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("PresetAllUnitFiles", "sbb", "a(sss)", method_preset_all_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("AddDependencyUnitFiles", "asssbb", "a(sss)", method_add_dependency_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("GetUnitFileLinks", "sb", "as", method_get_unit_file_links, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("SetExitCode", "y", NULL, method_set_exit_code, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("LookupDynamicUserByName", "s", "u", method_lookup_dynamic_user_by_name, SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD("LookupDynamicUserByUID", "u", "s", method_lookup_dynamic_user_by_uid, SD_BUS_VTABLE_UNPRIVILEGED),

View File

@ -92,6 +92,10 @@
send_interface="org.freedesktop.systemd1.Manager"
send_member="GetUnitProcesses"/>
<allow send_destination="org.freedesktop.systemd1"
send_interface="org.freedesktop.systemd1.Manager"
send_member="GetUnitFileLinks"/>
<allow send_destination="org.freedesktop.systemd1"
send_interface="org.freedesktop.systemd1.Manager"
send_member="ListJobs"/>

View File

@ -518,6 +518,7 @@ static int remove_marked_symlinks_fd(
const char *path,
const char *config_path,
const LookupPaths *lp,
bool dry_run,
bool *restart,
UnitFileChange **changes,
unsigned *n_changes) {
@ -566,7 +567,7 @@ static int remove_marked_symlinks_fd(
}
/* This will close nfd, regardless whether it succeeds or not */
q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, lp, restart, changes, n_changes);
q = remove_marked_symlinks_fd(remove_symlinks_to, nfd, p, config_path, lp, dry_run, restart, changes, n_changes);
if (q < 0 && r == 0)
r = q;
@ -603,14 +604,16 @@ static int remove_marked_symlinks_fd(
if (!found)
continue;
if (unlinkat(fd, de->d_name, 0) < 0 && errno != ENOENT) {
if (r == 0)
r = -errno;
unit_file_changes_add(changes, n_changes, -errno, p, NULL);
continue;
}
if (!dry_run) {
if (unlinkat(fd, de->d_name, 0) < 0 && errno != ENOENT) {
if (r == 0)
r = -errno;
unit_file_changes_add(changes, n_changes, -errno, p, NULL);
continue;
}
(void) rmdir_parents(p, config_path);
(void) rmdir_parents(p, config_path);
}
unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
@ -621,7 +624,7 @@ static int remove_marked_symlinks_fd(
q = mark_symlink_for_removal(&remove_symlinks_to, rp ?: p);
if (q < 0)
return q;
if (q > 0)
if (q > 0 && !dry_run)
*restart = true;
}
}
@ -633,6 +636,7 @@ static int remove_marked_symlinks(
Set *remove_symlinks_to,
const char *config_path,
const LookupPaths *lp,
bool dry_run,
UnitFileChange **changes,
unsigned *n_changes) {
@ -659,7 +663,7 @@ static int remove_marked_symlinks(
return -errno;
/* This takes possession of cfd and closes it */
q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, lp, &restart, changes, n_changes);
q = remove_marked_symlinks_fd(remove_symlinks_to, cfd, config_path, config_path, lp, dry_run, &restart, changes, n_changes);
if (r == 0)
r = q;
} while (restart);
@ -1861,6 +1865,7 @@ int unit_file_unmask(
size_t n_todo = 0, n_allocated = 0;
const char *config_path;
char **i;
bool dry_run;
int r, q;
assert(scope >= 0);
@ -1871,6 +1876,7 @@ int unit_file_unmask(
return r;
config_path = (flags & UNIT_FILE_RUNTIME) ? paths.runtime_config : paths.persistent_config;
dry_run = !!(flags & UNIT_FILE_DRY_RUN);
STRV_FOREACH(i, files) {
_cleanup_free_ char *path = NULL;
@ -1907,7 +1913,7 @@ int unit_file_unmask(
if (!path)
return -ENOMEM;
if (unlink(path) < 0) {
if (!dry_run && unlink(path) < 0) {
if (errno != ENOENT) {
if (r >= 0)
r = -errno;
@ -1925,7 +1931,7 @@ int unit_file_unmask(
return q;
}
q = remove_marked_symlinks(remove_symlinks_to, config_path, &paths, changes, n_changes);
q = remove_marked_symlinks(remove_symlinks_to, config_path, &paths, dry_run, changes, n_changes);
if (r >= 0)
r = q;
@ -2175,11 +2181,11 @@ int unit_file_revert(
return q;
}
q = remove_marked_symlinks(remove_symlinks_to, paths.runtime_config, &paths, changes, n_changes);
q = remove_marked_symlinks(remove_symlinks_to, paths.runtime_config, &paths, false, changes, n_changes);
if (r >= 0)
r = q;
q = remove_marked_symlinks(remove_symlinks_to, paths.persistent_config, &paths, changes, n_changes);
q = remove_marked_symlinks(remove_symlinks_to, paths.persistent_config, &paths, false, changes, n_changes);
if (r >= 0)
r = q;
@ -2341,7 +2347,7 @@ int unit_file_disable(
if (r < 0)
return r;
return remove_marked_symlinks(remove_symlinks_to, config_path, &paths, changes, n_changes);
return remove_marked_symlinks(remove_symlinks_to, config_path, &paths, !!(flags & UNIT_FILE_DRY_RUN), changes, n_changes);
}
int unit_file_reenable(
@ -2730,7 +2736,7 @@ static int execute_preset(
if (r < 0)
return r;
r = remove_marked_symlinks(remove_symlinks_to, config_path, paths, changes, n_changes);
r = remove_marked_symlinks(remove_symlinks_to, config_path, paths, false, changes, n_changes);
} else
r = 0;

View File

@ -82,6 +82,7 @@ enum UnitFileChangeType {
enum UnitFileFlags {
UNIT_FILE_RUNTIME = 1,
UNIT_FILE_FORCE = 1 << 1,
UNIT_FILE_DRY_RUN = 1 << 2,
};
/* type can either one of the UnitFileChangeTypes listed above, or a negative error.

View File

@ -6271,6 +6271,63 @@ finish:
return r;
}
static int show_installation_targets_client_side(const char *name) {
UnitFileChange *changes = NULL;
unsigned n_changes = 0, i;
UnitFileFlags flags;
char **p;
int r;
p = STRV_MAKE(name);
flags = UNIT_FILE_DRY_RUN |
(arg_runtime ? UNIT_FILE_RUNTIME : 0);
r = unit_file_disable(UNIT_FILE_SYSTEM, flags, NULL, p, &changes, &n_changes);
if (r < 0)
return log_error_errno(r, "Failed to get file links for %s: %m", name);
for (i = 0; i < n_changes; i++)
if (changes[i].type == UNIT_FILE_UNLINK)
printf(" %s\n", changes[i].path);
return 0;
}
static int show_installation_targets(sd_bus *bus, const char *name) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
const char *link;
int r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"GetUnitFileLinks",
&error,
&reply,
"sb", name, arg_runtime);
if (r < 0)
return log_error_errno(r, "Failed to get unit file links for %s: %s", name, bus_error_message(&error, r));
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "s");
if (r < 0)
return bus_log_parse_error(r);
while ((r = sd_bus_message_read(reply, "s", &link)) > 0)
printf(" %s\n", link);
if (r < 0)
return bus_log_parse_error(r);
r = sd_bus_message_exit_container(reply);
if (r < 0)
return bus_log_parse_error(r);
return 0;
}
static int unit_is_enabled(int argc, char *argv[], void *userdata) {
_cleanup_strv_free_ char **names = NULL;
@ -6289,7 +6346,6 @@ static int unit_is_enabled(int argc, char *argv[], void *userdata) {
enabled = r > 0;
if (install_client_side()) {
STRV_FOREACH(name, names) {
UnitFileState state;
@ -6305,8 +6361,14 @@ static int unit_is_enabled(int argc, char *argv[], void *userdata) {
UNIT_FILE_GENERATED))
enabled = true;
if (!arg_quiet)
if (!arg_quiet) {
puts(unit_file_state_to_string(state));
if (arg_full) {
r = show_installation_targets_client_side(*name);
if (r < 0)
return r;
}
}
}
r = 0;
@ -6341,8 +6403,14 @@ static int unit_is_enabled(int argc, char *argv[], void *userdata) {
if (STR_IN_SET(s, "enabled", "enabled-runtime", "static", "indirect", "generated"))
enabled = true;
if (!arg_quiet)
if (!arg_quiet) {
puts(s);
if (arg_full) {
r = show_installation_targets(bus, *name);
if (r < 0)
return r;
}
}
}
}