Comparison

util-src/pposix.c @ 859:43f7e342135d

Adding setrlimits() binding.
author Tobias Markmann <tm@ayena.de>
date Sun, 22 Feb 2009 20:35:41 +0100
parent 804:9bc1544c99b7
child 860:048aaec22b57
comparison
equal deleted inserted replaced
814:f12b1ddd458d 859:43f7e342135d
1 /* Prosody IM v0.3 1 /* Prosody IM v0.3
2 -- Copyright (C) 2008-2009 Matthew Wild 2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain 3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- Copyright 2009 Tobias Markmann
4 -- 5 --
5 -- This project is MIT/X11 licensed. Please see the 6 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 7 -- COPYING file in the source package for more information.
7 -- 8 --
8 */ 9 */
15 #define MODULE_VERSION "0.3.0" 16 #define MODULE_VERSION "0.3.0"
16 17
17 #include <stdlib.h> 18 #include <stdlib.h>
18 #include <unistd.h> 19 #include <unistd.h>
19 #include <libgen.h> 20 #include <libgen.h>
21 #include <sys/resource.h>
20 #include <sys/types.h> 22 #include <sys/types.h>
21 #include <sys/stat.h> 23 #include <sys/stat.h>
22 #include <fcntl.h> 24 #include <fcntl.h>
23 25
24 #include <syslog.h> 26 #include <syslog.h>
287 lua_pushboolean(L, 0); 289 lua_pushboolean(L, 0);
288 lua_pushstring(L, "invalid-uid"); 290 lua_pushstring(L, "invalid-uid");
289 return 2; 291 return 2;
290 } 292 }
291 293
294 /* Like POSIX's setrlimit()/getrlimit() API functions.
295 *
296 * Syntax:
297 * pposix.setrlimit( resource, soft limit, hard limit)
298 *
299 * Any negative limit will be replace with the current limit by an additional call of getrlimit().
300 *
301 * Example usage:
302 * pposix.setrlimit("NOFILE", 1000, 2000)
303 */
304 int string2resource(const char *s) {
305 if (!strcmp(resource, "CORE")) return RLIMIT_CORE;
306 if (!strcmp(resource, "CPU")) return RLIMIT_CPU;
307 if (!strcmp(resource, "DATA")) return RLIMIT_DATA;
308 if (!strcmp(resource, "FSIZE")) return RLIMIT_FSIZE;
309 if (!strcmp(resource, "MEMLOCK")) return RLIMIT_MEMLOCK;
310 if (!strcmp(resource, "NOFILE")) return RLIMIT_NOFILE;
311 if (!strcmp(resource, "NPROC")) return RLIMIT_NPROC;
312 if (!strcmp(resource, "RSS")) return RLIMIT_RSS;
313 if (!strcmp(resource, "STACK")) return RLIMIT_STACK;
314 return -1;
315 }
316
317 int lc_setrlimit(lua_State *L) {
318 int arguments = lua_gettop(L);
319 int softlimit = -1;
320 int hardlimit = -1;
321 const char *resource = NULL;
322 int rid = -1;
323 if(arguments < 1 || arguments > 3) {
324 lua_pushboolean(L, 0);
325 lua_pushstring(L, "Wrong number of arguments");
326 }
327
328 resource = luaL_checkstring(L, 1);
329 softlimit = luaL_checkinteger(L, 2);
330 hardlimit = luaL_checkinteger(L, 3);
331
332 rid = string2resource(resource);
333 if (rid != -1) {
334 rlimit lim;
335 rlimit lim_current;
336
337 if (softlimit < 0 || hardlimit < 0) {
338 if (getrlimit(rid, &lim_current)) {
339 lua_pushboolean(L, 0);
340 lua_pushstring(L, "getrlimit() failed.");
341 return 2;
342 }
343 }
344
345 if (softimit < 0) lim.rlim_cur = lim_current.rlim_cur;
346 else lim.rlim_cur = softlimit;
347 if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max;
348 else lim.rlim_max = hardlimit;
349
350 if (setrlimit(rid, &lim)) {
351 lua_pushboolean(L, 0);
352 lua_pushstring(L, "setrlimit() failed.");
353 return 2;
354 }
355 } else {
356 lua_pushboolean(L, 0);
357 lua_pushstring(L, "Unsupported resoucrce. Sorry I'm pretty limited by POSIX standard.");
358 return 2;
359 }
360 lua_pushboolean(L, 1);
361 return 1;
362 }
363
364 int lc_getrlimit(lua_State *L) {
365 return 0;
366 }
367
292 /* Register functions */ 368 /* Register functions */
293 369
294 int luaopen_util_pposix(lua_State *L) 370 int luaopen_util_pposix(lua_State *L)
295 { 371 {
296 lua_newtable(L); 372 lua_newtable(L);
316 lua_pushcfunction(L, lc_getuid); 392 lua_pushcfunction(L, lc_getuid);
317 lua_setfield(L, -2, "getuid"); 393 lua_setfield(L, -2, "getuid");
318 394
319 lua_pushcfunction(L, lc_setuid); 395 lua_pushcfunction(L, lc_setuid);
320 lua_setfield(L, -2, "setuid"); 396 lua_setfield(L, -2, "setuid");
397
398 lua_pushcfunction(L, lc_setrlimit);
399 lua_setfield(L, -2, "setrlimit");
400
401 lua_pushcfunction(L, lc_getrlimit);
402 lua_setfield(L, -2, "getrlimit");
321 403
322 lua_pushliteral(L, "pposix"); 404 lua_pushliteral(L, "pposix");
323 lua_setfield(L, -2, "_NAME"); 405 lua_setfield(L, -2, "_NAME");
324 406
325 lua_pushliteral(L, MODULE_VERSION); 407 lua_pushliteral(L, MODULE_VERSION);