[systemd-devel] [PATCH] systemctl: add add-wants and add-requires verbs

Lukas Nykryn lnykryn at redhat.com
Fri Sep 19 07:33:46 PDT 2014


---
 TODO                                   |  1 -
 man/systemctl.xml                      | 27 ++++++++++
 src/core/dbus-manager.c                | 85 +++++++++++++++++++++++++----
 src/core/org.freedesktop.systemd1.conf |  8 +++
 src/shared/install.c                   | 87 +++++++++++++++++++++++++++---
 src/shared/install.h                   |  8 +++
 src/systemctl/systemctl.c              | 99 +++++++++++++++++++++++++++++++++-
 7 files changed, 295 insertions(+), 20 deletions(-)

diff --git a/TODO b/TODO
index 9ac6fac..923cbb8 100644
--- a/TODO
+++ b/TODO
@@ -450,7 +450,6 @@ Features:
   - "systemctl mask" should find all names by which a unit is accessible
     (i.e. by scanning for symlinks to it) and link them all to /dev/null
   - systemctl list-unit-files should list generated files (and probably with a new state "generated" for them, or so)
-  - systemctl: maybe add "systemctl add-wants" or so...
 
 * timer units:
   - timer units should get the ability to trigger when:
diff --git a/man/systemctl.xml b/man/systemctl.xml
index b28a3b7..73dfa87 100644
--- a/man/systemctl.xml
+++ b/man/systemctl.xml
@@ -1098,6 +1098,33 @@ kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
         </varlistentry>
 
         <varlistentry>
+          <term><command>add-wants|add-requires <replaceable>TARGET</replaceable>
+          <replaceable>NAME</replaceable>...</command>
+          </term>
+
+          <listitem>
+            <para>Enables one or more units similarly to <command>enable</command>,
+            but instead of looking to <literal>[Install]</literal> section of
+            the unit file, adds <literal>Wants</literal>
+            resp. <literal>Requires</literal> dependency to the specified
+            <replaceable>TARGET</replaceable>.
+            </para>
+
+            <para>This command will print the actions executed. This
+            output may be suppressed by passing <option>--quiet</option>.
+            </para>
+
+            <para>Depending on whether <option>--system</option>,
+            <option>--user</option>, <option>--runtime</option>,
+            or <option>--global</option> is specified, this enables the unit
+            for the system, for the calling user only, for only this boot of
+            the system, or for all future logins of all users, or only this
+            boot.  Note that in the last case, no systemd daemon
+            configuration is reloaded.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
           <term><command>link <replaceable>FILENAME</replaceable>...</command></term>
 
           <listitem>
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
index 533ce43..9335e94 100644
--- a/src/core/dbus-manager.c
+++ b/src/core/dbus-manager.c
@@ -1562,9 +1562,6 @@ static int method_enable_unit_files_generic(
                 sd_bus_error *error) {
 
         _cleanup_strv_free_ char **l = NULL;
-#ifdef HAVE_SELINUX
-        char **i;
-#endif
         UnitFileChange *changes = NULL;
         unsigned n_changes = 0;
         UnitFileScope scope;
@@ -1589,14 +1586,17 @@ static int method_enable_unit_files_generic(
                 return r;
 
 #ifdef HAVE_SELINUX
-        STRV_FOREACH(i, l) {
-                Unit *u;
-
-                u = manager_get_unit(m, *i);
-                if (u) {
-                        r = selinux_unit_access_check(u, message, verb, error);
-                        if (r < 0)
-                                return r;
+        {
+                char **i;
+                STRV_FOREACH(i, l) {
+                        Unit *u;
+
+                        u = manager_get_unit(m, *i);
+                        if (u) {
+                                r = selinux_unit_access_check(u, message, verb, error);
+                                if (r < 0)
+                                        return r;
+                        }
                 }
         }
 #endif
@@ -1828,6 +1828,67 @@ static int method_preset_all_unit_files(sd_bus *bus, sd_bus_message *message, vo
         return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
 }
 
+static int method_add_install_dependency_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, UnitFileInstallDependency dep, sd_bus_error *error) {
+        _cleanup_strv_free_ char **l = NULL;
+        Manager *m = userdata;
+        UnitFileChange *changes = NULL;
+        unsigned n_changes = 0;
+        UnitFileScope scope;
+        int runtime, force, r;
+        char *target;
+
+        assert(bus);
+        assert(message);
+        assert(m);
+
+        r = bus_verify_manage_unit_files_async(m, message, error);
+        if (r < 0)
+                return r;
+        if (r == 0)
+                return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
+
+        r = sd_bus_message_read_strv(message, &l);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_read(message, "sbb", &target, &runtime, &force);
+        if (r < 0)
+                return r;
+
+#ifdef HAVE_SELINUX
+        {
+                char **i;
+                STRV_FOREACH(i, l) {
+                        Unit *u;
+
+                        u = manager_get_unit(m, *i);
+                        if (u) {
+                                //for purposes of selinux, this should be same as enabling unit files
+                                r = selinux_unit_access_check(u, message, "enable", error);
+                                if (r < 0)
+                                        return r;
+                        }
+                }
+        }
+#endif
+
+        scope = m->running_as == SYSTEMD_SYSTEM ? UNIT_FILE_SYSTEM : UNIT_FILE_USER;
+
+        r = unit_file_add_install_dependency(scope, runtime, NULL, l, target, dep, force, &changes, &n_changes);
+        if (r < 0)
+                return r;
+
+        return reply_unit_file_changes_and_free(m, bus, message, -1, changes, n_changes);
+}
+
+static int method_add_wants_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        return method_add_install_dependency_unit_files(bus, message, userdata, UNIT_FILE_INSTALL_DEPENDENCY_WANTS, error);
+}
+
+static int method_add_requires_unit_files(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
+        return method_add_install_dependency_unit_files(bus, message, userdata, UNIT_FILE_INSTALL_DEPENDENCY_REQUIRES, error);
+}
+
 const sd_bus_vtable bus_manager_vtable[] = {
         SD_BUS_VTABLE_START(0),
 
@@ -1918,6 +1979,8 @@ const sd_bus_vtable bus_manager_vtable[] = {
         SD_BUS_METHOD("SetDefaultTarget", "sb", "a(sss)", method_set_default_target, SD_BUS_VTABLE_UNPRIVILEGED),
         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("AddWantsUnitFiles", "assbb", "a(sss)", method_add_wants_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("AddRequiresUnitFiles", "assbb", "a(sss)", method_add_requires_unit_files, SD_BUS_VTABLE_UNPRIVILEGED),
 
         SD_BUS_SIGNAL("UnitNew", "so", 0),
         SD_BUS_SIGNAL("UnitRemoved", "so", 0),
diff --git a/src/core/org.freedesktop.systemd1.conf b/src/core/org.freedesktop.systemd1.conf
index 3e13825..0b95fe6 100644
--- a/src/core/org.freedesktop.systemd1.conf
+++ b/src/core/org.freedesktop.systemd1.conf
@@ -199,6 +199,14 @@
                        send_member="PresetAllUnitFiles"/>
 
                 <allow send_destination="org.freedesktop.systemd1"
+                       send_interface="org.freedesktop.systemd1.Manager"
+                       send_member="AddWantsUnitFiles"/>
+
+                <allow send_destination="org.freedesktop.systemd1"
+                       send_interface="org.freedesktop.systemd1.Manager"
+                       send_member="AddRequiresUnitFiles"/>
+
+                <allow send_destination="org.freedesktop.systemd1"
                        send_interface="org.freedesktop.systemd1.Job"
                        send_member="Cancel"/>
 
diff --git a/src/shared/install.c b/src/shared/install.c
index 5d3fcf5..6ff278e 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -1089,7 +1089,8 @@ static int unit_file_search(
                 InstallInfo *info,
                 LookupPaths *paths,
                 const char *root_dir,
-                bool allow_symlink) {
+                bool allow_symlink,
+                bool load) {
 
         char **p;
         int r;
@@ -1110,7 +1111,11 @@ static int unit_file_search(
                 if (!path)
                         return -ENOMEM;
 
-                r = unit_file_load(c, info, path, root_dir, allow_symlink);
+                if (load)
+                        r = unit_file_load(c, info, path, root_dir, allow_symlink);
+                else
+                        r = access(path, F_OK) ? -errno : 0;
+
                 if (r >= 0) {
                         info->path = path;
                         path = NULL;
@@ -1139,7 +1144,11 @@ static int unit_file_search(
                         if (!path)
                                 return -ENOMEM;
 
-                        r = unit_file_load(c, info, path, root_dir, allow_symlink);
+                        if (load)
+                                r = unit_file_load(c, info, path, root_dir, allow_symlink);
+                        else
+                                r = access(path, F_OK) ? -errno : 0;
+
                         if (r >= 0) {
                                 info->path = path;
                                 path = NULL;
@@ -1172,7 +1181,7 @@ static int unit_file_can_install(
 
         assert_se(i = hashmap_first(c.will_install));
 
-        r = unit_file_search(&c, i, paths, root_dir, allow_symlink);
+        r = unit_file_search(&c, i, paths, root_dir, allow_symlink, true);
 
         if (r >= 0)
                 r =
@@ -1399,7 +1408,7 @@ static int install_context_apply(
 
                 assert_se(hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
 
-                q = unit_file_search(c, i, paths, root_dir, false);
+                q = unit_file_search(c, i, paths, root_dir, false, true);
                 if (q < 0) {
                         if (r >= 0)
                                 r = q;
@@ -1440,7 +1449,7 @@ static int install_context_mark_for_removal(
 
                 assert_se(hashmap_move_one(c->have_installed, c->will_install, i->name) == 0);
 
-                q = unit_file_search(c, i, paths, root_dir, false);
+                q = unit_file_search(c, i, paths, root_dir, false, true);
                 if (q == -ENOENT) {
                         /* do nothing */
                 } else if (q < 0) {
@@ -1486,6 +1495,70 @@ static int install_context_mark_for_removal(
         return r;
 }
 
+int unit_file_add_install_dependency(
+                UnitFileScope scope,
+                bool runtime,
+                const char *root_dir,
+                char **files,
+                char *target,
+                UnitFileInstallDependency dep,
+                bool force,
+                UnitFileChange **changes,
+                unsigned *n_changes) {
+
+        _cleanup_lookup_paths_free_ LookupPaths paths = {};
+        _cleanup_(install_context_done) InstallContext c = {};
+        _cleanup_free_ char *config_path = NULL;
+        char **i;
+        int r;
+        InstallInfo *info;
+
+        assert(scope >= 0);
+        assert(scope < _UNIT_FILE_SCOPE_MAX);
+
+        r = lookup_paths_init_from_scope(&paths, scope, root_dir);
+        if (r < 0)
+                return r;
+
+        r = get_config_path(scope, runtime, root_dir, &config_path);
+        if (r < 0)
+                return r;
+
+        STRV_FOREACH(i, files) {
+                r = install_info_add_auto(&c, *i);
+                if (r < 0)
+                        return r;
+        }
+
+        while ((info = hashmap_first(c.will_install))) {
+                r = hashmap_ensure_allocated(&(c.have_installed), &string_hash_ops);
+                if (r < 0)
+                        return r;
+
+                assert_se(hashmap_move_one(c.have_installed, c.will_install, info->name) == 0);
+
+                r = unit_file_search(&c, info, &paths, root_dir, false, false);
+                if (r < 0)
+                        return r;
+
+                if (dep == UNIT_FILE_INSTALL_DEPENDENCY_WANTS)
+                        r = strv_extend(&info->wanted_by, target);
+                else if (dep == UNIT_FILE_INSTALL_DEPENDENCY_REQUIRES)
+                        r = strv_extend(&info->required_by, target);
+                else
+                        r = -EINVAL;
+
+                if (r < 0)
+                        return r;
+
+                r = install_info_apply(info, &paths, config_path, root_dir, force, changes, n_changes);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 int unit_file_enable(
                 UnitFileScope scope,
                 bool runtime,
@@ -1622,7 +1695,7 @@ int unit_file_set_default(
 
         assert_se(i = hashmap_first(c.will_install));
 
-        r = unit_file_search(&c, i, &paths, root_dir, false);
+        r = unit_file_search(&c, i, &paths, root_dir, false, true);
         if (r < 0)
                 return r;
 
diff --git a/src/shared/install.h b/src/shared/install.h
index ff16d9f..9a67495 100644
--- a/src/shared/install.h
+++ b/src/shared/install.h
@@ -53,6 +53,13 @@ typedef enum UnitFilePresetMode {
         _UNIT_FILE_PRESET_INVALID = -1
 } UnitFilePresetMode;
 
+typedef enum UnitFileInstallDependency {
+        UNIT_FILE_INSTALL_DEPENDENCY_WANTS,
+        UNIT_FILE_INSTALL_DEPENDENCY_REQUIRES,
+        _UNIT_FILE_INSTALL_DEPENDENCY_MAX,
+        _UNIT_FILE_INSTALL_DEPENDENCY_INVALID = -1
+} UnitFileInstallDependency;
+
 typedef enum UnitFileChangeType {
         UNIT_FILE_SYMLINK,
         UNIT_FILE_UNLINK,
@@ -93,6 +100,7 @@ int unit_file_mask(UnitFileScope scope, bool runtime, const char *root_dir, char
 int unit_file_unmask(UnitFileScope scope, bool runtime, const char *root_dir, char **files, UnitFileChange **changes, unsigned *n_changes);
 int unit_file_set_default(UnitFileScope scope, const char *root_dir, const char *file, bool force, UnitFileChange **changes, unsigned *n_changes);
 int unit_file_get_default(UnitFileScope scope, const char *root_dir, char **name);
+int unit_file_add_install_dependency(UnitFileScope scope, bool runtime, const char *root_dir, char **files, char *target, UnitFileInstallDependency dep, bool force, UnitFileChange **changes, unsigned *n_changes);
 
 UnitFileState unit_file_get_state(UnitFileScope scope, const char *root_dir, const char *filename);
 
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 88be871..1f45009 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -5289,6 +5289,97 @@ finish:
         return r;
 }
 
+static int add_install_dependency(sd_bus *bus, char **args) {
+        _cleanup_strv_free_ char **names = NULL;
+        _cleanup_free_ char *target = NULL;
+        const char *verb = args[0];
+        int r = 0;
+
+        if (!args[1])
+                return 0;
+
+        target = unit_name_mangle_with_suffix(args[1], MANGLE_NOGLOB, ".target");
+
+        if (!target)
+                return log_oom();
+
+        r = mangle_names(args+2, &names);
+        if (r < 0)
+                return r;
+
+        if (!bus || avoid_bus()) {
+                UnitFileChange *changes = NULL;
+                unsigned n_changes = 0;
+
+                if (streq(verb, "add-wants"))
+                        r = unit_file_add_install_dependency(arg_scope, arg_runtime, arg_root, names, target, UNIT_FILE_INSTALL_DEPENDENCY_WANTS, arg_force, &changes, &n_changes);
+                else if (streq(verb, "add-requires"))
+                        r = unit_file_add_install_dependency(arg_scope, arg_runtime, arg_root, names, target, UNIT_FILE_INSTALL_DEPENDENCY_REQUIRES, arg_force, &changes, &n_changes);
+                else
+                        assert_not_reached("Unknown verb");
+
+                if (!arg_quiet)
+                        dump_unit_file_changes(changes, n_changes);
+
+                unit_file_changes_free(changes, n_changes);
+
+        } else {
+                _cleanup_bus_message_unref_ sd_bus_message *reply = NULL, *m = NULL;
+                _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+                const char *method;
+
+                if (streq(verb, "add-wants"))
+                        method="AddWantsUnitFiles";
+                else if (streq(verb, "add-requires"))
+                        method="AddRequiresUnitFiles";
+                else
+                        assert_not_reached("Unknown verb");
+
+                r = sd_bus_message_new_method_call(
+                                bus,
+                                &m,
+                                "org.freedesktop.systemd1",
+                                "/org/freedesktop/systemd1",
+                                "org.freedesktop.systemd1.Manager",
+                                method);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_append_strv(m, names);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_append(m, "s", target);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_append(m, "b", arg_runtime);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_message_append(m, "b", arg_force);
+                if (r < 0)
+                        return bus_log_create_error(r);
+
+                r = sd_bus_call(bus, m, 0, &error, &reply);
+                if (r < 0) {
+                        log_error("Failed to execute operation: %s", bus_error_message(&error, r));
+                        return r;
+                }
+
+                r = deserialize_and_dump_unit_file_changes(reply);
+                if (r < 0)
+                        return r;
+
+                if (!arg_no_reload)
+                        r = daemon_reload(bus, args);
+                else
+                        r = 0;
+        }
+
+        return r;
+}
+
 static int preset_all(sd_bus *bus, char **args) {
         UnitFileChange *changes = NULL;
         unsigned n_changes = 0;
@@ -5526,7 +5617,6 @@ static void systemctl_help(void) {
                "  disable NAME...                 Disable one or more unit files\n"
                "  reenable NAME...                Reenable one or more unit files\n"
                "  preset NAME...                  Enable/disable one or more unit files\n"
-               "                                  based on preset configuration\n"
                "  preset-all                      Enable/disable all unit files based on\n"
                "                                  preset configuration\n"
                "  is-enabled NAME...              Check whether unit files are enabled\n\n"
@@ -5534,6 +5624,11 @@ static void systemctl_help(void) {
                "  unmask NAME...                  Unmask one or more units\n"
                "  link PATH...                    Link one or more units files into\n"
                "                                  the search path\n"
+               "                                  based on preset configuration\n"
+               "  add-wants TARGET NAME...        Add 'Wants' dependency for the target\n"
+               "                                  on specified one or more units\n"
+               "  add-requires TARGET NAME...     Add 'Requires' dependency for the target\n"
+               "                                  on specified one or more units\n"
                "  get-default                     Get the name of the default target\n"
                "  set-default NAME                Set the default target\n\n"
                "Machine Commands:\n"
@@ -6544,6 +6639,8 @@ static int systemctl_main(sd_bus *bus, int argc, char *argv[], int bus_error) {
                 { "get-default",           EQUAL, 1, get_default,      NOBUS },
                 { "set-property",          MORE,  3, set_property      },
                 { "is-system-running",     EQUAL, 1, is_system_running },
+                { "add-wants",             MORE,  3, add_install_dependency,        NOBUS },
+                { "add-requires",          MORE,  3, add_install_dependency,        NOBUS },
                 {}
         }, *verb = verbs;
 
-- 
1.8.3.1



More information about the systemd-devel mailing list