FreeBSD compatibility #3
[project/luci.git] / libs / nixio / src / splice.c
index 556b4d7..f641446 100644 (file)
  *  limitations under the License.
  */
 
+#ifdef __linux__
 #define _GNU_SOURCE
+#endif
 
 #include "nixio.h"
 #include <fcntl.h>
 #include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/param.h>
+
+#ifndef BSD
 #include <sys/sendfile.h>
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/uio.h>
+#endif
+
+#ifdef _GNU_SOURCE
 
 /* guess what sucks... */
 #ifdef __UCLIBC__
 #include <unistd.h>
-#include <errno.h>
 #include <sys/syscall.h>
 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
         __off64_t *__offout, size_t __len, unsigned int __flags) {
@@ -46,6 +59,28 @@ ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
 #endif /* __UCLIBC__ */
 
 /**
+ * splice(fd_in, fd_out, length, flags)
+ */
+static int nixio_splice(lua_State *L) {
+       int fd_in = nixio__checkfd(L, 1);
+       int fd_out = nixio__checkfd(L, 2);
+       size_t len = luaL_checkinteger(L, 3);
+       int flags = luaL_optinteger(L, 4, 0);
+       long spliced;
+
+       do {
+               spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
+       } while (spliced == -1 && errno == EINTR);
+
+       if (spliced < 0) {
+               return nixio__perror(L);
+       }
+
+       lua_pushnumber(L, spliced);
+       return 1;
+}
+
+/**
  * Translate splice flags to integer
  */
 static int nixio_splice_flags(lua_State *L) {
@@ -69,39 +104,37 @@ static int nixio_splice_flags(lua_State *L) {
        return 1;
 }
 
-/**
- * splice(fd_in, fd_out, length, flags)
- */
-static int nixio_splice(lua_State *L) {
-       int fd_in = nixio__checkfd(L, 1);
-       int fd_out = nixio__checkfd(L, 2);
-       size_t len = luaL_checkinteger(L, 3);
-       int flags = luaL_optinteger(L, 4, 0);
-
-
-       long spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
-
-       if (spliced < 0) {
-               return nixio__perror(L);
-       }
-
-       lua_pushnumber(L, spliced);
-       return 1;
-}
+#endif /* _GNU_SOURCE */
 
 /**
  * sendfile(outfd, infd, length)
  */
 static int nixio_sendfile(lua_State *L) {
-       int sockfd = nixio__checksockfd(L);
+       int sock = nixio__checksockfd(L);
        int infd = nixio__checkfd(L, 2);
        size_t len = luaL_checkinteger(L, 3);
+       off_t spliced;
 
-       long spliced = sendfile(sockfd, infd, NULL, len);
+#ifndef BSD
+       do {
+               spliced = sendfile(sock, infd, NULL, len);
+       } while (spliced == -1 && errno == EINTR);
 
-       if (spliced < 0) {
+       if (spliced == -1) {
                return nixio__perror(L);
        }
+#else
+       int r;
+       const off_t offset = lseek(infd, 0, SEEK_CUR);
+
+       do {
+               r = sendfile(infd, sock, offset, len, NULL, &spliced, 0);
+       } while (r == -1 && errno == EINTR);
+
+       if (r == -1) {
+               return nixio__perror(L);
+       }
+#endif
 
        lua_pushnumber(L, spliced);
        return 1;
@@ -109,8 +142,10 @@ static int nixio_sendfile(lua_State *L) {
 
 /* module table */
 static const luaL_reg R[] = {
+#ifdef _GNU_SOURCE
        {"splice",                      nixio_splice},
        {"splice_flags",        nixio_splice_flags},
+#endif
        {"sendfile",            nixio_sendfile},
        {NULL,                  NULL}
 };