root/src/noit_livestream_listener.c

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