12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205 |
- /* Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include "fspr_arch_networkio.h"
- #include "fspr_support.h"
- #if APR_HAS_SENDFILE
- /* This file is needed to allow us access to the fspr_file_t internals. */
- #include "fspr_arch_file_io.h"
- #endif /* APR_HAS_SENDFILE */
- /* osreldate.h is only needed on FreeBSD for sendfile detection */
- #if defined(__FreeBSD__)
- #include <osreldate.h>
- #endif
- #include <assert.h> /* assert() */
- fspr_status_t fspr_socket_send(fspr_socket_t *sock, const char *buf,
- fspr_size_t *len)
- {
- fspr_ssize_t rv;
-
- if (sock->options & APR_INCOMPLETE_WRITE) {
- sock->options &= ~APR_INCOMPLETE_WRITE;
- goto do_select;
- }
- do {
- rv = write(sock->socketdes, buf, (*len));
- } while (rv == -1 && errno == EINTR);
- while (rv == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- fspr_status_t arv;
- do_select:
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- else {
- do {
- rv = write(sock->socketdes, buf, (*len));
- } while (rv == -1 && errno == EINTR);
- }
- }
- if (rv == -1) {
- *len = 0;
- return errno;
- }
- if ((sock->timeout > 0) && (rv < *len)) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- (*len) = rv;
- return APR_SUCCESS;
- }
- fspr_status_t fspr_socket_recv(fspr_socket_t *sock, char *buf, fspr_size_t *len)
- {
- fspr_ssize_t rv;
- fspr_status_t arv;
- if (sock->options & APR_INCOMPLETE_READ) {
- sock->options &= ~APR_INCOMPLETE_READ;
- goto do_select;
- }
- do {
- rv = read(sock->socketdes, buf, (*len));
- } while (rv == -1 && errno == EINTR);
- while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- do_select:
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 1);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- else {
- do {
- rv = read(sock->socketdes, buf, (*len));
- } while (rv == -1 && errno == EINTR);
- }
- }
- if (rv == -1) {
- (*len) = 0;
- return errno;
- }
- if ((sock->timeout > 0) && (rv < *len)) {
- sock->options |= APR_INCOMPLETE_READ;
- }
- (*len) = rv;
- if (rv == 0) {
- return APR_EOF;
- }
- return APR_SUCCESS;
- }
- fspr_status_t fspr_socket_sendto(fspr_socket_t *sock, fspr_sockaddr_t *where,
- fspr_int32_t flags, const char *buf,
- fspr_size_t *len)
- {
- fspr_ssize_t rv;
- do {
- rv = sendto(sock->socketdes, buf, (*len), flags,
- (const struct sockaddr*)&where->sa,
- where->salen);
- } while (rv == -1 && errno == EINTR);
- while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- } else {
- do {
- rv = sendto(sock->socketdes, buf, (*len), flags,
- (const struct sockaddr*)&where->sa,
- where->salen);
- } while (rv == -1 && errno == EINTR);
- }
- }
- if (rv == -1) {
- *len = 0;
- return errno;
- }
- *len = rv;
- return APR_SUCCESS;
- }
- fspr_status_t fspr_socket_recvfrom(fspr_sockaddr_t *from, fspr_socket_t *sock,
- fspr_int32_t flags, char *buf,
- fspr_size_t *len)
- {
- fspr_ssize_t rv;
-
- from->salen = sizeof(from->sa);
- do {
- rv = recvfrom(sock->socketdes, buf, (*len), flags,
- (struct sockaddr*)&from->sa, &from->salen);
- } while (rv == -1 && errno == EINTR);
- while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 1);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- } else {
- do {
- rv = recvfrom(sock->socketdes, buf, (*len), flags,
- (struct sockaddr*)&from->sa, &from->salen);
- } while (rv == -1 && errno == EINTR);
- }
- }
- if (rv == -1) {
- (*len) = 0;
- return errno;
- }
- fspr_sockaddr_vars_set(from, from->sa.sin.sin_family, ntohs(from->sa.sin.sin_port));
- (*len) = rv;
- if (rv == 0 && sock->type == SOCK_STREAM) {
- return APR_EOF;
- }
- return APR_SUCCESS;
- }
- fspr_status_t fspr_socket_sendv(fspr_socket_t * sock, const struct iovec *vec,
- fspr_int32_t nvec, fspr_size_t *len)
- {
- #ifdef HAVE_WRITEV
- fspr_ssize_t rv;
- fspr_size_t requested_len = 0;
- fspr_int32_t i;
- for (i = 0; i < nvec; i++) {
- requested_len += vec[i].iov_len;
- }
- if (sock->options & APR_INCOMPLETE_WRITE) {
- sock->options &= ~APR_INCOMPLETE_WRITE;
- goto do_select;
- }
- do {
- rv = writev(sock->socketdes, vec, nvec);
- } while (rv == -1 && errno == EINTR);
- while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- fspr_status_t arv;
- do_select:
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- else {
- do {
- rv = writev(sock->socketdes, vec, nvec);
- } while (rv == -1 && errno == EINTR);
- }
- }
- if (rv == -1) {
- *len = 0;
- return errno;
- }
- if ((sock->timeout > 0) && (rv < requested_len)) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- (*len) = rv;
- return APR_SUCCESS;
- #else
- *len = vec[0].iov_len;
- return fspr_socket_send(sock, vec[0].iov_base, len);
- #endif
- }
- #if APR_HAS_SENDFILE
- /* TODO: Verify that all platforms handle the fd the same way,
- * i.e. that they don't move the file pointer.
- */
- /* TODO: what should flags be? int_32? */
- /* Define a structure to pass in when we have a NULL header value */
- static fspr_hdtr_t no_hdtr;
- #if defined(__linux__) && defined(HAVE_WRITEV)
- fspr_status_t fspr_socket_sendfile(fspr_socket_t *sock, fspr_file_t *file,
- fspr_hdtr_t *hdtr, fspr_off_t *offset,
- fspr_size_t *len, fspr_int32_t flags)
- {
- int rv, nbytes = 0, total_hdrbytes, i;
- fspr_status_t arv;
- #if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILE64)
- fspr_off_t off = *offset;
- #define sendfile sendfile64
- #elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
- /* 64-bit fspr_off_t but no sendfile64(): fail if trying to send
- * past the 2Gb limit. */
- off_t off;
-
- if ((fspr_int64_t)*offset + *len > INT_MAX) {
- return EINVAL;
- }
-
- off = *offset;
- #else
- off_t off = *offset;
- /* Multiple reports have shown sendfile failing with EINVAL if
- * passed a >=2Gb count value on some 64-bit kernels. It won't
- * noticably hurt performance to limit each call to <2Gb at a
- * time, so avoid that issue here: */
- if (sizeof(off_t) == 8 && *len > INT_MAX) {
- *len = INT_MAX;
- }
- #endif
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- /* Ignore flags for now. */
- flags = 0;
- assert(flags==0);
- if (hdtr->numheaders > 0) {
- fspr_size_t hdrbytes;
- /* cork before writing headers */
- rv = fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 1);
- if (rv != APR_SUCCESS) {
- return rv;
- }
- /* Now write the headers */
- arv = fspr_socket_sendv(sock, hdtr->headers, hdtr->numheaders,
- &hdrbytes);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return errno;
- }
- nbytes += hdrbytes;
- /* If this was a partial write and we aren't doing timeouts,
- * return now with the partial byte count; this is a non-blocking
- * socket.
- */
- total_hdrbytes = 0;
- for (i = 0; i < hdtr->numheaders; i++) {
- total_hdrbytes += hdtr->headers[i].iov_len;
- }
- if (hdrbytes < total_hdrbytes) {
- *len = hdrbytes;
- return fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
- }
- }
- if (sock->options & APR_INCOMPLETE_WRITE) {
- sock->options &= ~APR_INCOMPLETE_WRITE;
- goto do_select;
- }
- do {
- rv = sendfile(sock->socketdes, /* socket */
- file->filedes, /* open file descriptor of the file to be sent */
- &off, /* where in the file to start */
- *len); /* number of bytes to send */
- } while (rv == -1 && errno == EINTR);
- while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- do_select:
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- else {
- do {
- rv = sendfile(sock->socketdes, /* socket */
- file->filedes, /* open file descriptor of the file to be sent */
- &off, /* where in the file to start */
- *len); /* number of bytes to send */
- } while (rv == -1 && errno == EINTR);
- }
- }
- if (rv == -1) {
- *len = nbytes;
- rv = errno;
- fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
- return rv;
- }
- nbytes += rv;
- if (rv < *len) {
- *len = nbytes;
- arv = fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
- if (rv > 0) {
-
- /* If this was a partial write, return now with the
- * partial byte count; this is a non-blocking socket.
- */
- if (sock->timeout > 0) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- return arv;
- }
- else {
- /* If the file got smaller mid-request, eventually the offset
- * becomes equal to the new file size and the kernel returns 0.
- * Make this an error so the caller knows to log something and
- * exit.
- */
- return APR_EOF;
- }
- }
- /* Now write the footers */
- if (hdtr->numtrailers > 0) {
- fspr_size_t trbytes;
- arv = fspr_socket_sendv(sock, hdtr->trailers, hdtr->numtrailers,
- &trbytes);
- nbytes += trbytes;
- if (arv != APR_SUCCESS) {
- *len = nbytes;
- rv = errno;
- fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
- return rv;
- }
- }
- fspr_socket_opt_set(sock, APR_TCP_NOPUSH, 0);
-
- (*len) = nbytes;
- return rv < 0 ? errno : APR_SUCCESS;
- }
- #elif defined(DARWIN)
- /* OS/X Release 10.5 or greater */
- fspr_status_t fspr_socket_sendfile(fspr_socket_t * sock, fspr_file_t * file,
- fspr_hdtr_t * hdtr, fspr_off_t * offset,
- fspr_size_t * len, fspr_int32_t flags)
- {
- fspr_off_t nbytes = *len;
- int rv;
- struct sf_hdtr headerstruct;
- /* Ignore flags for now. */
- flags = 0;
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- headerstruct.headers = hdtr->headers;
- headerstruct.hdr_cnt = hdtr->numheaders;
- headerstruct.trailers = hdtr->trailers;
- headerstruct.trl_cnt = hdtr->numtrailers;
- /* BSD can send the headers/footers as part of the system call */
- do {
- if (sock->options & APR_INCOMPLETE_WRITE) {
- fspr_status_t arv;
- sock->options &= ~APR_INCOMPLETE_WRITE;
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
- if (nbytes) {
- /* We won't dare call sendfile() if we don't have
- * header or file bytes to send because nbytes == 0
- * means send the remaining file to EOF.
- */
- rv = sendfile(file->filedes, /* file to be sent */
- sock->socketdes, /* socket */
- *offset, /* where in the file to start */
- &nbytes, /* number of bytes to write/written */
- &headerstruct, /* Headers/footers */
- flags); /* undefined, set to 0 */
- if (rv == -1) {
- if (errno == EAGAIN) {
- if (sock->timeout > 0) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- /* BSD's sendfile can return -1/EAGAIN even if it
- * sent bytes. Sanitize the result so we get normal EAGAIN
- * semantics w.r.t. bytes sent.
- */
- if (nbytes) {
- /* normal exit for a big file & non-blocking io */
- (*len) = nbytes;
- return APR_SUCCESS;
- }
- }
- }
- else { /* rv == 0 (or the kernel is broken) */
- if (nbytes == 0) {
- /* Most likely the file got smaller after the stat.
- * Return an error so the caller can do the Right Thing.
- */
- (*len) = nbytes;
- return APR_EOF;
- }
- }
- }
- else {
- /* just trailer bytes... use writev()
- */
- rv = writev(sock->socketdes,
- hdtr->trailers,
- hdtr->numtrailers);
- if (rv > 0) {
- nbytes = rv;
- rv = 0;
- }
- else {
- nbytes = 0;
- }
- }
- if ((rv == -1) && (errno == EAGAIN)
- && (sock->timeout > 0)) {
- fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
- } while (rv == -1 && (errno == EINTR || errno == EAGAIN));
- (*len) = nbytes;
- if (rv == -1) {
- return errno;
- }
- return APR_SUCCESS;
- }
- #elif defined(__FreeBSD__) || defined(__DragonFly__)
- /* Release 3.1 or greater */
- fspr_status_t fspr_socket_sendfile(fspr_socket_t * sock, fspr_file_t * file,
- fspr_hdtr_t * hdtr, fspr_off_t * offset,
- fspr_size_t * len, fspr_int32_t flags)
- {
- off_t nbytes = 0;
- int rv;
- #if defined(__FreeBSD_version) && __FreeBSD_version < 460001
- int i;
- #endif
- struct sf_hdtr headerstruct;
- fspr_size_t bytes_to_send = *len;
- /* Ignore flags for now. */
- flags = 0;
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- #if defined(__FreeBSD_version) && __FreeBSD_version < 460001
- else if (hdtr->numheaders) {
- /* On early versions of FreeBSD sendfile, the number of bytes to send
- * must include the length of the headers. Don't look at the man page
- * for this :( Instead, look at the the logic in
- * src/sys/kern/uipc_syscalls::sendfile().
- *
- * This was fixed in the middle of 4.6-STABLE
- */
- for (i = 0; i < hdtr->numheaders; i++) {
- bytes_to_send += hdtr->headers[i].iov_len;
- }
- }
- #endif
- headerstruct.headers = hdtr->headers;
- headerstruct.hdr_cnt = hdtr->numheaders;
- headerstruct.trailers = hdtr->trailers;
- headerstruct.trl_cnt = hdtr->numtrailers;
- /* FreeBSD can send the headers/footers as part of the system call */
- do {
- if (sock->options & APR_INCOMPLETE_WRITE) {
- fspr_status_t arv;
- sock->options &= ~APR_INCOMPLETE_WRITE;
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
- if (bytes_to_send) {
- /* We won't dare call sendfile() if we don't have
- * header or file bytes to send because bytes_to_send == 0
- * means send the whole file.
- */
- rv = sendfile(file->filedes, /* file to be sent */
- sock->socketdes, /* socket */
- *offset, /* where in the file to start */
- bytes_to_send, /* number of bytes to send */
- &headerstruct, /* Headers/footers */
- &nbytes, /* number of bytes written */
- flags); /* undefined, set to 0 */
- if (rv == -1) {
- if (errno == EAGAIN) {
- if (sock->timeout > 0) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- /* FreeBSD's sendfile can return -1/EAGAIN even if it
- * sent bytes. Sanitize the result so we get normal EAGAIN
- * semantics w.r.t. bytes sent.
- */
- if (nbytes) {
- /* normal exit for a big file & non-blocking io */
- (*len) = nbytes;
- return APR_SUCCESS;
- }
- }
- }
- else { /* rv == 0 (or the kernel is broken) */
- if (nbytes == 0) {
- /* Most likely the file got smaller after the stat.
- * Return an error so the caller can do the Right Thing.
- */
- (*len) = nbytes;
- return APR_EOF;
- }
- }
- }
- else {
- /* just trailer bytes... use writev()
- */
- rv = writev(sock->socketdes,
- hdtr->trailers,
- hdtr->numtrailers);
- if (rv > 0) {
- nbytes = rv;
- rv = 0;
- }
- else {
- nbytes = 0;
- }
- }
- if ((rv == -1) && (errno == EAGAIN)
- && (sock->timeout > 0)) {
- fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
- } while (rv == -1 && (errno == EINTR || errno == EAGAIN));
- (*len) = nbytes;
- if (rv == -1) {
- return errno;
- }
- return APR_SUCCESS;
- }
- #elif defined(__APPLE__)
- /*
- int
- sendfile(int fd, int s, off_t offset, off_t *len, struct sf_hdtr *hdtr,
- int flags);
- */
- fspr_status_t fspr_socket_sendfile(fspr_socket_t * sock, fspr_file_t * file,
- fspr_hdtr_t * hdtr, fspr_off_t * offset,
- fspr_size_t * len, fspr_int32_t flags)
- {
- int rv, i;
- struct sf_hdtr headerstruct;
- fspr_off_t bytes_to_send = *len;
-
- /* Ignore flags for now. */
- flags = 0;
-
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- else{
- if(hdtr->numheaders){
- for (i = 0; i < hdtr->numheaders; i++) {
- bytes_to_send += hdtr->headers[i].iov_len;
- }
- }
- else hdtr->headers=NULL; //for us having headers pointing to a valid buffer, but numheaders=0 constitues EINVAL..
- }
- headerstruct.headers = hdtr->headers;
- headerstruct.hdr_cnt = hdtr->numheaders;
- headerstruct.trailers = hdtr->trailers;
- headerstruct.trl_cnt = hdtr->numtrailers;
-
- do {
- if (sock->options & APR_INCOMPLETE_WRITE) {
- fspr_status_t arv;
- sock->options &= ~APR_INCOMPLETE_WRITE;
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
- if (bytes_to_send) {
- /* We won't dare call sendfile() if we don't have
- * header or file bytes to send because bytes_to_send == 0
- * means send the whole file.
- */
- int lflags = fcntl(sock->socketdes,F_GETFL,0);
- lflags &= ~O_NONBLOCK;
- fcntl(sock->socketdes,F_SETFL,lflags);
- rv = sendfile(file->filedes, /* file to be sent */
- sock->socketdes, /* socket */
- *offset, /* where in the file to start */
- &bytes_to_send, /* number of bytes to send */
- &headerstruct, /* Headers/footers */
- flags); /* undefined, set to 0 */
- lflags |= O_NONBLOCK;
- fcntl(sock->socketdes,F_SETFL,lflags);
- if (rv == -1) {
- if (errno == EAGAIN) {
- if (sock->timeout > 0) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- /* FreeBSD's sendfile can return -1/EAGAIN even if it
- * sent bytes. Sanitize the result so we get normal EAGAIN
- * semantics w.r.t. bytes sent.
- */
- if (bytes_to_send) {
- /* normal exit for a big file & non-blocking io */
- (*len) = bytes_to_send;
- return APR_SUCCESS;
- }
- }
- }
- else { /* rv == 0 (or the kernel is broken) */
- if (bytes_to_send == 0) {
- /* Most likely the file got smaller after the stat.
- * Return an error so the caller can do the Right Thing.
- */
- (*len) = bytes_to_send;
- return APR_EOF;
- }
- }
- }
- else {
- /* just trailer bytes... use writev()
- */
- rv = writev(sock->socketdes,
- hdtr->trailers,
- hdtr->numtrailers);
- if (rv > 0) {
- bytes_to_send = rv;
- rv = 0;
- }
- else {
- bytes_to_send = 0;
- }
- }
- if ((rv == -1) && (errno == EAGAIN)
- && (sock->timeout > 0)) {
- fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
- } while (rv == -1 && (errno == EINTR || errno == EAGAIN));
-
- (*len) = bytes_to_send;
- if (rv == -1) {
- return errno;
- }
- return APR_SUCCESS;
- }
- #elif defined(__hpux) || defined(__hpux__)
- /* HP cc in ANSI mode defines __hpux; gcc defines __hpux__ */
- /* HP-UX Version 10.30 or greater
- * (no worries, because we only get here if autoconfiguration found sendfile)
- */
- /* ssize_t sendfile(int s, int fd, off_t offset, size_t nbytes,
- * const struct iovec *hdtrl, int flags);
- *
- * nbytes is the number of bytes to send just from the file; as with FreeBSD,
- * if nbytes == 0, the rest of the file (from offset) is sent
- */
- fspr_status_t fspr_socket_sendfile(fspr_socket_t *sock, fspr_file_t *file,
- fspr_hdtr_t *hdtr, fspr_off_t *offset,
- fspr_size_t *len, fspr_int32_t flags)
- {
- int i;
- fspr_ssize_t rc;
- fspr_size_t nbytes = *len, headerlen, trailerlen;
- struct iovec hdtrarray[2];
- char *headerbuf, *trailerbuf;
- #if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILE64)
- /* later HP-UXes have a sendfile64() */
- #define sendfile sendfile64
- fspr_off_t off = *offset;
- #elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
- /* HP-UX 11.00 doesn't have a sendfile64(): fail if trying to send
- * past the 2Gb limit */
- off_t off;
- if ((fspr_int64_t)*offset + *len > INT_MAX) {
- return EINVAL;
- }
- off = *offset;
- #else
- fspr_off_t off = *offset;
- #endif
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- /* Ignore flags for now. */
- flags = 0;
- /* HP-UX can only send one header iovec and one footer iovec; try to
- * only allocate storage to combine input iovecs when we really have to
- */
- switch(hdtr->numheaders) {
- case 0:
- hdtrarray[0].iov_base = NULL;
- hdtrarray[0].iov_len = 0;
- break;
- case 1:
- hdtrarray[0] = hdtr->headers[0];
- break;
- default:
- headerlen = 0;
- for (i = 0; i < hdtr->numheaders; i++) {
- headerlen += hdtr->headers[i].iov_len;
- }
- /* XXX: BUHHH? wow, what a memory leak! */
- headerbuf = hdtrarray[0].iov_base = fspr_palloc(sock->pool, headerlen);
- hdtrarray[0].iov_len = headerlen;
- for (i = 0; i < hdtr->numheaders; i++) {
- memcpy(headerbuf, hdtr->headers[i].iov_base,
- hdtr->headers[i].iov_len);
- headerbuf += hdtr->headers[i].iov_len;
- }
- }
- switch(hdtr->numtrailers) {
- case 0:
- hdtrarray[1].iov_base = NULL;
- hdtrarray[1].iov_len = 0;
- break;
- case 1:
- hdtrarray[1] = hdtr->trailers[0];
- break;
- default:
- trailerlen = 0;
- for (i = 0; i < hdtr->numtrailers; i++) {
- trailerlen += hdtr->trailers[i].iov_len;
- }
- /* XXX: BUHHH? wow, what a memory leak! */
- trailerbuf = hdtrarray[1].iov_base = fspr_palloc(sock->pool, trailerlen);
- hdtrarray[1].iov_len = trailerlen;
- for (i = 0; i < hdtr->numtrailers; i++) {
- memcpy(trailerbuf, hdtr->trailers[i].iov_base,
- hdtr->trailers[i].iov_len);
- trailerbuf += hdtr->trailers[i].iov_len;
- }
- }
- do {
- if (nbytes) { /* any bytes to send from the file? */
- rc = sendfile(sock->socketdes, /* socket */
- file->filedes, /* file descriptor to send */
- off, /* where in the file to start */
- nbytes, /* number of bytes to send from file */
- hdtrarray, /* Headers/footers */
- flags); /* undefined, set to 0 */
- }
- else { /* we can't call sendfile() with no bytes to send from the file */
- rc = writev(sock->socketdes, hdtrarray, 2);
- }
- } while (rc == -1 && errno == EINTR);
- while ((rc == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- fspr_status_t arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- else {
- do {
- if (nbytes) {
- rc = sendfile(sock->socketdes, /* socket */
- file->filedes, /* file descriptor to send */
- off, /* where in the file to start */
- nbytes, /* number of bytes to send from file */
- hdtrarray, /* Headers/footers */
- flags); /* undefined, set to 0 */
- }
- else { /* we can't call sendfile() with no bytes to send from the file */
- rc = writev(sock->socketdes, hdtrarray, 2);
- }
- } while (rc == -1 && errno == EINTR);
- }
- }
- if (rc == -1) {
- *len = 0;
- return errno;
- }
- /* Set len to the number of bytes written */
- *len = rc;
- return APR_SUCCESS;
- }
- #elif defined(_AIX) || defined(__MVS__)
- /* AIX and OS/390 have the same send_file() interface.
- *
- * subtle differences:
- * AIX doesn't update the file ptr but OS/390 does
- *
- * availability (correctly determined by autoconf):
- *
- * AIX - version 4.3.2 with APAR IX85388, or version 4.3.3 and above
- * OS/390 - V2R7 and above
- */
- fspr_status_t fspr_socket_sendfile(fspr_socket_t * sock, fspr_file_t * file,
- fspr_hdtr_t * hdtr, fspr_off_t * offset,
- fspr_size_t * len, fspr_int32_t flags)
- {
- int i, ptr, rv = 0;
- void * hbuf=NULL, * tbuf=NULL;
- fspr_status_t arv;
- struct sf_parms parms;
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- /* Ignore flags for now. */
- flags = 0;
- /* word to the wise: by default, AIX stores files sent by send_file()
- * in the network buffer cache... there are supposedly scenarios
- * where the most recent copy of the file won't be sent, but I can't
- * recreate the potential problem, perhaps because of the way we
- * use send_file()... if you suspect such a problem, try turning
- * on the SF_SYNC_CACHE flag
- */
- /* AIX can also send the headers/footers as part of the system call */
- parms.header_length = 0;
- if (hdtr && hdtr->numheaders) {
- if (hdtr->numheaders == 1) {
- parms.header_data = hdtr->headers[0].iov_base;
- parms.header_length = hdtr->headers[0].iov_len;
- }
- else {
- for (i = 0; i < hdtr->numheaders; i++) {
- parms.header_length += hdtr->headers[i].iov_len;
- }
- #if 0
- /* Keepalives make fspr_palloc a bad idea */
- hbuf = malloc(parms.header_length);
- #else
- /* but headers are small, so maybe we can hold on to the
- * memory for the life of the socket...
- */
- hbuf = fspr_palloc(sock->pool, parms.header_length);
- #endif
- ptr = 0;
- for (i = 0; i < hdtr->numheaders; i++) {
- memcpy((char *)hbuf + ptr, hdtr->headers[i].iov_base,
- hdtr->headers[i].iov_len);
- ptr += hdtr->headers[i].iov_len;
- }
- parms.header_data = hbuf;
- }
- }
- else parms.header_data = NULL;
- parms.trailer_length = 0;
- if (hdtr && hdtr->numtrailers) {
- if (hdtr->numtrailers == 1) {
- parms.trailer_data = hdtr->trailers[0].iov_base;
- parms.trailer_length = hdtr->trailers[0].iov_len;
- }
- else {
- for (i = 0; i < hdtr->numtrailers; i++) {
- parms.trailer_length += hdtr->trailers[i].iov_len;
- }
- #if 0
- /* Keepalives make fspr_palloc a bad idea */
- tbuf = malloc(parms.trailer_length);
- #else
- tbuf = fspr_palloc(sock->pool, parms.trailer_length);
- #endif
- ptr = 0;
- for (i = 0; i < hdtr->numtrailers; i++) {
- memcpy((char *)tbuf + ptr, hdtr->trailers[i].iov_base,
- hdtr->trailers[i].iov_len);
- ptr += hdtr->trailers[i].iov_len;
- }
- parms.trailer_data = tbuf;
- }
- }
- else {
- parms.trailer_data = NULL;
- }
- /* Whew! Headers and trailers set up. Now for the file data */
- parms.file_descriptor = file->filedes;
- parms.file_offset = *offset;
- parms.file_bytes = *len;
- /* O.K. All set up now. Let's go to town */
- if (sock->options & APR_INCOMPLETE_WRITE) {
- sock->options &= ~APR_INCOMPLETE_WRITE;
- goto do_select;
- }
- do {
- rv = send_file(&(sock->socketdes), /* socket */
- &(parms), /* all data */
- flags); /* flags */
- } while (rv == -1 && errno == EINTR);
- while ((rv == -1) && (errno == EAGAIN || errno == EWOULDBLOCK)
- && (sock->timeout > 0)) {
- do_select:
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- else {
- do {
- rv = send_file(&(sock->socketdes), /* socket */
- &(parms), /* all data */
- flags); /* flags */
- } while (rv == -1 && errno == EINTR);
- }
- }
- (*len) = parms.bytes_sent;
- #if 0
- /* Clean up after ourselves */
- if(hbuf) free(hbuf);
- if(tbuf) free(tbuf);
- #endif
- if (rv == -1) {
- return errno;
- }
- if ((sock->timeout > 0)
- && (parms.bytes_sent
- < (parms.file_bytes + parms.header_length + parms.trailer_length))) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- return APR_SUCCESS;
- }
- #elif defined(__osf__) && defined (__alpha)
- /* Tru64's sendfile implementation doesn't work, and we need to make sure that
- * we don't use it until it is fixed. If it is used as it is now, it will
- * hang the machine and the only way to fix it is a reboot.
- */
- #elif defined(HAVE_SENDFILEV)
- /* Solaris 8's sendfilev() interface
- *
- * SFV_FD_SELF refers to our memory space.
- *
- * Required Sparc patches (or newer):
- * 111297-01, 108528-09, 109472-06, 109234-03, 108995-02, 111295-01, 109025-03,
- * 108991-13
- * Required x86 patches (or newer):
- * 111298-01, 108529-09, 109473-06, 109235-04, 108996-02, 111296-01, 109026-04,
- * 108992-13
- */
- #if APR_HAS_LARGE_FILES && defined(HAVE_SENDFILEV64)
- #define sendfilevec_t sendfilevec64_t
- #define sendfilev sendfilev64
- #endif
- fspr_status_t fspr_socket_sendfile(fspr_socket_t *sock, fspr_file_t *file,
- fspr_hdtr_t *hdtr, fspr_off_t *offset,
- fspr_size_t *len, fspr_int32_t flags)
- {
- fspr_status_t rv, arv;
- fspr_size_t nbytes;
- sendfilevec_t *sfv;
- int vecs, curvec, i, repeat;
- fspr_size_t requested_len = 0;
- if (!hdtr) {
- hdtr = &no_hdtr;
- }
- /* Ignore flags for now. */
- flags = 0;
- /* Calculate how much space we need. */
- vecs = hdtr->numheaders + hdtr->numtrailers + 1;
- sfv = fspr_palloc(sock->pool, sizeof(sendfilevec_t) * vecs);
- curvec = 0;
- /* Add the headers */
- for (i = 0; i < hdtr->numheaders; i++, curvec++) {
- sfv[curvec].sfv_fd = SFV_FD_SELF;
- sfv[curvec].sfv_flag = 0;
- /* Cast to unsigned long to prevent sign extension of the
- * pointer value for the LFS case; see PR 39463. */
- sfv[curvec].sfv_off = (unsigned long)hdtr->headers[i].iov_base;
- sfv[curvec].sfv_len = hdtr->headers[i].iov_len;
- requested_len += sfv[curvec].sfv_len;
- }
- /* If the len is 0, we skip the file. */
- if (*len)
- {
- sfv[curvec].sfv_fd = file->filedes;
- sfv[curvec].sfv_flag = 0;
- sfv[curvec].sfv_off = *offset;
- sfv[curvec].sfv_len = *len;
- requested_len += sfv[curvec].sfv_len;
- curvec++;
- }
- else {
- vecs--;
- }
- /* Add the footers */
- for (i = 0; i < hdtr->numtrailers; i++, curvec++) {
- sfv[curvec].sfv_fd = SFV_FD_SELF;
- sfv[curvec].sfv_flag = 0;
- sfv[curvec].sfv_off = (unsigned long)hdtr->trailers[i].iov_base;
- sfv[curvec].sfv_len = hdtr->trailers[i].iov_len;
- requested_len += sfv[curvec].sfv_len;
- }
- /* If the last write couldn't send all the requested data,
- * wait for the socket to become writable before proceeding
- */
- if (sock->options & APR_INCOMPLETE_WRITE) {
- sock->options &= ~APR_INCOMPLETE_WRITE;
- arv = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (arv != APR_SUCCESS) {
- *len = 0;
- return arv;
- }
- }
-
- /* Actually do the sendfilev
- *
- * Solaris may return -1/EAGAIN even if it sent bytes on a non-block sock.
- *
- * If no bytes were originally sent (nbytes == 0) and we are on a TIMEOUT
- * socket (which as far as the OS is concerned is a non-blocking socket),
- * we want to retry after waiting for the other side to read the data (as
- * determined by poll). Once it is clear to send, we want to retry
- * sending the sendfilevec_t once more.
- */
- arv = 0;
- do {
- /* Clear out the repeat */
- repeat = 0;
- /* socket, vecs, number of vecs, bytes written */
- rv = sendfilev(sock->socketdes, sfv, vecs, &nbytes);
- if (rv == -1 && errno == EAGAIN) {
- if (nbytes) {
- rv = 0;
- }
- else if (!arv && (sock->timeout > 0)) {
- fspr_status_t t = fspr_wait_for_io_or_timeout(NULL, sock, 0);
- if (t != APR_SUCCESS) {
- *len = 0;
- return t;
- }
- arv = 1;
- repeat = 1;
- }
- }
- } while ((rv == -1 && errno == EINTR) || repeat);
- if (rv == -1) {
- *len = 0;
- return errno;
- }
- /* Update how much we sent */
- *len = nbytes;
- if ((sock->timeout > 0) && (*len < requested_len)) {
- sock->options |= APR_INCOMPLETE_WRITE;
- }
- return APR_SUCCESS;
- }
- #else
- #error APR has detected sendfile on your system, but nobody has written a
- #error version of it for APR yet. To get past this, either write
- #error fspr_socket_sendfile or change APR_HAS_SENDFILE in apr.h to 0.
- #endif /* __linux__, __FreeBSD__, __DragonFly__, __HPUX__, _AIX, __MVS__,
- Tru64/OSF1 */
- #endif /* APR_HAS_SENDFILE */
|