Skip to content

Commit 29da440

Browse files
patricklucasjonashaag
authored andcommitted
OS X support
* abstract sendfile() differences * memset-zero sockaddr_in before using * build with -fcommon -- let the linker strip duplicate symbols Closes jonashaag#60, jonashaag#59 and jonashaag#19.
1 parent f957bb6 commit 29da440

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ objects = $(HTTP_PARSER_OBJ) \
1515
CPPFLAGS += $(PYTHON_INCLUDE) -I . -I $(SOURCE_DIR) -I $(HTTP_PARSER_DIR)
1616
CFLAGS += $(FEATURES) -std=c99 -fno-strict-aliasing -Wall -Wextra \
1717
-Wno-unused -g -O0 -fPIC
18-
LDFLAGS += $(PYTHON_LDFLAGS) -l ev -shared
18+
LDFLAGS += $(PYTHON_LDFLAGS) -l ev -shared -fcommon
1919

2020
ifneq ($(WANT_SENDFILE), no)
2121
FEATURES += -D WANT_SENDFILE

bjoern/portable_sendfile.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <stdlib.h>
2+
3+
#define SENDFILE_CHUNK_SIZE 16*1024
4+
5+
#ifdef __APPLE__
6+
7+
/* OS X */
8+
9+
#include <sys/socket.h>
10+
#include <sys/types.h>
11+
12+
ssize_t portable_sendfile(int out_fd, int in_fd) {
13+
off_t len = SENDFILE_CHUNK_SIZE;
14+
if(sendfile(in_fd, out_fd, 0, &len, NULL, 0) == -1)
15+
return -1;
16+
return len;
17+
}
18+
19+
#else
20+
21+
/* Linux */
22+
23+
#include <sys/sendfile.h>
24+
25+
ssize_t portable_sendfile(int out_fd, int in_fd) {
26+
return sendfile(out_fd, in_fd, NULL, SENDFILE_CHUNK_SIZE);
27+
}
28+
29+
#endif

bjoern/portable_sendfile.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssize_t portable_sendfile(int out_fd, int in_fd);

bjoern/server.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#ifdef WANT_SIGINT_HANDLING
77
# include <sys/signal.h>
88
#endif
9-
#include <sys/sendfile.h>
109
#include <ev.h>
10+
#include "portable_sendfile.h"
1111
#include "common.h"
1212
#include "wsgi.h"
1313
#include "server.h"
@@ -104,6 +104,7 @@ bool server_init(const char* hostaddr, const int port)
104104
return false;
105105

106106
struct sockaddr_in sockaddr;
107+
memset(&sockaddr, 0, sizeof(sockaddr));
107108
sockaddr.sin_family = PF_INET;
108109
inet_pton(AF_INET, hostaddr, &sockaddr.sin_addr);
109110
sockaddr.sin_port = htons(port);
@@ -302,7 +303,6 @@ ev_io_on_write(struct ev_loop* mainloop, ev_io* watcher, const int events)
302303
static bool
303304
send_chunk(Request* request)
304305
{
305-
Py_ssize_t chunk_length;
306306
Py_ssize_t bytes_sent;
307307

308308
assert(request->current_chunk != NULL);
@@ -327,15 +327,12 @@ send_chunk(Request* request)
327327
return true;
328328
}
329329

330-
#define SENDFILE_CHUNK_SIZE 16*1024
331-
332330
static bool
333331
do_sendfile(Request* request)
334332
{
335-
Py_ssize_t bytes_sent = sendfile(
336-
request->client_fd,
337-
request->current_chunk_p, /* current_chunk_p stores the file fd */
338-
NULL, SENDFILE_CHUNK_SIZE
333+
Py_ssize_t bytes_sent = portable_sendfile(
334+
request->client_fd,
335+
request->current_chunk_p /* current_chunk_p stores the file fd */
339336
);
340337
if(bytes_sent == -1)
341338
return handle_nonzero_errno(request);

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
('WANT_SIGINT_HANDLING', '1')],
1515
extra_compile_args = ['-std=c99', '-fno-strict-aliasing', '-Wall',
1616
'-Wextra', '-Wno-unused', '-g', '-fPIC',
17-
'-Wno-missing-field-initializers']
17+
'-Wno-missing-field-initializers', '-fcommon']
1818
)
1919

2020
setup(

0 commit comments

Comments
 (0)