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