root/luci/trunk/libs/nixio/src/splice.c @ 4312

Revision 4312, 2.9 KB (checked in by Cyrus, 4 years ago)

nixio: reenable splice() for linux

Line 
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/sendfile.h>
29
30#ifdef _GNU_SOURCE
31
32/* guess what sucks... */
33#ifdef __UCLIBC__
34#include <unistd.h>
35#include <sys/syscall.h>
36ssize_t splice(int __fdin, __off64_t *__offin, int __fdout,
37        __off64_t *__offout, size_t __len, unsigned int __flags) {
38#ifdef __NR_splice
39    return syscall(__NR_splice, __fdin, __offin, __fdout, __offout, __len, __flags);
40#else
41    (void)__fdin;
42    (void)__offin;
43    (void)__fdout;
44    (void)__offout;
45    (void)__len;
46    (void)__flags;
47    errno = ENOSYS;
48    return -1;
49#endif
50}
51#endif /* __UCLIBC__ */
52
53/**
54 * splice(fd_in, fd_out, length, flags)
55 */
56static 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);
61    long spliced;
62
63    do {
64        spliced = splice(fd_in, NULL, fd_out, NULL, len, flags);
65    } while (spliced == -1 && errno == EINTR);
66
67    if (spliced < 0) {
68        return nixio__perror(L);
69    }
70
71    lua_pushnumber(L, spliced);
72    return 1;
73}
74
75/**
76 * Translate splice flags to integer
77 */
78static int nixio_splice_flags(lua_State *L) {
79    const int j = lua_gettop(L);
80    int flags = 0;
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;
89        } else {
90            return luaL_argerror(L, i, "supported values: "
91             "move, nonblock, more");
92        }
93    }
94    lua_pushinteger(L, flags);
95
96    return 1;
97}
98
99#endif /* _GNU_SOURCE */
100
101/**
102 * sendfile(outfd, infd, length)
103 */
104static 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);
108
109    long spliced = sendfile(sockfd, infd, NULL, len);
110
111    if (spliced < 0) {
112        return nixio__perror(L);
113    }
114
115    lua_pushnumber(L, spliced);
116    return 1;
117}
118
119/* module table */
120static const luaL_reg R[] = {
121#ifdef _GNU_SOURCE
122    {"splice",          nixio_splice},
123    {"splice_flags",    nixio_splice_flags},
124#endif
125    {"sendfile",        nixio_sendfile},
126    {NULL,          NULL}
127};
128
129void nixio_open_splice(lua_State *L) {
130    luaL_register(L, NULL, R);
131}
Note: See TracBrowser for help on using the browser.