nixio: Export more constants, introduce {g,s}et{g,u}id, getpid, getppid
[oweals/luci.git] / libs / nixio / src / splice.c
index 2b283706093e999520d03bab8af760ccd4b61cd1..0f715c2bb337e5b61545c16c8e077a5d8ea39ec8 100644 (file)
  *  limitations under the License.
  */
 
-#define _GNU_SOURCE
-
 #include "nixio.h"
 #include <fcntl.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
 #include <sys/sendfile.h>
 
+#ifdef _GNU_SOURCE
+
 /* guess what sucks... */
 #ifdef __UCLIBC__
 #include <unistd.h>
 #include <sys/syscall.h>
 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
         __off64_t *__offout, size_t __len, unsigned int __flags) {
+#ifdef __NR_splice
        return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
+#else
+       (void)__fdin;
+       (void)__offin;
+       (void)__fdout;
+       (void)__offout;
+       (void)__len;
+       (void)__flags;
+       errno = ENOSYS;
+       return -1;
+#endif
 }
 #endif /* __UCLIBC__ */
 
-/**
- * Checks whether a flag is set in the table and translates it into a bitmap
- */
-static void nixio_splice_flags__w(lua_State *L, int *m, int f, const char *t) {
-       lua_pushstring(L, t);
-       lua_rawget(L, -2);
-       if (lua_toboolean(L, -1)) {
-               *m |= f;
-       }
-       lua_pop(L, 1);
-}
-
-/**
- * Translate integer to poll flags and vice versa
- */
-static int nixio_splice_flags(lua_State *L) {
-       int flags = 0;
-
-       luaL_checktype(L, 1, LUA_TTABLE);
-       lua_settop(L, 1);
-       nixio_splice_flags__w(L, &flags, SPLICE_F_MOVE, "move");
-       nixio_splice_flags__w(L, &flags, SPLICE_F_NONBLOCK, "nonblock");
-       nixio_splice_flags__w(L, &flags, SPLICE_F_MORE, "more");
-       lua_pushinteger(L, flags);
-
-       return 1;
-}
-
 /**
  * splice(fd_in, fd_out, length, flags)
  */
@@ -68,9 +54,11 @@ static int nixio_splice(lua_State *L) {
        int fd_out = nixio__checkfd(L, 2);
        size_t len = luaL_checkinteger(L, 3);
        int flags = luaL_optinteger(L, 4, 0);
+       long spliced;
 
-
-       long spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
+       do {
+               spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
+       } while (spliced == -1 && errno == EINTR);
 
        if (spliced < 0) {
                return nixio__perror(L);
@@ -80,6 +68,32 @@ static int nixio_splice(lua_State *L) {
        return 1;
 }
 
+/**
+ * Translate splice flags to integer
+ */
+static int nixio_splice_flags(lua_State *L) {
+       const int j = lua_gettop(L);
+       int flags = 0;
+       for (int i=1; i<=j; i++) {
+               const char *flag = luaL_checkstring(L, i);
+               if (!strcmp(flag, "move")) {
+                       flags |= SPLICE_F_MOVE;
+               } else if (!strcmp(flag, "nonblock")) {
+                       flags |= SPLICE_F_NONBLOCK;
+               } else if (!strcmp(flag, "more")) {
+                       flags |= SPLICE_F_MORE;
+               } else {
+                       return luaL_argerror(L, i, "supported values: "
+                        "move, nonblock, more");
+               }
+       }
+       lua_pushinteger(L, flags);
+
+       return 1;
+}
+
+#endif /* _GNU_SOURCE */
+
 /**
  * sendfile(outfd, infd, length)
  */
@@ -100,8 +114,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}
 };