summaryrefslogtreecommitdiff
path: root/source3/lib/sendfile.c
blob: 33282e704a5a5c7fbce5d57bcc9d6967e5b4ca56 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 Unix SMB/Netbios implementation.
 Version 2.2.x / 3.0.x
 sendfile implementations.
 Copyright (C) Jeremy Allison 2002.

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * This file handles the OS dependent sendfile implementations.
 * The API is such that it returns -1 on error, else returns the
 * number of bytes written.
 */

#include "includes.h"

#if defined(LINUX_SENDFILE_API)

#include <sys/sendfile.h>

#ifndef MSG_MORE
#define MSG_MORE 0x8000
#endif

ssize_t sys_sendfile(int outfd, int infd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
{
	size_t total=0;
	ssize_t ret;

	/*
	 * Send the header first.
	 * Use MSG_MORE to cork the TCP output until sendfile is called.
	 */

	while (total < header->length) {
		ret = sys_send(outfd, header->data + total,header->length - total, MSG_MORE);
		if (ret == -1)
			return -1;
		total += ret;
	}

	total = count;
	while (total) {
		ssize_t nwritten;
		do {
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(SENDFILE64)
			nwritten = sendfile64(outfd, infd, &offset, total);
#else
			nwritten = sendfile(outfd, infd, &offset, total);
#endif
		} while (nwritten == -1 && errno == EINTR);
		if (nwritten == -1)
			return -1;
		if (nwritten == 0)
			return -1; /* I think we're at EOF here... */
		total -= nwritten;
	}
	return count + header->length;
}

#elif defined(SOLARIS_SENDFILE_API)

ssize_t sys_sendfile(int outfd, int infd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
{
}

#elif defined(HPUX_SENDFILE_API)

#include <sys/socket.h>
#include <sys/uio.h>

ssize_t sys_sendfile(int outfd, int infd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
{
	size_t total=0;
	struct iovec hdtrl[2];

	/* Set up the header/trailer iovec. */
	hdtrl[0].iov_base = header->data;
	hdtrl[0].iov_len = header->length;
	hdtrl[1].iov_base = NULL;
	hdtrl[1].iov_base = 0;

	total = count;
	while (total) {
		ssize_t nwritten;
		do {
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(SENDFILE64)
			nwritten = sendfile64(outfd, infd, &offset, total, &hdtrl, 0);
#else
			nwritten = sendfile(outfd, infd, &offset, total, &hdtrl, 0);
#endif
		} while (nwritten == -1 && errno == EINTR);
		if (nwritten == -1)
			return -1;
		total -= nwritten;
	}
	return count + header->length;
}

#elif defined(FREEBSD_SENDFILE_API)

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

ssize_t sys_sendfile(int outfd, int infd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
{
	size_t total=0;
	struct sf_hdtr hdr;
	struct iovec hdtrl;

	hdr->headers = &hdtrl;
	hdr->hdr_cnt = 1;
	hdr->trailers = NULL;
	hdr->trl_cnt = 0;

	/* Set up the header iovec. */
	hdtrl.iov_base = header->data;
	hdtrl.iov_len = header->length;

	total = count;
	while (total) {
		ssize_t nwritten;
		do {
#if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(SENDFILE64)
			nwritten = sendfile64(outfd, infd, &offset, total, &hdr, NULL, 0);
#else
			nwritten = sendfile(outfd, infd, &offset, total, &hdr, NULL, 0);
#endif
		} while (nwritten == -1 && errno == EINTR);
		if (nwritten == -1)
			return -1;
		total -= nwritten;
	}
	return count + header->length;
}

#else /* No sendfile implementation. Return error. */

ssize_t sys_sendfile(int outfd, int infd, const DATA_BLOB *header, SMB_OFF_T offset, size_t count)
{
	/* No sendfile syscall. */
	errno = ENOSYS;
	return -1;
}
#endif