+ assert (conn != NULL);
+
+ virDomainPtr *doms = malloc (sizeof (virDomainPtr) * (n+1));
+ if (doms == NULL) {
+ perror ("malloc");
+ exit (1);
+ }
+
+ /* Discard 'domain not found' errors from libvirt while we
+ * run this loop.
+ */
+ virConnSetErrorFunc (conn, NULL, discard_virterror);
+
+ int i;
+ for (i = 0; i < n; ++i) {
+ /* Try in the order: UUID, name, ID. */
+ doms[i] = virDomainLookupByUUIDString (conn, domains[i]);
+ if (!doms[i]) {
+ doms[i] = virDomainLookupByName (conn, domains[i]);
+ if (!doms[i] && domains[i][0] >= '0' && domains[i][0] <= '9') {
+ errno = 0;
+ char *endptr;
+ int id = strtol (domains[i], &endptr, 10);
+ if ((errno == ERANGE && (id == LONG_MAX || id == LONG_MIN)) ||
+ (errno != 0 && id == 0)) {
+ perror ("strtol");
+ exit (EXIT_FAILURE);
+ }
+ if (endptr == domains[i])
+ error (_("no digits found while trying to parse ID '%s'"),
+ domains[i]);
+ if (*endptr)
+ error (_("parse error, extra digits after ID '%s'"),
+ domains[i]);
+ doms[i] = virDomainLookupByID (conn, id);
+ }
+ }
+
+ if (!doms[i])
+ error (_("%s: libvirt guest not found (use name, UUID or ID here)"),
+ domains[i]);
+
+ if (!virDomainIsActive (doms[i]))
+ error (_("%s: this domain is inactive. This program only works on running guests."),
+ virDomainGetName (doms[i]));
+ }
+
+ /* Restore default virterror behaviour. */
+ virConnSetErrorFunc (conn, NULL, NULL);
+
+ return doms;