]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/logind-utmp.c
Merge pull request #6582 from poettering/logind-tty
[thirdparty/systemd.git] / src / login / logind-utmp.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Daniel Mack
5
6 systemd is free software; you can redistribute it and/or modify it
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <pwd.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "sd-messages.h"
26
27 #include "alloc-util.h"
28 #include "audit-util.h"
29 #include "bus-common-errors.h"
30 #include "bus-error.h"
31 #include "bus-util.h"
32 #include "format-util.h"
33 #include "logind.h"
34 #include "path-util.h"
35 #include "special.h"
36 #include "strv.h"
37 #include "unit-name.h"
38 #include "user-util.h"
39 #include "utmp-wtmp.h"
40
41 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
42
43 usec_t left;
44 unsigned int i;
45 static const int wall_timers[] = {
46 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
47 25, 40, 55, 70, 100, 130, 150, 180,
48 };
49
50 /* If the time is already passed, then don't announce */
51 if (n >= elapse)
52 return 0;
53
54 left = elapse - n;
55
56 for (i = 1; i < ELEMENTSOF(wall_timers); i++)
57 if (wall_timers[i] * USEC_PER_MINUTE >= left)
58 return left - wall_timers[i-1] * USEC_PER_MINUTE;
59
60 return left % USEC_PER_HOUR;
61 }
62
63 bool logind_wall_tty_filter(const char *tty, void *userdata) {
64 Manager *m = userdata;
65 const char *p;
66
67 assert(m);
68
69 if (!m->scheduled_shutdown_tty)
70 return true;
71
72 p = path_startswith(tty, "/dev/");
73 if (!p)
74 return true;
75
76 return !streq(p, m->scheduled_shutdown_tty);
77 }
78
79 static int warn_wall(Manager *m, usec_t n) {
80 char date[FORMAT_TIMESTAMP_MAX] = {};
81 _cleanup_free_ char *l = NULL;
82 usec_t left;
83 int r;
84
85 assert(m);
86
87 if (!m->enable_wall_messages)
88 return 0;
89
90 left = m->scheduled_shutdown_timeout > n;
91
92 r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
93 strempty(m->wall_message),
94 isempty(m->wall_message) ? "" : "\n",
95 m->scheduled_shutdown_type,
96 left ? "at " : "NOW",
97 left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
98 if (r < 0) {
99 log_oom();
100 return 0;
101 }
102
103 utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
104 m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
105
106 return 1;
107 }
108
109 static int wall_message_timeout_handler(
110 sd_event_source *s,
111 uint64_t usec,
112 void *userdata) {
113
114 Manager *m = userdata;
115 usec_t n, next;
116 int r;
117
118 assert(m);
119 assert(s == m->wall_message_timeout_source);
120
121 n = now(CLOCK_REALTIME);
122
123 r = warn_wall(m, n);
124 if (r == 0)
125 return 0;
126
127 next = when_wall(n, m->scheduled_shutdown_timeout);
128 if (next > 0) {
129 r = sd_event_source_set_time(s, n + next);
130 if (r < 0)
131 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
132
133 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
134 if (r < 0)
135 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
136 }
137
138 return 0;
139 }
140
141 int manager_setup_wall_message_timer(Manager *m) {
142
143 usec_t n, elapse;
144 int r;
145
146 assert(m);
147
148 n = now(CLOCK_REALTIME);
149 elapse = m->scheduled_shutdown_timeout;
150
151 /* wall message handling */
152
153 if (isempty(m->scheduled_shutdown_type)) {
154 warn_wall(m, n);
155 return 0;
156 }
157
158 if (elapse < n)
159 return 0;
160
161 /* Warn immediately if less than 15 minutes are left */
162 if (elapse - n < 15 * USEC_PER_MINUTE) {
163 r = warn_wall(m, n);
164 if (r == 0)
165 return 0;
166 }
167
168 elapse = when_wall(n, elapse);
169 if (elapse == 0)
170 return 0;
171
172 if (m->wall_message_timeout_source) {
173 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
174 if (r < 0)
175 return log_error_errno(r, "sd_event_source_set_time() failed. %m");
176
177 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
178 if (r < 0)
179 return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
180 } else {
181 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
182 CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
183 if (r < 0)
184 return log_error_errno(r, "sd_event_add_time() failed. %m");
185 }
186
187 return 0;
188 }