3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or (at your option)
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 Set the global per context limit of a resource (memory, file handle).
21 This utility can do it either for the current context or a selected
24 It uses the same options as ulimit, when possible
42 #define VERSION_COPYRIGHT_DISCLAIMER
44 inline static void UNUSED
45 writeStr(int fd, char const *cmd)
47 (void)write(fd, cmd, strlen(cmd));
50 #define WRITE_MSG(FD,X) (void)(write(FD,X,sizeof(X)-1))
51 #define WRITE_STR(FD,X) writeStr(FD,X)
54 { #X, required_argument, 0, 2048|X }
56 static struct option const
58 { "help", no_argument, 0, 'h' },
59 { "version", no_argument, 0, 'v' },
60 { "all", no_argument, 0, 'a' },
61 NUMLIM( 0), NUMLIM( 1), NUMLIM( 2), NUMLIM( 3),
62 NUMLIM( 4), NUMLIM( 5), NUMLIM( 6), NUMLIM( 7),
63 NUMLIM( 8), NUMLIM( 9), NUMLIM(10), NUMLIM(11),
64 NUMLIM(12), NUMLIM(13), NUMLIM(14), NUMLIM(15),
65 NUMLIM(16), NUMLIM(17), NUMLIM(18), NUMLIM(19),
66 NUMLIM(20), NUMLIM(21), NUMLIM(22), NUMLIM(23),
67 NUMLIM(24), NUMLIM(25), NUMLIM(26), NUMLIM(27),
68 NUMLIM(28), NUMLIM(29), NUMLIM(30), NUMLIM(31),
73 showHelp(int fd, char const *cmd, int res)
75 WRITE_MSG(fd, "Usage: ");
78 " [-c|--ctx <ctx>] [-a|--all] [-MSH --<nr> <value>]*\n"
79 "Please report bugs to " PACKAGE_BUGREPORT "\n");
87 "vlimit " VERSION " -- limits context-resources\n"
88 "This program is part of " PACKAGE_STRING "\n\n"
89 "Copyright (C) 2003 Enrico Scholz\n"
90 VERSION_COPYRIGHT_DISCLAIMER);
95 appendLimit(char *ptr, bool do_it, vc_limit_t lim)
100 if (lim==VC_LIM_INFINITY) {
105 memcpy(ptr, "0x", 2); ptr += 2;
107 ptr += utilvserver_fmt_uint64_base(ptr, lim, 16);
112 memcpy(ptr, "N/A", 3);
122 struct vc_rlimit_mask mask;
125 if (vc_get_rlimit_mask(-2, &mask)==-1) {
126 perror("vc_get_rlimit_mask()");
130 for (i=0; i<32; ++i) {
131 uint32_t bitmask = (1<<i);
132 struct vc_rlimit limit;
133 char buf[100], *ptr=buf;
135 if (vc_get_rlimit(ctx, i, &limit)==-1) {
136 perror("vc_get_rlimit()");
140 memset(buf, ' ', sizeof buf);
141 ptr += utilvserver_fmt_uint(ptr, i);
144 ptr = appendLimit(buf+10, mask.min &bitmask, limit.min);
145 ptr = appendLimit(buf+30, mask.soft&bitmask, limit.soft);
146 ptr = appendLimit(buf+50, mask.hard&bitmask, limit.hard);
149 write(1, buf, ptr-buf);
154 setLimits(int ctx, struct vc_rlimit const limits[], uint32_t mask)
157 for (i=0; i<32; ++i) {
158 if ((mask & (1<<i))==0) continue;
159 if (vc_set_rlimit(ctx, i, limits+i)) {
160 perror("vc_set_rlimit()");
165 int main (int argc, char *argv[])
167 // overall used limits
168 uint32_t lim_mask = 0;
170 struct vc_rlimit limits[32];
171 bool show_all = false;
172 ctx_t ctx = VC_SAMECTX;
176 for (i=0; i<32; ++i) {
177 limits[i].min = VC_LIM_KEEP;
178 limits[i].soft = VC_LIM_KEEP;
179 limits[i].hard = VC_LIM_KEEP;
184 int c = getopt_long(argc, argv, "MSHhvac:", CMDLINE_OPTIONS, 0);
187 if (2048<=c && c<2048+32) {
191 if (strcmp(optarg, "inf")==0) val = VC_LIM_INFINITY;
192 else val = atoll(optarg);
194 if (set_mask & 1) limits[id].min = val;
195 if (set_mask & 2) limits[id].soft = val;
196 if (set_mask & 4) limits[id].soft = val;
202 case 'h' : showHelp(1, argv[0], 0);
203 case 'v' : showVersion();
204 case 'c' : ctx = atoi(optarg); break;
205 case 'a' : show_all = true; break;
210 case 'M' : set_mask |= 1; break;
211 case 'S' : set_mask |= 2; break;
212 case 'H' : set_mask |= 4; break;
213 default : assert(false);
218 WRITE_MSG(2, "Try '");
219 WRITE_STR(2, argv[0]);
220 WRITE_MSG(2, " --help\" for more information.\n");
226 setLimits(ctx, limits, lim_mask);
227 if (show_all) showAll(ctx);