Merge pull request #593 from Umeaboy/patch-12
[project/luci.git] / libs / luci-lib-nixio / src / nixio.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 #include "nixio.h"
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <signal.h>
24
25 #define VERSION 0.4
26
27
28 /* pushes nil, error number and errstring on the stack */
29 int nixio__perror(lua_State *L) {
30         if (errno == EAGAIN || errno == EWOULDBLOCK) {
31                 lua_pushboolean(L, 0);
32         } else {
33                 lua_pushnil(L);
34         }
35     lua_pushinteger(L, errno);
36     lua_pushstring(L, strerror(errno));
37     return 3;
38 }
39
40 /* pushes true, if operation succeeded, otherwise call nixio__perror */
41 int nixio__pstatus(lua_State *L, int condition) {
42         if (condition) {
43                 lua_pushboolean(L, 1);
44                 return 1;
45         } else {
46                 return nixio__perror(L);
47         }
48 }
49
50 /* checks whether the first argument is a socket and returns it */
51 nixio_sock* nixio__checksock(lua_State *L) {
52     nixio_sock *sock = (nixio_sock*)luaL_checkudata(L, 1, NIXIO_META);
53     luaL_argcheck(L, sock->fd != -1, 1, "invalid socket object");
54     return sock;
55 }
56
57 /* read fd from nixio_sock object */
58 int nixio__checksockfd(lua_State *L) {
59         return nixio__checksock(L)->fd;
60 }
61
62 /* return any possible fd, otherwise error out */
63 int nixio__checkfd(lua_State *L, int ud) {
64         int fd = nixio__tofd(L, ud);
65         return (fd != -1) ? fd : luaL_argerror(L, ud, "invalid file descriptor");
66 }
67
68 /* return any possible fd */
69 int nixio__tofd(lua_State *L, int ud) {
70         void *udata = lua_touserdata(L, ud);
71         int fd = -1;
72         if (lua_getmetatable(L, ud)) {
73                 luaL_getmetatable(L, NIXIO_META);
74                 luaL_getmetatable(L, NIXIO_FILE_META);
75                 luaL_getmetatable(L, LUA_FILEHANDLE);
76                 if (lua_rawequal(L, -3, -4)) {
77                         fd = ((nixio_sock*)udata)->fd;
78                 } else if (lua_rawequal(L, -2, -4)) {
79                         fd = *((int*)udata);
80                 } else if (lua_rawequal(L, -1, -4)) {
81                         fd = (*((FILE **)udata)) ? fileno(*((FILE **)udata)) : -1;
82                 }
83                 lua_pop(L, 4);
84         }
85         return fd;
86 }
87
88 /* An empty iterator */
89 int nixio__nulliter(lua_State *L) {
90         lua_pushnil(L);
91         return 1;
92 }
93
94 static int nixio_errno(lua_State *L) {
95         lua_pushinteger(L, errno);
96         return 1;
97 }
98
99 static int nixio_strerror(lua_State *L) {
100         lua_pushstring(L, strerror(luaL_checkinteger(L, 1)));
101         return 1;
102 }
103
104 /* object table */
105 static const luaL_reg R[] = {
106         {"errno",               nixio_errno},
107         {"strerror",    nixio_strerror},
108         {NULL,                  NULL}
109 };
110
111 /* entry point */
112 NIXIO_API int luaopen_nixio(lua_State *L) {
113         /* create metatable */
114         luaL_newmetatable(L, NIXIO_META);
115
116         /* metatable.__index = metatable */
117         lua_pushvalue(L, -1);
118         lua_setfield(L, -2, "__index");
119
120         /* register module */
121         luaL_register(L, "nixio", R);
122
123         /* register metatable as socket_meta */
124         lua_pushvalue(L, -2);
125         lua_setfield(L, -2, "meta_socket");
126
127         /* register methods */
128 #ifdef __WINNT__
129         nixio_open__mingw(L);
130 #endif
131         nixio_open_file(L);
132         nixio_open_socket(L);
133         nixio_open_sockopt(L);
134         nixio_open_bind(L);
135         nixio_open_address(L);
136         nixio_open_protoent(L);
137         nixio_open_poll(L);
138         nixio_open_io(L);
139         nixio_open_splice(L);
140         nixio_open_process(L);
141         nixio_open_syslog(L);
142         nixio_open_bit(L);
143         nixio_open_bin(L);
144         nixio_open_fs(L);
145         nixio_open_user(L);
146
147 #ifndef NO_TLS
148         nixio_open_tls_crypto(L);
149         nixio_open_tls_context(L);
150         nixio_open_tls_socket(L);
151 #endif
152
153         /* module version */
154         lua_pushinteger(L, VERSION);
155         lua_setfield(L, -2, "version");
156
157         /* some constants */
158         lua_newtable(L);
159
160         lua_pushliteral(L, NIXIO_SEP);
161         lua_setfield(L, -2, "sep");
162
163         lua_pushliteral(L, NIXIO_PATHSEP);
164         lua_setfield(L, -2, "pathsep");
165
166         lua_pushinteger(L, NIXIO_BUFFERSIZE);
167         lua_setfield(L, -2, "buffersize");
168
169         NIXIO_PUSH_CONSTANT(EACCES);
170         NIXIO_PUSH_CONSTANT(EINTR);
171         NIXIO_PUSH_CONSTANT(ENOSYS);
172         NIXIO_PUSH_CONSTANT(EINVAL);
173         NIXIO_PUSH_CONSTANT(EAGAIN);
174         NIXIO_PUSH_CONSTANT(ENOMEM);
175         NIXIO_PUSH_CONSTANT(ENOENT);
176         NIXIO_PUSH_CONSTANT(ECHILD);
177         NIXIO_PUSH_CONSTANT(EIO);
178         NIXIO_PUSH_CONSTANT(EBADF);
179         NIXIO_PUSH_CONSTANT(EFAULT);
180         NIXIO_PUSH_CONSTANT(EFBIG);
181         NIXIO_PUSH_CONSTANT(ENOSPC);
182         NIXIO_PUSH_CONSTANT(EPIPE);
183         NIXIO_PUSH_CONSTANT(ESPIPE);
184         NIXIO_PUSH_CONSTANT(EISDIR);
185         NIXIO_PUSH_CONSTANT(EPERM);
186         NIXIO_PUSH_CONSTANT(EEXIST);
187         NIXIO_PUSH_CONSTANT(EMFILE);
188         NIXIO_PUSH_CONSTANT(ENAMETOOLONG);
189         NIXIO_PUSH_CONSTANT(ENFILE);
190         NIXIO_PUSH_CONSTANT(ENODEV);
191         NIXIO_PUSH_CONSTANT(EXDEV);
192         NIXIO_PUSH_CONSTANT(ENOTDIR);
193         NIXIO_PUSH_CONSTANT(ENXIO);
194         NIXIO_PUSH_CONSTANT(EROFS);
195         NIXIO_PUSH_CONSTANT(EBUSY);
196         NIXIO_PUSH_CONSTANT(ESRCH);
197         NIXIO_PUSH_CONSTANT(SIGINT);
198         NIXIO_PUSH_CONSTANT(SIGTERM);
199         NIXIO_PUSH_CONSTANT(SIGSEGV);
200
201 #ifndef __WINNT__
202         NIXIO_PUSH_CONSTANT(EALREADY);
203         NIXIO_PUSH_CONSTANT(EINPROGRESS);
204         NIXIO_PUSH_CONSTANT(EWOULDBLOCK);
205         NIXIO_PUSH_CONSTANT(ELOOP);
206         NIXIO_PUSH_CONSTANT(EOVERFLOW);
207         NIXIO_PUSH_CONSTANT(ETXTBSY);
208         NIXIO_PUSH_CONSTANT(EAFNOSUPPORT);
209         NIXIO_PUSH_CONSTANT(ENOBUFS);
210         NIXIO_PUSH_CONSTANT(EPROTONOSUPPORT);
211         NIXIO_PUSH_CONSTANT(ENOPROTOOPT);
212         NIXIO_PUSH_CONSTANT(EADDRINUSE);
213         NIXIO_PUSH_CONSTANT(ENETDOWN);
214         NIXIO_PUSH_CONSTANT(ENETUNREACH);
215
216         NIXIO_PUSH_CONSTANT(SIGALRM);
217         NIXIO_PUSH_CONSTANT(SIGKILL);
218         NIXIO_PUSH_CONSTANT(SIGHUP);
219         NIXIO_PUSH_CONSTANT(SIGSTOP);
220         NIXIO_PUSH_CONSTANT(SIGCONT);
221         NIXIO_PUSH_CONSTANT(SIGCHLD);
222         NIXIO_PUSH_CONSTANT(SIGQUIT);
223         NIXIO_PUSH_CONSTANT(SIGUSR1);
224         NIXIO_PUSH_CONSTANT(SIGUSR2);
225         NIXIO_PUSH_CONSTANT(SIGIO);
226         NIXIO_PUSH_CONSTANT(SIGURG);
227         NIXIO_PUSH_CONSTANT(SIGPIPE);
228
229         lua_pushvalue(L, -1);
230         lua_setfield(L, -3, "const_sock");
231
232         signal(SIGPIPE, SIG_IGN);
233 #endif /* !__WINNT__ */
234         lua_setfield(L, -2, "const");
235
236         /* remove meta table */
237         lua_remove(L, -2);
238
239         return 1;
240 }