1 /* $Id: setrlimit.c,v 1.2 2007-02-27 14:54:52 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 only be raised (from the
14 * default of 1024 in Ubuntu) by root, this program often needs to run
15 * as root -- hence the option for resetting the UID after performing
25 #include <sys/resource.h>
26 #include <sys/types.h>
30 int main(int argc, char **argv) {
36 while ((c = getopt(argc, argv, "vn:u:")) != -1) {
38 case 'v': verbose++; break;
39 case 'n': n = atoi(optarg); break;
40 case 'u': user = optarg; break;
43 fprintf(stderr, "Usage: %s [options] <command>\n\
45 -n <number> Set maximum open files to <number>\n\
46 -u <user> Run subcommand as <user>\n",
56 struct rlimit old, new;
57 getrlimit(RLIMIT_NOFILE, &old);
63 if (new.rlim_cur != old.rlim_cur)
64 printf("%s: changing soft NOFILE from %ld to %ld\n",
65 argv[0], (long) old.rlim_cur, (long) new.rlim_cur);
66 if (new.rlim_max != old.rlim_max)
67 printf("%s: changing soft NOFILE from %ld to %ld\n",
68 argv[0], (long) old.rlim_max, (long) new.rlim_max);
70 if (setrlimit(RLIMIT_NOFILE, &new) < 0) {
71 fprintf(stderr, "%s: setrlimit(n=%d): %s\n",
72 argv[0], n, strerror(errno));
79 if ((pwd = getpwnam(user)) == 0) {
80 fprintf(stderr, "%s: user '%s' not known\n", argv[0], user);
84 if (setuid(pwd->pw_uid) < 0) {
85 fprintf(stderr, "%s: setuid('%s'=%ld): %s\n",
86 argv[0], user, (long) pwd->pw_uid, strerror(errno));
92 printf("%s: n=%d, user='%s', optind=%d, new argc=%d, argv[0]='%s'\n",
93 argv[0], n, user, optind, argc-optind, argv[optind]);
95 execvp(argv[optind], argv+optind);
96 fprintf(stderr, "%s: execvp('%s'): %s\n",
97 argv[0], argv[optind], strerror(errno));