static void process_domains(PyObject *domains, char ***rdomains, int *ndomains) { Py_ssize_t i, n; PyObject *item, *data_fast; char **c_domains; char *arg_str, *tmp_str; c_domains = NULL; *rdomains = NULL; *ndomains = 0; if ((data_fast = PySequence_Fast(domains, "argument 1 must be an iterable")) == NULL) { goto cleanup; } n = PySequence_Fast_GET_SIZE(data_fast); if (n > INT_MAX) { PyErr_SetString(PyExc_ValueError, "argument 1 is too long"); goto cleanup; } if (n == 0) { return; } c_domains = (char **)PyMem_Malloc(sizeof(char *) * n+1); if (!c_domains) { PyErr_NoMemory(); goto cleanup; } memset(c_domains, 0, n+1); i = 0; while (i < n) { item = PySequence_Fast_GET_ITEM(data_fast, i); if (!item || !PyArg_Parse(item, "s;args contains a non-string value", &arg_str)) { Py_XDECREF(item); goto cleanup; } Py_DECREF(item); tmp_str = (char *) PyMem_Malloc(strlen(arg_str) + 1); if (!tmp_str) { PyErr_NoMemory(); goto cleanup; } strcpy(tmp_str, arg_str); c_domains[i] = tmp_str; i++; } c_domains[n] = NULL; *rdomains = c_domains; *ndomains = n; return; cleanup: *rdomains = NULL; *ndomains = -1; free_domains(c_domains); }
int libvirt_close(void) { free_block_devices(); free_interface_devices(); free_domains(); if(conn != NULL) virConnectClose(conn); conn = NULL; }
static int lv_shutdown (void) { free_block_devices (); free_interface_devices (); free_domains (); if (conn != NULL) virConnectClose (conn); conn = NULL; ignorelist_free (il_domains); il_domains = NULL; ignorelist_free (il_block_devices); il_block_devices = NULL; ignorelist_free (il_interface_devices); il_interface_devices = NULL; return 0; }
int libvirt_open() { conn = virConnectOpenReadOnly("xen:///"); if(conn == NULL) { printf("libvirt connection open error on uri \n"); return 0; } int n; n = virConnectNumOfDomains(conn); if(n < 0) { printf("Error reading number of domains \n"); return -1; } int i; int *domids; domids = malloc(sizeof(int) * n); if(domids == 0) { printf("libvirt domain ids malloc failed"); return -1; } n = virConnectListDomains(conn, domids, n); if(n < 0) { printf("Error reading list of domains \n"); free(domids); return -1; } free_block_devices(); free_interface_devices(); free_domains(); for (i = 0; i < n ; ++i) { virDomainPtr dom = NULL; const char *name; char *xml = NULL; xmlDocPtr xml_doc = NULL; xmlXPathContextPtr xpath_ctx = NULL; xmlXPathObjectPtr xpath_obj = NULL; int j; //printf("Publishing Domain Id : %d \n ", domids[i]); dom = virDomainLookupByID(conn, domids[i]); if(dom == NULL) { printf("Domain no longer active or moved away .. \n"); } name = virDomainGetName(dom); //printf("Publishing Domain Name : %s \n ", name); if(name == NULL) { printf("Domain name not valid .. \n"); goto cont; } if(add_domain(dom) < 0) { printf("libvirt plugin malloc failed .. \n"); goto cont; } xml = virDomainGetXMLDesc(dom, 0); if(!xml) { printf("Virt domain xml description error ..\n"); goto cont; } //printf("Publishing XML : \n %s \n ", xml); xml_doc = xmlReadDoc((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET); if(xml_doc == NULL) { printf("XML read doc error ..\n"); goto cont; } xpath_ctx = xmlXPathNewContext(xml_doc); xpath_obj = xmlXPathEval((xmlChar *) "/domain/devices/disk/target[@dev]", xpath_ctx); if(xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) { goto cont; } for(j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; node = xpath_obj->nodesetval->nodeTab[j]; if(!node) continue; path = (char *) xmlGetProp (node, (xmlChar *) "dev"); if(!path) continue; add_block_device(dom, path); } xmlXPathFreeObject(xpath_obj); xpath_obj = xmlXPathEval((xmlChar *) "/domain/devices/interface/target[@dev]", xpath_ctx); if(xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) { goto cont; } for(j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; node = xpath_obj->nodesetval->nodeTab[j]; if(!node) continue; path = (char *) xmlGetProp(node, (xmlChar *) "dev"); if(!path) continue; add_interface_device(dom, path); } cont: if(xpath_obj) xmlXPathFreeObject(xpath_obj); if(xpath_ctx) xmlXPathFreeContext(xpath_ctx); if(xml_doc) xmlFreeDoc(xml_doc); if(xml) free(xml); } free(domids); return 0; }
static int refresh_lists (void) { int n; n = virConnectNumOfDomains (conn); if (n < 0) { VIRT_ERROR (conn, "reading number of domains"); return -1; } if (n > 0) { int i; int *domids; /* Get list of domains. */ domids = malloc (sizeof (*domids) * n); if (domids == NULL) { ERROR (PLUGIN_NAME " plugin: malloc failed."); return -1; } n = virConnectListDomains (conn, domids, n); if (n < 0) { VIRT_ERROR (conn, "reading list of domains"); sfree (domids); return -1; } free_block_devices (); free_interface_devices (); free_domains (); /* Fetch each domain and add it to the list, unless ignore. */ for (i = 0; i < n; ++i) { virDomainPtr dom = NULL; const char *name; char *xml = NULL; xmlDocPtr xml_doc = NULL; xmlXPathContextPtr xpath_ctx = NULL; xmlXPathObjectPtr xpath_obj = NULL; int j; dom = virDomainLookupByID (conn, domids[i]); if (dom == NULL) { VIRT_ERROR (conn, "virDomainLookupByID"); /* Could be that the domain went away -- ignore it anyway. */ continue; } name = virDomainGetName (dom); if (name == NULL) { VIRT_ERROR (conn, "virDomainGetName"); goto cont; } if (il_domains && ignorelist_match (il_domains, name) != 0) goto cont; if (add_domain (dom) < 0) { ERROR (PLUGIN_NAME " plugin: malloc failed."); goto cont; } /* Get a list of devices for this domain. */ xml = virDomainGetXMLDesc (dom, 0); if (!xml) { VIRT_ERROR (conn, "virDomainGetXMLDesc"); goto cont; } /* Yuck, XML. Parse out the devices. */ xml_doc = xmlReadDoc ((xmlChar *) xml, NULL, NULL, XML_PARSE_NONET); if (xml_doc == NULL) { VIRT_ERROR (conn, "xmlReadDoc"); goto cont; } xpath_ctx = xmlXPathNewContext (xml_doc); /* Block devices. */ xpath_obj = xmlXPathEval ((xmlChar *) "/domain/devices/disk/target[@dev]", xpath_ctx); if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) goto cont; for (j = 0; j < xpath_obj->nodesetval->nodeNr; ++j) { xmlNodePtr node; char *path = NULL; node = xpath_obj->nodesetval->nodeTab[j]; if (!node) continue; path = (char *) xmlGetProp (node, (xmlChar *) "dev"); if (!path) continue; if (il_block_devices && ignore_device_match (il_block_devices, name, path) != 0) goto cont2; add_block_device (dom, path); cont2: if (path) xmlFree (path); } xmlXPathFreeObject (xpath_obj); /* Network interfaces. */ xpath_obj = xmlXPathEval ((xmlChar *) "/domain/devices/interface[target[@dev]]", xpath_ctx); if (xpath_obj == NULL || xpath_obj->type != XPATH_NODESET || xpath_obj->nodesetval == NULL) goto cont; xmlNodeSetPtr xml_interfaces = xpath_obj->nodesetval; for (j = 0; j < xml_interfaces->nodeNr; ++j) { char *path = NULL; char *address = NULL; xmlNodePtr xml_interface; xml_interface = xml_interfaces->nodeTab[j]; if (!xml_interface) continue; xmlNodePtr child = NULL; for (child = xml_interface->children; child; child = child->next) { if (child->type != XML_ELEMENT_NODE) continue; if (xmlStrEqual(child->name, (const xmlChar *) "target")) { path = (char *) xmlGetProp (child, (const xmlChar *) "dev"); if (!path) continue; } else if (xmlStrEqual(child->name, (const xmlChar *) "mac")) { address = (char *) xmlGetProp (child, (const xmlChar *) "address"); if (!address) continue; } } if (il_interface_devices && (ignore_device_match (il_interface_devices, name, path) != 0 || ignore_device_match (il_interface_devices, name, address) != 0)) goto cont3; add_interface_device (dom, path, address, j+1); cont3: if (path) xmlFree (path); if (address) xmlFree (address); } cont: if (xpath_obj) xmlXPathFreeObject (xpath_obj); if (xpath_ctx) xmlXPathFreeContext (xpath_ctx); if (xml_doc) xmlFreeDoc (xml_doc); sfree (xml); } sfree (domids); } return 0; }
static int Channel_tp_init(Channel *self, PyObject *args, PyObject *kwargs) { int r, flags, tries, ndots, tcp_port, udp_port, optmask, ndomains, socket_send_buffer_size, socket_receive_buffer_size; char *lookups; char **c_domains; double timeout; struct ares_options options; PyObject *servers, *domains, *sock_state_cb; static char *kwlist[] = {"flags", "timeout", "tries", "ndots", "tcp_port", "udp_port", "servers", "domains", "lookups", "sock_state_cb", "socket_send_buffer_size", "socket_receive_buffer_size", NULL}; optmask = 0; flags = tries = ndots = tcp_port = udp_port = socket_send_buffer_size = socket_receive_buffer_size = -1; timeout = -1.0; lookups = NULL; c_domains = NULL; servers = domains = sock_state_cb = NULL; if (self->channel) { PyErr_SetString(PyExc_AresError, "Object already initialized"); return -1; } if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|idiiiiOOsOii:__init__", kwlist, &flags, &timeout, &tries, &ndots, &tcp_port, &udp_port, &servers, &domains, &lookups, &sock_state_cb, &socket_send_buffer_size, &socket_receive_buffer_size)) { return -1; } if (sock_state_cb && !PyCallable_Check(sock_state_cb)) { PyErr_SetString(PyExc_TypeError, "sock_state_cb is not callable"); return -1; } r = ares_library_init(ARES_LIB_INIT_ALL); if (r != ARES_SUCCESS) { RAISE_ARES_EXCEPTION(r); return -1; } self->lib_initialized = True; memset(&options, 0, sizeof(struct ares_options)); if (flags != -1) { options.flags = flags; optmask |= ARES_OPT_FLAGS; } if (timeout != -1) { options.timeout = (int)timeout * 1000; optmask |= ARES_OPT_TIMEOUTMS; } if (tries != -1) { options.tries = tries; optmask |= ARES_OPT_TRIES; } if (ndots != -1) { options.ndots = ndots; optmask |= ARES_OPT_NDOTS; } if (tcp_port != -1) { options.tcp_port = tcp_port; optmask |= ARES_OPT_TCP_PORT; } if (udp_port != -1) { options.udp_port = udp_port; optmask |= ARES_OPT_UDP_PORT; } if (socket_send_buffer_size != -1) { options.socket_send_buffer_size = socket_send_buffer_size; optmask |= ARES_OPT_SOCK_SNDBUF; } if (socket_receive_buffer_size != -1) { options.socket_receive_buffer_size = socket_receive_buffer_size; optmask |= ARES_OPT_SOCK_RCVBUF; } if (sock_state_cb) { options.sock_state_cb = ares__sock_state_cb; options.sock_state_cb_data = (void *)self; optmask |= ARES_OPT_SOCK_STATE_CB; Py_INCREF(sock_state_cb); self->sock_state_cb = sock_state_cb; } if (lookups) { options.lookups = lookups; optmask |= ARES_OPT_LOOKUPS; } if (domains) { process_domains(domains, &c_domains, &ndomains); if (ndomains == -1) { goto error; } options.domains = c_domains; options.ndomains = ndomains; optmask |= ARES_OPT_DOMAINS; } r = ares_init_options(&self->channel, &options, optmask); if (r != ARES_SUCCESS) { RAISE_ARES_EXCEPTION(r); goto error; } free_domains(c_domains); if (servers) { return set_nameservers(self, servers); } return 0; error: free_domains(c_domains); Py_XDECREF(sock_state_cb); return -1; }
int main (int argc, char *argv[]) { setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEBASEDIR); textdomain (PACKAGE); enum { HELP_OPTION = CHAR_MAX + 1 }; static const char *options = "a:c:d:hiP:vVx"; static const struct option long_options[] = { { "add", 1, 0, 'a' }, { "connect", 1, 0, 'c' }, { "csv", 0, 0, 0 }, { "domain", 1, 0, 'd' }, { "format", 2, 0, 0 }, { "help", 0, 0, HELP_OPTION }, { "human-readable", 0, 0, 'h' }, { "inodes", 0, 0, 'i' }, { "long-options", 0, 0, 0 }, { "one-per-guest", 0, 0, 0 }, { "short-options", 0, 0, 0 }, { "uuid", 0, 0, 0 }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } }; struct drv *drvs = NULL; struct drv *drv; const char *format = NULL; bool format_consumed = true; int c; int option_index; size_t max_threads = 0; int err; g = guestfs_create (); if (g == NULL) { fprintf (stderr, _("guestfs_create: failed to create handle\n")); exit (EXIT_FAILURE); } for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); if (c == -1) break; switch (c) { case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); else if (STREQ (long_options[option_index].name, "short-options")) display_short_options (options); else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; } else if (STREQ (long_options[option_index].name, "csv")) { csv = 1; } else if (STREQ (long_options[option_index].name, "one-per-guest")) { /* nothing - left for backwards compatibility */ } else if (STREQ (long_options[option_index].name, "uuid")) { uuid = 1; } else { fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), program_name, long_options[option_index].name, option_index); exit (EXIT_FAILURE); } break; case 'a': OPTION_a; break; case 'c': OPTION_c; break; case 'd': OPTION_d; break; case 'h': human = 1; break; case 'i': inodes = 1; break; case 'P': if (sscanf (optarg, "%zu", &max_threads) != 1) { fprintf (stderr, _("%s: -P option is not numeric\n"), program_name); exit (EXIT_FAILURE); } break; case 'v': OPTION_v; break; case 'V': OPTION_V; break; case 'x': OPTION_x; break; case HELP_OPTION: usage (EXIT_SUCCESS); default: usage (EXIT_FAILURE); } } /* Old-style syntax? There were no -a or -d options in the old * virt-df which is how we detect this. */ if (drvs == NULL) { while (optind < argc) { if (strchr (argv[optind], '/') || access (argv[optind], F_OK) == 0) { /* simulate -a option */ drv = calloc (1, sizeof (struct drv)); if (!drv) { perror ("malloc"); exit (EXIT_FAILURE); } drv->type = drv_a; drv->a.filename = strdup (argv[optind]); if (!drv->a.filename) { perror ("strdup"); exit (EXIT_FAILURE); } drv->next = drvs; drvs = drv; } else { /* simulate -d option */ drv = calloc (1, sizeof (struct drv)); if (!drv) { perror ("malloc"); exit (EXIT_FAILURE); } drv->type = drv_d; drv->d.guest = argv[optind]; drv->next = drvs; drvs = drv; } optind++; } } /* These are really constants, but they have to be variables for the * options parsing code. Assert here that they have known-good * values. */ assert (read_only == 1); assert (inspector == 0); assert (live == 0); /* Must be no extra arguments on the command line. */ if (optind != argc) usage (EXIT_FAILURE); CHECK_OPTION_format_consumed; /* -h and --csv doesn't make sense. Spreadsheets will corrupt these * fields. (RHBZ#600977). */ if (human && csv) { fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"), program_name); exit (EXIT_FAILURE); } /* virt-df has two modes. If the user didn't specify any drives, * then we do the df on every libvirt guest. That's the if-clause * below. If the user specified domains/drives, then we assume they * belong to a single guest. That's the else-clause below. */ if (drvs == NULL) { #if defined(HAVE_LIBVIRT) get_all_libvirt_domains (libvirt_uri); print_title (); err = start_threads (max_threads, g, df_work); free_domains (); #else fprintf (stderr, _("%s: compiled without support for libvirt.\n"), program_name); exit (EXIT_FAILURE); #endif } else { /* Single guest. */ CLEANUP_FREE char *name = NULL; /* Add domains/drives from the command line (for a single guest). */ add_drives (drvs, 'a'); if (guestfs_launch (g) == -1) exit (EXIT_FAILURE); print_title (); /* Synthesize a display name. */ name = make_display_name (drvs); /* XXX regression: in the Perl version we cached the UUID from the * libvirt domain handle so it was available to us here. In this * version the libvirt domain handle is hidden inside * guestfs_add_domain so the UUID is not available easily for * single '-d' command-line options. */ err = df_on_handle (g, name, NULL, stdout); /* Free up data structures, no longer needed after this point. */ free_drives (drvs); } guestfs_close (g); exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE); }
int main (int argc, char *argv[]) { setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEBASEDIR); textdomain (PACKAGE); enum { HELP_OPTION = CHAR_MAX + 1 }; static const char *options = "a:c:d:P:qvVx"; static const struct option long_options[] = { { "add", 1, 0, 'a' }, { "connect", 1, 0, 'c' }, { "domain", 1, 0, 'd' }, { "format", 2, 0, 0 }, { "help", 0, 0, HELP_OPTION }, { "long-options", 0, 0, 0 }, { "quiet", 0, 0, 'q' }, { "uuid", 0, 0, 0, }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { 0, 0, 0, 0 } }; struct drv *drvs = NULL; const char *format = NULL; bool format_consumed = true; int c; int option_index; int exit_code; size_t max_threads = 0; int r; g = guestfs_create (); if (g == NULL) { fprintf (stderr, _("guestfs_create: failed to create handle\n")); exit (EXIT_FAILURE); } for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); if (c == -1) break; switch (c) { case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "long-options")) display_long_options (long_options); else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; } else if (STREQ (long_options[option_index].name, "uuid")) { uuid = 1; } else { fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), program_name, long_options[option_index].name, option_index); exit (EXIT_FAILURE); } break; case 'a': OPTION_a; break; case 'c': OPTION_c; break; case 'd': OPTION_d; break; case 'P': if (sscanf (optarg, "%zu", &max_threads) != 1) { fprintf (stderr, _("%s: -P option is not numeric\n"), program_name); exit (EXIT_FAILURE); } break; case 'q': quiet = 1; break; case 'v': OPTION_v; break; case 'V': OPTION_V; break; case 'x': OPTION_x; break; case HELP_OPTION: usage (EXIT_SUCCESS); default: usage (EXIT_FAILURE); } } /* These are really constants, but they have to be variables for the * options parsing code. Assert here that they have known-good * values. */ assert (read_only == 1); assert (inspector == 0); assert (live == 0); /* Must be no extra arguments on the command line. */ if (optind != argc) usage (EXIT_FAILURE); CHECK_OPTION_format_consumed; /* virt-alignment-scan has two modes. If the user didn't specify * any drives, then we do the scan on every libvirt guest. That's * the if-clause below. If the user specified domains/drives, then * we assume they belong to a single guest. That's the else-clause * below. */ if (drvs == NULL) { #if defined(HAVE_LIBVIRT) get_all_libvirt_domains (libvirt_uri); r = start_threads (max_threads, g, scan_work); free_domains (); if (r == -1) exit (EXIT_FAILURE); #else fprintf (stderr, _("%s: compiled without support for libvirt.\n"), program_name); exit (EXIT_FAILURE); #endif } else { /* Single guest. */ if (uuid) { fprintf (stderr, _("%s: --uuid option cannot be used with -a or -d\n"), program_name); exit (EXIT_FAILURE); } /* Add domains/drives from the command line (for a single guest). */ add_drives (drvs, 'a'); if (guestfs_launch (g) == -1) exit (EXIT_FAILURE); /* Free up data structures, no longer needed after this point. */ free_drives (drvs); /* Perform the scan. */ r = scan (g, NULL, stdout); guestfs_close (g); if (r == -1) exit (EXIT_FAILURE); } /* Decide on an appropriate exit code. */ if (worst_alignment < 10) /* 2^10 = 4096 */ exit_code = 3; else if (worst_alignment < 16) /* 2^16 = 65536 */ exit_code = 2; else exit_code = 0; exit (exit_code); }