示例#1
0
文件: virlog.c 项目: 6WIND/libvirt
static int
virLogFormatString(char **msg,
                   int linenr,
                   const char *funcname,
                   virLogPriority priority,
                   const char *str)
{
    int ret;

    /*
     * Be careful when changing the following log message formatting, we rely
     * on it when stripping libvirt debug messages from qemu log files. So when
     * changing this, you might also need to change the code there.
     * virLogFormatString() function name is mentioned there so it's sufficient
     * to just grep for it to find the right place.
     */
    if ((funcname != NULL)) {
        ret = virAsprintfQuiet(msg, "%llu: %s : %s:%d : %s\n",
                               virThreadSelfID(), virLogPriorityString(priority),
                               funcname, linenr, str);
    } else {
        ret = virAsprintfQuiet(msg, "%llu: %s : %s\n",
                               virThreadSelfID(), virLogPriorityString(priority),
                               str);
    }
    return ret;
}
示例#2
0
文件: driver.c 项目: cardoe/libvirt
void *
virDriverLoadModule(const char *name)
{
    char *modfile = NULL, *regfunc = NULL;
    void *handle = NULL;
    int (*regsym)(void);

    if (moddir == NULL)
        virDriverModuleInitialize(NULL);

    VIR_DEBUG("Module load %s", name);

    if (virAsprintfQuiet(&modfile, "%s/libvirt_driver_%s.so", moddir, name) < 0)
        return NULL;

    if (access(modfile, R_OK) < 0) {
        VIR_WARN("Module %s not accessible", modfile);
        goto cleanup;
    }

    handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
    if (!handle) {
        VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
        goto cleanup;
    }

    if (virAsprintfQuiet(&regfunc, "%sRegister", name) < 0) {
        goto cleanup;
    }

    regsym = dlsym(handle, regfunc);
    if (!regsym) {
        VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
        goto cleanup;
    }

    if ((*regsym)() < 0) {
        VIR_ERROR(_("Failed module registration %s"), regfunc);
        goto cleanup;
    }

    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    return handle;

cleanup:
    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    if (handle)
        dlclose(handle);
    return NULL;
}
示例#3
0
/**
 * virProcessTranslateStatus:
 * @status: child exit status to translate
 *
 * Translate an exit status into a malloc'd string.  Generic helper
 * for virCommandRun(), virCommandWait(), virRun(), and virProcessWait()
 * status argument, as well as raw waitpid().
 */
char *
virProcessTranslateStatus(int status)
{
    char *buf;
    if (WIFEXITED(status)) {
        ignore_value(virAsprintfQuiet(&buf, _("exit status %d"),
                                      WEXITSTATUS(status)));
    } else if (WIFSIGNALED(status)) {
        ignore_value(virAsprintfQuiet(&buf, _("fatal signal %d"),
                                      WTERMSIG(status)));
    } else {
        ignore_value(virAsprintfQuiet(&buf, _("invalid value %d"), status));
    }
    return buf;
}
示例#4
0
/*
 * Poor man's mocked NUMA guesser.  We basically check if
 * /sys/devices/system/node (where /sys/devices/system can already be mocked or
 * changed in the tests) exists and cache the result.
 */
bool
virNumaIsAvailable(void)
{
    if (numa_avail < 0) {
        char *sysfs_node_path = NULL;

        if (virAsprintfQuiet(&sysfs_node_path, "%s/node", SYSFS_SYSTEM_PATH) < 0)
            return false;

        numa_avail = virFileExists(sysfs_node_path);

        VIR_FREE(sysfs_node_path);
    }

    /*
     * Quite a few more things need to be mocked if NUMA is not available and
     * you are using this file.  Do not remove the abort() call below unless you
     * make sure all under virCapabilitiesInitNUMAFake() is mocked (and whatever
     * might have changed since this comment was added.  You are welcome.
     */
    if (!numa_avail)
        abort();

    return numa_avail;
}
示例#5
0
static char *
virDomainAuditGetRdev(const char *path)
{
    char *ret = NULL;
    struct stat sb;

    if (stat(path, &sb) == 0 &&
        (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))) {
        int maj = major(sb.st_rdev);
        int min = minor(sb.st_rdev);
        ignore_value(virAsprintfQuiet(&ret, "%02X:%02X", maj, min));
    }
    return ret;
}
示例#6
0
char *
canonicalize_file_name(const char *path)
{
    if (getenv("LIBVIRT_MTAB")) {
        const char *p;
        char *ret;

        if ((p = STRSKIP(path, "/some/symlink")))
            ignore_value(virAsprintfQuiet(&ret, "/gluster%s", p));
        else
            ignore_value(VIR_STRDUP_QUIET(ret, path));

        return ret;
    }

    return real_canonicalize_file_name(path);
}
示例#7
0
static void init_sysfs(void)
{
    if (fakerootdir && fakesysfscgroupdir)
        return;

    if (!(fakerootdir = getenv("LIBVIRT_FAKE_ROOT_DIR"))) {
        fprintf(stderr, "Missing LIBVIRT_FAKE_ROOT_DIR env variable\n");
        abort();
    }

    if (virAsprintfQuiet(&fakesysfscgroupdir, "%s%s",
                         fakerootdir, SYSFS_CGROUP_PREFIX) < 0)
        abort();

    if (virFileMakePath(fakesysfscgroupdir) < 0) {
        fprintf(stderr, "Cannot create %s\n", fakesysfscgroupdir);
        abort();
    }

# define MAKE_CONTROLLER(subpath)                                      \
    do {                                                               \
        char *path;                                                    \
        if (asprintf(&path, "%s/%s", fakesysfscgroupdir, subpath) < 0) \
            abort();                                                   \
        if (make_controller(path, 0755) < 0) {                         \
            fprintf(stderr, "Cannot initialize %s\n", path);           \
            free(path);                                                \
            abort();                                                   \
        }                                                              \
        free(path);                                                    \
    } while (0)

    MAKE_CONTROLLER("cpu");
    MAKE_CONTROLLER("cpuacct");
    MAKE_CONTROLLER("cpu,cpuacct");
    MAKE_CONTROLLER("cpu,cpuacct/system");
    MAKE_CONTROLLER("cpuset");
    MAKE_CONTROLLER("blkio");
    MAKE_CONTROLLER("memory");
    MAKE_CONTROLLER("freezer");

    if (make_file(fakesysfscgroupdir,
                  SYSFS_CPU_PRESENT_MOCKED, "8-23,48-159\n") < 0)
        abort();
}
示例#8
0
static int
getrealpath(char **newpath,
            const char *path)
{
    if (STRPREFIX(path, LEASEDIR)) {
        if (virAsprintfQuiet(newpath, "%s/nssdata/%s",
                             abs_srcdir,
                             path + strlen(LEASEDIR)) < 0) {
            errno = ENOMEM;
            return -1;
        }
    } else {
        if (VIR_STRDUP_QUIET(*newpath, path) < 0)
            return -1;
    }

    return 0;
}
示例#9
0
/* Similar to virGetHostname() but avoids use of error
 * reporting APIs or logging APIs, to prevent recursion
 */
static int
virLogHostnameString(char **rawmsg,
                     char **msg)
{
    char *hostname = virGetHostnameQuiet();
    char *hoststr;

    if (!hostname)
        return -1;

    if (virAsprintfQuiet(&hoststr, "hostname: %s", hostname) < 0) {
        VIR_FREE(hostname);
        return -1;
    }
    VIR_FREE(hostname);

    if (virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, hoststr) < 0) {
        VIR_FREE(hoststr);
        return -1;
    }
    *rawmsg = hoststr;
    return 0;
}
示例#10
0
/* creates network intereface for VM */
static int
createVifNetwork(virConnectPtr conn, xen_vm vm, int device,
                 char *bridge, char *mac)
{
    xen_session *session = ((struct _xenapiPrivate *)(conn->privateData))->session;
    xen_vm xvm = NULL;
    char *uuid = NULL;
    xen_vm_get_uuid(session, &uuid, vm);
    if (uuid) {
        if (!xen_vm_get_by_uuid(session, &xvm, uuid))
            return -1;
        VIR_FREE(uuid);
    }
    xen_vm_record_opt *vm_opt = xen_vm_record_opt_alloc();
    vm_opt->is_record = 0;
    vm_opt->u.handle = xvm;
    xen_network_set *net_set = NULL;
    xen_network_record *net_rec = NULL;
    int cnt = 0;
    if (xen_network_get_all(session, &net_set)) {
        for (cnt = 0; cnt < net_set->size; cnt++) {
            if (xen_network_get_record(session, &net_rec, net_set->contents[cnt])) {
                if (STREQ(net_rec->bridge, bridge)) {
                    break;
                } else {
                    xen_network_record_free(net_rec);
                }
            }
        }
    }
    if (cnt < net_set->size && net_rec) {
        xen_network network = NULL;
        xen_network_get_by_uuid(session, &network, net_rec->uuid);
        xen_network_record_opt *network_opt = xen_network_record_opt_alloc();
        network_opt->is_record = 0;
        network_opt->u.handle = network;
        xen_vif_record *vif_record = xen_vif_record_alloc();
        vif_record->mac = mac;
        vif_record->vm = vm_opt;
        vif_record->network = network_opt;
        xen_vif vif = NULL;

        vif_record->other_config = xen_string_string_map_alloc(0);
        vif_record->runtime_properties = xen_string_string_map_alloc(0);
        vif_record->qos_algorithm_params = xen_string_string_map_alloc(0);
        if (virAsprintfQuiet(&vif_record->device, "%d", device) < 0)
            return -1;
        xen_vif_create(session, &vif, vif_record);
        if (!vif) {
            xen_vif_free(vif);
            xen_vif_record_free(vif_record);
            xen_network_record_free(net_rec);
            xen_network_set_free(net_set);
            return 0;
        }
        xen_vif_record_free(vif_record);
        xen_network_record_free(net_rec);
    }
    if (net_set != NULL) xen_network_set_free(net_set);
    return -1;
}
示例#11
0
void *
virDriverLoadModule(const char *name)
{
    char *modfile = NULL, *regfunc = NULL, *fixedname = NULL;
    char *tmp;
    void *handle = NULL;
    int (*regsym)(void);

    VIR_DEBUG("Module load %s", name);

    if (!(modfile = virFileFindResourceFull(name,
                                            "libvirt_driver_",
                                            ".so",
                                            abs_topbuilddir "/src/.libs",
                                            DEFAULT_DRIVER_DIR,
                                            "LIBVIRT_DRIVER_DIR")))
        return NULL;

    if (access(modfile, R_OK) < 0) {
        VIR_INFO("Module %s not accessible", modfile);
        goto cleanup;
    }

    virUpdateSelfLastChanged(modfile);

    handle = dlopen(modfile, RTLD_NOW | RTLD_GLOBAL);
    if (!handle) {
        VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror());
        goto cleanup;
    }

    if (VIR_STRDUP_QUIET(fixedname, name) < 0) {
        VIR_ERROR(_("out of memory"));
        goto cleanup;
    }

    /* convert something_like_this into somethingLikeThis */
    while ((tmp = strchr(fixedname, '_'))) {
        memmove(tmp, tmp + 1, strlen(tmp));
        *tmp = c_toupper(*tmp);
    }

    if (virAsprintfQuiet(&regfunc, "%sRegister", fixedname) < 0)
        goto cleanup;

    regsym = dlsym(handle, regfunc);
    if (!regsym) {
        VIR_ERROR(_("Missing module registration symbol %s"), regfunc);
        goto cleanup;
    }

    if ((*regsym)() < 0) {
        VIR_ERROR(_("Failed module registration %s"), regfunc);
        goto cleanup;
    }

    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    VIR_FREE(fixedname);
    return handle;

 cleanup:
    VIR_FREE(modfile);
    VIR_FREE(regfunc);
    VIR_FREE(fixedname);
    if (handle)
        dlclose(handle);
    return NULL;
}
示例#12
0
static int
create_scsihost(const char *fakesysfsdir, const char *devicepath,
                const char *unique_id, const char *hostname)
{
    char *unique_id_path = NULL;
    char *link_path = NULL;
    char *spot;
    int ret = -1;
    int fd = -1;

    if (virAsprintfQuiet(&unique_id_path, "%s/devices/pci0000:00/%s/unique_id",
                         fakesysfsdir, devicepath) < 0 ||
        virAsprintfQuiet(&link_path, "%s/class/scsi_host/%s",
                         fakesysfsdir, hostname) < 0) {
        fprintf(stderr, "Out of memory\n");
        goto cleanup;
    }

    /* Rather than create path & file, temporarily snip off the file to
     * create the path
     */
    if (!(spot = strstr(unique_id_path, "unique_id"))) {
        fprintf(stderr, "Did not find unique_id in path\n");
        goto cleanup;
    }
    spot--;
    *spot = '\0';
    if (virFileMakePathWithMode(unique_id_path, 0755) < 0) {
        fprintf(stderr, "Unable to make path to '%s'\n", unique_id_path);
        goto cleanup;
    }
    *spot = '/';

    /* Rather than create path & file, temporarily snip off the file to
     * create the path
     */
    if (!(spot = strstr(link_path, hostname))) {
        fprintf(stderr, "Did not find hostname in path\n");
        goto cleanup;
    }
    spot--;
    *spot = '\0';
    if (virFileMakePathWithMode(link_path, 0755) < 0) {
        fprintf(stderr, "Unable to make path to '%s'\n", link_path);
        goto cleanup;
    }
    *spot = '/';

    if ((fd = open(unique_id_path, O_CREAT|O_WRONLY, 0444)) < 0) {
        fprintf(stderr, "Unable to create '%s'\n", unique_id_path);
        goto cleanup;
    }

    if (safewrite(fd, unique_id, 1) != 1) {
        fprintf(stderr, "Unable to write '%s'\n", unique_id);
        goto cleanup;
    }
    VIR_DEBUG("Created unique_id '%s'", unique_id_path);

    /* The link is to the path not the file - so remove the file */
    if (!(spot = strstr(unique_id_path, "unique_id"))) {
        fprintf(stderr, "Did not find unique_id in path\n");
        goto cleanup;
    }
    spot--;
    *spot = '\0';
    if (symlink(unique_id_path, link_path) < 0) {
        fprintf(stderr, "Unable to create symlink '%s' to '%s'\n",
                link_path, unique_id_path);
        goto cleanup;
    }
    VIR_DEBUG("Created symlink '%s'", link_path);

    ret = 0;

 cleanup:
    VIR_FORCE_CLOSE(fd);
    VIR_FREE(unique_id_path);
    VIR_FREE(link_path);
    return ret;
}