[161] | 1 | /*
|
---|
| 2 | Unix SMB/Netbios implementation.
|
---|
| 3 | Version 2.2.x / 3.0.x
|
---|
| 4 | sendfile implementations.
|
---|
| 5 | Copyright (C) Jeremy Allison 2002.
|
---|
| 6 |
|
---|
| 7 | This program is free software; you can redistribute it and/or modify
|
---|
| 8 | it under the terms of the GNU General Public License as published by
|
---|
| 9 | the Free Software Foundation; either version 2 of the License, or
|
---|
| 10 | (at your option) any later version.
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program; if not, write to the Free Software
|
---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 19 | */
|
---|
| 20 |
|
---|
| 21 | /*
|
---|
| 22 | * This file handles the OS dependent sendfile implementations.
|
---|
| 23 | * The API is such that it returns -1 on error, else returns the
|
---|
| 24 | * number of bytes written.
|
---|
| 25 | */
|
---|
| 26 |
|
---|
| 27 | #include "includes.h"
|
---|
| 28 |
|
---|
| 29 | #if defined(LINUX_SENDFILE_API)
|
---|
| 30 |
|
---|
| 31 | #include <sys/sendfile.h>
|
---|
| 32 |
|
---|
| 33 | #ifndef MSG_MORE
|
---|
| 34 | #define MSG_MORE 0x8000
|
---|
| 35 | #endif
|
---|
| 36 |
|
---|
| 37 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 38 | {
|
---|
| 39 | size_t total=0;
|
---|
| 40 | ssize_t ret;
|
---|
| 41 | size_t hdr_len = 0;
|
---|
| 42 |
|
---|
| 43 | /*
|
---|
| 44 | * Send the header first.
|
---|
| 45 | * Use MSG_MORE to cork the TCP output until sendfile is called.
|
---|
| 46 | */
|
---|
| 47 |
|
---|
| 48 | if (header) {
|
---|
| 49 | hdr_len = header->length;
|
---|
| 50 | while (total < hdr_len) {
|
---|
| 51 | ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
|
---|
| 52 | if (ret == -1)
|
---|
| 53 | return -1;
|
---|
| 54 | total += ret;
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | total = count;
|
---|
| 59 | while (total) {
|
---|
| 60 | ssize_t nwritten;
|
---|
| 61 | do {
|
---|
| 62 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
|
---|
| 63 | nwritten = sendfile64(tofd, fromfd, &offset, total);
|
---|
| 64 | #else
|
---|
| 65 | nwritten = sendfile(tofd, fromfd, &offset, total);
|
---|
| 66 | #endif
|
---|
| 67 | } while (nwritten == -1 && errno == EINTR);
|
---|
| 68 | if (nwritten == -1) {
|
---|
| 69 | if (errno == ENOSYS || errno == EINVAL) {
|
---|
| 70 | /* Ok - we're in a world of pain here. We just sent
|
---|
| 71 | * the header, but the sendfile failed. We have to
|
---|
| 72 | * emulate the sendfile at an upper layer before we
|
---|
| 73 | * disable it's use. So we do something really ugly.
|
---|
| 74 | * We set the errno to a strange value so we can detect
|
---|
| 75 | * this at the upper level and take care of it without
|
---|
| 76 | * layer violation. JRA.
|
---|
| 77 | */
|
---|
| 78 | errno = EINTR; /* Normally we can never return this. */
|
---|
| 79 | }
|
---|
| 80 | return -1;
|
---|
| 81 | }
|
---|
| 82 | if (nwritten == 0)
|
---|
| 83 | return -1; /* I think we're at EOF here... */
|
---|
| 84 | total -= nwritten;
|
---|
| 85 | }
|
---|
| 86 | return count + hdr_len;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | #elif defined(LINUX_BROKEN_SENDFILE_API)
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | * We must use explicit 32 bit types here. This code path means Linux
|
---|
| 93 | * won't do proper 64-bit sendfile. JRA.
|
---|
| 94 | */
|
---|
| 95 |
|
---|
| 96 | extern int32 sendfile (int out_fd, int in_fd, int32 *offset, uint32 count);
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | #ifndef MSG_MORE
|
---|
| 100 | #define MSG_MORE 0x8000
|
---|
| 101 | #endif
|
---|
| 102 |
|
---|
| 103 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 104 | {
|
---|
| 105 | size_t total=0;
|
---|
| 106 | ssize_t ret;
|
---|
| 107 | ssize_t hdr_len = 0;
|
---|
| 108 | uint32 small_total = 0;
|
---|
| 109 | int32 small_offset;
|
---|
| 110 |
|
---|
| 111 | /*
|
---|
| 112 | * Fix for broken Linux 2.4 systems with no working sendfile64().
|
---|
| 113 | * If the offset+count > 2 GB then pretend we don't have the
|
---|
| 114 | * system call sendfile at all. The upper layer catches this
|
---|
| 115 | * and uses a normal read. JRA.
|
---|
| 116 | */
|
---|
| 117 |
|
---|
| 118 | if ((sizeof(SMB_OFF_T) >= 8) && (offset + count > (SMB_OFF_T)0x7FFFFFFF)) {
|
---|
| 119 | errno = ENOSYS;
|
---|
| 120 | return -1;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * Send the header first.
|
---|
| 125 | * Use MSG_MORE to cork the TCP output until sendfile is called.
|
---|
| 126 | */
|
---|
| 127 |
|
---|
| 128 | if (header) {
|
---|
| 129 | hdr_len = header->length;
|
---|
| 130 | while (total < hdr_len) {
|
---|
| 131 | ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
|
---|
| 132 | if (ret == -1)
|
---|
| 133 | return -1;
|
---|
| 134 | total += ret;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | small_total = (uint32)count;
|
---|
| 139 | small_offset = (int32)offset;
|
---|
| 140 |
|
---|
| 141 | while (small_total) {
|
---|
| 142 | int32 nwritten;
|
---|
| 143 | do {
|
---|
| 144 | nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
|
---|
| 145 | } while (nwritten == -1 && errno == EINTR);
|
---|
| 146 | if (nwritten == -1) {
|
---|
| 147 | if (errno == ENOSYS || errno == EINVAL) {
|
---|
| 148 | /* Ok - we're in a world of pain here. We just sent
|
---|
| 149 | * the header, but the sendfile failed. We have to
|
---|
| 150 | * emulate the sendfile at an upper layer before we
|
---|
| 151 | * disable it's use. So we do something really ugly.
|
---|
| 152 | * We set the errno to a strange value so we can detect
|
---|
| 153 | * this at the upper level and take care of it without
|
---|
| 154 | * layer violation. JRA.
|
---|
| 155 | */
|
---|
| 156 | errno = EINTR; /* Normally we can never return this. */
|
---|
| 157 | }
|
---|
| 158 | return -1;
|
---|
| 159 | }
|
---|
| 160 | if (nwritten == 0)
|
---|
| 161 | return -1; /* I think we're at EOF here... */
|
---|
| 162 | small_total -= nwritten;
|
---|
| 163 | }
|
---|
| 164 | return count + hdr_len;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 | #elif defined(SOLARIS_SENDFILE_API)
|
---|
| 169 |
|
---|
| 170 | /*
|
---|
| 171 | * Solaris sendfile code written by Pierre Belanger <belanger@pobox.com>.
|
---|
| 172 | */
|
---|
| 173 |
|
---|
| 174 | #include <sys/sendfile.h>
|
---|
| 175 |
|
---|
| 176 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 177 | {
|
---|
| 178 | int sfvcnt;
|
---|
| 179 | size_t total, xferred;
|
---|
| 180 | struct sendfilevec vec[2];
|
---|
| 181 | ssize_t hdr_len = 0;
|
---|
| 182 |
|
---|
| 183 | if (header) {
|
---|
| 184 | sfvcnt = 2;
|
---|
| 185 |
|
---|
| 186 | vec[0].sfv_fd = SFV_FD_SELF;
|
---|
| 187 | vec[0].sfv_flag = 0;
|
---|
| 188 | vec[0].sfv_off = (off_t)header->data;
|
---|
| 189 | vec[0].sfv_len = hdr_len = header->length;
|
---|
| 190 |
|
---|
| 191 | vec[1].sfv_fd = fromfd;
|
---|
| 192 | vec[1].sfv_flag = 0;
|
---|
| 193 | vec[1].sfv_off = offset;
|
---|
| 194 | vec[1].sfv_len = count;
|
---|
| 195 |
|
---|
| 196 | } else {
|
---|
| 197 | sfvcnt = 1;
|
---|
| 198 |
|
---|
| 199 | vec[0].sfv_fd = fromfd;
|
---|
| 200 | vec[0].sfv_flag = 0;
|
---|
| 201 | vec[0].sfv_off = offset;
|
---|
| 202 | vec[0].sfv_len = count;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | total = count + hdr_len;
|
---|
| 206 |
|
---|
| 207 | while (total) {
|
---|
| 208 | ssize_t nwritten;
|
---|
| 209 |
|
---|
| 210 | /*
|
---|
| 211 | * Although not listed in the API error returns, this is almost certainly
|
---|
| 212 | * a slow system call and will be interrupted by a signal with EINTR. JRA.
|
---|
| 213 | */
|
---|
| 214 |
|
---|
| 215 | xferred = 0;
|
---|
| 216 |
|
---|
| 217 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILEV64)
|
---|
| 218 | nwritten = sendfilev64(tofd, vec, sfvcnt, &xferred);
|
---|
| 219 | #else
|
---|
| 220 | nwritten = sendfilev(tofd, vec, sfvcnt, &xferred);
|
---|
| 221 | #endif
|
---|
| 222 | if (nwritten == -1 && errno == EINTR) {
|
---|
| 223 | if (xferred == 0)
|
---|
| 224 | continue; /* Nothing written yet. */
|
---|
| 225 | else
|
---|
| 226 | nwritten = xferred;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | if (nwritten == -1)
|
---|
| 230 | return -1;
|
---|
| 231 | if (nwritten == 0)
|
---|
| 232 | return -1; /* I think we're at EOF here... */
|
---|
| 233 |
|
---|
| 234 | /*
|
---|
| 235 | * If this was a short (signal interrupted) write we may need
|
---|
| 236 | * to subtract it from the header data, or null out the header
|
---|
| 237 | * data altogether if we wrote more than vec[0].sfv_len bytes.
|
---|
| 238 | * We move vec[1].* to vec[0].* and set sfvcnt to 1
|
---|
| 239 | */
|
---|
| 240 |
|
---|
| 241 | if (sfvcnt == 2 && nwritten >= vec[0].sfv_len) {
|
---|
| 242 | vec[1].sfv_off += nwritten - vec[0].sfv_len;
|
---|
| 243 | vec[1].sfv_len -= nwritten - vec[0].sfv_len;
|
---|
| 244 |
|
---|
| 245 | /* Move vec[1].* to vec[0].* and set sfvcnt to 1 */
|
---|
| 246 | vec[0] = vec[1];
|
---|
| 247 | sfvcnt = 1;
|
---|
| 248 | } else {
|
---|
| 249 | vec[0].sfv_off += nwritten;
|
---|
| 250 | vec[0].sfv_len -= nwritten;
|
---|
| 251 | }
|
---|
| 252 | total -= nwritten;
|
---|
| 253 | }
|
---|
| 254 | return count + hdr_len;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | #elif defined(HPUX_SENDFILE_API)
|
---|
| 258 |
|
---|
| 259 | #include <sys/socket.h>
|
---|
| 260 | #include <sys/uio.h>
|
---|
| 261 |
|
---|
| 262 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 263 | {
|
---|
| 264 | size_t total=0;
|
---|
| 265 | struct iovec hdtrl[2];
|
---|
| 266 | size_t hdr_len = 0;
|
---|
| 267 |
|
---|
| 268 | if (header) {
|
---|
| 269 | /* Set up the header/trailer iovec. */
|
---|
| 270 | hdtrl[0].iov_base = header->data;
|
---|
| 271 | hdtrl[0].iov_len = hdr_len = header->length;
|
---|
| 272 | } else {
|
---|
| 273 | hdtrl[0].iov_base = NULL;
|
---|
| 274 | hdtrl[0].iov_len = hdr_len = 0;
|
---|
| 275 | }
|
---|
| 276 | hdtrl[1].iov_base = NULL;
|
---|
| 277 | hdtrl[1].iov_len = 0;
|
---|
| 278 |
|
---|
| 279 | total = count;
|
---|
| 280 | while (total + hdtrl[0].iov_len) {
|
---|
| 281 | ssize_t nwritten;
|
---|
| 282 |
|
---|
| 283 | /*
|
---|
| 284 | * HPUX guarantees that if any data was written before
|
---|
| 285 | * a signal interrupt then sendfile returns the number of
|
---|
| 286 | * bytes written (which may be less than requested) not -1.
|
---|
| 287 | * nwritten includes the header data sent.
|
---|
| 288 | */
|
---|
| 289 |
|
---|
| 290 | do {
|
---|
| 291 | #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
|
---|
| 292 | nwritten = sendfile64(tofd, fromfd, offset, total, &hdtrl[0], 0);
|
---|
| 293 | #else
|
---|
| 294 | nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0);
|
---|
| 295 | #endif
|
---|
| 296 | } while (nwritten == -1 && errno == EINTR);
|
---|
| 297 | if (nwritten == -1)
|
---|
| 298 | return -1;
|
---|
| 299 | if (nwritten == 0)
|
---|
| 300 | return -1; /* I think we're at EOF here... */
|
---|
| 301 |
|
---|
| 302 | /*
|
---|
| 303 | * If this was a short (signal interrupted) write we may need
|
---|
| 304 | * to subtract it from the header data, or null out the header
|
---|
| 305 | * data altogether if we wrote more than hdtrl[0].iov_len bytes.
|
---|
| 306 | * We change nwritten to be the number of file bytes written.
|
---|
| 307 | */
|
---|
| 308 |
|
---|
| 309 | if (hdtrl[0].iov_base && hdtrl[0].iov_len) {
|
---|
| 310 | if (nwritten >= hdtrl[0].iov_len) {
|
---|
| 311 | nwritten -= hdtrl[0].iov_len;
|
---|
| 312 | hdtrl[0].iov_base = NULL;
|
---|
| 313 | hdtrl[0].iov_len = 0;
|
---|
| 314 | } else {
|
---|
| 315 | /* iov_base is defined as a void *... */
|
---|
| 316 | hdtrl[0].iov_base = ((char *)hdtrl[0].iov_base) + nwritten;
|
---|
| 317 | hdtrl[0].iov_len -= nwritten;
|
---|
| 318 | nwritten = 0;
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 | total -= nwritten;
|
---|
| 322 | offset += nwritten;
|
---|
| 323 | }
|
---|
| 324 | return count + hdr_len;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | #elif defined(FREEBSD_SENDFILE_API)
|
---|
| 328 |
|
---|
| 329 | #include <sys/types.h>
|
---|
| 330 | #include <sys/socket.h>
|
---|
| 331 | #include <sys/uio.h>
|
---|
| 332 |
|
---|
| 333 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 334 | {
|
---|
| 335 | size_t total=0;
|
---|
| 336 | struct sf_hdtr hdr;
|
---|
| 337 | struct iovec hdtrl;
|
---|
| 338 | size_t hdr_len = 0;
|
---|
| 339 |
|
---|
| 340 | hdr.headers = &hdtrl;
|
---|
| 341 | hdr.hdr_cnt = 1;
|
---|
| 342 | hdr.trailers = NULL;
|
---|
| 343 | hdr.trl_cnt = 0;
|
---|
| 344 |
|
---|
| 345 | /* Set up the header iovec. */
|
---|
| 346 | if (header) {
|
---|
| 347 | hdtrl.iov_base = header->data;
|
---|
| 348 | hdtrl.iov_len = hdr_len = header->length;
|
---|
| 349 | } else {
|
---|
| 350 | hdtrl.iov_base = NULL;
|
---|
| 351 | hdtrl.iov_len = 0;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | total = count;
|
---|
| 355 | while (total + hdtrl.iov_len) {
|
---|
| 356 | SMB_OFF_T nwritten;
|
---|
| 357 | int ret;
|
---|
| 358 |
|
---|
| 359 | /*
|
---|
| 360 | * FreeBSD sendfile returns 0 on success, -1 on error.
|
---|
| 361 | * Remember, the tofd and fromfd are reversed..... :-).
|
---|
| 362 | * nwritten includes the header data sent.
|
---|
| 363 | */
|
---|
| 364 |
|
---|
| 365 | do {
|
---|
| 366 | ret = sendfile(fromfd, tofd, offset, total, &hdr, &nwritten, 0);
|
---|
| 367 | } while (ret == -1 && errno == EINTR);
|
---|
| 368 | if (ret == -1)
|
---|
| 369 | return -1;
|
---|
| 370 |
|
---|
| 371 | if (nwritten == 0)
|
---|
| 372 | return -1; /* I think we're at EOF here... */
|
---|
| 373 |
|
---|
| 374 | /*
|
---|
| 375 | * If this was a short (signal interrupted) write we may need
|
---|
| 376 | * to subtract it from the header data, or null out the header
|
---|
| 377 | * data altogether if we wrote more than hdtrl.iov_len bytes.
|
---|
| 378 | * We change nwritten to be the number of file bytes written.
|
---|
| 379 | */
|
---|
| 380 |
|
---|
| 381 | if (hdtrl.iov_base && hdtrl.iov_len) {
|
---|
| 382 | if (nwritten >= hdtrl.iov_len) {
|
---|
| 383 | nwritten -= hdtrl.iov_len;
|
---|
| 384 | hdtrl.iov_base = NULL;
|
---|
| 385 | hdtrl.iov_len = 0;
|
---|
| 386 | } else {
|
---|
| 387 | hdtrl.iov_base += nwritten;
|
---|
| 388 | hdtrl.iov_len -= nwritten;
|
---|
| 389 | nwritten = 0;
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 | total -= nwritten;
|
---|
| 393 | offset += nwritten;
|
---|
| 394 | }
|
---|
| 395 | return count + hdr_len;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | #elif defined(AIX_SENDFILE_API)
|
---|
| 399 |
|
---|
| 400 | /* BEGIN AIX SEND_FILE */
|
---|
| 401 |
|
---|
| 402 | /* Contributed by William Jojo <jojowil@hvcc.edu> */
|
---|
| 403 | #include <sys/socket.h>
|
---|
| 404 |
|
---|
| 405 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 406 | {
|
---|
| 407 | struct sf_parms hdtrl;
|
---|
| 408 |
|
---|
| 409 | /* Set up the header/trailer struct params. */
|
---|
| 410 | if (header) {
|
---|
| 411 | hdtrl.header_data = header->data;
|
---|
| 412 | hdtrl.header_length = header->length;
|
---|
| 413 | } else {
|
---|
| 414 | hdtrl.header_data = NULL;
|
---|
| 415 | hdtrl.header_length = 0;
|
---|
| 416 | }
|
---|
| 417 | hdtrl.trailer_data = NULL;
|
---|
| 418 | hdtrl.trailer_length = 0;
|
---|
| 419 |
|
---|
| 420 | hdtrl.file_descriptor = fromfd;
|
---|
| 421 | hdtrl.file_offset = offset;
|
---|
| 422 | hdtrl.file_bytes = count;
|
---|
| 423 |
|
---|
| 424 | while ( hdtrl.file_bytes + hdtrl.header_length ) {
|
---|
| 425 | ssize_t ret;
|
---|
| 426 |
|
---|
| 427 | /*
|
---|
| 428 | Return Value
|
---|
| 429 |
|
---|
| 430 | There are three possible return values from send_file:
|
---|
| 431 |
|
---|
| 432 | Value Description
|
---|
| 433 |
|
---|
| 434 | -1 an error has occurred, errno contains the error code.
|
---|
| 435 |
|
---|
| 436 | 0 the command has completed successfully.
|
---|
| 437 |
|
---|
| 438 | 1 the command was completed partially, some data has been
|
---|
| 439 | transmitted but the command has to return for some reason,
|
---|
| 440 | for example, the command was interrupted by signals.
|
---|
| 441 | */
|
---|
| 442 | do {
|
---|
| 443 | ret = send_file(&tofd, &hdtrl, 0);
|
---|
| 444 | } while ( (ret == 1) || (ret == -1 && errno == EINTR) );
|
---|
| 445 | if ( ret == -1 )
|
---|
| 446 | return -1;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | return count + header->length;
|
---|
| 450 | }
|
---|
| 451 | /* END AIX SEND_FILE */
|
---|
| 452 |
|
---|
| 453 | #else /* No sendfile implementation. Return error. */
|
---|
| 454 |
|
---|
| 455 | ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
|
---|
| 456 | {
|
---|
| 457 | /* No sendfile syscall. */
|
---|
| 458 | errno = ENOSYS;
|
---|
| 459 | return -1;
|
---|
| 460 | }
|
---|
| 461 | #endif
|
---|