rwrap: Handle trailing dot in dns names.
[Samba.git] / source3 / lib / recvfile.c
blob273c51f7703717112fe5920f81ae8dba6ad4f986
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.2.x
4 recvfile implementations.
5 Copyright (C) Jeremy Allison 2007.
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.
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/>.
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.
26 #include "includes.h"
27 #include "system/filesys.h"
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.
33 * Returns -1 on short reads from fromfd (read error)
34 * and sets errno.
36 * Returns number of bytes written to 'tofd'
37 * return != count then sets errno.
38 * Returns count if complete success.
41 #ifndef TRANSFER_BUF_SIZE
42 #define TRANSFER_BUF_SIZE (128*1024)
43 #endif
45 static ssize_t default_sys_recvfile(int fromfd,
46 int tofd,
47 off_t offset,
48 size_t count)
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];
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));
61 if (count == 0) {
62 return 0;
65 if (tofd != -1 && offset != (off_t)-1) {
66 if (lseek(tofd, offset, SEEK_SET) == -1) {
67 if (errno != ESPIPE) {
68 return -1;
73 while (total < count) {
74 size_t num_written = 0;
75 ssize_t read_ret;
76 size_t toread = MIN(bufsize,count - total);
79 * Read from socket - ignore EINTR.
80 * Can't use sys_read() as that also
81 * ignores EAGAIN and EWOULDBLOCK.
83 do {
84 read_ret = read(fromfd, buffer, toread);
85 } while (read_ret == -1 && errno == EINTR);
87 if (read_ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
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.
95 if (total_written != 0) {
96 return total_written;
98 return -1;
101 if (read_ret <= 0) {
102 /* EOF or socket error. */
103 return -1;
106 num_written = 0;
108 /* Don't write any more after a write error. */
109 while (tofd != -1 && (num_written < read_ret)) {
110 ssize_t write_ret;
112 /* Write to file - ignore EINTR. */
113 write_ret = sys_write(tofd,
114 buffer + num_written,
115 read_ret - num_written);
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;
126 saved_errno = errno;
127 break;
130 num_written += (size_t)write_ret;
131 total_written += (size_t)write_ret;
134 total += read_ret;
137 if (saved_errno) {
138 /* Return the correct write error. */
139 errno = saved_errno;
141 return (ssize_t)total_written;
144 #if defined(HAVE_LINUX_SPLICE)
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.
155 ssize_t sys_recvfile(int fromfd,
156 int tofd,
157 off_t offset,
158 size_t count)
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;
165 DEBUG(10,("sys_recvfile: from = %d, to = %d, "
166 "offset=%.0f, count = %lu\n",
167 fromfd, tofd, (double)offset,
168 (unsigned long)count));
170 if (count == 0) {
171 return 0;
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.
181 if (!try_splice_call) {
182 return default_sys_recvfile(fromfd,
183 tofd,
184 offset,
185 count);
188 if ((pipefd[0] == -1) && (pipe(pipefd) == -1)) {
189 try_splice_call = false;
190 return default_sys_recvfile(fromfd, tofd, offset, count);
193 while (count > 0) {
194 int nread, to_write;
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;
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);
208 if (errno == EAGAIN || errno == EWOULDBLOCK) {
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.
216 if (total_written != 0) {
217 return total_written;
219 return -1;
221 break;
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;
233 to_write -= thistime;
236 total_written += nread;
237 count -= nread;
240 done:
241 if (count) {
242 int saved_errno = errno;
243 if (drain_socket(fromfd, count) != count) {
244 /* socket is dead. */
245 return -1;
247 errno = saved_errno;
250 return total_written;
252 #else
254 /*****************************************************************
255 No recvfile system call - use the default 128 chunk implementation.
256 *****************************************************************/
258 ssize_t sys_recvfile(int fromfd,
259 int tofd,
260 off_t offset,
261 size_t count)
263 return default_sys_recvfile(fromfd, tofd, offset, count);
265 #endif
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 *****************************************************************/
273 ssize_t drain_socket(int sockfd, size_t count)
275 size_t total = 0;
276 size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
277 char buffer[bufsize];
278 int old_flags = 0;
280 if (count == 0) {
281 return 0;
284 old_flags = fcntl(sockfd, F_GETFL, 0);
285 if (set_blocking(sockfd, true) == -1) {
286 return -1;
289 while (total < count) {
290 ssize_t read_ret;
291 size_t toread = MIN(bufsize,count - total);
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;
300 total += read_ret;
303 out:
305 if (fcntl(sockfd, F_SETFL, old_flags) == -1) {
306 return -1;
308 return count;