From 4ac7a6f55fb628e67fec97568e9724719d8a9e0a Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Mon, 17 Aug 2020 12:28:40 +0300 Subject: Style: changing preprocessor directives. Using #if directives instead of #ifdef the same way as in other places. --- src/nxt_conn_write.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'src/nxt_conn_write.c') diff --git a/src/nxt_conn_write.c b/src/nxt_conn_write.c index d7a6a8da..3e08e43f 100644 --- a/src/nxt_conn_write.c +++ b/src/nxt_conn_write.c @@ -246,24 +246,30 @@ nxt_sendfile(int fd, int s, off_t pos, size_t size) { ssize_t res; -#ifdef NXT_HAVE_MACOSX_SENDFILE +#if (NXT_HAVE_MACOSX_SENDFILE) + off_t sent = size; int rc = sendfile(fd, s, pos, &sent, NULL, 0); res = (rc == 0 || sent > 0) ? sent : -1; -#endif -#ifdef NXT_HAVE_FREEBSD_SENDFILE +#elif (NXT_HAVE_FREEBSD_SENDFILE) + off_t sent = 0; int rc = sendfile(fd, s, pos, size, NULL, &sent, 0); res = (rc == 0 || sent > 0) ? sent : -1; -#endif -#ifdef NXT_HAVE_LINUX_SENDFILE +#elif (NXT_HAVE_LINUX_SENDFILE) + res = sendfile(s, fd, &pos, size); + +#else + + res = -1; + #endif return res; -- cgit From 7ffc617ae89fe08b8a9a17bed41ef8941b8151fb Mon Sep 17 00:00:00 2001 From: Max Romanov Date: Mon, 17 Aug 2020 12:28:48 +0300 Subject: Supporting platforms without sendfile() implementation. This is a quick and dirty sendfile() replacement. This closes #452 PR on GitHub. --- src/nxt_conn_write.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/nxt_conn_write.c') diff --git a/src/nxt_conn_write.c b/src/nxt_conn_write.c index 3e08e43f..bcf9e8fa 100644 --- a/src/nxt_conn_write.c +++ b/src/nxt_conn_write.c @@ -268,7 +268,24 @@ nxt_sendfile(int fd, int s, off_t pos, size_t size) #else - res = -1; + int err; + void *map; + off_t page_off; + + page_off = pos % nxt_pagesize; + + map = nxt_mem_mmap(NULL, size + page_off, PROT_READ, MAP_SHARED, fd, + pos - page_off); + if (nxt_slow_path(map == MAP_FAILED)) { + return -1; + } + + res = write(s, nxt_pointer_to(map, page_off), size); + + /* Backup and restore errno to catch socket errors in the upper level. */ + err = errno; + nxt_mem_munmap(map, size + page_off); + errno = err; #endif -- cgit