2012-11-28 12:17:31 +00:00
|
|
|
/*
|
|
|
|
* virinitctl.c: API for talking to init systems via initctl
|
|
|
|
*
|
2013-12-20 01:38:59 +00:00
|
|
|
* Copyright (C) 2012-2014 Red Hat, Inc.
|
2012-11-28 12:17:31 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it 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.
|
|
|
|
*
|
|
|
|
* This library 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 this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "virinitctl.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-11-28 12:17:31 +00:00
|
|
|
#include "virfile.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2012-11-28 12:17:31 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_INITCTL
|
|
|
|
|
2012-12-12 07:44:21 +00:00
|
|
|
#if defined(__linux__) || \
|
|
|
|
(defined(__FreeBSD_kernel__) && !(defined(__FreeBSD__)))
|
2012-11-28 12:17:31 +00:00
|
|
|
/* These constants & struct definitions are taken from
|
|
|
|
* systemd, under terms of LGPLv2+
|
|
|
|
*
|
|
|
|
* initreq.h Interface to talk to init through /dev/initctl.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1995-2004 Miquel van Smoorenburg
|
|
|
|
*/
|
|
|
|
|
2012-12-12 07:44:21 +00:00
|
|
|
# define VIR_INITCTL_MAGIC 0x03091969
|
|
|
|
# define VIR_INITCTL_CMD_START 0
|
|
|
|
# define VIR_INITCTL_CMD_RUNLVL 1
|
|
|
|
# define VIR_INITCTL_CMD_POWERFAIL 2
|
|
|
|
# define VIR_INITCTL_CMD_POWERFAILNOW 3
|
|
|
|
# define VIR_INITCTL_CMD_POWEROK 4
|
|
|
|
# define VIR_INITCTL_CMD_BSD 5
|
|
|
|
# define VIR_INITCTL_CMD_SETENV 6
|
|
|
|
# define VIR_INITCTL_CMD_UNSETENV 7
|
|
|
|
|
|
|
|
# define VIR_INITCTL_CMD_CHANGECONS 12345
|
|
|
|
|
|
|
|
# ifdef MAXHOSTNAMELEN
|
|
|
|
# define VIR_INITCTL_RQ_HLEN MAXHOSTNAMELEN
|
|
|
|
# else
|
|
|
|
# define VIR_INITCTL_RQ_HLEN 64
|
|
|
|
# endif
|
2012-11-28 12:17:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is what BSD 4.4 uses when talking to init.
|
|
|
|
* Linux doesn't use this right now.
|
|
|
|
*/
|
|
|
|
struct virInitctlRequestBSD {
|
|
|
|
char gen_id[8]; /* Beats me.. telnetd uses "fe" */
|
|
|
|
char tty_id[16]; /* Tty name minus /dev/tty */
|
|
|
|
char host[VIR_INITCTL_RQ_HLEN]; /* Hostname */
|
|
|
|
char term_type[16]; /* Terminal type */
|
|
|
|
int signal; /* Signal to send */
|
|
|
|
int pid_value; /* Process to send to */
|
|
|
|
char exec_name[128]; /* Program to execute */
|
|
|
|
char reserved[128]; /* For future expansion. */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because of legacy interfaces, "runlevel" and "sleeptime"
|
|
|
|
* aren't in a separate struct in the union.
|
|
|
|
*
|
|
|
|
* The weird sizes are because init expects the whole
|
|
|
|
* struct to be 384 bytes.
|
|
|
|
*/
|
|
|
|
struct virInitctlRequest {
|
|
|
|
int magic; /* Magic number */
|
|
|
|
int cmd; /* What kind of request */
|
|
|
|
int runlevel; /* Runlevel to change to */
|
|
|
|
int sleeptime; /* Time between TERM and KILL */
|
|
|
|
union {
|
|
|
|
struct virInitctlRequestBSD bsd;
|
|
|
|
char data[368];
|
|
|
|
} i;
|
|
|
|
};
|
|
|
|
|
2013-05-01 20:35:44 +00:00
|
|
|
# ifdef MAXHOSTNAMELEN
|
2013-05-02 07:17:44 +00:00
|
|
|
verify(sizeof(struct virInitctlRequest) == 320 + MAXHOSTNAMELEN);
|
2013-05-01 20:35:44 +00:00
|
|
|
# else
|
2013-05-02 07:17:44 +00:00
|
|
|
verify(sizeof(struct virInitctlRequest) == 384);
|
|
|
|
# endif
|
2012-11-28 12:17:31 +00:00
|
|
|
|
2019-01-25 11:37:53 +00:00
|
|
|
|
|
|
|
/* List of fifos that inits are known to listen on */
|
|
|
|
const char *virInitctlFifos[] = {
|
|
|
|
"/run/initctl",
|
|
|
|
"/dev/initctl",
|
|
|
|
"/etc/.initctl",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virInitctlSetRunLevel:
|
|
|
|
* @fifo: the path to fifo that init listens on (can be NULL for autodetection)
|
|
|
|
* @level: the desired runlevel
|
|
|
|
*
|
2013-12-24 05:55:51 +00:00
|
|
|
* Send a message to init to change the runlevel. This function is
|
|
|
|
* asynchronous-signal-safe (thus safe to use after fork of a
|
|
|
|
* multithreaded parent) - which is good, because it should only be
|
|
|
|
* used after forking and entering correct namespace.
|
2012-11-28 12:17:31 +00:00
|
|
|
*
|
|
|
|
* Returns 1 on success, 0 if initctl does not exist, -1 on error
|
|
|
|
*/
|
2013-12-24 05:55:51 +00:00
|
|
|
int
|
2019-01-25 11:37:53 +00:00
|
|
|
virInitctlSetRunLevel(const char *fifo,
|
|
|
|
virInitctlRunLevel level)
|
2012-11-28 12:17:31 +00:00
|
|
|
{
|
|
|
|
struct virInitctlRequest req;
|
|
|
|
int fd = -1;
|
|
|
|
int ret = -1;
|
2019-01-25 11:37:53 +00:00
|
|
|
const int open_flags = O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY;
|
2014-10-22 12:09:49 +00:00
|
|
|
size_t i = 0;
|
2012-11-28 12:17:31 +00:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.magic = VIR_INITCTL_MAGIC;
|
|
|
|
req.sleeptime = 0;
|
|
|
|
req.cmd = VIR_INITCTL_CMD_RUNLVL;
|
|
|
|
/* Yes it is an 'int' field, but wants a numeric character. Go figure */
|
|
|
|
req.runlevel = '0' + level;
|
|
|
|
|
2019-01-25 11:37:53 +00:00
|
|
|
if (fifo) {
|
|
|
|
if ((fd = open(fifo, open_flags)) < 0) {
|
2014-10-22 12:09:49 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot open init control %s"),
|
2019-01-25 11:37:53 +00:00
|
|
|
fifo);
|
2012-11-28 12:17:31 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-01-25 11:37:53 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; virInitctlFifos[i]; i++) {
|
|
|
|
fifo = virInitctlFifos[i];
|
|
|
|
|
|
|
|
if ((fd = open(fifo, open_flags)) >= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (errno != ENOENT) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Cannot open init control %s"),
|
|
|
|
fifo);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 12:09:49 +00:00
|
|
|
|
2019-01-25 11:37:53 +00:00
|
|
|
/* Ensure we found a valid initctl fifo */
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-11-28 12:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (safewrite(fd, &req, sizeof(req)) != sizeof(req)) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to send request to init control %s"),
|
2019-01-25 11:37:53 +00:00
|
|
|
fifo);
|
2012-11-28 12:17:31 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2012-11-28 12:17:31 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-12-12 07:44:21 +00:00
|
|
|
#else
|
2019-01-25 11:37:53 +00:00
|
|
|
int virInitctlSetRunLevel(const char *fifo ATTRIBUTE_UNUSED,
|
|
|
|
virInitctlRunLevel level ATTRIBUTE_UNUSED)
|
2012-12-12 07:44:21 +00:00
|
|
|
{
|
2013-12-20 01:38:59 +00:00
|
|
|
virReportUnsupportedError();
|
|
|
|
return -1;
|
2012-12-12 07:44:21 +00:00
|
|
|
}
|
|
|
|
#endif
|