root/src/noit_livestream_listener.c

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

casting warnings, refs #34

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