3 * Copyright (c) 1998-2012 by the citadel.org team
5 * This program is open source 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.
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.
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
29 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
54 #include <libcitadel.h>
57 #include "citserver.h"
64 #include "internet_addressing.h"
67 #include "clientsocket.h"
68 #include "locate_host.h"
69 #include "citadel_dirs.h"
71 #include "event_client.h"
72 #include "ctdl_module.h"
74 static void IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents);
75 static void IO_abort_shutdown_callback(struct ev_loop *loop,
80 /*------------------------------------------------------------------------------
82 *----------------------------------------------------------------------------*/
83 extern int evdb_count;
84 extern pthread_mutex_t DBEventQueueMutex;
85 extern pthread_mutex_t DBEventExitQueueMutex;
86 extern HashList *DBInboundEventQueue;
87 extern struct ev_loop *event_db;
88 extern ev_async DBAddJob;
89 extern ev_async DBExitEventLoop;
91 eNextState QueueDBOperation(AsyncIO *IO, IO_CallBack CB)
96 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
99 ev_cleanup_init(&IO->db_abort_by_shutdown,
100 IO_abort_shutdown_callback);
101 IO->db_abort_by_shutdown.data = IO;
103 pthread_mutex_lock(&DBEventQueueMutex);
104 if (DBInboundEventQueue == NULL)
106 /* shutting down... */
108 EVM_syslog(LOG_DEBUG, "DBEVENT Q exiting.\n");
109 pthread_mutex_unlock(&DBEventQueueMutex);
112 EVM_syslog(LOG_DEBUG, "DBEVENT Q\n");
114 Put(DBInboundEventQueue, IKEY(i), h, NULL);
115 pthread_mutex_unlock(&DBEventQueueMutex);
117 pthread_mutex_lock(&DBEventExitQueueMutex);
118 if (event_db == NULL)
120 pthread_mutex_unlock(&DBEventExitQueueMutex);
123 ev_async_send (event_db, &DBAddJob);
124 pthread_mutex_unlock(&DBEventExitQueueMutex);
126 EVM_syslog(LOG_DEBUG, "DBEVENT Q Done.\n");
130 void StopDBWatchers(AsyncIO *IO)
132 ev_cleanup_stop(event_db, &IO->db_abort_by_shutdown);
133 ev_idle_stop(event_db, &IO->db_unwind_stack);
136 void ShutDownDBCLient(AsyncIO *IO)
138 CitContext *Ctx =IO->CitContext;
141 EVM_syslog(LOG_DEBUG, "DBEVENT Terminating.\n");
144 assert(IO->DBTerminate);
149 DB_PerformNext(struct ev_loop *loop, ev_idle *watcher, int revents)
151 AsyncIO *IO = watcher->data;
152 IO->Now = ev_now(event_db);
153 EV_syslog(LOG_DEBUG, "%s()", __FUNCTION__);
154 become_session(IO->CitContext);
156 ev_idle_stop(event_db, &IO->db_unwind_stack);
158 assert(IO->NextDBOperation);
159 switch (IO->NextDBOperation(IO))
173 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
175 case eTerminateConnection:
177 ev_idle_stop(event_db, &IO->db_unwind_stack);
178 ev_cleanup_stop(loop, &IO->db_abort_by_shutdown);
179 ShutDownDBCLient(IO);
183 eNextState NextDBOperation(AsyncIO *IO, IO_CallBack CB)
185 IO->NextDBOperation = CB;
186 ev_idle_init(&IO->db_unwind_stack,
188 IO->db_unwind_stack.data = IO;
189 ev_idle_start(event_db, &IO->db_unwind_stack);
193 /*------------------------------------------------------------------------------
195 *----------------------------------------------------------------------------*/
196 extern int evbase_count;
197 extern pthread_mutex_t EventQueueMutex;
198 extern pthread_mutex_t EventExitQueueMutex;
199 extern HashList *InboundEventQueue;
200 extern struct ev_loop *event_base;
201 extern ev_async AddJob;
202 extern ev_async ExitEventLoop;
204 static void IO_abort_shutdown_callback(struct ev_loop *loop,
208 AsyncIO *IO = watcher->data;
209 EV_syslog(LOG_DEBUG, "EVENT Q: %s\n", __FUNCTION__);
210 IO->Now = ev_now(event_base);
211 assert(IO->ShutdownAbort);
212 IO->ShutdownAbort(IO);
216 eNextState QueueEventContext(AsyncIO *IO, IO_CallBack CB)
221 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
224 ev_cleanup_init(&IO->abort_by_shutdown,
225 IO_abort_shutdown_callback);
226 IO->abort_by_shutdown.data = IO;
228 pthread_mutex_lock(&EventQueueMutex);
229 if (InboundEventQueue == NULL)
232 /* shutting down... */
233 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
234 pthread_mutex_unlock(&EventQueueMutex);
237 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
239 Put(InboundEventQueue, IKEY(i), h, NULL);
240 pthread_mutex_unlock(&EventQueueMutex);
242 pthread_mutex_lock(&EventExitQueueMutex);
243 if (event_base == NULL) {
244 pthread_mutex_unlock(&EventExitQueueMutex);
247 ev_async_send (event_base, &AddJob);
248 pthread_mutex_unlock(&EventExitQueueMutex);
249 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
253 extern eNextState evcurl_handle_start(AsyncIO *IO);
255 eNextState QueueCurlContext(AsyncIO *IO)
260 h = (IOAddHandler*)malloc(sizeof(IOAddHandler));
262 h->EvAttch = evcurl_handle_start;
264 pthread_mutex_lock(&EventQueueMutex);
265 if (InboundEventQueue == NULL)
267 /* shutting down... */
269 EVM_syslog(LOG_DEBUG, "EVENT Q exiting.\n");
270 pthread_mutex_unlock(&EventQueueMutex);
274 EVM_syslog(LOG_DEBUG, "EVENT Q\n");
276 Put(InboundEventQueue, IKEY(i), h, NULL);
277 pthread_mutex_unlock(&EventQueueMutex);
279 pthread_mutex_lock(&EventExitQueueMutex);
280 if (event_base == NULL) {
281 pthread_mutex_unlock(&EventExitQueueMutex);
284 ev_async_send (event_base, &AddJob);
285 pthread_mutex_unlock(&EventExitQueueMutex);
287 EVM_syslog(LOG_DEBUG, "EVENT Q Done.\n");
291 void DestructCAres(AsyncIO *IO);
292 void FreeAsyncIOContents(AsyncIO *IO)
294 CitContext *Ctx = IO->CitContext;
296 FreeStrBuf(&IO->IOBuf);
297 FreeStrBuf(&IO->SendBuf.Buf);
298 FreeStrBuf(&IO->RecvBuf.Buf);
302 FreeURL(&IO->ConnectMe);
303 FreeStrBuf(&IO->HttpReq.ReplyData);
306 Ctx->state = CON_IDLE;
312 void StopClientWatchers(AsyncIO *IO)
314 ev_timer_stop (event_base, &IO->rw_timeout);
315 ev_timer_stop(event_base, &IO->conn_fail);
316 ev_idle_stop(event_base, &IO->unwind_stack);
317 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
319 ev_io_stop(event_base, &IO->conn_event);
320 ev_io_stop(event_base, &IO->send_event);
321 ev_io_stop(event_base, &IO->recv_event);
323 if (IO->SendBuf.fd != 0) {
324 close(IO->SendBuf.fd);
330 void StopCurlWatchers(AsyncIO *IO)
332 ev_timer_stop (event_base, &IO->rw_timeout);
333 ev_timer_stop(event_base, &IO->conn_fail);
334 ev_idle_stop(event_base, &IO->unwind_stack);
335 ev_cleanup_stop(event_base, &IO->abort_by_shutdown);
337 ev_io_stop(event_base, &IO->conn_event);
338 ev_io_stop(event_base, &IO->send_event);
339 ev_io_stop(event_base, &IO->recv_event);
341 if (IO->SendBuf.fd != 0) {
342 close(IO->SendBuf.fd);
348 void ShutDownCLient(AsyncIO *IO)
350 CitContext *Ctx =IO->CitContext;
353 EVM_syslog(LOG_DEBUG, "EVENT Terminating \n");
355 StopClientWatchers(IO);
357 if (IO->DNS.Channel != NULL) {
358 ares_destroy(IO->DNS.Channel);
359 EV_DNS_LOG_STOP(DNS.recv_event);
360 EV_DNS_LOG_STOP(DNS.send_event);
361 ev_io_stop(event_base, &IO->DNS.recv_event);
362 ev_io_stop(event_base, &IO->DNS.send_event);
363 IO->DNS.Channel = NULL;
365 assert(IO->Terminate);
369 void PostInbound(AsyncIO *IO)
371 switch (IO->NextState) {
373 ev_io_start(event_base, &IO->send_event);
377 assert(IO->SendDone);
378 IO->NextState = IO->SendDone(IO);
379 ev_io_start(event_base, &IO->send_event);
384 ev_io_start(event_base, &IO->recv_event);
386 case eTerminateConnection:
400 eReadState HandleInbound(AsyncIO *IO)
402 const char *Err = NULL;
403 eReadState Finished = eBufferNotEmpty;
405 become_session(IO->CitContext);
407 while ((Finished == eBufferNotEmpty) &&
408 ((IO->NextState == eReadMessage)||
409 (IO->NextState == eReadMore)||
410 (IO->NextState == eReadFile)||
411 (IO->NextState == eReadPayload)))
414 * lex line reply in callback,
415 * or do it ourselves.
416 * i.e. as nnn-blabla means continue reading in SMTP
418 if ((IO->NextState == eReadFile) &&
419 (Finished == eBufferNotEmpty))
421 Finished = WriteIOBAlreadyRead(&IO->IOB, &Err);
422 if (Finished == eReadSuccess)
424 IO->NextState = eSendReply;
427 else if (IO->LineReader)
428 Finished = IO->LineReader(IO);
430 Finished = StrBufChunkSipLine(IO->IOBuf,
434 case eMustReadMore: /// read new from socket...
436 case eBufferNotEmpty: /* shouldn't happen... */
437 case eReadSuccess: /// done for now...
439 case eReadFail: /// WHUT?
444 if (Finished != eMustReadMore) {
445 assert(IO->ReadDone);
446 ev_io_stop(event_base, &IO->recv_event);
447 IO->NextState = IO->ReadDone(IO);
448 Finished = StrBufCheckBuffer(&IO->RecvBuf);
459 IO_send_callback(struct ev_loop *loop, ev_io *watcher, int revents)
462 AsyncIO *IO = watcher->data;
463 const char *errmsg = NULL;
465 IO->Now = ev_now(event_base);
466 become_session(IO->CitContext);
472 const char *pch = ChrPtr(IO->SendBuf.Buf);
473 const char *pchh = IO->SendBuf.ReadWritePointer;
479 nbytes = StrLength(IO->SendBuf.Buf) - (pchh - pch);
480 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
481 ((CitContext*)(IO->CitContext))->ServiceName,
484 fd = fopen(fn, "a+");
485 fprintf(fd, "Send: BufSize: %ld BufContent: [",
487 rv = fwrite(pchh, nbytes, 1, fd);
488 if (!rv) printf("failed to write debug to %s!\n", fn);
491 switch (IO->NextState) {
493 rc = FileSendChunked(&IO->IOB, &errmsg);
495 StrBufPlain(IO->ErrMsg, errmsg, -1);
498 rc = StrBuf_write_one_chunk_callback(watcher->fd,
504 fprintf(fd, "Sent: BufSize: %d bytes.\n", rc);
510 ev_io_stop(event_base, &IO->send_event);
511 switch (IO->NextState) {
513 assert(IO->SendDone);
514 IO->NextState = IO->SendDone(IO);
516 if ((IO->NextState == eTerminateConnection) ||
517 (IO->NextState == eAbort) )
520 ev_io_start(event_base, &IO->send_event);
524 if (IO->IOB.ChunkSendRemain > 0) {
525 ev_io_start(event_base, &IO->recv_event);
526 SetNextTimeout(IO, 100.0);
529 assert(IO->ReadDone);
530 IO->NextState = IO->ReadDone(IO);
531 switch(IO->NextState) {
540 ev_io_start(event_base,
548 case eTerminateConnection:
555 if (StrBufCheckBuffer(&IO->SendBuf) != eReadSuccess)
557 IO->NextState = eReadMore;
562 if (StrBufCheckBuffer(&IO->RecvBuf) == eBufferNotEmpty)
567 ev_io_start(event_base, &IO->recv_event);
573 * we now live in another queue,
574 * so we have to unregister.
576 ev_cleanup_stop(loop, &IO->abort_by_shutdown);
581 case eTerminateConnection:
587 IO_Timeout_callback(loop, &IO->rw_timeout, revents);
589 /* else : must write more. */
592 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
594 ev_timer_stop(event_base, &IO->conn_fail);
595 ev_timer_start(event_base, &IO->rw_timeout);
597 switch(IO->NextState) {
601 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
602 ev_io_start(event_base, &IO->recv_event);
608 become_session(IO->CitContext);
609 IO_send_callback(loop, &IO->send_event, revents);
615 case eTerminateConnection:
623 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
625 AsyncIO *IO = watcher->data;
627 IO->Now = ev_now(event_base);
628 ev_timer_stop (event_base, &IO->rw_timeout);
629 become_session(IO->CitContext);
631 if (IO->SendBuf.fd != 0)
633 ev_io_stop(event_base, &IO->send_event);
634 ev_io_stop(event_base, &IO->recv_event);
635 ev_timer_stop (event_base, &IO->rw_timeout);
636 close(IO->SendBuf.fd);
637 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
641 switch (IO->Timeout(IO))
651 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
653 AsyncIO *IO = watcher->data;
655 IO->Now = ev_now(event_base);
656 ev_timer_stop (event_base, &IO->conn_fail);
658 if (IO->SendBuf.fd != 0)
660 ev_io_stop(loop, &IO->conn_event);
661 ev_io_stop(event_base, &IO->send_event);
662 ev_io_stop(event_base, &IO->recv_event);
663 ev_timer_stop (event_base, &IO->rw_timeout);
664 close(IO->SendBuf.fd);
665 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
667 become_session(IO->CitContext);
669 assert(IO->ConnFail);
670 switch (IO->ConnFail(IO))
681 IO_connfailimmediate_callback(struct ev_loop *loop,
685 AsyncIO *IO = watcher->data;
687 IO->Now = ev_now(event_base);
688 ev_idle_stop (event_base, &IO->conn_fail_immediate);
690 if (IO->SendBuf.fd != 0)
692 close(IO->SendBuf.fd);
693 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
695 become_session(IO->CitContext);
697 assert(IO->ConnFail);
698 switch (IO->ConnFail(IO))
709 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
711 AsyncIO *IO = watcher->data;
713 socklen_t lon = sizeof(so_err);
716 IO->Now = ev_now(event_base);
717 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
719 ev_io_stop(loop, &IO->conn_event);
720 ev_timer_stop(event_base, &IO->conn_fail);
722 err = getsockopt(IO->SendBuf.fd,
728 if ((err == 0) && (so_err != 0))
730 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
733 IO_connfail_callback(loop, &IO->conn_fail, revents);
738 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
739 set_start_callback(loop, IO, revents);
744 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
748 AsyncIO *IO = watcher->data;
750 IO->Now = ev_now(event_base);
751 switch (IO->NextState) {
753 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
755 StrBufPlain(IO->ErrMsg, errmsg, -1);
758 if (IO->IOB.ChunkSendRemain == 0)
760 IO->NextState = eSendReply;
761 assert(IO->ReadDone);
762 ev_io_stop(event_base, &IO->recv_event);
771 nbytes = StrBuf_read_one_chunk_callback(watcher->fd,
783 const char *pch = ChrPtr(IO->RecvBuf.Buf);
784 const char *pchh = IO->RecvBuf.ReadWritePointer;
789 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
790 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
791 ((CitContext*)(IO->CitContext))->ServiceName,
794 fd = fopen(fn, "a+");
795 fprintf(fd, "Read: BufSize: %ld BufContent: [",
797 rv = fwrite(pchh, nbytes, 1, fd);
798 if (!rv) printf("failed to write debug to %s!\n", fn);
805 } else if (nbytes == 0) {
806 IO_Timeout_callback(loop, &IO->rw_timeout, revents);
808 } else if (nbytes == -1) {
809 // FD is gone. kick it.
810 StopClientWatchers(IO);
812 "EVENT: Socket Invalid! %s \n",
820 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
822 AsyncIO *IO = watcher->data;
823 IO->Now = ev_now(event_base);
824 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
825 become_session(IO->CitContext);
826 assert(IO->DNS.Query->PostDNS);
827 switch (IO->DNS.Query->PostDNS(IO))
830 assert(IO->DNS.Fail);
831 switch (IO->DNS.Fail(IO)) {
833 //// StopClientWatchers(IO);
844 eNextState EvConnectSock(AsyncIO *IO,
846 double first_rw_timeout,
849 struct sockaddr_in egress_sin;
853 become_session(IO->CitContext);
856 IO->NextState = eReadMessage;
859 IO->NextState = eSendReply;
862 IO->SendBuf.fd = IO->RecvBuf.fd =
864 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
868 if (IO->SendBuf.fd < 0) {
870 "EVENT: socket() failed: %s\n",
873 StrBufPrintf(IO->ErrMsg,
874 "Failed to create socket: %s",
876 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
879 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
882 "EVENT: unable to get socket flags! %s \n",
884 StrBufPrintf(IO->ErrMsg,
885 "Failed to get socket flags: %s",
887 close(IO->SendBuf.fd);
888 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
891 fdflags = fdflags | O_NONBLOCK;
892 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
895 "EVENT: unable to set socket nonblocking flags! %s \n",
897 StrBufPrintf(IO->ErrMsg,
898 "Failed to set socket flags: %s",
900 close(IO->SendBuf.fd);
901 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
904 /* TODO: maye we could use offsetof() to calc the position of data...
905 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
907 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
908 IO->recv_event.data = IO;
909 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
910 IO->send_event.data = IO;
912 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
913 IO->conn_fail.data = IO;
914 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
915 IO->rw_timeout.data = IO;
920 /* for debugging you may bypass it like this:
921 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
922 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
923 * inet_addr("127.0.0.1");
925 if (IO->ConnectMe->IPv6) {
926 rc = connect(IO->SendBuf.fd,
927 &IO->ConnectMe->Addr,
928 sizeof(struct sockaddr_in6));
931 /* If citserver is bound to a specific IP address on the host, make
932 * sure we use that address for outbound connections.
935 memset(&egress_sin, 0, sizeof(egress_sin));
936 egress_sin.sin_family = AF_INET;
937 if (!IsEmptyStr(config.c_ip_addr)) {
938 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
939 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
940 egress_sin.sin_addr.s_addr = INADDR_ANY;
943 /* If this bind fails, no problem; we can still use INADDR_ANY */
944 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
946 rc = connect(IO->SendBuf.fd,
947 (struct sockaddr_in *)&IO->ConnectMe->Addr,
948 sizeof(struct sockaddr_in));
952 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
953 set_start_callback(event_base, IO, 0);
954 return IO->NextState;
956 else if (errno == EINPROGRESS) {
957 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
959 ev_io_init(&IO->conn_event,
960 IO_connestd_callback,
964 IO->conn_event.data = IO;
966 ev_io_start(event_base, &IO->conn_event);
967 ev_timer_start(event_base, &IO->conn_fail);
968 return IO->NextState;
971 ev_idle_init(&IO->conn_fail_immediate,
972 IO_connfailimmediate_callback);
973 IO->conn_fail_immediate.data = IO;
974 ev_idle_start(event_base, &IO->conn_fail_immediate);
976 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
977 StrBufPrintf(IO->ErrMsg,
978 "Failed to connect: %s",
980 return IO->NextState;
982 return IO->NextState;
985 void SetNextTimeout(AsyncIO *IO, double timeout)
987 IO->rw_timeout.repeat = timeout;
988 ev_timer_again (event_base, &IO->rw_timeout);
992 eNextState ReAttachIO(AsyncIO *IO,
997 become_session(IO->CitContext);
998 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1000 IO->NextState = eReadMessage;
1003 IO->NextState = eSendReply;
1005 set_start_callback(event_base, IO, 0);
1007 return IO->NextState;
1010 void InitIOStruct(AsyncIO *IO,
1012 eNextState NextState,
1013 IO_LineReaderCallback LineReader,
1014 IO_CallBack DNS_Fail,
1015 IO_CallBack SendDone,
1016 IO_CallBack ReadDone,
1017 IO_CallBack Terminate,
1018 IO_CallBack DBTerminate,
1019 IO_CallBack ConnFail,
1020 IO_CallBack Timeout,
1021 IO_CallBack ShutdownAbort)
1025 IO->CitContext = CloneContext(CC);
1026 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1028 IO->NextState = NextState;
1030 IO->SendDone = SendDone;
1031 IO->ReadDone = ReadDone;
1032 IO->Terminate = Terminate;
1033 IO->DBTerminate = DBTerminate;
1034 IO->LineReader = LineReader;
1035 IO->ConnFail = ConnFail;
1036 IO->Timeout = Timeout;
1037 IO->ShutdownAbort = ShutdownAbort;
1039 IO->DNS.Fail = DNS_Fail;
1041 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1042 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1043 IO->IOBuf = NewStrBuf();
1044 EV_syslog(LOG_DEBUG,
1045 "EVENT: Session lives at %p IO at %p \n",
1050 extern int evcurl_init(AsyncIO *IO);
1052 int InitcURLIOStruct(AsyncIO *IO,
1055 IO_CallBack SendDone,
1056 IO_CallBack Terminate,
1057 IO_CallBack DBTerminate,
1058 IO_CallBack ShutdownAbort)
1062 IO->CitContext = CloneContext(CC);
1063 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1065 IO->SendDone = SendDone;
1066 IO->Terminate = Terminate;
1067 IO->DBTerminate = DBTerminate;
1068 IO->ShutdownAbort = ShutdownAbort;
1070 strcpy(IO->HttpReq.errdesc, Desc);
1073 return evcurl_init(IO);
1077 extern int DebugEventLoopBacktrace;
1078 void EV_backtrace(AsyncIO *IO)
1080 #ifdef HAVE_BACKTRACE
1081 void *stack_frames[50];
1085 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1087 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1088 strings = backtrace_symbols(stack_frames, size);
1089 for (i = 0; i < size; i++) {
1090 if (strings != NULL) {
1091 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1094 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1102 ev_tstamp ctdl_ev_now (void)
1104 return ev_now(event_base);