root/src/noit_livestream_listener.c

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

make the logging API private and add locking for jlog so that in-flight reopens are safe, refs #215

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