chiark / gitweb /
treewide: fix typos
[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 "formats-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         int r;
140
141         assert(m);
142
143         n = now(CLOCK_REALTIME);
144         elapse = m->scheduled_shutdown_timeout;
145
146         /* wall message handling */
147
148         if (isempty(m->scheduled_shutdown_type)) {
149                 warn_wall(m, n);
150                 return 0;
151         }
152
153         if (elapse < n)
154                 return 0;
155
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
167         if (m->wall_message_timeout_source) {
168                 r = sd_event_source_set_time(m->wall_message_timeout_source, n + elapse);
169                 if (r < 0)
170                         return log_error_errno(r, "sd_event_source_set_time() failed. %m");
171
172                 r = sd_event_source_set_enabled(m->wall_message_timeout_source, SD_EVENT_ONESHOT);
173                 if (r < 0)
174                         return log_error_errno(r, "sd_event_source_set_enabled() failed. %m");
175         } else {
176                 r = sd_event_add_time(m->event, &m->wall_message_timeout_source,
177                                       CLOCK_REALTIME, n + elapse, 0, wall_message_timeout_handler, m);
178                 if (r < 0)
179                         return log_error_errno(r, "sd_event_add_time() failed. %m");
180         }
181
182         return 0;
183 }