556b4d7dadcc719ac39e1099699ae5dca75ae3fb
[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 <sys/sendfile.h>
25
26 /* guess what sucks... */
27 #ifdef __UCLIBC__
28 #include <unistd.h>
29 #include <errno.h>
30 #include <sys/syscall.h>
31 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
32         __off64_t *__offout, size_t __len, unsigned int __flags) {
33 #ifdef __NR_splice
34         return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
35 #else
36         (void)__fdin;
37         (void)__offin;
38         (void)__fdout;
39         (void)__offout;
40         (void)__len;
41         (void)__flags;
42         errno = ENOSYS;
43         return -1;
44 #endif
45 }
46 #endif /* __UCLIBC__ */
47
48 /**
49  * Translate splice flags to integer
50  */
51 static int nixio_splice_flags(lua_State *L) {
52         const int j = lua_gettop(L);
53         int flags = 0;
54         for (int i=1; i<=j; i++) {
55                 const char *flag = luaL_checkstring(L, i);
56                 if (!strcmp(flag, "move")) {
57                         flags |= SPLICE_F_MOVE;
58                 } else if (!strcmp(flag, "nonblock")) {
59                         flags |= SPLICE_F_NONBLOCK;
60                 } else if (!strcmp(flag, "more")) {
61                         flags |= SPLICE_F_MORE;
62                 } else {
63                         return luaL_argerror(L, i, "supported values: "
64                          "move, nonblock, more");
65                 }
66         }
67         lua_pushinteger(L, flags);
68
69         return 1;
70 }
71
72 /**
73  * splice(fd_in, fd_out, length, flags)
74  */
75 static int nixio_splice(lua_State *L) {
76         int fd_in = nixio__checkfd(L, 1);
77         int fd_out = nixio__checkfd(L, 2);
78         size_t len = luaL_checkinteger(L, 3);
79         int flags = luaL_optinteger(L, 4, 0);
80
81
82         long spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
83
84         if (spliced < 0) {
85                 return nixio__perror(L);
86         }
87
88         lua_pushnumber(L, spliced);
89         return 1;
90 }
91
92 /**
93  * sendfile(outfd, infd, length)
94  */
95 static int nixio_sendfile(lua_State *L) {
96         int sockfd = nixio__checksockfd(L);
97         int infd = nixio__checkfd(L, 2);
98         size_t len = luaL_checkinteger(L, 3);
99
100         long spliced = sendfile(sockfd, infd, NULL, len);
101
102         if (spliced < 0) {
103                 return nixio__perror(L);
104         }
105
106         lua_pushnumber(L, spliced);
107         return 1;
108 }
109
110 /* module table */
111 static const luaL_reg R[] = {
112         {"splice",                      nixio_splice},
113         {"splice_flags",        nixio_splice_flags},
114         {"sendfile",            nixio_sendfile},
115         {NULL,                  NULL}
116 };
117
118 void nixio_open_splice(lua_State *L) {
119         luaL_register(L, NULL, R);
120 }