Fixes to get it compiled
[handlervirt.git] / handler_virt.c
blob66e3dd091442a3ce0349bc68b482081363cd1de6
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /* Cherokee
5 * Authors:
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
23 * USA
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'/>" \
34 " </interface>"
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'/>" \
43 " </disk>"
47 #define VIRT_DOMAIN_XML \
48 "<domain type='xen'>" \
49 " <name>%s_%s</name>" \
50 " <os>" \
51 " <type>linux</type>" \
52 " <kernel>/usr/lib/xen/boot/linux-2.6.20-xen-r6</kernel>" \
53 " <cmdline> root=/dev/xvda ro</cmdline>" \
54 " </os>" \
55 " <memory>%d</memory>" \
56 " <vcpu>%d</vcpu>" \
57 " <on_poweroff>destroy</on_poweroff>" \
58 " <on_reboot>restart</on_reboot>" \
59 " <on_crash>destroy</on_crash>" \
60 " <devices>" \
61 " %s" \
62 " </devices>" \
63 "</domain>"
65 #define VIRT_STORAGE_XML \
66 "<volume type='%s'>" \
67 " <name>%s</name>" \
68 " <allocation>%lu</allocation>" \
69 " <capacity unit='%s'>%lu</capacity>" \
70 " <target>" \
71 " <path>%s/%s/images/%s</path>" \
72 " <permissions>" \
73 " <owner>0744</owner>" \
74 " <group>0744</group>" \
75 " <mode>0744</mode>" \
76 " <label>%s_%s</label>" \
77 " </permissions>" \
78 " </target>" \
79 "</volume>"
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
101 static ret_t
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));
111 ret_t
112 cherokee_handler_virt_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
114 cherokee_list_t *i;
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);
156 /* Init base class
159 return cherokee_handler_avahi_configure (conf, srv, _props);
163 ret_t
164 cherokee_handler_virt_init (cherokee_handler_virt_t *hdl)
166 int isroot = false;
167 int len;
168 char *this, *next;
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;
186 else {
187 cherokee_buffer_add (&hdl->user, this, len);
190 if (HDL_VIRT_PROPS(hdl)->authenticate) {
191 if (!conn->validator ||
192 (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;
197 return ret_error;
199 } else {
200 isroot = true;
203 if (hdl->action == showall) {
204 if (!isroot) {
205 hdl->action = nothing;
206 conn->error_code = http_unauthorized;
207 return ret_error;
208 } else {
209 return virt_build_page(hdl);
214 if (!next) {
215 hdl->action = showuservms;
216 return virt_build_page(hdl);
217 } else {
218 this = next + 1;
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);
229 if (!next) {
230 hdl->action = domainGetXMLDesc;
231 return virt_build_page(hdl);
232 } else {
233 this = next + 1;
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) {
244 case http_get:
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;
271 break;
273 case http_post: {
274 off_t postl;
275 cherokee_post_get_len (&conn->post, &postl);
277 if (postl <= 0 || postl >= (INT_MAX-1)) {
278 conn->error_code = http_bad_request;
279 return ret_error;
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;
293 break;
296 default:
297 return ret_error;
300 if (hdl->action <= 0) {
301 conn->error_code = http_bad_request;
302 return ret_error;
306 return virt_build_page(hdl);
309 ret_t
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);
316 return ret_ok;
319 ret_t
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;
326 ret_t
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);
333 else
334 cherokee_buffer_add_str (buffer, "Content-Type: text/plain"CRLF);
336 return ret_ok;
340 static ret_t
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))) {
350 return ret_error;
354 if (!conn && !(conn = virConnectOpenReadOnly (uri.buf))) {
355 return ret_error;
356 } else {
357 virDomainPtr dom;
358 if (!(dom = virDomainLookupByName(conn, (const char *) key->buf))) {
359 return ret_error;
360 } else {
361 char *xml = virDomainGetXMLDesc(dom, 0);
362 cherokee_buffer_add(buf, xml, strlen(xml));
364 virConnectClose(conn);
367 cherokee_buffer_mrproper(&uri);
369 return ret_ok;
372 static ret_t
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: {
380 int result;
381 if ((result = virDomainUndefine(dom)) != 0) {
382 conn->error_code = http_internal_error;
385 cherokee_buffer_add_long10(buf, result);
386 break;
389 case domainAttachDevice_args: {
390 int result;
391 void *temp;
392 ret_t ret;
393 ret = cherokee_connection_parse_args (conn);
394 if (unlikely(ret < ret_ok))
395 return ret;
397 ret = cherokee_avl_get_ptr (conn->arguments, "type", &temp);
399 if (ret == ret_ok) {
400 cherokee_buffer_t xml = CHEROKEE_BUF_INIT;
402 if (strcmp(temp, "disk") == 0) {
403 void *file, *device;
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);
407 } else {
408 ret = ret_error;
412 else if (strcmp(temp, "interface") == 0) {
413 void *mac, *ip;
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);
416 } else {
417 char *mac = NULL;
418 char *ip = NULL;
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) {
430 ret = ret_ok;
431 } else {
432 conn->error_code = http_internal_error;
433 ret = ret_error;
436 cherokee_buffer_add_long10(buf, result);
437 cherokee_buffer_mrproper(&xml);
440 return ret;
443 case domainAttachDevice: {
444 off_t postl;
445 int result;
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);
454 break;
458 case domainGetXMLDesc: {
459 char *xml = virDomainGetXMLDesc(dom, 0);
460 cherokee_buffer_add(buf, xml, strlen(xml));
461 free(xml);
462 break;
466 case domainDetachDevice: {
467 off_t postl;
468 int result;
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);
477 break;
480 case domainGetID: {
481 cherokee_buffer_add_ulong10(buf, virDomainGetID(dom));
482 break;
484 case domainGetMaxMemory: {
485 cherokee_buffer_add_ulong10(buf, virDomainGetMaxMemory (dom));
486 break;
488 case domainGetMaxVcpus: {
489 cherokee_buffer_add_long10(buf, virDomainGetMaxVcpus (dom));
490 break;
492 case domainGetName: {
493 const char *name = virDomainGetName (dom);
494 cherokee_buffer_add(buf, name, strlen(name));
495 break;
497 case domainGetUUID: {
498 unsigned char uuid[VIR_UUID_BUFLEN];
499 if (virDomainGetUUID(dom, uuid) == 0) {
500 cherokee_buffer_add_str(buf, uuid);
501 } else {
502 conn->error_code = http_internal_error;
503 return ret_error;
505 break;
507 case domainGetUUIDString: {
508 unsigned char uuid[VIR_UUID_STRING_BUFLEN];
509 if (virDomainGetUUIDString(dom, uuid) == 0) {
510 cherokee_buffer_add_str(buf, uuid);
511 } else {
512 conn->error_code = http_internal_error;
513 return ret_error;
515 break;
518 case domainCreate: {
519 cherokee_buffer_add_long10(buf, virDomainCreate (dom));
520 break;
523 case domainDestroy: {
524 cherokee_buffer_add_long10(buf, virDomainDestroy (dom));
525 break;
528 case domainReboot: {
529 cherokee_buffer_add_long10(buf, virDomainReboot (dom, 0));
530 break;
533 case domainShutdown: {
534 cherokee_buffer_add_long10(buf, virDomainShutdown (dom));
535 break;
539 case domainGetOSType: {
540 char *ostype = virDomainGetOSType(dom);
541 cherokee_buffer_add(buf, ostype, strlen(ostype));
542 free(ostype);
543 break;
548 return ret_ok;
551 static ret_t
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;
557 virDomainPtr result;
558 virStoragePoolPtr pool;
560 switch (hdl->action) {
561 case storageVolCreateXML_args: {
562 void *temp, *type, *name, *unit;
563 char *pool_path;
564 unsigned long int capacity, allocation;
565 ret_t ret;
566 ret = cherokee_connection_parse_args (conn);
567 if (unlikely(ret < ret_ok))
568 return ret;
570 ret = cherokee_avl_get_ptr (conn->arguments, "pool", &temp);
571 if (unlikely(ret < ret_ok))
572 return ret;
574 pool = virStoragePoolLookupByName(virConn, temp);
576 if (pool == NULL)
577 return ret_error;
579 pool_path = "/mnt/netapp/users";
581 pool_path = virStoragePoolGetPath(pool);
583 if (!pool_path) {
584 virStoragePoolFree(pool);
585 return ret_error;
590 ret = cherokee_avl_get_ptr (conn->arguments, "name", &name);
591 if (unlikely(ret < ret_ok))
592 return ret;
594 ret = cherokee_avl_get_ptr (conn->arguments, "type", &type);
595 if (unlikely(ret < ret_ok))
596 return ret;
598 ret = cherokee_avl_get_ptr (conn->arguments, "unit", &unit);
599 if (unlikely(ret < ret_ok))
600 return ret;
602 ret = cherokee_avl_get_ptr (conn->arguments, "allocation", &temp);
603 if (unlikely(ret < ret_ok))
604 return ret;
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))
611 return ret;
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);
617 // free(pool_path);
621 case domainDefineXML_args: {
622 ret_t ret;
623 unsigned int i;
624 unsigned long vcpu = 0, interface = 0, memory = 0;
626 cherokee_buffer_t xml_interfaces = CHEROKEE_BUF_INIT;
627 void *temp = NULL;
629 ret = cherokee_connection_parse_args (conn);
630 if (unlikely(ret < ret_ok))
631 return ret;
633 ret = cherokee_avl_get_ptr (conn->arguments, "vcpu", &temp);
634 if (ret == ret_ok)
635 vcpu = strtoul(temp, (char **) NULL, 10);
637 if (ret != ret_ok || errno == ERANGE || vcpu == 0) {
638 conn->error_code = http_internal_error;
639 return ret_error;
643 ret = cherokee_avl_get_ptr (conn->arguments, "memory", &temp);
644 if (ret == ret_ok)
645 memory = strtoul(temp, (char **) NULL, 10);
647 if (ret != ret_ok || errno == ERANGE || memory == 0) {
648 conn->error_code = http_internal_error;
649 return ret_error;
653 ret = cherokee_avl_get_ptr (conn->arguments, "interface", &temp);
654 if (ret == ret_ok)
655 interface = strtoul(temp, (char **) NULL, 10);
657 if (ret != ret_ok || errno == ERANGE) {
658 conn->error_code = http_internal_error;
659 return ret_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++) {
666 char *mac = NULL;
667 char *ip = NULL;
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);
678 break;
681 case storageVolCreateXML:
682 case domainDefineXML: {
683 off_t postl;
684 cherokee_post_get_len (&conn->post, &postl);
685 cherokee_post_walk_read (&conn->post, &xml, (cuint_t) postl);
686 break;
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;
700 return ret_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);
707 fclose(fd);
708 cherokee_buffer_add(buf, output, strlen(output));
709 free(output);
710 break;
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);
721 if (vol == NULL)
722 return ret_error;
724 cherokee_buffer_add_long10(buf, 0);
725 break;
729 return ret_ok;
733 static ret_t
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;
747 return ret_error;
750 switch (hdl->action) {
751 case domainDefineXML_args:
752 case domainDefineXML: {
753 ret = virt_virt_new(hdl, virConn);
754 break;
757 default: {
758 virDomainPtr dom;
759 if ((dom = virDomainLookupByName(virConn, domu->buf)) == NULL) {
760 conn->error_code = http_not_found;
761 } else {
762 ret = virt_virt_function(hdl, dom);
763 virDomainFree(dom);
768 virConnectClose(virConn);
769 return ret;
772 static ret_t
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);
782 return ret_ok;
785 static ret_t
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);
791 return ret_ok;
794 static ret_t
795 virt_build_page (cherokee_handler_virt_t *hdl)
797 ret_t ret;
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) {
809 case showall:
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>");
813 ret = ret_ok;
814 break;
816 case showuservms: {
817 void *param[3];
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>");
821 ret = ret_ok;
822 break;
825 default: {
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) {
831 virDomainPtr virDom;
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;
840 return ret_error;
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;
847 ret = ret_error;
848 } else {
849 ret = ret_ok;
851 } else {
852 virDomainFree(virDom);
853 ret = ret_ok;
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);
859 } else {
860 hdl->action = http_internal_error;
861 hdl->action = nothing;
862 ret = ret_error;
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);
878 return ret;
882 ret_t
883 cherokee_handler_virt_new (cherokee_handler_t **hdl, cherokee_connection_t *cnt, cherokee_module_props_t *props)
885 ret_t ret;
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))
902 return ret;
904 ret = cherokee_buffer_ensure_size (&n->buffer, 4*1024);
905 if (unlikely(ret != ret_ok))
906 return ret;
908 *hdl = HANDLER(n);
910 return ret_ok;