<!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>&nbsp;</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>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>When 
launch&nbsp;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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;<EM>cannot 
resolve host address&nbsp; server1</EM></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=366334021-14062011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>However,&nbsp;if i 
launch from console directly it works.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=366334021-14062011></SPAN></FONT>&nbsp;</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>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>int main(int argc, 
char *argv[])<BR>{<BR>&nbsp;char *params[4];<BR>&nbsp;params[0] = 
"spicec";<BR>&nbsp;params[1] = "-h cadi17";<BR>&nbsp;params[2] = "-p 
5900";<BR>&nbsp;params[3] = NULL;<BR>&nbsp;fprintf(stdout, "%s %s %s\n", 
params[0], params[1], params[2]);<BR>&nbsp;execvp(params[0], 
params);<BR>&nbsp;&nbsp;<BR>&nbsp;return 1;<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=366334021-14062011></SPAN></FONT>&nbsp;</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>&nbsp;int ret = -1;<BR>&nbsp;char phost[50];<BR>&nbsp;char 
pport[10];</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;// Check 
domain name<BR>&nbsp;virDomainPtr dom = virDomainLookupByName(conn, 
domain);<BR>&nbsp;if (dom == NULL) {<BR>&nbsp;&nbsp;fprintf(stderr, "Domain '%s' 
not found.\n", domain);<BR>&nbsp;&nbsp;return -1;<BR>&nbsp;}</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;// Get domain 
info<BR>&nbsp;virDomainInfo info;<BR>&nbsp;if (virDomainGetInfo(dom, &amp;info) 
!= 0) {<BR>&nbsp;&nbsp;fprintf(stderr, "Domain '%s' not info.\n", 
domain);&nbsp;<BR>&nbsp;&nbsp;goto out;<BR>&nbsp;}</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;// Startup 
domain is shutdown<BR>&nbsp;fprintf(stdout, "Domain '%s' is %s.\n", domain, 
getState(info.state));<BR>&nbsp;if (info.state == VIR_DOMAIN_SHUTOFF) 
{<BR>&nbsp;&nbsp;if (virDomainCreate(dom) != 0) 
{<BR>&nbsp;&nbsp;&nbsp;fprintf(stderr, "Domain '%s' not boot.\n", 
domain);<BR>&nbsp;&nbsp;&nbsp;goto out;<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;else 
{<BR>&nbsp;&nbsp;&nbsp;fprintf(stdout, "Domain '%s' starting.\n", 
domain);<BR>&nbsp;&nbsp;}<BR>&nbsp;}</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;// parse xml 
looking for listen port<BR>&nbsp;// <A 
href="http://xmlsoft.org/html/index.html">http://xmlsoft.org/html/index.html</A><BR>&nbsp;char 
*xml = virDomainGetXMLDesc(dom, 1);<BR>&nbsp;xmlDocPtr doc = xmlReadMemory(xml, 
strlen(xml), "noname.xml", NULL, 0);<BR>&nbsp;if (doc != NULL) 
{<BR>&nbsp;&nbsp;xmlXPathContextPtr xpathCtx = 
xmlXPathNewContext(doc);<BR>&nbsp;&nbsp;xmlChar *xpathExpr = (xmlChar 
*)"<A>/domain/devices/graphics/@port</A>";<BR>&nbsp;&nbsp;xmlXPathObjectPtr 
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);<BR>&nbsp;&nbsp;char 
*host = virConnectGetHostname(conn);<BR>&nbsp;&nbsp;char *port = 
nodeAttrContent(xpathObj);<BR>&nbsp;&nbsp;fprintf(stdout, "Domain '%s' listen on 
%s:%s.\n", domain, host, port);<BR>&nbsp;&nbsp;strcpy(phost, "-h 
");<BR>&nbsp;&nbsp;strcat(phost, host);<BR>&nbsp;&nbsp;strcpy(pport, "-p 
");<BR>&nbsp;&nbsp;strcat(pport, 
port);<BR>&nbsp;&nbsp;xmlXPathFreeContext(xpathCtx);&nbsp;<BR>&nbsp;&nbsp;ret = 
0;&nbsp;&nbsp;&nbsp;<BR>&nbsp;}<BR>&nbsp;// free 
objects<BR>&nbsp;xmlFreeDoc(doc);<BR>&nbsp;free(xml);<BR>out:<BR>&nbsp;virDomainFree(dom);</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;// launch 
client<BR>&nbsp;if (ret == 0) {<BR>&nbsp;&nbsp;char 
*params[4];<BR>&nbsp;&nbsp;params[0] = "spicec";<BR>&nbsp;&nbsp;params[1] = 
phost;<BR>&nbsp;&nbsp;params[2] = pport;<BR>&nbsp;&nbsp;params[3] = 
NULL;<BR>&nbsp;&nbsp;fprintf(stdout, "%s %s %s\n", params[0], params[1], 
params[2]);<BR>&nbsp;&nbsp;execvp(params[0], params);<BR>&nbsp;}<BR>&nbsp;return 
ret;<BR>}</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>int main(int argc, 
char *argv[])<BR>{<BR>&nbsp;&nbsp;&nbsp; virConnectPtr conn;<BR>&nbsp;AuthData 
authData;<BR>&nbsp;char *uri, *domain;</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;if (argc != 5) 
{<BR>&nbsp;&nbsp;printf("Usage: %s &lt;uri&gt; &lt;username&gt; &lt;password&gt; 
&lt;domain&gt;\n", argv[0]);<BR>&nbsp;&nbsp;return -1;<BR>&nbsp;}<BR>&nbsp;uri = 
argv[1];<BR>&nbsp;authData.username = argv[2];<BR>&nbsp;authData.password = 
argv[3];<BR>&nbsp;&nbsp;&nbsp; auth.cbdata = &amp;authData;<BR>&nbsp;domain = 
argv[4];</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN class=366334021-14062011>&nbsp;&nbsp;&nbsp; 
conn = virConnectOpenAuth(uri, &amp;auth, 0);<BR>&nbsp;&nbsp;&nbsp; if (conn == 
NULL) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Failed to 
open connection to %s\n", uri);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
return -1;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;else 
{<BR>&nbsp;&nbsp;domainStartup(conn, domain);<BR>&nbsp;}<BR>&nbsp;&nbsp;&nbsp; 
virConnectClose(conn);<BR>&nbsp;&nbsp;&nbsp; return 0;<BR>}</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=366334021-14062011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2><SPAN 
class=366334021-14062011></SPAN></FONT>&nbsp;</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>&nbsp;</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>&nbsp;</DIV></BODY></HTML>