1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2015 Daniel Mack
11 #include "sd-messages.h"
13 #include "alloc-util.h"
14 #include "audit-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-error.h"
18 #include "format-util.h"
20 #include "path-util.h"
23 #include "unit-name.h"
24 #include "user-util.h"
25 #include "utmp-wtmp.h"
27 _const_
static usec_t
when_wall(usec_t n
, usec_t elapse
) {
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,
36 /* If the time is already passed, then don't announce */
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
;
46 return left
% USEC_PER_HOUR
;
49 bool logind_wall_tty_filter(const char *tty
, void *userdata
) {
50 Manager
*m
= userdata
;
55 if (!m
->scheduled_shutdown_tty
)
58 p
= path_startswith(tty
, "/dev/");
62 return !streq(p
, m
->scheduled_shutdown_tty
);
65 static int warn_wall(Manager
*m
, usec_t n
) {
66 char date
[FORMAT_TIMESTAMP_MAX
] = {};
67 _cleanup_free_
char *l
= NULL
;
73 if (!m
->enable_wall_messages
)
76 left
= m
->scheduled_shutdown_timeout
> n
;
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
,
83 left
? format_timestamp(date
, sizeof(date
), m
->scheduled_shutdown_timeout
) : "");
89 utmp_wall(l
, uid_to_name(m
->scheduled_shutdown_uid
),
90 m
->scheduled_shutdown_tty
, logind_wall_tty_filter
, m
);
95 static int wall_message_timeout_handler(
100 Manager
*m
= userdata
;
105 assert(s
== m
->wall_message_timeout_source
);
107 n
= now(CLOCK_REALTIME
);
113 next
= when_wall(n
, m
->scheduled_shutdown_timeout
);
115 r
= sd_event_source_set_time(s
, n
+ next
);
117 return log_error_errno(r
, "sd_event_source_set_time() failed. %m");
119 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
121 return log_error_errno(r
, "sd_event_source_set_enabled() failed. %m");
127 int manager_setup_wall_message_timer(Manager
*m
) {
134 n
= now(CLOCK_REALTIME
);
135 elapse
= m
->scheduled_shutdown_timeout
;
137 /* wall message handling */
139 if (isempty(m
->scheduled_shutdown_type
)) {
147 /* Warn immediately if less than 15 minutes are left */
148 if (elapse
- n
< 15 * USEC_PER_MINUTE
) {
154 elapse
= when_wall(n
, elapse
);
158 if (m
->wall_message_timeout_source
) {
159 r
= sd_event_source_set_time(m
->wall_message_timeout_source
, n
+ elapse
);
161 return log_error_errno(r
, "sd_event_source_set_time() failed. %m");
163 r
= sd_event_source_set_enabled(m
->wall_message_timeout_source
, SD_EVENT_ONESHOT
);
165 return log_error_errno(r
, "sd_event_source_set_enabled() failed. %m");
167 r
= sd_event_add_time(m
->event
, &m
->wall_message_timeout_source
,
168 CLOCK_REALTIME
, n
+ elapse
, 0, wall_message_timeout_handler
, m
);
170 return log_error_errno(r
, "sd_event_add_time() failed. %m");