]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/network/serv_networkclient.c
NetworkClient: fix filedescriptor leaks
[citadel.git] / citadel / modules / network / serv_networkclient.c
index fbce060ebf22416f6968c91b0858e21de37fbae9..8ee884619ef2e6cac7d869acc83e90da17e0be6b 100644 (file)
@@ -274,7 +274,7 @@ eNextState NWC_ReadNDOPReply(AsyncNetworker *NW)
 
                NW->IO.IOB.TotalSentAlready = 0;
                TotalSendSize = atol (ChrPtr(NW->IO.IOBuf) + 4);
-               EVN_syslog(LOG_DEBUG, "Expecting to transfer %ld bytes\n", TotalSendSize);
+               EVN_syslog(LOG_DEBUG, "Expecting to transfer %d bytes\n", TotalSendSize);
                if (TotalSendSize <= 0) {
                        NW->State = eNUOP - 1;
                }
@@ -318,6 +318,7 @@ eNextState NWC_SendREAD(AsyncNetworker *NW)
                {
                        FDIOBufferDelete(&NW->IO.IOB);
                        unlink(ChrPtr(NW->tempFileName));
+                       FDIOBufferDelete(&IO->IOB);
                        return eAbort;
                }
                StrBufPrintf(NW->IO.SendBuf.Buf, "READ %ld|%ld\n",
@@ -352,6 +353,7 @@ eNextState NWC_ReadREADState(AsyncNetworker *NW)
                        NW->IO.IOB.ChunkSize = atol(ChrPtr(NW->IO.IOBuf)+4);
                return eReadFile;
        }
+       FDIOBufferDelete(&IO->IOB);
        return eAbort;
 }
 eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW);
@@ -391,12 +393,11 @@ eNextState NWC_ReadREADBlobDone(AsyncNetworker *NW)
        eNextState rc;
        AsyncIO *IO = &NW->IO;
 /* we don't have any data to debug print here. */
-       if (NW->IO.IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       if (NW->IO.IOB.TotalSentAlready >= NW->IO.IOB.TotalSendSize)
        {
                NW->State ++;
 
                FDIOBufferDelete(&NW->IO.IOB);
-
                if (link(ChrPtr(NW->tempFileName), ChrPtr(NW->SpoolFileName)) != 0) {
                        EVN_syslog(LOG_ALERT, 
                               "Could not link %s to %s: %s\n",
@@ -428,6 +429,7 @@ eNextState NWC_ReadCLOSReply(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
+       FDIOBufferDelete(&IO->IOB);
        if (ChrPtr(NW->IO.IOBuf)[0] != '2')
                return eTerminateConnection;
        return eSendReply;
@@ -448,7 +450,7 @@ eNextState NWC_SendNUOP(AsyncNetworker *NW)
                     ChrPtr(NW->node));
        StrBufStripSlashes(NW->SpoolFileName, 1);
 
-       fd = open(ChrPtr(NW->SpoolFileName), O_RDONLY);
+       fd = open(ChrPtr(NW->SpoolFileName), O_EXCL|O_NONBLOCK|O_RDONLY);
        if (fd < 0) {
                if (errno != ENOENT) {
                        EVN_syslog(LOG_CRIT,
@@ -476,6 +478,7 @@ eNextState NWC_SendNUOP(AsyncNetworker *NW)
                NW->State = eQUIT;
                rc = NWC_SendQUIT(NW);
                NWC_DBG_SEND();
+               if (fd > 0) close(fd);
                return rc;
        }
        FDIOBufferInit(&NW->IO.IOB, &NW->IO.SendBuf, fd, TotalSendSize);
@@ -489,8 +492,10 @@ eNextState NWC_ReadNUOPReply(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
        NWC_DBG_READ();
-       if (ChrPtr(NW->IO.IOBuf)[0] != '2')
+       if (ChrPtr(NW->IO.IOBuf)[0] != '2') {
+               FDIOBufferDelete(&IO->IOB);
                return eAbort;
+       }
        return eSendReply;
 }
 
@@ -508,6 +513,7 @@ eNextState NWC_ReadWRITReply(AsyncNetworker *NW)
        NWC_DBG_READ();
        if (ChrPtr(NW->IO.IOBuf)[0] != '7')
        {
+               FDIOBufferDelete(&IO->IOB);
                return eAbort;
        }
 
@@ -520,7 +526,7 @@ eNextState NWC_SendBlobDone(AsyncNetworker *NW)
 {
        AsyncIO *IO = &NW->IO;
        eNextState rc;
-       if (IO->IOB.TotalSendSize == NW->IO.IOB.TotalSentAlready)
+       if (NW->IO.IOB.TotalSentAlready >= IO->IOB.TotalSendSize)
        {
                NW->State ++;
 
@@ -556,6 +562,7 @@ eNextState NWC_ReadUCLS(AsyncNetworker *NW)
                EVN_syslog(LOG_DEBUG, "Removing <%s>\n", ChrPtr(NW->SpoolFileName));
                unlink(ChrPtr(NW->SpoolFileName));
        }
+       FDIOBufferDelete(&IO->IOB);
        return eSendReply;
 }