int ulimitcmd(int argc, char **argv) { int c; rlim_t val = 0; enum limtype how = SOFT | HARD; const struct limits *l; int set, all = 0; int optc, what; struct rlimit limit; what = 'f'; while ((optc = nextopt("HSa" #ifdef RLIMIT_CPU "t" #endif #ifdef RLIMIT_FSIZE "f" #endif #ifdef RLIMIT_DATA "d" #endif #ifdef RLIMIT_STACK "s" #endif #ifdef RLIMIT_CORE "c" #endif #ifdef RLIMIT_RSS "m" #endif #ifdef RLIMIT_MEMLOCK "l" #endif #ifdef RLIMIT_NPROC "p" #endif #ifdef RLIMIT_NOFILE "n" #endif #ifdef RLIMIT_AS "v" #endif #ifdef RLIMIT_LOCKS "w" #endif )) != '\0') switch (optc) { case 'H': how = HARD; break; case 'S': how = SOFT; break; case 'a': all = 1; break; default: what = optc; } for (l = limits; l->option != what; l++) ; set = *argptr ? 1 : 0; if (set) { char *p = *argptr; if (all || argptr[1]) sh_error("too many arguments"); if (strcmp(p, "unlimited") == 0) val = RLIM_INFINITY; else { val = (rlim_t) 0; while ((c = *p++) >= '0' && c <= '9') { val = (val * 10) + (long)(c - '0'); if (val < (rlim_t) 0) break; } if (c) sh_error("bad number"); val *= l->factor; } } if (all) { for (l = limits; l->name; l++) { getrlimit(l->cmd, &limit); out1fmt("%-20s ", l->name); printlim(how, &limit, l); } return 0; } getrlimit(l->cmd, &limit); if (set) { if (how & HARD) limit.rlim_max = val; if (how & SOFT) limit.rlim_cur = val; if (setrlimit(l->cmd, &limit) < 0) sh_error("error setting limit (%s)", strerror(errno)); } else { printlim(how, &limit, l); } return 0; }
int FAST_FUNC shell_builtin_ulimit(char **argv) { unsigned opts; unsigned argc; /* We can't use getopt32: need to handle commands like * ulimit 123 -c2 -l 456 */ /* In case getopt was already called: * reset the libc getopt() function, which keeps internal state. */ #ifdef __GLIBC__ optind = 0; #else /* BSD style */ optind = 1; /* optreset = 1; */ #endif /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */ argc = 1; while (argv[argc]) argc++; opts = 0; while (1) { struct rlimit limit; const struct limits *l; int opt_char = getopt(argc, argv, ulimit_opt_string); if (opt_char == -1) break; if (opt_char == 'H') { opts |= OPT_hard; continue; } if (opt_char == 'S') { opts |= OPT_soft; continue; } if (opt_char == 'a') { for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { getrlimit(l->cmd, &limit); printf("-%c: %-30s ", l->option, l->name); printlim(opts, &limit, l); } continue; } if (opt_char == 1) opt_char = 'f'; for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) { if (opt_char == l->option) { char *val_str; getrlimit(l->cmd, &limit); val_str = optarg; if (!val_str && argv[optind] && argv[optind][0] != '-') val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */ if (val_str) { rlim_t val; if (strcmp(val_str, "unlimited") == 0) val = RLIM_INFINITY; else { if (sizeof(val) == sizeof(int)) val = bb_strtou(val_str, NULL, 10); else if (sizeof(val) == sizeof(long)) val = bb_strtoul(val_str, NULL, 10); else val = bb_strtoull(val_str, NULL, 10); if (errno) { bb_error_msg("invalid number '%s'", val_str); return EXIT_FAILURE; } val <<= l->factor_shift; } //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val); /* from man bash: "If neither -H nor -S * is specified, both the soft and hard * limits are set. */ if (!opts) opts = OPT_hard + OPT_soft; if (opts & OPT_hard) limit.rlim_max = val; if (opts & OPT_soft) limit.rlim_cur = val; //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max); if (setrlimit(l->cmd, &limit) < 0) { bb_perror_msg("error setting limit"); return EXIT_FAILURE; } } else { printlim(opts, &limit, l); } break; } } /* for (every possible opt) */ if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) { /* bad option. getopt already complained. */ break; } } /* while (there are options) */ return 0; }