300 * |
300 * |
301 * Example usage: |
301 * Example usage: |
302 * pposix.setrlimit("NOFILE", 1000, 2000) |
302 * pposix.setrlimit("NOFILE", 1000, 2000) |
303 */ |
303 */ |
304 int string2resource(const char *s) { |
304 int string2resource(const char *s) { |
305 if (!strcmp(resource, "CORE")) return RLIMIT_CORE; |
305 if (!strcmp(s, "CORE")) return RLIMIT_CORE; |
306 if (!strcmp(resource, "CPU")) return RLIMIT_CPU; |
306 if (!strcmp(s, "CPU")) return RLIMIT_CPU; |
307 if (!strcmp(resource, "DATA")) return RLIMIT_DATA; |
307 if (!strcmp(s, "DATA")) return RLIMIT_DATA; |
308 if (!strcmp(resource, "FSIZE")) return RLIMIT_FSIZE; |
308 if (!strcmp(s, "FSIZE")) return RLIMIT_FSIZE; |
309 if (!strcmp(resource, "MEMLOCK")) return RLIMIT_MEMLOCK; |
309 if (!strcmp(s, "MEMLOCK")) return RLIMIT_MEMLOCK; |
310 if (!strcmp(resource, "NOFILE")) return RLIMIT_NOFILE; |
310 if (!strcmp(s, "NOFILE")) return RLIMIT_NOFILE; |
311 if (!strcmp(resource, "NPROC")) return RLIMIT_NPROC; |
311 if (!strcmp(s, "NPROC")) return RLIMIT_NPROC; |
312 if (!strcmp(resource, "RSS")) return RLIMIT_RSS; |
312 if (!strcmp(s, "RSS")) return RLIMIT_RSS; |
313 if (!strcmp(resource, "STACK")) return RLIMIT_STACK; |
313 if (!strcmp(s, "STACK")) return RLIMIT_STACK; |
314 return -1; |
314 return -1; |
315 } |
315 } |
316 |
316 |
317 int lc_setrlimit(lua_State *L) { |
317 int lc_setrlimit(lua_State *L) { |
318 int arguments = lua_gettop(L); |
318 int arguments = lua_gettop(L); |
329 softlimit = luaL_checkinteger(L, 2); |
329 softlimit = luaL_checkinteger(L, 2); |
330 hardlimit = luaL_checkinteger(L, 3); |
330 hardlimit = luaL_checkinteger(L, 3); |
331 |
331 |
332 rid = string2resource(resource); |
332 rid = string2resource(resource); |
333 if (rid != -1) { |
333 if (rid != -1) { |
334 rlimit lim; |
334 struct rlimit lim; |
335 rlimit lim_current; |
335 struct rlimit lim_current; |
336 |
336 |
337 if (softlimit < 0 || hardlimit < 0) { |
337 if (softlimit < 0 || hardlimit < 0) { |
338 if (getrlimit(rid, &lim_current)) { |
338 if (getrlimit(rid, &lim_current)) { |
339 lua_pushboolean(L, 0); |
339 lua_pushboolean(L, 0); |
340 lua_pushstring(L, "getrlimit() failed."); |
340 lua_pushstring(L, "getrlimit() failed."); |
341 return 2; |
341 return 2; |
342 } |
342 } |
343 } |
343 } |
344 |
344 |
345 if (softimit < 0) lim.rlim_cur = lim_current.rlim_cur; |
345 if (softlimit < 0) lim.rlim_cur = lim_current.rlim_cur; |
346 else lim.rlim_cur = softlimit; |
346 else lim.rlim_cur = softlimit; |
347 if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max; |
347 if (hardlimit < 0) lim.rlim_max = lim_current.rlim_max; |
348 else lim.rlim_max = hardlimit; |
348 else lim.rlim_max = hardlimit; |
349 |
349 |
350 if (setrlimit(rid, &lim)) { |
350 if (setrlimit(rid, &lim)) { |
363 |
363 |
364 int lc_getrlimit(lua_State *L) { |
364 int lc_getrlimit(lua_State *L) { |
365 int arguments = lua_gettop(L); |
365 int arguments = lua_gettop(L); |
366 const char *resource = NULL; |
366 const char *resource = NULL; |
367 int rid = -1; |
367 int rid = -1; |
368 rlimit lim; |
368 struct rlimit lim; |
369 |
369 |
370 if (arguments != 1) { |
370 if (arguments != 1) { |
371 lua_pushboolean(L, 0); |
371 lua_pushboolean(L, 0); |
372 lua_pushstring(L, "I expect one argument only, the resource string."); |
372 lua_pushstring(L, "I expect one argument only, the resource string."); |
373 return 2; |
373 return 2; |