1 /* sendfile -- copy data directly from one file descriptor to another
2    Copyright (C) 2002-2022 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <sys/sendfile.h>
20 #include <hurd.h>
21 #include <hurd/fd.h>
22 #include <sys/mman.h>
23 
24 /* Send COUNT bytes from file associated with IN_FD starting at OFFSET to
25    descriptor OUT_FD.  */
26 ssize_t
__sendfile64(int out_fd,int in_fd,off64_t * offset,size_t count)27 __sendfile64 (int out_fd, int in_fd, off64_t *offset, size_t count)
28 {
29   /* We just do a vanilla io_read followed by a vanilla io_write here.
30      In theory the IN_FD filesystem can return us out-of-line data that
31      we then send out-of-line to the OUT_FD filesystem and no copying
32      takes place until those pages need to be flushed or packaged by
33      that filesystem (e.g. packetized by a network socket).  However,
34      we momentarily consume COUNT bytes of our local address space,
35      which might blow if it's huge or address space is real tight.  */
36 
37   char *data = 0;
38   size_t datalen = 0;
39   error_t err = HURD_DPORT_USE (in_fd,
40 				__io_read (port, &data, &datalen,
41 					   offset ? *offset : (off_t) -1,
42 					   count));
43   if (err == 0)
44     {
45       size_t nwrote;
46       if (datalen == 0)
47 	return 0;
48       err = HURD_DPORT_USE (out_fd, __io_write (port, data, datalen,
49 						(off_t) -1, &nwrote));
50       __munmap (data, datalen);
51       if (err == 0)
52 	{
53 	  if (offset)
54 	    *offset += datalen;
55 	  return nwrote;
56 	}
57     }
58   return __hurd_fail (err);
59 }
60 strong_alias (__sendfile64, sendfile64)
61