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; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <sys/resource.h>
30 #define DECLARE_LIMIT(RES,FNAME) { #FNAME, RLIMIT_##RES }
32 int wrapper_exit_code = 255;
38 DECLARE_LIMIT(CPU, cpu),
39 DECLARE_LIMIT(DATA, data),
40 DECLARE_LIMIT(FSIZE, fsize),
41 DECLARE_LIMIT(LOCKS, locks),
42 DECLARE_LIMIT(MEMLOCK, memlock),
43 DECLARE_LIMIT(NOFILE, nofile),
44 DECLARE_LIMIT(NPROC, nproc),
45 DECLARE_LIMIT(RSS, rss),
46 DECLARE_LIMIT(STACK, stack),
50 readValue(int fd, char const *filename)
53 size_t len = Eread(fd, buf, sizeof(buf)-1);
58 if (strncmp(buf, "inf", 3)==0) return RLIM_INFINITY;
59 res = strtol(buf, &errptr, 0);
61 if (errptr==buf || (*errptr!='\0' && *errptr!='\n')) {
62 WRITE_MSG(2, "Invalid limit in '");
63 WRITE_STR(2, filename);
72 readSingleLimit(struct rlimit *lim, char const *fname_base)
74 size_t fname_len = strlen(fname_base);
75 char fname[fname_len + sizeof(".hard")];
77 bool is_modified = false;
79 strcpy(fname, fname_base);
81 fd = open(fname, O_RDONLY);
83 rlim_t tmp = readValue(fd, fname_base);
91 strcpy(fname+fname_len, ".hard");
92 fd = open(fname, O_RDONLY);
94 lim->rlim_max = readValue(fd, fname_base);
100 strcpy(fname+fname_len, ".soft");
101 fd = open(fname, O_RDONLY);
103 lim->rlim_cur = readValue(fd, fname_base);
110 lim->rlim_max!=RLIM_INFINITY &&
111 (lim->rlim_cur==RLIM_INFINITY ||
112 lim->rlim_cur>lim->rlim_max))
113 lim->rlim_cur = lim->rlim_max;
119 showHelp(int fd, char const *cmd, int res)
121 WRITE_MSG(fd, "Usage: ");
124 "<ulimit-cfgdir> <cmd> <argv>*\n\n"
125 "Please report bugs to " PACKAGE_BUGREPORT "\n");
133 "exec-ulimit " VERSION " -- executes programs with resource limits\n"
134 "This program is part of " PACKAGE_STRING "\n\n"
135 "Copyright (C) 2003 Enrico Scholz\n"
136 VERSION_COPYRIGHT_DISCLAIMER);
140 int main(int argc, char *argv[])
143 int cur_fd = Eopen(".", O_RDONLY, 0);
146 if (strcmp(argv[1], "--help")==0) showHelp(1,argv[0],0);
147 if (strcmp(argv[1], "--version")==0) showVersion();
151 WRITE_MSG(2, "Bad parameter count; use '--help' for more information.\n");
155 if (chdir(argv[1])!=-1) {
156 for (i=0; i<sizeof(LIMITS)/sizeof(LIMITS[0]); ++i) {
159 Egetrlimit(LIMITS[i].code, &limit);
160 if (readSingleLimit(&limit, LIMITS[i].fname))
161 Esetrlimit(LIMITS[i].code, &limit);
167 Eexecv(argv[2], argv+2);