chiark / gitweb /
unit: reduce heap usage for unit objects
[elogind.git] / src / swap.c
index f7f9530a03963daf34df4c5cb1bcdb4f1e012335..202c4e6625fb0e986693880c41bf8b888a178f64 100644 (file)
@@ -36,6 +36,8 @@
 #include "dbus-swap.h"
 #include "special.h"
 #include "bus-errors.h"
+#include "exit-status.h"
+#include "def.h"
 
 static const UnitActiveState state_translation_table[_SWAP_STATE_MAX] = {
         [SWAP_DEAD] = UNIT_INACTIVE,
@@ -72,7 +74,7 @@ static void swap_unset_proc_swaps(Swap *s) {
         s->parameters_proc_swaps.what = NULL;
 }
 
- static void swap_init(Unit *u) {
+static void swap_init(Unit *u) {
         Swap *s = SWAP(u);
 
         assert(s);
@@ -81,12 +83,16 @@ static void swap_unset_proc_swaps(Swap *s) {
         s->timeout_usec = DEFAULT_TIMEOUT_USEC;
 
         exec_context_init(&s->exec_context);
+        s->exec_context.std_output = u->meta.manager->default_std_output;
+        s->exec_context.std_error = u->meta.manager->default_std_error;
 
         s->parameters_etc_fstab.priority = s->parameters_proc_swaps.priority = s->parameters_fragment.priority = -1;
 
         s->timer_watch.type = WATCH_INVALID;
 
         s->control_command_id = _MOUNT_EXEC_COMMAND_INVALID;
+
+        s->meta.ignore_on_isolate = true;
 }
 
 static void swap_unwatch_control_pid(Swap *s) {
@@ -150,7 +156,7 @@ static int swap_add_mount_links(Swap *s) {
 
         assert(s);
 
-        LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_MOUNT])
+        LIST_FOREACH(units_by_type, other, s->meta.manager->units_by_type[UNIT_MOUNT])
                 if ((r = swap_add_one_mount_link(s, (Mount*) other)) < 0)
                         return r;
 
@@ -175,8 +181,9 @@ static int swap_add_target_links(Swap *s) {
                 return r;
 
         if (!p->noauto &&
+            !p->nofail &&
             (p->handle || s->meta.manager->swap_auto) &&
-            !s->from_fragment &&
+            s->from_etc_fstab &&
             s->meta.manager->running_as == MANAGER_SYSTEM)
                 if ((r = unit_add_dependency(tu, UNIT_WANTS, UNIT(s), true)) < 0)
                         return r;
@@ -199,9 +206,15 @@ static int swap_add_device_links(Swap *s) {
         else
                 return 0;
 
-        return unit_add_node_link(UNIT(s), s->what,
-                                  !p->noauto && p->nofail &&
-                                  s->meta.manager->running_as == MANAGER_SYSTEM);
+        if (is_device_path(s->what))
+                return unit_add_node_link(UNIT(s), s->what,
+                                          !p->noauto && p->nofail &&
+                                          s->meta.manager->running_as == MANAGER_SYSTEM);
+        else
+                /* File based swap devices need to be ordered after
+                 * remount-rootfs.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);
 }
 
 static int swap_add_default_dependencies(Swap *s) {
@@ -211,10 +224,7 @@ static int swap_add_default_dependencies(Swap *s) {
 
         if (s->meta.manager->running_as == MANAGER_SYSTEM) {
 
-                if ((r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_SYSINIT_TARGET, NULL, true)) < 0)
-                        return r;
-
-                if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
+                if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_UMOUNT_TARGET, NULL, true)) < 0)
                         return r;
         }
 
@@ -259,6 +269,8 @@ static int swap_load(Unit *u) {
                 return r;
 
         if (u->meta.load_state == UNIT_LOADED) {
+                if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
+                        return r;
 
                 if (s->meta.fragment_path)
                         s->from_fragment = true;
@@ -283,7 +295,6 @@ static int swap_load(Unit *u) {
                         if ((r = unit_set_description(u, s->what)) < 0)
                                 return r;
 
-
                 if ((r = swap_add_device_links(s)) < 0)
                         return r;
 
@@ -293,6 +304,9 @@ static int swap_load(Unit *u) {
                 if ((r = swap_add_target_links(s)) < 0)
                         return r;
 
+                if ((r = unit_add_default_cgroups(u)) < 0)
+                        return r;
+
                 if (s->meta.default_dependencies)
                         if ((r = swap_add_default_dependencies(s)) < 0)
                                 return r;
@@ -320,7 +334,8 @@ int swap_add_one(
         assert(m);
         assert(what);
 
-        if (!(e = unit_name_from_path(what, ".swap")))
+        e = unit_name_from_path(what, ".swap");
+        if (!e)
                 return -ENOMEM;
 
         u = manager_get_unit(m, e);
@@ -334,15 +349,18 @@ int swap_add_one(
         if (!u) {
                 delete = true;
 
-                if (!(u = unit_new(m))) {
+                u = unit_new(m, sizeof(Swap));
+                if (!u) {
                         free(e);
                         return -ENOMEM;
                 }
 
-                if ((r = unit_add_name(u, e)) < 0)
+                r = unit_add_name(u, e);
+                if (r < 0)
                         goto fail;
 
-                if (!(SWAP(u)->what = strdup(what))) {
+                SWAP(u)->what = strdup(what);
+                if (!SWAP(u)->what) {
                         r = -ENOMEM;
                         goto fail;
                 }
@@ -496,7 +514,7 @@ static void swap_set_state(Swap *s, SwapState state) {
                           swap_state_to_string(old_state),
                           swap_state_to_string(state));
 
-        unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
+        unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], true);
 }
 
 static int swap_coldplug(Unit *u) {
@@ -600,6 +618,7 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
                             true,
                             s->meta.manager->confirm_spawn,
                             s->meta.cgroup_bondings,
+                            s->meta.cgroup_attributes,
                             &pid)) < 0)
                 goto fail;
 
@@ -650,9 +669,7 @@ static void swap_enter_signal(Swap *s, SwapState state, bool success) {
                            state == SWAP_DEACTIVATING_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
 
                 if (s->control_pid > 0) {
-                        if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
-                                 -s->control_pid :
-                                 s->control_pid, sig) < 0 && errno != ESRCH)
+                        if (kill_and_sigcont(s->control_pid, sig) < 0 && errno != ESRCH)
 
                                 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
                         else
@@ -671,13 +688,14 @@ static void swap_enter_signal(Swap *s, SwapState state, bool success) {
                                 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
                                         goto fail;
 
-                        if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
+                        if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, true, pid_set)) < 0) {
                                 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
                                         log_warning("Failed to kill control group: %s", strerror(-r));
                         } else if (r > 0)
                                 wait_for_exit = true;
 
                         set_free(pid_set);
+                        pid_set = NULL;
                 }
         }
 
@@ -928,7 +946,7 @@ static void swap_sigchld_event(Unit *u, pid_t pid, int code, int status) {
         s->failure = s->failure || !success;
 
         if (s->control_command) {
-                exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id);
+                exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
                 s->control_command = NULL;
                 s->control_command_id = _SWAP_EXEC_COMMAND_INVALID;
         }
@@ -990,13 +1008,23 @@ static void swap_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
                 break;
 
         case SWAP_ACTIVATING_SIGTERM:
-                log_warning("%s activation timed out. Killing.", u->meta.id);
-                swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
+                if (s->exec_context.send_sigkill) {
+                        log_warning("%s activation timed out. Killing.", u->meta.id);
+                        swap_enter_signal(s, SWAP_ACTIVATING_SIGKILL, false);
+                } else {
+                        log_warning("%s activation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
+                        swap_enter_dead(s, false);
+                }
                 break;
 
         case SWAP_DEACTIVATING_SIGTERM:
-                log_warning("%s deactivation timed out. Killing.", u->meta.id);
-                swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
+                if (s->exec_context.send_sigkill) {
+                        log_warning("%s deactivation timed out. Killing.", u->meta.id);
+                        swap_enter_signal(s, SWAP_DEACTIVATING_SIGKILL, false);
+                } else {
+                        log_warning("%s deactivation timed out. Skipping SIGKILL. Ignoring.", u->meta.id);
+                        swap_enter_dead(s, false);
+                }
                 break;
 
         case SWAP_ACTIVATING_SIGKILL:
@@ -1074,11 +1102,11 @@ int swap_fd_event(Manager *m, int events) {
         assert(m);
         assert(events & EPOLLPRI);
 
-        if ((r == swap_load_proc_swaps(m, true)) < 0) {
+        if ((r = swap_load_proc_swaps(m, true)) < 0) {
                 log_error("Failed to reread /proc/swaps: %s", strerror(-r));
 
                 /* Reset flags, just in case, for late calls */
-                LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
+                LIST_FOREACH(units_by_type, meta, m->units_by_type[UNIT_SWAP]) {
                         Swap *swap = (Swap*) meta;
 
                         swap->is_active = swap->just_activated = false;
@@ -1089,7 +1117,7 @@ int swap_fd_event(Manager *m, int events) {
 
         manager_dispatch_load_queue(m);
 
-        LIST_FOREACH(units_per_type, meta, m->units_per_type[UNIT_SWAP]) {
+        LIST_FOREACH(units_by_type, meta, m->units_by_type[UNIT_SWAP]) {
                 Swap *swap = (Swap*) meta;
 
                 if (!swap->is_active) {
@@ -1163,6 +1191,39 @@ static Unit *swap_following(Unit *u) {
         return UNIT(first);
 }
 
+static int swap_following_set(Unit *u, Set **_set) {
+        Swap *s = SWAP(u);
+        Swap *other;
+        Set *set;
+        int r;
+
+        assert(s);
+        assert(_set);
+
+        if (LIST_JUST_US(same_proc_swaps, s)) {
+                *_set = NULL;
+                return 0;
+        }
+
+        if (!(set = set_new(NULL, NULL)))
+                return -ENOMEM;
+
+        LIST_FOREACH_AFTER(same_proc_swaps, other, s)
+                if ((r = set_put(set, other)) < 0)
+                        goto fail;
+
+        LIST_FOREACH_BEFORE(same_proc_swaps, other, s)
+                if ((r = set_put(set, other)) < 0)
+                        goto fail;
+
+        *_set = set;
+        return 1;
+
+fail:
+        set_free(set);
+        return r;
+}
+
 static void swap_shutdown(Manager *m) {
         assert(m);
 
@@ -1182,7 +1243,7 @@ static int swap_enumerate(Manager *m) {
 
         if (!m->proc_swaps) {
                 if (!(m->proc_swaps = fopen("/proc/swaps", "re")))
-                        return -errno;
+                        return (errno == ENOENT) ? 0 : -errno;
 
                 m->swap_watch.type = WATCH_SWAP;
                 m->swap_watch.fd = fileno(m->proc_swaps);
@@ -1223,19 +1284,20 @@ static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *
 
         if (who == KILL_MAIN) {
                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "Swap units have no main processes");
-                return -EINVAL;
+                return -ESRCH;
         }
 
         if (s->control_pid <= 0 && who == KILL_CONTROL) {
                 dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill");
-                return -ENOENT;
+                return -ESRCH;
         }
 
-        if (s->control_pid > 0)
-                if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0)
-                        r = -errno;
+        if (who == KILL_CONTROL || who == KILL_ALL)
+                if (s->control_pid > 0)
+                        if (kill(s->control_pid, signo) < 0)
+                                r = -errno;
 
-        if (mode == KILL_CONTROL_GROUP) {
+        if (who == KILL_ALL && mode == KILL_CONTROL_GROUP) {
                 int q;
 
                 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func)))
@@ -1248,8 +1310,8 @@ static int swap_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *
                                 goto finish;
                         }
 
-                if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0)
-                        if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
+                if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, false, pid_set)) < 0)
+                        if (q != -EAGAIN && q != -ESRCH && q != -ENOENT)
                                 r = q;
         }
 
@@ -1283,10 +1345,14 @@ DEFINE_STRING_TABLE_LOOKUP(swap_exec_command, SwapExecCommand);
 
 const UnitVTable swap_vtable = {
         .suffix = ".swap",
+        .object_size = sizeof(Swap),
+        .sections =
+                "Unit\0"
+                "Swap\0"
+                "Install\0",
 
         .no_alias = true,
         .no_instances = true,
-        .no_isolate = true,
         .show_status = true,
 
         .init = swap_init,
@@ -1320,6 +1386,7 @@ const UnitVTable swap_vtable = {
         .bus_invalidating_properties =  bus_swap_invalidating_properties,
 
         .following = swap_following,
+        .following_set = swap_following_set,
 
         .enumerate = swap_enumerate,
         .shutdown = swap_shutdown