util-src/signal.c
changeset 13443 1e229d710a3c
parent 12980 a187600ec7d6
child 13445 6d96b6eeee5a
equal deleted inserted replaced
13442:0a0dd2505baa 13443:1e229d710a3c
    30 #define _GNU_SOURCE
    30 #define _GNU_SOURCE
    31 #endif
    31 #endif
    32 
    32 
    33 #include <signal.h>
    33 #include <signal.h>
    34 #include <stdlib.h>
    34 #include <stdlib.h>
       
    35 #ifdef __linux__
       
    36 #include <unistd.h>
       
    37 #include <sys/signalfd.h>
       
    38 #endif
    35 
    39 
    36 #include "lua.h"
    40 #include "lua.h"
    37 #include "lauxlib.h"
    41 #include "lauxlib.h"
    38 
    42 
    39 #if (LUA_VERSION_NUM < 503)
    43 #if (LUA_VERSION_NUM < 503)
   366 	return 1;
   370 	return 1;
   367 }
   371 }
   368 
   372 
   369 #endif
   373 #endif
   370 
   374 
       
   375 #ifdef __linux__
       
   376 static int l_signalfd(lua_State *L) {
       
   377 	sigset_t mask;
       
   378 
       
   379 	sigemptyset(&mask);
       
   380 	sigaddset(&mask, luaL_checkinteger(L, 1));
       
   381 
       
   382 	sigprocmask(SIG_BLOCK, &mask, NULL); /* TODO check err */
       
   383 
       
   384 	lua_pushinteger(L, signalfd(-1, &mask, SFD_NONBLOCK));
       
   385 	return 1;
       
   386 }
       
   387 
       
   388 static int l_signalfd_read(lua_State *L) {
       
   389 	const int sigfd = luaL_checkinteger(L, 1);
       
   390 	struct signalfd_siginfo siginfo;
       
   391 
       
   392 	if(read(sigfd, &siginfo, sizeof(siginfo)) < 0) {
       
   393 		return 0;
       
   394 	}
       
   395 
       
   396 	lua_pushinteger(L, siginfo.ssi_signo);
       
   397 	return 1;
       
   398 }
       
   399 #endif
       
   400 
   371 static const struct luaL_Reg lsignal_lib[] = {
   401 static const struct luaL_Reg lsignal_lib[] = {
   372 	{"signal", l_signal},
   402 	{"signal", l_signal},
   373 	{"raise", l_raise},
   403 	{"raise", l_raise},
   374 #if defined(__unix__) || defined(__APPLE__)
   404 #if defined(__unix__) || defined(__APPLE__)
   375 	{"kill", l_kill},
   405 	{"kill", l_kill},
       
   406 #endif
       
   407 #ifdef __linux__
       
   408 	{"signalfd", l_signalfd},
       
   409 	{"signalfd_read", l_signalfd_read},
   376 #endif
   410 #endif
   377 	{NULL, NULL}
   411 	{NULL, NULL}
   378 };
   412 };
   379 
   413 
   380 int luaopen_prosody_util_signal(lua_State *L) {
   414 int luaopen_prosody_util_signal(lua_State *L) {