r23779: Change from v2 or later to v3 or later.
[samba.git] / source3 / lib / sendfile.c
index d2ecf3f94ac4d3845728e9754a026697e021526d..46099efd8fea06a619507a52daf6ef20cc3f54e0 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
@@ -38,7 +38,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,8 +65,20 @@ 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) {
+                               /* 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... */
                total -= nwritten;
@@ -131,8 +143,20 @@ 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) {
+                               /* 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... */
                small_total -= nwritten;
@@ -144,7 +168,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 +185,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 +274,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) {
@@ -372,6 +395,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)