| 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 | |
|---|
| 63 | #undef SPLICE_F_MOVE |
|---|
| 64 | #undef SPLICE_F_NONBLOCK |
|---|
| 65 | #undef SPLICE_F_MORE |
|---|
| 66 | |
|---|
| 67 | #define SPLICE_F_MOVE 1 |
|---|
| 68 | #define SPLICE_F_NONBLOCK 2 |
|---|
| 69 | #define SPLICE_F_MORE 4 |
|---|
| 70 | |
|---|
| 71 | #endif /* __UCLIBC__ */ |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * splice(fd_in, fd_out, length, flags) |
|---|
| 75 | */ |
|---|
| 76 | static int nixio_splice(lua_State *L) { |
|---|
| 77 | int fd_in = nixio__checkfd(L, 1); |
|---|
| 78 | int fd_out = nixio__checkfd(L, 2); |
|---|
| 79 | size_t len = luaL_checkinteger(L, 3); |
|---|
| 80 | int flags = luaL_optinteger(L, 4, 0); |
|---|
| 81 | long spliced; |
|---|
| 82 | |
|---|
| 83 | do { |
|---|
| 84 | spliced = splice(fd_in, NULL, fd_out, NULL, len, flags); |
|---|
| 85 | } while (spliced == -1 && errno == EINTR); |
|---|
| 86 | |
|---|
| 87 | if (spliced < 0) { |
|---|
| 88 | return nixio__perror(L); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | lua_pushinteger(L, spliced); |
|---|
| 92 | return 1; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Translate splice flags to integer |
|---|
| 97 | */ |
|---|
| 98 | static int nixio_splice_flags(lua_State *L) { |
|---|
| 99 | const int j = lua_gettop(L); |
|---|
| 100 | int flags = 0; |
|---|
| 101 | for (int i=1; i<=j; i++) { |
|---|
| 102 | const char *flag = luaL_checkstring(L, i); |
|---|
| 103 | if (!strcmp(flag, "move")) { |
|---|
| 104 | flags |= SPLICE_F_MOVE; |
|---|
| 105 | } else if (!strcmp(flag, "nonblock")) { |
|---|
| 106 | flags |= SPLICE_F_NONBLOCK; |
|---|
| 107 | } else if (!strcmp(flag, "more")) { |
|---|
| 108 | flags |= SPLICE_F_MORE; |
|---|
| 109 | } else { |
|---|
| 110 | return luaL_argerror(L, i, "supported values: " |
|---|
| 111 | "move, nonblock, more"); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | lua_pushinteger(L, flags); |
|---|
| 115 | |
|---|
| 116 | return 1; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | #endif /* _GNU_SOURCE */ |
|---|
| 120 | |
|---|
| 121 | /** |
|---|
| 122 | * sendfile(outfd, infd, length) |
|---|
| 123 | */ |
|---|
| 124 | static int nixio_sendfile(lua_State *L) { |
|---|
| 125 | int sock = nixio__checksockfd(L); |
|---|
| 126 | int infd = nixio__checkfd(L, 2); |
|---|
| 127 | size_t len = luaL_checkinteger(L, 3); |
|---|
| 128 | off_t spliced; |
|---|
| 129 | |
|---|
| 130 | #ifndef BSD |
|---|
| 131 | do { |
|---|
| 132 | spliced = sendfile(sock, infd, NULL, len); |
|---|
| 133 | } while (spliced == -1 && errno == EINTR); |
|---|
| 134 | |
|---|
| 135 | if (spliced == -1) { |
|---|
| 136 | return nixio__perror(L); |
|---|
| 137 | } |
|---|
| 138 | #else |
|---|
| 139 | int r; |
|---|
| 140 | const off_t offset = lseek(infd, 0, SEEK_CUR); |
|---|
| 141 | |
|---|
| 142 | do { |
|---|
| 143 | r = sendfile(infd, sock, offset, len, NULL, &spliced, 0); |
|---|
| 144 | } while (r == -1 && errno == EINTR); |
|---|
| 145 | |
|---|
| 146 | if (r == -1) { |
|---|
| 147 | return nixio__perror(L); |
|---|
| 148 | } |
|---|
| 149 | #endif |
|---|
| 150 | |
|---|
| 151 | lua_pushinteger(L, spliced); |
|---|
| 152 | return 1; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /* module table */ |
|---|
| 156 | static const luaL_reg R[] = { |
|---|
| 157 | #ifdef _GNU_SOURCE |
|---|
| 158 | {"splice", nixio_splice}, |
|---|
| 159 | {"splice_flags", nixio_splice_flags}, |
|---|
| 160 | #endif |
|---|
| 161 | {"sendfile", nixio_sendfile}, |
|---|
| 162 | {NULL, NULL} |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | void nixio_open_splice(lua_State *L) { |
|---|
| 167 | luaL_register(L, NULL, R); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | #else /* __WINNT__ */ |
|---|
| 171 | |
|---|
| 172 | void nixio_open_splice(lua_State *L) { |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | #endif /* !__WINNT__ */ |
|---|