<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.17092" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011>Hi,</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>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.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>The syntax would be
as:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011># myclient server1
dom-xp</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>When
launch command as '</SPAN></FONT><FONT face=Arial size=2><SPAN
class=366334021-14062011>spicec -h server1 -p 5902' from c program spice
says:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> <EM>cannot
resolve host address server1</EM></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>However, if i
launch from console directly it works.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>The code simplified
is:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>int main(int argc,
char *argv[])<BR>{<BR> char *params[4];<BR> params[0] =
"spicec";<BR> params[1] = "-h cadi17";<BR> params[2] = "-p
5900";<BR> params[3] = NULL;<BR> fprintf(stdout, "%s %s %s\n",
params[0], params[1], params[2]);<BR> execvp(params[0],
params);<BR> <BR> return 1;<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>And more
detailed:</SPAN></FONT><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011>...</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>/* Startup domain if
shutdown state */<BR>int domainStartup(virConnectPtr conn, char *
domain)<BR>{<BR> int ret = -1;<BR> char phost[50];<BR> char
pport[10];</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> // Check
domain name<BR> virDomainPtr dom = virDomainLookupByName(conn,
domain);<BR> if (dom == NULL) {<BR> fprintf(stderr, "Domain '%s'
not found.\n", domain);<BR> return -1;<BR> }</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> // Get domain
info<BR> virDomainInfo info;<BR> if (virDomainGetInfo(dom, &info)
!= 0) {<BR> fprintf(stderr, "Domain '%s' not info.\n",
domain); <BR> goto out;<BR> }</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> // Startup
domain is shutdown<BR> fprintf(stdout, "Domain '%s' is %s.\n", domain,
getState(info.state));<BR> if (info.state == VIR_DOMAIN_SHUTOFF)
{<BR> if (virDomainCreate(dom) != 0)
{<BR> fprintf(stderr, "Domain '%s' not boot.\n",
domain);<BR> goto out;<BR> }<BR> else
{<BR> fprintf(stdout, "Domain '%s' starting.\n",
domain);<BR> }<BR> }</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> // parse xml
looking for listen port<BR> // <A
href="http://xmlsoft.org/html/index.html">http://xmlsoft.org/html/index.html</A><BR> char
*xml = virDomainGetXMLDesc(dom, 1);<BR> xmlDocPtr doc = xmlReadMemory(xml,
strlen(xml), "noname.xml", NULL, 0);<BR> if (doc != NULL)
{<BR> xmlXPathContextPtr xpathCtx =
xmlXPathNewContext(doc);<BR> xmlChar *xpathExpr = (xmlChar
*)"<A>/domain/devices/graphics/@port</A>";<BR> xmlXPathObjectPtr
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);<BR> char
*host = virConnectGetHostname(conn);<BR> char *port =
nodeAttrContent(xpathObj);<BR> fprintf(stdout, "Domain '%s' listen on
%s:%s.\n", domain, host, port);<BR> strcpy(phost, "-h
");<BR> strcat(phost, host);<BR> strcpy(pport, "-p
");<BR> strcat(pport,
port);<BR> xmlXPathFreeContext(xpathCtx); <BR> ret =
0; <BR> }<BR> // free
objects<BR> xmlFreeDoc(doc);<BR> free(xml);<BR>out:<BR> virDomainFree(dom);</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> // launch
client<BR> if (ret == 0) {<BR> char
*params[4];<BR> params[0] = "spicec";<BR> params[1] =
phost;<BR> params[2] = pport;<BR> params[3] =
NULL;<BR> fprintf(stdout, "%s %s %s\n", params[0], params[1],
params[2]);<BR> execvp(params[0], params);<BR> }<BR> return
ret;<BR>}</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>int main(int argc,
char *argv[])<BR>{<BR> virConnectPtr conn;<BR> AuthData
authData;<BR> char *uri, *domain;</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011> if (argc != 5)
{<BR> printf("Usage: %s <uri> <username> <password>
<domain>\n", argv[0]);<BR> return -1;<BR> }<BR> uri =
argv[1];<BR> authData.username = argv[2];<BR> authData.password =
argv[3];<BR> auth.cbdata = &authData;<BR> domain =
argv[4];</SPAN></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>
conn = virConnectOpenAuth(uri, &auth, 0);<BR> if (conn ==
NULL) {<BR> fprintf(stderr, "Failed to
open connection to %s\n", uri);<BR>
return -1;<BR> }<BR> else
{<BR> domainStartup(conn, domain);<BR> }<BR>
virConnectClose(conn);<BR> return 0;<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=366334021-14062011>Regards</SPAN></FONT><SPAN
class=807550408-10082010><FONT face=Arial size=2>,</FONT></SPAN></DIV>
<DIV align=left>
<DIV><SPAN class=807550408-10082010><FONT face=Arial
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=807550408-10082010><FONT face=Arial size=2>Mariano
Grau</FONT></SPAN></DIV>
<DIV><SPAN class=807550408-10082010><FONT face=Arial size=2>mgrau @
grupojoly.com</FONT></SPAN></DIV>
<DIV><SPAN class=807550408-10082010><FONT face=Arial size=2>Dpto.
Sistemas</FONT></SPAN></DIV>
<DIV><SPAN class=807550408-10082010><FONT face=Arial><FONT size=2>Grupo
Joly</FONT></SPAN></DIV></FONT></DIV>
<DIV> </DIV></BODY></HTML>