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