More splicing stuff
[project/luci.git] / libs / nixio / src / splice.c
1 /*
2  * nixio - Linux I/O library for lua
3  *
4  *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20
21 #include "nixio.h"
22 #include <fcntl.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <sys/sendfile.h>
27
28 /* guess what sucks... */
29 #ifdef __UCLIBC__
30 #include <unistd.h>
31 #include <sys/syscall.h>
32 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
33         __off64_t *__offout, size_t __len, unsigned int __flags) {
34 #ifdef __NR_splice
35         return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
36 #else
37         (void)__fdin;
38         (void)__offin;
39         (void)__fdout;
40         (void)__offout;
41         (void)__len;
42         (void)__flags;
43         errno = ENOSYS;
44         return -1;
45 #endif
46 }
47 #endif /* __UCLIBC__ */
48
49 /**
50  * Translate splice flags to integer
51  */
52 static int nixio_splice_flags(lua_State *L) {
53         const int j = lua_gettop(L);
54         int flags = 0;
55         for (int i=1; i<=j; i++) {
56                 const char *flag = luaL_checkstring(L, i);
57                 if (!strcmp(flag, "move")) {
58                         flags |= SPLICE_F_MOVE;
59                 } else if (!strcmp(flag, "nonblock")) {
60                         flags |= SPLICE_F_NONBLOCK;
61                 } else if (!strcmp(flag, "more")) {
62                         flags |= SPLICE_F_MORE;
63                 } else {
64                         return luaL_argerror(L, i, "supported values: "
65                          "move, nonblock, more");
66                 }
67         }
68         lua_pushinteger(L, flags);
69
70         return 1;
71 }
72
73 /**
74  * splice(fd_in, fd_out, length, flags)
75  */
76 static int nixio_splice(lua_State *L) {
77         int fd_in = nixio__checkfd(L, 1);
78         int fd_out = nixio__checkfd(L, 2);
79         size_t len = luaL_checkinteger(L, 3);
80         int flags = luaL_optinteger(L, 4, 0);
81         long spliced;
82
83         do {
84                 spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
85         } while (spliced == -1 && errno == EINTR);
86
87         if (spliced < 0) {
88                 return nixio__perror(L);
89         }
90
91         lua_pushnumber(L, spliced);
92         return 1;
93 }
94
95 static int nixio_splice_avail(lua_State *L) {
96         splice(-1, 0, -1, 0, 0, 0);
97         lua_pushboolean(L, errno != ENOSYS);
98         return 1;
99 }
100
101 /**
102  * sendfile(outfd, infd, length)
103  */
104 static int nixio_sendfile(lua_State *L) {
105         int sockfd = nixio__checksockfd(L);
106         int infd = nixio__checkfd(L, 2);
107         size_t len = luaL_checkinteger(L, 3);
108
109         long spliced = sendfile(sockfd, infd, NULL, len);
110
111         if (spliced < 0) {
112                 return nixio__perror(L);
113         }
114
115         lua_pushnumber(L, spliced);
116         return 1;
117 }
118
119 /* module table */
120 static const luaL_reg R[] = {
121         {"splice",                      nixio_splice},
122         {"splice_flags",        nixio_splice_flags},
123         {"splice_avail",        nixio_splice_avail},
124         {"sendfile",            nixio_sendfile},
125         {NULL,                  NULL}
126 };
127
128 void nixio_open_splice(lua_State *L) {
129         luaL_register(L, NULL, R);
130 }