toybox/toys/posix/ulimit.c
<<
>>
Prefs
   1/* ulimit.c - Modify resource limits
   2 *
   3 * Copyright 2015 Rob Landley <rob@landley.net>
   4 *
   5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ulimit.html
   6 * And man prlimit(2).
   7 *
   8 * Deviations from posix: The units on -f are supposed to be 512 byte
   9 * "blocks" (no other options are specified, and even hard drives don't
  10 * do that anymore). Bash uses 1024 byte blocks, so they don't care either.
  11 * We consistently use bytes everywhere we can.
  12 *
  13 * Deviations from bash: Sizes are in bytes (instead of -p 512 and -f 1024).
  14 * Bash's -p value has been wrong since 2010 (git 35f3d14dbbc5).
  15 * The kernel implementation of RLIMIT_LOCKS (-x) was removed from Linux in
  16 * 2003. Bash never implemented -b (it's in the help but unrecognized at
  17 * runtime). We support -P to affect processes other than us.
  18
  19USE_ULIMIT(NEWTOY(ulimit, ">1P#<1SHavutsrRqpnmlifedc[-SH][!apvutsrRqnmlifedc]", TOYFLAG_USR|TOYFLAG_BIN))
  20
  21config ULIMIT
  22  bool "ulimit"
  23  default y
  24  depends on TOYBOX_PRLIMIT
  25  help
  26    usage: ulimit [-P PID] [-SHRacdefilmnpqrstuv] [LIMIT]
  27
  28    Print or set resource limits for process number PID. If no LIMIT specified
  29    (or read-only -ap selected) display current value (sizes in bytes).
  30    Default is ulimit -P $PPID -Sf" (show soft filesize of your shell).
  31    
  32    -S  Set/show soft limit          -H  Set/show hard (maximum) limit
  33    -a  Show all limits              -c  Core file size
  34    -d  Process data segment         -e  Max scheduling priority
  35    -f  Output file size             -i  Pending signal count
  36    -l  Locked memory                -m  Resident Set Size
  37    -n  Number of open files         -p  Pipe buffer
  38    -q  Posix message queue          -r  Max Real-time priority
  39    -R  Realtime latency (usec)      -s  Stack size
  40    -t  Total CPU time (in seconds)  -u  Maximum processes (under this UID)
  41    -v  Virtual memory size          -P  PID to affect (default $PPID)
  42*/
  43
  44#define FOR_ulimit
  45#include "toys.h"
  46
  47GLOBALS(
  48  long pid;
  49)
  50
  51// This is a linux kernel syscall added in 2.6.36 (git c022a0acad53) which
  52// glibc only exports a wrapper prototype for if you #define _FSF_HURD_RULZE.
  53int prlimit(pid_t pid, int resource, const struct rlimit *new_limit,
  54  struct rlimit *old_limit);
  55
  56// I'd like to sort the RLIMIT values 0-15, but mips, alpha and sparc
  57// override the asm-generic values for 5-9. Also, the kernel implementation
  58// of RLIMIT_LOCKS (-x) was removed from Linux in 2003.
  59void ulimit_main(void)
  60{
  61  struct rlimit rr;
  62  int i;
  63  // Order is cdefilmnqRrstuv
  64  char map[] = {RLIMIT_CORE, RLIMIT_DATA, RLIMIT_NICE, RLIMIT_FSIZE,
  65                RLIMIT_SIGPENDING, RLIMIT_MEMLOCK, RLIMIT_RSS, RLIMIT_NOFILE, 0,
  66                RLIMIT_MSGQUEUE, RLIMIT_RTTIME, RLIMIT_RTPRIO, RLIMIT_STACK,
  67                RLIMIT_CPU, RLIMIT_NPROC, RLIMIT_AS};
  68
  69  if (!(toys.optflags&(FLAG_H-1))) toys.optflags |= FLAG_f;
  70  if ((toys.optflags&(FLAG_a|FLAG_p)) && toys.optc) error_exit("can't set -ap");
  71
  72  // Fetch data
  73  if (!(toys.optflags&FLAG_P)) TT.pid = getppid();
  74
  75  for (i=0; i<sizeof(map); i++) {
  76    char *flags="cdefilmnpqRrstuv";
  77
  78    int get = toys.optflags&(FLAG_a|(1<<i));
  79
  80    if (get && prlimit(TT.pid, map[i], 0, &rr)) perror_exit("-%c", flags[i]);
  81    if (!toys.optc) {
  82      if (toys.optflags&FLAG_a) printf("-%c: ", flags[i]);
  83      if (get) {
  84        if ((1<<i)&FLAG_p) {
  85          if (toys.optflags&FLAG_H)
  86            xreadfile("/proc/sys/fs/pipe-max-size", toybuf, sizeof(toybuf));
  87          else {
  88            int pp[2];
  89
  90            xpipe(pp);
  91            sprintf(toybuf, "%d\n", fcntl(*pp, F_GETPIPE_SZ));
  92          }
  93          printf("%s", toybuf);
  94        } else {
  95          rlim_t rl = (toys.optflags&FLAG_H) ? rr.rlim_max : rr.rlim_cur;
  96
  97          if (rl == RLIM_INFINITY) printf("unlimited\n");
  98          else printf("%ld\n", (long)rl);
  99        }
 100      }
 101    }
 102    if (toys.optflags&(1<<i)) break;
 103  }
 104
 105  if (toys.optflags&(FLAG_a|FLAG_p)) return;
 106
 107  if (toys.optc) {
 108    rlim_t val;
 109
 110    if (tolower(**toys.optargs) == 'u') val = RLIM_INFINITY;
 111    else val = atolx_range(*toys.optargs, 0, LONG_MAX);
 112
 113    if (toys.optflags&FLAG_H) rr.rlim_max = val;
 114    else rr.rlim_cur = val;
 115    if (prlimit(TT.pid, map[i], &rr, 0)) perror_exit(0);
 116  }
 117}
 118