Search This Blog

Monday, April 11, 2011

What does my bash ulimit function use to set/report the limitation of resources

Everyone knows the output from the 'ulimit' bash, builtin function. But what functionality actually this function uses to mange all of this.

$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Let's find out if the 'strace' program come in handy for this job.

# let's create auxilary file we will use later
$ cat mylimits.sh
ulimit -a

$ strace bash mylimits.sh 1>/dev/null 2>/tmp/strace.out
$ tail -20 /tmp/strace.out 
getrlimit(RLIMIT_NOFILE, {rlim_cur=1024, rlim_max=1024}) = 0
write(1, "open files                      "..., 42) = 42
write(1, "pipe size            (512 bytes,"..., 39) = 39
getrlimit(RLIMIT_MSGQUEUE, {rlim_cur=800*1024, rlim_max=800*1024}) = 0
write(1, "POSIX message queues     (bytes,"..., 44) = 44
getrlimit(RLIMIT_RTPRIO, {rlim_cur=0, rlim_max=0}) = 0
write(1, "real-time priority              "..., 39) = 39
getrlimit(RLIMIT_STACK, {rlim_cur=8192*1024, rlim_max=RLIM_INFINITY}) = 0
write(1, "stack size              (kbytes,"..., 42) = 42
getrlimit(RLIMIT_CPU, {rlim_cur=RLIM_INFINITY, rlim_max=RLIM_INFINITY}) = 0
write(1, "cpu time               (seconds,"..., 47) = 47
getrlimit(RLIMIT_NPROC, {rlim_cur=RLIM_INFINITY, rlim_max=RLIM_INFINITY}) = 0
write(1, "max user processes              "..., 47) = 47
getrlimit(RLIMIT_AS, {rlim_cur=RLIM_INFINITY, rlim_max=RLIM_INFINITY}) = 0
write(1, "virtual memory          (kbytes,"..., 47) = 47
getrlimit(RLIMIT_LOCKS, {rlim_cur=RLIM_INFINITY, rlim_max=RLIM_INFINITY}) = 0
write(1, "file locks                      "..., 47) = 47
rt_sigprocmask(SIG_BLOCK, NULL, [], 8)  = 0
read(255, "", 10)                       = 0
exit_group(0)                           = ?

So as we can see the limitations are managed by the 'getrlimit' function and enforced on the kernel level.

For more info about this take a look at man 2 setrlimit

For these who are not afraid of glibc library a quick look at they wrappers may help as well: glibc "Resource Usage And Limitation"

An obsolete kernel function man 3 ulimit

No comments:

Post a Comment