source: branches/samba-3.0/source/lib/sendfile.c@ 165

Last change on this file since 165 was 161, checked in by Paul Smedley, 17 years ago

Update source to 3.0.34

File size: 11.9 KB
Line 
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
37ssize_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
96extern 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
103ssize_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;