[Spice-devel] To launch spicec from a c program

Mariano Grau Calín mgrau at grupojoly.com
Tue Jun 14 15:14:56 PDT 2011


Hi,
 
I'm writing a little client in c that given hypervisor host and domain name launch spice client with port used by vm. Also, if domain is shutdown startup it.
The syntax would be as:
 
# myclient server1 dom-xp
 
When launch command as 'spicec -h server1 -p 5902' from c program spice says:
 
 cannot resolve host address  server1
 
However, if i launch from console directly it works.
 
The code simplified is:
 
int main(int argc, char *argv[])
{
 char *params[4];
 params[0] = "spicec";
 params[1] = "-h cadi17";
 params[2] = "-p 5900";
 params[3] = NULL;
 fprintf(stdout, "%s %s %s\n", params[0], params[1], params[2]);
 execvp(params[0], params);
  
 return 1;
}
 
And more detailed:
...
/* Startup domain if shutdown state */
int domainStartup(virConnectPtr conn, char * domain)
{
 int ret = -1;
 char phost[50];
 char pport[10];
 
 // Check domain name
 virDomainPtr dom = virDomainLookupByName(conn, domain);
 if (dom == NULL) {
  fprintf(stderr, "Domain '%s' not found.\n", domain);
  return -1;
 }
 
 // Get domain info
 virDomainInfo info;
 if (virDomainGetInfo(dom, &info) != 0) {
  fprintf(stderr, "Domain '%s' not info.\n", domain); 
  goto out;
 }
 
 // Startup domain is shutdown
 fprintf(stdout, "Domain '%s' is %s.\n", domain, getState(info.state));
 if (info.state == VIR_DOMAIN_SHUTOFF) {
  if (virDomainCreate(dom) != 0) {
   fprintf(stderr, "Domain '%s' not boot.\n", domain);
   goto out;
  }
  else {
   fprintf(stdout, "Domain '%s' starting.\n", domain);
  }
 }
 
 // parse xml looking for listen port
 // http://xmlsoft.org/html/index.html
 char *xml = virDomainGetXMLDesc(dom, 1);
 xmlDocPtr doc = xmlReadMemory(xml, strlen(xml), "noname.xml", NULL, 0);
 if (doc != NULL) {
  xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
  xmlChar *xpathExpr = (xmlChar *)"/domain/devices/graphics/@port";
  xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);
  char *host = virConnectGetHostname(conn);
  char *port = nodeAttrContent(xpathObj);
  fprintf(stdout, "Domain '%s' listen on %s:%s.\n", domain, host, port);
  strcpy(phost, "-h ");
  strcat(phost, host);
  strcpy(pport, "-p ");
  strcat(pport, port);
  xmlXPathFreeContext(xpathCtx); 
  ret = 0;   
 }
 // free objects
 xmlFreeDoc(doc);
 free(xml);
out:
 virDomainFree(dom);
 
 // launch client
 if (ret == 0) {
  char *params[4];
  params[0] = "spicec";
  params[1] = phost;
  params[2] = pport;
  params[3] = NULL;
  fprintf(stdout, "%s %s %s\n", params[0], params[1], params[2]);
  execvp(params[0], params);
 }
 return ret;
}
 
int main(int argc, char *argv[])
{
    virConnectPtr conn;
 AuthData authData;
 char *uri, *domain;
 
 if (argc != 5) {
  printf("Usage: %s <uri> <username> <password> <domain>\n", argv[0]);
  return -1;
 }
 uri = argv[1];
 authData.username = argv[2];
 authData.password = argv[3];
    auth.cbdata = &authData;
 domain = argv[4];
 
    conn = virConnectOpenAuth(uri, &auth, 0);
    if (conn == NULL) {
        fprintf(stderr, "Failed to open connection to %s\n", uri);
        return -1;
    }
 else {
  domainStartup(conn, domain);
 }
    virConnectClose(conn);
    return 0;
}
 
 
Regards,
 
Mariano Grau
mgrau @ grupojoly.com
Dpto. Sistemas
Grupo Joly
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/spice-devel/attachments/20110615/84caa371/attachment.html>


More information about the Spice-devel mailing list