root/src/noit_capabilities_listener.c

Revision 88a71780101cbf23034aa0cb840f9f0368fda2dd, 6.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

fixes #126

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "eventer/eventer.h"
35 #include "noit_listener.h"
36 #include "utils/noit_hash.h"
37 #include "utils/noit_log.h"
38 #include "utils/noit_sem.h"
39 #include "noit_capabilities_listener.h"
40 #include "noit_module.h"
41 #include "noit_check.h"
42
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #include <errno.h>
46
47 #include <libxml/xmlsave.h>
48 #include <libxml/tree.h>
49
50 typedef struct noit_capsvc_closure {
51   char *buff;
52   size_t written;
53   size_t towrite;
54 } noit_capsvc_closure_t;
55
56 void
57 noit_capabilities_listener_init() {
58   eventer_name_callback("capabilities_transit/1.0", noit_capabilities_handler);
59   noit_control_dispatch_delegate(noit_control_dispatch,
60                                  NOIT_CAPABILITIES_SERVICE,
61                                  noit_capabilities_handler);
62 }
63
64 int
65 noit_capabilities_handler(eventer_t e, int mask, void *closure,
66                           struct timeval *now) {
67   int newmask = EVENTER_WRITE | EVENTER_EXCEPTION;
68   acceptor_closure_t *ac = closure;
69   noit_capsvc_closure_t *cl = ac->service_ctx;
70
71   if(mask & EVENTER_EXCEPTION) {
72 socket_error:
73     /* Exceptions cause us to simply snip the connection */
74 cleanup_shutdown:
75     eventer_remove_fd(e->fd);
76     e->opset->close(e->fd, &newmask, e);
77     if(cl) {
78       if(cl->buff) free(cl->buff);
79       free(cl);
80     }
81     if(ac) acceptor_closure_free(ac);
82     return 0;
83   }
84
85   if(!ac->service_ctx) {
86     char vbuff[128];
87     noit_hash_table *lc;
88     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
89     const char *k;
90     int klen;
91     void *data;
92
93     xmlDocPtr xmldoc;
94     xmlNodePtr root, cmds;
95     xmlBufferPtr xmlbuffer;
96     xmlSaveCtxtPtr savectx;
97
98     cl = ac->service_ctx = calloc(1, sizeof(*cl));
99     /* fill out capabilities */
100     noit_build_version(vbuff, sizeof(vbuff));
101
102     /* Create an XML Document */
103     xmldoc = xmlNewDoc((xmlChar *)"1.0");
104     root = xmlNewDocNode(xmldoc, NULL, (xmlChar *)"noitd_capabilities", NULL);
105     xmlDocSetRootElement(xmldoc, root);
106
107     /* Fill in the document */
108     xmlNewTextChild(root, NULL, (xmlChar *)"version", (xmlChar *)vbuff);
109
110     cmds = xmlNewNode(NULL, (xmlChar *)"services");
111     xmlAddChild(root, cmds);
112     lc = noit_listener_commands();
113     while(noit_hash_next(lc, &iter, &k, &klen, &data)) {
114       xmlNodePtr cnode;
115       char hexcode[10];
116       const char *name;
117       eventer_func_t *f = (eventer_func_t *)k;
118       noit_hash_table *sc = (noit_hash_table *)data;
119       noit_hash_iter sc_iter = NOIT_HASH_ITER_ZERO;
120       const char *sc_k;
121       int sc_klen;
122       void *sc_data;
123
124       name = eventer_name_for_callback(*f);
125       cnode = xmlNewNode(NULL, (xmlChar *)"service");
126       xmlSetProp(cnode, (xmlChar *)"name", name ? (xmlChar *)name : NULL);
127       if(*f == ac->dispatch)
128         xmlSetProp(cnode, (xmlChar *)"connected", (xmlChar *)"true");
129       xmlAddChild(cmds, cnode);
130       while(noit_hash_next(sc, &sc_iter, &sc_k, &sc_klen, &sc_data)) {
131         xmlNodePtr scnode;
132         char *name_copy, *version = NULL;
133         eventer_func_t *f = (eventer_func_t *)sc_data;
134
135         snprintf(hexcode, sizeof(hexcode), "0x%08x", *((u_int32_t *)sc_k));
136         name = eventer_name_for_callback(*f);
137         name_copy = strdup(name ? name : "[[unknown]]");
138         version = strchr(name_copy, '/');
139         if(version) *version++ = '\0';
140
141         scnode = xmlNewNode(NULL, (xmlChar *)"command");
142         xmlSetProp(scnode, (xmlChar *)"name", (xmlChar *)name_copy);
143         if(version)
144           xmlSetProp(scnode, (xmlChar *)"version", (xmlChar *)version);
145         xmlSetProp(scnode, (xmlChar *)"code", (xmlChar *)hexcode);
146         xmlAddChild(cnode, scnode);
147         free(name_copy);
148       }
149     }
150
151     /* Write it out to a buffer and copy it for writing */
152     xmlbuffer = xmlBufferCreate();
153     savectx = xmlSaveToBuffer(xmlbuffer, "utf8", 1);
154     xmlSaveDoc(savectx, xmldoc);
155     xmlSaveClose(savectx);
156     cl->buff = strdup((const char *)xmlBufferContent(xmlbuffer));
157     cl->towrite = xmlBufferLength(xmlbuffer);
158
159     /* Clean up after ourselves */
160     xmlBufferFree(xmlbuffer);
161     xmlFreeDoc(xmldoc);
162   }
163
164   while(cl->towrite > cl->written) {
165     int len;
166     while((len = e->opset->write(e->fd, cl->buff + cl->written,
167                                  cl->towrite - cl->written,
168                                  &newmask, e)) == -1 && errno == EINTR);
169     if(len < 0) {
170       if(errno == EAGAIN) return newmask | EVENTER_EXCEPTION;
171       goto socket_error;
172     }
173     cl->written += len;
174   }
175   goto cleanup_shutdown;
176
177   return 0;
178 }
Note: See TracBrowser for help on using the browser.