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
43 #include <sys/resource.h>
49 #define ENSC_WRAPPERS_PREFIX "vlimit: "
50 #define ENSC_WRAPPERS_UNISTD 1
51 #define ENSC_WRAPPERS_VSERVER 1
54 #define CMD_DIR 0x8000
55 #define CMD_MISSINGOK 0x8001
57 int wrapper_exit_code = 255;
60 { #X, required_argument, 0, 2048|X }
61 #define OPT_RESLIM(RES,V) \
62 { #RES, required_argument, 0, 2048|RLIMIT_##V }
64 static struct option const
66 { "help", no_argument, 0, 'h' },
67 { "version", no_argument, 0, 'v' },
68 { "all", no_argument, 0, 'a' },
69 { "dir", required_argument, 0, CMD_DIR },
70 { "missingok", no_argument, 0, CMD_MISSINGOK },
71 NUMLIM( 0), NUMLIM( 1), NUMLIM( 2), NUMLIM( 3),
72 NUMLIM( 4), NUMLIM( 5), NUMLIM( 6), NUMLIM( 7),
73 NUMLIM( 8), NUMLIM( 9), NUMLIM(10), NUMLIM(11),
74 NUMLIM(12), NUMLIM(13), NUMLIM(14), NUMLIM(15),
75 NUMLIM(16), NUMLIM(17), NUMLIM(18), NUMLIM(19),
76 NUMLIM(20), NUMLIM(21), NUMLIM(22), NUMLIM(23),
77 NUMLIM(24), NUMLIM(25), NUMLIM(26), NUMLIM(27),
78 NUMLIM(28), NUMLIM(29), NUMLIM(30), NUMLIM(31),
80 OPT_RESLIM(fsize, FSIZE),
81 OPT_RESLIM(data, DATA),
82 OPT_RESLIM(stack, STACK),
83 OPT_RESLIM(core, CORE),
85 OPT_RESLIM(nproc, NPROC),
86 OPT_RESLIM(nofile, NOFILE),
87 OPT_RESLIM(memlock, MEMLOCK),
89 OPT_RESLIM(locks, LOCKS),
93 #define REV_RESLIM(X) [RLIMIT_##X] = #X
94 static char const * const LIMIT_STR[] = {
95 REV_RESLIM(CPU), REV_RESLIM(FSIZE), REV_RESLIM(DATA), REV_RESLIM(STACK),
96 REV_RESLIM(CORE), REV_RESLIM(RSS), REV_RESLIM(NPROC), REV_RESLIM(NOFILE),
97 REV_RESLIM(MEMLOCK), REV_RESLIM(AS), REV_RESLIM(LOCKS)
101 showHelp(int fd, char const *cmd, int res)
103 VSERVER_DECLARE_CMD(cmd);
105 WRITE_MSG(fd, "Usage: ");
108 " [-c <xid>] [-nd] [-a|--all] [[-MSH] --(<resource>|<nr>) <value>]*\n"
109 " [--dir <pathname> [--missingok]] [--] [<program> <args>*]\n\n"
111 " -c <xid> ... operate on context <xid>\n"
112 " -a|--all ... show all available limits\n"
113 " -n ... do not resolve limit-names\n"
114 " -d ... show limits in decimal\n"
115 " -M ... set Minimum limit\n"
116 " -S ... set Soft limit\n"
117 " -H ... set Hard limit (assumed by default, when neither\n"
118 " M nor S was requested)\n"
119 " --dir <pathname>\n"
120 " ... read limits from <pathname>/; allowed filenames are\n"
121 " <resource> and <resource>.{min,soft,hard}. When a limit\n"
122 " was set by the CLI already, the corresponding file\n"
124 " --missingok ... do not fail when <pathname> does not exist\n"
125 " --<resource>|<nr> <value>\n"
126 " ... set specified (MSH) limit for <resource> to <value>\n\n"
127 "Valid values for resource are cpu, fsize, data, stack, core, rss, nproc,\n"
128 "nofile, memlock, as and locks.\n\n"
129 "Please report bugs to " PACKAGE_BUGREPORT "\n");
137 "vlimit " VERSION " -- limits context-resources\n"
138 "This program is part of " PACKAGE_STRING "\n\n"
139 "Copyright (C) 2003 Enrico Scholz\n"
140 VERSION_COPYRIGHT_DISCLAIMER);
145 fmtHex(char *ptr, vc_limit_t lim)
147 memcpy(ptr, "0x", 2);
148 return utilvserver_fmt_xuint64(ptr+2, lim) + 2;
151 static bool do_resolve = true;
152 static size_t (*fmt_func)(char *, vc_limit_t) = fmtHex;
155 appendLimit(char *ptr, bool do_it, vc_limit_t lim)
160 if (lim==VC_LIM_INFINITY) {
165 ptr += (*fmt_func)(ptr, lim);
170 memcpy(ptr, "N/A", 3);
180 struct vc_rlimit_mask mask;
183 if (vc_get_rlimit_mask(ctx, &mask)==-1) {
184 perror("vc_get_rlimit_mask()");
185 exit(wrapper_exit_code);
188 for (i=0; i<32; ++i) {
189 uint32_t bitmask = (1<<i);
190 struct vc_rlimit limit;
191 char buf[128], *ptr=buf;
193 if (((mask.min|mask.soft|mask.hard) & bitmask)==0) continue;
194 if (vc_get_rlimit(ctx, i, &limit)==-1) {
195 perror("vc_get_rlimit()");
199 memset(buf, ' ', sizeof buf);
200 if (do_resolve && i<DIM_OF(LIMIT_STR)) {
201 size_t l = strlen(LIMIT_STR[i]);
202 memcpy(ptr, LIMIT_STR[i], l);
206 ptr += utilvserver_fmt_uint(ptr, i);
210 ptr = appendLimit(buf+10, mask.min &bitmask, limit.min);
211 ptr = appendLimit(buf+30, mask.soft&bitmask, limit.soft);
212 ptr = appendLimit(buf+50, mask.hard&bitmask, limit.hard);
215 write(1, buf, ptr-buf);
220 setLimits(int ctx, struct vc_rlimit const limits[], uint32_t mask)
223 for (i=0; i<32; ++i) {
224 if ((mask & (1<<i))==0) continue;
225 if (vc_set_rlimit(ctx, i, limits+i)) {
226 perror("vc_set_rlimit()");
232 readValue(int fd, char const *filename)
235 size_t len = Eread(fd, buf, sizeof(buf)-1);
240 if (!vc_parseLimit(buf, &res)) {
241 WRITE_MSG(2, "Invalid limit in '");
242 WRITE_STR(2, filename);
244 exit(wrapper_exit_code);
251 readFile(char const *file, char *base, char const *suffix,
256 strcpy(base, suffix);
257 fd = open(file, O_RDONLY);
259 *limit = readValue(fd, file);
267 readFromDir(struct vc_rlimit limits[32], uint_least32_t *mask,
268 char const *pathname, bool missing_ok)
272 size_t l_pathname = strlen(pathname);
273 char buf[l_pathname + sizeof("/memlock.hard") + 32];
275 if (stat(pathname, &st)==-1) {
276 if (errno==ENOENT && missing_ok) return;
277 PERROR_Q("vlimit: fstat", pathname);
278 exit(wrapper_exit_code);
281 memcpy(buf, pathname, l_pathname);
282 if (l_pathname>0 && pathname[l_pathname-1]!='/')
283 buf[l_pathname++] = '/';
285 for (i=0; i<DIM_OF(LIMIT_STR); ++i) {
287 char * ptr = buf+l_pathname;
289 // ignore unimplemented limits
290 if (LIMIT_STR[i]==0) continue;
292 // ignore limits set on cli already
293 if (*mask & (1<<i)) continue;
295 l_res = strlen(LIMIT_STR[i]);
296 memcpy(ptr, LIMIT_STR[i], l_res+1);
298 *ptr = tolower(*ptr);
302 if (readFile(buf, ptr, "", &limits[i].min)) {
303 limits[i].soft = limits[i].hard = limits[i].min;
307 if (readFile(buf, ptr, ".min", &limits[i].min))
310 if (readFile(buf, ptr, ".soft", &limits[i].soft))
313 if (readFile(buf, ptr, ".hard", &limits[i].hard))
318 int main (int argc, char *argv[])
320 // overall used limits
321 uint32_t lim_mask = 0;
323 struct vc_rlimit limits[32];
324 bool show_all = false;
325 xid_t ctx = VC_NOCTX;
326 char const * dir = 0;
327 bool missing_ok = false;
331 for (i=0; i<32; ++i) {
332 limits[i].min = VC_LIM_KEEP;
333 limits[i].soft = VC_LIM_KEEP;
334 limits[i].hard = VC_LIM_KEEP;
339 int c = getopt_long(argc, argv, "+MSHndhvac:", CMDLINE_OPTIONS, 0);
342 if (2048<=c && c<2048+32) {
346 if (!vc_parseLimit(optarg, &val)) {
347 WRITE_MSG(2, "Can not parse limit '");
348 WRITE_STR(2, optarg);
350 exit(wrapper_exit_code);
353 if (set_mask==0) set_mask=4;
355 if (set_mask & 1) limits[id].min = val;
356 if (set_mask & 2) limits[id].soft = val;
357 if (set_mask & 4) limits[id].hard = val;
363 case 'h' : showHelp(1, argv[0], 0);
364 case 'v' : showVersion();
365 case 'c' : ctx = atoi(optarg); break;
366 case 'a' : show_all = true; break;
367 case 'n' : do_resolve = false; break;
368 case 'd' : fmt_func = utilvserver_fmt_uint64; break;
369 case CMD_DIR : dir = optarg; break;
370 case CMD_MISSINGOK: missing_ok = true; break;
375 case 'M' : set_mask |= 1; break;
376 case 'S' : set_mask |= 2; break;
377 case 'H' : set_mask |= 4; break;
378 default : assert(false);
383 WRITE_MSG(2, "Try '");
384 WRITE_STR(2, argv[0]);
385 WRITE_MSG(2, " --help\" for more information.\n");
386 exit(wrapper_exit_code) ;
392 ctx = Evc_get_task_xid(0);
395 readFromDir(limits, &lim_mask, dir, missing_ok);
397 setLimits(ctx, limits, lim_mask);
398 if (show_all) showAll(ctx);
401 EexecvpD(argv[optind], argv+optind);