root/src/noit_capabilities_listener.c

Revision 520106c37dca8dd9921431f0385fa4a74fecf4d5, 6.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

support versioning from git

  • 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 "noit_version.h"
35 #include "eventer/eventer.h"
36 #include "noit_listener.h"
37 #include "utils/noit_hash.h"
38 #include "utils/noit_log.h"
39 #include "utils/noit_sem.h"
40 #include "noit_capabilities_listener.h"
41 #include "noit_module.h"
42 #include "noit_check.h"
43 #include "noit_xml.h"
44
45 #include <unistd.h>
46 #include <sys/ioctl.h>
47 #include <errno.h>
48
49 #include <libxml/xmlsave.h>
50 #include <libxml/tree.h>
51
52 typedef struct noit_capsvc_closure {
53   char *buff;
54   size_t written;
55   size_t towrite;
56 } noit_capsvc_closure_t;
57
58 void
59 noit_capabilities_listener_init() {
60   eventer_name_callback("capabilities_transit/1.0", noit_capabilities_handler);
61   noit_control_dispatch_delegate(noit_control_dispatch,
62                                  NOIT_CAPABILITIES_SERVICE,
63                                  noit_capabilities_handler);
64 }
65
66 static void
67 noit_capabilities_tobuff(noit_capsvc_closure_t *cl, eventer_func_t curr) {
68     char vbuff[128];
69     noit_hash_table *lc;
70     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
71     const char *k;
72     int klen;
73     void *data;
74     struct timeval now;
75
76     xmlDocPtr xmldoc;
77     xmlNodePtr root, cmds;
78
79     /* fill out capabilities */
80
81     /* Create an XML Document */
82     xmldoc = xmlNewDoc((xmlChar *)"1.0");
83     root = xmlNewDocNode(xmldoc, NULL, (xmlChar *)"noit_capabilities", NULL);
84     xmlDocSetRootElement(xmldoc, root);
85
86     /* Fill in the document */
87     noit_build_version(vbuff, sizeof(vbuff));
88     xmlNewTextChild(root, NULL, (xmlChar *)"version", (xmlChar *)vbuff);
89
90     /* time (poor man's time check) */
91     gettimeofday(&now, NULL);
92     snprintf(vbuff, sizeof(vbuff), "%llu.%03d", (unsigned long long)now.tv_sec,
93              (int)(now.tv_usec / 1000));
94     xmlNewTextChild(root, NULL, (xmlChar *)"current_time", (xmlChar *)vbuff);
95
96     cmds = xmlNewNode(NULL, (xmlChar *)"services");
97     xmlAddChild(root, cmds);
98     lc = noit_listener_commands();
99     while(noit_hash_next(lc, &iter, &k, &klen, &data)) {
100       xmlNodePtr cnode;
101       char hexcode[11];
102       const char *name;
103       eventer_func_t *f = (eventer_func_t *)k;
104       noit_hash_table *sc = (noit_hash_table *)data;
105       noit_hash_iter sc_iter = NOIT_HASH_ITER_ZERO;
106       const char *sc_k;
107       int sc_klen;
108       void *sc_data;
109
110       name = eventer_name_for_callback(*f);
111       cnode = xmlNewNode(NULL, (xmlChar *)"service");
112       xmlSetProp(cnode, (xmlChar *)"name", name ? (xmlChar *)name : NULL);
113       if(*f == curr)
114         xmlSetProp(cnode, (xmlChar *)"connected", (xmlChar *)"true");
115       xmlAddChild(cmds, cnode);
116       while(noit_hash_next(sc, &sc_iter, &sc_k, &sc_klen, &sc_data)) {
117         xmlNodePtr scnode;
118         char *name_copy, *version = NULL;
119         eventer_func_t *f = (eventer_func_t *)sc_data;
120
121         snprintf(hexcode, sizeof(hexcode), "0x%08x", *((u_int32_t *)sc_k));
122         name = eventer_name_for_callback(*f);
123         name_copy = strdup(name ? name : "[[unknown]]");
124         version = strchr(name_copy, '/');
125         if(version) *version++ = '\0';
126
127         scnode = xmlNewNode(NULL, (xmlChar *)"command");
128         xmlSetProp(scnode, (xmlChar *)"name", (xmlChar *)name_copy);
129         if(version)
130           xmlSetProp(scnode, (xmlChar *)"version", (xmlChar *)version);
131         xmlSetProp(scnode, (xmlChar *)"code", (xmlChar *)hexcode);
132         xmlAddChild(cnode, scnode);
133         free(name_copy);
134       }
135     }
136
137     /* Write it out to a buffer and copy it for writing */
138     cl->buff = noit_xmlSaveToBuffer(xmldoc);
139     cl->towrite = strlen(cl->buff);
140
141     /* Clean up after ourselves */
142     xmlFreeDoc(xmldoc);
143 }
144
145 int
146 noit_capabilities_handler(eventer_t e, int mask, void *closure,
147                           struct timeval *now) {
148   int newmask = EVENTER_WRITE | EVENTER_EXCEPTION;
149   acceptor_closure_t *ac = closure;
150   noit_capsvc_closure_t *cl = ac->service_ctx;
151
152   if(mask & EVENTER_EXCEPTION) {
153 socket_error:
154     /* Exceptions cause us to simply snip the connection */
155 cleanup_shutdown:
156     eventer_remove_fd(e->fd);
157     e->opset->close(e->fd, &newmask, e);
158     if(cl) {
159       if(cl->buff) free(cl->buff);
160       free(cl);
161     }
162     if(ac) acceptor_closure_free(ac);
163     return 0;
164   }
165
166   if(!ac->service_ctx) {
167     cl = ac->service_ctx = calloc(1, sizeof(*cl));
168     noit_capabilities_tobuff(cl, ac->dispatch);
169   }
170
171   while(cl->towrite > cl->written) {
172     int len;
173     while((len = e->opset->write(e->fd, cl->buff + cl->written,
174                                  cl->towrite - cl->written,
175                                  &newmask, e)) == -1 && errno == EINTR);
176     if(len < 0) {
177       if(errno == EAGAIN) return newmask | EVENTER_EXCEPTION;
178       goto socket_error;
179     }
180     cl->written += len;
181   }
182   goto cleanup_shutdown;
183 }
184
185
Note: See TracBrowser for help on using the browser.