0f715c2bb337e5b61545c16c8e077a5d8ea39ec8
[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 #include "nixio.h"
20 #include <fcntl.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <unistd.h>
24 #include <sys/sendfile.h>
25
26 #ifdef _GNU_SOURCE
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  * splice(fd_in, fd_out, length, flags)
51  */
52 static int nixio_splice(lua_State *L) {
53         int fd_in = nixio__checkfd(L, 1);
54         int fd_out = nixio__checkfd(L, 2);
55         size_t len = luaL_checkinteger(L, 3);
56         int flags = luaL_optinteger(L, 4, 0);
57         long spliced;
58
59         do {
60                 spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
61         } while (spliced == -1 && errno == EINTR);
62
63         if (spliced < 0) {
64                 return nixio__perror(L);
65         }
66
67         lua_pushnumber(L, spliced);
68         return 1;
69 }
70
71 /**
72  * Translate splice flags to integer
73  */
74 static int nixio_splice_flags(lua_State *L) {
75         const int j = lua_gettop(L);
76         int flags = 0;
77         for (int i=1; i<=j; i++) {
78                 const char *flag = luaL_checkstring(L, i);
79                 if (!strcmp(flag, "move")) {
80                         flags |= SPLICE_F_MOVE;
81                 } else if (!strcmp(flag, "nonblock")) {
82                         flags |= SPLICE_F_NONBLOCK;
83                 } else if (!strcmp(flag, "more")) {
84                         flags |= SPLICE_F_MORE;
85                 } else {
86                         return luaL_argerror(L, i, "supported values: "
87                          "move, nonblock, more");
88                 }
89         }
90         lua_pushinteger(L, flags);
91
92         return 1;
93 }
94
95 #endif /* _GNU_SOURCE */
96
97 /**
98  * sendfile(outfd, infd, length)
99  */
100 static int nixio_sendfile(lua_State *L) {
101         int sockfd = nixio__checksockfd(L);
102         int infd = nixio__checkfd(L, 2);
103         size_t len = luaL_checkinteger(L, 3);
104
105         long spliced = sendfile(sockfd, infd, NULL, len);
106
107         if (spliced < 0) {
108                 return nixio__perror(L);
109         }
110
111         lua_pushnumber(L, spliced);
112         return 1;
113 }
114
115 /* module table */
116 static const luaL_reg R[] = {
117 #ifdef _GNU_SOURCE
118         {"splice",                      nixio_splice},
119         {"splice_flags",        nixio_splice_flags},
120 #endif
121         {"sendfile",            nixio_sendfile},
122         {NULL,                  NULL}
123 };
124
125 void nixio_open_splice(lua_State *L) {
126         luaL_register(L, NULL, R);
127 }