static int dominfo_from_dom(const char *uri, const char *domain, struct domain **d) { virConnectPtr conn = NULL; virDomainPtr dom = NULL; int ret = 0; conn = virConnectOpen(uri); if (conn == NULL) { printf("Unable to connect to libvirt\n"); goto out; } dom = virDomainLookupByName(conn, domain); if (dom == NULL) { printf("Unable to find domain `%s'\n", domain); goto out; } ret = get_dominfo(dom, d); out: virDomainFree(dom); virConnectClose(conn); return ret; }
int main(int argc, char **argv) { int option = 0; int domid = -1, bus = -1, device = -1; int vassign = 0, vunassign = 0, attach = 0, detach = 0, list = 0, dump = 0; char *action = NULL; dominfo_t di; usbinfo_t ui; int ret = 0; xs_handle = xs_daemon_open(); if (xs_handle == NULL) { xd_log(LOG_ERR, "Failed to connect to xenstore"); exit(1); } if ((xs_dom0path = xs_get_domain_path(xs_handle, 0)) == NULL) { xd_log(LOG_ERR, "Could not get domain 0 path from XenStore"); exit(1); } while ((option = getopt(argc, argv, "D:b:d:")) != -1) { switch (option) { case 'D': domid = atoi(optarg); if (get_dominfo(domid, &di)) exit(1); break; case 'b': bus = atoi(optarg); break; case 'd': device = atoi(optarg); break; } } if (optind != argc-1) { usage(); } action = argv[optind]; vassign = !strcmp(action, "assign"); vunassign = !strcmp(action, "unassign"); attach = !strcmp(action, "attach"); detach = !strcmp(action, "detach"); list = !strcmp(action, "list"); dump = !strcmp(action, "dump"); if (vassign || vunassign || attach || detach || dump) { if (bus < 0 || device < 0) usage(); if (get_usbinfo(bus, device, &ui)) { if (detach) { /* can proceed detaching without detailed device info, we only need bus & dev num */ } else if (vunassign) { /* no device so nothing to unassign */ exit(0); } else { xd_log(LOG_ERR, "Failed to find device %d:%d", bus, device); exit(1); } } } if (optind == argc) { dump_dev(&ui); return 0; } if (dump) dump_dev(&ui); else if (vassign) ret = vusb_assign(ui.usb_vendor, ui.usb_product, 1); else if (vunassign) ret = vusb_assign(ui.usb_vendor, ui.usb_product, 0); else if (attach) { if (domid < 0) usage(); ret = xenstore_create_usb(&di, &ui); } else if (detach) { if (domid < 0) usage(); ret = xenstore_destroy_usb(&di, &ui); } else if (list) { if (domid < 0) usage(); list_domain_devs(&di); } if (ret) xd_log(LOG_ERR, "Operation failed"); return ret == 0 ? 0 : 1; }