nixio: added syslog support
[project/luci.git] / libs / 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.2
26
27
28 /* pushes nil, error number and errstring on the stack */
29 int nixio__perror(lua_State *L) {
30 if (errno == EAGAIN) {
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 static int nixio_strerror(lua_State *L) {
89 lua_pushstring(L, strerror(luaL_checkinteger(L, 1)));
90 return 1;
91 }
92
93 /* object table */
94 static const luaL_reg R[] = {
95 {"strerror", nixio_strerror},
96 {NULL, NULL}
97 };
98
99 /* entry point */
100 LUALIB_API int luaopen_nixio(lua_State *L) {
101 /* create metatable */
102 luaL_newmetatable(L, NIXIO_META);
103
104 /* metatable.__index = metatable */
105 lua_pushvalue(L, -1);
106 lua_setfield(L, -2, "__index");
107
108 /* register module */
109 luaL_register(L, "nixio", R);
110
111 /* register metatable as socket_meta */
112 lua_pushvalue(L, -2);
113 lua_setfield(L, -2, "meta_socket");
114
115 /* register methods */
116 nixio_open_file(L);
117 nixio_open_socket(L);
118 nixio_open_sockopt(L);
119 nixio_open_bind(L);
120 nixio_open_address(L);
121 nixio_open_poll(L);
122 nixio_open_io(L);
123 nixio_open_splice(L);
124 nixio_open_process(L);
125 nixio_open_syslog(L);
126 nixio_open_tls_context(L);
127 nixio_open_tls_socket(L);
128
129 /* module version */
130 lua_pushnumber(L, VERSION);
131 lua_setfield(L, -2, "version");
132
133 /* some constants */
134 lua_createtable(L, 0, 49);
135
136 NIXIO_PUSH_CONSTANT(EACCES);
137 NIXIO_PUSH_CONSTANT(EINTR);
138 NIXIO_PUSH_CONSTANT(ENOSYS);
139 NIXIO_PUSH_CONSTANT(EINVAL);
140 NIXIO_PUSH_CONSTANT(EWOULDBLOCK);
141 NIXIO_PUSH_CONSTANT(EAGAIN);
142 NIXIO_PUSH_CONSTANT(ENOMEM);
143 NIXIO_PUSH_CONSTANT(ENOENT);
144 NIXIO_PUSH_CONSTANT(ECHILD);
145 NIXIO_PUSH_CONSTANT(EIO);
146 NIXIO_PUSH_CONSTANT(EBADF);
147 NIXIO_PUSH_CONSTANT(EFAULT);
148 NIXIO_PUSH_CONSTANT(EFBIG);
149 NIXIO_PUSH_CONSTANT(ENOSPC);
150 NIXIO_PUSH_CONSTANT(EPIPE);
151 NIXIO_PUSH_CONSTANT(ESPIPE);
152 NIXIO_PUSH_CONSTANT(EISDIR);
153 NIXIO_PUSH_CONSTANT(EPERM);
154 NIXIO_PUSH_CONSTANT(EEXIST);
155 NIXIO_PUSH_CONSTANT(ELOOP);
156 NIXIO_PUSH_CONSTANT(EMFILE);
157 NIXIO_PUSH_CONSTANT(ENAMETOOLONG);
158 NIXIO_PUSH_CONSTANT(ENFILE);
159 NIXIO_PUSH_CONSTANT(ENODEV);
160 NIXIO_PUSH_CONSTANT(ENOTDIR);
161 NIXIO_PUSH_CONSTANT(ENXIO);
162 NIXIO_PUSH_CONSTANT(EOVERFLOW);
163 NIXIO_PUSH_CONSTANT(EROFS);
164 NIXIO_PUSH_CONSTANT(ETXTBSY);
165 NIXIO_PUSH_CONSTANT(EAFNOSUPPORT);
166 NIXIO_PUSH_CONSTANT(ENOBUFS);
167 NIXIO_PUSH_CONSTANT(EPROTONOSUPPORT);
168 NIXIO_PUSH_CONSTANT(ENOPROTOOPT);
169 NIXIO_PUSH_CONSTANT(EBUSY);
170 NIXIO_PUSH_CONSTANT(ESRCH);
171 NIXIO_PUSH_CONSTANT(SIGALRM);
172 NIXIO_PUSH_CONSTANT(SIGINT);
173 NIXIO_PUSH_CONSTANT(SIGTERM);
174 NIXIO_PUSH_CONSTANT(SIGKILL);
175 NIXIO_PUSH_CONSTANT(SIGHUP);
176 NIXIO_PUSH_CONSTANT(SIGSTOP);
177 NIXIO_PUSH_CONSTANT(SIGCONT);
178 NIXIO_PUSH_CONSTANT(SIGSEGV);
179 NIXIO_PUSH_CONSTANT(SIGCHLD);
180 NIXIO_PUSH_CONSTANT(SIGQUIT);
181 NIXIO_PUSH_CONSTANT(SIGUSR1);
182 NIXIO_PUSH_CONSTANT(SIGUSR2);
183 NIXIO_PUSH_CONSTANT(SIGIO);
184 NIXIO_PUSH_CONSTANT(SIGURG);
185
186 lua_setfield(L, -2, "const");
187
188 /* remove meta table */
189 lua_remove(L, -2);
190
191 return 1;
192 }