EVENT: output the FD number in the debug log once we know it
[citadel.git] / citadel / event_client.c
index 35581e516b37022ef18b602117fb0c63d60e3acd..7122ffc720035fe0102995cccfbe405346b34f8e 100644 (file)
@@ -298,6 +298,7 @@ void FreeAsyncIOContents(AsyncIO *IO)
        if (Ctx) {
                Ctx->state = CON_IDLE;
                Ctx->kill_me = 1;
+               IO->CitContext = NULL;
        }
 }
 
@@ -887,10 +888,12 @@ eNextState EvConnectSock(AsyncIO *IO,
        fdflags = fcntl(IO->SendBuf.fd, F_GETFL);
        if (fdflags < 0) {
                EV_syslog(LOG_ERR,
-                         "EVENT: unable to get socket flags! %s \n",
+                         "EVENT: unable to get socket %d flags! %s \n",
+                         IO->SendBuf.fd,
                          strerror(errno));
                StrBufPrintf(IO->ErrMsg,
-                            "Failed to get socket flags: %s",
+                            "Failed to get socket %d flags: %s",
+                            IO->SendBuf.fd,
                             strerror(errno));
                close(IO->SendBuf.fd);
                IO->SendBuf.fd = IO->RecvBuf.fd = 0;
@@ -900,7 +903,8 @@ eNextState EvConnectSock(AsyncIO *IO,
        if (fcntl(IO->SendBuf.fd, F_SETFL, fdflags) < 0) {
                EV_syslog(
                        LOG_ERR,
-                       "EVENT: unable to set socket nonblocking flags! %s \n",
+                       "EVENT: unable to set socket %d nonblocking flags! %s \n",
+                       IO->SendBuf.fd,
                        strerror(errno));
                StrBufPrintf(IO->ErrMsg,
                             "Failed to set socket flags: %s",
@@ -957,12 +961,12 @@ eNextState EvConnectSock(AsyncIO *IO,
        }
 
        if (rc >= 0){
-               EVM_syslog(LOG_DEBUG, "connect() immediate success.\n");
+               EV_syslog(LOG_DEBUG, "connect() = %d immediate success.\n", IO->SendBuf.fd);
                set_start_callback(event_base, IO, 0);
                return IO->NextState;
        }
        else if (errno == EINPROGRESS) {
-               EVM_syslog(LOG_DEBUG, "connect() have to wait now.\n");
+               EV_syslog(LOG_DEBUG, "connect() = %d have to wait now.\n", IO->SendBuf.fd);
 
                ev_io_init(&IO->conn_event,
                           IO_connestd_callback,
@@ -981,7 +985,11 @@ eNextState EvConnectSock(AsyncIO *IO,
                IO->conn_fail_immediate.data = IO;
                ev_idle_start(event_base, &IO->conn_fail_immediate);
 
-               EV_syslog(LOG_ERR, "connect() failed: %s\n", strerror(errno));
+               EV_syslog(LOG_ERR,
+                         "connect() = %d failed: %s\n",
+                         IO->SendBuf.fd,
+                         strerror(errno));
+
                StrBufPrintf(IO->ErrMsg,
                             "Failed to connect: %s",
                             strerror(errno));
@@ -1031,7 +1039,8 @@ void InitIOStruct(AsyncIO *IO,
        IO->Data          = Data;
 
        IO->CitContext    = CloneContext(CC);
-       ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
+       IO->CitContext->session_specific_data = (char*) Data;
+       IO->CitContext->IO = IO;
 
        IO->NextState     = NextState;
 
@@ -1068,7 +1077,8 @@ int InitcURLIOStruct(AsyncIO *IO,
        IO->Data          = Data;
 
        IO->CitContext    = CloneContext(CC);
-       ((CitContext *)IO->CitContext)->session_specific_data = (char*) Data;
+       IO->CitContext->session_specific_data = (char*) Data;
+       IO->CitContext->IO = IO;
 
        IO->SendDone      = SendDone;
        IO->Terminate     = Terminate;
@@ -1082,6 +1092,85 @@ int InitcURLIOStruct(AsyncIO *IO,
 
 }
 
+
+typedef struct KillOtherSessionContext {
+       AsyncIO IO;
+       AsyncIO *OtherOne;
+}KillOtherSessionContext;
+
+eNextState KillTerminate(AsyncIO *IO)
+{
+       KillOtherSessionContext *Ctx = (KillOtherSessionContext*)IO->Data;
+       EV_syslog(LOG_DEBUG, "%s Exit\n", __FUNCTION__);
+       FreeAsyncIOContents(IO);
+       memset(Ctx, 0, sizeof(KillOtherSessionContext));
+       free(Ctx);
+       return eAbort;
+
+}
+
+eNextState KillShutdown(AsyncIO *IO)
+{
+       return eTerminateConnection;
+}
+
+eNextState KillOtherContextNow(AsyncIO *IO)
+{
+       KillOtherSessionContext *Ctx = IO->Data;
+
+       if (Ctx->OtherOne->ShutdownAbort != NULL)
+               Ctx->OtherOne->ShutdownAbort(Ctx->OtherOne);
+       return eTerminateConnection;
+}
+
+void KillAsyncIOContext(AsyncIO *IO)
+{
+       KillOtherSessionContext *Ctx;
+
+       Ctx = (KillOtherSessionContext*) malloc(sizeof(KillOtherSessionContext));
+       memset(Ctx, 0, sizeof(KillOtherSessionContext));
+       
+       InitIOStruct(&Ctx->IO,
+                    Ctx,
+                    eReadMessage,
+                    NULL,
+                    NULL,
+                    NULL,
+                    NULL,
+                    KillTerminate,
+                    NULL,
+                    NULL,
+                    NULL,
+                    KillShutdown);
+
+       Ctx->OtherOne = IO;
+
+       switch(IO->NextState) {
+       case eSendDNSQuery:
+       case eReadDNSReply:
+
+       case eConnect:
+       case eSendReply:
+       case eSendMore:
+       case eSendFile:
+
+       case eReadMessage:
+       case eReadMore:
+       case eReadPayload:
+       case eReadFile:
+               QueueEventContext(&Ctx->IO, KillOtherContextNow);
+               break;
+       case eDBQuery:
+               QueueDBOperation(&Ctx->IO, KillOtherContextNow);
+               break;
+       case eTerminateConnection:
+       case eAbort:
+               /*hm, its already dying, dunno which Queue its in... */
+               free(Ctx);
+       }
+       
+}
+
 extern int DebugEventLoopBacktrace;
 void EV_backtrace(AsyncIO *IO)
 {