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