Remove a few #ifdef EWOULDBLOCk
[samba.git] / source3 / lib / recvfile.c
1 /*
2  Unix SMB/Netbios implementation.
3  Version 3.2.x
4  recvfile implementations.
5  Copyright (C) Jeremy Allison 2007.
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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 /*
21  * This file handles the OS dependent recvfile implementations.
22  * The API is such that it returns -1 on error, else returns the
23  * number of bytes written.
24  */
25
26 #include "includes.h"
27 #include "system/filesys.h"
28
29 /* Do this on our own in TRANSFER_BUF_SIZE chunks.
30  * It's safe to make direct syscalls to lseek/write here
31  * as we're below the Samba vfs layer.
32  *
33  * Returns -1 on short reads from fromfd (read error)
34  * and sets errno.
35  *
36  * Returns number of bytes written to 'tofd'
37  * return != count then sets errno.
38  * Returns count if complete success.
39  */
40
41 #ifndef TRANSFER_BUF_SIZE
42 #define TRANSFER_BUF_SIZE (128*1024)
43 #endif
44
45 static ssize_t default_sys_recvfile(int fromfd,
46                         int tofd,
47                         off_t offset,
48                         size_t count)
49 {
50         int saved_errno = 0;
51         size_t total = 0;
52         size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
53         size_t total_written = 0;
54         char buffer[bufsize];
55
56         DEBUG(10,("default_sys_recvfile: from = %d, to = %d, "
57                 "offset=%.0f, count = %lu\n",
58                 fromfd, tofd, (double)offset,
59                 (unsigned long)count));
60
61         if (count == 0) {
62                 return 0;
63         }
64
65         if (tofd != -1 && offset != (off_t)-1) {
66                 if (lseek(tofd, offset, SEEK_SET) == -1) {
67                         if (errno != ESPIPE) {
68                                 return -1;
69                         }
70                 }
71         }
72
73         while (total < count) {
74                 size_t num_written = 0;
75                 ssize_t read_ret;
76                 size_t toread = MIN(bufsize,count - total);
77
78                 /*
79                  * Read from socket - ignore EINTR.
80                  * Can't use sys_read() as that also
81                  * ignores EAGAIN and EWOULDBLOCK.
82                  */
83                 do {
84                         read_ret = read(fromfd, buffer, toread);
85                 } while (read_ret == -1 && errno == EINTR);
86
87                 if (read_ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
88                         /*
89                          * fromfd socket is in non-blocking mode.
90                          * If we already read some and wrote
91                          * it successfully, return that.
92                          * Only return -1 if this is the first read
93                          * attempt. Caller will handle both cases.
94                          */
95                         if (total_written != 0) {
96                                 return total_written;
97                         }
98                         return -1;
99                 }
100
101                 if (read_ret <= 0) {
102                         /* EOF or socket error. */
103                         return -1;
104                 }
105
106                 num_written = 0;
107
108                 /* Don't write any more after a write error. */
109                 while (tofd != -1 && (num_written < read_ret)) {
110                         ssize_t write_ret;
111
112                         /* Write to file - ignore EINTR. */
113                         write_ret = sys_write(tofd,
114                                         buffer + num_written,
115                                         read_ret - num_written);
116
117                         if (write_ret <= 0) {
118                                 /* write error - stop writing. */
119                                 tofd = -1;
120                                 if (total_written == 0) {
121                                         /* Ensure we return
122                                            -1 if the first
123                                            write failed. */
124                                         total_written = -1;
125                                 }
126                                 saved_errno = errno;
127                                 break;
128                         }
129
130                         num_written += (size_t)write_ret;
131                         total_written += (size_t)write_ret;
132                 }
133
134                 total += read_ret;
135         }
136
137         if (saved_errno) {
138                 /* Return the correct write error. */
139                 errno = saved_errno;
140         }
141         return (ssize_t)total_written;
142 }
143
144 #if defined(HAVE_LINUX_SPLICE)
145
146 /*
147  * Try and use the Linux system call to do this.
148  * Remember we only return -1 if the socket read
149  * failed. Else we return the number of bytes
150  * actually written. We always read count bytes
151  * from the network in the case of return != -1.
152  */
153
154
155 ssize_t sys_recvfile(int fromfd,
156                         int tofd,
157                         off_t offset,
158                         size_t count)
159 {
160         static int pipefd[2] = { -1, -1 };
161         static bool try_splice_call = false;
162         size_t total_written = 0;
163         loff_t splice_offset = offset;
164
165         DEBUG(10,("sys_recvfile: from = %d, to = %d, "
166                 "offset=%.0f, count = %lu\n",
167                 fromfd, tofd, (double)offset,
168                 (unsigned long)count));
169
170         if (count == 0) {
171                 return 0;
172         }
173
174         /*
175          * Older Linux kernels have splice for sendfile,
176          * but it fails for recvfile. Ensure we only try
177          * this once and always fall back to the userspace
178          * implementation if recvfile splice fails. JRA.
179          */
180
181         if (!try_splice_call) {
182                 return default_sys_recvfile(fromfd,
183                                 tofd,
184                                 offset,
185                                 count);
186         }
187
188         if ((pipefd[0] == -1) && (pipe(pipefd) == -1)) {
189                 try_splice_call = false;
190                 return default_sys_recvfile(fromfd, tofd, offset, count);
191         }
192
193         while (count > 0) {
194                 int nread, to_write;
195
196                 nread = splice(fromfd, NULL, pipefd[1], NULL,
197                                MIN(count, 16384), SPLICE_F_MOVE);
198                 if (nread == -1) {
199                         if (errno == EINTR) {
200                                 continue;
201                         }
202                         if (total_written == 0 &&
203                             (errno == EBADF || errno == EINVAL)) {
204                                 try_splice_call = false;
205                                 return default_sys_recvfile(fromfd, tofd,
206                                                             offset, count);
207                         }
208                         if (errno == EAGAIN || errno == EWOULDBLOCK) {
209                                 /*
210                                  * fromfd socket is in non-blocking mode.
211                                  * If we already read some and wrote
212                                  * it successfully, return that.
213                                  * Only return -1 if this is the first read
214                                  * attempt. Caller will handle both cases.
215                                  */
216                                 if (total_written != 0) {
217                                         return total_written;
218                                 }
219                                 return -1;
220                         }
221                         break;
222                 }
223
224                 to_write = nread;
225                 while (to_write > 0) {
226                         int thistime;
227                         thistime = splice(pipefd[0], NULL, tofd,
228                                           &splice_offset, to_write,
229                                           SPLICE_F_MOVE);
230                         if (thistime == -1) {
231                                 goto done;
232                         }
233                         to_write -= thistime;
234                 }
235
236                 total_written += nread;
237                 count -= nread;
238         }
239
240  done:
241         if (count) {
242                 int saved_errno = errno;
243                 if (drain_socket(fromfd, count) != count) {
244                         /* socket is dead. */
245                         return -1;
246                 }
247                 errno = saved_errno;
248         }
249
250         return total_written;
251 }
252 #else
253
254 /*****************************************************************
255  No recvfile system call - use the default 128 chunk implementation.
256 *****************************************************************/
257
258 ssize_t sys_recvfile(int fromfd,
259                         int tofd,
260                         off_t offset,
261                         size_t count)
262 {
263         return default_sys_recvfile(fromfd, tofd, offset, count);
264 }
265 #endif
266
267 /*****************************************************************
268  Throw away "count" bytes from the client socket.
269  Returns count or -1 on error.
270  Must only operate on a blocking socket.
271 *****************************************************************/
272
273 ssize_t drain_socket(int sockfd, size_t count)
274 {
275         size_t total = 0;
276         size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
277         char buffer[bufsize];
278         int old_flags = 0;
279
280         if (count == 0) {
281                 return 0;
282         }
283
284         old_flags = fcntl(sockfd, F_GETFL, 0);
285         if (set_blocking(sockfd, true) == -1) {
286                 return -1;
287         }
288
289         while (total < count) {
290                 ssize_t read_ret;
291                 size_t toread = MIN(bufsize,count - total);
292
293                 /* Read from socket - ignore EINTR. */
294                 read_ret = sys_read(sockfd, buffer, toread);
295                 if (read_ret <= 0) {
296                         /* EOF or socket error. */
297                         count = (size_t)-1;
298                         goto out;
299                 }
300                 total += read_ret;
301         }
302
303   out:
304
305         if (fcntl(sockfd, F_SETFL, old_flags) == -1) {
306                 return -1;
307         }
308         return count;
309 }