Denis Vlasenko | 9bff26c | 2006-10-20 19:40:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2001-2006, Gerrit Pape |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | |
| 8 | 1. Redistributions of source code must retain the above copyright notice, |
| 9 | this list of conditions and the following disclaimer. |
| 10 | 2. Redistributions in binary form must reproduce the above copyright |
| 11 | notice, this list of conditions and the following disclaimer in the |
| 12 | documentation and/or other materials provided with the distribution. |
| 13 | 3. The name of the author may not be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 28 | /* Busyboxed by Denys Vlasenko <vda.linux@googlemail.com> */ |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 29 | /* Dependencies on runit_lib.c removed */ |
Denis Vlasenko | 9bff26c | 2006-10-20 19:40:44 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 31 | #include "libbb.h" |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 33 | /* |
| 34 | Five applets here: chpst, envdir, envuidgid, setuidgid, softlimit. |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 35 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 36 | Only softlimit and chpst are taking options: |
| 37 | |
| 38 | # common |
| 39 | -o N Limit number of open files per process |
| 40 | -p N Limit number of processes per uid |
| 41 | -m BYTES Same as -d BYTES -s BYTES -l BYTES [-a BYTES] |
| 42 | -d BYTES Limit data segment |
| 43 | -f BYTES Limit output file sizes |
| 44 | -c BYTES Limit core file size |
| 45 | # softlimit |
| 46 | -a BYTES Limit total size of all segments |
| 47 | -s BYTES Limit stack segment |
| 48 | -l BYTES Limit locked memory size |
| 49 | -r BYTES Limit resident set size |
| 50 | -t N Limit CPU time |
| 51 | # chpst |
| 52 | -u USER[:GRP] Set uid and gid |
| 53 | -U USER[:GRP] Set $UID and $GID in environment |
| 54 | -e DIR Set environment variables as specified by files in DIR |
| 55 | -/ DIR Chroot to DIR |
| 56 | -n NICE Add NICE to nice value |
| 57 | -v Verbose |
| 58 | -P Create new process group |
| 59 | -0 -1 -2 Close fd 0,1,2 |
| 60 | |
| 61 | Even though we accept all these options for both softlimit and chpst, |
| 62 | they are not to be advertised on their help texts. |
| 63 | We have enough problems with feature creep in other people's |
| 64 | software, don't want to add our own. |
| 65 | |
| 66 | envdir, envuidgid, setuidgid take no options, but they reuse code which |
| 67 | handles -e, -U and -u. |
| 68 | */ |
| 69 | |
| 70 | enum { |
| 71 | OPT_a = (1 << 0) * ENABLE_SOFTLIMIT, |
| 72 | OPT_c = (1 << 1) * (ENABLE_SOFTLIMIT || ENABLE_CHPST), |
| 73 | OPT_d = (1 << 2) * (ENABLE_SOFTLIMIT || ENABLE_CHPST), |
| 74 | OPT_f = (1 << 3) * (ENABLE_SOFTLIMIT || ENABLE_CHPST), |
| 75 | OPT_l = (1 << 4) * ENABLE_SOFTLIMIT, |
| 76 | OPT_m = (1 << 5) * (ENABLE_SOFTLIMIT || ENABLE_CHPST), |
| 77 | OPT_o = (1 << 6) * (ENABLE_SOFTLIMIT || ENABLE_CHPST), |
| 78 | OPT_p = (1 << 7) * (ENABLE_SOFTLIMIT || ENABLE_CHPST), |
| 79 | OPT_r = (1 << 8) * ENABLE_SOFTLIMIT, |
| 80 | OPT_s = (1 << 9) * ENABLE_SOFTLIMIT, |
| 81 | OPT_t = (1 << 10) * ENABLE_SOFTLIMIT, |
| 82 | OPT_u = (1 << 11) * (ENABLE_CHPST || ENABLE_SETUIDGID), |
| 83 | OPT_U = (1 << 12) * (ENABLE_CHPST || ENABLE_ENVUIDGID), |
| 84 | OPT_e = (1 << 13) * (ENABLE_CHPST || ENABLE_ENVDIR), |
| 85 | OPT_root = (1 << 14) * ENABLE_CHPST, |
| 86 | OPT_n = (1 << 15) * ENABLE_CHPST, |
| 87 | OPT_v = (1 << 16) * ENABLE_CHPST, |
| 88 | OPT_P = (1 << 17) * ENABLE_CHPST, |
| 89 | OPT_0 = (1 << 18) * ENABLE_CHPST, |
| 90 | OPT_1 = (1 << 19) * ENABLE_CHPST, |
| 91 | OPT_2 = (1 << 20) * ENABLE_CHPST, |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 92 | }; |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 93 | |
Bernhard Reutner-Fischer | ca25449 | 2009-10-26 23:27:07 +0100 | [diff] [blame] | 94 | /* TODO: use recursive_action? */ |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 95 | static NOINLINE void edir(const char *directory_name) |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 96 | { |
| 97 | int wdir; |
| 98 | DIR *dir; |
| 99 | struct dirent *d; |
| 100 | int fd; |
| 101 | |
| 102 | wdir = xopen(".", O_RDONLY | O_NDELAY); |
| 103 | xchdir(directory_name); |
Bernhard Reutner-Fischer | ca25449 | 2009-10-26 23:27:07 +0100 | [diff] [blame] | 104 | dir = xopendir("."); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 105 | for (;;) { |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 106 | char buf[256]; |
Denis Vlasenko | 9895dfd | 2008-04-01 16:13:14 +0000 | [diff] [blame] | 107 | char *tail; |
| 108 | int size; |
| 109 | |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 110 | errno = 0; |
| 111 | d = readdir(dir); |
| 112 | if (!d) { |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 113 | if (errno) |
| 114 | bb_perror_msg_and_die("readdir %s", |
| 115 | directory_name); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 116 | break; |
| 117 | } |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 118 | if (d->d_name[0] == '.') |
| 119 | continue; |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 120 | fd = open(d->d_name, O_RDONLY | O_NDELAY); |
| 121 | if (fd < 0) { |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 122 | if ((errno == EISDIR) && directory_name) { |
| 123 | if (option_mask32 & OPT_v) |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 124 | bb_perror_msg("warning: %s/%s is a directory", |
| 125 | directory_name, d->d_name); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 126 | continue; |
| 127 | } else |
Denis Vlasenko | 83ea643 | 2006-11-16 02:27:24 +0000 | [diff] [blame] | 128 | bb_perror_msg_and_die("open %s/%s", |
| 129 | directory_name, d->d_name); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 130 | } |
Denis Vlasenko | 9895dfd | 2008-04-01 16:13:14 +0000 | [diff] [blame] | 131 | size = full_read(fd, buf, sizeof(buf)-1); |
| 132 | close(fd); |
| 133 | if (size < 0) |
| 134 | bb_perror_msg_and_die("read %s/%s", |
| 135 | directory_name, d->d_name); |
| 136 | if (size == 0) { |
| 137 | unsetenv(d->d_name); |
| 138 | continue; |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 139 | } |
Denis Vlasenko | 9895dfd | 2008-04-01 16:13:14 +0000 | [diff] [blame] | 140 | buf[size] = '\n'; |
| 141 | tail = strchr(buf, '\n'); |
| 142 | /* skip trailing whitespace */ |
| 143 | while (1) { |
| 144 | *tail = '\0'; |
| 145 | tail--; |
| 146 | if (tail < buf || !isspace(*tail)) |
| 147 | break; |
| 148 | } |
| 149 | xsetenv(d->d_name, buf); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 150 | } |
| 151 | closedir(dir); |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 152 | if (fchdir(wdir) == -1) |
| 153 | bb_perror_msg_and_die("fchdir"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 154 | close(wdir); |
| 155 | } |
| 156 | |
| 157 | static void limit(int what, long l) |
| 158 | { |
| 159 | struct rlimit r; |
| 160 | |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 161 | /* Never fails under Linux (except if you pass it bad arguments) */ |
| 162 | getrlimit(what, &r); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 163 | if ((l < 0) || (l > r.rlim_max)) |
| 164 | r.rlim_cur = r.rlim_max; |
| 165 | else |
| 166 | r.rlim_cur = l; |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 167 | if (setrlimit(what, &r) == -1) |
| 168 | bb_perror_msg_and_die("setrlimit"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 171 | int chpst_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 172 | int chpst_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 173 | { |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 174 | struct bb_uidgid_t ugid; |
Denis Vlasenko | ec73d30 | 2008-08-05 17:43:29 +0000 | [diff] [blame] | 175 | char *set_user = set_user; /* for compiler */ |
| 176 | char *env_user = env_user; |
| 177 | char *env_dir = env_dir; |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 178 | char *root; |
| 179 | char *nicestr; |
| 180 | unsigned limita; |
| 181 | unsigned limitc; |
| 182 | unsigned limitd; |
| 183 | unsigned limitf; |
| 184 | unsigned limitl; |
| 185 | unsigned limitm; |
| 186 | unsigned limito; |
| 187 | unsigned limitp; |
| 188 | unsigned limitr; |
| 189 | unsigned limits; |
| 190 | unsigned limitt; |
| 191 | unsigned opt; |
| 192 | |
| 193 | if ((ENABLE_CHPST && applet_name[0] == 'c') |
| 194 | || (ENABLE_SOFTLIMIT && applet_name[1] == 'o') |
| 195 | ) { |
| 196 | // FIXME: can we live with int-sized limits? |
| 197 | // can we live with 40000 days? |
| 198 | // if yes -> getopt converts strings to numbers for us |
| 199 | opt_complementary = "-1:a+:c+:d+:f+:l+:m+:o+:p+:r+:s+:t+"; |
| 200 | opt = getopt32(argv, "+a:c:d:f:l:m:o:p:r:s:t:u:U:e:" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 201 | IF_CHPST("/:n:vP012"), |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 202 | &limita, &limitc, &limitd, &limitf, &limitl, |
| 203 | &limitm, &limito, &limitp, &limitr, &limits, &limitt, |
| 204 | &set_user, &env_user, &env_dir |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 205 | IF_CHPST(, &root, &nicestr)); |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 206 | argv += optind; |
| 207 | if (opt & OPT_m) { // -m means -asld |
| 208 | limita = limits = limitl = limitd = limitm; |
| 209 | opt |= (OPT_s | OPT_l | OPT_a | OPT_d); |
| 210 | } |
| 211 | } else { |
| 212 | option_mask32 = opt = 0; |
| 213 | argv++; |
Denis Vlasenko | b9c262b | 2008-08-20 22:19:27 +0000 | [diff] [blame] | 214 | if (!*argv) |
| 215 | bb_show_usage(); |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // envdir? |
| 219 | if (ENABLE_ENVDIR && applet_name[3] == 'd') { |
| 220 | env_dir = *argv++; |
| 221 | opt |= OPT_e; |
| 222 | } |
| 223 | |
| 224 | // setuidgid? |
Denis Vlasenko | b9c262b | 2008-08-20 22:19:27 +0000 | [diff] [blame] | 225 | if (ENABLE_SETUIDGID && applet_name[1] == 'e') { |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 226 | set_user = *argv++; |
| 227 | opt |= OPT_u; |
| 228 | } |
| 229 | |
| 230 | // envuidgid? |
Denis Vlasenko | 69ed309 | 2008-08-15 21:03:17 +0000 | [diff] [blame] | 231 | if (ENABLE_ENVUIDGID && applet_name[0] == 'e' && applet_name[3] == 'u') { |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 232 | env_user = *argv++; |
| 233 | opt |= OPT_U; |
| 234 | } |
| 235 | |
| 236 | // we must have PROG [ARGS] |
| 237 | if (!*argv) |
| 238 | bb_show_usage(); |
| 239 | |
| 240 | // set limits |
| 241 | if (opt & OPT_d) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 242 | #ifdef RLIMIT_DATA |
| 243 | limit(RLIMIT_DATA, limitd); |
| 244 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 245 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 246 | bb_error_msg("system does not support RLIMIT_%s", |
| 247 | "DATA"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 248 | #endif |
| 249 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 250 | if (opt & OPT_s) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 251 | #ifdef RLIMIT_STACK |
| 252 | limit(RLIMIT_STACK, limits); |
| 253 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 254 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 255 | bb_error_msg("system does not support RLIMIT_%s", |
| 256 | "STACK"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 257 | #endif |
| 258 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 259 | if (opt & OPT_l) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 260 | #ifdef RLIMIT_MEMLOCK |
| 261 | limit(RLIMIT_MEMLOCK, limitl); |
| 262 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 263 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 264 | bb_error_msg("system does not support RLIMIT_%s", |
| 265 | "MEMLOCK"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 266 | #endif |
| 267 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 268 | if (opt & OPT_a) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 269 | #ifdef RLIMIT_VMEM |
| 270 | limit(RLIMIT_VMEM, limita); |
| 271 | #else |
| 272 | #ifdef RLIMIT_AS |
| 273 | limit(RLIMIT_AS, limita); |
| 274 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 275 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 276 | bb_error_msg("system does not support RLIMIT_%s", |
| 277 | "VMEM"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 278 | #endif |
| 279 | #endif |
| 280 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 281 | if (opt & OPT_o) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 282 | #ifdef RLIMIT_NOFILE |
| 283 | limit(RLIMIT_NOFILE, limito); |
| 284 | #else |
| 285 | #ifdef RLIMIT_OFILE |
| 286 | limit(RLIMIT_OFILE, limito); |
| 287 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 288 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 289 | bb_error_msg("system does not support RLIMIT_%s", |
| 290 | "NOFILE"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 291 | #endif |
| 292 | #endif |
| 293 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 294 | if (opt & OPT_p) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 295 | #ifdef RLIMIT_NPROC |
| 296 | limit(RLIMIT_NPROC, limitp); |
| 297 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 298 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 299 | bb_error_msg("system does not support RLIMIT_%s", |
| 300 | "NPROC"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 301 | #endif |
| 302 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 303 | if (opt & OPT_f) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 304 | #ifdef RLIMIT_FSIZE |
| 305 | limit(RLIMIT_FSIZE, limitf); |
| 306 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 307 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 308 | bb_error_msg("system does not support RLIMIT_%s", |
| 309 | "FSIZE"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 310 | #endif |
| 311 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 312 | if (opt & OPT_c) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 313 | #ifdef RLIMIT_CORE |
| 314 | limit(RLIMIT_CORE, limitc); |
| 315 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 316 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 317 | bb_error_msg("system does not support RLIMIT_%s", |
| 318 | "CORE"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 319 | #endif |
| 320 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 321 | if (opt & OPT_r) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 322 | #ifdef RLIMIT_RSS |
| 323 | limit(RLIMIT_RSS, limitr); |
| 324 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 325 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 326 | bb_error_msg("system does not support RLIMIT_%s", |
| 327 | "RSS"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 328 | #endif |
| 329 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 330 | if (opt & OPT_t) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 331 | #ifdef RLIMIT_CPU |
| 332 | limit(RLIMIT_CPU, limitt); |
| 333 | #else |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 334 | if (opt & OPT_v) |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 335 | bb_error_msg("system does not support RLIMIT_%s", |
| 336 | "CPU"); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 337 | #endif |
| 338 | } |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 339 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 340 | if (opt & OPT_P) |
| 341 | setsid(); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 343 | if (opt & OPT_e) |
| 344 | edir(env_dir); |
Denis Vlasenko | 23e3e25 | 2007-10-05 21:23:49 +0000 | [diff] [blame] | 345 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 346 | // FIXME: chrooted jail must have /etc/passwd if we move this after chroot! |
| 347 | // OTOH chroot fails for non-roots! |
| 348 | // SOLUTION: cache uid/gid before chroot, apply uid/gid after |
| 349 | if (opt & OPT_U) { |
| 350 | xget_uidgid(&ugid, env_user); |
| 351 | xsetenv("GID", utoa(ugid.gid)); |
| 352 | xsetenv("UID", utoa(ugid.uid)); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 353 | } |
Denis Vlasenko | f7996f3 | 2007-01-11 17:20:00 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 355 | if (opt & OPT_u) { |
| 356 | xget_uidgid(&ugid, set_user); |
| 357 | } |
| 358 | |
| 359 | if (opt & OPT_root) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 360 | xchdir(root); |
Denis Vlasenko | 394eebe | 2008-02-25 20:30:24 +0000 | [diff] [blame] | 361 | xchroot("."); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 362 | } |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 363 | |
| 364 | if (opt & OPT_u) { |
| 365 | if (setgroups(1, &ugid.gid) == -1) |
| 366 | bb_perror_msg_and_die("setgroups"); |
| 367 | xsetgid(ugid.gid); |
| 368 | xsetuid(ugid.uid); |
| 369 | } |
| 370 | |
| 371 | if (opt & OPT_n) { |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 372 | errno = 0; |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 373 | if (nice(xatoi(nicestr)) == -1) |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 374 | bb_perror_msg_and_die("nice"); |
| 375 | } |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 376 | |
Denis Vlasenko | b357149 | 2008-07-30 21:23:26 +0000 | [diff] [blame] | 377 | if (opt & OPT_0) |
| 378 | close(STDIN_FILENO); |
| 379 | if (opt & OPT_1) |
| 380 | close(STDOUT_FILENO); |
| 381 | if (opt & OPT_2) |
| 382 | close(STDERR_FILENO); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 383 | |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 384 | BB_EXECVP_or_die(argv); |
Denis Vlasenko | f0a97fb | 2006-10-03 17:52:24 +0000 | [diff] [blame] | 385 | } |