1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_arch_networkio.h"
18 #include "apr_support.h"
21 /* This file is needed to allow us access to the apr_file_t internals. */
22 #include "apr_arch_file_io.h"
23 #endif /* APR_HAS_SENDFILE */
25 /* osreldate.h is only needed on FreeBSD for sendfile detection */
26 #if defined(__FreeBSD__)
27 #include <osreldate.h>
30 apr_status_t apr_socket_send(apr_socket_t *sock, const char *buf,
35 if (sock->options & APR_INCOMPLETE_WRITE) {
36 sock->options &= ~APR_INCOMPLETE_WRITE;
41 rv = write(sock->socketdes, buf, (*len));
42 } while (rv == -1 && errno == EINTR);
44 while (rv == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)
45 && (sock->timeout > 0)) {
48 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
49 if (arv != APR_SUCCESS) {
55 rv = write(sock->socketdes, buf, (*len));
56 } while (rv == -1 && errno == EINTR);
63 if ((sock->timeout > 0) && (rv < *len)) {
64 sock->options |= APR_INCOMPLETE_WRITE;
70 apr_status_t apr_socket_recv(apr_socket_t *sock, char *buf, apr_size_t *len)
75 if (sock->options & APR_INCOMPLETE_READ) {
76 sock->options &= ~APR_INCOMPLETE_READ;
81 rv = read(sock->socketdes, buf, (*len));
82 } while (rv == -1 && errno == EINTR);
84 while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
85 && (sock->timeout > 0)) {
87 arv = apr_wait_for_io_or_timeout(NULL, sock, 1);
88 if (arv != APR_SUCCESS) {
94 rv = read(sock->socketdes, buf, (*len));
95 } while (rv == -1 && errno == EINTR);
102 if ((sock->timeout > 0) && (rv < *len)) {
103 sock->options |= APR_INCOMPLETE_READ;
112 apr_status_t apr_socket_sendto(apr_socket_t *sock, apr_sockaddr_t *where,
113 apr_int32_t flags, const char *buf,
119 rv = sendto(sock->socketdes, buf, (*len), flags,
120 (const struct sockaddr*)&where->sa,
122 } while (rv == -1 && errno == EINTR);
124 while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
125 && (sock->timeout > 0)) {
126 apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
127 if (arv != APR_SUCCESS) {
132 rv = sendto(sock->socketdes, buf, (*len), flags,
133 (const struct sockaddr*)&where->sa,
135 } while (rv == -1 && errno == EINTR);
146 apr_status_t apr_socket_recvfrom(apr_sockaddr_t *from, apr_socket_t *sock,
147 apr_int32_t flags, char *buf,
152 from->salen = sizeof(from->sa);
155 rv = recvfrom(sock->socketdes, buf, (*len), flags,
156 (struct sockaddr*)&from->sa, &from->salen);
157 } while (rv == -1 && errno == EINTR);
159 while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
160 && (sock->timeout > 0)) {
161 apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 1);
162 if (arv != APR_SUCCESS) {
167 rv = recvfrom(sock->socketdes, buf, (*len), flags,
168 (struct sockaddr*)&from->sa, &from->salen);
169 } while (rv == -1 && errno == EINTR);
178 * Check if we have a valid address. recvfrom() with MSG_PEEK may return
179 * success without filling in the address.
181 if (from->salen > APR_OFFSETOF(struct sockaddr_in, sin_port)) {
182 apr_sockaddr_vars_set(from, from->sa.sin.sin_family,
183 ntohs(from->sa.sin.sin_port));
187 if (rv == 0 && sock->type == SOCK_STREAM) {
194 apr_status_t apr_socket_sendv(apr_socket_t * sock, const struct iovec *vec,
195 apr_int32_t nvec, apr_size_t *len)
199 apr_size_t requested_len = 0;
202 for (i = 0; i < nvec; i++) {
203 requested_len += vec[i].iov_len;
206 if (sock->options & APR_INCOMPLETE_WRITE) {
207 sock->options &= ~APR_INCOMPLETE_WRITE;
212 rv = writev(sock->socketdes, vec, nvec);
213 } while (rv == -1 && errno == EINTR);
215 while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
216 && (sock->timeout > 0)) {
219 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
220 if (arv != APR_SUCCESS) {
226 rv = writev(sock->socketdes, vec, nvec);
227 } while (rv == -1 && errno == EINTR);
234 if ((sock->timeout > 0) && (rv < requested_len)) {
235 sock->options |= APR_INCOMPLETE_WRITE;
240 *len = vec[0].iov_len;
241 return apr_socket_send(sock, vec[0].iov_base, len);
247 /* TODO: Verify that all platforms handle the fd the same way,
248 * i.e. that they don't move the file pointer.
250 /* TODO: what should flags be? int_32? */
252 /* Define a structure to pass in when we have a NULL header value */
253 static apr_hdtr_t no_hdtr;
255 #if (defined(__linux__) || defined(__GNU__)) && defined(HAVE_WRITEV)
257 apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file,
258 apr_hdtr_t *hdtr, apr_off_t *offset,
259 apr_size_t *len, apr_int32_t flags)
261 int rv, nbytes = 0, total_hdrbytes, i;
264 #if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILE64)
265 apr_off_t off = *offset;
266 #define sendfile sendfile64
268 #elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
269 /* 64-bit apr_off_t but no sendfile64(): fail if trying to send
270 * past the 2Gb limit. */
273 if ((apr_int64_t)*offset + *len > INT_MAX) {
282 /* Multiple reports have shown sendfile failing with EINVAL if
283 * passed a >=2Gb count value on some 64-bit kernels. It won't
284 * noticably hurt performance to limit each call to <2Gb at a
285 * time, so avoid that issue here: */
286 if (sizeof(off_t) == 8 && *len > INT_MAX) {
295 if (hdtr->numheaders > 0) {
298 /* cork before writing headers */
299 rv = apr_socket_opt_set(sock, APR_TCP_NOPUSH, 1);
300 if (rv != APR_SUCCESS) {
304 /* Now write the headers */
305 arv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders,
307 if (arv != APR_SUCCESS) {
313 /* If this was a partial write and we aren't doing timeouts,
314 * return now with the partial byte count; this is a non-blocking
318 for (i = 0; i < hdtr->numheaders; i++) {
319 total_hdrbytes += hdtr->headers[i].iov_len;
321 if (hdrbytes < total_hdrbytes) {
323 return apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
327 if (sock->options & APR_INCOMPLETE_WRITE) {
328 sock->options &= ~APR_INCOMPLETE_WRITE;
333 rv = sendfile(sock->socketdes, /* socket */
334 file->filedes, /* open file descriptor of the file to be sent */
335 &off, /* where in the file to start */
336 *len); /* number of bytes to send */
337 } while (rv == -1 && errno == EINTR);
339 while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
340 && (sock->timeout > 0)) {
342 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
343 if (arv != APR_SUCCESS) {
349 rv = sendfile(sock->socketdes, /* socket */
350 file->filedes, /* open file descriptor of the file to be sent */
351 &off, /* where in the file to start */
352 *len); /* number of bytes to send */
353 } while (rv == -1 && errno == EINTR);
360 apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
368 arv = apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
371 /* If this was a partial write, return now with the
372 * partial byte count; this is a non-blocking socket.
375 if (sock->timeout > 0) {
376 sock->options |= APR_INCOMPLETE_WRITE;
381 /* If the file got smaller mid-request, eventually the offset
382 * becomes equal to the new file size and the kernel returns 0.
383 * Make this an error so the caller knows to log something and
390 /* Now write the footers */
391 if (hdtr->numtrailers > 0) {
393 arv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers,
396 if (arv != APR_SUCCESS) {
399 apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
404 apr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
407 return rv < 0 ? errno : APR_SUCCESS;
410 #elif defined(DARWIN)
412 /* OS/X Release 10.5 or greater */
413 apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file,
414 apr_hdtr_t *hdtr, apr_off_t *offset,
415 apr_size_t *len, apr_int32_t flags)
417 apr_off_t nbytes = 0;
418 apr_off_t bytes_to_send = *len;
419 apr_off_t bytes_sent = 0;
423 /* Ignore flags for now. */
430 /* OS X can send the headers/footers as part of the system call,
431 * but how it counts bytes isn't documented properly. We use
432 * apr_socket_sendv() instead.
434 if (hdtr->numheaders > 0) {
438 /* Now write the headers */
439 arv = apr_socket_sendv(sock, hdtr->headers, hdtr->numheaders,
441 if (arv != APR_SUCCESS) {
448 for (i = 0; i < hdtr->numheaders; i++) {
449 hbytes += hdtr->headers[i].iov_len;
451 if (bytes_sent < hbytes) {
458 if (!bytes_to_send) {
461 if (sock->options & APR_INCOMPLETE_WRITE) {
463 sock->options &= ~APR_INCOMPLETE_WRITE;
464 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
465 if (arv != APR_SUCCESS) {
471 nbytes = bytes_to_send;
472 rv = sendfile(file->filedes, /* file to be sent */
473 sock->socketdes, /* socket */
474 *offset, /* where in the file to start */
475 &nbytes, /* number of bytes to write/written */
476 NULL, /* Headers/footers */
477 flags); /* undefined, set to 0 */
480 if (errno == EAGAIN) {
481 if (sock->timeout > 0) {
482 sock->options |= APR_INCOMPLETE_WRITE;
484 /* BSD's sendfile can return -1/EAGAIN even if it
485 * sent bytes. Sanitize the result so we get normal EAGAIN
486 * semantics w.r.t. bytes sent.
489 bytes_sent += nbytes;
490 /* normal exit for a big file & non-blocking io */
496 else { /* rv == 0 (or the kernel is broken) */
497 bytes_sent += nbytes;
499 /* Most likely the file got smaller after the stat.
500 * Return an error so the caller can do the Right Thing.
506 } while (rv == -1 && (errno == EINTR || errno == EAGAIN));
508 /* Now write the footers */
509 if (hdtr->numtrailers > 0) {
511 arv = apr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers,
513 bytes_sent += tbytes;
514 if (arv != APR_SUCCESS) {
528 #elif defined(__FreeBSD__) || defined(__DragonFly__)
530 /* Release 3.1 or greater */
531 apr_status_t apr_socket_sendfile(apr_socket_t * sock, apr_file_t * file,
532 apr_hdtr_t * hdtr, apr_off_t * offset,
533 apr_size_t * len, apr_int32_t flags)
537 #if defined(__FreeBSD_version) && __FreeBSD_version < 460001
540 struct sf_hdtr headerstruct;
541 apr_size_t bytes_to_send = *len;
543 /* Ignore flags for now. */
550 #if defined(__FreeBSD_version) && __FreeBSD_version < 460001
551 else if (hdtr->numheaders) {
553 /* On early versions of FreeBSD sendfile, the number of bytes to send
554 * must include the length of the headers. Don't look at the man page
555 * for this :( Instead, look at the the logic in
556 * src/sys/kern/uipc_syscalls::sendfile().
558 * This was fixed in the middle of 4.6-STABLE
560 for (i = 0; i < hdtr->numheaders; i++) {
561 bytes_to_send += hdtr->headers[i].iov_len;
566 headerstruct.headers = hdtr->headers;
567 headerstruct.hdr_cnt = hdtr->numheaders;
568 headerstruct.trailers = hdtr->trailers;
569 headerstruct.trl_cnt = hdtr->numtrailers;
571 /* FreeBSD can send the headers/footers as part of the system call */
573 if (sock->options & APR_INCOMPLETE_WRITE) {
575 sock->options &= ~APR_INCOMPLETE_WRITE;
576 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
577 if (arv != APR_SUCCESS) {
583 /* We won't dare call sendfile() if we don't have
584 * header or file bytes to send because bytes_to_send == 0
585 * means send the whole file.
587 rv = sendfile(file->filedes, /* file to be sent */
588 sock->socketdes, /* socket */
589 *offset, /* where in the file to start */
590 bytes_to_send, /* number of bytes to send */
591 &headerstruct, /* Headers/footers */
592 &nbytes, /* number of bytes written */
593 flags); /* undefined, set to 0 */
596 if (errno == EAGAIN) {
597 if (sock->timeout > 0) {
598 sock->options |= APR_INCOMPLETE_WRITE;
600 /* FreeBSD's sendfile can return -1/EAGAIN even if it
601 * sent bytes. Sanitize the result so we get normal EAGAIN
602 * semantics w.r.t. bytes sent.
605 /* normal exit for a big file & non-blocking io */
611 else { /* rv == 0 (or the kernel is broken) */
613 /* Most likely the file got smaller after the stat.
614 * Return an error so the caller can do the Right Thing.
622 /* just trailer bytes... use writev()
624 rv = writev(sock->socketdes,
635 if ((rv == -1) && (errno == EAGAIN)
636 && (sock->timeout > 0)) {
637 apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
638 if (arv != APR_SUCCESS) {
643 } while (rv == -1 && (errno == EINTR || errno == EAGAIN));
652 #elif defined(__hpux) || defined(__hpux__)
654 /* HP cc in ANSI mode defines __hpux; gcc defines __hpux__ */
656 /* HP-UX Version 10.30 or greater
657 * (no worries, because we only get here if autoconfiguration found sendfile)
660 /* ssize_t sendfile(int s, int fd, off_t offset, size_t nbytes,
661 * const struct iovec *hdtrl, int flags);
663 * nbytes is the number of bytes to send just from the file; as with FreeBSD,
664 * if nbytes == 0, the rest of the file (from offset) is sent
667 apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file,
668 apr_hdtr_t *hdtr, apr_off_t *offset,
669 apr_size_t *len, apr_int32_t flags)
673 apr_size_t nbytes = *len, headerlen, trailerlen;
674 struct iovec hdtrarray[2];
675 char *headerbuf, *trailerbuf;
677 #if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILE64)
678 /* later HP-UXes have a sendfile64() */
679 #define sendfile sendfile64
680 apr_off_t off = *offset;
682 #elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
683 /* HP-UX 11.00 doesn't have a sendfile64(): fail if trying to send
684 * past the 2Gb limit */
687 if ((apr_int64_t)*offset + *len > INT_MAX) {
692 apr_off_t off = *offset;
699 /* Ignore flags for now. */
702 /* HP-UX can only send one header iovec and one footer iovec; try to
703 * only allocate storage to combine input iovecs when we really have to
706 switch(hdtr->numheaders) {
708 hdtrarray[0].iov_base = NULL;
709 hdtrarray[0].iov_len = 0;
712 hdtrarray[0] = hdtr->headers[0];
716 for (i = 0; i < hdtr->numheaders; i++) {
717 headerlen += hdtr->headers[i].iov_len;
720 /* XXX: BUHHH? wow, what a memory leak! */
721 headerbuf = hdtrarray[0].iov_base = apr_palloc(sock->pool, headerlen);
722 hdtrarray[0].iov_len = headerlen;
724 for (i = 0; i < hdtr->numheaders; i++) {
725 memcpy(headerbuf, hdtr->headers[i].iov_base,
726 hdtr->headers[i].iov_len);
727 headerbuf += hdtr->headers[i].iov_len;
731 switch(hdtr->numtrailers) {
733 hdtrarray[1].iov_base = NULL;
734 hdtrarray[1].iov_len = 0;
737 hdtrarray[1] = hdtr->trailers[0];
741 for (i = 0; i < hdtr->numtrailers; i++) {
742 trailerlen += hdtr->trailers[i].iov_len;
745 /* XXX: BUHHH? wow, what a memory leak! */
746 trailerbuf = hdtrarray[1].iov_base = apr_palloc(sock->pool, trailerlen);
747 hdtrarray[1].iov_len = trailerlen;
749 for (i = 0; i < hdtr->numtrailers; i++) {
750 memcpy(trailerbuf, hdtr->trailers[i].iov_base,
751 hdtr->trailers[i].iov_len);
752 trailerbuf += hdtr->trailers[i].iov_len;
757 if (nbytes) { /* any bytes to send from the file? */
758 rc = sendfile(sock->socketdes, /* socket */
759 file->filedes, /* file descriptor to send */
760 off, /* where in the file to start */
761 nbytes, /* number of bytes to send from file */
762 hdtrarray, /* Headers/footers */
763 flags); /* undefined, set to 0 */
765 else { /* we can't call sendfile() with no bytes to send from the file */
766 rc = writev(sock->socketdes, hdtrarray, 2);
768 } while (rc == -1 && errno == EINTR);
770 while ((rc == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
771 && (sock->timeout > 0)) {
772 apr_status_t arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
774 if (arv != APR_SUCCESS) {
781 rc = sendfile(sock->socketdes, /* socket */
782 file->filedes, /* file descriptor to send */
783 off, /* where in the file to start */
784 nbytes, /* number of bytes to send from file */
785 hdtrarray, /* Headers/footers */
786 flags); /* undefined, set to 0 */
788 else { /* we can't call sendfile() with no bytes to send from the file */
789 rc = writev(sock->socketdes, hdtrarray, 2);
791 } while (rc == -1 && errno == EINTR);
800 /* Set len to the number of bytes written */
804 #elif defined(_AIX) || defined(__MVS__)
805 /* AIX and OS/390 have the same send_file() interface.
807 * subtle differences:
808 * AIX doesn't update the file ptr but OS/390 does
810 * availability (correctly determined by autoconf):
812 * AIX - version 4.3.2 with APAR IX85388, or version 4.3.3 and above
813 * OS/390 - V2R7 and above
815 apr_status_t apr_socket_sendfile(apr_socket_t * sock, apr_file_t * file,
816 apr_hdtr_t * hdtr, apr_off_t * offset,
817 apr_size_t * len, apr_int32_t flags)
820 void * hbuf=NULL, * tbuf=NULL;
822 struct sf_parms parms;
828 /* Ignore flags for now. */
831 /* word to the wise: by default, AIX stores files sent by send_file()
832 * in the network buffer cache... there are supposedly scenarios
833 * where the most recent copy of the file won't be sent, but I can't
834 * recreate the potential problem, perhaps because of the way we
835 * use send_file()... if you suspect such a problem, try turning
836 * on the SF_SYNC_CACHE flag
839 /* AIX can also send the headers/footers as part of the system call */
840 parms.header_length = 0;
841 if (hdtr && hdtr->numheaders) {
842 if (hdtr->numheaders == 1) {
843 parms.header_data = hdtr->headers[0].iov_base;
844 parms.header_length = hdtr->headers[0].iov_len;
847 for (i = 0; i < hdtr->numheaders; i++) {
848 parms.header_length += hdtr->headers[i].iov_len;
851 /* Keepalives make apr_palloc a bad idea */
852 hbuf = malloc(parms.header_length);
854 /* but headers are small, so maybe we can hold on to the
855 * memory for the life of the socket...
857 hbuf = apr_palloc(sock->pool, parms.header_length);
860 for (i = 0; i < hdtr->numheaders; i++) {
861 memcpy((char *)hbuf + ptr, hdtr->headers[i].iov_base,
862 hdtr->headers[i].iov_len);
863 ptr += hdtr->headers[i].iov_len;
865 parms.header_data = hbuf;
868 else parms.header_data = NULL;
869 parms.trailer_length = 0;
870 if (hdtr && hdtr->numtrailers) {
871 if (hdtr->numtrailers == 1) {
872 parms.trailer_data = hdtr->trailers[0].iov_base;
873 parms.trailer_length = hdtr->trailers[0].iov_len;
876 for (i = 0; i < hdtr->numtrailers; i++) {
877 parms.trailer_length += hdtr->trailers[i].iov_len;
880 /* Keepalives make apr_palloc a bad idea */
881 tbuf = malloc(parms.trailer_length);
883 tbuf = apr_palloc(sock->pool, parms.trailer_length);
886 for (i = 0; i < hdtr->numtrailers; i++) {
887 memcpy((char *)tbuf + ptr, hdtr->trailers[i].iov_base,
888 hdtr->trailers[i].iov_len);
889 ptr += hdtr->trailers[i].iov_len;
891 parms.trailer_data = tbuf;
895 parms.trailer_data = NULL;
898 /* Whew! Headers and trailers set up. Now for the file data */
900 parms.file_descriptor = file->filedes;
901 parms.file_offset = *offset;
902 parms.file_bytes = *len;
904 /* O.K. All set up now. Let's go to town */
906 if (sock->options & APR_INCOMPLETE_WRITE) {
907 sock->options &= ~APR_INCOMPLETE_WRITE;
912 rv = send_file(&(sock->socketdes), /* socket */
913 &(parms), /* all data */
915 } while (rv == -1 && errno == EINTR);
917 while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
918 && (sock->timeout > 0)) {
920 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
921 if (arv != APR_SUCCESS) {
927 rv = send_file(&(sock->socketdes), /* socket */
928 &(parms), /* all data */
930 } while (rv == -1 && errno == EINTR);
934 (*len) = parms.bytes_sent;
937 /* Clean up after ourselves */
946 if ((sock->timeout > 0)
948 < (parms.file_bytes + parms.header_length + parms.trailer_length))) {
949 sock->options |= APR_INCOMPLETE_WRITE;
954 #elif defined(__osf__) && defined (__alpha)
955 /* Tru64's sendfile implementation doesn't work, and we need to make sure that
956 * we don't use it until it is fixed. If it is used as it is now, it will
957 * hang the machine and the only way to fix it is a reboot.
959 #elif defined(HAVE_SENDFILEV)
960 /* Solaris 8's sendfilev() interface
962 * SFV_FD_SELF refers to our memory space.
964 * Required Sparc patches (or newer):
965 * 111297-01, 108528-09, 109472-06, 109234-03, 108995-02, 111295-01, 109025-03,
967 * Required x86 patches (or newer):
968 * 111298-01, 108529-09, 109473-06, 109235-04, 108996-02, 111296-01, 109026-04,
972 #if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILEV64)
973 #define sendfilevec_t sendfilevec64_t
974 #define sendfilev sendfilev64
977 apr_status_t apr_socket_sendfile(apr_socket_t *sock, apr_file_t *file,
978 apr_hdtr_t *hdtr, apr_off_t *offset,
979 apr_size_t *len, apr_int32_t flags)
981 apr_status_t rv, arv;
984 int vecs, curvec, i, repeat;
985 apr_size_t requested_len = 0;
991 /* Ignore flags for now. */
994 /* Calculate how much space we need. */
995 vecs = hdtr->numheaders + hdtr->numtrailers + 1;
996 sfv = apr_palloc(sock->pool, sizeof(sendfilevec_t) * vecs);
1000 /* Add the headers */
1001 for (i = 0; i < hdtr->numheaders; i++, curvec++) {
1002 sfv[curvec].sfv_fd = SFV_FD_SELF;
1003 sfv[curvec].sfv_flag = 0;
1004 /* Cast to unsigned long to prevent sign extension of the
1005 * pointer value for the LFS case; see PR 39463. */
1006 sfv[curvec].sfv_off = (unsigned long)hdtr->headers[i].iov_base;
1007 sfv[curvec].sfv_len = hdtr->headers[i].iov_len;
1008 requested_len += sfv[curvec].sfv_len;
1011 /* If the len is 0, we skip the file. */
1014 sfv[curvec].sfv_fd = file->filedes;
1015 sfv[curvec].sfv_flag = 0;
1016 sfv[curvec].sfv_off = *offset;
1017 sfv[curvec].sfv_len = *len;
1018 requested_len += sfv[curvec].sfv_len;
1026 /* Add the footers */
1027 for (i = 0; i < hdtr->numtrailers; i++, curvec++) {
1028 sfv[curvec].sfv_fd = SFV_FD_SELF;
1029 sfv[curvec].sfv_flag = 0;
1030 sfv[curvec].sfv_off = (unsigned long)hdtr->trailers[i].iov_base;
1031 sfv[curvec].sfv_len = hdtr->trailers[i].iov_len;
1032 requested_len += sfv[curvec].sfv_len;
1035 /* If the last write couldn't send all the requested data,
1036 * wait for the socket to become writable before proceeding
1038 if (sock->options & APR_INCOMPLETE_WRITE) {
1039 sock->options &= ~APR_INCOMPLETE_WRITE;
1040 arv = apr_wait_for_io_or_timeout(NULL, sock, 0);
1041 if (arv != APR_SUCCESS) {
1047 /* Actually do the sendfilev
1049 * Solaris may return -1/EAGAIN even if it sent bytes on a non-block sock.
1051 * If no bytes were originally sent (nbytes == 0) and we are on a TIMEOUT
1052 * socket (which as far as the OS is concerned is a non-blocking socket),
1053 * we want to retry after waiting for the other side to read the data (as
1054 * determined by poll). Once it is clear to send, we want to retry
1055 * sending the sendfilevec_t once more.
1059 /* Clear out the repeat */
1062 /* socket, vecs, number of vecs, bytes written */
1063 rv = sendfilev(sock->socketdes, sfv, vecs, &nbytes);
1065 if (rv == -1 && errno == EAGAIN) {
1069 else if (!arv && (sock->timeout > 0)) {
1070 apr_status_t t = apr_wait_for_io_or_timeout(NULL, sock, 0);
1072 if (t != APR_SUCCESS) {
1081 } while ((rv == -1 && errno == EINTR) || repeat);
1088 /* Update how much we sent */
1092 /* Most likely the file got smaller after the stat.
1093 * Return an error so the caller can do the Right Thing.
1098 if ((sock->timeout > 0) && (*len < requested_len)) {
1099 sock->options |= APR_INCOMPLETE_WRITE;
1104 #error APR has detected sendfile on your system, but nobody has written a
1105 #error version of it for APR yet. To get past this, either write
1106 #error apr_socket_sendfile or change APR_HAS_SENDFILE in apr.h to 0.
1107 #endif /* __linux__, __FreeBSD__, __DragonFly__, __HPUX__, _AIX, __MVS__,
1110 #endif /* APR_HAS_SENDFILE */