root/src/stratcon_jlog_streamer.c

Revision 4da7bc195ba74cd373f56a55be00fb4deb4cbaea, 39.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 4 years ago)

all logging cleanup

  • 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_conf.h"
36 #include "utils/noit_hash.h"
37 #include "utils/noit_log.h"
38 #include "noit_jlog_listener.h"
39 #include "noit_rest.h"
40 #include "stratcon_datastore.h"
41 #include "stratcon_jlog_streamer.h"
42 #include "stratcon_iep.h"
43
44 #include <unistd.h>
45 #include <assert.h>
46 #include <errno.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #ifdef HAVE_SYS_FILIO_H
50 #include <sys/filio.h>
51 #endif
52 #include <netinet/in.h>
53 #include <sys/un.h>
54 #include <arpa/inet.h>
55
56 pthread_mutex_t noits_lock;
57 noit_hash_table noits = NOIT_HASH_EMPTY;
58
59 static void noit_connection_initiate_connection(noit_connection_ctx_t *ctx);
60
61 static const char *feed_type_to_str(int jlog_feed_cmd) {
62   switch(jlog_feed_cmd) {
63     case NOIT_JLOG_DATA_FEED: return "durable/storage";
64     case NOIT_JLOG_DATA_TEMP_FEED: return "transient/iep";
65   }
66   return "unknown";
67 }
68
69 static int
70 remote_str_sort(const void *a, const void *b) {
71   int rv;
72   noit_connection_ctx_t * const *actx = a;
73   noit_connection_ctx_t * const *bctx = b;
74   jlog_streamer_ctx_t *ajctx = (*actx)->consumer_ctx;
75   jlog_streamer_ctx_t *bjctx = (*bctx)->consumer_ctx;
76   rv = strcmp((*actx)->remote_str, (*bctx)->remote_str);
77   if(rv) return rv;
78   return (ajctx->jlog_feed_cmd < bjctx->jlog_feed_cmd) ? -1 :
79            ((ajctx->jlog_feed_cmd == bjctx->jlog_feed_cmd) ? 0 : 1);
80 }
81 static void
82 nc_print_noit_conn_brief(noit_console_closure_t ncct,
83                           noit_connection_ctx_t *ctx) {
84   jlog_streamer_ctx_t *jctx = ctx->consumer_ctx;
85   struct timeval now, diff, session_duration;
86   const char *feedtype = "unknown";
87   const char *lasttime = "never";
88   if(ctx->last_connect.tv_sec != 0) {
89     char cmdbuf[4096];
90     time_t r = ctx->last_connect.tv_sec;
91     struct tm tbuf, *tm;
92     tm = gmtime_r(&r, &tbuf);
93     strftime(cmdbuf, sizeof(cmdbuf), "%Y-%m-%d %H:%M:%S UTC", tm);
94     lasttime = cmdbuf;
95   }
96   nc_printf(ncct, "%s [%s]:\n\tLast connect: %s\n", ctx->remote_str,
97             ctx->remote_cn ? "connected" :
98                              (ctx->timeout_event ? "disconnected" :
99                                                    "connecting"), lasttime);
100   if(ctx->e) {
101     char buff[128];
102     const char *addrstr = NULL;
103     struct sockaddr_in6 addr6;
104     socklen_t len = sizeof(addr6);
105     if(getsockname(ctx->e->fd, (struct sockaddr *)&addr6, &len) == 0) {
106       unsigned short port = 0;
107       if(addr6.sin6_family == AF_INET) {
108         addrstr = inet_ntop(addr6.sin6_family,
109                             &((struct sockaddr_in *)&addr6)->sin_addr,
110                             buff, sizeof(buff));
111         port = ntohs(((struct sockaddr_in *)&addr6)->sin_port);
112       }
113       else if(addr6.sin6_family == AF_INET6) {
114         addrstr = inet_ntop(addr6.sin6_family, &addr6.sin6_addr,
115                             buff, sizeof(buff));
116         port = ntohs(addr6.sin6_port);
117       }
118       if(addrstr != NULL)
119         nc_printf(ncct, "\tLocal address is %s:%u\n", buff, port);
120       else
121         nc_printf(ncct, "\tLocal address not interpretable\n");
122     }
123     else {
124       nc_printf(ncct, "\tLocal address error[%d]: %s\n",
125                 ctx->e->fd, strerror(errno));
126     }
127   }
128   feedtype = feed_type_to_str(ntohl(jctx->jlog_feed_cmd));
129   nc_printf(ncct, "\tJLog event streamer [%s]\n", feedtype);
130   gettimeofday(&now, NULL);
131   if(ctx->timeout_event) {
132     sub_timeval(ctx->timeout_event->whence, now, &diff);
133     nc_printf(ncct, "\tNext attempt in %lld.%06us\n",
134               (long long)diff.tv_sec, (unsigned int) diff.tv_usec);
135   }
136   else if(ctx->remote_cn) {
137     nc_printf(ncct, "\tRemote CN: '%s'\n",
138               ctx->remote_cn ? ctx->remote_cn : "???");
139     if(ctx->consumer_callback == stratcon_jlog_recv_handler) {
140       struct timeval last;
141       double session_duration_seconds;
142       const char *state = "unknown";
143
144       switch(jctx->state) {
145         case JLOG_STREAMER_WANT_INITIATE: state = "initiate"; break;
146         case JLOG_STREAMER_WANT_COUNT: state = "waiting for next batch"; break;
147         case JLOG_STREAMER_WANT_ERROR: state = "waiting for error"; break;
148         case JLOG_STREAMER_WANT_HEADER: state = "reading header"; break;
149         case JLOG_STREAMER_WANT_BODY: state = "reading body"; break;
150         case JLOG_STREAMER_IS_ASYNC: state = "asynchronously processing"; break;
151         case JLOG_STREAMER_WANT_CHKPT: state = "checkpointing"; break;
152       }
153       last.tv_sec = jctx->header.tv_sec;
154       last.tv_usec = jctx->header.tv_usec;
155       sub_timeval(now, last, &diff);
156       sub_timeval(now, ctx->last_connect, &session_duration);
157       session_duration_seconds = session_duration.tv_sec +
158                                  (double)session_duration.tv_usec/1000000.0;
159       nc_printf(ncct, "\tState: %s\n"
160                       "\tNext checkpoint: [%08x:%08x]\n"
161                       "\tLast event: %lld.%06us ago\n"
162                       "\tEvents this session: %llu (%0.2f/s)\n"
163                       "\tOctets this session: %llu (%0.2f/s)\n",
164                 state,
165                 jctx->header.chkpt.log, jctx->header.chkpt.marker,
166                 (long long)diff.tv_sec, (unsigned int)diff.tv_usec,
167                 jctx->total_events,
168                 (double)jctx->total_events/session_duration_seconds,
169                 jctx->total_bytes_read,
170                 (double)jctx->total_bytes_read/session_duration_seconds);
171     }
172     else {
173       nc_printf(ncct, "\tUnknown type.\n");
174     }
175   }
176 }
177
178 jlog_streamer_ctx_t *
179 stratcon_jlog_streamer_datastore_ctx_alloc(void) {
180   jlog_streamer_ctx_t *ctx;
181   ctx = stratcon_jlog_streamer_ctx_alloc();
182   ctx->jlog_feed_cmd = htonl(NOIT_JLOG_DATA_FEED);
183   ctx->push = stratcon_datastore_push;
184   return ctx;
185 }
186 jlog_streamer_ctx_t *
187 stratcon_jlog_streamer_ctx_alloc(void) {
188   jlog_streamer_ctx_t *ctx;
189   ctx = calloc(1, sizeof(*ctx));
190   return ctx;
191 }
192 noit_connection_ctx_t *
193 noit_connection_ctx_alloc(void) {
194   noit_connection_ctx_t *ctx, **pctx;
195   ctx = calloc(1, sizeof(*ctx));
196   ctx->refcnt = 1;
197   pctx = malloc(sizeof(*pctx));
198   *pctx = ctx;
199   pthread_mutex_lock(&noits_lock);
200   noit_hash_store(&noits, (const char *)pctx, sizeof(*pctx), ctx);
201   pthread_mutex_unlock(&noits_lock);
202   return ctx;
203 }
204 int
205 noit_connection_reinitiate(eventer_t e, int mask, void *closure,
206                          struct timeval *now) {
207   noit_connection_ctx_t *ctx = closure;
208   ctx->timeout_event = NULL;
209   noit_connection_initiate_connection(closure);
210   return 0;
211 }
212 void
213 noit_connection_schedule_reattempt(noit_connection_ctx_t *ctx,
214                                    struct timeval *now) {
215   struct timeval __now, interval;
216   const char *v;
217   u_int32_t min_interval = 1000, max_interval = 8000;
218   if(ctx->remote_cn) {
219     free(ctx->remote_cn);
220     ctx->remote_cn = NULL;
221   }
222   if(noit_hash_retr_str(ctx->config,
223                         "reconnect_initial_interval",
224                         strlen("reconnect_initial_interval"),
225                         &v)) {
226     min_interval = MAX(atoi(v), 100); /* .1 second minimum */
227   }
228   if(noit_hash_retr_str(ctx->config,
229                         "reconnect_maximum_interval",
230                         strlen("reconnect_maximum_interval"),
231                         &v)) {
232     max_interval = MIN(atoi(v), 3600*1000); /* 1 hour maximum */
233   }
234   if(ctx->current_backoff == 0) ctx->current_backoff = min_interval;
235   else {
236     ctx->current_backoff *= 2;
237     ctx->current_backoff = MAX(min_interval, ctx->current_backoff);
238     ctx->current_backoff = MIN(max_interval, ctx->current_backoff);
239   }
240   if(!now) {
241     gettimeofday(&__now, NULL);
242     now = &__now;
243   }
244   interval.tv_sec = ctx->current_backoff / 1000;
245   interval.tv_usec = (ctx->current_backoff % 1000) * 1000;
246   noitL(noit_debug, "Next jlog_streamer attempt in %ums\n",
247         ctx->current_backoff);
248   if(ctx->timeout_event)
249     eventer_remove(ctx->timeout_event);
250   else
251     ctx->timeout_event = eventer_alloc();
252   ctx->timeout_event->callback = noit_connection_reinitiate;
253   ctx->timeout_event->closure = ctx;
254   ctx->timeout_event->mask = EVENTER_TIMER;
255   add_timeval(*now, interval, &ctx->timeout_event->whence);
256   eventer_add(ctx->timeout_event);
257 }
258 static void
259 noit_connection_ctx_free(noit_connection_ctx_t *ctx) {
260   if(ctx->remote_cn) free(ctx->remote_cn);
261   if(ctx->remote_str) free(ctx->remote_str);
262   if(ctx->timeout_event) {
263     eventer_remove(ctx->timeout_event);
264     eventer_free(ctx->timeout_event);
265   }
266   ctx->consumer_free(ctx->consumer_ctx);
267   free(ctx);
268 }
269 void
270 noit_connection_ctx_deref(noit_connection_ctx_t *ctx) {
271   if(noit_atomic_dec32(&ctx->refcnt) == 0)
272     noit_connection_ctx_free(ctx);
273 }
274 void
275 noit_connection_ctx_dealloc(noit_connection_ctx_t *ctx) {
276   noit_connection_ctx_t **pctx = &ctx;
277   pthread_mutex_lock(&noits_lock);
278   noit_hash_delete(&noits, (const char *)pctx, sizeof(*pctx),
279                    free, (void (*)(void *))noit_connection_ctx_deref);
280   pthread_mutex_unlock(&noits_lock);
281 }
282 void
283 jlog_streamer_ctx_free(void *cl) {
284   jlog_streamer_ctx_t *ctx = cl;
285   if(ctx->buffer) free(ctx->buffer);
286   free(ctx);
287 }
288
289 #define Eread(a,b) e->opset->read(e->fd, (a), (b), &mask, e)
290 static int
291 __read_on_ctx(eventer_t e, jlog_streamer_ctx_t *ctx, int *newmask) {
292   int len, mask;
293   while(ctx->bytes_read < ctx->bytes_expected) {
294     len = Eread(ctx->buffer + ctx->bytes_read,
295                 ctx->bytes_expected - ctx->bytes_read);
296     if(len < 0) {
297       *newmask = mask;
298       return -1;
299     }
300     /* if we get 0 inside SSL, and there was a real error, we
301      * will actually get a -1 here.
302      * if(len == 0) return ctx->bytes_read;
303      */
304     ctx->total_bytes_read += len;
305     ctx->bytes_read += len;
306   }
307   assert(ctx->bytes_read == ctx->bytes_expected);
308   return ctx->bytes_read;
309 }
310 #define FULLREAD(e,ctx,size) do { \
311   int mask, len; \
312   if(!ctx->bytes_expected) { \
313     ctx->bytes_expected = size; \
314     if(ctx->buffer) free(ctx->buffer); \
315     ctx->buffer = malloc(size + 1); \
316     if(ctx->buffer == NULL) { \
317       noitL(noit_error, "malloc(%lu) failed.\n", (long unsigned int)size + 1); \
318       goto socket_error; \
319     } \
320     ctx->buffer[size] = '\0'; \
321   } \
322   len = __read_on_ctx(e, ctx, &mask); \
323   if(len < 0) { \
324     if(errno == EAGAIN) return mask | EVENTER_EXCEPTION; \
325     noitL(noit_error, "[%s] SSL read error: %s\n", nctx->remote_str, strerror(errno)); \
326     goto socket_error; \
327   } \
328   ctx->bytes_read = 0; \
329   ctx->bytes_expected = 0; \
330   if(len != size) { \
331     noitL(noit_error, "[%s] SSL short read [%d] (%d/%lu).  Reseting connection.\n", \
332           nctx->remote_str, ctx->state, len, (long unsigned int)size); \
333     goto socket_error; \
334   } \
335 } while(0)
336
337 int
338 stratcon_jlog_recv_handler(eventer_t e, int mask, void *closure,
339                            struct timeval *now) {
340   noit_connection_ctx_t *nctx = closure;
341   jlog_streamer_ctx_t *ctx = nctx->consumer_ctx;
342   jlog_streamer_ctx_t dummy;
343   int len;
344   jlog_id n_chkpt;
345
346   if(mask & EVENTER_EXCEPTION || nctx->wants_shutdown) {
347     if(write(e->fd, e, 0) == -1)
348       noitL(noit_error, "socket error: %s\n", strerror(errno));
349  socket_error:
350     ctx->state = JLOG_STREAMER_WANT_INITIATE;
351     ctx->count = 0;
352     ctx->bytes_read = 0;
353     ctx->bytes_expected = 0;
354     if(ctx->buffer) free(ctx->buffer);
355     ctx->buffer = NULL;
356     noit_connection_schedule_reattempt(nctx, now);
357     eventer_remove_fd(e->fd);
358     nctx->e = NULL;
359     e->opset->close(e->fd, &mask, e);
360     return 0;
361   }
362
363   while(1) {
364     switch(ctx->state) {
365       case JLOG_STREAMER_WANT_INITIATE:
366         len = e->opset->write(e->fd, &ctx->jlog_feed_cmd,
367                               sizeof(ctx->jlog_feed_cmd),
368                               &mask, e);
369         if(len < 0) {
370           if(errno == EAGAIN) return mask | EVENTER_EXCEPTION;
371           goto socket_error;
372         }
373         if(len != sizeof(ctx->jlog_feed_cmd)) {
374           noitL(noit_error, "short write [%d/%d] on initiating stream.\n",
375                 (int)len, (int)sizeof(ctx->jlog_feed_cmd));
376           goto socket_error;
377         }
378         ctx->state = JLOG_STREAMER_WANT_COUNT;
379         break;
380
381       case JLOG_STREAMER_WANT_ERROR:
382         FULLREAD(e, ctx, 0 - ctx->count);
383         noitL(noit_error, "[%s] %.*s\n", nctx->remote_str,
384               0 - ctx->count, ctx->buffer);
385         free(ctx->buffer); ctx->buffer = NULL;
386         goto socket_error;
387         break;
388
389       case JLOG_STREAMER_WANT_COUNT:
390         FULLREAD(e, ctx, sizeof(u_int32_t));
391         memcpy(&dummy.count, ctx->buffer, sizeof(u_int32_t));
392         ctx->count = ntohl(dummy.count);
393         free(ctx->buffer); ctx->buffer = NULL;
394         if(ctx->count < 0)
395           ctx->state = JLOG_STREAMER_WANT_ERROR;
396         else
397           ctx->state = JLOG_STREAMER_WANT_HEADER;
398         break;
399
400       case JLOG_STREAMER_WANT_HEADER:
401         if(ctx->count == 0) {
402           ctx->state = JLOG_STREAMER_WANT_COUNT;
403           break;
404         }
405         FULLREAD(e, ctx, sizeof(ctx->header));
406         memcpy(&dummy.header, ctx->buffer, sizeof(ctx->header));
407         ctx->header.chkpt.log = ntohl(dummy.header.chkpt.log);
408         ctx->header.chkpt.marker = ntohl(dummy.header.chkpt.marker);
409         ctx->header.tv_sec = ntohl(dummy.header.tv_sec);
410         ctx->header.tv_usec = ntohl(dummy.header.tv_usec);
411         ctx->header.message_len = ntohl(dummy.header.message_len);
412         free(ctx->buffer); ctx->buffer = NULL;
413         ctx->state = JLOG_STREAMER_WANT_BODY;
414         break;
415
416       case JLOG_STREAMER_WANT_BODY:
417         FULLREAD(e, ctx, (unsigned long)ctx->header.message_len);
418         if(ctx->header.message_len > 0)
419           ctx->push(DS_OP_INSERT, &nctx->r.remote, nctx->remote_cn,
420                     ctx->buffer, NULL);
421         else if(ctx->buffer)
422           free(ctx->buffer);
423         /* Don't free the buffer, it's used by the datastore process. */
424         ctx->buffer = NULL;
425         ctx->count--;
426         ctx->total_events++;
427         if(ctx->count == 0) {
428           eventer_t completion_e;
429           eventer_remove_fd(e->fd);
430           completion_e = eventer_alloc();
431           memcpy(completion_e, e, sizeof(*e));
432           nctx->e = completion_e;
433           completion_e->mask = EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION;
434           ctx->state = JLOG_STREAMER_IS_ASYNC;
435           ctx->push(DS_OP_CHKPT, &nctx->r.remote, nctx->remote_cn,
436                     NULL, completion_e);
437           noitL(noit_debug, "Pushing %s batch async [%s]: [%u/%u]\n",
438                 feed_type_to_str(ntohl(ctx->jlog_feed_cmd)),
439                 nctx->remote_cn ? nctx->remote_cn : "(null)",
440                 ctx->header.chkpt.log, ctx->header.chkpt.marker);
441           return 0;
442         } else
443           ctx->state = JLOG_STREAMER_WANT_HEADER;
444         break;
445
446       case JLOG_STREAMER_IS_ASYNC:
447         ctx->state = JLOG_STREAMER_WANT_CHKPT; /* falls through */
448       case JLOG_STREAMER_WANT_CHKPT:
449         noitL(noit_debug, "Pushing %s checkpoint [%s]: [%u/%u]\n",
450               feed_type_to_str(ntohl(ctx->jlog_feed_cmd)),
451               nctx->remote_cn ? nctx->remote_cn : "(null)",
452               ctx->header.chkpt.log, ctx->header.chkpt.marker);
453         n_chkpt.log = htonl(ctx->header.chkpt.log);
454         n_chkpt.marker = htonl(ctx->header.chkpt.marker);
455
456         /* screw short writes.  I'd rather die than not write my data! */
457         len = e->opset->write(e->fd, &n_chkpt, sizeof(jlog_id),
458                               &mask, e);
459         if(len < 0) {
460           if(errno == EAGAIN) return mask | EVENTER_EXCEPTION;
461           goto socket_error;
462         }
463         if(len != sizeof(jlog_id)) {
464           noitL(noit_error, "short write on checkpointing stream.\n");
465           goto socket_error;
466         }
467         ctx->state = JLOG_STREAMER_WANT_COUNT;
468         break;
469     }
470   }
471   /* never get here */
472 }
473
474 int
475 noit_connection_ssl_upgrade(eventer_t e, int mask, void *closure,
476                             struct timeval *now) {
477   noit_connection_ctx_t *nctx = closure;
478   int rv;
479
480   rv = eventer_SSL_connect(e, &mask);
481   if(rv > 0) {
482     eventer_ssl_ctx_t *sslctx;
483     e->callback = nctx->consumer_callback;
484     /* We must make a copy of the acceptor_closure_t for each new
485      * connection.
486      */
487     if((sslctx = eventer_get_eventer_ssl_ctx(e)) != NULL) {
488       const char *cn, *end;
489       cn = eventer_ssl_get_peer_subject(sslctx);
490       if(cn && (cn = strstr(cn, "CN=")) != NULL) {
491         cn += 3;
492         end = cn;
493         while(*end && *end != '/') end++;
494         nctx->remote_cn = malloc(end - cn + 1);
495         memcpy(nctx->remote_cn, cn, end - cn);
496         nctx->remote_cn[end-cn] = '\0';
497       }
498     }
499     return e->callback(e, mask, e->closure, now);
500   }
501   if(errno == EAGAIN) return mask | EVENTER_EXCEPTION;
502
503   noitL(noit_error, "jlog streamer SSL upgrade failed.\n");
504   eventer_remove_fd(e->fd);
505   nctx->e = NULL;
506   e->opset->close(e->fd, &mask, e);
507   noit_connection_schedule_reattempt(nctx, now);
508   return 0;
509 }
510 int
511 noit_connection_complete_connect(eventer_t e, int mask, void *closure,
512                                  struct timeval *now) {
513   noit_connection_ctx_t *nctx = closure;
514   const char *cert, *key, *ca, *ciphers;
515   char remote_str[128], tmp_str[128];
516   eventer_ssl_ctx_t *sslctx;
517   int aerrno, len;
518   socklen_t aerrno_len = sizeof(aerrno);
519
520   if(getsockopt(e->fd,SOL_SOCKET,SO_ERROR, &aerrno, &aerrno_len) == 0)
521     if(aerrno != 0) goto connect_error;
522   aerrno = 0;
523
524   if(mask & EVENTER_EXCEPTION) {
525     if(aerrno == 0 && (write(e->fd, e, 0) == -1))
526       aerrno = errno;
527  connect_error:
528     switch(nctx->r.remote.sa_family) {
529       case AF_INET:
530         len = sizeof(struct sockaddr_in);
531         inet_ntop(nctx->r.remote.sa_family, &nctx->r.remote_in.sin_addr,
532                   tmp_str, len);
533         snprintf(remote_str, sizeof(remote_str), "%s:%d",
534                  tmp_str, ntohs(nctx->r.remote_in.sin_port));
535         break;
536       case AF_INET6:
537         len = sizeof(struct sockaddr_in6);
538         inet_ntop(nctx->r.remote.sa_family, &nctx->r.remote_in6.sin6_addr,
539                   tmp_str, len);
540         snprintf(remote_str, sizeof(remote_str), "%s:%d",
541                  tmp_str, ntohs(nctx->r.remote_in6.sin6_port));
542        break;
543       case AF_UNIX:
544         len = SUN_LEN(&(nctx->r.remote_un));
545         snprintf(remote_str, sizeof(remote_str), "%s", nctx->r.remote_un.sun_path);
546         break;
547       default:
548         snprintf(remote_str, sizeof(remote_str), "(unknown)");
549     }
550     noitL(noit_error, "Error connecting to %s: %s\n",
551           remote_str, strerror(aerrno));
552     eventer_remove_fd(e->fd);
553     nctx->e = NULL;
554     e->opset->close(e->fd, &mask, e);
555     noit_connection_schedule_reattempt(nctx, now);
556     return 0;
557   }
558
559 #define SSLCONFGET(var,name) do { \
560   if(!noit_hash_retr_str(nctx->sslconfig, name, strlen(name), \
561                          &var)) var = NULL; } while(0)
562   SSLCONFGET(cert, "certificate_file");
563   SSLCONFGET(key, "key_file");
564   SSLCONFGET(ca, "ca_chain");
565   SSLCONFGET(ciphers, "ciphers");
566   sslctx = eventer_ssl_ctx_new(SSL_CLIENT, cert, key, ca, ciphers);
567   if(!sslctx) goto connect_error;
568
569   memcpy(&nctx->last_connect, now, sizeof(*now));
570   eventer_ssl_ctx_set_verify(sslctx, eventer_ssl_verify_cert,
571                              nctx->sslconfig);
572   EVENTER_ATTACH_SSL(e, sslctx);
573   e->callback = noit_connection_ssl_upgrade;
574   return e->callback(e, mask, closure, now);
575 }
576 static void
577 noit_connection_initiate_connection(noit_connection_ctx_t *nctx) {
578   struct timeval __now;
579   eventer_t e;
580   int rv, fd = -1;
581 #ifdef SO_KEEPALIVE
582   int optval;
583   socklen_t optlen = sizeof(optval);
584 #endif
585
586   nctx->e = NULL;
587   if(nctx->wants_permanent_shutdown) {
588     noit_connection_ctx_dealloc(nctx);
589     return;
590   }
591   /* Open a socket */
592   fd = socket(nctx->r.remote.sa_family, SOCK_STREAM, 0);
593   if(fd < 0) goto reschedule;
594
595   /* Make it non-blocking */
596   if(eventer_set_fd_nonblocking(fd)) goto reschedule;
597 #define set_or_bail(type, opt, val) do { \
598   optval = val; \
599   optlen = sizeof(optval); \
600   if(setsockopt(fd, type, opt, &optval, optlen) < 0) { \
601     noitL(noit_error, "Cannot set " #type "/" #opt " on jlog socket: %s\n", \
602           strerror(errno)); \
603     goto reschedule; \
604   } \
605 } while(0)
606 #ifdef SO_KEEPALIVE
607   set_or_bail(SOL_SOCKET, SO_KEEPALIVE, 1);
608 #endif
609 #ifdef TCP_KEEPALIVE_THRESHOLD
610   set_or_bail(IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD, 10 * 1000);
611 #endif
612 #ifdef TCP_KEEPALIVE_ABORT_THRESHOLD
613   set_or_bail(IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, 30 * 1000);
614 #endif
615 #ifdef TCP_CONN_NOTIFY_THRESHOLD
616   set_or_bail(IPPROTO_TCP, TCP_CONN_NOTIFY_THRESHOLD, 10 * 1000);
617 #endif
618 #ifdef TCP_CONN_ABORT_THRESHOLD
619   set_or_bail(IPPROTO_TCP, TCP_CONN_ABORT_THRESHOLD, 30 * 1000);
620 #endif
621
622   /* Initiate a connection */
623   rv = connect(fd, &nctx->r.remote, nctx->remote_len);
624   if(rv == -1 && errno != EINPROGRESS) goto reschedule;
625
626   /* Register a handler for connection completion */
627   e = eventer_alloc();
628   e->fd = fd;
629   e->mask = EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION;
630   e->callback = noit_connection_complete_connect;
631   e->closure = nctx;
632   nctx->e = e;
633   eventer_add(e);
634   return;
635
636  reschedule:
637   if(fd >= 0) close(fd);
638   gettimeofday(&__now, NULL);
639   noit_connection_schedule_reattempt(nctx, &__now);
640   return;
641 }
642
643 int
644 initiate_noit_connection(const char *host, unsigned short port,
645                          noit_hash_table *sslconfig, noit_hash_table *config,
646                          eventer_func_t handler, void *closure,
647                          void (*freefunc)(void *)) {
648   noit_connection_ctx_t *ctx;
649
650   int8_t family;
651   int rv;
652   union {
653     struct in_addr addr4;
654     struct in6_addr addr6;
655   } a;
656
657   if(host[0] == '/') {
658     family = AF_UNIX;
659   }
660   else {
661     family = AF_INET;
662     rv = inet_pton(family, host, &a);
663     if(rv != 1) {
664       family = AF_INET6;
665       rv = inet_pton(family, host, &a);
666       if(rv != 1) {
667         noitL(noit_stderr, "Cannot translate '%s' to IP\n", host);
668         return -1;
669       }
670     }
671   }
672
673   ctx = noit_connection_ctx_alloc();
674   ctx->remote_str = calloc(1, strlen(host) + 7);
675   snprintf(ctx->remote_str, strlen(host) + 7,
676            "%s:%d", host, port);
677  
678   memset(&ctx->r, 0, sizeof(ctx->r));
679   if(family == AF_UNIX) {
680     struct sockaddr_un *s = &ctx->r.remote_un;
681     s->sun_family = AF_UNIX;
682     strncpy(s->sun_path, host, sizeof(s->sun_path)-1);
683     ctx->remote_len = sizeof(*s);
684   }
685   else if(family == AF_INET) {
686     struct sockaddr_in *s = &ctx->r.remote_in;
687     s->sin_family = family;
688     s->sin_port = htons(port);
689     memcpy(&s->sin_addr, &a, sizeof(struct in_addr));
690     ctx->remote_len = sizeof(*s);
691   }
692   else {
693     struct sockaddr_in6 *s = &ctx->r.remote_in6;
694     s->sin6_family = family;
695     s->sin6_port = htons(port);
696     memcpy(&s->sin6_addr, &a, sizeof(a));
697     ctx->remote_len = sizeof(*s);
698   }
699
700   if(ctx->sslconfig)
701     noit_hash_delete_all(ctx->sslconfig, free, free);
702   else
703     ctx->sslconfig = calloc(1, sizeof(noit_hash_table));
704   noit_hash_merge_as_dict(ctx->sslconfig, sslconfig);
705   if(ctx->config)
706     noit_hash_delete_all(ctx->config, free, free);
707   else
708     ctx->config = calloc(1, sizeof(noit_hash_table));
709   noit_hash_merge_as_dict(ctx->config, config);
710
711   ctx->consumer_callback = handler;
712   ctx->consumer_free = freefunc;
713   ctx->consumer_ctx = closure;
714   noit_connection_initiate_connection(ctx);
715   return 0;
716 }
717
718 void
719 stratcon_streamer_connection(const char *toplevel, const char *destination,
720                              eventer_func_t handler,
721                              void *(*handler_alloc)(void), void *handler_ctx,
722                              void (*handler_free)(void *)) {
723   int i, cnt = 0;
724   noit_conf_section_t *noit_configs;
725   char path[256];
726
727   snprintf(path, sizeof(path), "/%s/noits//noit", toplevel ? toplevel : "*");
728   noit_configs = noit_conf_get_sections(NULL, path, &cnt);
729   noitL(noit_error, "Found %d %s stanzas\n", cnt, path);
730   for(i=0; i<cnt; i++) {
731     char address[256];
732     unsigned short port;
733     int portint;
734     noit_hash_table *sslconfig, *config;
735
736     if(!noit_conf_get_stringbuf(noit_configs[i],
737                                 "ancestor-or-self::node()/@address",
738                                 address, sizeof(address))) {
739       noitL(noit_error, "address attribute missing in noit %d\n", i+1);
740       continue;
741     }
742     /* if destination is specified, exact match it */
743     if(destination && strcmp(address, destination)) continue;
744
745     if(!noit_conf_get_int(noit_configs[i],
746                           "ancestor-or-self::node()/@port", &portint))
747       portint = 0;
748     port = (unsigned short) portint;
749     if(address[0] != '/' && (portint == 0 || (port != portint))) {
750       /* UNIX sockets don't require a port (they'll ignore it if specified */
751       noitL(noit_stderr,
752             "Invalid port [%d] specified in stanza %d\n", port, i+1);
753       continue;
754     }
755     sslconfig = noit_conf_get_hash(noit_configs[i], "sslconfig");
756     config = noit_conf_get_hash(noit_configs[i], "config");
757
758     noitL(noit_error, "initiating to %s\n", address);
759     initiate_noit_connection(address, port, sslconfig, config,
760                              handler,
761                              handler_alloc ? handler_alloc() : handler_ctx,
762                              handler_free);
763     noit_hash_destroy(sslconfig,free,free);
764     free(sslconfig);
765     noit_hash_destroy(config,free,free);
766     free(config);
767   }
768   free(noit_configs);
769 }
770 void
771 stratcon_jlog_streamer_reload(const char *toplevel) {
772   stratcon_streamer_connection(toplevel, NULL,
773                                stratcon_jlog_recv_handler,
774                                (void *(*)())stratcon_jlog_streamer_datastore_ctx_alloc,
775                                NULL,
776                                jlog_streamer_ctx_free);
777 }
778
779 static int
780 stratcon_console_show_noits(noit_console_closure_t ncct,
781                             int argc, char **argv,
782                             noit_console_state_t *dstate,
783                             void *closure) {
784   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
785   uuid_t key_id;
786   int klen, n = 0, i;
787   void *vconn;
788   noit_connection_ctx_t **ctx;
789
790   pthread_mutex_lock(&noits_lock);
791   ctx = malloc(sizeof(*ctx) * noits.size);
792   while(noit_hash_next(&noits, &iter, (const char **)key_id, &klen,
793                        &vconn)) {
794     ctx[n] = (noit_connection_ctx_t *)vconn;
795     noit_atomic_inc32(&ctx[n]->refcnt);
796     n++;
797   }
798   pthread_mutex_unlock(&noits_lock);
799   qsort(ctx, n, sizeof(*ctx), remote_str_sort);
800   for(i=0; i<n; i++) {
801     nc_print_noit_conn_brief(ncct, ctx[i]);
802     noit_connection_ctx_deref(ctx[i]);
803   }
804   free(ctx);
805   return 0;
806 }
807
808 static int
809 rest_show_noits(noit_http_rest_closure_t *restc,
810                 int npats, char **pats) {
811   xmlDocPtr doc;
812   xmlNodePtr root;
813   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
814   uuid_t key_id;
815   int klen, n = 0, i;
816   void *vconn;
817   noit_connection_ctx_t **ctxs;
818   struct timeval now, diff, last;
819   gettimeofday(&now, NULL);
820
821   pthread_mutex_lock(&noits_lock);
822   ctxs = malloc(sizeof(*ctxs) * noits.size);
823   while(noit_hash_next(&noits, &iter, (const char **)key_id, &klen,
824                        &vconn)) {
825     ctxs[n] = (noit_connection_ctx_t *)vconn;
826     noit_atomic_inc32(&ctxs[n]->refcnt);
827     n++;
828   }
829   pthread_mutex_unlock(&noits_lock);
830   qsort(ctxs, n, sizeof(*ctxs), remote_str_sort);
831
832   doc = xmlNewDoc((xmlChar *)"1.0");
833   root = xmlNewDocNode(doc, NULL, (xmlChar *)"noits", NULL);
834   xmlDocSetRootElement(doc, root);
835   for(i=0; i<n; i++) {
836     char buff[256];
837     const char *feedtype = "unknown", *state = "unknown";
838     xmlNodePtr node;
839     noit_connection_ctx_t *ctx = ctxs[i];
840     jlog_streamer_ctx_t *jctx = ctx->consumer_ctx;
841
842     node = xmlNewNode(NULL, (xmlChar *)"noit");
843     snprintf(buff, sizeof(buff), "%llu.%06d",
844              (long long unsigned)ctx->last_connect.tv_sec,
845              (int)ctx->last_connect.tv_usec);
846     xmlSetProp(node, (xmlChar *)"last_connect", (xmlChar *)buff);
847     xmlSetProp(node, (xmlChar *)"state", ctx->remote_cn ?
848                (xmlChar *)"connected" :
849                (ctx->timeout_event ? (xmlChar *)"disconnected" :
850                                     (xmlChar *)"connecting"));
851     if(ctx->e) {
852       char buff[128];
853       const char *addrstr = NULL;
854       struct sockaddr_in6 addr6;
855       socklen_t len = sizeof(addr6);
856       if(getsockname(ctx->e->fd, (struct sockaddr *)&addr6, &len) == 0) {
857         unsigned short port = 0;
858         if(addr6.sin6_family == AF_INET) {
859           addrstr = inet_ntop(addr6.sin6_family,
860                               &((struct sockaddr_in *)&addr6)->sin_addr,
861                               buff, sizeof(buff));
862           port = ntohs(((struct sockaddr_in *)&addr6)->sin_port);
863         }
864         else if(addr6.sin6_family == AF_INET6) {
865           addrstr = inet_ntop(addr6.sin6_family, &addr6.sin6_addr,
866                               buff, sizeof(buff));
867           port = ntohs(addr6.sin6_port);
868         }
869         if(addrstr != NULL) {
870           snprintf(buff + strlen(buff), sizeof(buff) - strlen(buff),
871                    ":%u", port);
872           xmlSetProp(node, (xmlChar *)"local", (xmlChar *)buff);
873         }
874       }
875     }
876     xmlSetProp(node, (xmlChar *)"remote", (xmlChar *)ctx->remote_str);
877     feedtype = feed_type_to_str(ntohl(jctx->jlog_feed_cmd));
878     xmlSetProp(node, (xmlChar *)"type", (xmlChar *)feedtype);
879     if(ctx->timeout_event) {
880       sub_timeval(ctx->timeout_event->whence, now, &diff);
881       snprintf(buff, sizeof(buff), "%llu.%06d",
882                (long long unsigned)diff.tv_sec, (int)diff.tv_usec);
883       xmlSetProp(node, (xmlChar *)"next_attempt", (xmlChar *)buff);
884     }
885     else if(ctx->remote_cn) {
886       if(ctx->remote_cn)
887         xmlSetProp(node, (xmlChar *)"remote_cn", (xmlChar *)ctx->remote_cn);
888  
889       switch(jctx->state) {
890         case JLOG_STREAMER_WANT_INITIATE: state = "initiate"; break;
891         case JLOG_STREAMER_WANT_COUNT: state = "waiting for next batch"; break;
892         case JLOG_STREAMER_WANT_ERROR: state = "waiting for error"; break;
893         case JLOG_STREAMER_WANT_HEADER: state = "reading header"; break;
894         case JLOG_STREAMER_WANT_BODY: state = "reading body"; break;
895         case JLOG_STREAMER_IS_ASYNC: state = "asynchronously processing"; break;
896         case JLOG_STREAMER_WANT_CHKPT: state = "checkpointing"; break;
897       }
898       xmlSetProp(node, (xmlChar *)"state", (xmlChar *)state);
899       snprintf(buff, sizeof(buff), "%08x:%08x",
900                jctx->header.chkpt.log, jctx->header.chkpt.marker);
901       xmlSetProp(node, (xmlChar *)"checkpoint", (xmlChar *)buff);
902       snprintf(buff, sizeof(buff), "%llu",
903                (unsigned long long)jctx->total_events);
904       xmlSetProp(node, (xmlChar *)"session_events", (xmlChar *)buff);
905       snprintf(buff, sizeof(buff), "%llu",
906                (unsigned long long)jctx->total_bytes_read);
907       xmlSetProp(node, (xmlChar *)"session_bytes", (xmlChar *)buff);
908  
909       sub_timeval(now, ctx->last_connect, &diff);
910       snprintf(buff, sizeof(buff), "%lld.%06d",
911                (long long)diff.tv_sec, (int)diff.tv_usec);
912       xmlSetProp(node, (xmlChar *)"session_duration", (xmlChar *)buff);
913  
914       if(jctx->header.tv_sec) {
915         last.tv_sec = jctx->header.tv_sec;
916         last.tv_usec = jctx->header.tv_usec;
917         snprintf(buff, sizeof(buff), "%llu.%06d",
918                  (unsigned long long)last.tv_sec, (int)last.tv_usec);
919         xmlSetProp(node, (xmlChar *)"last_event", (xmlChar *)buff);
920         sub_timeval(now, last, &diff);
921         snprintf(buff, sizeof(buff), "%lld.%06d",
922                  (long long)diff.tv_sec, (int)diff.tv_usec);
923         xmlSetProp(node, (xmlChar *)"last_event_age", (xmlChar *)buff);
924       }
925     }
926
927     xmlAddChild(root, node);
928     noit_connection_ctx_deref(ctx);
929   }
930   free(ctxs);
931
932   noit_http_response_ok(restc->http_ctx, "text/xml");
933   noit_http_response_xml(restc->http_ctx, doc);
934   noit_http_response_end(restc->http_ctx);
935   xmlFreeDoc(doc);
936   return 0;
937 }
938 static int
939 stratcon_add_noit(const char *target, unsigned short port) {
940   int cnt;
941   char path[256];
942   char port_str[6];
943   noit_conf_section_t *noit_configs, parent;
944   xmlNodePtr newnoit;
945
946   snprintf(path, sizeof(path),
947            "//noits//noit[@address=\"%s\" and @port=\"%d\"]", target, port);
948   noit_configs = noit_conf_get_sections(NULL, path, &cnt);
949   free(noit_configs);
950   if(cnt != 0) return 0;
951
952   parent = noit_conf_get_section(NULL, "//noits");
953   if(!parent) return 0;
954   snprintf(port_str, sizeof(port_str), "%d", port);
955   newnoit = xmlNewNode(NULL, (xmlChar *)"noit");
956   xmlSetProp(newnoit, (xmlChar *)"address", (xmlChar *)target);
957   xmlSetProp(newnoit, (xmlChar *)"port", (xmlChar *)port_str);
958   xmlAddChild(parent, newnoit);
959   stratcon_streamer_connection(NULL, target,
960                                stratcon_jlog_recv_handler,
961                                (void *(*)())stratcon_jlog_streamer_datastore_ctx_alloc,
962                                NULL,
963                                jlog_streamer_ctx_free);
964   stratcon_streamer_connection(NULL, target,
965                                stratcon_jlog_recv_handler,
966                                (void *(*)())stratcon_jlog_streamer_iep_ctx_alloc,
967                                NULL,
968                                jlog_streamer_ctx_free);
969   return 1;
970 }
971 static int
972 stratcon_remove_noit(const char *target, unsigned short port) {
973   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
974   uuid_t key_id;
975   int klen, n = 0, i, cnt = 0;
976   void *vconn;
977   noit_connection_ctx_t **ctx;
978   noit_conf_section_t *noit_configs;
979   char path[256];
980   char remote_str[256];
981
982   snprintf(remote_str, sizeof(remote_str), "%s:%d", target, port);
983
984   snprintf(path, sizeof(path),
985            "//noits//noit[@address=\"%s\" and @port=\"%d\"]", target, port);
986   noit_configs = noit_conf_get_sections(NULL, path, &cnt);
987   for(i=0; i<cnt; i++) {
988     xmlUnlinkNode(noit_configs[i]);
989     xmlFreeNode(noit_configs[i]);
990   }
991   free(noit_configs);
992
993   pthread_mutex_lock(&noits_lock);
994   ctx = malloc(sizeof(*ctx) * noits.size);
995   while(noit_hash_next(&noits, &iter, (const char **)key_id, &klen,
996                        &vconn)) {
997     if(!strcmp(((noit_connection_ctx_t *)vconn)->remote_str, remote_str)) {
998       ctx[n] = (noit_connection_ctx_t *)vconn;
999       noit_atomic_inc32(&ctx[n]->refcnt);
1000       n++;
1001     }
1002   }
1003   pthread_mutex_unlock(&noits_lock);
1004   for(i=0; i<n; i++) {
1005     noit_connection_ctx_dealloc(ctx[i]); /* once for the record */
1006     noit_connection_ctx_deref(ctx[i]);   /* once for the aboce inc32 */
1007   }
1008   free(ctx);
1009   return n;
1010 }
1011 static int
1012 rest_set_noit(noit_http_rest_closure_t *restc,
1013               int npats, char **pats) {
1014   noit_http_session_ctx *ctx = restc->http_ctx;
1015   unsigned short port = 43191;
1016   if(npats < 1 || npats > 2)
1017     noit_http_response_server_error(ctx, "text/xml");
1018   if(npats == 2) port = atoi(pats[1]);
1019   if(stratcon_add_noit(pats[0], port))
1020     noit_http_response_ok(ctx, "text/xml");
1021   else
1022     noit_http_response_standard(ctx, 409, "EXISTS", "text/xml");
1023   if(noit_conf_write_file(NULL) != 0)
1024     noitL(noit_error, "local config write failed\n");
1025   noit_conf_mark_changed();
1026   noit_http_response_end(ctx);
1027   return 0;
1028 }
1029 static int
1030 rest_delete_noit(noit_http_rest_closure_t *restc,
1031                  int npats, char **pats) {
1032   noit_http_session_ctx *ctx = restc->http_ctx;
1033   unsigned short port = 43191;
1034   if(npats < 1 || npats > 2)
1035     noit_http_response_server_error(ctx, "text/xml");
1036   if(npats == 2) port = atoi(pats[1]);
1037   if(stratcon_remove_noit(pats[0], port))
1038     noit_http_response_ok(ctx, "text/xml");
1039   else
1040     noit_http_response_not_found(ctx, "text/xml");
1041   if(noit_conf_write_file(NULL) != 0)
1042     noitL(noit_error, "local config write failed\n");
1043   noit_conf_mark_changed();
1044   noit_http_response_end(ctx);
1045   return 0;
1046 }
1047 static int
1048 stratcon_console_conf_noits(noit_console_closure_t ncct,
1049                             int argc, char **argv,
1050                             noit_console_state_t *dstate,
1051                             void *closure) {
1052   char *cp, target[128];
1053   unsigned short port = 43191;
1054   int adding = (int)(vpsized_int)closure;
1055   if(argc != 1)
1056     return -1;
1057
1058   cp = strchr(argv[0], ':');
1059   if(cp) {
1060     strlcpy(target, argv[0], MIN(sizeof(target), cp-argv[0]+1));
1061     port = atoi(cp+1);
1062   }
1063   else strlcpy(target, argv[0], sizeof(target));
1064   if(adding) {
1065     if(stratcon_add_noit(target, port)) {
1066       nc_printf(ncct, "Added noit at %s:%d\n", target, port);
1067     }
1068     else {
1069       nc_printf(ncct, "Failed to add noit at %s:%d\n", target, port);
1070     }
1071   }
1072   else {
1073     if(stratcon_remove_noit(target, port)) {
1074       nc_printf(ncct, "Removed noit at %s:%d\n", target, port);
1075     }
1076     else {
1077       nc_printf(ncct, "Failed to remove noit at %s:%d\n", target, port);
1078     }
1079   }
1080   return 0;
1081 }
1082
1083 static void
1084 register_console_streamer_commands() {
1085   noit_console_state_t *tl;
1086   cmd_info_t *showcmd, *confcmd, *conftcmd, *conftnocmd;
1087
1088   tl = noit_console_state_initial();
1089   showcmd = noit_console_state_get_cmd(tl, "show");
1090   assert(showcmd && showcmd->dstate);
1091   confcmd = noit_console_state_get_cmd(tl, "configure");
1092   conftcmd = noit_console_state_get_cmd(confcmd->dstate, "terminal");
1093   conftnocmd = noit_console_state_get_cmd(conftcmd->dstate, "no");
1094
1095   noit_console_state_add_cmd(conftcmd->dstate,
1096     NCSCMD("noit", stratcon_console_conf_noits, NULL, NULL, (void *)1));
1097   noit_console_state_add_cmd(conftnocmd->dstate,
1098     NCSCMD("noit", stratcon_console_conf_noits, NULL, NULL, (void *)0));
1099
1100   noit_console_state_add_cmd(showcmd->dstate,
1101     NCSCMD("noits", stratcon_console_show_noits, NULL, NULL, NULL));
1102 }
1103
1104 void
1105 stratcon_jlog_streamer_init(const char *toplevel) {
1106   pthread_mutex_init(&noits_lock, NULL);
1107   eventer_name_callback("noit_connection_reinitiate",
1108                         noit_connection_reinitiate);
1109   eventer_name_callback("stratcon_jlog_recv_handler",
1110                         stratcon_jlog_recv_handler);
1111   eventer_name_callback("noit_connection_ssl_upgrade",
1112                         noit_connection_ssl_upgrade);
1113   eventer_name_callback("noit_connection_complete_connect",
1114                         noit_connection_complete_connect);
1115   register_console_streamer_commands();
1116   stratcon_jlog_streamer_reload(toplevel);
1117   assert(noit_http_rest_register_auth(
1118     "GET", "/noits/", "^show$", rest_show_noits,
1119              noit_http_rest_client_cert_auth
1120   ) == 0);
1121   assert(noit_http_rest_register_auth(
1122     "PUT", "/noits/", "^set/([^/:]+)$", rest_set_noit,
1123              noit_http_rest_client_cert_auth
1124   ) == 0);
1125   assert(noit_http_rest_register_auth(
1126     "PUT", "/noits/", "^set/([^/:]+):(\\d+)$", rest_set_noit,
1127              noit_http_rest_client_cert_auth
1128   ) == 0);
1129   assert(noit_http_rest_register_auth(
1130     "DELETE", "/noits/", "^delete/([^/:]+)$", rest_delete_noit,
1131              noit_http_rest_client_cert_auth
1132   ) == 0);
1133   assert(noit_http_rest_register_auth(
1134     "DELETE", "/noits/", "^delete/([^/:]+):(\\d+)$", rest_delete_noit,
1135              noit_http_rest_client_cert_auth
1136   ) == 0);
1137 }
1138
Note: See TracBrowser for help on using the browser.