int get_dom_info(xen_interface_t *xen, const char *input, domid_t *domID, char **name) { uint32_t _domID = ~0; char *_name = NULL; sscanf(input, "%u", &_domID); if (_domID == ~0) { _name = strdup(input); libxl_name_to_domid(xen->xl_ctx, input, &_domID); if (!_domID || _domID == ~0) { printf("Domain is not running, failed to get domID from name!\n"); free(_name); return -1; } else { //printf("Got domID from name: %u\n", _domID); } } else { xc_dominfo_t info = { 0 }; if ( 1 == xc_domain_getinfo(xen->xc, _domID, 1, &info) && info.domid == _domID) { _name = libxl_domid_to_name(xen->xl_ctx, _domID); } else { _domID = ~0; } } *name = _name; *domID = (domid_t)_domID; return 1; }
int get_dom_info(xen_interface_t *xen, const char *input, uint32_t *domID, char **name) { uint32_t _domID = INVALID_DOMID; char *_name = NULL; sscanf(input, "%u", &_domID); if (_domID == INVALID_DOMID) { _name = strdup(input); libxl_name_to_domid(xen->xl_ctx, input, &_domID); if (!_domID || _domID == INVALID_DOMID) { printf("Domain is not running, failed to get domID from name!\n"); free(_name); return -1; } else { //printf("Got domID from name: %u\n", _domID); } } else { //printf("Converting domid %u to name\n", _domID); _name = libxl_domid_to_name(xen->xl_ctx, _domID); } *name = _name; *domID = _domID; return 1; }
int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name, uint32_t *domid) { int i, rv; for (i=0; name[i]; i++) { if (!CTYPE(isdigit, name[i])) { goto nondigit_found; } } *domid = strtoul(name, NULL, 10); return 0; nondigit_found: /* this could also check for uuids */ rv = libxl_name_to_domid(ctx, name, domid); return rv; }
int libxl__domain_rename(libxl__gc *gc, uint32_t domid, const char *old_name, const char *new_name, xs_transaction_t trans) { libxl_ctx *ctx = libxl__gc_owner(gc); char *dom_path = 0; const char *name_path; char *got_old_name; unsigned int got_old_len; xs_transaction_t our_trans = 0; uint32_t stub_dm_domid; const char *stub_dm_old_name = NULL, *stub_dm_new_name = NULL; int rc; libxl_dominfo info; char *uuid; const char *vm_name_path; libxl_dominfo_init(&info); dom_path = libxl__xs_get_dompath(gc, domid); if (!dom_path) goto x_nomem; name_path= GCSPRINTF("%s/name", dom_path); if (!name_path) goto x_nomem; stub_dm_domid = libxl_get_stubdom_id(CTX, domid); if (stub_dm_domid) { stub_dm_old_name = libxl__stub_dm_name(gc, old_name); stub_dm_new_name = libxl__stub_dm_name(gc, new_name); } retry_transaction: if (!trans) { trans = our_trans = xs_transaction_start(ctx->xsh); if (!our_trans) { LOGEVD(ERROR, errno, domid, "Create xs transaction for domain (re)name"); goto x_fail; } } if (!new_name) { LOGD(ERROR, domid, "New domain name not specified"); rc = ERROR_INVAL; goto x_rc; } if (new_name[0]) { /* nonempty names must be unique */ uint32_t domid_e; rc = libxl_name_to_domid(ctx, new_name, &domid_e); if (rc == ERROR_INVAL) { /* no such domain, good */ } else if (rc != 0) { LOGD(ERROR, domid, "Unexpected error checking for existing domain"); goto x_rc; } else if (domid_e == domid) { /* domain already has this name, ok (but we do still * need the rest of the code as we may need to check * old_name, for example). */ } else { LOGD(ERROR, domid, "Domain with name \"%s\" already exists.", new_name); rc = ERROR_INVAL; goto x_rc; } } if (old_name) { got_old_name = xs_read(ctx->xsh, trans, name_path, &got_old_len); if (!got_old_name) { LOGEVD(ERROR, errno, domid, "Check old name for domain allegedly named `%s'", old_name); goto x_fail; } if (strcmp(old_name, got_old_name)) { LOGD(ERROR, domid, "Allegedly named `%s' is actually named `%s' - racing ?", old_name, got_old_name); free(got_old_name); goto x_fail; } free(got_old_name); } if (!xs_write(ctx->xsh, trans, name_path, new_name, strlen(new_name))) { LOGD(ERROR, domid, "Failed to write new name `%s'" " for domain previously named `%s'", new_name, old_name); goto x_fail; } /* update /vm/<uuid>/name */ rc = libxl_domain_info(ctx, &info, domid); if (rc) goto x_rc; uuid = GCSPRINTF(LIBXL_UUID_FMT, LIBXL_UUID_BYTES(info.uuid)); vm_name_path = GCSPRINTF("/vm/%s/name", uuid); if (libxl__xs_write_checked(gc, trans, vm_name_path, new_name)) goto x_fail; if (stub_dm_domid) { rc = libxl__domain_rename(gc, stub_dm_domid, stub_dm_old_name, stub_dm_new_name, trans); if (rc) { LOGED(ERROR, domid, "Unable to rename stub-domain"); goto x_rc; } } if (our_trans) { if (!xs_transaction_end(ctx->xsh, our_trans, 0)) { trans = our_trans = 0; if (errno != EAGAIN) { LOGD(ERROR, domid, "Failed to commit new name `%s'" " for domain previously named `%s'", new_name, old_name); goto x_fail; } LOGD(DEBUG, domid, "Need to retry rename transaction" " for domain (name_path=\"%s\", new_name=\"%s\")", name_path, new_name); goto retry_transaction; } our_trans = 0; } rc = 0; x_rc: if (our_trans) xs_transaction_end(ctx->xsh, our_trans, 1); libxl_dominfo_dispose(&info); return rc; x_fail: rc = ERROR_FAIL; goto x_rc; x_nomem: rc = ERROR_NOMEM; goto x_rc; }