1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2015 Daniel Mack
13 #include "sd-messages.h"
15 #include "alloc-util.h"
16 #include "audit-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
20 #include "format-util.h"
22 #include "path-util.h"
25 #include "unit-name.h"
26 #include "user-util.h"
27 #include "utmp-wtmp.h"
29 _const_
static usec_t
when_wall(usec_t n
, usec_t elapse
) {
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,
38 /* If the time is already passed, then don't announce */
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
;
48 return left
% USEC_PER_HOUR
;
51 bool logind_wall_tty_filter(const char *tty
, void *userdata
) {
52 Manager
*m
= userdata
;
57 if (!m
->scheduled_shutdown_tty
)
60 p
= path_startswith(tty
, "/dev/");
64 return !streq(p
, m
->scheduled_shutdown_tty
);
67 static int warn_wall(Manager
*m
, usec_t n
) {
68 char date
[FORMAT_TIMESTAMP_MAX
] = {};
69 _cleanup_free_
char *l
= NULL
;
75 if (!m
->enable_wall_messages
)
78 left
= m
->scheduled_shutdown_timeout
> n
;
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
,
85 left
? format_timestamp(date
, sizeof(date
), m
->scheduled_shutdown_timeout
) : "");
91 utmp_wall(l
, uid_to_name(m
->scheduled_shutdown_uid
),
92 m
->scheduled_shutdown_tty
, logind_wall_tty_filter
, m
);
97 static int wall_message_timeout_handler(
102 Manager
*m
= userdata
;
107 assert(s
== m
->wall_message_timeout_source
);
109 n
= now(CLOCK_REALTIME
);
115 next
= when_wall(n
, m
->scheduled_shutdown_timeout
);
117 r
= sd_event_source_set_time(s
, n
+ next
);
119 return log_error_errno(r
, "sd_event_source_set_time() failed. %m");
121 r
= sd_event_source_set_enabled(s
, SD_EVENT_ONESHOT
);
123 return log_error_errno(r
, "sd_event_source_set_enabled() failed. %m");
129 int manager_setup_wall_message_timer(Manager
*m
) {
136 n
= now(CLOCK_REALTIME
);
137 elapse
= m
->scheduled_shutdown_timeout
;
139 /* wall message handling */
141 if (isempty(m
->scheduled_shutdown_type
)) {
149 /* Warn immediately if less than 15 minutes are left */
150 if (elapse
- n
< 15 * USEC_PER_MINUTE
) {
156 elapse
= when_wall(n
, elapse
);
160 if (m
->wall_message_timeout_source
) {
161 r
= sd_event_source_set_time(m
->wall_message_timeout_source
, n
+ elapse
);
163 return log_error_errno(r
, "sd_event_source_set_time() failed. %m");
165 r
= sd_event_source_set_enabled(m
->wall_message_timeout_source
, SD_EVENT_ONESHOT
);
167 return log_error_errno(r
, "sd_event_source_set_enabled() failed. %m");
169 r
= sd_event_add_time(m
->event
, &m
->wall_message_timeout_source
,
170 CLOCK_REALTIME
, n
+ elapse
, 0, wall_message_timeout_handler
, m
);
172 return log_error_errno(r
, "sd_event_add_time() failed. %m");