shutdownd.c revision a5c32cff1f56afe6f0c6c70d91a88a7a8238b2d7
/*-*- 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 Lesser General Public License as published by
the Free Software Foundation; either version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser 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 <stddef.h>
#include <systemd/sd-daemon.h>
#include <systemd/sd-shutdown.h>
#include "log.h"
#include "macro.h"
#include "util.h"
#include "utmp-wtmp.h"
#include "mkdir.h"
#include "fileio.h"
union shutdown_buffer {
struct sd_shutdown_command command;
};
union {
} control;
ssize_t n;
union shutdown_buffer b; /* We maintain our own copy here, in order not to corrupt the last message */
if (n <= 0) {
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;
}
log_warning("Message has invalid size. Ignoring.");
return 0;
}
log_warning("Message has invalid mode. Ignoring.");
return 0;
}
b.space[n] = 0;
*_b = b;
return 1;
}
char date[FORMAT_TIMESTAMP_MAX];
const char *prefix;
char *l = NULL;
assert(c);
if (n >= c->usec)
return;
if (c->mode == SD_SHUTDOWN_HALT)
prefix = "The system is going down for system halt at ";
else if (c->mode == SD_SHUTDOWN_POWEROFF)
prefix = "The system is going down for power-off at ";
else if (c->mode == SD_SHUTDOWN_REBOOT)
prefix = "The system is going down for reboot at ";
else if (c->mode == SD_SHUTDOWN_KEXEC)
prefix = "The system is going down for kexec reboot at ";
else if (c->mode == SD_SHUTDOWN_NONE)
prefix = "The system shutdown has been cancelled at ";
else
assert_not_reached("Unknown mode!");
log_error("Failed to allocate wall message");
else {
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))
}
}
static const char *mode_to_string(enum sd_shutdown_mode m) {
switch (m) {
case SD_SHUTDOWN_REBOOT:
return "reboot";
case SD_SHUTDOWN_POWEROFF:
return "poweroff";
case SD_SHUTDOWN_HALT:
return "halt";
case SD_SHUTDOWN_KEXEC:
return "kexec";
default:
return NULL;
}
}
static int update_schedule_file(struct sd_shutdown_command *c) {
int r;
FILE *f;
char *temp_path, *t;
assert(c);
if (r < 0) {
return r;
}
t = cescape(c->wall_message);
if (!t)
return log_oom();
if (r < 0) {
free(t);
return r;
}
fprintf(f,
"USEC=%llu\n"
"WARN_WALL=%i\n"
"MODE=%s\n",
(unsigned long long) c->usec,
c->warn_wall,
mode_to_string(c->mode));
if (c->dry_run)
fputs("DRY_RUN=1\n", f);
if (!isempty(t))
fprintf(f, "WALL_MESSAGE=%s\n", t);
free(t);
fflush(f);
log_error("Failed to write information about scheduled shutdowns: %m");
r = -errno;
}
fclose(f);
return r;
}
static bool scheduled(struct sd_shutdown_command *c) {
}
enum {
};
int r = EXIT_FAILURE, n_fds;
union shutdown_buffer b;
bool exec_shutdown = false, unlink_nologin = false;
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();
umask(0022);
n_fds = sd_listen_fds(true);
if (n_fds < 0) {
return EXIT_FAILURE;
}
if (n_fds != 1) {
log_error("Need exactly one file descriptor.");
return EXIT_FAILURE;
}
zero(b);
for (i = FD_WALL_TIMER; i < _FD_MAX; i++) {
log_error("timerfd_create(): %m");
goto finish;
}
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
for (;;) {
int k;
usec_t n;
if (k < 0) {
continue;
log_error("poll(): %m");
goto finish;
}
/* Exit on idle */
if (k == 0)
break;
n = now(CLOCK_REALTIME);
if (k < 0)
goto finish;
else if (k > 0) {
struct itimerspec its;
char date[FORMAT_TIMESTAMP_MAX];
log_info("Shutdown canceled.");
break;
}
/* Send wall messages every so often */
/* Warn immediately if less than 15 minutes are left */
}
log_error("timerfd_settime(): %m");
goto finish;
}
/* 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 (%s)...",
}
}
struct itimerspec its;
/* Restart timer */
log_error("timerfd_settime(): %m");
goto finish;
}
}
int e;
if (e < 0)
else
unlink_nologin = true;
}
exec_shutdown = true;
goto finish;
}
}
r = EXIT_SUCCESS;
for (i = 0; i < _FD_MAX; i++)
if (unlink_nologin)
char sw[3];
sw[0] = '-';
sw[2] = 0;
"shutdown",
sw,
"now",
NULL);
}
sd_notify(false,
"STATUS=Exiting...");
return r;
}