1 /* $Id: setrlimit.c,v 1.4 2008-04-10 08:13:29 mike Exp $ */
4 * A simple wrapper program for the setrlimit(2) system call, which
5 * can be used to run a subprocess under a different regime -- much
6 * like "nice", "time", etc. This is needed for IRSpy, since when it
7 * runs against many servers simultaneously, it runs out of file
8 * descriptors -- a condition, by the way, which Perl sometimes
9 * reports very misleadingly (e.g. "Can't locate Scalar/Util.pm in
10 * @INC" when the open() failure was due to EMFILE rather than
13 * Since the file-descriptor limit can be raised (from the default of
14 * 1024 in Ubuntu) only by root, this program often needs to run as
15 * root -- hence the option for resetting the UID after performing the
25 #include <sys/resource.h>
26 #include <sys/types.h>
36 { 'a', "AS", RLIMIT_AS, 1024*1024 },
37 { 'n', "NOFILE", RLIMIT_NOFILE, 1 },
41 int main(int argc, char **argv) {
47 for (i = 0; i < 26; i++) {
51 while ((c = getopt(argc, argv, "vu:a:n:")) != -1) {
61 values[c-'a'] = strtol(optarg, (char**) 0, 0);
65 fprintf(stderr, "Usage: %s [options] <command>\n\
67 -u <user> Run subcommand as <user>\n\
68 -a <Mbytes> Set maximum size of address-space (memory)\n\
69 -n <number> Set maximum open files to <number>\n",
78 for (c = 'a'; c <= 'z'; c++) {
79 long n = values[c - 'a'];
81 int i, ntypes = sizeof types/sizeof *types;
82 struct rlimit old, new;
84 for (i = 0; i < ntypes; i++) {
90 fprintf(stderr, "%s: no such type '%c'\n", argv[0], c);
94 n *= types[i].multiplier;
95 getrlimit(types[i].value, &old);
101 if (new.rlim_cur != old.rlim_cur)
102 fprintf(stderr, "%s: changing soft %s from %ld to %ld\n",
103 argv[0], types[i].name,
104 (long) old.rlim_cur, (long) new.rlim_cur);
105 if (new.rlim_max != old.rlim_max)
106 fprintf(stderr, "%s: changing hard %s from %ld to %ld\n",
107 argv[0], types[i].name,
108 (long) old.rlim_max, (long) new.rlim_max);
110 if (setrlimit(types[i].value, &new) < 0) {
111 fprintf(stderr, "%s: setrlimit(%s=%ld): %s\n",
112 argv[0], types[i].name, n, strerror(errno));
120 if ((pwd = getpwnam(user)) == 0) {
121 fprintf(stderr, "%s: user '%s' not known\n", argv[0], user);
125 if (setuid(pwd->pw_uid) < 0) {
126 fprintf(stderr, "%s: setuid('%s'=%ld): %s\n",
127 argv[0], user, (long) pwd->pw_uid, strerror(errno));
133 fprintf(stderr, "%s: user='%s', optind=%d, new argc=%d, argv[0]='%s'\n",
134 argv[0], user, optind, argc-optind, argv[optind]);
136 execvp(argv[optind], argv+optind);
137 fprintf(stderr, "%s: execvp('%s'): %s\n",
138 argv[0], argv[optind], strerror(errno));