s6

Mirror/fork of https://skarnet.org/software/s6/
git clone https://ccx.te2000.cz/git/s6
Log | Files | Refs | README | LICENSE

s6-softlimit.c (2819B)


      1 /* ISC license. */
      2 
      3 #include <sys/types.h>
      4 #include <sys/resource.h>
      5 
      6 #include <skalibs/strerr.h>
      7 #include <skalibs/sgetopt.h>
      8 #include <skalibs/uint64.h>
      9 #include <skalibs/exec.h>
     10 
     11 #define USAGE "s6-softlimit [ -a allbytes ] [ -c corebytes ] [ -d databytes ] [ -f filebytes ] [ -l lockbytes ] [ -m membytes ] [ -o openfiles ] [ -p processes ] [ -r residentbytes ] [ -s stackbytes ] [ -t cpusecs ] prog..."
     12 
     13 static int what = 1 ;
     14 
     15 static void doit (int res, char const *arg)
     16 {
     17   struct rlimit r ;
     18   if (getrlimit(res, &r) < 0) strerr_diefu1sys(111, "getrlimit") ;
     19   if ((arg[0] == '=') && !arg[1])
     20   {
     21     if (what & 2) r.rlim_max = RLIM_INFINITY ;
     22     if (what & 1) r.rlim_cur = r.rlim_max ;
     23   }
     24   else
     25   {
     26     uint64_t n ;
     27     if (!uint640_scan(arg, &n)) strerr_dieusage(100, USAGE) ;
     28     if (what & 2) r.rlim_max = n ;
     29     if (what & 1)
     30     {
     31       if (n > r.rlim_max) n = r.rlim_max ;
     32       r.rlim_cur = n ;
     33     }
     34   }
     35   if (setrlimit(res, &r) < 0) strerr_diefu1sys(111, "setrlimit") ;
     36 }
     37 
     38 int main (int argc, char const *const *argv)
     39 {
     40   subgetopt l = SUBGETOPT_ZERO ;
     41   PROG = "s6-softlimit" ;
     42   for (;;)
     43   {
     44     int opt = subgetopt_r(argc, argv, "hHa:c:d:f:l:m:o:p:r:s:t:", &l) ;
     45     if (opt == -1) break ;
     46     switch (opt)
     47     {
     48       case 'h' : what = 2 ; break ;
     49       case 'H' : what = 3 ; break ;
     50       case 'a' :
     51 #ifdef RLIMIT_AS
     52         doit(RLIMIT_AS, l.arg) ;
     53 #endif
     54 #ifdef RLIMIT_VMEM
     55         doit(RLIMIT_VMEM, l.arg) ;
     56 #endif
     57         break ;
     58       case 'c' :
     59 #ifdef RLIMIT_CORE
     60         doit(RLIMIT_CORE, l.arg) ;
     61 #endif
     62         break ;
     63       case 'd' :
     64 #ifdef RLIMIT_DATA
     65         doit(RLIMIT_DATA, l.arg) ;
     66 #endif
     67         break ;
     68       case 'f' :
     69 #ifdef RLIMIT_FSIZE
     70         doit(RLIMIT_FSIZE, l.arg) ;
     71 #endif
     72         break ;
     73       case 'l' :
     74 #ifdef RLIMIT_MEMLOCK
     75         doit(RLIMIT_MEMLOCK, l.arg) ;
     76 #endif
     77         break ;
     78       case 'm' :
     79 #ifdef RLIMIT_DATA
     80         doit(RLIMIT_DATA, l.arg) ;
     81 #endif
     82 #ifdef RLIMIT_STACK
     83         doit(RLIMIT_STACK, l.arg) ;
     84 #endif
     85 #ifdef RLIMIT_MEMLOCK
     86         doit(RLIMIT_MEMLOCK, l.arg) ;
     87 #endif
     88 #ifdef RLIMIT_VMEM
     89         doit(RLIMIT_VMEM, l.arg) ;
     90 #endif
     91 #ifdef RLIMIT_AS
     92         doit(RLIMIT_AS, l.arg) ;
     93 #endif
     94 	break ;
     95       case 'o' :
     96 #ifdef RLIMIT_NOFILE
     97         doit(RLIMIT_NOFILE, l.arg) ;
     98 #endif
     99 #ifdef RLIMIT_OFILE
    100         doit(RLIMIT_OFILE, l.arg) ;
    101 #endif
    102         break ;
    103       case 'p' :
    104 #ifdef RLIMIT_NPROC
    105         doit(RLIMIT_NPROC, l.arg) ;
    106 #endif
    107         break ;
    108       case 'r' :
    109 #ifdef RLIMIT_RSS
    110         doit(RLIMIT_RSS, l.arg) ;
    111 #endif
    112         break ;
    113       case 's' :
    114 #ifdef RLIMIT_STACK
    115         doit(RLIMIT_STACK, l.arg) ;
    116 #endif
    117         break ;
    118       case 't' :
    119 #ifdef RLIMIT_CPU
    120         doit(RLIMIT_CPU, l.arg) ;
    121 #endif
    122         break ;
    123     }
    124   }
    125   argc -= l.ind ; argv += l.ind ;
    126   if (!argc) strerr_dieusage(100, USAGE) ;
    127   xexec(argv) ;
    128 }