]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/user-sessions/user-sessions.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / user-sessions / user-sessions.c
CommitLineData
e9278741
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
e9278741
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
e9278741 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
e9278741
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
e9278741 20#include <errno.h>
07630cea 21#include <unistd.h>
e9278741 22
07630cea 23#include "fileio.h"
ea4e6292 24#include "fileio-label.h"
e9278741 25#include "log.h"
ea4e6292 26#include "selinux-util.h"
07630cea 27#include "string-util.h"
e9278741 28#include "util.h"
e9278741
LP
29
30int main(int argc, char*argv[]) {
e9278741
LP
31
32 if (argc != 2) {
33 log_error("This program requires one argument.");
34 return EXIT_FAILURE;
35 }
36
4cfa2c99 37 log_set_target(LOG_TARGET_AUTO);
e9278741
LP
38 log_parse_environment();
39 log_open();
40
4c12626c
LP
41 umask(0022);
42
ea4e6292
MS
43 mac_selinux_init(NULL);
44
e9278741 45 if (streq(argv[1], "start")) {
fe5af612 46 int r = 0;
e9278741 47
ece174c5 48 if (unlink("/run/nologin") < 0 && errno != ENOENT)
76ef789d
LP
49 r = log_error_errno(errno,
50 "Failed to remove /run/nologin file: %m");
e9278741 51
4a8a5b29 52 if (unlink("/etc/nologin") < 0 && errno != ENOENT) {
bd118f8e
LP
53 /* If the file doesn't exist and /etc simply
54 * was read-only (in which case unlink()
55 * returns EROFS even if the file doesn't
56 * exist), don't complain */
57
58 if (errno != EROFS || access("/etc/nologin", F_OK) >= 0) {
56f64d95 59 log_error_errno(errno, "Failed to remove /etc/nologin file: %m");
fe5af612 60 return EXIT_FAILURE;
bd118f8e 61 }
e9278741
LP
62 }
63
fe5af612
LP
64 if (r < 0)
65 return EXIT_FAILURE;
4a8a5b29 66
e9278741 67 } else if (streq(argv[1], "stop")) {
7fb3ee51 68 int r;
e9278741 69
ea4e6292 70 r = write_string_file_atomic_label("/run/nologin", "System is going down.");
fe5af612 71 if (r < 0) {
da927ba9 72 log_error_errno(r, "Failed to create /run/nologin: %m");
fe5af612
LP
73 return EXIT_FAILURE;
74 }
e9278741 75
e9278741
LP
76 } else {
77 log_error("Unknown verb %s.", argv[1]);
fe5af612 78 return EXIT_FAILURE;
e9278741
LP
79 }
80
ea4e6292
MS
81 mac_selinux_finish();
82
fe5af612 83 return EXIT_SUCCESS;
e9278741 84}