int main(int argc, char *argv[]) { struct winsize ws; int ret, c, socket = 0, show_perm = 0; struct xs_handle *xsh; #define PAD 2 memset(&ws, 0, sizeof(ws)); ret = ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); if (!ret) max_width = ws.ws_col - PAD; while (0 < (c = getopt(argc, argv, "pswf"))) { switch (c) { case 'w': max_width= STRING_MAX - PAD; desired_width = 0; break; case 'p': show_perm = 1; break; case 's': socket = 1; break; case 'f': max_width = INT_MAX/2; desired_width = 0; show_whole_path = 1; break; case ':': case '?': default: usage(argc, argv); return 0; } } /* Adjust the width here to avoid argument order dependency */ if ( show_perm ) { max_width -= 16; } xsh = socket ? xs_daemon_open() : xs_domain_open(); if (xsh == NULL) err(1, socket ? "xs_daemon_open" : "xs_domain_open"); print_dir(xsh, (argc - optind) == 1 ? argv[optind] : "/", 0, show_perm); return 0; }
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; }
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; }
static int get_dom_id() { struct xs_handle *xsh = NULL; char *domid = NULL; int domID = -1; unsigned int len; xsh = xs_domain_open(); if (xsh == NULL) { libmsg("xs_domain_open: cannot open xenstore\n"); return -1; } domid = xs_read(xsh, XBT_NULL, "domid", &len); if (domid) { domID = atoi(domid); free(domid); } return domID; }
int xenstore_init(void) { unsigned int len, domid; char *buf; char *end; xs = xs_domain_open(); if (xs == NULL) { RTE_LOG(ERR, PMD,"%s: xs_domain_open failed\n", __func__); return -1; } buf = xs_read(xs, XBT_NULL, "domid", &len); if (buf == NULL) { RTE_LOG(ERR, PMD, "%s: failed read domid\n", __func__); return -1; } errno = 0; domid = strtoul(buf, &end, 0); if (errno != 0 || end == NULL || end == buf || domid == 0) return -1; RTE_LOG(INFO, PMD, "retrieved dom ID = %d\n", domid); dompath = xs_get_domain_path(xs, domid); if (dompath == NULL) return -1; xs_transaction_start(xs); /* When to stop transaction */ if (is_xenstore_cleaned_up == 0) { if (xenstore_cleanup()) return -1; is_xenstore_cleaned_up = 1; } return 0; }
main(int argc, char **argv) { struct xs_handle *xs; unsigned int count; char **vec; char dummy; if (argc != 2) { fprintf(stderr, "usage: %s xenstore_path\n", argv[0]); exit(1); } xs = xs_domain_open(); if (!xs) { perror("xs_domain_open"); exit(1); } if (!xs_watch(xs, argv[1], &dummy)) { perror("xs_watch"); exit(1); } vec = xs_read_watch(xs, &count); free(vec); vec = xs_read_watch(xs, &count); free(vec); }
main( int argc, char **argv) { vxt_db_start_arg_t start_db_args; struct xs_handle *xs; char token; char **vec; char *tail; int ret; int parse_result; int num; int domid; int auth_dev; fd_set set; int fd; char target_uuid[MAX_VXT_UUID] = " "; int is_provision = 0; if (daemon(0, 0) < 0) { perror("daemon"); } auth_dev = open(VXT_AUTH_DEVICE_NAME, O_RDWR); if(auth_dev < 0) { printf("main: Failed to open the Vxt authorization" " database device\n"); } /* * Start the database if it hasn't already been * started */ strncpy(start_db_args.db_file_name, VXT_DB_DEFAULT_FILE_NAME, VXT_DB_NAME_MAX); ioctl(auth_dev, VXT_AUTH_DB_START, (void *)(long)&start_db_args); /* printf("The domain device %s\n", xs_domain_dev()); */ xs = xs_domain_open(); if (!xs) { perror("xs_domain_open failure"); exit(1); } ret = xs_watch(xs, "/vm", "vxtToken"); if (ret == 0) { perror("xs_watch failed\n"); exit(1); } fd = xs_fileno(xs); while (1) { FD_ZERO(&set); FD_SET(fd, &set); /* * Poll for data * printf("Poll for data, 0x%x\n", fd); */ if ((ret = select(fd+1, &set, NULL, NULL, NULL)) >= 0) { /* printf("Watch point triggered ret = 0x%x\n", ret); */ vec = xs_read_watch(xs, &num); printf("\n after read watch "); if (!vec) error(); parse_result = xsd_vm_name(vec[XS_WATCH_PATH], target_uuid); if (parse_result == 1) { char **list; char path[64]; int entry_cnt; int i; /* * get a list of all the domain ids, * then check the name of each one * to find the id for the specified domain */ list = xs_directory(xs, XBT_NULL, "/local/domain", &entry_cnt); for (i = (entry_cnt-1); i > 0; i--) { char *rec_uuid; sprintf(path, "/local/domain/%s/vm", list[i]); rec_uuid = xs_read(xs, XBT_NULL, path, NULL); if ( rec_uuid == NULL ) { continue; } if (strcmp(&(rec_uuid[4]), target_uuid) == 0) { free(rec_uuid); domid = atoi(list[i]); break; } free(rec_uuid); } free(list); if (i == 0) { free(vec); continue; } xsd_write_auth_record(auth_dev, target_uuid); /* printf("main: call vxt_auth_db on uuid = %s\n", target_uuid); */ xsd_provision_controller(xs, domid); is_provision = 1; update_vm_state(target_uuid, is_provision); } else if (parse_result == 2) { /* treating this case as deprovision */ is_provision = 0; update_vm_state(target_uuid, is_provision); } else { /* target_uuid is undefined * don't call update_vm_state */ } free(vec); } else { break; } } printf("returned from xs_watch, xenstore exited\n"); }
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; }
int main() { int listensock, clientsock; struct sockaddr_in name; struct sockaddr_in clientname; int size; int pid; int opt; signal(SIGCHLD, sigchl_handler); xs = xs_domain_open(); if (!xs) { perror("xs_domain_open"); exit (EXIT_FAILURE); } #if V4V listensock = socket(PF_XENV4V, SOCK_STREAM, 0); #else listensock = socket(PF_INET, SOCK_STREAM, 0); #endif if (listensock < 0) { perror ("socket"); exit (EXIT_FAILURE); } name.sin_family = AF_INET; name.sin_port = htons(PORT); name.sin_addr.s_addr = htonl(INADDR_ANY); opt=1; setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); #if !V4V if (setsockopt(listensock, SOL_SOCKET, SO_BINDTODEVICE, &interface, strlen(interface))) { perror ("setsockopt(SO_BINDTODEVICE)"); exit (EXIT_FAILURE); } #endif if (bind(listensock, (struct sockaddr *)&name, sizeof(name)) < 0) { perror ("bind"); exit (EXIT_FAILURE); } if (listen(listensock, 1) < 0) { perror("listen"); exit(EXIT_FAILURE); } while (1) { size = sizeof(clientname); clientsock = accept(listensock, (struct sockaddr *) &clientname, &size); if (clientsock<0) { if (errno == EAGAIN) { fprintf(stderr, "Interrupted\n"); continue; } perror("accept"); exit(EXIT_FAILURE); } pid = fork(); if (pid < 0) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { doit(clientsock); exit(0); } else { close(clientsock); dprintf("Spawned pid %d for connection from %s:%d\n", pid, inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port)); } } }
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 pa__init(pa_module*m) { struct userdata *u; pa_modargs *ma; pa_sink_new_data data; int backend_state; int ret; char strbuf[100]; pa_assert(m); if (!(ma = pa_modargs_new(m->argument, valid_modargs))) { pa_log("Failed to parse module arguments."); goto fail; } ss = m->core->default_sample_spec; map = m->core->default_channel_map; /* user arguments override these */ if (pa_modargs_get_sample_spec_and_channel_map(ma, &ss, &map, PA_CHANNEL_MAP_DEFAULT) < 0) { pa_log("Invalid sample format specification or channel map"); return 1; } /* Xen Basic init */ xsh = xs_domain_open(); if (xsh==NULL) { pa_log("xs_domain_open failed"); goto fail; } set_state(XenbusStateUnknown); xch = xc_interface_open(NULL, NULL, 0); if (xch==0) { pa_log("xc_interface_open failed"); goto fail; } xce = xc_evtchn_open(NULL, 0); if (xce==0) { pa_log("xc_evtchn_open failed"); goto fail; } /* use only dom0 as the backend for now */ xen_evtchn_port = xc_evtchn_bind_unbound_port(xce, 0); if (xen_evtchn_port == 0) { pa_log("xc_evtchn_bind_unbound_port failed"); } /* get grant reference & map locally */ if (alloc_gref(&gref, (void**)&ioring)) { pa_log("alloc_gref failed"); }; device_id = 0; /* hardcoded for now */ if (register_backend_state_watch()) { pa_log("Xen sink: register xenstore watch failed"); }; publish_param_int("event-channel", xen_evtchn_port); publish_param_int("ring-ref", gref.gref_ids[0]); /* let's ask for something absurd and deal with rejection */ ss.rate = 192000; publish_spec(&ss); ret=0; while (!ret) { backend_state = wait_for_backend_state_change(); if (backend_state == STATE_UNDEFINED) { pa_log("Xen Backend is taking long to respond, still waiting..."); continue; } else if (backend_state == -1) { pa_log("Error while waiting for backend: %s", strerror(errno)); break; goto fail; } ret = state_callbacks[backend_state](); } if (ret!=NEGOTIATION_OK) { pa_log("Negotiation with Xen backend failed!"); return 1; } pa_sample_spec_snprint(strbuf, 100, &ss); pa_log_debug("Negotiation ended, the result was: %s", strbuf); /* End of Phase 2, begin playback cycle */ u = pa_xnew0(struct userdata, 1); u->core = m->core; u->module = m; m->userdata = u; pa_memchunk_reset(&u->memchunk); u->rtpoll = pa_rtpoll_new(); pa_thread_mq_init(&u->thread_mq, m->core->mainloop, u->rtpoll); u->write_type = 0; /* init ring buffer */ ioring->prod_indx = ioring->cons_indx = 0; ioring->usable_buffer_space = BUFSIZE - BUFSIZE % pa_frame_size(&ss); pa_sink_new_data_init(&data); data.driver = __FILE__; data.module = m; pa_sink_new_data_set_name(&data, pa_modargs_get_value(ma, "sink_name", DEFAULT_SINK_NAME)); pa_proplist_sets(data.proplist, PA_PROP_DEVICE_STRING, "xensink"); pa_proplist_setf(data.proplist, PA_PROP_DEVICE_DESCRIPTION, "Xen PV audio sink"); pa_sink_new_data_set_sample_spec(&data, &ss); pa_sink_new_data_set_channel_map(&data, &map); if (pa_modargs_get_proplist(ma, "sink_properties", data.proplist, PA_UPDATE_REPLACE) < 0) { pa_log("Invalid properties"); pa_sink_new_data_done(&data); goto fail; } u->sink = pa_sink_new(m->core, &data, PA_SINK_LATENCY); pa_sink_new_data_done(&data); if (!u->sink) { pa_log("Failed to create sink."); goto fail; } u->sink->parent.process_msg = sink_process_msg; u->sink->userdata = u; pa_sink_set_asyncmsgq(u->sink, u->thread_mq.inq); pa_sink_set_rtpoll(u->sink, u->rtpoll); pa_sink_set_max_request(u->sink, ioring->usable_buffer_space); pa_sink_set_fixed_latency(u->sink, pa_bytes_to_usec(ioring->usable_buffer_space, &u->sink->sample_spec)); u->rtpoll_item = pa_rtpoll_item_new(u->rtpoll, PA_RTPOLL_NEVER, 1); if (!(u->thread = pa_thread_new("xenpv-sink", thread_func, u))) { pa_log("Failed to create thread."); goto fail; } pa_sink_put(u->sink); pa_modargs_free(ma); return 0; fail: if (ma) pa_modargs_free(ma); pa__done(m); 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; } }
/* * dump metrics received from xenstore to the dest file */ int dump_xenstore_metrics(const char *dest_file) { char *buf = NULL, *path = NULL, *metrics = NULL; struct xs_handle *xsh = NULL; unsigned int len; int ret = 0; xmlParserCtxtPtr pctxt = NULL; xmlDocPtr doc = NULL; int domid; FILE *fp; if (dest_file) { fp = fopen(dest_file, "w"); if (fp == NULL) { libmsg("Error, unable to dump metrics from xenstore: %s\n", strerror(errno)); return -1; } } else { fp = stdout; } if ((domid = get_dom_id()) == -1) { libmsg("Unable to derive domID.\n" ); ret = -1; goto out; } xsh = xs_domain_open(); if (xsh == NULL) { libmsg("xs_domain_open() error. errno: %d.\n", errno); ret = -1; goto out; } path = xs_get_domain_path(xsh, domid); if (path == NULL) { libmsg("xs_get_domain_path() error. domid %d.\n", 0); ret = -1; goto out; } asprintf(&buf, "%s/metrics", path); metrics = xs_read(xsh, XBT_NULL, buf, &len); if (metrics == NULL) { libmsg("xs_read(): uuid get error. %s.\n", buf); ret = -1; goto out; } pctxt = xmlNewParserCtxt(); if (!pctxt || !pctxt->sax) { libmsg("%s(): failed to create parser \n", __func__); ret = -1; goto out; } doc = xmlCtxtReadMemory(pctxt, metrics, strlen(metrics), "mdisk.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOWARNING); if (!doc) { libmsg("%s(): libxml failed to xenstore metrics attribute\n", __func__); ret = -1; goto out; } xmlDocFormatDump(fp, doc, 1); out: if (fp && fp != stdout) fclose(fp); if (doc) xmlFreeDoc(doc); if (pctxt) xmlFreeParserCtxt(pctxt); free(path); free(buf); free(metrics); return ret; }
main(int argc, char **argv) { int ret; pthread_t rcv_thread; uint64_t threadid; uint32_t redirect_input = 0; char *parse_string; char send_buf[RSH_BUF_SIZE]; char input_file_name[MAX_FILE_STRING]; int option = 0; int target_endpoint = 0; int input_commands; int i,j; if (argc < 2) { printf("\n\nusage ... \n\n"); printf("\tvxt_rsh <name>\n\n"); return -1; } i = 1; j = 0; input_commands = 0; input_file_name[0] = '\0'; parse_string = NULL; while(i < argc) { /* * We may parse multiple flags or options into * one token if there are no spaces. Hence * the while loop below. */ while ((i < argc) && (argv[i][j] != '\0')) { VXTPRINT(4, "switch on, %c\n", argv[i][j]); switch(argv[i][j]) { case '-': { if (option) { VXTPRINT(4, "double option \"-\"\n"); return command_line_error(); } option = 1; j++; break; } case 'I': { if (option == 1) { VXTPRINT(4, "case I, " "Input redirection\n"); parse_string = input_file_name; } /* * We are now in an option parse * for a string, go to the option * string reader, we may not have a * space to separate the option letter from * its string. */ ret = vxtdb_parse_option_parm(argv, argc, parse_string, &option, &i, &j); if (ret != 0) { return ret; } if (option == 0) { parse_string = NULL; } break; } default: { VXTPRINT(4, "default, i %d, j %d\n",i,j); if (option) { VXTPRINT(4, "in option parsing\n"); if (parse_string == input_file_name) { VXTPRINT(4, "in filestring " "aquisition\n"); strncpy(parse_string, &(argv[i][j]), MAX_FILE_STRING); option = 0; j=0; i++; parse_string [MAX_FILE_STRING - 1] = 0; parse_string = NULL; VXTPRINT(4, "finished getting " "filename after " "space\n"); break; } else { printf("\n\nUnknown Option\n"); return command_line_error(); } } else { if (j != 0) { VXTPRINT(4, "starting in the" "middle of default " "string\n"); return command_line_error(); } if (target_endpoint != 0) { return command_line_error(); } target_endpoint = 1; target_domain = atoi(&argv[i][j]); i++; } } if (i >= argc) { break; } } } option = 0; j = 0; i++; } pthread_mutex_init(&rcv_sync_lock, NULL); pthread_cond_init(&rcv_sync_var, NULL); VXTPRINT(4, "redirect file name: %s\n", input_file_name); if (target_domain == 0) { /* * Default connection to the HUB */ strncpy(target_uuid, VXTDB_DOM0_ENDPOINT, MAX_VXT_UUID); } else { vxt_db_query_arg_t query_arg; int authdev; VXTPRINT(3, "Open VxT Authorization Database\n"); authdev = open("/dev/vxtdb-auth", O_RDWR); if(authdev < 0) { VXTPRINT(0, "\n\nFailed to open the Vxt authorization " "database device\n\n"); return -1; } query_arg.hub = 1; ioctl(authdev, VXT_AUTH_DB_QUERY, (void *)(long)&query_arg); if (!query_arg.hub) { /* * Get the routing and authorization through the * auth db intermediary. */ VXTPRINT(3, "We are not a hub, use default routing." " If in future we utilize a physical device" " With internal routing, code may go here\n"); strncpy(target_uuid, VXTDB_DOM0_ENDPOINT, MAX_VXT_UUID); } else { #ifdef XSTOOLS struct xs_handle *xs; xs_transaction_t th; int len; char ep_id_path[32]; char *xenstore_uuid_path; xs = xs_domain_open(); if (!xs) { perror("xs_domain_open failure"); return -1; } sprintf(ep_id_path, "/local/domain/%d/vm", target_domain); th = xs_transaction_start(xs); xenstore_uuid_path = (char *) xs_read(xs, th, ep_id_path, &len); if (!xenstore_uuid_path) { VXTPRINT(0, "Target domain %d not valid. " "Exiting...\n", target_domain); return -1; } strncpy(target_uuid, &(xenstore_uuid_path[4]), MAX_VXT_UUID-4); free(xenstore_uuid_path); target_uuid[MAX_VXT_UUID-1] = 0; if(!xs_transaction_end(xs, th, 0)) { VXTPRINT(0, "Could not read xenstore database\n"); return -1; } #else strncpy(target_uuid, VXTDB_DOM0_ENDPOINT, MAX_VXT_UUID); #endif } } vrsh_set_sig_handler(); ret = vrsh_create_device(&vsock); if (ret != VXT_SUCCESS) { return -1; } vxt_mb(); ret = pthread_create(&rcv_thread, NULL, (void *) &rsh_rcv_thread, (void *)&vsock); if (ret == -1) { endwin(); return -1; } /* * Starting local interpreter */ while(TRUE) { char buf[1024]; int vxt_send_len; int read_len; size_t len = 0; ssize_t read_size; /* char *line = NULL; read_size = getline(&line, &len, stdin); VXTPRINT(4, "Picked up command: %s of length %d\n", line, read_size); */ if (strnlen(input_file_name, MAX_FILE_STRING) != 0) { input_commands = open(input_file_name, O_RDWR); if(input_commands < 0) { VXTPRINT(0, "\n\n\tFailed to open the Vxt " "input redirection command file\n\n"); return -1; } while ((read_len = read(input_commands, &buf, 1024)) > 0) { vxt_send_len = read_len; while (vxt_send_len != 0) { ret = vxt_send(vsock, buf, &vxt_send_len, VXT_SOCK_SIGNAL | VXT_SOCK_WAIT); read_len -= vxt_send_len; vxt_send_len = read_len; } } pthread_mutex_lock(&rcv_sync_lock); pthread_cond_wait(&rcv_sync_var, &rcv_sync_lock); pthread_mutex_unlock(&rcv_sync_lock); return 0; } while ((read_len = read(STDOUT_FILENO, &buf, 1024)) > 0) { /* * Local debug trace, writes stream bound for * remote to stdout * write(STDOUT_FILENO, &buf, sendlen); */ vxt_send_len = read_len; while (vxt_send_len != 0) { ret = vxt_send(vsock, buf, &vxt_send_len, VXT_SOCK_SIGNAL | VXT_SOCK_WAIT); read_len -= vxt_send_len; vxt_send_len = read_len; } } } }