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; }
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; }
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; }
static char * xenstore_getsv(int domid, const char *fmt, va_list ap) { char *path = NULL, *s; uint64_t value = 0; struct xs_handle *xsh = NULL; int n, m; char key[1024]; bzero(key, sizeof(key)); xsh = xs_daemon_open(); if (xsh == NULL) return 0; path = xs_get_domain_path(xsh, domid); if (path == NULL) goto out; n = snprintf(key, sizeof(key), "%s/", path); if (n < 0) goto out; m = vsnprintf(key + n, sizeof(key) - n, fmt, ap); if (m < 0) goto out; s = xs_read(xsh, XBT_NULL, key, NULL); out: xs_daemon_close(xsh); free(path); return s; }
static int xenstore_putsv(int domid, const char *val, const char *fmt, va_list ap) { char *path = NULL; struct xs_handle *xsh = NULL; int n, m, rc; char key[1024]; bool success; rc = 1; bzero(key, sizeof(key)); xsh = xs_daemon_open(); if (xsh == NULL) goto out; path = xs_get_domain_path(xsh, domid); if (path == NULL) goto out; n = snprintf(key, sizeof(key), "%s/", path); if (n < 0) goto out; m = vsnprintf(key + n, sizeof(key) - n, fmt, ap); if (m < 0) goto out; success = xs_write(xsh, XBT_NULL, key, val, strlen(val)); rc = success? 0 : 1; out: xs_daemon_close(xsh); free(path); return rc; }
int main(int argc, char **argv) { struct xs_handle * xsh; if (argc < 2 || strcmp(argv[1], "check")) { fprintf(stderr, "Usage:\n" "\n" " %s check\n" "\n", argv[0]); return 2; } xsh = xs_daemon_open(); if (xsh == NULL) { fprintf(stderr, "Failed to contact Xenstored.\n"); return 1; } xs_debug_command(xsh, argv[1], NULL, 0); xs_daemon_close(xsh); return 0; }
/** * xenStoreClose: * @conn: pointer to the connection block * * Close the connection to the Xen hypervisor. * * Returns 0 in case of success or -1 in case of error. */ int xenStoreClose(virConnectPtr conn) { xenUnifiedPrivatePtr priv = conn->privateData; if (xenStoreRemoveWatch(conn, "@introduceDomain", "introduceDomain") < 0) { VIR_DEBUG("Warning, could not remove @introduceDomain watch"); /* not fatal */ } if (xenStoreRemoveWatch(conn, "@releaseDomain", "releaseDomain") < 0) { VIR_DEBUG("Warning, could not remove @releaseDomain watch"); /* not fatal */ } xenStoreWatchListFree(priv->xsWatchList); priv->xsWatchList = NULL; xenUnifiedDomainInfoListFree(priv->activeDomainList); priv->activeDomainList = NULL; if (priv->xshandle == NULL) return -1; if (priv->xsWatch != -1) virEventRemoveHandle(priv->xsWatch); xs_daemon_close(priv->xshandle); priv->xshandle = NULL; return 0; }
static int suspend_evtchn_init(int xc, int domid) { struct xs_handle *xs; char path[128]; char *portstr; unsigned int plen; int port; int rc; si.xce = -1; si.suspend_evtchn = -1; xs = xs_daemon_open(); if (!xs) { warnx("failed to get xenstore handle"); return -1; } sprintf(path, "/local/domain/%d/device/suspend/event-channel", domid); portstr = xs_read(xs, XBT_NULL, path, &plen); xs_daemon_close(xs); if (!portstr || !plen) { warnx("could not read suspend event channel"); return -1; } port = atoi(portstr); free(portstr); si.xce = xc_evtchn_open(); if (si.xce < 0) { warnx("failed to open event channel handle"); goto cleanup; } si.suspend_evtchn = xc_evtchn_bind_interdomain(si.xce, domid, port); if (si.suspend_evtchn < 0) { warnx("failed to bind suspend event channel: %d", si.suspend_evtchn); goto cleanup; } rc = xc_domain_subscribe_for_suspend(xc, domid, port); if (rc < 0) { warnx("failed to subscribe to domain: %d", rc); goto cleanup; } /* event channel is pending immediately after binding */ await_suspend(); return 0; cleanup: suspend_evtchn_release(); return -1; }
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); }
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 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); } }
/* Remove the backend area in xenbus since the framebuffer really is going away. */ void xenfb_shutdown(struct xenfb *xenfb) { fprintf(stderr, "FB: Shutting down backend\n"); xs_rm(xenfb->xsh, XBT_NULL, xenfb->fb.nodename); xs_rm(xenfb->xsh, XBT_NULL, xenfb->kbd.nodename); xenfb_detach_dom(xenfb); if (xenfb->xc >= 0) xc_interface_close(xenfb->xc); if (xenfb->evt_xch >= 0) xc_evtchn_close(xenfb->evt_xch); if (xenfb->xsh) xs_daemon_close(xenfb->xsh); free(xenfb); }
static DWORD WINAPI thread_func(PVOID _ident) { int ident; HANDLE h; int x; char path[4096], buf[2000]; char *t; size_t l; ident = (int)(ULONG_PTR)_ident; h = xs_domain_open(); if (!h) win_err(1, "thread %d can't start", ident); sprintf(path, "data/test/thread%d", ident); for (x = 0; x < 5000; x++) { if ( (ident + x) % 256 == 0) memset(buf, 1, sizeof(buf)); else memset(buf, ident + x, sizeof(buf)); buf[sizeof(buf)-1] = 0; if (!xs_write(h, path, buf)) fail_test(__LINE__, "thread %d pass %d", ident, x); t = xs_read(h, path, &l); if (!t) { if (GetLastError() == ERROR_FILE_NOT_FOUND) { printf("ERROR_NOT_FOUND, did the VM get suspended?\n"); } else { fail_test(__LINE__, "thread %d pass %d (%d)", ident, x, GetLastError()); } } else { if (l != sizeof(buf)-1) fail_test(__LINE__, "thread %d pass %d (%d, %d)", ident, x, l, sizeof(buf)-1); if (strcmp(buf, t)) fail_test(__LINE__, "thread %d pass %d", ident, x); free(t); } } xs_daemon_close(h); return 0; }
static void xen_cleanup() { char keybuf[64]; /*XXX hardcoded*/ munmap((void*)gref.index, 4096); set_state(XenbusStateClosing); /* send one last event to unblock the backend */ xc_evtchn_notify(xce, xen_evtchn_port); /* close xen interfaces */ xc_evtchn_close(xce); xc_interface_close(xch); /* delete xenstore keys */ publish_param_int("state", XenbusStateClosed); snprintf(keybuf, sizeof(keybuf), "device/audio/%d", device_id); xs_rm(xsh, 0, keybuf); xs_daemon_close(xsh); }
int get_memory_size (xa_instance_t *instance) { int ret = XA_SUCCESS; if (XA_MODE_XEN == instance->mode){ #ifdef ENABLE_XEN struct xs_handle *xsh = NULL; xs_transaction_t xth = XBT_NULL; char *tmp = malloc(100); if (NULL == tmp){ fprintf(stderr, "ERROR: failed to allocate memory for tmp variable\n"); ret = XA_FAILURE; goto error_exit; } memset(tmp, 0, 100); sprintf(tmp, "/local/domain/%d/memory/target", instance->m.xen.domain_id); xsh = xs_domain_open(); instance->m.xen.size = strtol(xs_read(xsh, xth, tmp, NULL), NULL, 10) * 1024; if (0 == instance->m.xen.size){ fprintf(stderr, "ERROR: failed to get memory size for Xen domain.\n"); ret = XA_FAILURE; goto error_exit; } xa_dbprint("**set instance->m.xen.size = %d\n", instance->m.xen.size); if (xsh) xs_daemon_close(xsh); #endif /* ENABLE_XEN */ } else if (XA_MODE_FILE == instance->mode){ struct stat s; if (fstat(fileno(instance->m.file.fhandle), &s) == -1){ fprintf(stderr, "ERROR: Failed to stat file\n"); ret = XA_FAILURE; goto error_exit; } instance->m.file.size = (uint32_t) s.st_size; xa_dbprint("**set instance->m.file.size = %d\n", instance->m.file.size); } error_exit: return ret; }
static int init_xs_srv(struct libxenvchan *ctrl, int domain, const char* xs_base, int ring_ref) { int ret = -1; struct xs_handle *xs; struct xs_permissions perms[2]; char buf[64]; char ref[16]; char* domid_str = NULL; xs = xs_domain_open(); if (!xs) goto fail; domid_str = xs_read(xs, 0, "domid", NULL); if (!domid_str) goto fail_xs_open; // owner domain is us perms[0].id = atoi(domid_str); // permissions for domains not listed = none perms[0].perms = XS_PERM_NONE; // other domains perms[1].id = domain; perms[1].perms = XS_PERM_READ; snprintf(ref, sizeof ref, "%d", ring_ref); snprintf(buf, sizeof buf, "%s/ring-ref", xs_base); if (!xs_write(xs, 0, buf, ref, strlen(ref))) goto fail_xs_open; if (!xs_set_permissions(xs, 0, buf, perms, 2)) goto fail_xs_open; snprintf(ref, sizeof ref, "%d", ctrl->event_port); snprintf(buf, sizeof buf, "%s/event-channel", xs_base); if (!xs_write(xs, 0, buf, ref, strlen(ref))) goto fail_xs_open; if (!xs_set_permissions(xs, 0, buf, perms, 2)) goto fail_xs_open; ret = 0; fail_xs_open: free(domid_str); xs_daemon_close(xs); fail: return ret; }
int switch_qemu_logdirty(int domid, unsigned enable, void *data) { char *path = NULL; struct xs_handle *xsh = NULL; bool rc; pasprintf(&path, "/local/domain/0/device-model/%u/logdirty/cmd", domid); xsh = xs_daemon_open(); if (xsh == NULL) errx(1, "Couldn't contact xenstore"); if (enable) rc = xs_write(xsh, XBT_NULL, path, "enable", strlen("enable")); else rc = xs_write(xsh, XBT_NULL, path, "disable", strlen("disable")); xs_daemon_close(xsh); free(path); return rc ? 0 : 1; }
static void xenstore_get_host_limits(size_t *kernel_max_size, size_t *ramdisk_max_size) { static const char *kernel_max_path = "/mh/limits/pv-kernel-max-size"; static const char *ramdisk_max_path = "/mh/limits/pv-ramdisk-max-size"; struct xs_handle *xsh = NULL; size_t value; char *s; /* Safe defaults */ *kernel_max_size = (32 * 1024 * 1024); *ramdisk_max_size = (128 * 1024 * 1024); xsh = xs_daemon_open(); if (xsh == NULL) return; s = xs_read(xsh, XBT_NULL, kernel_max_path, NULL); if (s) { errno = 0; value = strtoul(s, NULL, 10); if ( errno == 0 ) *kernel_max_size = value; free(s); } s = xs_read(xsh, XBT_NULL, ramdisk_max_path, NULL); if (s) { errno = 0; value = strtoul(s, NULL, 10); if ( errno == 0 ) *ramdisk_max_size = value; free(s); } xs_daemon_close(xsh); return; }
static void dm_xenstore_record_pid(void *for_spawn, pid_t innerchild) { libxl__device_model_starting *starting = for_spawn; struct xs_handle *xsh; char *path = NULL, *pid = NULL; int len; if (asprintf(&path, "%s/%s", starting->dom_path, "image/device-model-pid") < 0) goto out; len = asprintf(&pid, "%d", innerchild); if (len < 0) goto out; /* we mustn't use the parent's handle in the child */ xsh = xs_daemon_open(); xs_write(xsh, XBT_NULL, path, pid, len); xs_daemon_close(xsh); out: free(path); free(pid); }
static 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; } 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); xs = NULL; } return -1; }
int main (int argc, char **argv) { int i; struct xs_handle *xs; xs_transaction_t th; char *path; int fd; fd_set set; int er; char **vec; unsigned int num_strings; char * buf; unsigned int len; char *program; char **arguments; int arguments_count; pid_t pid; int j; char *last_value = NULL; int status; program_name = argv[0]; i = decode_switches (argc, argv); if (argc - i < 1) usage(1); path = argv[i++]; if (argc - i > 0) { program = argv[i++]; arguments_count = argc - i; arguments = malloc(sizeof(char*) * (argc - i + 2)); arguments[0] = program; for (j=0; j<arguments_count; j++) arguments[j + 1] = argv[i + j]; arguments[j + 1] = NULL; } else { program = NULL; arguments = NULL; arguments_count = 0; } if (want_verbose) { printf("Path: %s\n", path); if (program) { printf("Program: %s", program); for (i=1; i<arguments_count + 1; i++) printf(" %s", arguments[i]); printf("\n"); } } /* Get a connection to the daemon */ xs = xs_daemon_open(); if ( xs == NULL ) xs = xs_domain_open(); if ( xs == NULL ) { error("Unable to connect to XenStore"); exit(1); } /* Create a watch on /local/domain/0/mynode. */ er = xs_watch(xs, path, "token"); if ( er == 0 ) { error("Unable to create watch"); exit(1); } /* We are notified of read availability on the watch via the * file descriptor. */ fd = xs_fileno(xs); while (1) { FD_ZERO(&set); FD_SET(fd, &set); /* Poll for data. */ if ( select(fd + 1, &set, NULL, NULL, NULL) > 0 && FD_ISSET(fd, &set)) { /* num_strings will be set to the number of elements in vec * (typically, 2 - the watched path and the token) */ vec = xs_read_watch(xs, &num_strings); if ( !vec ) { error("Unable to read watch"); continue; } if (want_verbose) printf("Path changed: %s\n", vec[XS_WATCH_PATH]); /* Prepare a transaction and do a read. */ th = xs_transaction_start(xs); buf = xs_read(xs, th, vec[XS_WATCH_PATH], &len); xs_transaction_end(xs, th, false); if (buf) { if (last_value && strcmp(buf, last_value) == 0) { if (want_verbose) printf("Value did not change\n"); continue; } if (want_verbose) printf("New value: %s\n", buf); if (program) { pid = fork(); if (pid == 0) { setenv("XENSTORE_WATCH_PATH", vec[XS_WATCH_PATH], 1); setenv("XENSTORE_WATCH_VALUE", buf, 1); for (i=0; arguments[i]; i++) { if (strcmp(arguments[i], "%v") == 0) arguments[i] = buf; else if (strcmp(arguments[i], "%p") == 0) arguments[i] = vec[XS_WATCH_PATH]; } execvp(program, arguments); error("Unable to start program"); exit(1); } else { waitpid(pid, &status, 0); } } else { if (!want_verbose) printf("%s\n", buf); } if (last_value) free(last_value); last_value = buf; } } } /* Cleanup */ close(fd); xs_daemon_close(xs); free(path); exit(0); }
int __cdecl main() { char *vm_path, *uuid, *t; size_t l; char **contents; unsigned count; HANDLE xs_handle2; HANDLE event; HANDLE event2; int watch_h; int watch_h2; int i; DWORD status; xs_handle = xs_domain_open(); if (!xs_handle) win_err(1, "openning xenstore interface"); /* Try to give ourselves a clean place to start */ xs_remove(xs_handle, "data/test"); /* Check basic xenstore reads with relative path... */ vm_path = xs_read(xs_handle, "vm", NULL); if (!vm_path) win_err(1, "reading vm path"); if (vm_path[0] != '/') { fail_test(__LINE__, "expected vm path to be absolute, got %s", vm_path); } /* and with an absolute path. */ uuid = gather_read(&l, vm_path, "uuid", NULL); if (!uuid) win_err(1, "reading uuid"); if (l != 36) { fail_test(__LINE__, "uuid length was %d bytes, expected 36"); } if (strlen(uuid) != 36) { fail_test(__LINE__, "uuid was %s, not right length (%d, should be 36), returned length %d", uuid, strlen(uuid), l); } /* Make sure read error sets a suitable code. */ xs_read_expected_error(__LINE__, "non_existent", ERROR_FILE_NOT_FOUND); xs_read_expected_error(__LINE__, "invalid\\path", ERROR_INVALID_PARAMETER); xs_read_expected_error(__LINE__, "/local/domain/0/name", ERROR_ACCESS_DENIED); /* Test basic xs_write functionality. */ if (!xs_write(xs_handle, "data/test/key1", "data1")) { fail_test(__LINE__, "write data/test/key1 failed with %lx", GetLastError()); } else { t = xs_read(xs_handle, "data/test/key1", &l); if (!t) { fail_test(__LINE__, "error reading from data/test/key1: %lx", GetLastError()); } else { if (l != 5) { fail_test(__LINE__, "manifest length wrong reading data/test/key1: %d should be 5.", l); } if (strcmp(t, "data1")) { fail_test(__LINE__, "got wrong data reading data/test/key1: %s should be data1.", t); } free(t); } } xs_write_expected_error(__LINE__, "foo", "bar", ERROR_ACCESS_DENIED); xs_write_expected_error(__LINE__, "/foo", "bar", ERROR_ACCESS_DENIED); /* Try a very large write and make sure that it fails in the expected way. */ t = malloc(65536); memset(t, 'a', 65536); t[65535] = 0; xs_write_expected_error(__LINE__,"data/test/key1", t, ERROR_DISK_FULL); free(t); /* Test that read and write work for keys containing nul bytes. */ if (!xs_write_bin(xs_handle, "data/test/key1", "xxx\0yyy", 7)) { fail_test(__LINE__, "failed to write nul bytes (%d)", GetLastError()); } t = xs_read(xs_handle, "data/test/key1", &l); if (!t) { fail_test(__LINE__, "failed to read nul bytes (%d)", GetLastError()); } else { if (l != 7) { fail_test(__LINE__, "read with nuls: expected 7, got %d.\n", l); } else if (memcmp(t, "xxx\0yyy", 7)) { fail_test(__LINE__, "bad data from read with nuls: %s", t); } free(t); } if (!xs_remove(xs_handle, "data/test/key1")) { fail_test(__LINE__, "failed to remove data/test/key1 (%d)", GetLastError()); } xs_read_expected_error(__LINE__, "data/test/key1", ERROR_FILE_NOT_FOUND); xs_ls_expected_error(__LINE__, "data/test/key1", ERROR_FILE_NOT_FOUND); if (!xs_write(xs_handle, "data/test/key1", "data1")) { fail_test(__LINE__, "failed to rewrite data/test/key1"); } contents = xs_directory(xs_handle, "data/test/key1", &count); if (!contents) { fail_test(__LINE__, "failed to ls data/test/key1: %x", GetLastError()); } else if (count != 0) { fail_test(__LINE__, "ls data/test/key1 had %d items", count); free(contents); } else { free(contents); } if (!xs_write(xs_handle, "data/test/key1/key2", "data2")) { fail_test(__LINE__, "failed to rewrite data/test/key1/key2"); } contents = xs_directory(xs_handle, "data/test/key1", &count); if (!contents) { fail_test(__LINE__, "failed to ls data/test/key1: %x", GetLastError()); } else if (count != 1) { fail_test(__LINE__, "ls data/test/key1 had %d items", count); free(contents); } else if (strcmp(contents[0], "key2")) { fail_test(__LINE__, "ls data/test/key1 gave unexpected result %s", contents[0]); } xs_remove(xs_handle, "data/test"); /* Looks like most of the basic functionality works. Try * transactions. */ xs_handle2 = xs_domain_open(); if (!xs_handle2) win_err(1, "couldn't re-open domain interface"); if (!xs_write(xs_handle, "data/test/key1", "before")) fail_test(__LINE__, "failed to write to data/test/key1: %x", GetLastError()); if (!xs_transaction_start(xs_handle2)) win_err(1, "couldn't open a transaction on second handle"); if (!xs_write(xs_handle2, "data/test/key1", "after")) fail_test(__LINE__, "failed to write to data/test/key1 under transaction: %x", GetLastError()); if (!xs_transaction_end(xs_handle2, FALSE)) fail_test(__LINE__, "failed to write to end transaction: %x", GetLastError()); if (strcmp(xs_read(xs_handle, "data/test/key1", NULL), "after")) fail_test(__LINE__, "transaction didn't stick"); /* Now try aborting the transaction. */ if (!xs_write(xs_handle, "data/test/key1", "before")) fail_test(__LINE__, "failed to write to data/test/key1: %x", GetLastError()); if (!xs_transaction_start(xs_handle2)) win_err(1, "couldn't open a transaction on second handle"); if (!xs_write(xs_handle2, "data/test/key1", "after")) fail_test(__LINE__, "failed to write to data/test/key1 under transaction: %x", GetLastError()); if (!xs_transaction_end(xs_handle2, TRUE)) fail_test(__LINE__, "failed to write to end transaction: %x", GetLastError()); if (strcmp(xs_read(xs_handle, "data/test/key1", NULL), "before")) fail_test(__LINE__, "transaction didn't abort"); /* Try to arrange that the transaction fails. */ if (!xs_write(xs_handle, "data/test/key1", "before")) fail_test(__LINE__, "failed to write to data/test/key1: %x", GetLastError()); if (!xs_transaction_start(xs_handle2)) win_err(1, "couldn't open a transaction on second handle"); if (!xs_write(xs_handle2, "data/test/key1", "after")) fail_test(__LINE__, "failed to write to data/test/key1 under transaction: %x", GetLastError()); if (!xs_write(xs_handle, "data/test/key1", "other")) fail_test(__LINE__, "failed to write to data/test/key1: %x", GetLastError()); if (xs_transaction_end(xs_handle2, FALSE)) fail_test(__LINE__, "transaction succeeded when it shouldn't", GetLastError()); if (strcmp(xs_read(xs_handle, "data/test/key1", NULL), "other")) fail_test(__LINE__, "transaction did something strange"); if (!xs_write(xs_handle, "data/test/key1", "before1")) fail_test(__LINE__, "failed to write to data/test/key1: %x", GetLastError()); if (!xs_write(xs_handle, "data/test/key2", "before2")) fail_test(__LINE__, "failed to write to data/test/key2: %x", GetLastError()); if (!xs_transaction_start(xs_handle2)) win_err(1, "couldn't open a transaction on second handle"); if (!xs_write(xs_handle2, "data/test/key1", "after")) fail_test(__LINE__, "failed to write to data/test/key1 under transaction: %x", GetLastError()); t = xs_read(xs_handle2, "data/test/key2", NULL); if (!t) { fail_test(__LINE__, "failed to read data/test/key2 under transaction: %x", GetLastError()); } else { if (strcmp(t, "before2")) fail_test(__LINE__, "got wrong thing reading dtaa/test/key2 (%s)", t); free(t); } if (!xs_write(xs_handle, "data/test/key2", "other")) fail_test(__LINE__, "failed to write to data/test/key1: %x", GetLastError()); if (xs_transaction_end(xs_handle2, FALSE)) fail_test(__LINE__, "transaction succeeded when it shouldn't", GetLastError()); if (strcmp(xs_read(xs_handle, "data/test/key1", NULL), "before1")) fail_test(__LINE__, "transaction did something strange"); xs_daemon_close(xs_handle2); /* Try a couple of transaction error cases. */ xs_handle2 = xs_domain_open(); if (!xs_handle2) win_err(1, "couldn't re-open domain interface a second time"); if (!xs_transaction_start(xs_handle2)) win_err(1, "couldn't open a transaction for re-test"); if (xs_transaction_start(xs_handle2)) { fail_test(__LINE__, "openned two transactions on same handle"); } xs_daemon_close(xs_handle2); xs_handle2 = xs_domain_open(); if (!xs_handle2) win_err(1, "couldn't re-open domain interface a third time"); if (xs_transaction_end(xs_handle2, FALSE)) { fail_test(__LINE__, "ended transaction without starting it"); } if (!xs_transaction_start(xs_handle2)) win_err(1, "couldn't open a transaction for re-test"); if (!xs_transaction_end(xs_handle2, FALSE)) fail_test(__LINE__, "failed to end transaction"); if (xs_transaction_end(xs_handle2, FALSE)) { fail_test(__LINE__, "double-ended transaction"); } xs_daemon_close(xs_handle2); /* Transactions appear to be working, at least in their most basic form. Have a go at watches. */ event = CreateEvent(NULL, FALSE, FALSE, NULL); watch_h = xs_watch(xs_handle, "data/test/key1", event); if (watch_h < 0) { fail_test(__LINE__, "couldn't watch data/test/key1"); } else { while (WaitForSingleObject(event, 100) != WAIT_TIMEOUT) ; xs_write(xs_handle, "data/test/key1", "foo"); if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) fail_test(__LINE__, "failed wait for data/test/key1: %x", GetLastError()); xs_write(xs_handle, "data/test/key1", "foo"); if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) fail_test(__LINE__, "failed wait for data/test/key1: %x", GetLastError()); xs_write(xs_handle, "data/test/key1", "foo"); if (WaitForSingleObject(event, INFINITE) != WAIT_OBJECT_0) fail_test(__LINE__, "failed wait for data/test/key1: %x", GetLastError()); status = WaitForSingleObject(event, 2000); if (status != WAIT_TIMEOUT) fail_test(__LINE__, "should have timed out waiting for data/test/key1 (%d, %d)", status, GetLastError()); if (!xs_unwatch(xs_handle, watch_h)) fail_test(__LINE__, "failed to unwatch"); } /* Create two watches on the same key, kill one of them, and then make sure that the other one still works. */ watch_h = xs_watch(xs_handle, "data/test/key1/subkey", event); if (watch_h < 0) { fail_test(__LINE__, "couldn't watch data/test/key1/subkey"); } else { event2 = CreateEvent(NULL, FALSE, FALSE, NULL); watch_h2 = xs_watch(xs_handle, "data/test/key1/subkey", event); if (watch_h2 < 0) { fail_test(__LINE__, "couldn't double watch data/test/key1/subkey"); } else { if (!xs_unwatch(xs_handle, watch_h2)) fail_test(__LINE__, "failed to unwatch h2"); ResetEvent(event); xs_remove(xs_handle, "data/test/key1"); if (WaitForSingleObject(event, 5000) != WAIT_OBJECT_0) fail_test(__LINE__, "failed wait for data/test/key1: %x", GetLastError()); if (!xs_unwatch(xs_handle, watch_h)) fail_test(__LINE__, "failed to unwatch"); } } /* Watch a node, then modify it in a transaction, and check that the watch fires. */ watch_h = xs_watch(xs_handle, "data/test/key1", event); if (watch_h < 0) { fail_test(__LINE__, "couldn't watch data/test/key1"); } else { for (i = 0; i < 100; i++) { ResetEvent(event); do { if (!xs_transaction_start(xs_handle)) win_err(1, "couldn't open a transaction for watch test"); xs_write(xs_handle, "data/test/key1", "foo"); } while (!xs_transaction_end(xs_handle, FALSE)); if (WaitForSingleObject(event, 5000) != WAIT_OBJECT_0) fail_test(__LINE__, "failed wait for data/test/key1(%d): %x", i, GetLastError()); } if (!xs_unwatch(xs_handle, watch_h)) fail_test(__LINE__, "failed to unwatch"); } /* Make a lot of watches, make sure they all work. */ test_many_watches(); /* Try some different sized requests */ test_write_sizes(4096); xs_daemon_close(xs_handle); run_stress(); if (failed) { printf("failed\n"); return 1; } else { printf("passed\n"); return 0; } }
int destroy_domain(char *domname) { int domid, rcode, i, DMid, status; int __attribute__((__unused__)) ret; #ifdef XENCTRL_HAS_XC_INTERFACE xc_interface *xc_handle = NULL; #else int xc_handle = 0; #endif unsigned int len; char *s; char path[256]; struct xs_handle *xsh = NULL; FILE *f; char buf[256]; char *backend; xsh = xs_daemon_open(); if (!xsh) { perror("Couldn't get xsh handle."); rcode = 1; goto out; } domid = getDomidByName(xsh, domname); if (domid < 1) { perror("Error,Can't destroy domId, domId should > 0\n"); rcode = 1; goto out; } #ifdef XENCTRL_HAS_XC_INTERFACE xc_handle = xc_interface_open(NULL, NULL, 0); if (xc_handle == NULL) { #else xc_handle = xc_interface_open(); if (xc_handle < 0) { #endif perror("Couldn't open xc handle."); rcode = 1; goto out; } /* TODO PCI clean paths = self._prepare_phantom_paths() if self.dompath is not None: self._cleanup_phantom_devs(paths) xc_domain_destroy_hook(xc_handle, domid); */ xc_domain_pause(xc_handle, domid); rcode = xc_domain_destroy(xc_handle, domid); // free Device Model sprintf(path,"/local/domain/%d/image/device-model-pid", domid); s = xs_read(xsh, XBT_NULL, path, &len); if( s != NULL) { DMid = atoi(s); free(s); DEBUG("Deivce Model Id is %d\n", DMid); } else { rcode = 1; DEBUG("can't read Deivce Model Id\n"); goto out; } kill(DMid, SIGHUP); for( i=0; i< 100; i++) { if(DMid == waitpid(DMid, &status, WNOHANG)) break; sleep(0.1); } if( i == 100) { DEBUG("DeviceModel %d took more than 10s " "to terminate: sending SIGKILL\n", DMid); kill(DMid, SIGKILL); waitpid(DMid, &status, 0); } sprintf(path,"/local/domain/0/device-model/%i", domid); xs_rm(xsh, XBT_NULL, path); // unlink pipe sprintf(path,"/var/run/tap/qemu-read-%d", domid); ret = unlink(path); sprintf(path,"/var/run/tap/qemu-write-%d", domid); ret = unlink(path); //notify backend to reap the source assigned to this VM sprintf(path, "xenstore-ls /local/domain/%d/device > /var/tmp/xdestroy_device.%d", domid, domid); ret = system(path); sprintf(path, "/var/tmp/xdestroy_device.%d", domid); f = fopen(path, "r"); if ( f == NULL) { DEBUG(" error to open device file\n"); return -1; } DEBUG("- begin to reap\n"); while(1) { if( fgets(buf, 256, f)== NULL) break; else { if( buf[0] != 0x20) { backend = device_backend(xsh, buf, domid); if( backend != NULL) { notify_backend(xsh, backend); free(backend); } } } } DEBUG("- end to reap\n"); fclose(f); sprintf(path, "rm /var/tmp/xdestroy_device.%d", domid); ret = system(path); extra_call(domname, DMid); out: if (xsh) xs_daemon_close(xsh); if (xc_handle) xc_interface_close(xc_handle); return rcode; } int main(int argc, char **argv) { if (argc != 2) { printf("Miss destroy name\n"); return -1; } xlist(argc, argv); //shutdown_domain(argv[1]); destroy_domain(argv[1]); return 0; }
int close(int fd) { printk("close(%d)\n", fd); if (fd < 0 || fd > NOFILE) { errno = EBADF; return -1; } switch (files[fd].type) { default: files[fd].type = FTYPE_NONE; return 0; case FTYPE_XENBUS: xs_daemon_close((void*)(intptr_t) fd); return 0; #ifdef HAVE_LWIP case FTYPE_SOCKET: { int res = lwip_close(files[fd].socket.fd); files[fd].type = FTYPE_NONE; return res; } #endif case FTYPE_XC: minios_interface_close_fd(fd); return 0; case FTYPE_EVTCHN: minios_evtchn_close_fd(fd); return 0; case FTYPE_GNTMAP: printk("close gntmap fd %d; gntmap %p, %d entries\n", fd, files[fd].gntmap.entries, files[fd].gntmap.nentries); minios_gnttab_close_fd(fd); return 0; case FTYPE_TAP: shutdown_netfront(files[fd].tap.dev); files[fd].type = FTYPE_NONE; return 0; case FTYPE_BLK: shutdown_blkfront(files[fd].blk.dev); files[fd].type = FTYPE_NONE; return 0; case FTYPE_KBD: shutdown_kbdfront(files[fd].kbd.dev); files[fd].type = FTYPE_NONE; return 0; case FTYPE_FB: shutdown_fbfront(files[fd].fb.dev); files[fd].type = FTYPE_NONE; return 0; case FTYPE_SAVEFILE: case FTYPE_CONSOLE: fini_console(files[fd].cons.dev); files[fd].type = FTYPE_NONE; return 0; case FTYPE_NONE: break; } printk("close(%d): Bad descriptor\n", fd); errno = EBADF; return -1; }
int main(int argc, char *argv[]) { char *devname; tapdev_info_t *ctlinfo; int tap_pfd, store_pfd, xs_fd, ret, timeout, pfd_count, count=0; struct xs_handle *h; struct pollfd pfd[NUM_POLL_FDS]; pid_t process; char buf[128]; __init_blkif(); snprintf(buf, sizeof(buf), "BLKTAPCTRL[%d]", getpid()); openlog(buf, LOG_CONS|LOG_ODELAY, LOG_DAEMON); if (daemon(0,0)) { DPRINTF("daemon failed (%d)\n", errno); goto open_failed; } print_drivers(); init_driver_list(); init_rng(); register_new_blkif_hook(blktapctrl_new_blkif); register_new_devmap_hook(map_new_blktapctrl); register_new_unmap_hook(unmap_blktapctrl); ctlfd = blktap_interface_open(); if (ctlfd < 0) { DPRINTF("couldn't open blktap interface\n"); goto open_failed; } #ifdef MEMSHR memshr_daemon_initialize(); #endif retry: /* Set up store connection and watch. */ h = xs_daemon_open(); if (h == NULL) { DPRINTF("xs_daemon_open failed -- " "is xenstore running?\n"); if (count < MAX_ATTEMPTS) { count++; sleep(2); goto retry; } else goto open_failed; } ret = setup_probe_watch(h); if (ret != 0) { DPRINTF("Failed adding device probewatch\n"); xs_daemon_close(h); goto open_failed; } ioctl(ctlfd, BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_INTERPOSE ); process = getpid(); write_pidfile(process); ret = ioctl(ctlfd, BLKTAP_IOCTL_SENDPID, process ); /*Static pollhooks*/ pfd_count = 0; tap_pfd = pfd_count++; pfd[tap_pfd].fd = ctlfd; pfd[tap_pfd].events = POLLIN; store_pfd = pfd_count++; pfd[store_pfd].fd = xs_fileno(h); pfd[store_pfd].events = POLLIN; while (run) { timeout = 1000; /*Milliseconds*/ ret = poll(pfd, pfd_count, timeout); if (ret > 0) { if (pfd[store_pfd].revents) { ret = xs_fire_next_watch(h); } } } xs_daemon_close(h); ioctl(ctlfd, BLKTAP_IOCTL_SETMODE, BLKTAP_MODE_PASSTHROUGH ); close(ctlfd); closelog(); return 0; open_failed: DPRINTF("Unable to start blktapctrl\n"); closelog(); return -1; }
int main (int argc, char **argv) { data_t data = { 0, }; int retval = EXIT_SUCCESS, i = 0, cat_result = 0; openlog (argv[0], LOG_NOWAIT | LOG_PID, LOG_DAEMON); if (argc < 2) { syslog (LOG_EMERG, "argc is less than 2, that's very wrong"); retval = EXIT_FAILURE; goto exit; } if (is_selinux_enabled () != 1) { syslog (LOG_WARNING, "SELinux is disabled. sVirt will do nothing."); goto exit; } /* not really parsing parameters, just going on position */ data.domid = atoi (argv [1]); syslog (LOG_INFO, "domain id: %d", data.domid); data.xsh = xs_daemon_open(); if (data.xsh == NULL) { syslog (LOG_CRIT, "ERROR connecting to XenStore. Halting"); retval = EXIT_FAILURE; goto exit; } /* get files that we need to relabele */ data.files = get_writable_files (data.xsh, data.domid); if (data.files == NULL) { syslog (LOG_CRIT, "ERROR getting files. Halting"); retval = EXIT_FAILURE; goto exit_session; } for (i = 0; data.files [i] != NULL; ++i) syslog (LOG_INFO, "got file: %s", data.files [i]); /* get category for our domid and save it to xenstore */ cat_result = create_category (data.xsh); if (cat_result < 0) { syslog (LOG_CRIT, "ERROR generating unique category. Halting"); retval = EXIT_FAILURE; goto exit_files; } data.category = cat_result; syslog (LOG_INFO, "got unique mcs: %d", data.category); if (set_domid_category (data.xsh, data.domid, data.category) == false) { syslog (LOG_CRIT, "ERROR setting category. Halting"); retval = EXIT_FAILURE; goto exit_files; } /* SELinux stuff */ /* get SELinux default contexts */ if (get_default_contexts (&data) != 0) { syslog (LOG_CRIT, "ERROR getting default contexts. Halting"); retval = EXIT_FAILURE; goto exit_files; } /* label files */ if (file_con_fixup (&data) != 0) { syslog (LOG_CRIT, "ERROR setting contexts for VM device files. Halting"); retval = EXIT_FAILURE; goto exit_files; } /* Set Execution Context */ if (set_exec_context (&data) != true) { syslog (LOG_CRIT, "ERROR setting context to %s for qemu execution: %s. Halting", strerror (errno)); retval = EXIT_FAILURE; goto exit_files; } syslog (LOG_INFO, "Successfully set set MCS label %d for domid %d", data.category, data.domid); exit_files: if (data.files != NULL) { for (i = 0; data.files [i] != NULL; ++i) free (data.files [i]); free (data.files); } exit_session: if (data.xsh != NULL) xs_daemon_close (data.xsh); exit: closelog (); /* execute the real qemu if no previous errors prevent it */ if (retval != EXIT_FAILURE) exec_cmd (argv); exit (retval); }
int read_config_file (xa_instance_t *instance) { extern FILE *yyin; int ret = XA_SUCCESS; xa_config_entry_t *entry; #ifdef ENABLE_XEN struct xs_handle *xsh = NULL; xs_transaction_t xth = XBT_NULL; #endif /* ENABLE_XEN */ char *tmp = NULL; yyin = fopen("/etc/xenaccess.conf", "r"); if (NULL == yyin){ fprintf(stderr, "ERROR: config file not found at /etc/xenaccess.conf\n"); ret = XA_FAILURE; goto error_exit; } /* convert domain id to domain name for Xen mode */ if (XA_MODE_XEN == instance->mode){ #ifdef ENABLE_XEN tmp = malloc(100); if (NULL == tmp){ fprintf(stderr, "ERROR: failed to allocate memory for tmp variable\n"); ret = XA_FAILURE; goto error_exit; } memset(tmp, 0, 100); sprintf(tmp, "/local/domain/%d/name", instance->m.xen.domain_id); xsh = xs_domain_open(); instance->image_type = xs_read(xsh, xth, tmp, NULL); if (NULL == instance->image_type){ fprintf(stderr, "ERROR: domain id %d is not running\n", instance->m.xen.domain_id); ret = XA_FAILURE; goto error_exit; } xa_dbprint("--got domain name from id (%d ==> %s).\n", instance->m.xen.domain_id, instance->image_type); #endif /* ENABLE_XEN */ } if (xa_parse_config(instance->image_type)){ fprintf(stderr, "ERROR: failed to read config file\n"); ret = XA_FAILURE; goto error_exit; } entry = xa_get_config(); /* copy the values from entry into instance struct */ instance->sysmap = strdup(entry->sysmap); xa_dbprint("--got sysmap from config (%s).\n", instance->sysmap); if (strncmp(entry->ostype, "Linux", CONFIG_STR_LENGTH) == 0){ instance->os_type = XA_OS_LINUX; } else if (strncmp(entry->ostype, "Windows", CONFIG_STR_LENGTH) == 0){ instance->os_type = XA_OS_WINDOWS; } else{ fprintf(stderr, "ERROR: Unknown or undefined OS type.\n"); ret = XA_FAILURE; goto error_exit; } /* Copy config info based on OS type */ if(XA_OS_LINUX == instance->os_type){ xa_dbprint("--reading in linux offsets from config file.\n"); if(entry->offsets.linux_offsets.tasks){ instance->os.linux_instance.tasks_offset = entry->offsets.linux_offsets.tasks; } if(entry->offsets.linux_offsets.mm){ instance->os.linux_instance.mm_offset = entry->offsets.linux_offsets.mm; } if(entry->offsets.linux_offsets.pid){ instance->os.linux_instance.pid_offset = entry->offsets.linux_offsets.pid; } if(entry->offsets.linux_offsets.pgd){ instance->os.linux_instance.pgd_offset = entry->offsets.linux_offsets.pgd; } if(entry->offsets.linux_offsets.addr){ instance->os.linux_instance.addr_offset = entry->offsets.linux_offsets.addr; } } else if (XA_OS_WINDOWS == instance->os_type){ xa_dbprint("--reading in windows offsets from config file.\n"); if(entry->offsets.windows_offsets.ntoskrnl){ instance->os.windows_instance.ntoskrnl = entry->offsets.windows_offsets.ntoskrnl; } if(entry->offsets.windows_offsets.tasks){ instance->os.windows_instance.tasks_offset = entry->offsets.windows_offsets.tasks; } if(entry->offsets.windows_offsets.pdbase){ instance->os.windows_instance.pdbase_offset = entry->offsets.windows_offsets.pdbase; } if(entry->offsets.windows_offsets.pid){ instance->os.windows_instance.pid_offset = entry->offsets.windows_offsets.pid; } if(entry->offsets.windows_offsets.peb){ instance->os.windows_instance.peb_offset = entry->offsets.windows_offsets.peb; } if(entry->offsets.windows_offsets.iba){ instance->os.windows_instance.iba_offset = entry->offsets.windows_offsets.iba; } if(entry->offsets.windows_offsets.ph){ instance->os.windows_instance.ph_offset = entry->offsets.windows_offsets.ph; } } #ifdef XA_DEBUG xa_dbprint("--got ostype from config (%s).\n", entry->ostype); if (instance->os_type == XA_OS_LINUX){ xa_dbprint("**set instance->os_type to Linux.\n"); } else if (instance->os_type == XA_OS_WINDOWS){ xa_dbprint("**set instance->os_type to Windows.\n"); } else{ xa_dbprint("**set instance->os_type to unknown.\n"); } #endif error_exit: if (tmp) free(tmp); if (yyin) fclose(yyin); #ifdef ENABLE_XEN if (xsh) xs_daemon_close(xsh); #endif /* ENABLE_XEN */ return ret; }
struct libxenvchan *libxenvchan_client_init(xentoollog_logger *logger, int domain, const char* xs_path) { struct libxenvchan *ctrl = malloc(sizeof(struct libxenvchan)); struct xs_handle *xs = NULL; char buf[64]; char *ref; int ring_ref; unsigned int len; if (!ctrl) return 0; ctrl->ring = NULL; ctrl->event = NULL; ctrl->gnttab = NULL; ctrl->write.order = ctrl->read.order = 0; ctrl->is_server = 0; xs = xs_daemon_open(); if (!xs) xs = xs_domain_open(); if (!xs) goto fail; // find xenstore entry snprintf(buf, sizeof buf, "%s/ring-ref", xs_path); ref = xs_read(xs, 0, buf, &len); if (!ref) goto fail; ring_ref = atoi(ref); free(ref); if (!ring_ref) goto fail; snprintf(buf, sizeof buf, "%s/event-channel", xs_path); ref = xs_read(xs, 0, buf, &len); if (!ref) goto fail; ctrl->event_port = atoi(ref); free(ref); if (!ctrl->event_port) goto fail; ctrl->gnttab = xc_gnttab_open(logger, 0); if (!ctrl->gnttab) goto fail; // set up event channel if (init_evt_cli(ctrl, domain, logger)) goto fail; // set up shared page(s) if (init_gnt_cli(ctrl, domain, ring_ref)) goto fail; ctrl->ring->cli_live = 1; ctrl->ring->srv_notify = VCHAN_NOTIFY_WRITE; out: if (xs) xs_daemon_close(xs); return ctrl; fail: libxenvchan_close(ctrl); ctrl = NULL; goto out; }
int close(int fd) { printk("close(%d)\n", fd); switch (files[fd].type) { default: files[fd].type = FTYPE_NONE; return 0; #ifdef CONFIG_XENBUS case FTYPE_XENBUS: xs_daemon_close((void*)(intptr_t) fd); return 0; #endif #ifdef HAVE_LWIP case FTYPE_SOCKET: { int res = lwip_close(files[fd].socket.fd); files[fd].type = FTYPE_NONE; return res; } #endif #ifdef CONFIG_XC case FTYPE_XC: minios_interface_close_fd(fd); return 0; case FTYPE_EVTCHN: minios_evtchn_close_fd(fd); return 0; case FTYPE_GNTMAP: minios_gnttab_close_fd(fd); return 0; #endif #ifdef CONFIG_NETFRONT case FTYPE_TAP: shutdown_netfront(files[fd].tap.dev); files[fd].type = FTYPE_NONE; return 0; #endif #ifdef CONFIG_BLKFRONT case FTYPE_BLK: shutdown_blkfront(files[fd].blk.dev); files[fd].type = FTYPE_NONE; return 0; #endif #ifdef CONFIG_TPMFRONT case FTYPE_TPMFRONT: shutdown_tpmfront(files[fd].tpmfront.dev); files[fd].type = FTYPE_NONE; return 0; #endif #ifdef CONFIG_TPM_TIS case FTYPE_TPM_TIS: shutdown_tpm_tis(files[fd].tpm_tis.dev); files[fd].type = FTYPE_NONE; return 0; #endif #ifdef CONFIG_KBDFRONT case FTYPE_KBD: shutdown_kbdfront(files[fd].kbd.dev); files[fd].type = FTYPE_NONE; return 0; #endif #ifdef CONFIG_FBFRONT case FTYPE_FB: shutdown_fbfront(files[fd].fb.dev); files[fd].type = FTYPE_NONE; return 0; #endif #ifdef CONFIG_CONSFRONT case FTYPE_SAVEFILE: case FTYPE_CONSOLE: fini_console(files[fd].cons.dev); files[fd].type = FTYPE_NONE; return 0; #endif case FTYPE_NONE: break; } printk("close(%d): Bad descriptor\n", fd); errno = EBADF; return -1; }