Various cleanups of abort conditions
[citadel.git] / citadel / event_client.c
1 /*
2  *
3  * Copyright (c) 1998-2009 by the citadel.org team
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "sysdep.h"
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <termios.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <pwd.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <syslog.h>
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42 #include <sys/wait.h>
43 #include <ctype.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <assert.h>
50
51 #include <libcitadel.h>
52 #include "citadel.h"
53 #include "server.h"
54 #include "citserver.h"
55 #include "support.h"
56 #include "config.h"
57 #include "control.h"
58 #include "user_ops.h"
59 #include "database.h"
60 #include "msgbase.h"
61 #include "internet_addressing.h"
62 #include "genstamp.h"
63 #include "domain.h"
64 #include "clientsocket.h"
65 #include "locate_host.h"
66 #include "citadel_dirs.h"
67
68 #include "event_client.h"
69
70 extern citthread_mutex_t EventQueueMutex;
71 extern HashList *InboundEventQueue;
72 extern struct ev_loop *event_base;
73 extern ev_async AddJob;   
74 extern ev_async ExitEventLoop;
75
76 static void
77 IO_abort_shutdown_callback(struct ev_loop *loop, ev_cleanup *watcher, int revents)
78 {
79         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
80
81         AsyncIO *IO = watcher->data;
82         IO->ShutdownAbort(IO);
83 }
84         
85 int QueueEventContext(AsyncIO *IO, IO_CallBack CB)
86 {
87         IOAddHandler *h;
88         int i;
89
90         h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
91         h->IO = IO;
92         h->EvAttch = CB;
93         ev_cleanup_init(&IO->abort_by_shutdown, 
94                         IO_abort_shutdown_callback);
95         IO->abort_by_shutdown.data = IO;
96         ev_cleanup_start(event_base, &IO->abort_by_shutdown);
97
98         citthread_mutex_lock(&EventQueueMutex);
99         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q\n");
100         i = GetCount(InboundEventQueue);
101         Put(InboundEventQueue, IKEY(i), h, NULL);
102         citthread_mutex_unlock(&EventQueueMutex);
103
104         ev_async_send (event_base, &AddJob);
105         CtdlLogPrintf(CTDL_DEBUG, "EVENT Q Done.\n");
106         return 0;
107 }
108
109
110 int ShutDownEventQueue(void)
111 {
112         citthread_mutex_lock(&EventQueueMutex);
113         ev_async_send (EV_DEFAULT_ &ExitEventLoop);
114         citthread_mutex_unlock(&EventQueueMutex);
115         return 0;
116 }
117
118 void FreeAsyncIOContents(AsyncIO *IO)
119 {
120         FreeStrBuf(&IO->IOBuf);
121         FreeStrBuf(&IO->SendBuf.Buf);
122         FreeStrBuf(&IO->RecvBuf.Buf);
123 }
124
125
126 void ShutDownCLient(AsyncIO *IO)
127 {
128         CtdlLogPrintf(CTDL_DEBUG, "EVENT x %d\n", IO->SendBuf.fd);
129
130         ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
131
132         ev_timer_stop(event_base, &IO->conn_fail);
133         ev_io_stop(event_base, &IO->conn_event);
134         ev_idle_stop(event_base, &IO->unwind_stack);
135
136         if (IO->SendBuf.fd != 0)
137         {
138                 ev_io_stop(event_base, &IO->send_event);
139                 ev_io_stop(event_base, &IO->recv_event);
140                 ev_timer_stop (event_base, &IO->rw_timeout);
141                 close(IO->SendBuf.fd);
142                 IO->SendBuf.fd = 0;
143                 IO->RecvBuf.fd = 0;
144         }
145         if (IO->DNSChannel != NULL) {
146                 ares_destroy(IO->DNSChannel);
147                 ev_io_stop(event_base, &IO->dns_recv_event);
148                 ev_io_stop(event_base, &IO->dns_send_event);
149                 IO->DNSChannel = NULL;
150         }
151         assert(IO->Terminate);
152         become_session(IO->CitContext);
153         IO->Terminate(IO);
154         
155 }
156
157
158 eReadState HandleInbound(AsyncIO *IO)
159 {
160         eReadState Finished = eBufferNotEmpty;
161         
162         become_session(IO->CitContext);
163
164         while ((Finished == eBufferNotEmpty) && (IO->NextState == eReadMessage)){
165                 if (IO->RecvBuf.nBlobBytesWanted != 0) { 
166                                 
167                 }
168                 else { /* Reading lines... */
169 //// lex line reply in callback, or do it ourselves. as nnn-blabla means continue reading in SMTP
170                         if (IO->LineReader)
171                                 Finished = IO->LineReader(IO);
172                         else 
173                                 Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
174                                 
175                         switch (Finished) {
176                         case eMustReadMore: /// read new from socket... 
177                                 return Finished;
178                                 break;
179                         case eBufferNotEmpty: /* shouldn't happen... */
180                         case eReadSuccess: /// done for now...
181                                 break;
182                         case eReadFail: /// WHUT?
183                                 ///todo: shut down! 
184                                 break;
185                         }
186                                         
187                 }
188                         
189                 if (Finished != eMustReadMore) {
190                         assert(IO->ReadDone);
191                         ev_io_stop(event_base, &IO->recv_event);
192                         IO->NextState = IO->ReadDone(IO);
193                         Finished = StrBufCheckBuffer(&IO->RecvBuf);
194                 }
195         }
196
197
198         if ((IO->NextState == eSendReply) ||
199             (IO->NextState == eSendMore))
200         {
201                 assert(IO->SendDone);
202                 IO->NextState = IO->SendDone(IO);
203                 ev_io_start(event_base, &IO->send_event);
204         }
205         else if ((IO->NextState == eTerminateConnection) ||
206                  (IO->NextState == eAbort) )
207                 ShutDownCLient(IO);
208         return Finished;
209 }
210
211
212 static void
213 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
214 {
215         int rc;
216         AsyncIO *IO = watcher->data;
217
218         become_session(IO->CitContext);
219         rc = StrBuf_write_one_chunk_callback(watcher->fd, 0/*TODO*/, &IO->SendBuf);
220
221         if (rc == 0)
222         {               
223 #ifdef BIGBAD_IODBG
224                 {
225                         int rv = 0;
226                         char fn [SIZ];
227                         FILE *fd;
228                         const char *pch = ChrPtr(IO->SendBuf.Buf);
229                         const char *pchh = IO->SendBuf.ReadWritePointer;
230                         long nbytes;
231
232                         if (pchh == NULL)
233                                 pchh = pch;
234                         
235                         nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
236                         snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d", "smtpev", IO->SendBuf.fd);
237                 
238                         fd = fopen(fn, "a+");
239                         fprintf(fd, "Read: BufSize: %ld BufContent: [",
240                                 nbytes);
241                         rv = fwrite(pchh, nbytes, 1, fd);
242                         fprintf(fd, "]\n");
243                 
244                         
245                         fclose(fd);
246                 }
247 #endif
248                 ev_io_stop(event_base, &IO->send_event);
249                 switch (IO->NextState) {
250                 case eSendReply:
251                         break;
252                 case eSendMore:
253                         assert(IO->SendDone);
254                         IO->NextState = IO->SendDone(IO);
255
256                         if ((IO->NextState == eTerminateConnection) ||
257                             (IO->NextState == eAbort) )
258                                 ShutDownCLient(IO);
259                         else {
260                                 ev_io_start(event_base, &IO->send_event);
261                         }
262                         break;
263                 case eReadMessage:
264                         if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty) {
265                                 HandleInbound(IO);
266                         }
267                         else {
268                                 ev_io_start(event_base, &IO->recv_event);
269                         }
270
271                         break;
272                 case eTerminateConnection:
273                 case eAbort:
274                         break;
275                 }
276         }
277         else if (rc < 0) {
278                 assert(IO->Timeout);
279                 IO->Timeout(IO);
280         }
281         /* else : must write more. */
282 }
283 static void
284 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
285 {
286         
287         switch(IO->NextState) {
288         case eReadMessage:
289                 ev_io_start(event_base, &IO->recv_event);
290                 break;
291         case eSendReply:
292         case eSendMore:
293                 become_session(IO->CitContext);
294                 IO_send_callback(loop, &IO->send_event, revents);
295                 break;
296         case eTerminateConnection:
297         case eAbort:
298                 /// TODO: WHUT?
299                 break;
300         }
301 }
302
303 static void
304 IO_Timout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
305 {
306         AsyncIO *IO = watcher->data;
307
308         ev_timer_stop (event_base, &IO->rw_timeout);
309         become_session(IO->CitContext);
310
311         assert(IO->Timeout);
312         switch (IO->Timeout(IO))
313         {
314         case eAbort:
315                 ShutDownCLient(IO);
316         default:
317                 break;
318         }
319 }
320 static void
321 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
322 {
323         AsyncIO *IO = watcher->data;
324
325         ev_timer_stop (event_base, &IO->conn_fail);
326         ev_io_stop(loop, &IO->conn_event);
327         become_session(IO->CitContext);
328
329         assert(IO->ConnFail);
330         switch (IO->ConnFail(IO))
331         {
332         case eAbort:
333                 ShutDownCLient(IO);
334         default:
335                 break;
336
337         }
338 }
339 static void
340 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
341 {
342         AsyncIO *IO = watcher->data;
343
344         ev_io_stop(loop, &IO->conn_event);
345         ev_timer_stop (event_base, &IO->conn_fail);
346         set_start_callback(loop, IO, revents);
347 }
348 static void
349 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
350 {
351         ssize_t nbytes;
352         AsyncIO *IO = watcher->data;
353
354         nbytes = StrBuf_read_one_chunk_callback(watcher->fd, 0 /*TODO */, &IO->RecvBuf);
355         if (nbytes > 0) {
356                 HandleInbound(IO);
357         } else if (nbytes == 0) {
358                 assert(IO->Timeout);
359
360                 switch (IO->Timeout(IO))
361                 {
362                 case eAbort:
363                         ShutDownCLient(IO);
364                 default:
365                         break;
366                 }
367                 return;
368         } else if (nbytes == -1) {
369 /// TODO: FD is gone. kick it.        sock_buff_invoke_free(sb, errno);
370                 return;
371         }
372 }
373
374 void
375 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
376 {
377         AsyncIO *IO = watcher->data;
378         CtdlLogPrintf(CTDL_DEBUG, "event: %s\n", __FUNCTION__);
379         become_session(IO->CitContext);
380
381         IO->DNSQuery->PostDNS(IO);
382 }
383
384 eNextState event_connect_socket(AsyncIO *IO, double conn_timeout, double first_rw_timeout)
385 {
386         int fdflags; 
387         int rc = -1;
388
389         IO->SendBuf.fd = IO->RecvBuf.fd = 
390                 socket(
391                         (IO->ConnectMe->IPv6)?PF_INET6:PF_INET, 
392                         SOCK_STREAM, 
393                         IPPROTO_TCP);
394
395         if (IO->SendBuf.fd < 0) {
396                 CtdlLogPrintf(CTDL_ERR, "EVENT: socket() failed: %s\n", strerror(errno));
397                 StrBufPrintf(IO->ErrMsg, "Failed to create socket: %s", strerror(errno));
398 //              freeaddrinfo(res);
399                 return eAbort;
400         }
401         fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
402         if (fdflags < 0) {
403                 CtdlLogPrintf(CTDL_DEBUG,
404                               "EVENT: unable to get socket flags! %s \n",
405                               strerror(errno));
406                 StrBufPrintf(IO->ErrMsg, "Failed to get socket flags: %s", strerror(errno));
407                 return eAbort;
408         }
409         fdflags = fdflags | O_NONBLOCK;
410         if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
411                 CtdlLogPrintf(CTDL_DEBUG,
412                               "EVENT: unable to set socket nonblocking flags! %s \n",
413                               strerror(errno));
414                 StrBufPrintf(IO->ErrMsg, "Failed to set socket flags: %s", strerror(errno));
415                 close(IO->SendBuf.fd);
416                 IO->SendBuf.fd = IO->RecvBuf.fd = -1;
417                 return eAbort;
418         }
419 /* TODO: maye we could use offsetof() to calc the position of data... 
420  * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
421  */
422         ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
423         IO->recv_event.data = IO;
424         ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
425         IO->send_event.data = IO;
426
427         ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
428         IO->conn_fail.data = IO;
429         ev_timer_init(&IO->rw_timeout, IO_Timout_callback, first_rw_timeout, 0);
430         IO->rw_timeout.data = IO;
431
432         if (IO->ConnectMe->IPv6)
433                 rc = connect(IO->SendBuf.fd, &IO->ConnectMe->Addr, sizeof(struct sockaddr_in6));
434         else
435                 rc = connect(IO->SendBuf.fd, (struct sockaddr_in *)&IO->ConnectMe->Addr, sizeof(struct sockaddr_in));
436
437         if (rc >= 0){
438 ////            freeaddrinfo(res);
439                 set_start_callback(event_base, IO, 0);
440                 ev_timer_start(event_base, &IO->rw_timeout);
441                 return IO->NextState;
442         }
443         else if (errno == EINPROGRESS) {
444
445                 ev_io_init(&IO->conn_event, IO_connestd_callback, IO->SendBuf.fd, EV_READ|EV_WRITE);
446                 IO->conn_event.data = IO;
447
448                 ev_io_start(event_base, &IO->conn_event);
449                 ev_timer_start(event_base, &IO->conn_fail);
450                 return IO->NextState;
451         }
452         else {
453                 CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno));
454                 StrBufPrintf(IO->ErrMsg, "Failed to connect: %s", strerror(errno));
455                 assert(IO->ConnFail);
456                 IO->ConnFail(IO);
457                 return eAbort;
458         }
459         return IO->NextState;
460 }
461
462 void SetNextTimeout(AsyncIO *IO, double timeout)
463 {
464         IO->rw_timeout.repeat = timeout;
465         ev_timer_again (event_base,  &IO->rw_timeout);
466 }
467
468 eNextState InitEventIO(AsyncIO *IO, 
469                        void *pData, 
470                        double conn_timeout, 
471                        double first_rw_timeout,
472                        int ReadFirst)
473 {
474         IO->Data = pData;
475         become_session(IO->CitContext);
476         
477         if (ReadFirst) {
478                 IO->NextState = eReadMessage;
479         }
480         else {
481                 IO->NextState = eSendReply;
482         }
483         return event_connect_socket(IO, conn_timeout, first_rw_timeout);
484 }