1 /* $Id: setrlimit.c,v 1.1 2007-02-27 14:52:55 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) {
35 while ((c = getopt(argc, argv, "n:u:")) != -1) {
37 case 'n': n = atoi(optarg); break;
38 case 'u': user = optarg; break;
41 fprintf(stderr, "Usage: %s [options] <command>\n\
42 -n <number> Set maximum open files to <number>\n\
43 -u <user> Run subcommand as <user>\n",
53 struct rlimit old, new;
54 getrlimit(RLIMIT_NOFILE, &old);
60 if (new.rlim_cur != old.rlim_cur)
61 printf("%s: changing soft NOFILE from %ld to %ld\n",
62 argv[0], (long) old.rlim_cur, (long) new.rlim_cur);
63 if (new.rlim_max != old.rlim_max)
64 printf("%s: changing soft NOFILE from %ld to %ld\n",
65 argv[0], (long) old.rlim_max, (long) new.rlim_max);
67 if (setrlimit(RLIMIT_NOFILE, &new) < 0) {
68 fprintf(stderr, "%s: setrlimit(n=%d): %s\n",
69 argv[0], n, strerror(errno));
76 if ((pwd = getpwnam(user)) == 0) {
77 fprintf(stderr, "%s: user '%s' not known\n", argv[0], user);
81 if (setuid(pwd->pw_uid) < 0) {
82 fprintf(stderr, "%s: setuid('%s'=%ld): %s\n",
83 argv[0], user, (long) pwd->pw_uid, strerror(errno));
89 printf("%s: n=%d, user='%s', optind=%d, new argc=%d, new argv[0]='%s'\n",
90 argv[0], n, user, optind, argc-optind, argv[optind]);
92 execvp(argv[optind], argv+optind);
93 fprintf(stderr, "%s: execvp('%s'): %s\n",
94 argv[0], argv[optind], strerror(errno));