r174: Win95 registry files (like USER.DAT) can now be partially parsed
[kai/samba.git] / source / lib / sendfile.c
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                         return -1;
70                 if (nwritten == 0)
71                         return -1; /* I think we're at EOF here... */
72                 total -= nwritten;
73         }
74         return count + hdr_len;
75 }
76
77 #elif defined(LINUX_BROKEN_SENDFILE_API)
78
79 /*
80  * We must use explicit 32 bit types here. This code path means Linux
81  * won't do proper 64-bit sendfile. JRA.
82  */
83
84 extern int32 sendfile (int out_fd, int in_fd, int32 *offset, uint32 count);
85
86
87 #ifndef MSG_MORE
88 #define MSG_MORE 0x8000
89 #endif
90
91 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
92 {
93         size_t total=0;
94         ssize_t ret;
95         ssize_t hdr_len = 0;
96         uint32 small_total = 0;
97         int32 small_offset;
98
99         /* 
100          * Fix for broken Linux 2.4 systems with no working sendfile64().
101          * If the offset+count > 2 GB then pretend we don't have the
102          * system call sendfile at all. The upper layer catches this
103          * and uses a normal read. JRA.
104          */
105
106         if ((sizeof(SMB_OFF_T) >= 8) && (offset + count > (SMB_OFF_T)0x7FFFFFFF)) {
107                 errno = ENOSYS;
108                 return -1;
109         }
110
111         /*
112          * Send the header first.
113          * Use MSG_MORE to cork the TCP output until sendfile is called.
114          */
115
116         if (header) {
117                 hdr_len = header->length;
118                 while (total < hdr_len) {
119                         ret = sys_send(tofd, header->data + total,hdr_len - total, MSG_MORE);
120                         if (ret == -1)
121                                 return -1;
122                         total += ret;
123                 }
124         }
125
126         small_total = (uint32)count;
127         small_offset = (int32)offset;
128
129         while (small_total) {
130                 int32 nwritten;
131                 do {
132                         nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
133                 } while (nwritten == -1 && errno == EINTR);
134                 if (nwritten == -1)
135                         return -1;
136                 if (nwritten == 0)
137                         return -1; /* I think we're at EOF here... */
138                 small_total -= nwritten;
139         }
140         return count + hdr_len;
141 }
142
143
144 #elif defined(SOLARIS_SENDFILE_API)
145
146 /*
147  * Solaris sendfile code written by Pierre Belanger <belanger@pobox.com>.
148  */
149
150 #include <sys/sendfile.h>
151
152 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
153 {
154         int sfvcnt;
155         size_t total, xferred;
156         struct sendfilevec vec[2];
157         ssize_t hdr_len = 0;
158
159         if (header) {
160                 sfvcnt = 2;
161
162                 vec[0].sfv_fd = SFV_FD_SELF;
163                 vec[0].sfv_flag = 0;
164                 vec[0].sfv_off = header->data;
165                 vec[0].sfv_len = hdr_len = header->length;
166
167                 vec[1].sfv_fd = fromfd;
168                 vec[1].sfv_flag = 0;
169                 vec[1].sfv_off = offset;
170                 vec[1].sfv_len = count;
171
172         } else {
173                 sfvcnt = 1;
174
175                 vec[0].sfv_fd = fromfd;
176                 vec[0].sfv_flag = 0;
177                 vec[0].sfv_off = offset;
178                 vec[0].sfv_len = count;
179         }
180
181         total = count + hdr_len;
182
183         while (total) {
184                 ssize_t nwritten;
185
186                 /*
187                  * Although not listed in the API error returns, this is almost certainly
188                  * a slow system call and will be interrupted by a signal with EINTR. JRA.
189                  */
190
191                 xferred = 0;
192
193 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILEV64)
194                         nwritten = sendfilev64(tofd, vec, sfvcnt, &xferred);
195 #else
196                         nwritten = sendfilev(tofd, vec, sfvcnt, &xferred);
197 #endif
198                 if (nwritten == -1 && errno == EINTR) {
199                         if (xferred == 0)
200                                 continue; /* Nothing written yet. */
201                         else
202                                 nwritten = xferred;
203                 }
204
205                 if (nwritten == -1)
206                         return -1;
207                 if (nwritten == 0)
208                         return -1; /* I think we're at EOF here... */
209
210                 /*
211                  * If this was a short (signal interrupted) write we may need
212                  * to subtract it from the header data, or null out the header
213                  * data altogether if we wrote more than vec[0].sfv_len bytes.
214                  * We move vec[1].* to vec[0].* and set sfvcnt to 1
215                  */
216
217                 if (sfvcnt == 2 && nwritten >= vec[0].sfv_len) {
218                         vec[1].sfv_off += nwritten - vec[0].sfv_len;
219                         vec[1].sfv_len -= nwritten - vec[0].sfv_len;
220
221                         /* Move vec[1].* to vec[0].* and set sfvcnt to 1 */
222                         vec[0] = vec[1];
223                         sfvcnt = 1;
224                 } else {
225                         vec[0].sfv_off += nwritten;
226                         vec[0].sfv_len -= nwritten;
227                 }
228                 total -= nwritten;
229         }
230         return count + hdr_len;
231 }
232
233 #elif defined(HPUX_SENDFILE_API)
234
235 #include <sys/socket.h>
236 #include <sys/uio.h>
237
238 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
239 {
240         size_t total=0;
241         struct iovec hdtrl[2];
242         size_t hdr_len = 0;
243
244         if (header) {
245                 /* Set up the header/trailer iovec. */
246                 hdtrl[0].iov_base = header->data;
247                 hdtrl[0].iov_len = hdr_len = header->length;
248         } else {
249                 hdtrl[0].iov_base = NULL;
250                 hdtrl[0].iov_len = hdr_len = 0;
251         }
252         hdtrl[1].iov_base = NULL;
253         hdtrl[1].iov_base = 0;
254
255         total = count;
256         while (total + hdtrl[0].iov_len) {
257                 ssize_t nwritten;
258
259                 /*
260                  * HPUX guarantees that if any data was written before
261                  * a signal interrupt then sendfile returns the number of
262                  * bytes written (which may be less than requested) not -1.
263                  * nwritten includes the header data sent.
264                  */
265
266                 do {
267 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_SENDFILE64)
268                         nwritten = sendfile64(tofd, fromfd, offset, total, &hdtrl[0], 0);
269 #else
270                         nwritten = sendfile(tofd, fromfd, offset, total, &hdtrl[0], 0);
271 #endif
272                 } while (nwritten == -1 && errno == EINTR);
273                 if (nwritten == -1)
274                         return -1;
275                 if (nwritten == 0)
276                         return -1; /* I think we're at EOF here... */
277
278                 /*
279                  * If this was a short (signal interrupted) write we may need
280                  * to subtract it from the header data, or null out the header
281                  * data altogether if we wrote more than hdtrl[0].iov_len bytes.
282                  * We change nwritten to be the number of file bytes written.
283                  */
284
285                 if (hdtrl[0].iov_base && hdtrl[0].iov_len) {
286                         if (nwritten >= hdtrl[0].iov_len) {
287                                 nwritten -= hdtrl[0].iov_len;
288                                 hdtrl[0].iov_base = NULL;
289                                 hdtrl[0].iov_len = 0;
290                         } else {
291                                 /* iov_base is defined as a void *... */
292                                 hdtrl[0].iov_base = ((char *)hdtrl[0].iov_base) + nwritten;
293                                 hdtrl[0].iov_len -= nwritten;
294                                 nwritten = 0;
295                         }
296                 }
297                 total -= nwritten;
298                 offset += nwritten;
299         }
300         return count + hdr_len;
301 }
302
303 #elif defined(FREEBSD_SENDFILE_API)
304
305 #include <sys/types.h>
306 #include <sys/socket.h>
307 #include <sys/uio.h>
308
309 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
310 {
311         size_t total=0;
312         struct sf_hdtr hdr;
313         struct iovec hdtrl;
314         size_t hdr_len = 0;
315
316         hdr.headers = &hdtrl;
317         hdr.hdr_cnt = 1;
318         hdr.trailers = NULL;
319         hdr.trl_cnt = 0;
320
321         /* Set up the header iovec. */
322         if (header) {
323                 hdtrl.iov_base = header->data;
324                 hdtrl.iov_len = hdr_len = header->length;
325         } else {
326                 hdtrl.iov_base = NULL;
327                 hdtrl.iov_len = 0;
328         }
329
330         total = count;
331         while (total + hdtrl.iov_len) {
332                 SMB_OFF_T nwritten;
333                 int ret;
334
335                 /*
336                  * FreeBSD sendfile returns 0 on success, -1 on error.
337                  * Remember, the tofd and fromfd are reversed..... :-).
338                  * nwritten includes the header data sent.
339                  */
340
341                 do {
342                         ret = sendfile(fromfd, tofd, offset, total, &hdr, &nwritten, 0);
343                 } while (ret == -1 && errno == EINTR);
344                 if (ret == -1)
345                         return -1;
346
347                 if (nwritten == 0)
348                         return -1; /* I think we're at EOF here... */
349
350                 /*
351                  * If this was a short (signal interrupted) write we may need
352                  * to subtract it from the header data, or null out the header
353                  * data altogether if we wrote more than hdtrl.iov_len bytes.
354                  * We change nwritten to be the number of file bytes written.
355                  */
356
357                 if (hdtrl.iov_base && hdtrl.iov_len) {
358                         if (nwritten >= hdtrl.iov_len) {
359                                 nwritten -= hdtrl.iov_len;
360                                 hdtrl.iov_base = NULL;
361                                 hdtrl.iov_len = 0;
362                         } else {
363                                 hdtrl.iov_base += nwritten;
364                                 hdtrl.iov_len -= nwritten;
365                                 nwritten = 0;
366                         }
367                 }
368                 total -= nwritten;
369                 offset += nwritten;
370         }
371         return count + hdr_len;
372 }
373
374 #else /* No sendfile implementation. Return error. */
375
376 ssize_t sys_sendfile(int tofd, int fromfd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
377 {
378         /* No sendfile syscall. */
379         errno = ENOSYS;
380         return -1;
381 }
382 #endif