root/src/noit_livestream_listener.c

Revision 93e294a62104f2f8828b291a0005854a2512520e, 9.4 kB (checked in by Phil Maddox <philip.maddox@circonus.com>, 6 months ago)

Fix Memory Leak In Livestream Bundles

We weren't freeing memory after writing it in the livestream listener.
Free this data - the buffers can grow very large and cause things to get
out of hand otherwise.

  • 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   struct log_entry *le = NULL;
175
176   mtev_memory_init_thread();
177   /* Go into blocking mode */
178   if(eventer_set_fd_blocking(e->fd) == -1) {
179     mtevL(noit_error, "failed setting livestream to blocking: [%d] [%s]\n",
180           errno, strerror(errno));
181     goto alldone;
182   }
183
184   while(1) {
185     u_int32_t netlen;
186     int rv;
187     le = NULL;
188    
189     sem_wait(&jcl->lqueue_sem);
190     pthread_mutex_lock(&jcl->lqueue_lock);
191     if(jcl->lqueue) {
192       /* If there are items, pop and advance the header pointer */
193       le = jcl->lqueue;
194       jcl->lqueue = jcl->lqueue->next;
195       if(!jcl->lqueue) jcl->lqueue_end = NULL;
196     }
197     pthread_mutex_unlock(&jcl->lqueue_lock);
198
199     if(!le) continue;
200
201     /* Here we actually push the message */
202     netlen = htonl(le->len);
203     if((rv = Ewrite(&netlen, sizeof(netlen))) != sizeof(netlen)) {
204       mtevL(noit_error, "Error writing le header over SSL %d != %d\n",
205             rv, (int)sizeof(netlen));
206       goto alldone;
207     }
208     if((rv = Ewrite(le->buff, le->len)) != le->len) {
209       mtevL(noit_error, "Error writing livestream message over SSL %d != %d\n",
210             rv, le->len);
211       goto alldone;
212     }
213     if (le->buff) free(le->buff);
214     free(le);
215   }
216
217  alldone:
218   if (le) {
219     if (le->buff) free(le->buff);
220     free(le);
221   }
222   e->opset->close(e->fd, &mask, e);
223   jcl->wants_shutdown = 1;
224   acceptor_closure_free(ac);
225   mtev_memory_maintenance();
226   /* Our semaphores are counting semaphores, not locks. */
227   /* coverity[missing_unlock] */
228   return NULL;
229 }
230
231 int
232 noit_livestream_handler(eventer_t e, int mask, void *closure,
233                         struct timeval *now) {
234   eventer_t newe;
235   pthread_t tid;
236   pthread_attr_t tattr;
237   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
238   acceptor_closure_t *ac = closure;
239   noit_livestream_closure_t *jcl = ac->service_ctx;
240
241   if(mask & EVENTER_EXCEPTION || (jcl && jcl->wants_shutdown)) {
242 socket_error:
243     /* Exceptions cause us to simply snip the connection */
244     eventer_remove_fd(e->fd);
245     e->opset->close(e->fd, &newmask, e);
246     if(jcl) noit_livestream_closure_free(jcl);
247     acceptor_closure_free(ac);
248     return 0;
249   }
250
251   if(!ac->service_ctx || !jcl->feed) {
252     int len;
253     if(!ac->service_ctx) ac->service_ctx = noit_livestream_closure_alloc();
254     jcl = ac->service_ctx;
255     /* Setup logger to this channel */
256     mtevL(noit_debug, "livestream initializing on fd %d\n", e->fd);
257     if(!jcl->period_read) {
258       u_int32_t nperiod;
259       len = e->opset->read(e->fd, &nperiod, sizeof(nperiod), &mask, e);
260       if(len == -1 && errno == EAGAIN) return mask | EVENTER_EXCEPTION;
261       if(len != sizeof(nperiod)) goto socket_error;
262       jcl->period = ntohl(nperiod);
263       jcl->period_read = mtev_true;
264       mtevL(noit_debug, "livestream initializing on fd %d [period %d]\n",
265             e->fd, jcl->period);
266     }
267     while(jcl->uuid_read < 36) {
268       len = e->opset->read(e->fd, jcl->uuid_str + jcl->uuid_read, 36 - jcl->uuid_read, &mask, e);
269       if(len == -1 && errno == EAGAIN) return mask | EVENTER_EXCEPTION;
270       if(len == 0) goto socket_error;
271       jcl->uuid_read += len;
272     }
273     jcl->uuid_str[36] = '\0';
274     if(uuid_parse(jcl->uuid_str, jcl->uuid)) {
275       mtevL(noit_error, "bad uuid received in livestream handler '%s'\n", jcl->uuid_str);
276       goto socket_error;
277     }
278     mtevL(noit_debug, "livestream initializing on fd %d [uuid %s]\n",
279           e->fd, jcl->uuid_str);
280
281     jcl->feed = malloc(32);
282     snprintf(jcl->feed, 32, "livestream/%d", mtev_atomic_inc32(&ls_counter));
283     mtev_log_stream_new(jcl->feed, "noit_livestream", jcl->feed,
284                         jcl, NULL);
285
286
287     jcl->check = noit_check_watch(jcl->uuid, jcl->period);
288     if(!jcl->check) {
289       e->opset->close(e->fd, &newmask, e);
290       return 0;
291     }
292     /* This check must be watched from the livestream */
293     noit_check_transient_add_feed(jcl->check, jcl->feed);
294     /* Note the check */
295     noit_check_log_check(jcl->check);
296     /* kick it off, if it isn't running already */
297     if(!NOIT_CHECK_LIVE(jcl->check)) noit_check_activate(jcl->check);
298   }
299
300   eventer_remove_fd(e->fd);
301   newe = eventer_alloc();
302   memcpy(newe, e, sizeof(*e));
303   pthread_attr_init(&tattr);
304   pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
305   if(pthread_create(&tid, &tattr, noit_livestream_thread_main, newe) == 0) {
306     return 0;
307   }
308
309   noit_check_transient_remove_feed(jcl->check, jcl->feed);
310   noit_livestream_closure_free(jcl);
311   /* Undo our dup */
312   eventer_free(newe);
313   /* Creating the thread failed, close it down and deschedule. */
314   e->opset->close(e->fd, &newmask, e);
315   return 0;
316 }
Note: See TracBrowser for help on using the browser.