virinitctl.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * virinitctl.c: API for talking to init systems via initctl
 *
 * Copyright (C) 2012 Red Hat, Inc.
 *
 * 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/>.
 *
 * Authors:
 *     Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <sys/param.h>
#include <fcntl.h>

#include "internal.h"
#include "virinitctl.h"
31
#include "virerror.h"
32
#include "virutil.h"
33
#include "viralloc.h"
34
#include "virfile.h"
35
#include "virstring.h"
36 37 38

#define VIR_FROM_THIS VIR_FROM_INITCTL

R
Roman Bogorodskiy 已提交
39 40
#if defined(__linux__) || \
    (defined(__FreeBSD_kernel__) && !(defined(__FreeBSD__)))
41 42 43 44 45 46 47 48
/* 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
 */

R
Roman Bogorodskiy 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
# if defined(__FreeBSD_kernel__)
#  define VIR_INITCTL_FIFO  "/etc/.initctl"
# else
#  define VIR_INITCTL_FIFO  "/dev/initctl"
# endif

# 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

/*
*      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;
};

107
# ifdef MAXHOSTNAMELEN
108
  verify(sizeof(struct virInitctlRequest) == 320 + MAXHOSTNAMELEN);
109
# else
110 111
  verify(sizeof(struct virInitctlRequest) == 384);
# endif
112 113

/*
114 115 116 117
 * 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.
118 119 120
 *
 * Returns 1 on success, 0 if initctl does not exist, -1 on error
 */
121 122
int
virInitctlSetRunLevel(virInitctlRunLevel level)
123 124 125 126 127 128 129 130 131 132 133 134 135
{
    struct virInitctlRequest req;
    int fd = -1;
    int ret = -1;

    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;

136 137
    if ((fd = open(VIR_INITCTL_FIFO,
                   O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY)) < 0) {
138 139 140 141 142 143
        if (errno == ENOENT) {
            ret = 0;
            goto cleanup;
        }
        virReportSystemError(errno,
                             _("Cannot open init control %s"),
144
                             VIR_INITCTL_FIFO);
145 146 147 148 149 150
        goto cleanup;
    }

    if (safewrite(fd, &req, sizeof(req)) != sizeof(req)) {
        virReportSystemError(errno,
                             _("Failed to send request to init control %s"),
151
                             VIR_INITCTL_FIFO);
152 153 154 155 156 157 158 159 160
        goto cleanup;
    }

    ret = 1;

cleanup:
    VIR_FORCE_CLOSE(fd);
    return ret;
}
R
Roman Bogorodskiy 已提交
161 162 163 164 165 166 167 168
#else
int virInitctlSetRunLevel(virInitctlRunLevel level ATTRIBUTE_UNUSED,
                          const char *vroot ATTRIBUTE_UNUSED)
{
   virReportError(VIR_ERR_NO_SUPPORT, "%s", __FUNCTION__);
   return -1;
}
#endif