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