Software /
code /
prosody
Comparison
util-src/signal.c @ 10799:763bb2ce3f60
util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3]
Passing around PIDs, UIDs etc as integers makes it more sane in Lua 5.3.
Getting 1234.0 as PID is silly. Shouldn't change any behavior as these
are all integers on the C side and the integral floats are accepted as
integers when passed back from Lua into C.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 04 May 2020 21:51:30 +0200 |
parent | 10480:94cacf9fd0ae |
child | 12575:1f6f05a98fcd |
comparison
equal
deleted
inserted
replaced
10798:b81f4fd7f21a | 10799:763bb2ce3f60 |
---|---|
37 #include "lauxlib.h" | 37 #include "lauxlib.h" |
38 | 38 |
39 #if (LUA_VERSION_NUM == 501) | 39 #if (LUA_VERSION_NUM == 501) |
40 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) | 40 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R) |
41 #endif | 41 #endif |
42 #if (LUA_VERSION_NUM < 503) | |
43 #define lua_isinteger(L, n) lua_isnumber(L, n) | |
44 #endif | |
42 | 45 |
43 #ifndef lsig | 46 #ifndef lsig |
44 | 47 |
45 #define lsig | 48 #define lsig |
46 | 49 |
174 | 177 |
175 lua_pushstring(L, LUA_SIGNAL); | 178 lua_pushstring(L, LUA_SIGNAL); |
176 lua_gettable(L, LUA_REGISTRYINDEX); | 179 lua_gettable(L, LUA_REGISTRYINDEX); |
177 | 180 |
178 for(int i = 0; i < nsig; i++) { | 181 for(int i = 0; i < nsig; i++) { |
179 lua_pushnumber(L, signals[i]); | 182 lua_pushinteger(L, signals[i]); |
180 lua_gettable(L, -2); | 183 lua_gettable(L, -2); |
181 lua_call(L, 0, 0); | 184 lua_call(L, 0, 0); |
182 }; | 185 }; |
183 | 186 |
184 nsig = 0; | 187 nsig = 0; |
221 /* get type of signal */ | 224 /* get type of signal */ |
222 luaL_checkany(L, 1); | 225 luaL_checkany(L, 1); |
223 t = lua_type(L, 1); | 226 t = lua_type(L, 1); |
224 | 227 |
225 if(t == LUA_TNUMBER) { | 228 if(t == LUA_TNUMBER) { |
226 sig = (int) lua_tonumber(L, 1); | 229 sig = (int) lua_tointeger(L, 1); |
227 } else if(t == LUA_TSTRING) { | 230 } else if(t == LUA_TSTRING) { |
228 lua_pushstring(L, LUA_SIGNAL); | 231 lua_pushstring(L, LUA_SIGNAL); |
229 lua_gettable(L, LUA_REGISTRYINDEX); | 232 lua_gettable(L, LUA_REGISTRYINDEX); |
230 lua_pushvalue(L, 1); | 233 lua_pushvalue(L, 1); |
231 lua_gettable(L, -2); | 234 lua_gettable(L, -2); |
232 | 235 |
233 if(!lua_isnumber(L, -1)) { | 236 if(!lua_isinteger(L, -1)) { |
234 return luaL_error(L, "invalid signal string"); | 237 return luaL_error(L, "invalid signal string"); |
235 } | 238 } |
236 | 239 |
237 sig = (int) lua_tonumber(L, -1); | 240 sig = (int) lua_tointeger(L, -1); |
238 lua_pop(L, 1); /* get rid of number we pushed */ | 241 lua_pop(L, 1); /* get rid of number we pushed */ |
239 } else { | 242 } else { |
240 luaL_checknumber(L, 1); /* will always error, with good error msg */ | 243 luaL_checknumber(L, 1); /* will always error, with good error msg */ |
241 return luaL_error(L, "unreachable: invalid number was accepted"); | 244 return luaL_error(L, "unreachable: invalid number was accepted"); |
242 } | 245 } |
243 | 246 |
244 /* set handler */ | 247 /* set handler */ |
245 if(args == 1 || lua_isnil(L, 2)) { /* clear handler */ | 248 if(args == 1 || lua_isnil(L, 2)) { /* clear handler */ |
246 lua_pushstring(L, LUA_SIGNAL); | 249 lua_pushstring(L, LUA_SIGNAL); |
247 lua_gettable(L, LUA_REGISTRYINDEX); | 250 lua_gettable(L, LUA_REGISTRYINDEX); |
248 lua_pushnumber(L, sig); | 251 lua_pushinteger(L, sig); |
249 lua_gettable(L, -2); /* return old handler */ | 252 lua_gettable(L, -2); /* return old handler */ |
250 lua_pushnumber(L, sig); | 253 lua_pushinteger(L, sig); |
251 lua_pushnil(L); | 254 lua_pushnil(L); |
252 lua_settable(L, -4); | 255 lua_settable(L, -4); |
253 lua_remove(L, -2); /* remove LUA_SIGNAL table */ | 256 lua_remove(L, -2); /* remove LUA_SIGNAL table */ |
254 signal(sig, SIG_DFL); | 257 signal(sig, SIG_DFL); |
255 } else { | 258 } else { |
256 luaL_checktype(L, 2, LUA_TFUNCTION); | 259 luaL_checktype(L, 2, LUA_TFUNCTION); |
257 | 260 |
258 lua_pushstring(L, LUA_SIGNAL); | 261 lua_pushstring(L, LUA_SIGNAL); |
259 lua_gettable(L, LUA_REGISTRYINDEX); | 262 lua_gettable(L, LUA_REGISTRYINDEX); |
260 | 263 |
261 lua_pushnumber(L, sig); | 264 lua_pushinteger(L, sig); |
262 lua_pushvalue(L, 2); | 265 lua_pushvalue(L, 2); |
263 lua_settable(L, -3); | 266 lua_settable(L, -3); |
264 | 267 |
265 /* Set the state for the handler */ | 268 /* Set the state for the handler */ |
266 Lsig = L; | 269 Lsig = L; |
290 */ | 293 */ |
291 | 294 |
292 static int l_raise(lua_State *L) { | 295 static int l_raise(lua_State *L) { |
293 /* int args = lua_gettop(L); */ | 296 /* int args = lua_gettop(L); */ |
294 int t = 0; /* type */ | 297 int t = 0; /* type */ |
295 lua_Number ret; | 298 lua_Integer ret; |
296 | 299 |
297 luaL_checkany(L, 1); | 300 luaL_checkany(L, 1); |
298 | 301 |
299 t = lua_type(L, 1); | 302 t = lua_type(L, 1); |
300 | 303 |
301 if(t == LUA_TNUMBER) { | 304 if(t == LUA_TNUMBER) { |
302 ret = (lua_Number) raise((int) lua_tonumber(L, 1)); | 305 ret = (lua_Integer) raise((int) lua_tointeger(L, 1)); |
303 lua_pushnumber(L, ret); | 306 lua_pushinteger(L, ret); |
304 } else if(t == LUA_TSTRING) { | 307 } else if(t == LUA_TSTRING) { |
305 lua_pushstring(L, LUA_SIGNAL); | 308 lua_pushstring(L, LUA_SIGNAL); |
306 lua_gettable(L, LUA_REGISTRYINDEX); | 309 lua_gettable(L, LUA_REGISTRYINDEX); |
307 lua_pushvalue(L, 1); | 310 lua_pushvalue(L, 1); |
308 lua_gettable(L, -2); | 311 lua_gettable(L, -2); |
309 | 312 |
310 if(!lua_isnumber(L, -1)) { | 313 if(!lua_isnumber(L, -1)) { |
311 return luaL_error(L, "invalid signal string"); | 314 return luaL_error(L, "invalid signal string"); |
312 } | 315 } |
313 | 316 |
314 ret = (lua_Number) raise((int) lua_tonumber(L, -1)); | 317 ret = (lua_Integer) raise((int) lua_tointeger(L, -1)); |
315 lua_pop(L, 1); /* get rid of number we pushed */ | 318 lua_pop(L, 1); /* get rid of number we pushed */ |
316 lua_pushnumber(L, ret); | 319 lua_pushinteger(L, ret); |
317 } else { | 320 } else { |
318 luaL_checknumber(L, 1); /* will always error, with good error msg */ | 321 luaL_checknumber(L, 1); /* will always error, with good error msg */ |
319 } | 322 } |
320 | 323 |
321 return 1; | 324 return 1; |
332 * signal = signal number or string | 335 * signal = signal number or string |
333 */ | 336 */ |
334 | 337 |
335 static int l_kill(lua_State *L) { | 338 static int l_kill(lua_State *L) { |
336 int t; /* type */ | 339 int t; /* type */ |
337 lua_Number ret; /* return value */ | 340 lua_Integer ret; /* return value */ |
338 | 341 |
339 luaL_checknumber(L, 1); /* must be int for pid */ | 342 luaL_checknumber(L, 1); /* must be int for pid */ |
340 luaL_checkany(L, 2); /* check for a second arg */ | 343 luaL_checkany(L, 2); /* check for a second arg */ |
341 | 344 |
342 t = lua_type(L, 2); | 345 t = lua_type(L, 2); |
343 | 346 |
344 if(t == LUA_TNUMBER) { | 347 if(t == LUA_TNUMBER) { |
345 ret = (lua_Number) kill((int) lua_tonumber(L, 1), | 348 ret = (lua_Integer) kill((int) lua_tointeger(L, 1), |
346 (int) lua_tonumber(L, 2)); | 349 (int) lua_tointeger(L, 2)); |
347 lua_pushnumber(L, ret); | 350 lua_pushinteger(L, ret); |
348 } else if(t == LUA_TSTRING) { | 351 } else if(t == LUA_TSTRING) { |
349 lua_pushstring(L, LUA_SIGNAL); | 352 lua_pushstring(L, LUA_SIGNAL); |
350 lua_gettable(L, LUA_REGISTRYINDEX); | 353 lua_gettable(L, LUA_REGISTRYINDEX); |
351 lua_pushvalue(L, 2); | 354 lua_pushvalue(L, 2); |
352 lua_gettable(L, -2); | 355 lua_gettable(L, -2); |
353 | 356 |
354 if(!lua_isnumber(L, -1)) { | 357 if(!lua_isnumber(L, -1)) { |
355 return luaL_error(L, "invalid signal string"); | 358 return luaL_error(L, "invalid signal string"); |
356 } | 359 } |
357 | 360 |
358 ret = (lua_Number) kill((int) lua_tonumber(L, 1), | 361 ret = (lua_Integer) kill((int) lua_tointeger(L, 1), |
359 (int) lua_tonumber(L, -1)); | 362 (int) lua_tointeger(L, -1)); |
360 lua_pop(L, 1); /* get rid of number we pushed */ | 363 lua_pop(L, 1); /* get rid of number we pushed */ |
361 lua_pushnumber(L, ret); | 364 lua_pushinteger(L, ret); |
362 } else { | 365 } else { |
363 luaL_checknumber(L, 2); /* will always error, with good error msg */ | 366 luaL_checknumber(L, 2); /* will always error, with good error msg */ |
364 } | 367 } |
365 | 368 |
366 return 1; | 369 return 1; |
394 lua_newtable(L); | 397 lua_newtable(L); |
395 | 398 |
396 while(lua_signals[i].name != NULL) { | 399 while(lua_signals[i].name != NULL) { |
397 /* registry table */ | 400 /* registry table */ |
398 lua_pushstring(L, lua_signals[i].name); | 401 lua_pushstring(L, lua_signals[i].name); |
399 lua_pushnumber(L, lua_signals[i].sig); | 402 lua_pushinteger(L, lua_signals[i].sig); |
400 lua_settable(L, -3); | 403 lua_settable(L, -3); |
401 /* signal table */ | 404 /* signal table */ |
402 lua_pushstring(L, lua_signals[i].name); | 405 lua_pushstring(L, lua_signals[i].name); |
403 lua_pushnumber(L, lua_signals[i].sig); | 406 lua_pushinteger(L, lua_signals[i].sig); |
404 lua_settable(L, -5); | 407 lua_settable(L, -5); |
405 i++; | 408 i++; |
406 } | 409 } |
407 | 410 |
408 /* add newtable to the registry */ | 411 /* add newtable to the registry */ |