Merge nixio 0.2
[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 #ifdef __linux__
20 #define _GNU_SOURCE
21 #endif
22
23 #include "nixio.h"
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29
30
31 #ifndef __WINNT__
32
33 #ifndef BSD
34 #include <sys/sendfile.h>
35 #else
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/uio.h>
39 #endif
40
41 #ifdef _GNU_SOURCE
42
43 /* guess what sucks... */
44 #ifdef __UCLIBC__
45 #include <unistd.h>
46 #include <sys/syscall.h>
47 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
48         __off64_t *__offout, size_t __len, unsigned int __flags) {
49 #ifdef __NR_splice
50         return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
51 #else
52         (void)__fdin;
53         (void)__offin;
54         (void)__fdout;
55         (void)__offout;
56         (void)__len;
57         (void)__flags;
58         errno = ENOSYS;
59         return -1;
60 #endif
61 }
62 #endif /* __UCLIBC__ */
63
64 /**
65  * splice(fd_in, fd_out, length, flags)
66  */
67 static int nixio_splice(lua_State *L) {
68         int fd_in = nixio__checkfd(L, 1);
69         int fd_out = nixio__checkfd(L, 2);
70         size_t len = luaL_checkinteger(L, 3);
71         int flags = luaL_optinteger(L, 4, 0);
72         long spliced;
73
74         do {
75                 spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
76         } while (spliced == -1 && errno == EINTR);
77
78         if (spliced < 0) {
79                 return nixio__perror(L);
80         }
81
82         lua_pushnumber(L, spliced);
83         return 1;
84 }
85
86 /**
87  * Translate splice flags to integer
88  */
89 static int nixio_splice_flags(lua_State *L) {
90         const int j = lua_gettop(L);
91         int flags = 0;
92         for (int i=1; i<=j; i++) {
93                 const char *flag = luaL_checkstring(L, i);
94                 if (!strcmp(flag, "move")) {
95                         flags |= SPLICE_F_MOVE;
96                 } else if (!strcmp(flag, "nonblock")) {
97                         flags |= SPLICE_F_NONBLOCK;
98                 } else if (!strcmp(flag, "more")) {
99                         flags |= SPLICE_F_MORE;
100                 } else {
101                         return luaL_argerror(L, i, "supported values: "
102                          "move, nonblock, more");
103                 }
104         }
105         lua_pushinteger(L, flags);
106
107         return 1;
108 }
109
110 #endif /* _GNU_SOURCE */
111
112 /**
113  * sendfile(outfd, infd, length)
114  */
115 static int nixio_sendfile(lua_State *L) {
116         int sock = nixio__checksockfd(L);
117         int infd = nixio__checkfd(L, 2);
118         size_t len = luaL_checkinteger(L, 3);
119         off_t spliced;
120
121 #ifndef BSD
122         do {
123                 spliced = sendfile(sock, infd, NULL, len);
124         } while (spliced == -1 && errno == EINTR);
125
126         if (spliced == -1) {
127                 return nixio__perror(L);
128         }
129 #else
130         int r;
131         const off_t offset = lseek(infd, 0, SEEK_CUR);
132
133         do {
134                 r = sendfile(infd, sock, offset, len, NULL, &spliced, 0);
135         } while (r == -1 && errno == EINTR);
136
137         if (r == -1) {
138                 return nixio__perror(L);
139         }
140 #endif
141
142         lua_pushnumber(L, spliced);
143         return 1;
144 }
145
146 /* module table */
147 static const luaL_reg R[] = {
148 #ifdef _GNU_SOURCE
149         {"splice",                      nixio_splice},
150         {"splice_flags",        nixio_splice_flags},
151 #endif
152         {"sendfile",            nixio_sendfile},
153         {NULL,                  NULL}
154 };
155
156
157 void nixio_open_splice(lua_State *L) {
158         luaL_register(L, NULL, R);
159 }
160
161 #else /* __WINNT__ */
162
163 void nixio_open_splice(lua_State *L) {
164 }
165
166 #endif /* !__WINNT__ */