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