]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/snapshot.c
util-lib: split string parsing related calls from util.[ch] into parse-util.[ch]
[thirdparty/systemd.git] / src / core / snapshot.c
index 82ec5104dbfa07bd81085e32fefd8ae950864783..da1c99ad12e0f05fcca724ee105784f14173a51c 100644 (file)
@@ -6,26 +6,29 @@
   Copyright 2010 Lennart Poettering
 
   systemd is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
+  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
-  General Public License for more details.
+  Lesser General Public License for more details.
 
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #include <errno.h>
 
-#include "unit.h"
+#include "bus-common-errors.h"
+#include "dbus-snapshot.h"
+#include "parse-util.h"
+#include "parse-util.h"
 #include "snapshot.h"
+#include "string-util.h"
 #include "unit-name.h"
-#include "dbus-snapshot.h"
-#include "bus-errors.h"
+#include "unit.h"
 
 static const UnitActiveState state_translation_table[_SNAPSHOT_STATE_MAX] = {
         [SNAPSHOT_DEAD] = UNIT_INACTIVE,
@@ -40,6 +43,7 @@ static void snapshot_init(Unit *u) {
 
         UNIT(s)->ignore_on_isolate = true;
         UNIT(s)->ignore_on_snapshot = true;
+        UNIT(s)->allow_isolate = true;
 }
 
 static void snapshot_set_state(Snapshot *s, SnapshotState state) {
@@ -50,10 +54,7 @@ static void snapshot_set_state(Snapshot *s, SnapshotState state) {
         s->state = state;
 
         if (state != old_state)
-                log_debug("%s changed %s -> %s",
-                          UNIT(s)->id,
-                          snapshot_state_to_string(old_state),
-                          snapshot_state_to_string(state));
+                log_unit_debug(UNIT(s), "Changed %s -> %s", snapshot_state_to_string(old_state), snapshot_state_to_string(state));
 
         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
 }
@@ -66,7 +67,7 @@ static int snapshot_load(Unit *u) {
 
         /* Make sure that only snapshots created via snapshot_create()
          * can be loaded */
-        if (!s->by_snapshot_create && UNIT(s)->manager->n_reloading <= 0)
+        if (!u->transient && UNIT(s)->manager->n_reloading <= 0)
                 return -ENOENT;
 
         u->load_state = UNIT_LOADED;
@@ -109,7 +110,7 @@ static int snapshot_start(Unit *u) {
         if (s->cleanup)
                 unit_add_to_cleanup_queue(u);
 
-        return 0;
+        return 1;
 }
 
 static int snapshot_stop(Unit *u) {
@@ -119,7 +120,7 @@ static int snapshot_stop(Unit *u) {
         assert(s->state == SNAPSHOT_ACTIVE);
 
         snapshot_set_state(s, SNAPSHOT_DEAD);
-        return 0;
+        return 1;
 }
 
 static int snapshot_serialize(Unit *u, FILE *f, FDSet *fds) {
@@ -151,44 +152,47 @@ static int snapshot_deserialize_item(Unit *u, const char *key, const char *value
         if (streq(key, "state")) {
                 SnapshotState state;
 
-                if ((state = snapshot_state_from_string(value)) < 0)
-                        log_debug("Failed to parse state value %s", value);
+                state = snapshot_state_from_string(value);
+                if (state < 0)
+                        log_unit_debug(u, "Failed to parse state value: %s", value);
                 else
                         s->deserialized_state = state;
 
         } else if (streq(key, "cleanup")) {
 
-                if ((r = parse_boolean(value)) < 0)
-                        log_debug("Failed to parse cleanup value %s", value);
+                r = parse_boolean(value);
+                if (r < 0)
+                        log_unit_debug(u, "Failed to parse cleanup value: %s", value);
                 else
                         s->cleanup = r;
 
         } else if (streq(key, "wants")) {
 
-                if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, value, NULL, true)) < 0)
+                r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_WANTS, value, NULL, true);
+                if (r < 0)
                         return r;
         } else
-                log_debug("Unknown serialization key '%s'", key);
+                log_unit_debug(u, "Unknown serialization key: %s", key);
 
         return 0;
 }
 
-static UnitActiveState snapshot_active_state(Unit *u) {
+_pure_ static UnitActiveState snapshot_active_state(Unit *u) {
         assert(u);
 
         return state_translation_table[SNAPSHOT(u)->state];
 }
 
-static const char *snapshot_sub_state_to_string(Unit *u) {
+_pure_ static const char *snapshot_sub_state_to_string(Unit *u) {
         assert(u);
 
         return snapshot_state_to_string(SNAPSHOT(u)->state);
 }
 
-int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Snapshot **_s) {
-        Iterator i;
+int snapshot_create(Manager *m, const char *name, bool cleanup, sd_bus_error *e, Snapshot **_s) {
+        _cleanup_free_ char *n = NULL;
         Unit *other, *u = NULL;
-        char *n = NULL;
+        Iterator i;
         int r;
         const char *k;
 
@@ -196,20 +200,14 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Sn
         assert(_s);
 
         if (name) {
-                if (!unit_name_is_valid(name, false)) {
-                        dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
-                        return -EINVAL;
-                }
+                if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
+                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
 
-                if (unit_name_to_type(name) != UNIT_SNAPSHOT) {
-                        dbus_set_error(e, BUS_ERROR_UNIT_TYPE_MISMATCH, "Unit name %s lacks snapshot suffix.", name);
-                        return -EINVAL;
-                }
+                if (!endswith(name, ".snapshot"))
+                        return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s lacks snapshot suffix.", name);
 
-                if (manager_get_unit(m, name)) {
-                        dbus_set_error(e, BUS_ERROR_UNIT_EXISTS, "Snapshot %s exists already.", name);
-                        return -EEXIST;
-                }
+                if (manager_get_unit(m, name))
+                        return sd_bus_error_setf(e, BUS_ERROR_UNIT_EXISTS, "Snapshot %s exists already.", name);
 
         } else {
 
@@ -217,28 +215,27 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Sn
                         if (asprintf(&n, "snapshot-%u.snapshot", ++ m->n_snapshots) < 0)
                                 return -ENOMEM;
 
-                        if (!manager_get_unit(m, n))
+                        if (!manager_get_unit(m, n)) {
+                                name = n;
                                 break;
+                        }
 
-                        free(n);
+                        n = mfree(n);
                 }
-
-                name = n;
         }
 
         r = manager_load_unit_prepare(m, name, NULL, e, &u);
-        free(n);
-
         if (r < 0)
                 goto fail;
 
-        SNAPSHOT(u)->by_snapshot_create = true;
+        u->transient = true;
         manager_dispatch_load_queue(m);
         assert(u->load_state == UNIT_LOADED);
 
         HASHMAP_FOREACH_KEY(other, k, m->units, i) {
 
-                if (other->ignore_on_snapshot)
+                if (other->ignore_on_snapshot ||
+                    other->transient)
                         continue;
 
                 if (k != other->id)
@@ -251,13 +248,16 @@ int snapshot_create(Manager *m, const char *name, bool cleanup, DBusError *e, Sn
                 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
                         continue;
 
-                if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_WANTS, other, true)) < 0)
+                r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_WANTS, other, true);
+                if (r < 0)
                         goto fail;
         }
 
         SNAPSHOT(u)->cleanup = cleanup;
         *_s = SNAPSHOT(u);
 
+        log_unit_info(u, "Created snapshot.");
+
         return 0;
 
 fail:
@@ -270,18 +270,12 @@ fail:
 void snapshot_remove(Snapshot *s) {
         assert(s);
 
+        log_unit_info(UNIT(s), "Removing snapshot.");
+
         unit_add_to_cleanup_queue(UNIT(s));
 }
 
-static const char* const snapshot_state_table[_SNAPSHOT_STATE_MAX] = {
-        [SNAPSHOT_DEAD] = "dead",
-        [SNAPSHOT_ACTIVE] = "active"
-};
-
-DEFINE_STRING_TABLE_LOOKUP(snapshot_state, SnapshotState);
-
 const UnitVTable snapshot_vtable = {
-        .suffix = ".snapshot",
         .object_size = sizeof(Snapshot),
 
         .no_alias = true,
@@ -289,8 +283,8 @@ const UnitVTable snapshot_vtable = {
         .no_gc = true,
 
         .init = snapshot_init,
-
         .load = snapshot_load,
+
         .coldplug = snapshot_coldplug,
 
         .dump = snapshot_dump,
@@ -304,6 +298,5 @@ const UnitVTable snapshot_vtable = {
         .active_state = snapshot_active_state,
         .sub_state_to_string = snapshot_sub_state_to_string,
 
-        .bus_interface = "org.freedesktop.systemd1.Snapshot",
-        .bus_message_handler = bus_snapshot_message_handler
+        .bus_vtable = bus_snapshot_vtable
 };