root/src/noit_livestream_listener.c

Revision 304ec80b8cf842fc0abe5f9029790908b6455957, 9.3 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 months ago)

Convert to libmtev.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  * Copyright (c) 2015, Circonus, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials provided
15  *       with the distribution.
16  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
17  *       of its contributors may be used to endorse or promote products
18  *       derived from this software without specific prior written
19  *       permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <mtev_defines.h>
35 #include <eventer/eventer.h>
36 #include <mtev_listener.h>
37 #include <mtev_memory.h>
38 #include <mtev_sem.h>
39
40 #include "noit_mtev_bridge.h"
41 #include "noit_livestream_listener.h"
42 #include "noit_check.h"
43
44 #include <unistd.h>
45 #include <errno.h>
46
47 static mtev_atomic32_t ls_counter = 0;
48
49 struct log_entry {
50   int len;
51   char *buff;
52   struct log_entry *next;
53 };
54
55 typedef struct {
56   u_int32_t period;
57   mtev_boolean period_read;
58   struct log_entry *lqueue;
59   struct log_entry *lqueue_end;
60   sem_t lqueue_sem;
61   pthread_mutex_t lqueue_lock;
62   int uuid_read;
63   char uuid_str[37];
64   char *feed;
65   uuid_t uuid;
66   noit_check_t *check;
67   int wants_shutdown;
68 } noit_livestream_closure_t;
69
70 noit_livestream_closure_t *
71 noit_livestream_closure_alloc(void) {
72   noit_livestream_closure_t *jcl;
73   jcl = calloc(1, sizeof(*jcl));
74   pthread_mutex_init(&jcl->lqueue_lock, NULL);
75   sem_init(&jcl->lqueue_sem, 0, 0);
76   return jcl;
77 }
78
79 void
80 noit_livestream_closure_free(noit_livestream_closure_t *jcl) {
81   struct log_entry *tofree;
82   while(jcl->lqueue) {
83     tofree = jcl->lqueue;
84     jcl->lqueue = jcl->lqueue->next;
85     free(tofree->buff);
86     free(tofree);
87   }
88   free(jcl);
89 }
90
91 static int
92 noit_livestream_logio_open(mtev_log_stream_t ls) {
93   return 0;
94 }
95 static int
96 noit_livestream_logio_reopen(mtev_log_stream_t ls) {
97   /* no op */
98   return 0;
99 }
100 static int
101 noit_livestream_logio_write(mtev_log_stream_t ls, const struct timeval *whence,
102                             const void *buf, size_t len) {
103   noit_livestream_closure_t *jcl;
104   struct log_entry *le;
105   (void)whence;
106
107   jcl = mtev_log_stream_get_ctx(ls);
108   if(!jcl) return 0;
109
110   if(jcl->wants_shutdown) {
111     /* This has been terminated by the client, _fail here_ */
112     return 0;
113   }
114
115   le = calloc(1, sizeof(*le));
116   le->len = len;
117   le->buff = malloc(len);
118   memcpy(le->buff, buf, len);
119   le->next = NULL;
120   pthread_mutex_lock(&jcl->lqueue_lock);
121   if(!jcl->lqueue_end) jcl->lqueue = le;
122   else jcl->lqueue_end->next = le;
123   jcl->lqueue_end = le;
124   pthread_mutex_unlock(&jcl->lqueue_lock);
125   sem_post(&jcl->lqueue_sem);
126   return len;
127 }
128 static int
129 noit_livestream_logio_close(mtev_log_stream_t ls) {
130   noit_livestream_closure_t *jcl;
131   jcl = mtev_log_stream_get_ctx(ls);
132   if(jcl) noit_livestream_closure_free(jcl);
133   mtev_log_stream_set_ctx(ls, NULL);
134   return 0;
135 }
136 static logops_t noit_livestream_logio_ops = {
137   mtev_false,
138   noit_livestream_logio_open,
139   noit_livestream_logio_reopen,
140   noit_livestream_logio_write,
141   NULL,
142   noit_livestream_logio_close,
143   NULL,
144   NULL
145 };
146
147 void
148 noit_livestream_listener_init() {
149   mtev_register_logops("noit_livestream", &noit_livestream_logio_ops);
150   eventer_name_callback("livestream_transit/1.0", noit_livestream_handler);
151   mtev_control_dispatch_delegate(mtev_control_dispatch,
152                                  NOIT_LIVESTREAM_DATA_FEED,
153                                  noit_livestream_handler);
154 }
155
156 static int
157 __safe_Ewrite(eventer_t e, void *b, int l, int *mask) {
158   int w, sofar = 0;
159   while(l > sofar) {
160     w = e->opset->write(e->fd, (char *)b + sofar, l - sofar, mask, e);
161     if(w <= 0) return w;
162     sofar += w;
163   }
164   return sofar;
165 }
166 #define Ewrite(a,b) __safe_Ewrite(e,a,b,&mask)
167
168 void *
169 noit_livestream_thread_main(void *e_vptr) {
170   int mask;
171   eventer_t e = e_vptr;
172   acceptor_closure_t *ac = e->closure;
173   noit_livestream_closure_t *jcl = ac->service_ctx;
174
175   mtev_memory_init_thread();
176   /* Go into blocking mode */
177   if(eventer_set_fd_blocking(e->fd) == -1) {
178     mtevL(noit_error, "failed setting livestream to blocking: [%d] [%s]\n",
179           errno, strerror(errno));
180     goto alldone;
181   }
182
183   while(1) {
184     u_int32_t netlen;
185     struct log_entry *le = NULL;
186     int rv;
187    
188     sem_wait(&jcl->lqueue_sem);
189     pthread_mutex_lock(&jcl->lqueue_lock);
190     if(jcl->lqueue) {
191       /* If there are items, pop and advance the header pointer */
192       le = jcl->lqueue;
193       jcl->lqueue = jcl->lqueue->next;
194       if(!jcl->lqueue) jcl->lqueue_end = NULL;
195     }
196     pthread_mutex_unlock(&jcl->lqueue_lock);
197
198     if(!le) continue;
199
200     /* Here we actually push the message */
201     netlen = htonl(le->len);
202     if((rv = Ewrite(&netlen, sizeof(netlen))) != sizeof(netlen)) {
203       mtevL(noit_error, "Error writing le header over SSL %d != %d\n",
204             rv, (int)sizeof(netlen));
205       goto alldone;
206     }
207     if((rv = Ewrite(le->buff, le->len)) != le->len) {
208       mtevL(noit_error, "Error writing livestream message over SSL %d != %d\n",
209             rv, le->len);
210       goto alldone;
211     }
212   }
213
214  alldone:
215   e->opset->close(e->fd, &mask, e);
216   jcl->wants_shutdown = 1;
217   acceptor_closure_free(ac);
218   mtev_memory_maintenance();
219   /* Our semaphores are counting semaphores, not locks. */
220   /* coverity[missing_unlock] */
221   return NULL;
222 }
223
224 int
225 noit_livestream_handler(eventer_t e, int mask, void *closure,
226                         struct timeval *now) {
227   eventer_t newe;
228   pthread_t tid;
229   pthread_attr_t tattr;
230   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
231   acceptor_closure_t *ac = closure;
232   noit_livestream_closure_t *jcl = ac->service_ctx;
233
234   if(mask & EVENTER_EXCEPTION || (jcl && jcl->wants_shutdown)) {
235 socket_error:
236     /* Exceptions cause us to simply snip the connection */
237     eventer_remove_fd(e->fd);
238     e->opset->close(e->fd, &newmask, e);
239     if(jcl) noit_livestream_closure_free(jcl);
240     acceptor_closure_free(ac);
241     return 0;
242   }
243
244   if(!ac->service_ctx || !jcl->feed) {
245     int len;
246     if(!ac->service_ctx) ac->service_ctx = noit_livestream_closure_alloc();
247     jcl = ac->service_ctx;
248     /* Setup logger to this channel */
249     mtevL(noit_debug, "livestream initializing on fd %d\n", e->fd);
250     if(!jcl->period_read) {
251       u_int32_t nperiod;
252       len = e->opset->read(e->fd, &nperiod, sizeof(nperiod), &mask, e);
253       if(len == -1 && errno == EAGAIN) return mask | EVENTER_EXCEPTION;
254       if(len != sizeof(nperiod)) goto socket_error;
255       jcl->period = ntohl(nperiod);
256       jcl->period_read = mtev_true;
257       mtevL(noit_debug, "livestream initializing on fd %d [period %d]\n",
258             e->fd, jcl->period);
259     }
260     while(jcl->uuid_read < 36) {
261       len = e->opset->read(e->fd, jcl->uuid_str + jcl->uuid_read, 36 - jcl->uuid_read, &mask, e);
262       if(len == -1 && errno == EAGAIN) return mask | EVENTER_EXCEPTION;
263       if(len == 0) goto socket_error;
264       jcl->uuid_read += len;
265     }
266     jcl->uuid_str[36] = '\0';
267     if(uuid_parse(jcl->uuid_str, jcl->uuid)) {
268       mtevL(noit_error, "bad uuid received in livestream handler '%s'\n", jcl->uuid_str);
269       goto socket_error;
270     }
271     mtevL(noit_debug, "livestream initializing on fd %d [uuid %s]\n",
272           e->fd, jcl->uuid_str);
273
274     jcl->feed = malloc(32);
275     snprintf(jcl->feed, 32, "livestream/%d", mtev_atomic_inc32(&ls_counter));
276     mtev_log_stream_new(jcl->feed, "noit_livestream", jcl->feed,
277                         jcl, NULL);
278
279
280     jcl->check = noit_check_watch(jcl->uuid, jcl->period);
281     if(!jcl->check) {
282       e->opset->close(e->fd, &newmask, e);
283       return 0;
284     }
285     /* This check must be watched from the livestream */
286     noit_check_transient_add_feed(jcl->check, jcl->feed);
287     /* Note the check */
288     noit_check_log_check(jcl->check);
289     /* kick it off, if it isn't running already */
290     if(!NOIT_CHECK_LIVE(jcl->check)) noit_check_activate(jcl->check);
291   }
292
293   eventer_remove_fd(e->fd);
294   newe = eventer_alloc();
295   memcpy(newe, e, sizeof(*e));
296   pthread_attr_init(&tattr);
297   pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
298   if(pthread_create(&tid, &tattr, noit_livestream_thread_main, newe) == 0) {
299     return 0;
300   }
301
302   noit_check_transient_remove_feed(jcl->check, jcl->feed);
303   noit_livestream_closure_free(jcl);
304   /* Undo our dup */
305   eventer_free(newe);
306   /* Creating the thread failed, close it down and deschedule. */
307   e->opset->close(e->fd, &newmask, e);
308   return 0;
309 }
Note: See TracBrowser for help on using the browser.