static void openXenHandles(HSP *sp) { // need to do this while we still have root privileges if(sp->xc_handle == 0) { sp->xc_handle = xc_interface_open(); if(sp->xc_handle <= 0) { myLog(LOG_ERR, "xc_interface_open() failed : %s", strerror(errno)); } else { sp->xs_handle = xs_daemon_open_readonly(); if(sp->xs_handle == NULL) { myLog(LOG_ERR, "xs_daemon_open_readonly() failed : %s", strerror(errno)); } // get the page size [ref xenstat.c] #if defined(PAGESIZE) sp->page_size = PAGESIZE; #elif defined(PAGE_SIZE) sp->page_size = PAGE_SIZE; #else sp->page_size = sysconf(_SC_PAGE_SIZE); if(pgsiz < 0) { myLog(LOG_ERR, "Failed to retrieve page size : %s", strerror(errno)); abort(); } #endif } } }
/* * libxenstat API */ xenstat_handle *xenstat_init(void) { xenstat_handle *handle; handle = (xenstat_handle *) calloc(1, sizeof(xenstat_handle)); if (handle == NULL) return NULL; #if defined(PAGESIZE) handle->page_size = PAGESIZE; #elif defined(PAGE_SIZE) handle->page_size = PAGE_SIZE; #else handle->page_size = sysconf(_SC_PAGE_SIZE); if (handle->page_size < 0) { perror("Failed to retrieve page size."); free(handle); return NULL; } #endif handle->xc_handle = xc_interface_open(); if (handle->xc_handle == -1) { perror("xc_interface_open"); free(handle); return NULL; } handle->xshandle = xs_daemon_open_readonly(); /* open handle to xenstore*/ if (handle->xshandle == NULL) { perror("unable to open xenstore\n"); xc_interface_close(handle->xc_handle); free(handle); return NULL; } return handle; }
/** * xenStoreOpen: * @conn: pointer to the connection block * @name: URL for the target, NULL for local * @flags: combination of virDrvOpenFlag(s) * * Connects to the Xen hypervisor. * * Returns 0 or -1 in case of error. */ int xenStoreOpen(virConnectPtr conn, virConnectAuthPtr auth ATTRIBUTE_UNUSED, unsigned int flags) { xenUnifiedPrivatePtr priv = conn->privateData; virCheckFlags(VIR_CONNECT_RO, -1); if (flags & VIR_CONNECT_RO) priv->xshandle = xs_daemon_open_readonly(); else priv->xshandle = xs_daemon_open(); if (priv->xshandle == NULL) { /* * not being able to connect via the socket as an unprivileged * user is rather normal, this should fallback to the proxy (or * remote) mechanism. */ if (xenHavePrivilege()) { virReportError(VIR_ERR_NO_XEN, "%s", _("failed to connect to Xen Store")); } return -1; } /* Init activeDomainList */ if (VIR_ALLOC(priv->activeDomainList) < 0) return -1; /* Init watch list before filling in domInfoList, so we can know if it is the first time through when the callback fires */ if (VIR_ALLOC(priv->xsWatchList) < 0) return -1; /* This will get called once at start */ if (xenStoreAddWatch(conn, "@releaseDomain", "releaseDomain", xenStoreDomainReleased, priv) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("adding watch @releaseDomain")); return -1; } /* The initial call of this will fill domInfoList */ if (xenStoreAddWatch(conn, "@introduceDomain", "introduceDomain", xenStoreDomainIntroduced, priv) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("adding watch @introduceDomain")); return -1; } /* Add an event handle */ if ((priv->xsWatch = virEventAddHandle(xs_fileno(priv->xshandle), VIR_EVENT_HANDLE_READABLE, xenStoreWatchEvent, conn, NULL)) < 0) VIR_DEBUG("Failed to add event handle, disabling events"); return 0; }