2 * A simple wrapper program for the setrlimit(2) system call, which
3 * can be used to run a subprocess under a different regime -- much
4 * like "nice", "time", etc. This is needed for IRSpy, since when it
5 * runs against many servers simultaneously, it runs out of file
6 * descriptors -- a condition, by the way, which Perl sometimes
7 * reports very misleadingly (e.g. "Can't locate Scalar/Util.pm in
8 * @INC" when the open() failure was due to EMFILE rather than
11 * Since the file-descriptor limit can be raised (from the default of
12 * 1024 in Ubuntu) only by root, this program often needs to run as
13 * root -- hence the option for resetting the UID after performing the
23 #include <sys/resource.h>
24 #include <sys/types.h>
34 { 'a', "AS", RLIMIT_AS, 1024*1024 },
35 { 'n', "NOFILE", RLIMIT_NOFILE, 1 },
39 int main(int argc, char **argv) {
45 for (i = 0; i < 26; i++) {
49 while ((c = getopt(argc, argv, "vu:a:n:")) != -1) {
59 values[c-'a'] = strtol(optarg, (char**) 0, 0);
63 fprintf(stderr, "Usage: %s [options] <command>\n\
65 -u <user> Run subcommand as <user>\n\
66 -a <Mbytes> Set maximum size of address-space (memory)\n\
67 -n <number> Set maximum open files to <number>\n",
76 for (c = 'a'; c <= 'z'; c++) {
77 long n = values[c - 'a'];
79 int i, ntypes = sizeof types/sizeof *types;
80 struct rlimit old, new;
82 for (i = 0; i < ntypes; i++) {
88 fprintf(stderr, "%s: no such type '%c'\n", argv[0], c);
92 n *= types[i].multiplier;
93 getrlimit(types[i].value, &old);
99 if (new.rlim_cur != old.rlim_cur)
100 fprintf(stderr, "%s: changing soft %s from %ld to %ld\n",
101 argv[0], types[i].name,
102 (long) old.rlim_cur, (long) new.rlim_cur);
103 if (new.rlim_max != old.rlim_max)
104 fprintf(stderr, "%s: changing hard %s from %ld to %ld\n",
105 argv[0], types[i].name,
106 (long) old.rlim_max, (long) new.rlim_max);
108 if (setrlimit(types[i].value, &new) < 0) {
109 fprintf(stderr, "%s: setrlimit(%s=%ld): %s\n",
110 argv[0], types[i].name, n, strerror(errno));
118 if ((pwd = getpwnam(user)) == 0) {
119 fprintf(stderr, "%s: user '%s' not known\n", argv[0], user);
123 if (setuid(pwd->pw_uid) < 0) {
124 fprintf(stderr, "%s: setuid('%s'=%ld): %s\n",
125 argv[0], user, (long) pwd->pw_uid, strerror(errno));
131 fprintf(stderr, "%s: user='%s', optind=%d, new argc=%d, argv[0]='%s'\n",
132 argv[0], user, optind, argc-optind, argv[optind]);
134 execvp(argv[optind], argv+optind);
135 fprintf(stderr, "%s: execvp('%s'): %s\n",
136 argv[0], argv[optind], strerror(errno));