root/src/noit_capabilities_listener.c

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

Running NTP checks against each noit is reasonable, but it would be good to be able to do all of this directly over port 43191.

Let's add a display of the time on the noit in the capabilities command. This can be used by an API client as a poor-man's time check. It will not be nearly as accurate as an NTP check, but we're looking for wild skew here anyway and this should do the trick.

closes #330

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