1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
/*
* Lua polling library
* Copyright (C) 2017-2022 Kim Alvefur
*
* This project is MIT licensed. Please see the
* COPYING file in the source package for more information.
*
*/
#include <unistd.h>
#include <string.h>
#include <errno.h>
#if defined(__linux__)
#define USE_EPOLL
#define POLL_BACKEND "epoll"
#elif defined(__unix__)
#define USE_POLL
#define POLL_BACKEND "poll"
#else
#define USE_SELECT
#define POLL_BACKEND "select"
#endif
#ifdef USE_EPOLL
#include <sys/epoll.h>
#ifndef MAX_EVENTS
#define MAX_EVENTS 64
#endif
#endif
#ifdef USE_POLL
#include <poll.h>
#ifndef MAX_EVENTS
#define MAX_EVENTS 10000
#endif
#endif
#ifdef USE_SELECT
#include <sys/select.h>
#endif
#include <lualib.h>
#include <lauxlib.h>
#define STATE_MT "util.poll<" POLL_BACKEND ">"
#if (LUA_VERSION_NUM < 504)
#define luaL_pushfail lua_pushnil
#endif
/*
* Structure to keep state for each type of API
*/
typedef struct Lpoll_state {
int processed;
#ifdef USE_EPOLL
int epoll_fd;
struct epoll_event events[MAX_EVENTS];
#endif
#ifdef USE_POLL
nfds_t count;
struct pollfd events[MAX_EVENTS];
#endif
#ifdef USE_SELECT
fd_set wantread;
fd_set wantwrite;
fd_set readable;
fd_set writable;
fd_set all;
fd_set err;
#endif
} Lpoll_state;
/*
* Add an FD to be watched
*/
static int Ladd(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
int wantread = lua_toboolean(L, 3);
int wantwrite = lua_toboolean(L, 4);
if(fd < 0) {
luaL_pushfail(L);
lua_pushstring(L, strerror(EBADF));
lua_pushinteger(L, EBADF);
return 3;
}
#ifdef USE_EPOLL
struct epoll_event event;
event.data.fd = fd;
event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_ADD, fd, &event);
if(ret < 0) {
ret = errno;
luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
lua_pushboolean(L, 1);
return 1;
#endif
#ifdef USE_POLL
for(nfds_t i = 0; i < state->count; i++) {
if(state->events[i].fd == fd) {
luaL_pushfail(L);
lua_pushstring(L, strerror(EEXIST));
lua_pushinteger(L, EEXIST);
return 3;
}
}
if(state->count >= MAX_EVENTS) {
luaL_pushfail(L);
lua_pushstring(L, strerror(EMFILE));
lua_pushinteger(L, EMFILE);
return 3;
}
state->events[state->count].fd = fd;
state->events[state->count].events = (wantread ? POLLIN : 0) | (wantwrite ? POLLOUT : 0);
state->events[state->count].revents = 0;
state->count++;
lua_pushboolean(L, 1);
return 1;
#endif
#ifdef USE_SELECT
if(fd > FD_SETSIZE) {
luaL_pushfail(L);
lua_pushstring(L, strerror(EBADF));
lua_pushinteger(L, EBADF);
return 3;
}
if(FD_ISSET(fd, &state->all)) {
luaL_pushfail(L);
lua_pushstring(L, strerror(EEXIST));
lua_pushinteger(L, EEXIST);
return 3;
}
FD_CLR(fd, &state->readable);
FD_CLR(fd, &state->writable);
FD_CLR(fd, &state->err);
FD_SET(fd, &state->all);
if(wantread) {
FD_SET(fd, &state->wantread);
}
else {
FD_CLR(fd, &state->wantread);
}
if(wantwrite) {
FD_SET(fd, &state->wantwrite);
}
else {
FD_CLR(fd, &state->wantwrite);
}
lua_pushboolean(L, 1);
return 1;
#endif
}
/*
* Set events to watch for, readable and/or writable
*/
static int Lset(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
#ifdef USE_EPOLL
int wantread = lua_toboolean(L, 3);
int wantwrite = lua_toboolean(L, 4);
struct epoll_event event;
event.data.fd = fd;
event.events = (wantread ? EPOLLIN : 0) | (wantwrite ? EPOLLOUT : 0);
event.events |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_MOD, fd, &event);
if(ret == 0) {
lua_pushboolean(L, 1);
return 1;
}
else {
ret = errno;
luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
#endif
#ifdef USE_POLL
int wantread = lua_toboolean(L, 3);
int wantwrite = lua_toboolean(L, 4);
for(nfds_t i = 0; i < state->count; i++) {
struct pollfd *event = &state->events[i];
if(event->fd == fd) {
event->events = (wantread ? POLLIN : 0) | (wantwrite ? POLLOUT : 0);
lua_pushboolean(L, 1);
return 1;
} else if(event->fd == -1) {
break;
}
}
luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
#endif
#ifdef USE_SELECT
if(!FD_ISSET(fd, &state->all)) {
luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
}
if(!lua_isnoneornil(L, 3)) {
if(lua_toboolean(L, 3)) {
FD_SET(fd, &state->wantread);
}
else {
FD_CLR(fd, &state->wantread);
}
}
if(!lua_isnoneornil(L, 4)) {
if(lua_toboolean(L, 4)) {
FD_SET(fd, &state->wantwrite);
}
else {
FD_CLR(fd, &state->wantwrite);
}
}
lua_pushboolean(L, 1);
return 1;
#endif
}
/*
* Remove FDs
*/
static int Ldel(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int fd = luaL_checkinteger(L, 2);
#ifdef USE_EPOLL
struct epoll_event event;
event.data.fd = fd;
int ret = epoll_ctl(state->epoll_fd, EPOLL_CTL_DEL, fd, &event);
if(ret == 0) {
lua_pushboolean(L, 1);
return 1;
}
else {
ret = errno;
luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
#endif
#ifdef USE_POLL
if(state->count == 0) {
luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
}
/*
* Move the last item on top of the removed one
*/
struct pollfd *last = &state->events[state->count - 1];
for(nfds_t i = 0; i < state->count; i++) {
struct pollfd *event = &state->events[i];
if(event->fd == fd) {
event->fd = last->fd;
event->events = last->events;
event->revents = last->revents;
last->fd = -1;
state->count--;
lua_pushboolean(L, 1);
return 1;
}
}
luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
#endif
#ifdef USE_SELECT
if(!FD_ISSET(fd, &state->all)) {
luaL_pushfail(L);
lua_pushstring(L, strerror(ENOENT));
lua_pushinteger(L, ENOENT);
return 3;
}
FD_CLR(fd, &state->wantread);
FD_CLR(fd, &state->wantwrite);
FD_CLR(fd, &state->readable);
FD_CLR(fd, &state->writable);
FD_CLR(fd, &state->all);
FD_CLR(fd, &state->err);
lua_pushboolean(L, 1);
return 1;
#endif
}
/*
* Check previously manipulated event state for FDs ready for reading or writing
*/
static int Lpushevent(lua_State *L, struct Lpoll_state *state) {
#ifdef USE_EPOLL
if(state->processed > 0) {
state->processed--;
struct epoll_event event = state->events[state->processed];
lua_pushinteger(L, event.data.fd);
lua_pushboolean(L, event.events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR));
lua_pushboolean(L, event.events & EPOLLOUT);
return 3;
}
#endif
#ifdef USE_POLL
for(int i = state->processed - 1; i >= 0; i--) {
struct pollfd *event = &state->events[i];
if(event->fd != -1 && event->revents != 0) {
lua_pushinteger(L, event->fd);
lua_pushboolean(L, event->revents & (POLLIN | POLLHUP | POLLERR));
lua_pushboolean(L, event->revents & POLLOUT);
event->revents = 0;
state->processed = i;
return 3;
}
}
#endif
#ifdef USE_SELECT
for(int fd = state->processed + 1; fd < FD_SETSIZE; fd++) {
if(FD_ISSET(fd, &state->readable) || FD_ISSET(fd, &state->writable) || FD_ISSET(fd, &state->err)) {
lua_pushinteger(L, fd);
lua_pushboolean(L, FD_ISSET(fd, &state->readable) | FD_ISSET(fd, &state->err));
lua_pushboolean(L, FD_ISSET(fd, &state->writable));
FD_CLR(fd, &state->readable);
FD_CLR(fd, &state->writable);
FD_CLR(fd, &state->err);
state->processed = fd;
return 3;
}
}
#endif
return 0;
}
/*
* Wait for event
*/
static int Lwait(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
int ret = Lpushevent(L, state);
if(ret != 0) {
return ret;
}
lua_Number timeout = luaL_checknumber(L, 2);
luaL_argcheck(L, timeout >= 0, 1, "positive number expected");
#ifdef USE_EPOLL
ret = epoll_wait(state->epoll_fd, state->events, MAX_EVENTS, timeout * 1000);
#endif
#ifdef USE_POLL
ret = poll(state->events, state->count, timeout * 1000);
#endif
#ifdef USE_SELECT
/*
* select(2) mutates the fd_sets passed to it so in order to not
* have to recreate it manually every time a copy is made.
*/
memcpy(&state->readable, &state->wantread, sizeof(fd_set));
memcpy(&state->writable, &state->wantwrite, sizeof(fd_set));
memcpy(&state->err, &state->all, sizeof(fd_set));
struct timeval tv;
tv.tv_sec = (time_t)timeout;
tv.tv_usec = ((suseconds_t)(timeout * 1000000)) % 1000000;
ret = select(FD_SETSIZE, &state->readable, &state->writable, &state->err, &tv);
#endif
if(ret == 0) {
/* Is this an error? */
lua_pushnil(L);
lua_pushstring(L, "timeout");
return 2;
}
else if(ret < 0 && errno == EINTR) {
/* Is this an error? */
lua_pushnil(L);
lua_pushstring(L, "signal");
return 2;
}
else if(ret < 0) {
ret = errno;
luaL_pushfail(L);
lua_pushstring(L, strerror(ret));
lua_pushinteger(L, ret);
return 3;
}
/*
* Search for the first ready FD and return it
*/
#ifdef USE_EPOLL
state->processed = ret;
#endif
#ifdef USE_POLL
state->processed = state->count;
#endif
#ifdef USE_SELECT
state->processed = -1;
#endif
return Lpushevent(L, state);
}
#ifdef USE_EPOLL
/*
* Return Epoll FD
*/
static int Lgetfd(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
lua_pushinteger(L, state->epoll_fd);
return 1;
}
/*
* Close epoll FD
*/
static int Lgc(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
if(state->epoll_fd == -1) {
return 0;
}
if(close(state->epoll_fd) == 0) {
state->epoll_fd = -1;
}
else {
lua_pushstring(L, strerror(errno));
lua_error(L);
}
return 0;
}
#endif
/*
* String representation
*/
static int Ltos(lua_State *L) {
struct Lpoll_state *state = luaL_checkudata(L, 1, STATE_MT);
lua_pushfstring(L, "%s: %p", STATE_MT, state);
return 1;
}
/*
* Create a new context
*/
static int Lnew(lua_State *L) {
/* Allocate state */
Lpoll_state *state = lua_newuserdata(L, sizeof(Lpoll_state));
luaL_setmetatable(L, STATE_MT);
/* Initialize state */
#ifdef USE_EPOLL
state->epoll_fd = -1;
state->processed = 0;
int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if(epoll_fd <= 0) {
luaL_pushfail(L);
lua_pushstring(L, strerror(errno));
lua_pushinteger(L, errno);
return 3;
}
state->epoll_fd = epoll_fd;
#endif
#ifdef USE_POLL
state->processed = -1;
state->count = 0;
for(nfds_t i = 0; i < MAX_EVENTS; i++) {
state->events[i].fd = -1;
state->events[i].events = 0;
state->events[i].revents = 0;
}
#endif
#ifdef USE_SELECT
FD_ZERO(&state->wantread);
FD_ZERO(&state->wantwrite);
FD_ZERO(&state->readable);
FD_ZERO(&state->writable);
FD_ZERO(&state->all);
FD_ZERO(&state->err);
state->processed = FD_SETSIZE;
#endif
return 1;
}
/*
* Open library
*/
int luaopen_util_poll(lua_State *L) {
luaL_checkversion(L);
luaL_newmetatable(L, STATE_MT);
{
lua_pushliteral(L, STATE_MT);
lua_setfield(L, -2, "__name");
lua_pushcfunction(L, Ltos);
lua_setfield(L, -2, "__tostring");
lua_createtable(L, 0, 2);
{
lua_pushcfunction(L, Ladd);
lua_setfield(L, -2, "add");
lua_pushcfunction(L, Lset);
lua_setfield(L, -2, "set");
lua_pushcfunction(L, Ldel);
lua_setfield(L, -2, "del");
lua_pushcfunction(L, Lwait);
lua_setfield(L, -2, "wait");
#ifdef USE_EPOLL
lua_pushcfunction(L, Lgetfd);
lua_setfield(L, -2, "getfd");
#endif
}
lua_setfield(L, -2, "__index");
#ifdef USE_EPOLL
lua_pushcfunction(L, Lgc);
lua_setfield(L, -2, "__gc");
#endif
}
lua_createtable(L, 0, 3);
{
lua_pushcfunction(L, Lnew);
lua_setfield(L, -2, "new");
#define push_errno(named_error) lua_pushinteger(L, named_error);\
lua_setfield(L, -2, #named_error);
push_errno(EEXIST);
push_errno(EMFILE);
push_errno(ENOENT);
lua_pushliteral(L, POLL_BACKEND);
lua_setfield(L, -2, "api");
}
return 1;
}
|