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.
27 static int nixio_gettimeofday(lua_State
*L
) {
29 gettimeofday(&tv
, NULL
);
30 nixio__pushnumber(L
, tv
.tv_sec
);
31 nixio__pushnumber(L
, tv
.tv_usec
);
39 static int nixio_nanosleep(lua_State
*L
) {
40 struct timespec req
, rem
;
41 req
.tv_sec
= luaL_optint(L
, 1, 0);
42 req
.tv_nsec
= luaL_optint(L
, 2, 0);
44 int status
= nanosleep(&req
, &rem
);
46 lua_pushboolean(L
, 1);
50 lua_pushboolean(L
, 0);
51 lua_pushinteger(L
, rem
.tv_sec
);
52 lua_pushinteger(L
, rem
.tv_nsec
);
55 return nixio__perror(L
);
61 * Checks whether a flag is set in the bitmap and sets the matching table value
63 static void nixio_poll_flags__r(lua_State
*L
, int *map
, int f
, const char *t
) {
66 lua_pushboolean(L
, 1);
74 * Translate integer to poll flags and vice versa
76 static int nixio_poll_flags(lua_State
*L
) {
78 if (lua_isnumber(L
, 1)) {
79 flags
= luaL_checkinteger(L
, 1);
81 nixio_poll_flags__r(L
, &flags
, POLLIN
, "in");
82 nixio_poll_flags__r(L
, &flags
, POLLOUT
, "out");
83 nixio_poll_flags__r(L
, &flags
, POLLERR
, "err");
85 nixio_poll_flags__r(L
, &flags
, POLLPRI
, "pri");
86 nixio_poll_flags__r(L
, &flags
, POLLHUP
, "hup");
87 nixio_poll_flags__r(L
, &flags
, POLLNVAL
, "nval");
91 const int j
= lua_gettop(L
);
92 for (int i
=1; i
<=j
; i
++) {
93 const char *flag
= luaL_checkstring(L
, i
);
94 if (!strcmp(flag
, "in")) {
96 } else if (!strcmp(flag
, "out")) {
98 } else if (!strcmp(flag
, "err")) {
100 } else if (!strcmp(flag
, "pri")) {
104 } else if (!strcmp(flag
, "hup")) {
108 } else if (!strcmp(flag
, "nval")) {
113 return luaL_argerror(L
, i
,
114 "supported values: in, pri, out, err, hup, nval");
117 lua_pushinteger(L
, flags
);
123 * poll({{fd = socket, events = FLAGS}, ...}, timeout)
125 static int nixio_poll(lua_State
*L
) {
126 int len
= lua_objlen(L
, 1);
128 int timeout
= luaL_optint(L
, 2, 0);
131 /* we are being abused as sleep() replacement... */
132 if (lua_isnoneornil(L
, 1) || len
< 1) {
133 if (!poll(NULL
, 0, timeout
)) {
134 lua_pushinteger(L
, 0);
137 return nixio__perror(L
);
141 luaL_checktype(L
, 1, LUA_TTABLE
);
142 struct pollfd
*fds
= calloc(len
, sizeof(struct pollfd
));
144 return luaL_error(L
, NIXIO_OOM
);
147 for (i
= 0; i
< len
; i
++) {
148 lua_rawgeti(L
, 1, i
+1);
149 if (!lua_istable(L
, -1)) {
151 return luaL_argerror(L
, 1, "invalid datastructure");
154 lua_pushliteral(L
, "fd");
156 fd
= nixio__tofd(L
, -1);
159 return luaL_argerror(L
, 1, "invalid fd in datastructure");
163 lua_pushliteral(L
, "events");
165 fds
[i
].events
= (short)lua_tointeger(L
, -1);
170 status
= poll(fds
, (nfds_t
)len
, timeout
);
174 lua_pushboolean(L
, 0);
176 } else if (status
< 0) {
178 return nixio__perror(L
);
181 for (i
= 0; i
< len
; i
++) {
182 lua_rawgeti(L
, 1, i
+1);
184 lua_pushliteral(L
, "revents");
185 lua_pushinteger(L
, fds
[i
].revents
);
193 lua_pushinteger(L
, status
);
200 static const luaL_reg R
[] = {
201 {"gettimeofday", nixio_gettimeofday
},
202 {"nanosleep", nixio_nanosleep
},
203 {"poll", nixio_poll
},
204 {"poll_flags", nixio_poll_flags
},
208 void nixio_open_poll(lua_State
*L
) {
209 luaL_register(L
, NULL
, R
);