/** * Tries to locate and load VBoxXPCOMC.so/dylib/dll, resolving all the related * function pointers. * * @returns 0 on success, -1 on failure. */ int VBoxCGlueInit(unsigned int *version) { size_t i; static const char *knownDirs[] = { "/usr/lib/virtualbox", "/usr/lib/virtualbox-ose", "/usr/lib64/virtualbox", "/usr/lib64/virtualbox-ose", "/usr/lib/VirtualBox", "/opt/virtualbox", "/opt/VirtualBox", "/opt/virtualbox/i386", "/opt/VirtualBox/i386", "/opt/virtualbox/amd64", "/opt/VirtualBox/amd64", "/usr/local/lib/virtualbox", "/usr/local/lib/VirtualBox", "/Applications/VirtualBox.app/Contents/MacOS" }; const char *home = virGetEnvBlockSUID("VBOX_APP_HOME"); /* If the user specifies the location, try only that. */ if (home != NULL) { if (tryLoadOne(home, false, false, version) < 0) { return -1; } } /* Try the additionally configured location. */ if (VBOX_XPCOMC_DIR[0] != '\0') { if (tryLoadOne(VBOX_XPCOMC_DIR, true, true, version) >= 0) { return 0; } } /* Try the known locations. */ for (i = 0; i < ARRAY_CARDINALITY(knownDirs); ++i) { if (tryLoadOne(knownDirs[i], true, true, version) >= 0) { return 0; } } /* Finally try the dynamic linker search path. */ if (tryLoadOne(NULL, false, true, version) >= 0) { return 0; } /* No luck, return failure. */ return -1; }
static int virRandomOnceInit(void) { unsigned int seed = time(NULL) ^ getpid(); #if 0 /* Normally we want a decent seed. But if reproducible debugging * of a fixed pseudo-random sequence is ever required, uncomment * this block to let an environment variable force the seed. */ const char *debug = virGetEnvBlockSUID("VIR_DEBUG_RANDOM_SEED"); if (debug && virStrToLong_ui(debug, NULL, 0, &seed) < 0) return -1; #endif if (initstate_r(seed, randomState, sizeof(randomState), &randomData) < 0) return -1; return 0; }
int virAuthGetConfigFilePathURI(virURIPtr uri, char **path) { int ret = -1; size_t i; const char *authenv = virGetEnvBlockSUID("LIBVIRT_AUTH_FILE"); char *userdir = NULL; *path = NULL; VIR_DEBUG("Determining auth config file path"); if (authenv) { VIR_DEBUG("Using path from env '%s'", authenv); if (VIR_STRDUP(*path, authenv) < 0) goto cleanup; return 0; } if (uri) { for (i = 0; i < uri->paramsCount; i++) { if (STREQ_NULLABLE(uri->params[i].name, "authfile") && uri->params[i].value) { VIR_DEBUG("Using path from URI '%s'", uri->params[i].value); if (VIR_STRDUP(*path, uri->params[i].value) < 0) goto cleanup; return 0; } } } if (!(userdir = virGetUserConfigDirectory())) goto cleanup; if (virAsprintf(path, "%s/auth.conf", userdir) < 0) goto cleanup; VIR_DEBUG("Checking for readability of '%s'", *path); if (access(*path, R_OK) == 0) goto done; VIR_FREE(*path); if (VIR_STRDUP(*path, SYSCONFDIR "/libvirt/auth.conf") < 0) goto cleanup; VIR_DEBUG("Checking for readability of '%s'", *path); if (access(*path, R_OK) == 0) goto done; VIR_FREE(*path); done: ret = 0; VIR_DEBUG("Using auth file '%s'", NULLSTR(*path)); cleanup: VIR_FREE(userdir); return ret; }
void virSystemdNotifyStartup(void) { #ifdef HAVE_SYS_UN_H const char *path; const char *msg = "READY=1"; int fd; struct sockaddr_un un = { .sun_family = AF_UNIX, }; struct iovec iov = { .iov_base = (char *)msg, .iov_len = strlen(msg), }; struct msghdr mh = { .msg_name = &un, .msg_iov = &iov, .msg_iovlen = 1, }; if (!(path = virGetEnvBlockSUID("NOTIFY_SOCKET"))) { VIR_DEBUG("Skipping systemd notify, not requested"); return; } /* NB sun_path field is *not* NUL-terminated, hence >, not >= */ if (strlen(path) > sizeof(un.sun_path)) { VIR_WARN("Systemd notify socket path '%s' too long", path); return; } memcpy(un.sun_path, path, strlen(path)); if (un.sun_path[0] == '@') un.sun_path[0] = '\0'; mh.msg_namelen = offsetof(struct sockaddr_un, sun_path) + strlen(path); fd = socket(AF_UNIX, SOCK_DGRAM, 0); if (fd < 0) { VIR_WARN("Unable to create socket FD"); return; } if (sendmsg(fd, &mh, MSG_NOSIGNAL) < 0) VIR_WARN("Failed to notify systemd"); VIR_FORCE_CLOSE(fd); #endif /* HAVE_SYS_UN_H */ } static int virSystemdPMSupportTarget(const char *methodName, bool *result) { int ret; DBusConnection *conn; DBusMessage *message = NULL; char *response; ret = virDBusIsServiceEnabled("org.freedesktop.login1"); if (ret < 0) return ret; if ((ret = virDBusIsServiceRegistered("org.freedesktop.login1")) < 0) return ret; if (!(conn = virDBusGetSystemBus())) return -1; ret = -1; if (virDBusCallMethod(conn, &message, NULL, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", methodName, NULL) < 0) return ret; if ((ret = virDBusMessageDecode(message, "s", &response)) < 0) goto cleanup; *result = STREQ("yes", response) || STREQ("challenge", response); ret = 0; cleanup: virDBusMessageUnref(message); VIR_FREE(response); return ret; } int virSystemdCanSuspend(bool *result) { return virSystemdPMSupportTarget("CanSuspend", result); } int virSystemdCanHibernate(bool *result) { return virSystemdPMSupportTarget("CanHibernate", result); } int virSystemdCanHybridSleep(bool *result) { return virSystemdPMSupportTarget("CanHybridSleep", result); }
virLockManagerPluginPtr virLockManagerPluginNew(const char *name, const char *driverName, const char *configDir, unsigned int flags) { void *handle = NULL; virLockDriverPtr driver; virLockManagerPluginPtr plugin = NULL; const char *moddir = virGetEnvBlockSUID("LIBVIRT_LOCK_MANAGER_PLUGIN_DIR"); char *modfile = NULL; char *configFile = NULL; VIR_DEBUG("name=%s driverName=%s configDir=%s flags=%x", name, driverName, configDir, flags); if (virAsprintf(&configFile, "%s/%s-%s.conf", configDir, driverName, name) < 0) return NULL; if (STREQ(name, "nop")) { driver = &virLockDriverNop; } else { if (moddir == NULL) moddir = virLockManagerPluginDir; VIR_DEBUG("Module load %s from %s", name, moddir); if (virAsprintf(&modfile, "%s/%s.so", moddir, name) < 0) goto cleanup; if (access(modfile, R_OK) < 0) { virReportSystemError(errno, _("Plugin %s not accessible"), modfile); goto cleanup; } handle = dlopen(modfile, RTLD_NOW | RTLD_LOCAL); if (!handle) { virReportError(VIR_ERR_SYSTEM_ERROR, _("Failed to load plugin %s: %s"), modfile, dlerror()); goto cleanup; } if (!(driver = dlsym(handle, "virLockDriverImpl"))) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Missing plugin initialization symbol 'virLockDriverImpl'")); goto cleanup; } } if (driver->drvInit(VIR_LOCK_MANAGER_VERSION, configFile, flags) < 0) goto cleanup; if (VIR_ALLOC(plugin) < 0) goto cleanup; plugin->driver = driver; plugin->handle = handle; plugin->refs = 1; if (VIR_STRDUP(plugin->name, name) < 0) goto cleanup; VIR_FREE(configFile); VIR_FREE(modfile); return plugin; cleanup: VIR_FREE(configFile); VIR_FREE(plugin); VIR_FREE(modfile); if (handle) dlclose(handle); return NULL; }