chiark / gitweb /
unit: add new dependency type RequiresMountsFor=
authorLennart Poettering <lennart@poettering.net>
Sun, 29 Apr 2012 12:26:07 +0000 (14:26 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 30 Apr 2012 08:52:07 +0000 (10:52 +0200)
RequiresMountsFor= is a shortcut for adding requires and after
dependencies to all mount units neeed for the specified paths.

This solves a couple of issues regarding dep loop cycles for encrypted
swap.

13 files changed:
Makefile.am
man/systemd.unit.xml
src/core/dbus-unit.c
src/core/dbus-unit.h
src/core/load-fragment-gperf.gperf.m4
src/core/load-fragment.c
src/core/load-fragment.h
src/core/manager.h
src/core/mount.c
src/core/unit.c
src/core/unit.h
units/systemd-random-seed-load.service.in
units/systemd-random-seed-save.service.in

index 41fd773795c846dd9950b4448421500961a054b2..e1501aecf059f8c4ac842c8cc2c8b370a123cad9 100644 (file)
@@ -2889,6 +2889,7 @@ SED_PROCESS = \
                -e 's,@PACKAGE_VERSION\@,$(PACKAGE_VERSION),g' \
                -e 's,@PACKAGE_NAME\@,$(PACKAGE_NAME),g' \
                -e 's,@PACKAGE_URL\@,$(PACKAGE_URL),g' \
                -e 's,@PACKAGE_VERSION\@,$(PACKAGE_VERSION),g' \
                -e 's,@PACKAGE_NAME\@,$(PACKAGE_NAME),g' \
                -e 's,@PACKAGE_URL\@,$(PACKAGE_URL),g' \
+               -e 's,@RANDOM_SEED\@,$(localstatedir)/lib/random-seed,g' \
                -e 's,@prefix\@,$(prefix),g' \
                -e 's,@exec_prefix\@,$(exec_prefix),g' \
                -e 's,@libdir\@,$(libdir),g' \
                -e 's,@prefix\@,$(prefix),g' \
                -e 's,@exec_prefix\@,$(exec_prefix),g' \
                -e 's,@libdir\@,$(libdir),g' \
index 12416fa31760be4093ddaa77fe15cb8a72d7e104..c81c7a30b04fb77c766e11cd7bf151f4dcf88104 100644 (file)
                                 settings.</para></listitem>
                         </varlistentry>
 
                                 settings.</para></listitem>
                         </varlistentry>
 
+                        <varlistentry>
+                                <term><varname>RequiresMountsFor=</varname></term>
+
+                                <listitem><para>Takes a space
+                                separated list of paths. Automatically
+                                adds dependencies of type
+                                <varname>Requires=</varname> and
+                                <varname>After=</varname> for all
+                                mount units required to access the
+                                specified path.</para></listitem>
+                        </varlistentry>
+
                         <varlistentry>
                                 <term><varname>OnFailureIsolate=</varname></term>
 
                         <varlistentry>
                                 <term><varname>OnFailureIsolate=</varname></term>
 
index 0484d75829d985b7de01c7fb5818c9454e910fbb..b8d661698b3531d4c343f076097d32f9c02d58f8 100644 (file)
@@ -810,6 +810,7 @@ const BusProperty bus_unit_properties[] = {
         { "TriggeredBy",          bus_unit_append_dependencies,      "as", offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]),            true },
         { "PropagateReloadTo",    bus_unit_append_dependencies,      "as", offsetof(Unit, dependencies[UNIT_PROPAGATE_RELOAD_TO]),     true },
         { "PropagateReloadFrom",  bus_unit_append_dependencies,      "as", offsetof(Unit, dependencies[UNIT_PROPAGATE_RELOAD_FROM]),   true },
         { "TriggeredBy",          bus_unit_append_dependencies,      "as", offsetof(Unit, dependencies[UNIT_TRIGGERED_BY]),            true },
         { "PropagateReloadTo",    bus_unit_append_dependencies,      "as", offsetof(Unit, dependencies[UNIT_PROPAGATE_RELOAD_TO]),     true },
         { "PropagateReloadFrom",  bus_unit_append_dependencies,      "as", offsetof(Unit, dependencies[UNIT_PROPAGATE_RELOAD_FROM]),   true },
+        { "RequiresMountsFor",    bus_property_append_strv,          "as", offsetof(Unit, requires_mounts_for),                        true },
         { "Description",          bus_unit_append_description,        "s", 0 },
         { "LoadState",            bus_unit_append_load_state,         "s", offsetof(Unit, load_state)                         },
         { "ActiveState",          bus_unit_append_active_state,       "s", 0 },
         { "Description",          bus_unit_append_description,        "s", 0 },
         { "LoadState",            bus_unit_append_load_state,         "s", offsetof(Unit, load_state)                         },
         { "ActiveState",          bus_unit_append_active_state,       "s", 0 },
index e6d549c0fbba77fc92ba5fbdf0fcb838dc2df028..d22802d27440d71616ccc76456b980ff75decf1f 100644 (file)
@@ -85,6 +85,7 @@
         "  <property name=\"TriggeredBy\" type=\"as\" access=\"read\"/>\n"    \
         "  <property name=\"PropagateReloadTo\" type=\"as\" access=\"read\"/>\n" \
         "  <property name=\"PropagateReloadFrom\" type=\"as\" access=\"read\"/>\n" \
         "  <property name=\"TriggeredBy\" type=\"as\" access=\"read\"/>\n"    \
         "  <property name=\"PropagateReloadTo\" type=\"as\" access=\"read\"/>\n" \
         "  <property name=\"PropagateReloadFrom\" type=\"as\" access=\"read\"/>\n" \
+        "  <property name=\"RequiresMountsFor\" type=\"as\" access=\"read\"/>\n" \
         "  <property name=\"Description\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"LoadState\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"ActiveState\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"Description\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"LoadState\" type=\"s\" access=\"read\"/>\n" \
         "  <property name=\"ActiveState\" type=\"s\" access=\"read\"/>\n" \
index c65db30fff8b96ee39c370ddeb640be8ec6cf43f..d9292738773443191f8403d53ad43e9b8c570d16 100644 (file)
@@ -104,6 +104,7 @@ Unit.After,                      config_parse_unit_deps,             UNIT_AFTER,
 Unit.OnFailure,                  config_parse_unit_deps,             UNIT_ON_FAILURE,               0
 Unit.PropagateReloadTo,          config_parse_unit_deps,             UNIT_PROPAGATE_RELOAD_TO,      0
 Unit.PropagateReloadFrom,        config_parse_unit_deps,             UNIT_PROPAGATE_RELOAD_FROM,    0
 Unit.OnFailure,                  config_parse_unit_deps,             UNIT_ON_FAILURE,               0
 Unit.PropagateReloadTo,          config_parse_unit_deps,             UNIT_PROPAGATE_RELOAD_TO,      0
 Unit.PropagateReloadFrom,        config_parse_unit_deps,             UNIT_PROPAGATE_RELOAD_FROM,    0
+Unit.RequiresMountsFor,          config_parse_unit_requires_mounts_for, 0,                          offsetof(Unit, requires_mounts_for)
 Unit.StopWhenUnneeded,           config_parse_bool,                  0,                             offsetof(Unit, stop_when_unneeded)
 Unit.RefuseManualStart,          config_parse_bool,                  0,                             offsetof(Unit, refuse_manual_start)
 Unit.RefuseManualStop,           config_parse_bool,                  0,                             offsetof(Unit, refuse_manual_stop)
 Unit.StopWhenUnneeded,           config_parse_bool,                  0,                             offsetof(Unit, stop_when_unneeded)
 Unit.RefuseManualStart,          config_parse_bool,                  0,                             offsetof(Unit, refuse_manual_start)
 Unit.RefuseManualStop,           config_parse_bool,                  0,                             offsetof(Unit, refuse_manual_stop)
index 1665be82a0276f0923b1f3103b0d9eb188724980..d24919f998fb0fca4af5d5cb942bced68ce0499f 100644 (file)
@@ -2032,6 +2032,35 @@ int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const
         return 0;
 }
 
         return 0;
 }
 
+int config_parse_unit_requires_mounts_for(
+                const char *filename,
+                unsigned line,
+                const char *section,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        Unit *u = userdata;
+        int r;
+        bool empty_before;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        empty_before = !u->requires_mounts_for;
+
+        r = config_parse_path_strv(filename, line, section, lvalue, ltype, rvalue, data, userdata);
+
+        /* Make it easy to find units with requires_mounts set */
+        if (empty_before && u->requires_mounts_for)
+                LIST_PREPEND(Unit, has_requires_mounts_for, u->manager->has_requires_mounts_for, u);
+
+        return r;
+}
 
 #define FOLLOW_MAX 8
 
 
 #define FOLLOW_MAX 8
 
@@ -2394,6 +2423,7 @@ void unit_dump_config_items(FILE *f) {
                 { config_parse_socket_bindtodevice,   "NETWORKINTERFACE" },
                 { config_parse_usec,                  "SECONDS" },
                 { config_parse_path_strv,             "PATH [...]" },
                 { config_parse_socket_bindtodevice,   "NETWORKINTERFACE" },
                 { config_parse_usec,                  "SECONDS" },
                 { config_parse_path_strv,             "PATH [...]" },
+                { config_parse_unit_requires_mounts_for, "PATH [...]" },
                 { config_parse_exec_mount_flags,      "MOUNTFLAG [...]" },
                 { config_parse_unit_string_printf,    "STRING" },
                 { config_parse_timer,                 "TIMER" },
                 { config_parse_exec_mount_flags,      "MOUNTFLAG [...]" },
                 { config_parse_unit_string_printf,    "STRING" },
                 { config_parse_timer,                 "TIMER" },
index 03e13a9c8c3a01d1ff26db5d16e7d2f0aec4b1f4..ccc436420e8d26a7ed4349636bda782d95806265 100644 (file)
@@ -83,6 +83,7 @@ int config_parse_unit_memory_limit(const char *filename, unsigned line, const ch
 int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_unit_requires_mounts_for(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 
 /* gperf prototypes */
 const struct ConfigPerfItem* load_fragment_gperf_lookup(const char *key, unsigned length);
 
 /* gperf prototypes */
 const struct ConfigPerfItem* load_fragment_gperf_lookup(const char *key, unsigned length);
index 154c1faeb7289797f810ce4cd1c53a62f4205591..6d1f5d86c2a5be2a08978b7b889bf3e71de2f67f 100644 (file)
@@ -104,6 +104,9 @@ struct Manager {
          * type we maintain a per type linked list */
         LIST_HEAD(Unit, units_by_type[_UNIT_TYPE_MAX]);
 
          * type we maintain a per type linked list */
         LIST_HEAD(Unit, units_by_type[_UNIT_TYPE_MAX]);
 
+        /* To optimize iteration of units that have requires_mounts_for set */
+        LIST_HEAD(Unit, has_requires_mounts_for);
+
         /* Units that need to be loaded */
         LIST_HEAD(Unit, load_queue); /* this is actually more a stack than a queue, but uh. */
 
         /* Units that need to be loaded */
         LIST_HEAD(Unit, load_queue); /* this is actually more a stack than a queue, but uh. */
 
index 3357b7df5b26a3662287db3da2f1327fecd12e88..5f50d9557a0442ef2fb6671fc289eba41e471e73 100644 (file)
@@ -263,6 +263,21 @@ static int mount_add_socket_links(Mount *m) {
         return 0;
 }
 
         return 0;
 }
 
+static int mount_add_requires_mounts_links(Mount *m) {
+        Unit *other;
+        int r;
+
+        assert(m);
+
+        LIST_FOREACH(has_requires_mounts_for, other, UNIT(m)->manager->has_requires_mounts_for) {
+                r = unit_add_one_mount_link(other, m);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 static char* mount_test_option(const char *haystack, const char *needle) {
         struct mntent me;
 
 static char* mount_test_option(const char *haystack, const char *needle) {
         struct mntent me;
 
@@ -614,6 +629,10 @@ static int mount_load(Unit *u) {
                 if ((r = mount_add_path_links(m)) < 0)
                         return r;
 
                 if ((r = mount_add_path_links(m)) < 0)
                         return r;
 
+                r = mount_add_requires_mounts_links(m);
+                if (r < 0)
+                        return r;
+
                 if ((r = mount_add_automount_links(m)) < 0)
                         return r;
 
                 if ((r = mount_add_automount_links(m)) < 0)
                         return r;
 
index 491ba3f33f846996e68b3f4925c80c7f59cd412a..fea75e88fe6fa4dbaafd0ea50588d73ef64c1d37 100644 (file)
@@ -370,6 +370,11 @@ void unit_free(Unit *u) {
         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
                 bidi_set_free(u, u->dependencies[d]);
 
         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
                 bidi_set_free(u, u->dependencies[d]);
 
+        if (u->requires_mounts_for) {
+                LIST_REMOVE(Unit, has_requires_mounts_for, u->manager->has_requires_mounts_for, u);
+                strv_free(u->requires_mounts_for);
+        }
+
         if (u->type != _UNIT_TYPE_INVALID)
                 LIST_REMOVE(Unit, units_by_type, u->manager->units_by_type[u->type], u);
 
         if (u->type != _UNIT_TYPE_INVALID)
                 LIST_REMOVE(Unit, units_by_type, u->manager->units_by_type[u->type], u);
 
@@ -691,6 +696,18 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
                         fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->id);
         }
 
                         fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->id);
         }
 
+        if (!strv_isempty(u->requires_mounts_for)) {
+                char **j;
+
+                fprintf(f,
+                        "%s\tRequiresMountsFor:", prefix);
+
+                STRV_FOREACH(j, u->requires_mounts_for)
+                        fprintf(f, " %s", *j);
+
+                fputs("\n", f);
+        }
+
         if (u->load_state == UNIT_LOADED) {
                 CGroupBonding *b;
                 CGroupAttribute *a;
         if (u->load_state == UNIT_LOADED) {
                 CGroupBonding *b;
                 CGroupAttribute *a;
@@ -869,6 +886,12 @@ int unit_load(Unit *u) {
                 if ((r = unit_add_default_dependencies(u)) < 0)
                         goto fail;
 
                 if ((r = unit_add_default_dependencies(u)) < 0)
                         goto fail;
 
+        if (u->load_state == UNIT_LOADED) {
+                r = unit_add_mount_links(u);
+                if (r < 0)
+                        return r;
+        }
+
         if (u->on_failure_isolate &&
             set_size(u->dependencies[UNIT_ON_FAILURE]) > 1) {
 
         if (u->on_failure_isolate &&
             set_size(u->dependencies[UNIT_ON_FAILURE]) > 1) {
 
@@ -2685,6 +2708,45 @@ void unit_ref_unset(UnitRef *ref) {
         ref->unit = NULL;
 }
 
         ref->unit = NULL;
 }
 
+int unit_add_one_mount_link(Unit *u, Mount *m) {
+        char **i;
+
+        assert(u);
+        assert(m);
+
+        if (u->load_state != UNIT_LOADED ||
+            UNIT(m)->load_state != UNIT_LOADED)
+                return 0;
+
+        STRV_FOREACH(i, u->requires_mounts_for) {
+
+                if (UNIT(m) == u)
+                        continue;
+
+                if (!path_startswith(*i, m->where))
+                        continue;
+
+                return unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, UNIT(m), true);
+        }
+
+        return 0;
+}
+
+int unit_add_mount_links(Unit *u) {
+        Unit *other;
+        int r;
+
+        assert(u);
+
+        LIST_FOREACH(units_by_type, other, u->manager->units_by_type[UNIT_MOUNT]) {
+                r = unit_add_one_mount_link(u, MOUNT(other));
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
         [UNIT_STUB] = "stub",
         [UNIT_LOADED] = "loaded",
 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
         [UNIT_STUB] = "stub",
         [UNIT_LOADED] = "loaded",
index c2bae0a45c677954724f3e0802faa114938747f8..33920892fdc0942136c897017849b188cf3289f9 100644 (file)
@@ -153,6 +153,8 @@ struct Unit {
         Set *names;
         Set *dependencies[_UNIT_DEPENDENCY_MAX];
 
         Set *names;
         Set *dependencies[_UNIT_DEPENDENCY_MAX];
 
+        char **requires_mounts_for;
+
         char *description;
 
         char *fragment_path; /* if loaded from a config file this is the primary path to it */
         char *description;
 
         char *fragment_path; /* if loaded from a config file this is the primary path to it */
@@ -186,6 +188,9 @@ struct Unit {
         /* Per type list */
         LIST_FIELDS(Unit, units_by_type);
 
         /* Per type list */
         LIST_FIELDS(Unit, units_by_type);
 
+        /* All units which have requires_mounts_for set */
+        LIST_FIELDS(Unit, has_requires_mounts_for);
+
         /* Load queue */
         LIST_FIELDS(Unit, load_queue);
 
         /* Load queue */
         LIST_FIELDS(Unit, load_queue);
 
@@ -554,6 +559,9 @@ void unit_ref_unset(UnitRef *ref);
 
 #define UNIT_DEREF(ref) ((ref).unit)
 
 
 #define UNIT_DEREF(ref) ((ref).unit)
 
+int unit_add_one_mount_link(Unit *u, Mount *m);
+int unit_add_mount_links(Unit *u);
+
 const char *unit_load_state_to_string(UnitLoadState i);
 UnitLoadState unit_load_state_from_string(const char *s);
 
 const char *unit_load_state_to_string(UnitLoadState i);
 UnitLoadState unit_load_state_from_string(const char *s);
 
index 6360436021fd688026138d37d3c02e0e9f430524..82f49c762b1f1a57dd8eda2530259cd96a526cc2 100644 (file)
@@ -8,10 +8,10 @@
 [Unit]
 Description=Load Random Seed
 DefaultDependencies=no
 [Unit]
 Description=Load Random Seed
 DefaultDependencies=no
-Wants=local-fs.target
 Conflicts=shutdown.target
 Conflicts=shutdown.target
-After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target
+After=systemd-readahead-collect.service systemd-readahead-replay.service
 Before=sysinit.target shutdown.target
 Before=sysinit.target shutdown.target
+RequiresMountsFor=@RANDOM_SEED@
 
 [Service]
 Type=oneshot
 
 [Service]
 Type=oneshot
index 219731bae55c9a9da4be35f45af5a092cfa9c51e..0ebd07d868427acb00229d76b9eac556804ae7bc 100644 (file)
@@ -11,6 +11,7 @@ DefaultDependencies=no
 After=systemd-random-seed-load.service
 Before=shutdown.target
 Conflicts=systemd-random-seed-load.service
 After=systemd-random-seed-load.service
 Before=shutdown.target
 Conflicts=systemd-random-seed-load.service
+RequiresMountsFor=@RANDOM_SEED@
 
 [Service]
 Type=oneshot
 
 [Service]
 Type=oneshot