s3:lib: use stack buffers in drain_socket() and default_sys_recvfile()
[metze/samba/wip.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                 /* Read from socket - ignore EINTR. */
79                 read_ret = sys_read(fromfd, buffer, toread);
80                 if (read_ret <= 0) {
81                         /* EOF or socket error. */
82                         return -1;
83                 }
84
85                 num_written = 0;
86
87                 /* Don't write any more after a write error. */
88                 while (tofd != -1 && (num_written < read_ret)) {
89                         ssize_t write_ret;
90
91                         /* Write to file - ignore EINTR. */
92                         write_ret = sys_write(tofd,
93                                         buffer + num_written,
94                                         read_ret - num_written);
95
96                         if (write_ret <= 0) {
97                                 /* write error - stop writing. */
98                                 tofd = -1;
99                                 if (total_written == 0) {
100                                         /* Ensure we return
101                                            -1 if the first
102                                            write failed. */
103                                         total_written = -1;
104                                 }
105                                 saved_errno = errno;
106                                 break;
107                         }
108
109                         num_written += (size_t)write_ret;
110                         total_written += (size_t)write_ret;
111                 }
112
113                 total += read_ret;
114         }
115
116         if (saved_errno) {
117                 /* Return the correct write error. */
118                 errno = saved_errno;
119         }
120         return (ssize_t)total_written;
121 }
122
123 #if defined(HAVE_LINUX_SPLICE)
124
125 /*
126  * Try and use the Linux system call to do this.
127  * Remember we only return -1 if the socket read
128  * failed. Else we return the number of bytes
129  * actually written. We always read count bytes
130  * from the network in the case of return != -1.
131  */
132
133
134 ssize_t sys_recvfile(int fromfd,
135                         int tofd,
136                         off_t offset,
137                         size_t count)
138 {
139         static int pipefd[2] = { -1, -1 };
140         static bool try_splice_call = false;
141         size_t total_written = 0;
142         loff_t splice_offset = offset;
143
144         DEBUG(10,("sys_recvfile: from = %d, to = %d, "
145                 "offset=%.0f, count = %lu\n",
146                 fromfd, tofd, (double)offset,
147                 (unsigned long)count));
148
149         if (count == 0) {
150                 return 0;
151         }
152
153         /*
154          * Older Linux kernels have splice for sendfile,
155          * but it fails for recvfile. Ensure we only try
156          * this once and always fall back to the userspace
157          * implementation if recvfile splice fails. JRA.
158          */
159
160         if (!try_splice_call) {
161                 return default_sys_recvfile(fromfd,
162                                 tofd,
163                                 offset,
164                                 count);
165         }
166
167         if ((pipefd[0] == -1) && (pipe(pipefd) == -1)) {
168                 try_splice_call = false;
169                 return default_sys_recvfile(fromfd, tofd, offset, count);
170         }
171
172         while (count > 0) {
173                 int nread, to_write;
174
175                 nread = splice(fromfd, NULL, pipefd[1], NULL,
176                                MIN(count, 16384), SPLICE_F_MOVE);
177                 if (nread == -1) {
178                         if (errno == EINTR) {
179                                 continue;
180                         }
181                         if (total_written == 0 &&
182                             (errno == EBADF || errno == EINVAL)) {
183                                 try_splice_call = false;
184                                 return default_sys_recvfile(fromfd, tofd,
185                                                             offset, count);
186                         }
187                         break;
188                 }
189
190                 to_write = nread;
191                 while (to_write > 0) {
192                         int thistime;
193                         thistime = splice(pipefd[0], NULL, tofd,
194                                           &splice_offset, to_write,
195                                           SPLICE_F_MOVE);
196                         if (thistime == -1) {
197                                 goto done;
198                         }
199                         to_write -= thistime;
200                 }
201
202                 total_written += nread;
203                 count -= nread;
204         }
205
206  done:
207         if (count) {
208                 int saved_errno = errno;
209                 if (drain_socket(fromfd, count) != count) {
210                         /* socket is dead. */
211                         return -1;
212                 }
213                 errno = saved_errno;
214         }
215
216         return total_written;
217 }
218 #else
219
220 /*****************************************************************
221  No recvfile system call - use the default 128 chunk implementation.
222 *****************************************************************/
223
224 ssize_t sys_recvfile(int fromfd,
225                         int tofd,
226                         off_t offset,
227                         size_t count)
228 {
229         return default_sys_recvfile(fromfd, tofd, offset, count);
230 }
231 #endif
232
233 /*****************************************************************
234  Throw away "count" bytes from the client socket.
235  Returns count or -1 on error.
236  Must only operate on a blocking socket.
237 *****************************************************************/
238
239 ssize_t drain_socket(int sockfd, size_t count)
240 {
241         size_t total = 0;
242         size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
243         char buffer[bufsize];
244         int old_flags = 0;
245
246         if (count == 0) {
247                 return 0;
248         }
249
250         old_flags = fcntl(sockfd, F_GETFL, 0);
251         if (set_blocking(sockfd, true) == -1) {
252                 return -1;
253         }
254
255         while (total < count) {
256                 ssize_t read_ret;
257                 size_t toread = MIN(bufsize,count - total);
258
259                 /* Read from socket - ignore EINTR. */
260                 read_ret = sys_read(sockfd, buffer, toread);
261                 if (read_ret <= 0) {
262                         /* EOF or socket error. */
263                         count = (size_t)-1;
264                         goto out;
265                 }
266                 total += read_ret;
267         }
268
269   out:
270
271         if (fcntl(sockfd, F_SETFL, old_flags) == -1) {
272                 return -1;
273         }
274         return count;
275 }