From: Lennart Poettering Date: Fri, 19 Dec 2014 19:43:18 +0000 (+0100) Subject: machined: add a full bus object for images X-Git-Tag: v219~895 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=ebeccf9eecf5939a2ef772c3160e89efcad96194 machined: add a full bus object for images --- diff --git a/Makefile.am b/Makefile.am index 840b8713d..9edc23571 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5039,7 +5039,8 @@ libsystemd_machine_core_la_SOURCES = \ src/machine/image.c \ src/machine/image.h \ src/machine/machined-dbus.c \ - src/machine/machine-dbus.c + src/machine/machine-dbus.c \ + src/machine/image-dbus.c libsystemd_machine_core_la_LIBADD = \ libsystemd-label.la \ diff --git a/src/machine/image-dbus.c b/src/machine/image-dbus.c new file mode 100644 index 000000000..afb849b41 --- /dev/null +++ b/src/machine/image-dbus.c @@ -0,0 +1,234 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2014 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include "bus-label.h" +#include "bus-common-errors.h" +#include "strv.h" +#include "image.h" + +static int image_find_by_bus_path(const char *path, Image **ret) { + _cleanup_free_ char *e = NULL; + const char *p; + + assert(path); + + p = startswith(path, "/org/freedesktop/machine1/image/"); + if (!p) + return 0; + + e = bus_label_unescape(p); + if (!e) + return -ENOMEM; + + return image_find(e, ret); +} + +static int image_find_by_bus_path_with_error(const char *path, Image **ret, sd_bus_error *error) { + int r; + + assert(path); + + r = image_find_by_bus_path(path, ret); + if (r == 0) + return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "Image doesn't exist."); + + return r; +} + +static int property_get_name( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + _cleanup_(image_unrefp) Image *image = NULL; + int r; + + assert(bus); + assert(reply); + + r = image_find_by_bus_path_with_error(path, &image, error); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "s", image->name); + if (r < 0) + return r; + + return 1; +} + +static int property_get_path( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + _cleanup_(image_unrefp) Image *image = NULL; + int r; + + assert(bus); + assert(reply); + + r = image_find_by_bus_path_with_error(path, &image, error); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "s", image->path); + if (r < 0) + return r; + + return 1; +} + +static int property_get_type( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + + _cleanup_(image_unrefp) Image *image = NULL; + int r; + + assert(bus); + assert(reply); + + r = image_find_by_bus_path_with_error(path, &image, error); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "s", image_type_to_string(image->type)); + if (r < 0) + return r; + + return 1; +} + +static int property_get_read_only( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + + _cleanup_(image_unrefp) Image *image = NULL; + int r; + + assert(bus); + assert(reply); + + r = image_find_by_bus_path_with_error(path, &image, error); + if (r < 0) + return r; + + r = sd_bus_message_append(reply, "b", image->read_only); + if (r < 0) + return r; + + return 1; +} + +const sd_bus_vtable image_vtable[] = { + SD_BUS_VTABLE_START(0), + SD_BUS_PROPERTY("Name", "s", property_get_name, 0, 0), + SD_BUS_PROPERTY("Path", "s", property_get_path, 0, 0), + SD_BUS_PROPERTY("Type", "s", property_get_type, 0, 0), + SD_BUS_PROPERTY("ReadOnly", "b", property_get_read_only, 0, 0), + SD_BUS_VTABLE_END +}; + +int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error) { + int r; + + assert(bus); + assert(path); + assert(interface); + assert(found); + + r = image_find_by_bus_path(path, NULL); + if (r <= 0) + return r; + + *found = NULL; + return 1; +} + +char *image_bus_path(const char *name) { + _cleanup_free_ char *e = NULL; + + assert(name); + + e = bus_label_escape(name); + if (!e) + return NULL; + + return strappend("/org/freedesktop/machine1/image/", e); +} + +int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { + _cleanup_(image_hashmap_freep) Hashmap *images = NULL; + _cleanup_strv_free_ char **l = NULL; + Image *image; + Iterator i; + int r; + + assert(bus); + assert(path); + assert(nodes); + + images = hashmap_new(&string_hash_ops); + if (!images) + return -ENOMEM; + + r = image_discover(images); + if (r < 0) + return r; + + HASHMAP_FOREACH(image, images, i) { + char *p; + + p = image_bus_path(image->name); + if (!p) + return -ENOMEM; + + r = strv_consume(&l, p); + if (r < 0) + return r; + } + + *nodes = l; + l = NULL; + + return 1; +} diff --git a/src/machine/image.c b/src/machine/image.c index 8f577adb5..c81c5f628 100644 --- a/src/machine/image.c +++ b/src/machine/image.c @@ -24,8 +24,8 @@ #include "strv.h" #include "utf8.h" #include "btrfs-util.h" +#include "path-util.h" #include "image.h" -#include "bus-label.h" static const char image_search_path[] = "/var/lib/container\0" @@ -71,9 +71,11 @@ static int image_new( return -ENOMEM; if (path) { - i->path = strdup(path); + i->path = strjoin(path, "/", name, NULL); if (!i->path) return -ENOMEM; + + path_kill_slashes(i->path); } *ret = i; @@ -264,18 +266,6 @@ void image_hashmap_free(Hashmap *map) { hashmap_free(map); } -char *image_bus_path(const char *name) { - _cleanup_free_ char *e = NULL; - - assert(name); - - e = bus_label_escape(name); - if (!e) - return NULL; - - return strappend("/org/freedesktop/machine1/image/", e); -} - static const char* const image_type_table[_IMAGE_TYPE_MAX] = { [IMAGE_DIRECTORY] = "directory", [IMAGE_SUBVOLUME] = "subvolume", diff --git a/src/machine/image.h b/src/machine/image.h index f298fc304..f04be239d 100644 --- a/src/machine/image.h +++ b/src/machine/image.h @@ -5,7 +5,7 @@ /*** This file is part of systemd. - Copyright 2013 Lennart Poettering + Copyright 2014 Lennart Poettering systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -23,6 +23,7 @@ #include "time-util.h" #include "hashmap.h" +#include "machined.h" typedef enum ImageType { IMAGE_DIRECTORY, @@ -43,16 +44,20 @@ typedef struct Image { } Image; Image *image_unref(Image *i); - void image_hashmap_free(Hashmap *map); +DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref); +DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free); + int image_find(const char *name, Image **ret); int image_discover(Hashmap *map); +extern const sd_bus_vtable image_vtable[]; + char *image_bus_path(const char *name); -DEFINE_TRIVIAL_CLEANUP_FUNC(Image*, image_unref); -DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, image_hashmap_free); +int image_object_find(sd_bus *bus, const char *path, const char *interface, void *userdata, void **found, sd_bus_error *error); +int image_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error); const char* image_type_to_string(ImageType t) _const_; ImageType image_type_from_string(const char *s) _pure_; diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 163c73d45..f6fd9cf36 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -32,7 +32,6 @@ #include "fileio.h" #include "in-addr-util.h" #include "local-addresses.h" -#include "image.h" #include "machine.h" static int property_get_id( @@ -476,11 +475,9 @@ char *machine_bus_path(Machine *m) { } int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char ***nodes, sd_bus_error *error) { - _cleanup_(image_hashmap_freep) Hashmap *images = NULL; _cleanup_strv_free_ char **l = NULL; Machine *machine = NULL; Manager *m = userdata; - Image *image; Iterator i; int r; @@ -500,26 +497,6 @@ int machine_node_enumerator(sd_bus *bus, const char *path, void *userdata, char return r; } - images = hashmap_new(&string_hash_ops); - if (!images) - return -ENOMEM; - - r = image_discover(images); - if (r < 0) - return r; - - HASHMAP_FOREACH(image, images, i) { - char *p; - - p = image_bus_path(image->name); - if (!p) - return -ENOMEM; - - r = strv_consume(&l, p); - if (r < 0) - return r; - } - *nodes = l; l = NULL; diff --git a/src/machine/machined.c b/src/machine/machined.c index ef59497fb..82cfcf0fd 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -27,15 +27,14 @@ #include #include "sd-daemon.h" - #include "strv.h" #include "conf-parser.h" #include "cgroup-util.h" #include "mkdir.h" #include "bus-util.h" #include "bus-error.h" -#include "machined.h" #include "label.h" +#include "machined.h" Manager *manager_new(void) { Manager *m; @@ -152,6 +151,14 @@ static int manager_connect_bus(Manager *m) { if (r < 0) return log_error_errno(r, "Failed to add machine enumerator: %m"); + r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/machine1/image", "org.freedesktop.machine1.Image", image_vtable, image_object_find, m); + if (r < 0) + return log_error_errno(r, "Failed to add image object vtable: %m"); + + r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/machine1/image", image_node_enumerator, m); + if (r < 0) + return log_error_errno(r, "Failed to add image enumerator: %m"); + r = sd_bus_add_match(m->bus, NULL, "type='signal'," diff --git a/src/machine/machined.h b/src/machine/machined.h index 2dba303df..ed0bd7020 100644 --- a/src/machine/machined.h +++ b/src/machine/machined.h @@ -33,6 +33,7 @@ typedef struct Manager Manager; #include "machine.h" +#include "image.h" struct Manager { sd_event *event;