1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 * Stefan de Konink <stefan@konink.de>
9 * Copyright (C) 2001-2008 Alvaro Lopez Ortega
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 // #define LINUX_CMDLINE "root=/dev/xvda ro ip=%s:1.2.3.4:%s:%s::eth0:off"
28 #define VIRT_INTERFACE_XML \
29 " <interface type='bridge'>" \
30 " <source bridge='xenbr0'/>" \
31 " <mac address='%s'/>" \
32 " <ip address='%s' />" \
33 " <script path='vif-bridge'/>" \
36 //" <target dev='vif1.0'/>" \
38 #define VIRT_DISK_XML \
39 " <disk type='file' device='disk'>" \
40 " <driver name='tap' type='qcow' />" \
41 " <source file='%s'/>" \
42 " <target dev='%s' bus='xen'/>" \
47 #define VIRT_DOMAIN_XML \
48 "<domain type='xen'>" \
49 " <name>%s_%s</name>" \
51 " <type>linux</type>" \
52 " <kernel>/usr/lib/xen/boot/linux-2.6.20-xen-r6</kernel>" \
53 " <cmdline> root=/dev/xvda ro</cmdline>" \
55 " <memory>%d</memory>" \
57 " <on_poweroff>destroy</on_poweroff>" \
58 " <on_reboot>restart</on_reboot>" \
59 " <on_crash>destroy</on_crash>" \
65 #define VIRT_STORAGE_XML \
66 "<volume type='%s'>" \
68 " <allocation>%lu</allocation>" \
69 " <capacity unit='%s'>%lu</capacity>" \
71 " <path>%s/%s/images/%s</path>" \
73 " <owner>0744</owner>" \
74 " <group>0744</group>" \
75 " <mode>0744</mode>" \
76 " <label>%s_%s</label>" \
82 #include "handler_virt.h"
83 #include "handler_avahi.h"
84 #include <cherokee/cherokee.h>
86 /* Plug-in initialization
88 * In this function you can use any of these:
89 * http_delete | http_get | http_post | http_put
91 * For a full list: cherokee_http_method_t
93 * It is what your handler to be implements.
96 PLUGIN_INFO_HANDLER_EASIEST_INIT (virt
, http_get
| http_post
);
99 /* Methods implementation
102 props_free (cherokee_handler_virt_props_t
*props
)
105 cherokee_buffer_mrproper(&props
->xsl
);
106 cherokee_buffer_mrproper(&props
->virt
);
107 return cherokee_module_props_free_base (MODULE_PROPS(props
));
112 cherokee_handler_virt_configure (cherokee_config_node_t
*conf
, cherokee_server_t
*srv
, cherokee_module_props_t
**_props
)
115 cherokee_handler_virt_props_t
*props
;
117 /* Instance a new property object
120 if (*_props
== NULL
) {
121 CHEROKEE_NEW_STRUCT (n
, handler_virt_props
);
123 cherokee_handler_avahi_props_init_base (PROP_AVAHI(n
), MODULE_PROPS_FREE(props_free
));
125 /* Look at handler_virt.h
126 * This is an virt of configuration.
128 n
->authenticate
= true; /* by default we are secure! */
129 n
->read_only
= true; /* by default we are secure! */
130 cherokee_buffer_init (&n
->xsl
); /* we support a custom header */
131 cherokee_buffer_init (&n
->virt
); /* your first xenserver */
133 *_props
= MODULE_PROPS(n
);
136 props
= PROP_VIRT(*_props
);
138 cherokee_config_node_foreach (i
, conf
) {
139 cherokee_config_node_t
*subconf
= CONFIG_NODE(i
);
141 if (equal_buf_str (&subconf
->key
, "authenticate")) {
142 props
->authenticate
= atoi(subconf
->val
.buf
);
144 else if (equal_buf_str (&subconf
->key
, "read_only")) {
145 props
->read_only
= atoi(subconf
->val
.buf
);
147 else if (equal_buf_str (&subconf
->key
, "xsl")) {
148 cherokee_buffer_add_buffer (&props
->xsl
, &subconf
->val
);
150 else if (equal_buf_str (&subconf
->key
, "virt")) {
151 cherokee_buffer_add_buffer (&props
->virt
, &subconf
->val
);
159 return cherokee_handler_avahi_configure (conf
, srv
, _props
);
164 cherokee_handler_virt_init (cherokee_handler_virt_t
*hdl
)
170 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
171 cherokee_buffer_init(&hdl
->user
);
172 cherokee_buffer_init(&hdl
->vm
);
174 hdl
->action
= nothing
;
176 cherokee_buffer_add (&conn
->pathinfo
,
177 conn
->request
.buf
+ conn
->web_directory
.len
,
178 conn
->request
.len
- conn
->web_directory
.len
);
180 this = conn
->pathinfo
.buf
+ 1;
181 next
= strchr(this, '/');
184 if ((!next
&& (this && (len
= strlen(this)) == 0)) || (next
&& ((len
= next
- this) == 0)) )
185 hdl
->action
= showall
;
187 cherokee_buffer_add (&hdl
->user
, this, len
);
190 if (HDL_VIRT_PROPS(hdl
)->authenticate
) {
191 if (!conn
->validator
||
193 (!cherokee_buffer_cmp_buf(&conn
->validator
->user
, &hdl
->user
) &&
194 !(isroot
= cherokee_buffer_cmp (&conn
->validator
->user
, "root", 4))))) {
195 hdl
->action
= nothing
; /* just in case */
196 conn
->error_code
= http_unauthorized
;
203 if (hdl
->action
== showall
) {
205 hdl
->action
= nothing
;
206 conn
->error_code
= http_unauthorized
;
209 return virt_build_page(hdl
);
215 hdl
->action
= showuservms
;
216 return virt_build_page(hdl
);
219 next
= strchr(this, '/');
221 if (!next
&& (this && (len
= strlen(this)) == 0) || (next
&& ((len
= next
- this) == 0)) ) {
222 hdl
->action
= showuservms
;
223 return virt_build_page(hdl
);
227 cherokee_buffer_add (&hdl
->vm
, this, len
);
230 hdl
->action
= domainGetXMLDesc
;
231 return virt_build_page(hdl
);
234 next
= strchr(this, '/');
236 if (!next
&& (this && (len
= strlen(this)) == 0) || (next
&& ((len
= next
- this) == 0)) ) {
237 hdl
->action
= domainGetXMLDesc
;
238 return virt_build_page(hdl
);
242 hdl
->action
= not_implemented
;
243 switch (conn
->header
.method
) {
245 if (strncmp(this, "virDomain", 9) == 0) {
246 if (strncmp(this+9, "Get", 3) == 0) {
247 if (strcmp(this+12, "ID") == 0) hdl
->action
= domainGetID
;
248 else if (strcmp(this+12, "Name") == 0) hdl
->action
= domainGetName
;
249 else if (strcmp(this+12, "MaxMemory") == 0) hdl
->action
= domainGetMaxMemory
;
250 else if (strcmp(this+12, "MaxVcpus") == 0) hdl
->action
= domainGetMaxVcpus
;
251 else if (strcmp(this+12, "OSType") == 0) hdl
->action
= domainGetOSType
;
252 else if (strcmp(this+12, "UUID") == 0) hdl
->action
= domainGetUUID
;
253 else if (strcmp(this+12, "UUIDString") == 0) hdl
->action
= domainGetUUIDString
;
254 else if (strcmp(this+12, "XMLDesc") == 0) hdl
->action
= domainGetXMLDesc
;
256 else if (strcmp(this+9, "Create") == 0) hdl
->action
= domainCreate
;
257 else if (strcmp(this+9, "Destroy") == 0) hdl
->action
= domainDestroy
;
258 else if (strcmp(this+9, "Reboot") == 0) hdl
->action
= domainReboot
;
259 else if (strcmp(this+9, "Shutdown") == 0) hdl
->action
= domainShutdown
;
261 else if (strcmp(this+9, "AttachDevice") == 0) hdl
->action
= domainAttachDevice_args
;
263 else if (strcmp(this+9, "DefineXML") == 0) hdl
->action
= domainDefineXML_args
;
264 else if (strcmp(this+9, "Undefine") == 0) hdl
->action
= domainUndefine
;
266 else if (strncmp(this, "virStorage", 10) == 0) {
267 if (strncmp(this+10, "Vol", 3) == 0) {
268 if (strcmp(this+13, "CreateXML") == 0) hdl
->action
= storageVolCreateXML_args
;
275 cherokee_post_get_len (&conn
->post
, &postl
);
277 if (postl
<= 0 || postl
>= (INT_MAX
-1)) {
278 conn
->error_code
= http_bad_request
;
282 if (strncmp(this, "virDomain", 9) == 0) {
283 if (strcmp(this+9, "AttachDevice") == 0) hdl
->action
= domainAttachDevice
;
284 else if (strcmp(this+9, "DetachDevice") == 0) hdl
->action
= domainDetachDevice
;
285 else if (strcmp(this+9, "DefineXML") == 0) hdl
->action
= domainDefineXML
;
287 else if (strncmp(this, "virStorage", 10) == 0) {
288 if (strncmp(this+10, "Vol", 3) == 0) {
289 if (strcmp(this+13, "CreateXML") == 0) hdl
->action
= storageVolCreateXML
;
300 if (hdl
->action
<= 0) {
301 conn
->error_code
= http_bad_request
;
306 return virt_build_page(hdl
);
310 cherokee_handler_virt_free (cherokee_handler_virt_t
*hdl
)
312 cherokee_buffer_mrproper (&hdl
->buffer
);
313 cherokee_buffer_mrproper (&hdl
->user
);
314 cherokee_buffer_mrproper (&hdl
->vm
);
320 cherokee_handler_virt_step (cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*buffer
)
322 cherokee_buffer_add_buffer (buffer
, &hdl
->buffer
);
323 return ret_eof_have_data
;
327 cherokee_handler_virt_add_headers (cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*buffer
)
329 cherokee_buffer_add_va (buffer
, "Content-Length: %d"CRLF
, hdl
->buffer
.len
);
331 if (hdl
->action
> xml
)
332 cherokee_buffer_add_str (buffer
, "Content-Type: application/xml"CRLF
);
334 cherokee_buffer_add_str (buffer
, "Content-Type: text/plain"CRLF
);
341 while_func_entries (cherokee_buffer_t *key, void *value, void *param) {
342 virConnectPtr conn = NULL;
343 cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
344 cherokee_buffer_t *buf = (cherokee_buffer_t *)param;
346 cherokee_buffer_add_va (&uri, "xen://%s/", value);
348 if (HDL_VIRT_PROPS(hdl)->read_only == FALSE && !(conn = virConnectOpen (uri.buf))) {
349 if (!(conn = virConnectOpen (uri.buf))) {
354 if (!conn && !(conn = virConnectOpenReadOnly (uri.buf))) {
358 if (!(dom = virDomainLookupByName(conn, (const char *) key->buf))) {
361 char *xml = virDomainGetXMLDesc(dom, 0);
362 cherokee_buffer_add(buf, xml, strlen(xml));
364 virConnectClose(conn);
367 cherokee_buffer_mrproper(&uri);
373 virt_virt_function(cherokee_handler_virt_t
*hdl
, virDomainPtr dom
) {
374 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
375 cherokee_buffer_t
*buf
= &HDL_AVAHI(hdl
)->buffer
;
377 switch (hdl
->action
) {
379 case domainUndefine
: {
381 if ((result
= virDomainUndefine(dom
)) != 0) {
382 conn
->error_code
= http_internal_error
;
385 cherokee_buffer_add_long10(buf
, result
);
389 case domainAttachDevice_args
: {
393 ret
= cherokee_connection_parse_args (conn
);
394 if (unlikely(ret
< ret_ok
))
397 ret
= cherokee_avl_get_ptr (conn
->arguments
, "type", &temp
);
400 cherokee_buffer_t xml
= CHEROKEE_BUF_INIT
;
402 if (strcmp(temp
, "disk") == 0) {
405 if ((ret
= cherokee_avl_get_ptr (conn
->arguments
, "file", &file
)) == ret_ok
&& (ret
= cherokee_avl_get_ptr (conn
->arguments
, "device", &device
)) == ret_ok
) {
406 cherokee_buffer_add_va (&xml
, VIRT_DISK_XML
, file
, device
);
412 else if (strcmp(temp
, "interface") == 0) {
414 if ((ret
= cherokee_avl_get_ptr (conn
->arguments
, "mac", &mac
)) == ret_ok
&& (ret
= cherokee_avl_get_ptr (conn
->arguments
, "ip", &ip
)) == ret_ok
) {
415 cherokee_buffer_add_va (&xml
, VIRT_INTERFACE_XML
, mac
, ip
);
419 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
420 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
421 getNewMac("dv28", domu
.buf
, &mac
, &ip
);
422 cherokee_buffer_mrproper(&domu
);
423 cherokee_buffer_add_va (&xml
, VIRT_INTERFACE_XML
, mac
, ip
);
429 if (ret
== ret_ok
&& (result
= virDomainAttachDevice(dom
, (const char *) xml
.buf
)) == 0) {
432 conn
->error_code
= http_internal_error
;
436 cherokee_buffer_add_long10(buf
, result
);
437 cherokee_buffer_mrproper(&xml
);
443 case domainAttachDevice
: {
446 cherokee_buffer_t post
= CHEROKEE_BUF_INIT
;
447 cherokee_post_get_len (&conn
->post
, &postl
);
448 cherokee_post_walk_read (&conn
->post
, &post
, (cuint_t
) postl
);
449 if ((result
= virDomainAttachDevice(dom
, (const char *) post
.buf
)) != 0)
450 conn
->error_code
= http_internal_error
;
452 cherokee_buffer_mrproper(&post
);
453 cherokee_buffer_add_long10(buf
, result
);
458 case domainGetXMLDesc
: {
459 char *xml
= virDomainGetXMLDesc(dom
, 0);
460 cherokee_buffer_add(buf
, xml
, strlen(xml
));
466 case domainDetachDevice
: {
469 cherokee_buffer_t post
= CHEROKEE_BUF_INIT
;
470 cherokee_post_get_len (&conn
->post
, &postl
);
471 cherokee_post_walk_read (&conn
->post
, &post
, (cuint_t
) postl
);
472 if ((result
= virDomainDetachDevice(dom
, (const char *) post
.buf
)) != 0) {
473 conn
->error_code
= http_internal_error
;
475 cherokee_buffer_mrproper(&post
);
476 cherokee_buffer_add_long10(buf
, result
);
481 cherokee_buffer_add_ulong10(buf
, virDomainGetID(dom
));
484 case domainGetMaxMemory
: {
485 cherokee_buffer_add_ulong10(buf
, virDomainGetMaxMemory (dom
));
488 case domainGetMaxVcpus
: {
489 cherokee_buffer_add_long10(buf
, virDomainGetMaxVcpus (dom
));
492 case domainGetName
: {
493 const char *name
= virDomainGetName (dom
);
494 cherokee_buffer_add(buf
, name
, strlen(name
));
497 case domainGetUUID
: {
498 unsigned char uuid
[VIR_UUID_BUFLEN
];
499 if (virDomainGetUUID(dom
, uuid
) == 0) {
500 cherokee_buffer_add_str(buf
, uuid
);
502 conn
->error_code
= http_internal_error
;
507 case domainGetUUIDString
: {
508 unsigned char uuid
[VIR_UUID_STRING_BUFLEN
];
509 if (virDomainGetUUIDString(dom
, uuid
) == 0) {
510 cherokee_buffer_add_str(buf
, uuid
);
512 conn
->error_code
= http_internal_error
;
519 cherokee_buffer_add_long10(buf
, virDomainCreate (dom
));
523 case domainDestroy
: {
524 cherokee_buffer_add_long10(buf
, virDomainDestroy (dom
));
529 cherokee_buffer_add_long10(buf
, virDomainReboot (dom
, 0));
533 case domainShutdown
: {
534 cherokee_buffer_add_long10(buf
, virDomainShutdown (dom
));
539 case domainGetOSType
: {
540 char *ostype
= virDomainGetOSType(dom
);
541 cherokee_buffer_add(buf
, ostype
, strlen(ostype
));
552 virt_virt_new(cherokee_handler_virt_t
*hdl
, virConnectPtr virConn
)
554 cherokee_buffer_t
*buf
= &HDL_AVAHI(hdl
)->buffer
;
555 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
556 cherokee_buffer_t xml
= CHEROKEE_BUF_INIT
;
558 virStoragePoolPtr pool
;
560 switch (hdl
->action
) {
561 case storageVolCreateXML_args
: {
562 void *temp
, *type
, *name
, *unit
;
564 unsigned long int capacity
, allocation
;
566 ret
= cherokee_connection_parse_args (conn
);
567 if (unlikely(ret
< ret_ok
))
570 ret
= cherokee_avl_get_ptr (conn
->arguments
, "pool", &temp
);
571 if (unlikely(ret
< ret_ok
))
574 pool
= virStoragePoolLookupByName(virConn
, temp
);
579 pool_path
= "/mnt/netapp/users";
581 pool_path = virStoragePoolGetPath(pool);
584 virStoragePoolFree(pool);
590 ret
= cherokee_avl_get_ptr (conn
->arguments
, "name", &name
);
591 if (unlikely(ret
< ret_ok
))
594 ret
= cherokee_avl_get_ptr (conn
->arguments
, "type", &type
);
595 if (unlikely(ret
< ret_ok
))
598 ret
= cherokee_avl_get_ptr (conn
->arguments
, "unit", &unit
);
599 if (unlikely(ret
< ret_ok
))
602 ret
= cherokee_avl_get_ptr (conn
->arguments
, "allocation", &temp
);
603 if (unlikely(ret
< ret_ok
))
606 allocation
= strtoul(temp
, NULL
, 10);
607 /* TODO: errno check */
609 ret
= cherokee_avl_get_ptr (conn
->arguments
, "capacity", &temp
);
610 if (unlikely(ret
< ret_ok
))
613 capacity
= strtoul(temp
, NULL
, 10);
614 /* TODO: errno check */
616 cherokee_buffer_add_va (&xml
, VIRT_STORAGE_XML
, type
, name
, allocation
, unit
, capacity
, pool_path
, hdl
->user
.buf
, name
, hdl
->user
.buf
, name
);
621 case domainDefineXML_args
: {
624 unsigned long vcpu
= 0, interface
= 0, memory
= 0;
626 cherokee_buffer_t xml_interfaces
= CHEROKEE_BUF_INIT
;
629 ret
= cherokee_connection_parse_args (conn
);
630 if (unlikely(ret
< ret_ok
))
633 ret
= cherokee_avl_get_ptr (conn
->arguments
, "vcpu", &temp
);
635 vcpu
= strtoul(temp
, (char **) NULL
, 10);
637 if (ret
!= ret_ok
|| errno
== ERANGE
|| vcpu
== 0) {
638 conn
->error_code
= http_internal_error
;
643 ret
= cherokee_avl_get_ptr (conn
->arguments
, "memory", &temp
);
645 memory
= strtoul(temp
, (char **) NULL
, 10);
647 if (ret
!= ret_ok
|| errno
== ERANGE
|| memory
== 0) {
648 conn
->error_code
= http_internal_error
;
653 ret
= cherokee_avl_get_ptr (conn
->arguments
, "interface", &temp
);
655 interface
= strtoul(temp
, (char **) NULL
, 10);
657 if (ret
!= ret_ok
|| errno
== ERANGE
) {
658 conn
->error_code
= http_internal_error
;
662 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
663 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
665 for (i
= 0; i
< interface
; i
++) {
668 getNewMac("dv28", domu
.buf
, &mac
, &ip
);
669 cherokee_buffer_add_va (&xml_interfaces
, VIRT_INTERFACE_XML
, mac
, ip
);
672 cherokee_buffer_mrproper(&domu
);
674 cherokee_buffer_add_va (&xml
, VIRT_DOMAIN_XML
,
675 hdl
->user
.buf
, hdl
->vm
.buf
, memory
, vcpu
, xml_interfaces
.buf
);
677 cherokee_buffer_mrproper(&xml_interfaces
);
681 case storageVolCreateXML
:
682 case domainDefineXML
: {
684 cherokee_post_get_len (&conn
->post
, &postl
);
685 cherokee_post_walk_read (&conn
->post
, &xml
, (cuint_t
) postl
);
690 switch (hdl
->action
) {
691 case domainDefineXML_args
:
692 case domainDefineXML
: {
693 result
= virDomainDefineXML(virConn
, (const char *) xml
.buf
);
694 // printf("%s\n", xml.buf);
695 cherokee_buffer_mrproper(&xml
);
697 if (result
== NULL
) {
698 /* TODO: vrij maken eventuele uitgegeven macs! */
699 conn
->error_code
= http_internal_error
;
703 char *output
= virDomainGetXMLDesc(result
, 0);
704 /* TODO: store the domain on DISK! */
705 FILE *fd
= fopen("/mnt/netapp/users/%s/%s.xml", "w");
706 fwrite(output
, strlen(output
), sizeof(char), fd
);
708 cherokee_buffer_add(buf
, output
, strlen(output
));
713 case storageVolCreateXML_args
:
714 case storageVolCreateXML
: {
715 printf("%s", &xml
.buf
);
717 virStorageVolPtr vol
= virStorageVolCreateXML(pool
, (const char *) &xml
.buf
, 0);
718 virStoragePoolFree(pool
);
719 cherokee_buffer_mrproper(&xml
);
724 cherokee_buffer_add_long10(buf
, 0);
734 virt_virt_do(cherokee_handler_virt_t
*hdl
, cherokee_buffer_t
*domu
, cherokee_buffer_t
*uri
)
736 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
738 ret_t ret
= ret_error
;
739 virConnectPtr virConn
= NULL
;
742 if (HDL_VIRT_PROPS(hdl
)->read_only
== FALSE
)
743 virConn
= virConnectOpen (uri
->buf
);
745 if (!virConn
&& !(virConn
= virConnectOpenReadOnly (uri
->buf
))) {
746 conn
->error_code
= http_service_unavailable
;
750 switch (hdl
->action
) {
751 case domainDefineXML_args
:
752 case domainDefineXML
: {
753 ret
= virt_virt_new(hdl
, virConn
);
759 if ((dom
= virDomainLookupByName(virConn
, domu
->buf
)) == NULL
) {
760 conn
->error_code
= http_not_found
;
762 ret
= virt_virt_function(hdl
, dom
);
768 virConnectClose(virConn
);
773 virt_while (cherokee_buffer_t
*key
, void *value
, void *param
) {
774 cherokee_handler_virt_t
* hdl
= param
;
775 // cherokee_buffer_t uri = CHEROKEE_BUF_INIT;
776 // cherokee_buffer_add_va (&uri, "xen://%s/", ((cherokee_buffer_t *) value)->buf);
777 // virt_virt_do((cherokee_handler_virt_t *) param, key, &uri);
778 // cherokee_buffer_mrproper(&uri);
780 cherokee_buffer_add_va (&hdl
->buffer
, "<domain><name>%s</name></domain>", key
->buf
);
786 virt_while_user (cherokee_buffer_t
*key
, void *value
, void *param
) {
787 cherokee_handler_virt_t
*hdl
= param
;
788 if (key
->len
> hdl
->user
.len
&& key
->buf
[hdl
->user
.len
] == '_' && strncmp(key
->buf
, hdl
->user
.buf
, hdl
->user
.len
) == 0)
789 return virt_while (key
, value
, param
);
795 virt_build_page (cherokee_handler_virt_t
*hdl
)
798 cherokee_connection_t
*conn
= HANDLER_CONN(hdl
);
799 cherokee_buffer_t uri
= CHEROKEE_BUF_INIT
;
800 cherokee_buffer_t domu
= CHEROKEE_BUF_INIT
;
802 if (hdl
->action
> xml
&& HDL_VIRT_PROPS(hdl
)->xsl
.len
> 0)
803 cherokee_buffer_add_va (&hdl
->buffer
, "<?xml version=\"1.0\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"%s\"?>\n", HDL_VIRT_PROPS(hdl
)->xsl
.buf
);
806 /* First, block the event loop */
807 avahi_threaded_poll_lock(HDL_AVAHI_PROPS(hdl
)->threaded_poll
);
808 switch (hdl
->action
) {
810 cherokee_buffer_add_str (&hdl
->buffer
, "<domains>");
811 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl
)->entries
, virt_while
, hdl
, NULL
, NULL
);
812 cherokee_buffer_add_str (&hdl
->buffer
, "</domains>");
818 cherokee_buffer_add_str (&hdl
->buffer
, "<domains>");
819 cherokee_avl_while(&HDL_AVAHI_PROPS(hdl
)->entries
, virt_while_user
, hdl
, NULL
, NULL
);
820 cherokee_buffer_add_str (&hdl
->buffer
, "</domains>");
826 cherokee_buffer_t
*hostname
= NULL
;
827 cherokee_buffer_add_va (&domu
, "%s_%s", hdl
->user
.buf
, hdl
->vm
.buf
);
828 ret
= cherokee_avl_get_ptr(&HDL_AVAHI_PROPS(hdl
)->entries
, domu
.buf
, (void **) &hostname
);
830 if (ret
== ret_not_found
) {
832 virConnectPtr virConn
;
833 cherokee_buffer_add_va (&uri
, "xen://%s/", HDL_VIRT_PROPS(hdl
)->virt
); // TODO: change!
835 if (HDL_VIRT_PROPS(hdl
)->read_only
== FALSE
)
836 virConn
= virConnectOpen (uri
.buf
);
838 if (!virConn
&& !(virConn
= virConnectOpenReadOnly (uri
.buf
))) {
839 conn
->error_code
= http_service_unavailable
;
843 if ((virDom
= virDomainLookupByName(virConn
, domu
.buf
)) == NULL
) {
844 if (hdl
->action
!= domainDefineXML_args
&& hdl
->action
!= domainDefineXML
) {
845 hdl
->action
= nothing
;
846 conn
->error_code
= http_not_found
;
852 virDomainFree(virDom
);
855 virConnectClose (virConn
);
856 } else if (ret
== ret_ok
) {
857 cherokee_buffer_add_va (&uri
, "xen://%s/", hostname
->buf
);
858 printf("%s\n", uri
.buf
);
860 hdl
->action
= http_internal_error
;
861 hdl
->action
= nothing
;
867 /* Finally, unblock the event loop */
868 avahi_threaded_poll_unlock(HDL_AVAHI_PROPS(hdl
)->threaded_poll
);
870 if (ret
== ret_ok
&& hdl
->action
!= showall
&& hdl
->action
!= showuservms
) {
871 ret
= virt_virt_do(hdl
, &domu
, &uri
);
874 cherokee_buffer_mrproper(&domu
);
875 cherokee_buffer_mrproper(&uri
);
883 cherokee_handler_virt_new (cherokee_handler_t
**hdl
, cherokee_connection_t
*cnt
, cherokee_module_props_t
*props
)
886 CHEROKEE_NEW_STRUCT (n
, handler_virt
);
888 /* Init the base class
891 cherokee_handler_init_base(HANDLER(n
), cnt
, HANDLER_PROPS(props
), PLUGIN_INFO_HANDLER_PTR(virt
));
893 MODULE(n
)->init
= (handler_func_init_t
) cherokee_handler_virt_init
;
894 MODULE(n
)->free
= (module_func_free_t
) cherokee_handler_virt_free
;
895 HANDLER(n
)->step
= (handler_func_step_t
) cherokee_handler_virt_step
;
896 HANDLER(n
)->add_headers
= (handler_func_add_headers_t
) cherokee_handler_virt_add_headers
;
898 HANDLER(n
)->support
= hsupport_length
| hsupport_range
;
900 ret
= cherokee_buffer_init (&n
->buffer
);
901 if (unlikely(ret
!= ret_ok
))
904 ret
= cherokee_buffer_ensure_size (&n
->buffer
, 4*1024);
905 if (unlikely(ret
!= ret_ok
))