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