1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/timerfd.h>
32 #include "shutdownd.h"
36 #include "sd-daemon.h"
37 #include "utmp-wtmp.h"
39 static int read_packet(int fd
, struct shutdownd_command
*_c
) {
44 struct cmsghdr cmsghdr
;
45 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
47 struct shutdownd_command c
;
55 iovec
.iov_len
= sizeof(c
);
59 msghdr
.msg_iov
= &iovec
;
60 msghdr
.msg_iovlen
= 1;
61 msghdr
.msg_control
= &control
;
62 msghdr
.msg_controllen
= sizeof(control
);
64 if ((n
= recvmsg(fd
, &msghdr
, MSG_DONTWAIT
)) <= 0) {
66 log_error("Short read");
70 if (errno
== EAGAIN
|| errno
== EINTR
)
73 log_error("recvmsg(): %m");
77 if (msghdr
.msg_controllen
< CMSG_LEN(sizeof(struct ucred
)) ||
78 control
.cmsghdr
.cmsg_level
!= SOL_SOCKET
||
79 control
.cmsghdr
.cmsg_type
!= SCM_CREDENTIALS
||
80 control
.cmsghdr
.cmsg_len
!= CMSG_LEN(sizeof(struct ucred
))) {
81 log_warning("Received message without credentials. Ignoring.");
85 ucred
= (struct ucred
*) CMSG_DATA(&control
.cmsghdr
);
86 if (ucred
->uid
!= 0) {
87 log_warning("Got request from unprivileged user. Ignoring.");
92 log_warning("Message has invalid size. Ignoring");
96 char_array_0(c
.wall_message
);
102 static void warn_wall(usec_t n
, struct shutdownd_command
*c
) {
105 assert(c
->warn_wall
);
110 if (c
->wall_message
[0])
111 utmp_wall(c
->wall_message
);
113 char date
[FORMAT_TIMESTAMP_MAX
];
118 prefix
= "The system is going down for system halt at ";
119 else if (c
->mode
== 'P')
120 prefix
= "The system is going down for power-off at ";
121 else if (c
->mode
== 'r')
122 prefix
= "The system is going down for reboot at ";
124 assert_not_reached("Unknown mode!");
126 if (asprintf(&l
, "%s%s!", prefix
, format_timestamp(date
, sizeof(date
), c
->elapse
)) < 0)
127 log_error("Failed to allocate wall message");
135 static usec_t
when_wall(usec_t n
, usec_t elapse
) {
137 static const struct {
141 { 10 * USEC_PER_MINUTE
, USEC_PER_MINUTE
},
142 { USEC_PER_HOUR
, 15 * USEC_PER_MINUTE
},
143 { 3 * USEC_PER_HOUR
, 30 * USEC_PER_MINUTE
}
149 /* If the time is already passed, then don't announce */
154 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
155 if (n
+ table
[i
].delay
>= elapse
) {
156 sub
= ((left
/ table
[i
].interval
) * table
[i
].interval
);
160 if (i
>= ELEMENTSOF(table
))
161 sub
= ((left
/ USEC_PER_HOUR
) * USEC_PER_HOUR
);
163 return elapse
> sub
? elapse
- sub
: 1;
166 static usec_t
when_nologin(usec_t elapse
) {
167 return elapse
> 5*USEC_PER_MINUTE
? elapse
- 5*USEC_PER_MINUTE
: 1;
170 int main(int argc
, char *argv
[]) {
179 int r
= EXIT_FAILURE
, n_fds
;
181 struct shutdownd_command c
;
182 struct pollfd pollfd
[_FD_MAX
];
183 bool exec_shutdown
= false, unlink_nologin
= false, failed
= false;
186 if (getppid() != 1) {
187 log_error("This program should be invoked by init only.");
192 log_error("This program does not take arguments.");
196 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
197 log_parse_environment();
200 if ((n_fds
= sd_listen_fds(true)) < 0) {
201 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r
));
206 log_error("Need exactly one file descriptor.");
210 if (setsockopt(SD_LISTEN_FDS_START
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0) {
211 log_error("SO_PASSCRED failed: %m");
218 pollfd
[FD_SOCKET
].fd
= SD_LISTEN_FDS_START
;
219 pollfd
[FD_SOCKET
].events
= POLLIN
;
221 for (i
= 0; i
< _FD_MAX
; i
++) {
226 pollfd
[i
].events
= POLLIN
;
228 if ((pollfd
[i
].fd
= timerfd_create(CLOCK_REALTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0) {
229 log_error("timerfd_create(): %m");
237 log_debug("systemd-shutdownd running as pid %lu", (unsigned long) getpid());
241 "STATUS=Processing requests...");
247 if (poll(pollfd
, _FD_MAX
, -1) < 0) {
249 if (errno
== EAGAIN
|| errno
== EINTR
)
252 log_error("poll(): %m");
256 n
= now(CLOCK_REALTIME
);
258 if (pollfd
[FD_SOCKET
].revents
) {
260 if ((k
= read_packet(pollfd
[FD_SOCKET
].fd
, &c
)) < 0)
262 else if (k
> 0 && c
.elapse
> 0) {
263 struct itimerspec its
;
264 char date
[FORMAT_TIMESTAMP_MAX
];
267 /* Send wall messages every so often */
269 timespec_store(&its
.it_value
, when_wall(n
, c
.elapse
));
270 if (timerfd_settime(pollfd
[FD_WALL_TIMER
].fd
, TFD_TIMER_ABSTIME
, &its
, NULL
) < 0) {
271 log_error("timerfd_settime(): %m");
275 /* Warn immediately if less than 15 minutes are left */
277 n
+ 15*USEC_PER_MINUTE
>= c
.elapse
)
281 /* Disallow logins 5 minutes prior to shutdown */
283 timespec_store(&its
.it_value
, when_nologin(c
.elapse
));
284 if (timerfd_settime(pollfd
[FD_NOLOGIN_TIMER
].fd
, TFD_TIMER_ABSTIME
, &its
, NULL
) < 0) {
285 log_error("timerfd_settime(): %m");
289 /* Shutdown after the specified time is reached */
291 timespec_store(&its
.it_value
, c
.elapse
);
292 if (timerfd_settime(pollfd
[FD_SHUTDOWN_TIMER
].fd
, TFD_TIMER_ABSTIME
, &its
, NULL
) < 0) {
293 log_error("timerfd_settime(): %m");
298 "STATUS=Shutting down at %s...",
299 format_timestamp(date
, sizeof(date
), c
.elapse
));
303 if (pollfd
[FD_WALL_TIMER
].revents
) {
304 struct itimerspec its
;
307 flush_fd(pollfd
[FD_WALL_TIMER
].fd
);
311 timespec_store(&its
.it_value
, when_wall(n
, c
.elapse
));
312 if (timerfd_settime(pollfd
[FD_WALL_TIMER
].fd
, TFD_TIMER_ABSTIME
, &its
, NULL
) < 0) {
313 log_error("timerfd_settime(): %m");
318 if (pollfd
[FD_NOLOGIN_TIMER
].revents
) {
321 log_info("Creating /etc/nologin, blocking further logins...");
323 if ((e
= touch("/etc/nologin")) < 0)
324 log_error("Failed to create /etc/nologin: %s", strerror(-e
));
326 unlink_nologin
= true;
328 flush_fd(pollfd
[FD_NOLOGIN_TIMER
].fd
);
331 if (pollfd
[FD_SHUTDOWN_TIMER
].revents
) {
332 exec_shutdown
= true;
336 } while (c
.elapse
> 0);
340 log_debug("systemd-shutdownd stopped as pid %lu", (unsigned long) getpid());
344 for (i
= 0; i
< _FD_MAX
; i
++)
345 if (pollfd
[i
].fd
>= 0)
346 close_nointr_nofail(pollfd
[i
].fd
);
349 unlink("/etc/nologin");
358 execl(SYSTEMCTL_BINARY_PATH
,
362 (c
.warn_wall
&& c
.wall_message
[0]) ? c
.wall_message
:
363 (c
.warn_wall
? NULL
: "--no-wall"),
366 log_error("Failed to execute /sbin/shutdown: %m");
370 "STATUS=Exiting...");