chiark / gitweb /
journal: set the _SYSTEMD_UNIT field for messages from terminated processes
[elogind.git] / src / core / swap.c
index 9c72732..f3c7fa3 100644 (file)
@@ -6,16 +6,16 @@
   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/>.
 ***/
 
@@ -38,6 +38,7 @@
 #include "bus-errors.h"
 #include "exit-status.h"
 #include "def.h"
+#include "path-util.h"
 
 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
         [SWAP_DEAD] = UNIT_INACTIVE,
@@ -86,7 +87,7 @@ static void swap_init(Unit *u) {
         s->exec_context.std_output = u->manager->default_std_output;
         s->exec_context.std_error = u->manager->default_std_error;
 
-        s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
+        s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
 
         s->timer_watch.type = WATCH_INVALID;
 
@@ -115,9 +116,8 @@ static void swap_done(Unit *u) {
         free(s->what);
         s->what = NULL;
 
-        free(s->parameters_etc_fstab.what);
         free(s->parameters_fragment.what);
-        s->parameters_etc_fstab.what = s->parameters_fragment.what = NULL;
+        s->parameters_fragment.what = NULL;
 
         exec_context_done(&s->exec_context);
         exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
@@ -165,29 +165,16 @@ static int swap_add_mount_links(Swap *s) {
 
 static int swap_add_target_links(Swap *s) {
         Unit *tu;
-        SwapParameters *p;
         int r;
 
         assert(s);
 
-        if (s->from_fragment)
-                p = &s->parameters_fragment;
-        else if (s->from_etc_fstab)
-                p = &s->parameters_etc_fstab;
-        else
+        if (!s->from_fragment)
                 return 0;
 
         if ((r = manager_load_unit(UNIT(s)->manager, SPECIAL_SWAP_TARGET, NULL, NULL, &tu)) < 0)
                 return r;
 
-        if (!p->noauto &&
-            !p->nofail &&
-            (p->handle || UNIT(s)->manager->swap_auto) &&
-            s->from_etc_fstab &&
-            UNIT(s)->manager->running_as == MANAGER_SYSTEM)
-                if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
-                        return r;
-
         return unit_add_dependency(UNIT(s), UNIT_BEFORE, tu, true);
 }
 
@@ -201,8 +188,6 @@ static int swap_add_device_links(Swap *s) {
 
         if (s->from_fragment)
                 p = &s->parameters_fragment;
-        else if (s->from_etc_fstab)
-                p = &s->parameters_etc_fstab;
         else
                 return 0;
 
@@ -212,9 +197,9 @@ static int swap_add_device_links(Swap *s) {
                                           UNIT(s)->manager->running_as == MANAGER_SYSTEM);
         else
                 /* File based swap devices need to be ordered after
-                 * remount-rootfs.service, since they might need a
+                 * systemd-remount-fs.service, since they might need a
                  * writable file system. */
-                return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_ROOTFS_SERVICE, NULL, true);
+                return unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_REMOUNT_FS_SERVICE, NULL, true);
 }
 
 static int swap_add_default_dependencies(Swap *s) {
@@ -222,11 +207,12 @@ static int swap_add_default_dependencies(Swap *s) {
 
         assert(s);
 
-        if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
+        if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
+                return 0;
 
-                if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
-                        return r;
-        }
+        r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true);
+        if (r < 0)
+                return r;
 
         return 0;
 }
@@ -278,8 +264,6 @@ static int swap_load(Unit *u) {
                 if (!s->what) {
                         if (s->parameters_fragment.what)
                                 s->what = strdup(s->parameters_fragment.what);
-                        else if (s->parameters_etc_fstab.what)
-                                s->what = strdup(s->parameters_etc_fstab.what);
                         else if (s->parameters_proc_swaps.what)
                                 s->what = strdup(s->parameters_proc_swaps.what);
                         else
@@ -315,14 +299,13 @@ static int swap_load(Unit *u) {
         return swap_verify(s);
 }
 
-int swap_add_one(
+static int swap_add_one(
                 Manager *m,
                 const char *what,
                 const char *what_proc_swaps,
                 int priority,
                 bool noauto,
                 bool nofail,
-                bool handle,
                 bool set_flags) {
 
         Unit *u = NULL;
@@ -330,9 +313,11 @@ int swap_add_one(
         bool delete = false;
         int r;
         SwapParameters *p;
+        Swap *first;
 
         assert(m);
         assert(what);
+        assert(what_proc_swaps);
 
         e = unit_name_from_path(what, ".swap");
         if (!e)
@@ -340,8 +325,7 @@ int swap_add_one(
 
         u = manager_get_unit(m, e);
 
-        if (what_proc_swaps &&
-            u &&
+        if (u &&
             SWAP(u)->from_proc_swaps &&
             !path_equal(SWAP(u)->parameters_proc_swaps.what, what_proc_swaps))
                 return -EEXIST;
@@ -369,58 +353,40 @@ int swap_add_one(
         } else
                 delete = false;
 
-        if (what_proc_swaps) {
-                Swap *first;
+        p = &SWAP(u)->parameters_proc_swaps;
 
-                p = &SWAP(u)->parameters_proc_swaps;
+        if (!p->what) {
+                if (!(wp = strdup(what_proc_swaps))) {
+                        r = -ENOMEM;
+                        goto fail;
+                }
 
-                if (!p->what) {
-                        if (!(wp = strdup(what_proc_swaps))) {
+                if (!m->swaps_by_proc_swaps)
+                        if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
                                 r = -ENOMEM;
                                 goto fail;
                         }
 
-                        if (!m->swaps_by_proc_swaps)
-                                if (!(m->swaps_by_proc_swaps = hashmap_new(string_hash_func, string_compare_func))) {
-                                        r = -ENOMEM;
-                                        goto fail;
-                                }
-
-                        free(p->what);
-                        p->what = wp;
-
-                        first = hashmap_get(m->swaps_by_proc_swaps, wp);
-                        LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
-
-                        if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
-                                goto fail;
-                }
-
-                if (set_flags) {
-                        SWAP(u)->is_active = true;
-                        SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
-                }
-
-                SWAP(u)->from_proc_swaps = true;
+                free(p->what);
+                p->what = wp;
 
-        } else {
-                p = &SWAP(u)->parameters_etc_fstab;
+                first = hashmap_get(m->swaps_by_proc_swaps, wp);
+                LIST_PREPEND(Swap, same_proc_swaps, first, SWAP(u));
 
-                if (!(wp = strdup(what))) {
-                        r = -ENOMEM;
+                if ((r = hashmap_replace(m->swaps_by_proc_swaps, wp, first)) < 0)
                         goto fail;
-                }
-
-                free(p->what);
-                p->what = wp;
+        }
 
-                SWAP(u)->from_etc_fstab = true;
+        if (set_flags) {
+                SWAP(u)->is_active = true;
+                SWAP(u)->just_activated = !SWAP(u)->from_proc_swaps;
         }
 
+        SWAP(u)->from_proc_swaps = true;
+
         p->priority = priority;
         p->noauto = noauto;
         p->nofail = nofail;
-        p->handle = handle;
 
         unit_add_to_dbus_queue(u);
 
@@ -457,8 +423,9 @@ static int swap_process_new_swap(Manager *m, const char *device, int prio, bool
                 if (!(d = udev_device_new_from_devnum(m->udev, 'b', st.st_rdev)))
                         return -ENOMEM;
 
-                if ((dn = udev_device_get_devnode(d)))
-                        r = swap_add_one(m, dn, device, prio, false, false, false, set_flags);
+                dn = udev_device_get_devnode(d);
+                if (dn)
+                        r = swap_add_one(m, dn, device, prio, false, false, set_flags);
 
                 /* Add additional units for all symlinks */
                 first = udev_device_get_devlinks_list_entry(d);
@@ -475,14 +442,16 @@ static int swap_process_new_swap(Manager *m, const char *device, int prio, bool
                                 if ((!S_ISBLK(st.st_mode)) || st.st_rdev != udev_device_get_devnum(d))
                                         continue;
 
-                        if ((k = swap_add_one(m, p, device, prio, false, false, false, set_flags)) < 0)
+                        k = swap_add_one(m, p, device, prio, false, false, set_flags);
+                        if (k < 0)
                                 r = k;
                 }
 
                 udev_device_unref(d);
         }
 
-        if ((k = swap_add_one(m, device, device, prio, false, false, false, set_flags)) < 0)
+        k = swap_add_one(m, device, device, prio, false, false, set_flags);
+        if (k < 0)
                 r = k;
 
         return r;
@@ -567,30 +536,29 @@ static void swap_dump(Unit *u, FILE *f, const char *prefix) {
         else if (s->from_fragment)
                 p = &s->parameters_fragment;
         else
-                p = &s->parameters_etc_fstab;
+                p = NULL;
 
         fprintf(f,
                 "%sSwap State: %s\n"
                 "%sResult: %s\n"
                 "%sWhat: %s\n"
-                "%sPriority: %i\n"
-                "%sNoAuto: %s\n"
-                "%sNoFail: %s\n"
-                "%sHandle: %s\n"
-                "%sFrom /etc/fstab: %s\n"
                 "%sFrom /proc/swaps: %s\n"
                 "%sFrom fragment: %s\n",
                 prefix, swap_state_to_string(s->state),
                 prefix, swap_result_to_string(s->result),
                 prefix, s->what,
-                prefix, p->priority,
-                prefix, yes_no(p->noauto),
-                prefix, yes_no(p->nofail),
-                prefix, yes_no(p->handle),
-                prefix, yes_no(s->from_etc_fstab),
                 prefix, yes_no(s->from_proc_swaps),
                 prefix, yes_no(s->from_fragment));
 
+        if (p)
+                fprintf(f,
+                        "%sPriority: %i\n"
+                        "%sNoAuto: %s\n"
+                        "%sNoFail: %s\n",
+                        prefix, p->priority,
+                        prefix, yes_no(p->noauto),
+                        prefix, yes_no(p->nofail));
+
         if (s->control_pid > 0)
                 fprintf(f,
                         "%sControl PID: %lu\n",
@@ -621,6 +589,9 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
                             UNIT(s)->manager->confirm_spawn,
                             UNIT(s)->cgroup_bondings,
                             UNIT(s)->cgroup_attributes,
+                            NULL,
+                            UNIT(s)->id,
+                            NULL,
                             &pid)) < 0)
                 goto fail;
 
@@ -690,7 +661,8 @@ static void swap_enter_signal(Swap *s, SwapState state, SwapResult f) {
                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
                                         goto fail;
 
-                        if ((r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, pid_set)) < 0) {
+                        r = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, sig, true, false, pid_set, NULL);
+                        if (r < 0) {
                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
                                         log_warning("Failed to kill control group: %s", strerror(-r));
                         } else if (r > 0)
@@ -730,8 +702,6 @@ static void swap_enter_activating(Swap *s) {
 
         if (s->from_fragment)
                 priority = s->parameters_fragment.priority;
-        else if (s->from_etc_fstab)
-                priority = s->parameters_etc_fstab.priority;
         else
                 priority = -1;
 
@@ -926,7 +896,7 @@ static bool swap_check_gc(Unit *u) {
 
         assert(s);
 
-        return s->from_etc_fstab || s->from_proc_swaps;
+        return s->from_proc_swaps;
 }
 
 static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
@@ -1267,8 +1237,6 @@ static int swap_enumerate(Manager *m) {
                         return -errno;
         }
 
-        /* We rely on mount.c to load /etc/fstab for us */
-
         if ((r = swap_load_proc_swaps(m, false)) < 0)
                 swap_shutdown(m);
 
@@ -1321,7 +1289,8 @@ static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *
                                 goto finish;
                         }
 
-                if ((q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, pid_set)) < 0)
+                q = cgroup_bonding_kill_list(UNIT(s)->cgroup_bondings, signo, false, false, pid_set, NULL);
+                if (q < 0)
                         if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
                                 r = q;
         }
@@ -1375,7 +1344,6 @@ const UnitVTable swap_vtable = {
 
         .no_alias = true,
         .no_instances = true,
-        .show_status = true,
 
         .init = swap_init,
         .load = swap_load,
@@ -1411,5 +1379,23 @@ const UnitVTable swap_vtable = {
         .following_set = swap_following_set,
 
         .enumerate = swap_enumerate,
-        .shutdown = swap_shutdown
+        .shutdown = swap_shutdown,
+
+        .status_message_formats = {
+                .starting_stopping = {
+                        [0] = "Activating swap %s...",
+                        [1] = "Deactivating swap %s...",
+                },
+                .finished_start_job = {
+                        [JOB_DONE]       = "Activated swap %s.",
+                        [JOB_FAILED]     = "Failed to activate swap %s.",
+                        [JOB_DEPENDENCY] = "Dependency failed for %s.",
+                        [JOB_TIMEOUT]    = "Timed out activating swap %s.",
+                },
+                .finished_stop_job = {
+                        [JOB_DONE]       = "Deactivated swap %s.",
+                        [JOB_FAILED]     = "Failed deactivating swap %s.",
+                        [JOB_TIMEOUT]    = "Timed out deactivating swap %s.",
+                },
+        },
 };