chiark / gitweb /
1d90083d136109b6dc457aedf9f2e8dca89c5b6e
[elogind.git] / src / login / logind-utmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   Copyright 2015 Daniel Mack
4 ***/
5
6 #include <errno.h>
7 #include <pwd.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include "sd-messages.h"
12
13 #include "alloc-util.h"
14 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
17 #include "bus-util.h"
18 #include "format-util.h"
19 #include "logind.h"
20 #include "path-util.h"
21 //#include "special.h"
22 #include "strv.h"
23 #include "unit-name.h"
24 #include "user-util.h"
25 #include "utmp-wtmp.h"
26
27 _const_ static usec_t when_wall(usec_t n, usec_t elapse) {
28
29         usec_t left;
30         unsigned int i;
31         static const int wall_timers[] = {
32                 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
33                 25, 40, 55, 70, 100, 130, 150, 180,
34         };
35
36         /* If the time is already passed, then don't announce */
37         if (n >= elapse)
38                 return 0;
39
40         left = elapse - n;
41
42         for (i = 1; i < ELEMENTSOF(wall_timers); i++)
43                 if (wall_timers[i] * USEC_PER_MINUTE >= left)
44                         return left - wall_timers[i-1] * USEC_PER_MINUTE;
45
46         return left % USEC_PER_HOUR;
47 }
48
49 bool logind_wall_tty_filter(const char *tty, void *userdata) {
50         Manager *m = userdata;
51         const char *p;
52
53         assert(m);
54
55         if (!m->scheduled_shutdown_tty)
56                 return true;
57
58         p = path_startswith(tty, "/dev/");
59         if (!p)
60                 return true;
61
62         return !streq(p, m->scheduled_shutdown_tty);
63 }
64
65 static int warn_wall(Manager *m, usec_t n) {
66         char date[FORMAT_TIMESTAMP_MAX] = {};
67         _cleanup_free_ char *l = NULL;
68         usec_t left;
69         int r;
70
71         assert(m);
72
73         if (!m->enable_wall_messages)
74                 return 0;
75
76         left = m->scheduled_shutdown_timeout > n;
77
78         r = asprintf(&l, "%s%sThe system is going down for %s %s%s!",
79                      strempty(m->wall_message),
80                      isempty(m->wall_message) ? "" : "\n",
81                      m->scheduled_shutdown_type,
82                      left ? "at " : "NOW",
83                      left ? format_timestamp(date, sizeof(date), m->scheduled_shutdown_timeout) : "");
84         if (r < 0) {
85                 log_oom();
86                 return 0;
87         }
88
89         utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid),
90                   m->scheduled_shutdown_tty, logind_wall_tty_filter, m);
91
92         return 1;
93 }
94
95 static int wall_message_timeout_handler(
96                         sd_event_source *s,
97                         uint64_t usec,
98                         void *userdata) {
99
100         Manager *m = userdata;
101         usec_t n, next;
102         int r;
103
104         assert(m);
105         assert(s == m->wall_message_timeout_source);
106
107         n = now(CLOCK_REALTIME);
108
109         r = warn_wall(m, n);
110         if (r == 0)
111                 return 0;
112
113         next = when_wall(n, m->scheduled_shutdown_timeout);
114         if (next > 0) {
115                 r = sd_event_source_set_time(s, n + next);
116                 if (r < 0)
117                         return log_error_errno(r, "sd_event_source_set_time() failed. %m");
118
119                 r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT);
120                 if (r < 0)
121                         return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
122         }
123
124         return 0;
125 }
126
127 int manager_setup_wall_message_timer(Manager *m) {
128
129         usec_t n, elapse;
130 #if 1 /// let's fix double wall messages in elogind - they suck.
131         usec_t left;
132 #endif // 1
133         int r;
134
135         assert(m);
136
137 #if 1 /// Make elogind more aware of the possibility to disable wall messages
138         /* Do not do anything if wall messages aren't enabled */
139         if (!m->enable_wall_messages)
140                 return 0;
141 #endif // 1
142         n = now(CLOCK_REALTIME);
143         elapse = m->scheduled_shutdown_timeout;
144
145         /* wall message handling */
146
147         if (isempty(m->scheduled_shutdown_type)) {
148                 warn_wall(m, n);
149                 return 0;
150         }
151
152         if (elapse < n)
153                 return 0;
154
155 #if 0 /// let's fix double wall messages in elogind - they suck.
156         /* Warn immediately if less than 15 minutes are left */
157         if (elapse - n < 15 * USEC_PER_MINUTE) {
158                 r = warn_wall(m, n);
159                 if (r == 0)
160                         return 0;
161         }
162
163         elapse = when_wall(n, elapse);
164         if (elapse == 0)
165                 return 0;
166 #else
167         left = elapse - n;
168         elapse = when_wall(n, elapse);
169
170         /* Warn immediately if less than 15 minutes are left, but not if
171          * there aren't more than three seconds to the next timeout. */
172         if ( (left   < 15 * USEC_PER_MINUTE)
173           && (elapse >  3 * USEC_PER_SEC) ) {
174                 r = warn_wall(m, n);
175                 if (r == 0)
176                         return 0;
177         }
178
179         /* If the next timeout is within on second, delay it by 3 seconds */
180         if (USEC_PER_SEC > elapse)
181                 elapse = 3 * USEC_PER_SEC;
182 #endif // 0
183
184         if (m->wall_message_timeout_source) {
185                 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
186                 if (r < 0)
187                         return log_error_errno(r, "sd_event_source_set_time() failed. %m");
188
189                 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
190                 if (r < 0)
191                         return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
192         } else {
193                 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
194                                       CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
195                 if (r < 0)
196                         return log_error_errno(r, "sd_event_add_time() failed. %m");
197         }
198
199         return 0;
200 }