3 # Resource limiting wrapper for command execution
5 # Why is this in shell script? Because bash has a setrlimit() wrapper
6 # and is available on most Linux systems. If Perl was distributed with
7 # BSD::Resource included, we would happily use that instead, but it isn't.
18 if [ "$MW_CPU_LIMIT" -gt 0 ]; then
19 ulimit -t "$MW_CPU_LIMIT"
21 if [ "$MW_MEM_LIMIT" -gt 0 ]; then
22 if [ -n "$MW_CGROUP" ]; then
24 if ! mkdir
-m 0700 "$MW_CGROUP"/$$
; then
25 echo "limit.sh: failed to create the cgroup." 1>&2
28 echo $$
> "$MW_CGROUP"/$$
/tasks
29 if [ -n "$MW_CGROUP_NOTIFY" ]; then
30 echo "1" > "$MW_CGROUP"/$$
/notify_on_release
33 echo $
(($MW_MEM_LIMIT*1024)) > "$MW_CGROUP"/$$
/memory.limit_in_bytes
35 echo $
(($MW_MEM_LIMIT*1024)) > "$MW_CGROUP"/$$
/memory.memsw.limit_in_bytes
37 ulimit -v "$MW_MEM_LIMIT"
40 if [ "$MW_FILE_SIZE_LIMIT" -gt 0 ]; then
41 ulimit -f "$MW_FILE_SIZE_LIMIT"
43 if [ "$MW_WALL_CLOCK_LIMIT" -gt 0 -a -x "/usr/bin/timeout" ]; then
44 /usr
/bin
/timeout
$MW_WALL_CLOCK_LIMIT /bin
/bash
-c "$1"
46 if [ "$STATUS" == 124 ]; then
47 echo "limit.sh: timed out." 1>&2
56 # First we have to move the current task into a "garbage" group, otherwise
57 # the cgroup will not be empty, and attempting to remove it will fail with
58 # "Device or resource busy"
59 if [ -w "$MW_CGROUP"/tasks
]; then
62 GARBAGE
="$MW_CGROUP"/garbage-
"$USER"
63 if [ ! -e "$GARBAGE" ]; then
64 mkdir
-m 0700 "$GARBAGE"
67 echo $BASHPID > "$GARBAGE"/tasks
69 # Suppress errors in case the cgroup has disappeared due to a release script
70 rmdir "$MW_CGROUP"/$$
2>/dev
/null
74 # There are lots of ways to count lines in a file in shell script, but this
75 # is one of the few that doesn't create another process, which would
76 # increase the returned number of tasks.
77 readarray
< "$MW_CGROUP"/$$
/tasks
78 NUM_TASKS
=${#MAPFILE[*]}
81 if [ -n "$MW_CGROUP" ]; then
84 if [ $NUM_TASKS -gt 1 ]; then
85 # Spawn a monitor process which will continue to poll for completion
86 # of all processes in the cgroup after termination of the parent shell
88 while [ $NUM_TASKS -gt 1 ]; do
93 ) >&/dev
/null
< /dev
/null
&