1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "cgroup-util.h"
31 static int kill_all_users(void) {
32 _cleanup_closedir_ DIR *d = NULL;
36 d = opendir("/run/systemd/users");
41 log_error("Failed to open /run/systemd/users: %m");
45 FOREACH_DIRENT(de, d, return -errno) {
46 _cleanup_free_ char *cgroup = NULL;
50 if (!dirent_is_file(de))
53 a = strappenda("/run/systemd/users/", de->d_name);
55 k = parse_env_file(a, NEWLINE, "CGROUP", &cgroup, NULL);
58 log_error("Failed to read user data: %s", strerror(-k));
66 log_error("User data did not contain cgroup field.");
71 k = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, cgroup, true);
73 log_error("Failed to kill cgroup %s: %s", cgroup, strerror(-k));
81 static int kill_all_sessions(void) {
82 _cleanup_closedir_ DIR *d = NULL;
86 d = opendir("/run/systemd/sessions");
91 log_error("Failed to open /run/systemd/sessions: %m");
95 FOREACH_DIRENT(de, d, return -errno) {
96 _cleanup_free_ char *cgroup = NULL;
100 if (!dirent_is_file(de))
103 a = strappenda("/run/systemd/sessions/", de->d_name);
105 k = parse_env_file(a, NEWLINE, "CGROUP", &cgroup, NULL);
108 log_error("Failed to read session data: %s", strerror(-k));
116 log_error("Session data did not contain cgroup field.");
121 k = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, cgroup, true);
123 log_error("Failed to kill cgroup %s: %s", cgroup, strerror(-k));
131 int main(int argc, char*argv[]) {
132 int ret = EXIT_FAILURE;
135 log_error("This program requires one argument.");
139 log_set_target(LOG_TARGET_AUTO);
140 log_parse_environment();
145 if (streq(argv[1], "start")) {
148 if (unlink("/run/nologin") < 0 && errno != ENOENT) {
149 log_error("Failed to remove /run/nologin file: %m");
153 if (unlink("/etc/nologin") < 0 && errno != ENOENT) {
155 /* If the file doesn't exist and /etc simply
156 * was read-only (in which case unlink()
157 * returns EROFS even if the file doesn't
158 * exist), don't complain */
160 if (errno != EROFS || access("/etc/nologin", F_OK) >= 0) {
161 log_error("Failed to remove /etc/nologin file: %m");
169 } else if (streq(argv[1], "stop")) {
172 r = write_string_file_atomic("/run/nologin", "System is going down.");
174 log_error("Failed to create /run/nologin: %s", strerror(-r));
176 q = kill_all_users();
180 q = kill_all_sessions();
185 log_error("Unknown verb %s.", argv[1]);