Comparison

util-src/pposix.c @ 13152:792f360a582b

util.pposix: Use Lua enum API for resource limit name argument Because diffstat.
author Kim Alvefur <zash@zash.se>
date Wed, 14 Jun 2023 13:39:39 +0200
parent 12976:a187600ec7d6
child 13184:d16845afb3e2
comparison
equal deleted inserted replaced
13151:7ebb3d6afcd1 13152:792f360a582b
505 * Any negative limit will be replace with the current limit by an additional call of getrlimit(). 505 * Any negative limit will be replace with the current limit by an additional call of getrlimit().
506 * 506 *
507 * Example usage: 507 * Example usage:
508 * pposix.setrlimit("NOFILE", 1000, 2000) 508 * pposix.setrlimit("NOFILE", 1000, 2000)
509 */ 509 */
510 static int string2resource(const char *s) { 510
511 if(!strcmp(s, "CORE")) { 511 static const char *const resource_strings[] = {
512 return RLIMIT_CORE; 512 /* Defined by POSIX */
513 } 513 "CORE",
514 514 "CPU",
515 if(!strcmp(s, "CPU")) { 515 "DATA",
516 return RLIMIT_CPU; 516 "FSIZE",
517 } 517 "NOFILE",
518 518 "STACK",
519 if(!strcmp(s, "DATA")) {
520 return RLIMIT_DATA;
521 }
522
523 if(!strcmp(s, "FSIZE")) {
524 return RLIMIT_FSIZE;
525 }
526
527 if(!strcmp(s, "NOFILE")) {
528 return RLIMIT_NOFILE;
529 }
530
531 if(!strcmp(s, "STACK")) {
532 return RLIMIT_STACK;
533 }
534
535 #if !(defined(sun) || defined(__sun) || defined(__APPLE__)) 519 #if !(defined(sun) || defined(__sun) || defined(__APPLE__))
536 520 "MEMLOCK",
537 if(!strcmp(s, "MEMLOCK")) { 521 "NPROC",
538 return RLIMIT_MEMLOCK; 522 "RSS",
539 }
540
541 if(!strcmp(s, "NPROC")) {
542 return RLIMIT_NPROC;
543 }
544
545 if(!strcmp(s, "RSS")) {
546 return RLIMIT_RSS;
547 }
548
549 #endif 523 #endif
550 #ifdef RLIMIT_NICE 524 #ifdef RLIMIT_NICE
551 525 "NICE",
552 if(!strcmp(s, "NICE")) { 526 #endif
553 return RLIMIT_NICE; 527 NULL
554 } 528 };
555 529
556 #endif 530 static int resource_constants[] = {
557 return -1; 531 RLIMIT_CORE,
558 } 532 RLIMIT_CPU,
533 RLIMIT_DATA,
534 RLIMIT_FSIZE,
535 RLIMIT_NOFILE,
536 RLIMIT_STACK,
537 #if !(defined(sun) || defined(__sun) || defined(__APPLE__))
538 RLIMIT_MEMLOCK,
539 RLIMIT_NPROC,
540 RLIMIT_RSS,
541 #endif
542 #ifdef RLIMIT_NICE
543 RLIMIT_NICE,
544 #endif
545 -1,
546 };
559 547
560 static rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) { 548 static rlim_t arg_to_rlimit(lua_State *L, int idx, rlim_t current) {
561 switch(lua_type(L, idx)) { 549 switch(lua_type(L, idx)) {
562 case LUA_TSTRING: 550 case LUA_TSTRING:
563 551
587 lua_pushboolean(L, 0); 575 lua_pushboolean(L, 0);
588 lua_pushstring(L, "incorrect-arguments"); 576 lua_pushstring(L, "incorrect-arguments");
589 return 2; 577 return 2;
590 } 578 }
591 579
592 rid = string2resource(luaL_checkstring(L, 1)); 580 rid = resource_constants[luaL_checkoption(L, 1,NULL, resource_strings)];
593 581
594 if(rid == -1) { 582 if(rid == -1) {
595 lua_pushboolean(L, 0); 583 lua_pushboolean(L, 0);
596 lua_pushstring(L, "invalid-resource"); 584 lua_pushstring(L, "invalid-resource");
597 return 2; 585 return 2;
617 return 1; 605 return 1;
618 } 606 }
619 607
620 static int lc_getrlimit(lua_State *L) { 608 static int lc_getrlimit(lua_State *L) {
621 int arguments = lua_gettop(L); 609 int arguments = lua_gettop(L);
622 const char *resource = NULL;
623 int rid = -1; 610 int rid = -1;
624 struct rlimit lim; 611 struct rlimit lim;
625 612
626 if(arguments != 1) { 613 if(arguments != 1) {
627 lua_pushboolean(L, 0); 614 lua_pushboolean(L, 0);
628 lua_pushstring(L, "invalid-arguments"); 615 lua_pushstring(L, "invalid-arguments");
629 return 2; 616 return 2;
630 } 617 }
631 618
632 resource = luaL_checkstring(L, 1); 619 rid = resource_constants[luaL_checkoption(L, 1, NULL, resource_strings)];
633 rid = string2resource(resource);
634 620
635 if(rid != -1) { 621 if(rid != -1) {
636 if(getrlimit(rid, &lim)) { 622 if(getrlimit(rid, &lim)) {
637 lua_pushboolean(L, 0); 623 lua_pushboolean(L, 0);
638 lua_pushstring(L, "getrlimit-failed."); 624 lua_pushstring(L, "getrlimit-failed.");