2 * nixio - Linux I/O library for lua
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <sys/sendfile.h>
32 /* guess what sucks... */
35 #include <sys/syscall.h>
36 ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
37 __off64_t *__offout, size_t __len, unsigned int __flags) {
39 return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
51 #endif /* __UCLIBC__ */
54 * splice(fd_in, fd_out, length, flags)
56 static int nixio_splice(lua_State *L) {
57 int fd_in = nixio__checkfd(L, 1);
58 int fd_out = nixio__checkfd(L, 2);
59 size_t len = luaL_checkinteger(L, 3);
60 int flags = luaL_optinteger(L, 4, 0);
64 spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
65 } while (spliced == -1 && errno == EINTR);
68 return nixio__perror(L);
71 lua_pushnumber(L, spliced);
76 * Translate splice flags to integer
78 static int nixio_splice_flags(lua_State *L) {
79 const int j = lua_gettop(L);
81 for (int i=1; i<=j; i++) {
82 const char *flag = luaL_checkstring(L, i);
83 if (!strcmp(flag, "move")) {
84 flags |= SPLICE_F_MOVE;
85 } else if (!strcmp(flag, "nonblock")) {
86 flags |= SPLICE_F_NONBLOCK;
87 } else if (!strcmp(flag, "more")) {
88 flags |= SPLICE_F_MORE;
90 return luaL_argerror(L, i, "supported values: "
91 "move, nonblock, more");
94 lua_pushinteger(L, flags);
99 #endif /* _GNU_SOURCE */
102 * sendfile(outfd, infd, length)
104 static int nixio_sendfile(lua_State *L) {
105 int sockfd = nixio__checksockfd(L);
106 int infd = nixio__checkfd(L, 2);
107 size_t len = luaL_checkinteger(L, 3);
109 long spliced = sendfile(sockfd, infd, NULL, len);
112 return nixio__perror(L);
115 lua_pushnumber(L, spliced);
120 static const luaL_reg R[] = {
122 {"splice", nixio_splice},
123 {"splice_flags", nixio_splice_flags},
125 {"sendfile", nixio_sendfile},
129 void nixio_open_splice(lua_State *L) {
130 luaL_register(L, NULL, R);