Fix bug noticed by Volker - if sendfile returns zero then
[jra/samba/.git] / source3 / lib / sendfile.c
index d2ecf3f94ac4d3845728e9754a026697e021526d..59a18ce6e184b164946557b1758355b35e5c323c 100644 (file)
@@ -6,7 +6,7 @@
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
+ the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,8 +14,7 @@
  GNU General Public License for more details.
 
  You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -38,7 +37,7 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
 {
        size_t total=0;
        ssize_t ret;
-       ssize_t hdr_len = 0;
+       size_t hdr_len = 0;
 
        /*
         * Send the header first.
@@ -65,10 +64,26 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
                        nwritten = sendfile(tofd, fromfd, &offset, total);
 #endif
                } while (nwritten == -1 && errno == EINTR);
-               if (nwritten == -1)
+               if (nwritten == -1) {
+                       if (errno == ENOSYS || errno == EINVAL) {
+                               /* Ok - we're in a world of pain here. We just sent
+                                * the header, but the sendfile failed. We have to
+                                * emulate the sendfile at an upper layer before we
+                                * disable it's use. So we do something really ugly.
+                                * We set the errno to a strange value so we can detect
+                                * this at the upper level and take care of it without
+                                * layer violation. JRA.
+                                */
+                               errno = EINTR; /* Normally we can never return this. */
+                       }
                        return -1;
-               if (nwritten == 0)
-                       return -1; /* I think we're at EOF here... */
+               }
+               if (nwritten == 0) {
+                       /*
+                        * EOF, return a short read
+                        */
+                       return hdr_len + (count - total);
+               }
                total -= nwritten;
        }
        return count + hdr_len;
@@ -131,10 +146,26 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
                do {
                        nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
                } while (nwritten == -1 && errno == EINTR);
-               if (nwritten == -1)
+               if (nwritten == -1) {
+                       if (errno == ENOSYS || errno == EINVAL) {
+                               /* Ok - we're in a world of pain here. We just sent
+                                * the header, but the sendfile failed. We have to
+                                * emulate the sendfile at an upper layer before we
+                                * disable it's use. So we do something really ugly.
+                                * We set the errno to a strange value so we can detect
+                                * this at the upper level and take care of it without
+                                * layer violation. JRA.
+                                */
+                               errno = EINTR; /* Normally we can never return this. */
+                       }
                        return -1;
-               if (nwritten == 0)
-                       return -1; /* I think we're at EOF here... */
+               }
+               if (nwritten == 0) {
+                       /*
+                        * EOF, return a short read
+                        */
+                       return hdr_len + (((uint32)count) - small_total);
+               }
                small_total -= nwritten;
        }
        return count + hdr_len;
@@ -144,7 +175,7 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
 #elif defined(SOLARIS_SENDFILE_API)
 
 /*
- * Solaris sendfile code written by Pierre Belanger <belanger@yahoo.com>.
+ * Solaris sendfile code written by Pierre Belanger <belanger@pobox.com>.
  */
 
 #include <sys/sendfile.h>
@@ -161,15 +192,14 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
 
                vec[0].sfv_fd = SFV_FD_SELF;
                vec[0].sfv_flag = 0;
-               vec[0].sfv_off = header->data;
-               vec[0].sfv_len = header->length;
+               vec[0].sfv_off = (off_t)header->data;
+               vec[0].sfv_len = hdr_len = header->length;
 
                vec[1].sfv_fd = fromfd;
                vec[1].sfv_flag = 0;
                vec[1].sfv_off = offset;
                vec[1].sfv_len = count;
 
-               hdr_len = header->length;
        } else {
                sfvcnt = 1;
 
@@ -251,7 +281,7 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
                hdtrl[0].iov_len = hdr_len = 0;
        }
        hdtrl[1].iov_base = NULL;
-       hdtrl[1].iov_base = 0;
+       hdtrl[1].iov_len = 0;
 
        total = count;
        while (total + hdtrl[0].iov_len) {
@@ -361,7 +391,8 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
                                hdtrl.iov_base = NULL;
                                hdtrl.iov_len = 0;
                        } else {
-                               hdtrl.iov_base += nwritten;
+                               hdtrl.iov_base =
+                                   (caddr_t)hdtrl.iov_base + nwritten;
                                hdtrl.iov_len -= nwritten;
                                nwritten = 0;
                        }
@@ -372,6 +403,61 @@ ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T of
        return count + hdr_len;
 }
 
+#elif defined(AIX_SENDFILE_API)
+
+/* BEGIN AIX SEND_FILE */
+
+/* Contributed by William Jojo <jojowil@hvcc.edu> */
+#include <sys/socket.h>
+
+ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
+{
+       struct sf_parms hdtrl;
+
+       /* Set up the header/trailer struct params. */
+       if (header) {
+               hdtrl.header_data = header->data;
+               hdtrl.header_length = header->length;
+       } else {
+               hdtrl.header_data = NULL;
+               hdtrl.header_length = 0;
+       }
+       hdtrl.trailer_data = NULL;
+       hdtrl.trailer_length = 0;
+
+       hdtrl.file_descriptor = fromfd;
+       hdtrl.file_offset = offset;
+       hdtrl.file_bytes = count;
+
+       while ( hdtrl.file_bytes + hdtrl.header_length ) {
+               ssize_t ret;
+
+               /*
+                Return Value
+
+                There are three possible return values from send_file:
+
+                Value Description
+
+                -1 an error has occurred, errno contains the error code.
+
+                0 the command has completed successfully.
+
+                1 the command was completed partially, some data has been
+                transmitted but the command has to return for some reason,
+                for example, the command was interrupted by signals.
+               */
+               do {
+                       ret = send_file(&tofd, &hdtrl, 0);
+               } while ( (ret == 1) || (ret == -1 && errno == EINTR) );
+               if ( ret == -1 )
+                       return -1;
+       }
+
+       return count + header->length;
+}
+/* END AIX SEND_FILE */
+
 #else /* No sendfile implementation. Return error. */
 
 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)