virkmod.c 4.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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 * virkmod.c: helper APIs for managing kernel modules
 *
 * Copyright (C) 2014 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/>.
 *
 */

#include <config.h>
#include "viralloc.h"
#include "virkmod.h"
#include "vircommand.h"
#include "virstring.h"

static int
doModprobe(const char *opts, const char *module, char **outbuf, char **errbuf)
{
    int ret = -1;
    virCommandPtr cmd = NULL;

    cmd = virCommandNew(MODPROBE);
    if (opts)
        virCommandAddArg(cmd, opts);
    if (module)
        virCommandAddArg(cmd, module);
    if (outbuf)
        virCommandSetOutputBuffer(cmd, outbuf);
    if (errbuf)
        virCommandSetErrorBuffer(cmd, errbuf);

    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    ret = 0;

49
 cleanup:
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    virCommandFree(cmd);
    return ret;
}

static int
doRmmod(const char *module, char **errbuf)
{
    int ret = -1;
    virCommandPtr cmd = NULL;

    cmd = virCommandNewArgList(RMMOD, module, NULL);
    virCommandSetErrorBuffer(cmd, errbuf);

    if (virCommandRun(cmd, NULL) < 0)
        goto cleanup;

    ret = 0;

68
 cleanup:
69 70 71 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    virCommandFree(cmd);
    return ret;
}

/**
 * virKModConfig:
 *
 * Get the current kernel module configuration
 *
 * Returns NULL on failure or a pointer to the output which
 * must be VIR_FREE()'d by the caller
 */
char *
virKModConfig(void)
{
    char *outbuf = NULL;

    if (doModprobe("-c", NULL, &outbuf, NULL) < 0)
        return NULL;

    return outbuf;
}


/**
 * virKModLoad:
 * @module: Name of the module to load
 * @useBlacklist: True if honoring blacklist
 *
 * Attempts to load a kernel module
 *
 * returns NULL in case of success and the error buffer output from the
 * virCommandRun() on failure.  The returned buffer must be VIR_FREE()
 * by the caller
 */
char *
virKModLoad(const char *module, bool useBlacklist)
{
    char *errbuf = NULL;

    if (doModprobe(useBlacklist ? "-b" : NULL, module, NULL, &errbuf) < 0)
        return errbuf;

    VIR_FREE(errbuf);
    return NULL;
}


/**
 * virKModUnload:
 * @module: Name of the module to unload
 *
 * Remove or unload a module.
 *
 * NB: Do not use 'modprobe -r' here as that code will recursively
 * unload any modules that were dependancies of the one being removed
 * even if things still require them. e.g. it'll see the 'bridge'
 * module has refcount of 0 and remove it, even if there are bridges
 * created on the host
 *
 * returns NULL in case of success and the error buffer output from the
 * virCommandRun() on failure.  The returned buffer must be VIR_FREE()
 * by the caller
 */
char *
virKModUnload(const char *module)
{
    char *errbuf = NULL;

    if (doRmmod(module, &errbuf) < 0)
        return errbuf;

    VIR_FREE(errbuf);
    return NULL;
}


/**
 * virKModIsBlacklisted:
 * @module: Name of the module to check for on the blacklist
 *
 * Search the output of the configuration data for the module being
 * blacklisted.
 *
 * returns true when found blacklisted, false otherwise.
 */
bool
virKModIsBlacklisted(const char *module)
{
    bool retval = false;
    size_t i;
    char *drvblklst = NULL;
    char *outbuf = NULL;

    if (virAsprintfQuiet(&drvblklst, "blacklist %s\n", module) < 0)
        goto cleanup;

    /* modprobe will convert all '-' into '_', so we need to as well */
    for (i = 0; i < drvblklst[i]; i++)
        if (drvblklst[i] == '-')
            drvblklst[i] = '_';

    if (doModprobe("-c", NULL, &outbuf, NULL) < 0)
        goto cleanup;

    if (strstr(outbuf, drvblklst))
        retval = true;

177
 cleanup:
178 179 180 181
    VIR_FREE(drvblklst);
    VIR_FREE(outbuf);
    return retval;
}