]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/path: implement transient path unit
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 11 Dec 2017 15:10:58 +0000 (00:10 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 15 Dec 2017 00:10:34 +0000 (09:10 +0900)
src/core/dbus-path.c
src/core/dbus-path.h
src/core/path.c

index 0f54b04f76cae2bb7d43e258e8bff6f4b5f6059a..d9cf212eb7afe1a3b60ac563c87b4fa58fd9cfe1 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "alloc-util.h"
 #include "bus-util.h"
 #include "dbus-path.h"
+#include "list.h"
 #include "path.h"
+#include "path-util.h"
 #include "string-util.h"
 #include "unit.h"
 
@@ -85,3 +88,119 @@ const sd_bus_vtable bus_path_vtable[] = {
         SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Path, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
         SD_BUS_VTABLE_END
 };
+
+static int bus_path_set_transient_property(
+                Path *p,
+                const char *name,
+                sd_bus_message *message,
+                UnitWriteFlags flags,
+                sd_bus_error *error) {
+
+        Unit *u = UNIT(p);
+        int r;
+
+        assert(p);
+        assert(name);
+        assert(message);
+
+        flags |= UNIT_PRIVATE;
+
+        if (STR_IN_SET(name, "PathExists", "PathExistsGlob", "PathChanged", "PathModified", "DirectoryNotEmpty")) {
+                const char *str;
+                PathType b;
+
+                b = path_type_from_string(name);
+                if (b < 0)
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type");
+
+                r = sd_bus_message_read(message, "s", &str);
+                if (r < 0)
+                        return r;
+
+                if (!isempty(str) && !path_is_absolute(str))
+                        return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path is not absolute");
+
+                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+                        if (isempty(str)) {
+                                path_free_specs(p);
+                                unit_write_settingf(u, flags, name, "%s=", name);
+                        } else {
+                                _cleanup_free_ char *k;
+                                PathSpec *s;
+
+                                k = strdup(str);
+                                if (!k)
+                                        return -ENOMEM;
+
+                                s = new0(PathSpec, 1);
+                                if (!s)
+                                        return -ENOMEM;
+
+                                s->unit = u;
+                                s->path = path_kill_slashes(k);
+                                k = NULL;
+                                s->type = b;
+                                s->inotify_fd = -1;
+
+                                LIST_PREPEND(spec, p->specs, s);
+
+                                unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str);
+                        }
+                }
+
+                return 1;
+
+        } else if (streq(name, "MakeDirectory")) {
+                int b;
+
+                r = sd_bus_message_read(message, "b", &b);
+                if (r < 0)
+                        return r;
+
+                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+                        p->make_directory = b;
+                        unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(b));
+                }
+
+                return 1;
+
+        } else if (streq(name, "DirectoryMode")) {
+                mode_t m;
+
+                r = sd_bus_message_read(message, "u", &m);
+                if (r < 0)
+                        return r;
+
+                if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
+                        p->directory_mode = m;
+                        unit_write_settingf(u, flags, name, "%s=%040o", name, m);
+                }
+
+                return 1;
+
+        } else if (streq(name, "Unit")) {
+                /* not implemented yet */
+                return 0;
+        }
+
+        return 0;
+}
+
+int bus_path_set_property(
+                Unit *u,
+                const char *name,
+                sd_bus_message *message,
+                UnitWriteFlags mode,
+                sd_bus_error *error) {
+
+        Path *p = PATH(u);
+
+        assert(p);
+        assert(name);
+        assert(message);
+
+        if (u->transient && u->load_state == UNIT_STUB)
+                return bus_path_set_transient_property(p, name, message, mode, error);
+
+        return 0;
+}
index 5e7e859b569ead2e9237e8d4f36382b205dc0b1c..ccd88c7f867098974b8f5a3618c3172202a3d46e 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "sd-bus.h"
 
+#include "unit.h"
 
 extern const sd_bus_vtable bus_path_vtable[];
+
+int bus_path_set_property(Unit *u, const char *name, sd_bus_message *i, UnitWriteFlags flags, sd_bus_error *error);
index 6b22451a08a6f7c4a691de8e22c090b1b9c1695d..8a5ec0a72f695bd86ab63660b85bb8d3a984a51d 100644 (file)
@@ -772,6 +772,9 @@ const UnitVTable path_vtable = {
                 "Unit\0"
                 "Path\0"
                 "Install\0",
+        .private_section = "Path",
+
+        .can_transient = true,
 
         .init = path_init,
         .done = path_done,
@@ -794,5 +797,6 @@ const UnitVTable path_vtable = {
 
         .reset_failed = path_reset_failed,
 
-        .bus_vtable = bus_path_vtable
+        .bus_vtable = bus_path_vtable,
+        .bus_set_property = bus_path_set_property,
 };