New
[irspy-moved-to-github.git] / bin / setrlimit.c
1 /* $Id: setrlimit.c,v 1.1 2007-02-27 14:52:55 mike Exp $ */
2
3 /*
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
11  * ENOENT).
12  *
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
16  * the limit-change.
17  */
18
19 #include <getopt.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <pwd.h>
29
30 int main(int argc, char **argv) {
31     int n = 0;
32     char *user = 0;
33     int c;
34
35     while ((c = getopt(argc, argv, "n:u:")) != -1) {
36         switch (c) {
37         case 'n': n = atoi(optarg); break;
38         case 'u': user = optarg; break;
39         default:
40         USAGE:
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",
44                     argv[0]);
45             exit(1);
46         }
47     }
48
49     if (optind == argc)
50         goto USAGE;
51
52     if (n != 0) {
53         struct rlimit old, new;
54         getrlimit(RLIMIT_NOFILE, &old);
55         new = old;
56         new.rlim_cur = n;
57         if (n > new.rlim_max)
58             new.rlim_max = n;
59 #if 0
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);
66 #endif
67         if (setrlimit(RLIMIT_NOFILE, &new) < 0) {
68             fprintf(stderr, "%s: setrlimit(n=%d): %s\n",
69                     argv[0], n, strerror(errno));
70             exit(2);
71         }
72     }
73
74     if (user != 0) {
75         struct passwd *pwd;
76         if ((pwd = getpwnam(user)) == 0) {
77             fprintf(stderr, "%s: user '%s' not known\n", argv[0], user);
78             exit(3);
79         }
80
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));
84             exit(4);
85         }
86     }
87
88 #if 0
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]);
91 #endif
92     execvp(argv[optind], argv+optind);
93     fprintf(stderr, "%s: execvp('%s'): %s\n",
94             argv[0], argv[optind], strerror(errno));
95     exit(5);
96 }