1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include "sendfile.h"
#if defined(__linux__)
#include <sys/sendfile.h>
#elif defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#else
#error "What platform is this?"
#endif
#include <errno.h>
bool sendfile_all(const int fd, const int sock, off_t foffset, const size_t bytes)
{
if ( bytes == 0 )
return true;
#ifdef DNBD3_SERVER_AFL
errno = 0;
return true;
#elif defined(__linux__)
size_t done = 0;
int againCount = 0;
while ( done < bytes ) {
const ssize_t sent = sendfile( sock, fd, &foffset, bytes - done );
if ( sent == 0 ) // Probably EOF, like with send(), but manpage is not clear :-/
return false;
if ( sent < 0 ) {
if ( errno == EAGAIN || errno == EINTR ) {
// Retry once, but give up otherwise - EAGAIN might just be the send timeout
if ( ++againCount > 1 )
return false;
continue;
}
return false;
}
done += sent;
}
#elif defined(__FreeBSD__)
off_t sent;
size_t done = 0;
int againCount = 0;
while ( done < bytes ) {
const int ret = sendfile( fd, sock, foffset + done, bytes - done, NULL, &sent, 0 );
if ( ret == 0 || errno == EAGAIN || errno == EINTR ) {
// Retry once, but give up otherwise - EAGAIN might just be the send timeout
if ( sent == 0 && ++againCount > 1 )
return false;
done += sent;
continue;
}
// Something else went wrong
return false;
}
#endif
return true;
}
|