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