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"
38 static int read_packet(int fd
, struct shutdownd_command
*_c
) {
43 struct cmsghdr cmsghdr
;
44 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
46 struct shutdownd_command c
;
54 iovec
.iov_len
= sizeof(c
);
58 msghdr
.msg_iov
= &iovec
;
59 msghdr
.msg_iovlen
= 1;
60 msghdr
.msg_control
= &control
;
61 msghdr
.msg_controllen
= sizeof(control
);
63 if ((n
= recvmsg(fd
, &msghdr
, MSG_DONTWAIT
)) <= 0) {
65 log_error("Short read");
69 if (errno
== EAGAIN
|| errno
== EINTR
)
72 log_error("recvmsg(): %m");
76 if (msghdr
.msg_controllen
< CMSG_LEN(sizeof(struct ucred
)) ||
77 control
.cmsghdr
.cmsg_level
!= SOL_SOCKET
||
78 control
.cmsghdr
.cmsg_type
!= SCM_CREDENTIALS
||
79 control
.cmsghdr
.cmsg_len
!= CMSG_LEN(sizeof(struct ucred
))) {
80 log_warning("Received message without credentials. Ignoring.");
84 ucred
= (struct ucred
*) CMSG_DATA(&control
.cmsghdr
);
85 if (ucred
->uid
!= 0) {
86 log_warning("Got request from unprivileged user. Ignoring.");
91 log_warning("Message has invalid size. Ignoring");
99 int main(int argc
, char *argv
[]) {
110 struct shutdownd_command c
;
111 struct pollfd pollfd
[_FD_MAX
];
112 bool exec_shutdown
= false, unlink_nologin
= false;
114 if (getppid() != 1) {
115 log_error("This program should be invoked by init only.");
120 log_error("This program does not take arguments.");
124 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
125 log_parse_environment();
127 if ((n
= sd_listen_fds(true)) < 0) {
128 log_error("Failed to read listening file descriptors from environment: %s", strerror(-r
));
133 log_error("Need exactly one file descriptor.");
137 if (setsockopt(SD_LISTEN_FDS_START
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
)) < 0) {
138 log_error("SO_PASSCRED failed: %m");
145 pollfd
[FD_SOCKET
].fd
= SD_LISTEN_FDS_START
;
146 pollfd
[FD_SOCKET
].events
= POLLIN
;
147 pollfd
[FD_SHUTDOWN_TIMER
].fd
= -1;
148 pollfd
[FD_SHUTDOWN_TIMER
].events
= POLLIN
;
149 pollfd
[FD_NOLOGIN_TIMER
].fd
= -1;
150 pollfd
[FD_NOLOGIN_TIMER
].events
= POLLIN
;
152 log_debug("systemd-shutdownd running as pid %lu", (unsigned long) getpid());
156 "STATUS=Processing requests...");
161 if (poll(pollfd
, n_fds
, -1) < 0) {
163 if (errno
== EAGAIN
|| errno
== EINTR
)
166 log_error("poll(): %m");
170 if (pollfd
[FD_SOCKET
].revents
) {
172 if ((k
= read_packet(pollfd
[FD_SOCKET
].fd
, &c
)) < 0)
174 else if (k
> 0 && c
.elapse
> 0) {
175 struct itimerspec its
;
178 if (pollfd
[FD_SHUTDOWN_TIMER
].fd
< 0)
179 if ((pollfd
[FD_SHUTDOWN_TIMER
].fd
= timerfd_create(CLOCK_REALTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0) {
180 log_error("timerfd_create(): %m");
184 if (pollfd
[FD_NOLOGIN_TIMER
].fd
< 0)
185 if ((pollfd
[FD_NOLOGIN_TIMER
].fd
= timerfd_create(CLOCK_REALTIME
, TFD_NONBLOCK
|TFD_CLOEXEC
)) < 0) {
186 log_error("timerfd_create(): %m");
190 /* Disallow logins 5 minutes prior to shutdown */
192 timespec_store(&its
.it_value
, c
.elapse
> 5*USEC_PER_MINUTE
? c
.elapse
- 5*USEC_PER_MINUTE
: 0);
193 if (timerfd_settime(pollfd
[FD_NOLOGIN_TIMER
].fd
, TFD_TIMER_ABSTIME
, &its
, NULL
) < 0) {
194 log_error("timerfd_settime(): %m");
198 /* Shutdown after the specified time is reached */
200 timespec_store(&its
.it_value
, c
.elapse
);
201 if (timerfd_settime(pollfd
[FD_SHUTDOWN_TIMER
].fd
, TFD_TIMER_ABSTIME
, &its
, NULL
) < 0) {
202 log_error("timerfd_settime(): %m");
208 ctime_r(&its
.it_value
.tv_sec
, buf
);
211 "STATUS=Shutting down at %s...",
216 if (pollfd
[FD_NOLOGIN_TIMER
].fd
>= 0 &&
217 pollfd
[FD_NOLOGIN_TIMER
].revents
) {
220 if ((e
= touch("/etc/nologin")) < 0)
221 log_error("Failed to create /etc/nologin: %s", strerror(-e
));
223 unlink_nologin
= true;
225 /* Disarm nologin timer */
226 close_nointr_nofail(pollfd
[FD_NOLOGIN_TIMER
].fd
);
227 pollfd
[FD_NOLOGIN_TIMER
].fd
= -1;
232 if (pollfd
[FD_SHUTDOWN_TIMER
].fd
>= 0 &&
233 pollfd
[FD_SHUTDOWN_TIMER
].revents
) {
234 exec_shutdown
= true;
238 } while (c
.elapse
> 0);
242 log_debug("systemd-shutdownd stopped as pid %lu", (unsigned long) getpid());
245 if (pollfd
[FD_SOCKET
].fd
>= 0)
246 close_nointr_nofail(pollfd
[FD_SOCKET
].fd
);
248 if (pollfd
[FD_SHUTDOWN_TIMER
].fd
>= 0)
249 close_nointr_nofail(pollfd
[FD_SHUTDOWN_TIMER
].fd
);
251 if (pollfd
[FD_NOLOGIN_TIMER
].fd
>= 0)
252 close_nointr_nofail(pollfd
[FD_NOLOGIN_TIMER
].fd
);
261 execl(SYSTEMCTL_BINARY_PATH
, "shutdown", sw
, "now", NULL
);
262 log_error("Failed to execute /sbin/shutdown: %m");
266 unlink("/etc/nologin");
269 "STATUS=Exiting...");