int main(int argc, char *argv[]) { int i, ret = 0; xc_physinfo_t physinfo = { 0 }; int nr_matches = 0; int matches_main_options[ARRAY_SIZE(main_options)]; if ( argc < 2 ) { show_help(); return 0; } xc_handle = xc_interface_open(0,0,0); if ( !xc_handle ) { fprintf(stderr, "failed to get the handler\n"); return EIO; } ret = xc_physinfo(xc_handle, &physinfo); if ( ret ) { ret = errno; fprintf(stderr, "failed to get processor information (%d - %s)\n", ret, strerror(ret)); xc_interface_close(xc_handle); return ret; } max_cpu_nr = physinfo.nr_cpus; /* calculate how many options match with user's input */ for ( i = 0; i < ARRAY_SIZE(main_options); i++ ) if ( !strncmp(main_options[i].name, argv[1], strlen(argv[1])) ) matches_main_options[nr_matches++] = i; if ( nr_matches > 1 ) { fprintf(stderr, "Ambiguous options: "); for ( i = 0; i < nr_matches; i++ ) fprintf(stderr, " %s", main_options[matches_main_options[i]].name); fprintf(stderr, "\n"); ret = EINVAL; } else if ( nr_matches == 1 ) /* dispatch to the corresponding function handler */ main_options[matches_main_options[0]].function(argc - 2, argv + 2); else { show_help(); ret = EINVAL; } xc_interface_close(xc_handle); return ret; }
static int xencpu_init (void) { xc_handle = xc_interface_open(XC_INTERFACE_INIT_ARGS); if (!xc_handle) { ERROR ("xencpu: xc_interface_open() failed"); return (-1); } xc_physinfo_t *physinfo; physinfo = calloc(1, sizeof(xc_physinfo_t)); if (physinfo == NULL) { ERROR ("xencpu plugin: calloc() for physinfo failed."); xc_interface_close(xc_handle); return (ENOMEM); } if (xc_physinfo(xc_handle, physinfo) < 0) { ERROR ("xencpu plugin: xc_physinfo() failed"); xc_interface_close(xc_handle); free(physinfo); return (-1); } num_cpus = physinfo->nr_cpus; free(physinfo); INFO ("xencpu plugin: Found %"PRIu32" processors.", num_cpus); cpu_info = calloc(num_cpus, sizeof(xc_cpuinfo_t)); if (cpu_info == NULL) { ERROR ("xencpu plugin: calloc() for num_cpus failed."); xc_interface_close(xc_handle); return (ENOMEM); } cpu_states = calloc (num_cpus, sizeof (value_to_rate_state_t)); if (cpu_states == NULL) { ERROR ("xencpu plugin: calloc() for cpu_states failed."); xc_interface_close(xc_handle); free(cpu_info); return (ENOMEM); } return (0); } /* static int xencpu_init */
static int xencpu_shutdown(void) { free(cpu_states); free(cpu_info); xc_interface_close(xc_handle); return 0; } /* static int xencpu_shutdown */
static int xen_init(MachineState *ms) { PCMachineState *pcms = PC_MACHINE(ms); /* Disable ACPI build because Xen handles it */ pcms->acpi_build_enabled = false; xen_xc = xc_interface_open(0, 0, 0); if (xen_xc == NULL) { xen_pv_printf(NULL, 0, "can't open xen interface\n"); return -1; } xen_fmem = xenforeignmemory_open(0, 0); if (xen_fmem == NULL) { xen_pv_printf(NULL, 0, "can't open xen fmem interface\n"); xc_interface_close(xen_xc); return -1; } qemu_add_vm_change_state_handler(xen_change_state_handler, NULL); global_state_set_optional(); savevm_skip_configuration(); savevm_skip_section_footers(); return 0; }
void checkpoint_close(checkpoint_state* s) { if (s->timer) delete_suspend_timer(s); if (s->suspend_thr) stop_suspend_thread(s); release_shutdown_watch(s); release_suspend_evtchn(s); if (s->xch >= 0) { xc_interface_close(s->xch); s->xch = -1; } if (s->xce >= 0) { xc_evtchn_close(s->xce); s->xce = -1; } if (s->xsh) { xs_daemon_close(s->xsh); s->xsh = NULL; } s->domid = 0; s->fd = -1; }
bool xen_setup(void) { xs = xs_daemon_open(); if (xs == NULL) { dolog(LOG_ERR, "Failed to contact xenstore (%m). Is it running?"); goto out; } xc = xc_interface_open(); if (xc == -1) { dolog(LOG_ERR, "Failed to contact hypervisor (%m)"); goto out; } if (!xs_watch(xs, "@introduceDomain", "domlist")) { dolog(LOG_ERR, "xenstore watch on @introduceDomain fails."); goto out; } if (!xs_watch(xs, "@releaseDomain", "domlist")) { dolog(LOG_ERR, "xenstore watch on @releaseDomain fails."); goto out; } return true; out: if (xs) xs_daemon_close(xs); if (xc != -1) xc_interface_close(xc); return false; }
static PyObject *pyflask_load(PyObject *self, PyObject *args, PyObject *kwds) { int xc_handle; char *policy; uint32_t len; int ret; static char *kwd_list[] = { "policy", NULL }; if( !PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwd_list, &policy, &len) ) return NULL; xc_handle = xc_interface_open(); if (xc_handle < 0) { errno = xc_handle; return PyErr_SetFromErrno(xc_error_obj); } ret = flask_load(xc_handle, policy, len); xc_interface_close(xc_handle); if ( ret != 0 ) { errno = -ret; return PyErr_SetFromErrno(xc_error_obj); } return Py_BuildValue("i", ret); }
static PyObject *pyflask_setenforce(PyObject *self, PyObject *args, PyObject *kwds) { int xc_handle; int mode; int ret; static char *kwd_list[] = { "mode", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &mode) ) return NULL; xc_handle = xc_interface_open(); if (xc_handle < 0) { errno = xc_handle; return PyErr_SetFromErrno(xc_error_obj); } ret = flask_setenforce(xc_handle, mode); xc_interface_close(xc_handle); if ( ret != 0 ) { errno = -ret; return PyErr_SetFromErrno(xc_error_obj); } return Py_BuildValue("i", ret); }
static PyObject *pyflask_sid_to_context(PyObject *self, PyObject *args, PyObject *kwds) { int xc_handle; uint32_t sid; char ctx[CTX_LEN]; uint32_t ctx_len = CTX_LEN; int ret; static char *kwd_list[] = { "sid", NULL }; if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i", kwd_list, &sid) ) return NULL; xc_handle = xc_interface_open(); if (xc_handle < 0) { errno = xc_handle; return PyErr_SetFromErrno(xc_error_obj); } ret = flask_sid_to_context(xc_handle, sid, ctx, ctx_len); xc_interface_close(xc_handle); if ( ret != 0 ) { errno = -ret; return PyErr_SetFromErrno(xc_error_obj); } return Py_BuildValue("s", ctx, ctx_len); }
static int __hypercall_perform(unsigned long cmd, unsigned long *arr) { int ret; xc_interface *xch = xc_interface_open(0, 0, 0); DECLARE_HYPERCALL; if (xch == NULL) goto err; hypercall.op = __HYPERVISOR_xen_version; hypercall.arg[0] = cmd; hypercall.arg[1] = (unsigned long) arr; ret = do_xen_hypercall(xch, &hypercall); xc_interface_close(xch); if (ret != 0) goto err; out: return ret; err: ret = -1; goto out; }
static PyObject *getpolicy(PyObject *self, PyObject *args) { struct acm_getpolicy getpolicy; int xc_handle, rc; uint8_t pull_buffer[8192]; PyObject *result; uint32_t len = sizeof(pull_buffer); memset(&getpolicy, 0x0, sizeof(getpolicy)); set_xen_guest_handle(getpolicy.pullcache, pull_buffer); getpolicy.pullcache_size = sizeof(pull_buffer); if ((xc_handle = xc_interface_open()) <= 0) { PyErr_SetString(PyExc_IOError, ctrlif_op); return NULL; } rc = xc_acm_op(xc_handle, ACMOP_getpolicy, &getpolicy, sizeof(getpolicy)); xc_interface_close(xc_handle); if (rc == 0) { struct acm_policy_buffer *header = (struct acm_policy_buffer *)pull_buffer; if (ntohl(header->len) < sizeof(pull_buffer)) len = ntohl(header->len); } else { len = 0; } result = Py_BuildValue("is#", rc, pull_buffer, len); return result; }
int main(int argc, char **argv) { unsigned int domid, maxit, max_f, flags; int xc_fd, io_fd, ret; if (argc != 6) errx(1, "usage: %s iofd domid maxit maxf flags", argv[0]); xc_fd = xc_interface_open(); if (xc_fd < 0) errx(1, "failed to open control interface"); io_fd = atoi(argv[1]); domid = atoi(argv[2]); maxit = atoi(argv[3]); max_f = atoi(argv[4]); flags = atoi(argv[5]); if (suspend_evtchn_init(xc_fd, domid) < 0) warnx("suspend event channel initialization failed, using slow path"); ret = xc_domain_save(xc_fd, io_fd, domid, maxit, max_f, flags, &suspend, !!(flags & XCFLAGS_HVM), &init_qemu_maps, &qemu_flip_buffer); suspend_evtchn_release(); xc_interface_close(xc_fd); return ret; }
/** * map_tbufs - memory map Xen trace buffers into user space * @tbufs_mfn: mfn of the trace buffers * @num: number of trace buffers to map * @size: size of each trace buffer * * Maps the Xen trace buffers them into process address space. */ struct t_buf *map_tbufs(unsigned long tbufs_mfn, unsigned int num, unsigned long size) { int xc_handle; struct t_buf *tbufs_mapped; xc_handle = xc_interface_open(); if ( xc_handle < 0 ) { exit(EXIT_FAILURE); } tbufs_mapped = xc_map_foreign_range(xc_handle, DOMID_XEN, size * num, PROT_READ | PROT_WRITE, tbufs_mfn); xc_interface_close(xc_handle); if ( tbufs_mapped == 0 ) { PERROR("Failed to mmap trace buffers"); exit(EXIT_FAILURE); } return tbufs_mapped; }
static void complete(int retval) { int errnoval = retval ? errno : 0; /* suppress irrelevant errnos */ xtl_log(&logger,XTL_DEBUG,errnoval,program,"complete r=%d",retval); helper_stub_complete(retval,errnoval,0); xc_interface_close(xch); exit(0); }
int main(int argc, char **argv) { int opt; while ((opt = getopt(argc, argv, "h")) != -1) { switch (opt) { case 'h': usage(0); break; default: usage(1); } } argv += optind; argc -= optind; if (argc <= 0) usage(1); xch = xc_interface_open(NULL, NULL, 0); if (!xch) err(1, "opening xc interface"); if (strcmp(argv[0], "reset") == 0) gcov_reset(); else if (strcmp(argv[0], "read") == 0) gcov_read(argc > 1 ? argv[1] : "-"); else usage(1); xc_interface_close(xch); return 0; }
/* generic shared function */ static void *__getssid(int domid, uint32_t *buflen) { struct acm_getssid getssid; int xc_handle; #define SSID_BUFFER_SIZE 4096 void *buf = NULL; if ((xc_handle = xc_interface_open()) < 0) { goto out1; } if ((buf = malloc(SSID_BUFFER_SIZE)) == NULL) { PERROR("acm.policytype: Could not allocate ssid buffer!\n"); goto out2; } memset(buf, 0, SSID_BUFFER_SIZE); set_xen_guest_handle(getssid.ssidbuf, buf); getssid.ssidbuf_size = SSID_BUFFER_SIZE; getssid.get_ssid_by = ACM_GETBY_domainid; getssid.id.domainid = domid; if (xc_acm_op(xc_handle, ACMOP_getssid, &getssid, sizeof(getssid)) < 0) { if (errno == EACCES) PERROR("ACM operation failed."); free(buf); buf = NULL; goto out2; } else { *buflen = SSID_BUFFER_SIZE; goto out2; } out2: xc_interface_close(xc_handle); out1: return buf; }
int main(int argc, char **argv) { int err = 0; xc_interface *xch; int value; if (argc != 3) usage(argv); value = str2bool(argv[2]); xch = xc_interface_open(0,0,0); if ( !xch ) { fprintf(stderr, "Unable to create interface to xenctrl: %s\n", strerror(errno)); err = 1; goto done; } err = xc_flask_setbool(xch, argv[1], value, 1); if (err) { fprintf(stderr, "xc_flask_setbool: Unable to set boolean %s=%s: %s (%d)", argv[1], argv[2], strerror(errno), err); err = 2; goto done; } done: if ( xch ) xc_interface_close(xch); return err; }
int xen_setup(void) { xs = xs_daemon_open(); if (xs == NULL) { dolog(LOG_ERR, "Failed to contact xenstore (%s). Is it running?", strerror(errno)); goto out; } xc = xc_interface_open(); if (xc == -1) { dolog(LOG_ERR, "Failed to contact hypervisor (%s)", strerror(errno)); goto out; } if (!xs_watch(xs, DOMAIN_PATH, "backend")) { dolog(LOG_ERR, "xenstore watch on backend fails."); goto out; } return 0; out: if (xs) xs_daemon_close(xs); if (xc != -1) xc_interface_close(xc); return -1; }
XenDomainWatcher::~XenDomainWatcher() { xs_unwatch( xsh_, "@introduceDomain", introduceToken_.c_str() ); xs_unwatch( xsh_, "@releaseDomain", releaseToken_.c_str() ); xs_close( xsh_ ); xc_interface_close( xci_ ); }
int main(int argc, char **argv) { xc_interface *xch; int domid, port, rc; xc_evtchn_status_t status; domid = (argc > 1) ? strtol(argv[1], NULL, 10) : 0; xch = xc_interface_open(0,0,0); if ( !xch ) errx(1, "failed to open control interface"); for ( port = 0; ; port++ ) { status.dom = domid; status.port = port; rc = xc_evtchn_status(xch, &status); if ( rc < 0 ) break; if ( status.status == EVTCHNSTAT_closed ) continue; printf("%4d: VCPU %u: ", port, status.vcpu); switch ( status.status ) { case EVTCHNSTAT_unbound: printf("Interdomain (Waiting connection) - Remote Domain %u", status.u.unbound.dom); break; case EVTCHNSTAT_interdomain: printf("Interdomain (Connected) - Remote Domain %u, Port %u", status.u.interdomain.dom, status.u.interdomain.port); break; case EVTCHNSTAT_pirq: printf("Physical IRQ %u", status.u.pirq); break; case EVTCHNSTAT_virq: printf("Virtual IRQ %u", status.u.virq); break; case EVTCHNSTAT_ipi: printf("IPI"); break; default: printf("Unknown"); break; } printf("\n"); } xc_interface_close(xch); return 0; }
// give index of this domain in the qos data array int indexof(int domid) { int idx; xc_dominfo_t dominfo[NDOMAINS]; int xc_handle, ndomains; extern void qos_kill_thread(int domid); if (domid < 0) { // shouldn't happen printf("bad domain id: %d\r\n", domid); return 0; } for (idx=0; idx<NDOMAINS; idx++) if ( (new_qos->domain_info[idx].id == domid) && new_qos->domain_info[idx].in_use) return idx; // not found, make a new entry for (idx=0; idx<NDOMAINS; idx++) if (new_qos->domain_info[idx].in_use == 0) { global_init_domain(domid, idx); return idx; } // call domaininfo hypercall to try and garbage collect unused entries xc_handle = xc_interface_open(); ndomains = xc_domain_getinfo(xc_handle, 0, NDOMAINS, dominfo); xc_interface_close(xc_handle); // for each domain in our data, look for it in the system dominfo structure // and purge the domain's data from our state if it does not exist in the // dominfo structure for (idx=0; idx<NDOMAINS; idx++) { int domid = new_qos->domain_info[idx].id; int jdx; for (jdx=0; jdx<ndomains; jdx++) { if (dominfo[jdx].domid == domid) break; } if (jdx == ndomains) // we didn't find domid in the dominfo struct if (domid != IDLE_DOMAIN_ID) // exception for idle domain, which is not // contained in dominfo qos_kill_thread(domid); // purge our stale data } // look again for a free slot for (idx=0; idx<NDOMAINS; idx++) if (new_qos->domain_info[idx].in_use == 0) { global_init_domain(domid, idx); return idx; } // still no space found, so bail fprintf(stderr, "out of space in domain table, increase NDOMAINS\r\n"); exit(2); }
/* retrieve access decision based on domain ids or ssidrefs */ static PyObject *getdecision(PyObject * self, PyObject * args) { char *arg1_name, *arg1, *arg2_name, *arg2, *decision = NULL; struct acm_getdecision getdecision; int xc_handle, rc; uint32_t hooktype; if (!PyArg_ParseTuple(args, "ssssi", &arg1_name, &arg1, &arg2_name, &arg2, &hooktype)) { return NULL; } if ((xc_handle = xc_interface_open()) <= 0) { PERROR("Could not open xen privcmd device!\n"); return NULL; } if ((strcmp(arg1_name, "domid") && strcmp(arg1_name, "ssidref")) || (strcmp(arg2_name, "domid") && strcmp(arg2_name, "ssidref"))) return NULL; getdecision.hook = hooktype; if (!strcmp(arg1_name, "domid")) { getdecision.get_decision_by1 = ACM_GETBY_domainid; getdecision.id1.domainid = atoi(arg1); } else { getdecision.get_decision_by1 = ACM_GETBY_ssidref; getdecision.id1.ssidref = atol(arg1); } if (!strcmp(arg2_name, "domid")) { getdecision.get_decision_by2 = ACM_GETBY_domainid; getdecision.id2.domainid = atoi(arg2); } else { getdecision.get_decision_by2 = ACM_GETBY_ssidref; getdecision.id2.ssidref = atol(arg2); } rc = xc_acm_op(xc_handle, ACMOP_getdecision, &getdecision, sizeof(getdecision)); xc_interface_close(xc_handle); if (rc < 0) { if (errno == EACCES) PERROR("ACM operation failed."); return NULL; } if (getdecision.acm_decision == ACM_ACCESS_PERMITTED) decision = "PERMITTED"; else if (getdecision.acm_decision == ACM_ACCESS_DENIED) decision = "DENIED"; return Py_BuildValue("s", decision); }
static void closeXenHandles(HSP *sp) { if(sp->xc_handle && sp->xc_handle != -1) { xc_interface_close(sp->xc_handle); sp->xc_handle = 0; } if(sp->xs_handle) { xs_daemon_close(sp->xs_handle); sp->xs_handle = NULL; } }
void closeIVCLibrary(libIVC_t *iface) { xc_evtchn_close(iface->ec); xs_close(iface->xs); xc_interface_close(iface->xc); xc_gnttab_close(iface->gt); #ifdef HAVE_XENSTORE_H if(iface->gs) xc_gntshr_close(iface->gs); #endif free(iface); }
void cleanup(void) { if (virq_port > -1) xenevtchn_unbind(xce_handle, virq_port); if (xce_handle) xenevtchn_close(xce_handle); if (xch) xc_interface_close(xch); if (xs_handle) xs_daemon_close(xs_handle); }
void xen_kexec_exec(void) { xc_interface *xch; xch = xc_interface_open(NULL, NULL, 0); if (!xch) return; xc_kexec_exec(xch, KEXEC_TYPE_DEFAULT); xc_interface_close(xch); }
void xen_free_interface(xen_interface_t* xen) { if (xen) { if (xen->xl_ctx) libxl_ctx_free(xen->xl_ctx); if (xen->xl_logger) xtl_logger_destroy(xen->xl_logger); //if (xen->xsh) xs_close(xen->xsh); if (xen->xc) xc_interface_close(xen->xc); free(xen); } }
void xenstat_uninit(xenstat_handle * handle) { unsigned int i; if (handle) { for (i = 0; i < NUM_COLLECTORS; i++) collectors[i].uninit(handle); xc_interface_close(handle->xc_handle); xs_daemon_close(handle->xshandle); free(handle->priv); free(handle); } }
static int get_memory_ranges_xen(struct memory_range **range, int *ranges) { int rc, ret = -1; struct e820entry e820entries[MAX_MEMORY_RANGES]; unsigned int i; #ifdef XENCTRL_HAS_XC_INTERFACE xc_interface *xc; #else int xc; #endif #ifdef XENCTRL_HAS_XC_INTERFACE xc = xc_interface_open(NULL, NULL, 0); if (!xc) { fprintf(stderr, "%s: Failed to open Xen control interface\n", __func__); goto err; } #else xc = xc_interface_open(); if (xc == -1) { fprintf(stderr, "%s: Failed to open Xen control interface\n", __func__); goto err; } #endif rc = xc_get_machine_memory_map(xc, e820entries, MAX_MEMORY_RANGES); if (rc < 0) { fprintf(stderr, "%s: xc_get_machine_memory_map: %s\n", __func__, strerror(rc)); goto err; } for (i = 0; i < rc; ++i) { memory_range[i].start = e820entries[i].addr; memory_range[i].end = e820entries[i].addr + e820entries[i].size; memory_range[i].type = xen_e820_to_kexec_type(e820entries[i].type); } qsort(memory_range, rc, sizeof(struct memory_range), compare_ranges); *range = memory_range; *ranges = rc; ret = 0; err: xc_interface_close(xc); return ret; }
static int xen_init(MachineState *ms) { xen_xc = xc_interface_open(0, 0, 0); if (xen_xc == NULL) { xen_pv_printf(NULL, 0, "can't open xen interface\n"); return -1; } xen_fmem = xenforeignmemory_open(0, 0); if (xen_fmem == NULL) { xen_pv_printf(NULL, 0, "can't open xen fmem interface\n"); xc_interface_close(xen_xc); return -1; } xen_dmod = xendevicemodel_open(0, 0); if (xen_dmod == NULL) { xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n"); xenforeignmemory_close(xen_fmem); xc_interface_close(xen_xc); return -1; } qemu_add_vm_change_state_handler(xen_change_state_handler, NULL); return 0; }