]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/fileio-label.c
Merge pull request #8149 from poettering/fake-root-cgroup
[thirdparty/systemd.git] / src / basic / fileio-label.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 Copyright 2010 Harald Hoyer
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <sys/stat.h>
23
24 #include "fileio-label.h"
25 #include "fileio.h"
26 #include "selinux-util.h"
27
28 int write_string_file_atomic_label_ts(const char *fn, const char *line, struct timespec *ts) {
29 int r;
30
31 r = mac_selinux_create_file_prepare(fn, S_IFREG);
32 if (r < 0)
33 return r;
34
35 r = write_string_file_ts(fn, line, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC, ts);
36
37 mac_selinux_create_file_clear();
38
39 return r;
40 }
41
42 int write_env_file_label(const char *fname, char **l) {
43 int r;
44
45 r = mac_selinux_create_file_prepare(fname, S_IFREG);
46 if (r < 0)
47 return r;
48
49 r = write_env_file(fname, l);
50
51 mac_selinux_create_file_clear();
52
53 return r;
54 }
55
56 int fopen_temporary_label(const char *target,
57 const char *path, FILE **f, char **temp_path) {
58 int r;
59
60 r = mac_selinux_create_file_prepare(target, S_IFREG);
61 if (r < 0)
62 return r;
63
64 r = fopen_temporary(path, f, temp_path);
65
66 mac_selinux_create_file_clear();
67
68 return r;
69 }
70
71 int create_shutdown_run_nologin_or_warn(void) {
72 int r;
73
74 /* This is used twice: once in systemd-user-sessions.service, in order to block logins when we actually go
75 * down, and once in systemd-logind.service when shutdowns are scheduled, and logins are to be turned off a bit
76 * in advance. We use the same wording of the message in both cases. */
77
78 r = write_string_file_atomic_label("/run/nologin",
79 "System is going down. Unprivileged users are not permitted to log in anymore. "
80 "For technical details, see pam_nologin(8).");
81 if (r < 0)
82 return log_error_errno(r, "Failed to create /run/nologin: %m");
83
84 return 0;
85 }