user-sessions.c revision e92787416c691c3f34f47349e5eae3fa68eae856
d657c51f14601d0235434ffb78cf6ac0f27cc83cLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
220a21d38f675eb835f5758e3d23e896573aa5eaLennart Poettering
b72ddf0f4f552dd53d6404b6ddbc9f17d02b8e12Kay Sievers/***
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering This file is part of systemd.
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b72ddf0f4f552dd53d6404b6ddbc9f17d02b8e12Kay Sievers Copyright 2010 Lennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering systemd is free software; you can redistribute it and/or modify it
b72ddf0f4f552dd53d6404b6ddbc9f17d02b8e12Kay Sievers under the terms of the GNU General Public License as published by
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering the Free Software Foundation; either version 2 of the License, or
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering (at your option) any later version.
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering systemd is distributed in the hope that it will be useful, but
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering General Public License for more details.
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering You should have received a copy of the GNU General Public License
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering***/
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering#include <string.h>
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering#include <unistd.h>
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering#include <errno.h>
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering#include "log.h"
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering#include "util.h"
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering#include "cgroup-util.h"
5f02e26ca7c039837dbaea63f3d3664fe45c26b9Thomas Hindoe Paaboel Andersen
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poetteringint main(int argc, char*argv[]) {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering int ret = EXIT_FAILURE;
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if (argc != 2) {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_error("This program requires one argument.");
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering return EXIT_FAILURE;
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering }
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_parse_environment();
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_open();
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if (streq(argv[1], "start")) {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if (unlink("/var/run/nologin") < 0 ||
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering unlink("/etc/nologin") < 0) {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if (errno != ENOENT) {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_error("Failed to remove nologin files: %m");
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering goto finish;
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering }
5f02e26ca7c039837dbaea63f3d3664fe45c26b9Thomas Hindoe Paaboel Andersen }
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering } else if (streq(argv[1], "stop")) {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering int r, q;
a1a4a25e7f6b515d0c8c25257714299853f261aaDaniel Mack
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if ((r = write_one_line_file("/var/run/nologin", "System is going down.")) < 0)
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_error("Failed to create /var/run/nologin: %s", strerror(-r));
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if ((q = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, "/user", true)) < 0)
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_error("Failed to kill sessions: %s", strerror(-q));
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering if (r < 0 || q < 0)
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering goto finish;
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering } else {
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering log_error("Unknown verb %s.", argv[1]);
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering goto finish;
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering }
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering ret = EXIT_SUCCESS;
5f02e26ca7c039837dbaea63f3d3664fe45c26b9Thomas Hindoe Paaboel Andersen
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poetteringfinish:
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering return ret;
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering}
b2ca0d63277b10c9382d5bcfcdf320dbb712511bLennart Poettering