1 /******************************************************************************
2 * setrlimit.c: execute program in with restricted resources
3 * Written by hondza. Public domain.
4 *****************************************************************************/
16 #include <sys/resource.h>
23 static int relaxed
= 0;
28 fputs("Usage: setrlimit [options] -- /path/to/executable [arguments]\n", stderr
);
29 fputs("\n(lower case sets soft limit, UPPER case hard limit)\n\n", stderr
);
30 fputs("Options:\n", stderr
);
31 fputs("-n <no>, -N <no>\tNOFILE\t\tmax file descriptors+1\n", stderr
);
32 fputs("-p <no>, -P <no>\tNPROC\t\tmax number of processes\n", stderr
);
33 fputs("-u <no>, -U <no>\tNPROC\t\tmax number of processes\n", stderr
);
34 fputs("-c <no>, -C <no>\tCORE\t\tmax core file size (bytes)\n", stderr
);
35 fputs("-f <no>, -F <no>\tFSIZE\t\tmax size of files written\n", stderr
);
36 fputs("-i <no>, -I <no>\tSIGPENDING\tmax pending signals\n", stderr
);
37 fputs("-s <no>, -S <no>\tSTACK\t\tmax stack size (bytes)\n", stderr
);
38 fputs("-t <no>, -T <no>\tCPU\t\tmax cpu usage (seconds)\n", stderr
);
39 fputs("-v <no>, -V <no>\tAS\t\tmax address space (bytes)\n\n", stderr
);
40 fputs("-h\tthis help\n", stderr
);
41 fputs("-r\trelaxed ({get,set}rlimit() mail fail)\n", stderr
);
42 fputs("-b\tset both soft and HARD limit\n", stderr
);
43 fputs("-x\tclose all file descriptors before execv()\n\n", stderr
);
47 void do_set(const int resource
, const char *arg
, int opt
)
53 opt
= both
? (SET_SOFT
| SET_HARD
) : (opt
);
55 /* If I'm setting both, I don't need to know current ones */
58 err
= getrlimit(resource
, &lim
);
63 perror("getrlimit failed");
69 lim
.rlim_cur
= lim
.rlim_max
= RLIM_INFINITY
;
74 l
= strtoul(arg
, NULL
, 10);
76 if(opt
& SET_SOFT
) lim
.rlim_cur
= l
;
77 if(opt
& SET_HARD
) lim
.rlim_max
= l
;
79 err
= setrlimit(resource
, &lim
);
80 if(-1 == err
&& !relaxed
)
82 perror("setrlimit failed");
89 int main(int argc
, char ** argv
)
99 while( (c
= getopt(argc
, argv
, "n:N:p:P:c:C:f:F:i:I:s:S:t:T:u:U:v:V:rbhx")) != -1 )
121 do_set(RLIMIT_NOFILE
, optarg
, SET_SOFT
);
124 do_set(RLIMIT_NOFILE
, optarg
, SET_HARD
);
128 do_set(RLIMIT_NPROC
, optarg
, SET_SOFT
);
131 do_set(RLIMIT_NPROC
, optarg
, SET_HARD
);
135 do_set(RLIMIT_CORE
, optarg
, SET_SOFT
);
138 do_set(RLIMIT_CORE
, optarg
, SET_HARD
);
142 do_set(RLIMIT_FSIZE
, optarg
, SET_SOFT
);
145 do_set(RLIMIT_FSIZE
, optarg
, SET_HARD
);
149 do_set(RLIMIT_SIGPENDING
, optarg
, SET_SOFT
);
152 do_set(RLIMIT_SIGPENDING
, optarg
, SET_HARD
);
156 do_set(RLIMIT_STACK
, optarg
, SET_SOFT
);
159 do_set(RLIMIT_STACK
, optarg
, SET_HARD
);
163 do_set(RLIMIT_CPU
, optarg
, SET_SOFT
);
166 do_set(RLIMIT_CPU
, optarg
, SET_HARD
);
170 do_set(RLIMIT_AS
, optarg
, SET_SOFT
);
173 do_set(RLIMIT_AS
, optarg
, SET_HARD
);
177 default: help(); exit(1);
178 } /* switch argument */
179 } /* while getopt() */
182 for ((i
= getdtablesize()); i
>= 0 ; i
--) close(i
);
186 c
= execv(argv
[optind
], argv
+optind
);
187 if(!doclose
&& -1 == c
)
189 perror("execv() failed");
195 fputs("FATAL: nothing to exec!\n", stderr
);