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, int CloseFD)
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 (CloseFD && (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, 1);
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(IO->SendBuf.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 if (errno != EAGAIN) {
588 StopClientWatchers(IO, 1);
590 "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
591 errno, strerror(errno), IO->SendBuf.fd);
592 StrBufPrintf(IO->ErrMsg,
593 "Socket Invalid! [%s]",
595 SetNextTimeout(IO, 0.0);
598 /* else : must write more. */
601 set_start_callback(struct ev_loop *loop, AsyncIO *IO, int revents)
603 ev_timer_stop(event_base, &IO->conn_fail);
604 ev_timer_start(event_base, &IO->rw_timeout);
606 switch(IO->NextState) {
610 StrBufAppendBufPlain(IO->ErrMsg, HKEY("[while waiting for greeting]"), 0);
611 ev_io_start(event_base, &IO->recv_event);
617 become_session(IO->CitContext);
618 IO_send_callback(loop, &IO->send_event, revents);
624 case eTerminateConnection:
632 IO_Timeout_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
634 AsyncIO *IO = watcher->data;
636 IO->Now = ev_now(event_base);
637 ev_timer_stop (event_base, &IO->rw_timeout);
638 become_session(IO->CitContext);
640 if (IO->SendBuf.fd != 0)
642 ev_io_stop(event_base, &IO->send_event);
643 ev_io_stop(event_base, &IO->recv_event);
644 ev_timer_stop (event_base, &IO->rw_timeout);
645 close(IO->SendBuf.fd);
646 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
650 switch (IO->Timeout(IO))
660 IO_connfail_callback(struct ev_loop *loop, ev_timer *watcher, int revents)
662 AsyncIO *IO = watcher->data;
664 IO->Now = ev_now(event_base);
665 ev_timer_stop (event_base, &IO->conn_fail);
667 if (IO->SendBuf.fd != 0)
669 ev_io_stop(loop, &IO->conn_event);
670 ev_io_stop(event_base, &IO->send_event);
671 ev_io_stop(event_base, &IO->recv_event);
672 ev_timer_stop (event_base, &IO->rw_timeout);
673 close(IO->SendBuf.fd);
674 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
676 become_session(IO->CitContext);
678 assert(IO->ConnFail);
679 switch (IO->ConnFail(IO))
690 IO_connfailimmediate_callback(struct ev_loop *loop,
694 AsyncIO *IO = watcher->data;
696 IO->Now = ev_now(event_base);
697 ev_idle_stop (event_base, &IO->conn_fail_immediate);
699 if (IO->SendBuf.fd != 0)
701 close(IO->SendBuf.fd);
702 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
704 become_session(IO->CitContext);
706 assert(IO->ConnFail);
707 switch (IO->ConnFail(IO))
718 IO_connestd_callback(struct ev_loop *loop, ev_io *watcher, int revents)
720 AsyncIO *IO = watcher->data;
722 socklen_t lon = sizeof(so_err);
725 IO->Now = ev_now(event_base);
726 EVM_syslog(LOG_DEBUG, "connect() succeeded.\n");
728 ev_io_stop(loop, &IO->conn_event);
729 ev_timer_stop(event_base, &IO->conn_fail);
731 err = getsockopt(IO->SendBuf.fd,
737 if ((err == 0) && (so_err != 0))
739 EV_syslog(LOG_DEBUG, "connect() failed [%d][%s]\n",
742 IO_connfail_callback(loop, &IO->conn_fail, revents);
747 EVM_syslog(LOG_DEBUG, "connect() succeeded\n");
748 set_start_callback(loop, IO, revents);
753 IO_recv_callback(struct ev_loop *loop, ev_io *watcher, int revents)
757 AsyncIO *IO = watcher->data;
759 IO->Now = ev_now(event_base);
760 switch (IO->NextState) {
762 nbytes = FileRecvChunked(&IO->IOB, &errmsg);
764 StrBufPlain(IO->ErrMsg, errmsg, -1);
767 if (IO->IOB.ChunkSendRemain == 0)
769 IO->NextState = eSendReply;
770 assert(IO->ReadDone);
771 ev_io_stop(event_base, &IO->recv_event);
780 nbytes = StrBuf_read_one_chunk_callback(IO->RecvBuf.fd,
792 const char *pch = ChrPtr(IO->RecvBuf.Buf);
793 const char *pchh = IO->RecvBuf.ReadWritePointer;
798 nbytes = StrLength(IO->RecvBuf.Buf) - (pchh - pch);
799 snprintf(fn, SIZ, "/tmp/foolog_ev_%s.%d",
800 ((CitContext*)(IO->CitContext))->ServiceName,
803 fd = fopen(fn, "a+");
804 fprintf(fd, "Read: BufSize: %ld BufContent: [",
806 rv = fwrite(pchh, nbytes, 1, fd);
807 if (!rv) printf("failed to write debug to %s!\n", fn);
814 } else if (nbytes == 0) {
815 SetNextTimeout(IO, 0.0);
817 } else if (nbytes == -1) {
818 if (errno != EAGAIN) {
819 // FD is gone. kick it.
820 StopClientWatchers(IO, 1);
822 "EVENT: Socket Invalid! [%d] [%s] [%d]\n",
823 errno, strerror(errno), IO->SendBuf.fd);
824 StrBufPrintf(IO->ErrMsg,
825 "Socket Invalid! [%s]",
827 SetNextTimeout(IO, 0.0);
834 IO_postdns_callback(struct ev_loop *loop, ev_idle *watcher, int revents)
836 AsyncIO *IO = watcher->data;
837 IO->Now = ev_now(event_base);
838 EV_syslog(LOG_DEBUG, "event: %s\n", __FUNCTION__);
839 become_session(IO->CitContext);
840 assert(IO->DNS.Query->PostDNS);
841 switch (IO->DNS.Query->PostDNS(IO))
844 assert(IO->DNS.Fail);
845 switch (IO->DNS.Fail(IO)) {
847 //// StopClientWatchers(IO);
858 eNextState EvConnectSock(AsyncIO *IO,
860 double first_rw_timeout,
863 struct sockaddr_in egress_sin;
867 become_session(IO->CitContext);
870 IO->NextState = eReadMessage;
873 IO->NextState = eSendReply;
876 IO->SendBuf.fd = IO->RecvBuf.fd =
878 (IO->ConnectMe->IPv6)?PF_INET6:PF_INET,
882 if (IO->SendBuf.fd < 0) {
884 "EVENT: socket() failed: %s\n",
887 StrBufPrintf(IO->ErrMsg,
888 "Failed to create socket: %s",
890 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
893 fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
896 "EVENT: unable to get socket flags! %s \n",
898 StrBufPrintf(IO->ErrMsg,
899 "Failed to get socket flags: %s",
901 close(IO->SendBuf.fd);
902 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
905 fdflags = fdflags | O_NONBLOCK;
906 if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
909 "EVENT: unable to set socket nonblocking flags! %s \n",
911 StrBufPrintf(IO->ErrMsg,
912 "Failed to set socket flags: %s",
914 close(IO->SendBuf.fd);
915 IO->SendBuf.fd = IO->RecvBuf.fd = 0;
918 /* TODO: maye we could use offsetof() to calc the position of data...
919 * http://doc.dvgu.ru/devel/ev.html#associating_custom_data_with_a_watcher
921 ev_io_init(&IO->recv_event, IO_recv_callback, IO->RecvBuf.fd, EV_READ);
922 IO->recv_event.data = IO;
923 ev_io_init(&IO->send_event, IO_send_callback, IO->SendBuf.fd, EV_WRITE);
924 IO->send_event.data = IO;
926 ev_timer_init(&IO->conn_fail, IO_connfail_callback, conn_timeout, 0);
927 IO->conn_fail.data = IO;
928 ev_timer_init(&IO->rw_timeout, IO_Timeout_callback, first_rw_timeout,0);
929 IO->rw_timeout.data = IO;
934 /* for debugging you may bypass it like this:
935 * IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
936 * ((struct sockaddr_in)IO->ConnectMe->Addr).sin_addr.s_addr =
937 * inet_addr("127.0.0.1");
939 if (IO->ConnectMe->IPv6) {
940 rc = connect(IO->SendBuf.fd,
941 &IO->ConnectMe->Addr,
942 sizeof(struct sockaddr_in6));
945 /* If citserver is bound to a specific IP address on the host, make
946 * sure we use that address for outbound connections.
949 memset(&egress_sin, 0, sizeof(egress_sin));
950 egress_sin.sin_family = AF_INET;
951 if (!IsEmptyStr(config.c_ip_addr)) {
952 egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
953 if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
954 egress_sin.sin_addr.s_addr = INADDR_ANY;
957 /* If this bind fails, no problem; we can still use INADDR_ANY */
958 bind(IO->SendBuf.fd, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
960 rc = connect(IO->SendBuf.fd,
961 (struct sockaddr_in *)&IO->ConnectMe->Addr,
962 sizeof(struct sockaddr_in));
966 EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
967 set_start_callback(event_base, IO, 0);
968 return IO->NextState;
970 else if (errno == EINPROGRESS) {
971 EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
973 ev_io_init(&IO->conn_event,
974 IO_connestd_callback,
978 IO->conn_event.data = IO;
980 ev_io_start(event_base, &IO->conn_event);
981 ev_timer_start(event_base, &IO->conn_fail);
982 return IO->NextState;
985 ev_idle_init(&IO->conn_fail_immediate,
986 IO_connfailimmediate_callback);
987 IO->conn_fail_immediate.data = IO;
988 ev_idle_start(event_base, &IO->conn_fail_immediate);
990 EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
991 StrBufPrintf(IO->ErrMsg,
992 "Failed to connect: %s",
994 return IO->NextState;
996 return IO->NextState;
999 void SetNextTimeout(AsyncIO *IO, double timeout)
1001 IO->rw_timeout.repeat = timeout;
1002 ev_timer_again (event_base, &IO->rw_timeout);
1006 eNextState ReAttachIO(AsyncIO *IO,
1011 become_session(IO->CitContext);
1012 ev_cleanup_start(event_base, &IO->abort_by_shutdown);
1014 IO->NextState = eReadMessage;
1017 IO->NextState = eSendReply;
1019 set_start_callback(event_base, IO, 0);
1021 return IO->NextState;
1024 void InitIOStruct(AsyncIO *IO,
1026 eNextState NextState,
1027 IO_LineReaderCallback LineReader,
1028 IO_CallBack DNS_Fail,
1029 IO_CallBack SendDone,
1030 IO_CallBack ReadDone,
1031 IO_CallBack Terminate,
1032 IO_CallBack DBTerminate,
1033 IO_CallBack ConnFail,
1034 IO_CallBack Timeout,
1035 IO_CallBack ShutdownAbort)
1039 IO->CitContext = CloneContext(CC);
1040 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1042 IO->NextState = NextState;
1044 IO->SendDone = SendDone;
1045 IO->ReadDone = ReadDone;
1046 IO->Terminate = Terminate;
1047 IO->DBTerminate = DBTerminate;
1048 IO->LineReader = LineReader;
1049 IO->ConnFail = ConnFail;
1050 IO->Timeout = Timeout;
1051 IO->ShutdownAbort = ShutdownAbort;
1053 IO->DNS.Fail = DNS_Fail;
1055 IO->SendBuf.Buf = NewStrBufPlain(NULL, 1024);
1056 IO->RecvBuf.Buf = NewStrBufPlain(NULL, 1024);
1057 IO->IOBuf = NewStrBuf();
1058 EV_syslog(LOG_DEBUG,
1059 "EVENT: Session lives at %p IO at %p \n",
1064 extern int evcurl_init(AsyncIO *IO);
1066 int InitcURLIOStruct(AsyncIO *IO,
1069 IO_CallBack SendDone,
1070 IO_CallBack Terminate,
1071 IO_CallBack DBTerminate,
1072 IO_CallBack ShutdownAbort)
1076 IO->CitContext = CloneContext(CC);
1077 ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
1079 IO->SendDone = SendDone;
1080 IO->Terminate = Terminate;
1081 IO->DBTerminate = DBTerminate;
1082 IO->ShutdownAbort = ShutdownAbort;
1084 strcpy(IO->HttpReq.errdesc, Desc);
1087 return evcurl_init(IO);
1091 extern int DebugEventLoopBacktrace;
1092 void EV_backtrace(AsyncIO *IO)
1094 #ifdef HAVE_BACKTRACE
1095 void *stack_frames[50];
1099 if ((IO == NULL) || (DebugEventLoopBacktrace == 0))
1101 size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
1102 strings = backtrace_symbols(stack_frames, size);
1103 for (i = 0; i < size; i++) {
1104 if (strings != NULL) {
1105 EV_syslog(LOG_ALERT, " BT %s\n", strings[i]);
1108 EV_syslog(LOG_ALERT, " BT %p\n", stack_frames[i]);
1116 ev_tstamp ctdl_ev_now (void)
1118 return ev_now(event_base);