root/src/noit_livestream_listener.c

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

much better support for timestamps and debugging information, refs #87

  • 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_livestream_listener.h"
40 #include "noit_check.h"
41
42 #include <unistd.h>
43 #include <errno.h>
44
45 static noit_atomic32_t ls_counter = 0;
46
47 struct log_entry {
48   int len;
49   char *buff;
50   struct log_entry *next;
51 };
52
53 typedef struct {
54   u_int32_t period;
55   struct log_entry *lqueue;
56   struct log_entry *lqueue_end;
57   sem_t lqueue_sem;
58   pthread_mutex_t lqueue_lock;
59   int uuid_read;
60   char uuid_str[37];
61   char *feed;
62   uuid_t uuid;
63   noit_check_t *check;
64   int wants_shutdown;
65 } noit_livestream_closure_t;
66
67 noit_livestream_closure_t *
68 noit_livestream_closure_alloc(void) {
69   noit_livestream_closure_t *jcl;
70   jcl = calloc(1, sizeof(*jcl));
71   pthread_mutex_init(&jcl->lqueue_lock, NULL);
72   sem_init(&jcl->lqueue_sem, 0, 0);
73   return jcl;
74 }
75
76 void
77 noit_livestream_closure_free(noit_livestream_closure_t *jcl) {
78   struct log_entry *tofree;
79   while(jcl->lqueue) {
80     tofree = jcl->lqueue;
81     jcl->lqueue = jcl->lqueue->next;
82     free(tofree->buff);
83     free(tofree);
84   }
85   free(jcl);
86 }
87
88 static int
89 noit_livestream_logio_open(noit_log_stream_t ls) {
90   return 0;
91 }
92 static int
93 noit_livestream_logio_reopen(noit_log_stream_t ls) {
94   /* no op */
95   return 0;
96 }
97 static int
98 noit_livestream_logio_write(noit_log_stream_t ls, const void *buf, size_t len) {
99   noit_livestream_closure_t *jcl;
100   struct log_entry *le;
101
102   jcl = noit_log_stream_get_ctx(ls);
103   if(!jcl) return 0;
104
105   if(jcl->wants_shutdown) {
106     /* This has been terminated by the client, _fail here_ */
107     return 0;
108   }
109
110   le = calloc(1, sizeof(*le));
111   le->len = len;
112   le->buff = malloc(len);
113   memcpy(le->buff, buf, len);
114   le->next = NULL;
115   pthread_mutex_lock(&jcl->lqueue_lock);
116   if(!jcl->lqueue_end) jcl->lqueue = le;
117   else jcl->lqueue_end->next = le;
118   jcl->lqueue_end = le;
119   pthread_mutex_unlock(&jcl->lqueue_lock);
120   sem_post(&jcl->lqueue_sem);
121   return len;
122 }
123 static int
124 noit_livestream_logio_close(noit_log_stream_t ls) {
125   noit_livestream_closure_t *jcl;
126   jcl = noit_log_stream_get_ctx(ls);
127   if(jcl) noit_livestream_closure_free(jcl);
128   noit_log_stream_set_ctx(ls, NULL);
129   return 0;
130 }
131 static logops_t noit_livestream_logio_ops = {
132   noit_livestream_logio_open,
133   noit_livestream_logio_reopen,
134   noit_livestream_logio_write,
135   NULL,
136   noit_livestream_logio_close,
137   NULL,
138   NULL
139 };
140
141 void
142 noit_livestream_listener_init() {
143   noit_register_logops("noit_livestream", &noit_livestream_logio_ops);
144   eventer_name_callback("livestream_transit/1.0", noit_livestream_handler);
145   noit_control_dispatch_delegate(noit_control_dispatch,
146                                  NOIT_LIVESTREAM_DATA_FEED,
147                                  noit_livestream_handler);
148 }
149
150 static int
151 __safe_Ewrite(eventer_t e, void *b, int l, int *mask) {
152   int w, sofar = 0;
153   while(l > sofar) {
154     w = e->opset->write(e->fd, (char *)b + sofar, l - sofar, mask, e);
155     if(w <= 0) return w;
156     sofar += w;
157   }
158   return sofar;
159 }
160 #define Ewrite(a,b) __safe_Ewrite(e,a,b,&mask)
161
162 void *
163 noit_livestream_thread_main(void *e_vptr) {
164   int mask;
165   eventer_t e = e_vptr;
166   acceptor_closure_t *ac = e->closure;
167   noit_livestream_closure_t *jcl = ac->service_ctx;
168
169   /* Go into blocking mode */
170   if(eventer_set_fd_blocking(e->fd) == -1) {
171     noitL(noit_error, "failed setting livestream to blocking: [%d] [%s]\n",
172           errno, strerror(errno));
173     goto alldone;
174   }
175
176   while(1) {
177     u_int32_t netlen;
178     struct log_entry *le = NULL;
179     int rv;
180    
181     sem_wait(&jcl->lqueue_sem);
182     pthread_mutex_lock(&jcl->lqueue_lock);
183     if(jcl->lqueue) {
184       /* If there are items, pop and advance the header pointer */
185       le = jcl->lqueue;
186       jcl->lqueue = jcl->lqueue->next;
187       if(!jcl->lqueue) jcl->lqueue_end = NULL;
188     }
189     pthread_mutex_unlock(&jcl->lqueue_lock);
190
191     if(!le) continue;
192
193     /* Here we actually push the message */
194     netlen = htonl(le->len);
195     if((rv = Ewrite(&netlen, sizeof(netlen))) != sizeof(netlen)) {
196       noitL(noit_error, "Error writing le header over SSL %d != %d\n",
197             rv, (int)sizeof(netlen));
198       goto alldone;
199     }
200     if((rv = Ewrite(le->buff, le->len)) != le->len) {
201       noitL(noit_error, "Error writing livestream message over SSL %d != %d\n",
202             rv, le->len);
203       goto alldone;
204     }
205   }
206
207  alldone:
208   e->opset->close(e->fd, &mask, e);
209   jcl->wants_shutdown = 1;
210   if(ac) acceptor_closure_free(ac);
211   return NULL;
212 }
213
214 int
215 noit_livestream_handler(eventer_t e, int mask, void *closure,
216                         struct timeval *now) {
217   eventer_t newe;
218   pthread_t tid;
219   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
220   acceptor_closure_t *ac = closure;
221   noit_livestream_closure_t *jcl = ac->service_ctx;
222
223   if(mask & EVENTER_EXCEPTION || (jcl && jcl->wants_shutdown)) {
224 socket_error:
225     /* Exceptions cause us to simply snip the connection */
226     eventer_remove_fd(e->fd);
227     e->opset->close(e->fd, &newmask, e);
228     if(jcl) noit_livestream_closure_free(jcl);
229     if(ac) acceptor_closure_free(ac);
230     return 0;
231   }
232
233   if(!ac->service_ctx || !jcl->feed) {
234     int len;
235     jcl = ac->service_ctx = noit_livestream_closure_alloc();
236     /* Setup logger to this channel */
237     if(!jcl->period) {
238       u_int32_t nperiod;
239       len = e->opset->read(e->fd, &nperiod, sizeof(nperiod), &mask, e);
240       if(len == -1 && errno == EAGAIN) return mask | EVENTER_EXCEPTION;
241       if(len != sizeof(nperiod)) goto socket_error;
242       jcl->period = ntohl(nperiod);
243       if(!jcl->period) {
244         noitL(noit_error, "period of 0 specified in livestream.  not allowed.\n");
245         goto socket_error;
246       }
247     }
248     while(jcl->uuid_read < 36) {
249       len = e->opset->read(e->fd, jcl->uuid_str + jcl->uuid_read, 36 - jcl->uuid_read, &mask, e);
250       if(len == -1 && errno == EAGAIN) return mask | EVENTER_EXCEPTION;
251       if(len == 0) goto socket_error;
252       jcl->uuid_read += len;
253     }
254     jcl->uuid_str[36] = '\0';
255     if(uuid_parse(jcl->uuid_str, jcl->uuid)) {
256       noitL(noit_error, "bad uuid received in livestream handler '%s'\n", jcl->uuid_str);
257       goto socket_error;
258     }
259
260     jcl->feed = malloc(32);
261     snprintf(jcl->feed, 32, "livestream/%d", noit_atomic_inc32(&ls_counter));
262     noit_log_stream_new(jcl->feed, "noit_livestream", jcl->feed,
263                         jcl, NULL);
264
265
266     jcl->check = noit_check_watch(jcl->uuid, jcl->period);
267     /* This check must be watched from the livestream */
268     noit_check_transient_add_feed(jcl->check, jcl->feed);
269     /* Note the check */
270     noit_check_log_check(jcl->check);
271     /* kick it off, if it isn't running already */
272     if(!NOIT_CHECK_LIVE(jcl->check)) noit_check_activate(jcl->check);
273   }
274
275   eventer_remove_fd(e->fd);
276   newe = eventer_alloc();
277   memcpy(newe, e, sizeof(*e));
278   if(pthread_create(&tid, NULL, noit_livestream_thread_main, newe) == 0) {
279     return 0;
280   }
281
282   noit_check_transient_remove_feed(jcl->check, jcl->feed);
283   noit_livestream_closure_free(jcl);
284   /* Undo our dup */
285   eventer_free(newe);
286   /* Creating the thread failed, close it down and deschedule. */
287   e->opset->close(e->fd, &newmask, e);
288   return 0;
289 }
Note: See TracBrowser for help on using the browser.