shutdownd.c revision ef9ffbd8065b9e06ef27fb989b1c783daf4dc1df
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "shutdownd.h"
#include "log.h"
#include "macro.h"
#include "util.h"
#include "sd-daemon.h"
#include "utmp-wtmp.h"
union {
} control;
struct shutdownd_command c;
ssize_t n;
if (n >= 0) {
log_error("Short read");
return -EIO;
}
return 0;
log_error("recvmsg(): %m");
return -errno;
}
log_warning("Received message without credentials. Ignoring.");
return 0;
}
log_warning("Got request from unprivileged user. Ignoring.");
return 0;
}
if (n != sizeof(c)) {
log_warning("Message has invalid size. Ignoring");
return 0;
}
*_c = c;
return 1;
}
assert(c);
if (n >= c->elapse)
return;
if (c->wall_message[0])
utmp_wall(c->wall_message);
else {
char date[FORMAT_TIMESTAMP_MAX];
const char* prefix;
char *l = NULL;
if (c->mode == 'H')
prefix = "The system is going down for system halt at ";
else if (c->mode == 'P')
prefix = "The system is going down for power-off at ";
else if (c->mode == 'r')
prefix = "The system is going down for reboot at ";
else
assert_not_reached("Unknown mode!");
log_error("Failed to allocate wall message");
else {
utmp_wall(l);
free(l);
}
}
}
static const struct {
} table[] = {
};
unsigned i;
/* If the time is already passed, then don't announce */
if (n >= elapse)
return 0;
for (i = 0; i < ELEMENTSOF(table); i++)
break;
}
if (i >= ELEMENTSOF(table))
}
}
enum {
};
int r = EXIT_FAILURE, n_fds;
int one = 1;
struct shutdownd_command c;
unsigned i;
if (getppid() != 1) {
log_error("This program should be invoked by init only.");
return EXIT_FAILURE;
}
if (argc > 1) {
log_error("This program does not take arguments.");
return EXIT_FAILURE;
}
log_open();
if ((n_fds = sd_listen_fds(true)) < 0) {
return EXIT_FAILURE;
}
if (n_fds != 1) {
log_error("Need exactly one file descriptor.");
return EXIT_FAILURE;
}
log_error("SO_PASSCRED failed: %m");
return EXIT_FAILURE;
}
zero(c);
for (i = 0; i < _FD_MAX; i++) {
if (i == FD_SOCKET)
continue;
log_error("timerfd_create(): %m");
failed = false;
}
}
if (failed)
goto finish;
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
do {
int k;
usec_t n;
continue;
log_error("poll(): %m");
goto finish;
}
n = now(CLOCK_REALTIME);
goto finish;
else if (k > 0 && c.elapse > 0) {
struct itimerspec its;
char date[FORMAT_TIMESTAMP_MAX];
if (c.warn_wall) {
/* Send wall messages every so often */
log_error("timerfd_settime(): %m");
goto finish;
}
/* Warn immediately if less than 15 minutes are left */
if (n < c.elapse &&
warn_wall(n, &c);
}
/* Disallow logins 5 minutes prior to shutdown */
log_error("timerfd_settime(): %m");
goto finish;
}
/* Shutdown after the specified time is reached */
log_error("timerfd_settime(): %m");
goto finish;
}
sd_notifyf(false,
"STATUS=Shutting down at %s...",
}
}
struct itimerspec its;
warn_wall(n, &c);
/* Restart timer */
log_error("timerfd_settime(): %m");
goto finish;
}
}
int e;
else
unlink_nologin = true;
}
exec_shutdown = true;
goto finish;
}
} while (c.elapse > 0);
r = EXIT_SUCCESS;
for (i = 0; i < _FD_MAX; i++)
if (unlink_nologin)
if (exec_shutdown) {
char sw[3];
sw[0] = '-';
sw[2] = 0;
"shutdown",
sw,
"now",
NULL);
}
sd_notify(false,
"STATUS=Exiting...");
return r;
}