fix builtin ulimit arg handling

Fixes #3570
This commit is contained in:
Kurtis Rader 2016-11-21 21:09:53 -08:00
parent 93e6f57dfc
commit 9ac78e06b4

View File

@ -2,6 +2,7 @@
#include "config.h" // IWYU pragma: keep #include "config.h" // IWYU pragma: keep
#include <errno.h> #include <errno.h>
#include <stddef.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <wchar.h> #include <wchar.h>
@ -9,6 +10,7 @@
#include "common.h" #include "common.h"
#include "fallback.h" // IWYU pragma: keep #include "fallback.h" // IWYU pragma: keep
#include "io.h" #include "io.h"
#include "proc.h"
#include "util.h" #include "util.h"
#include "wgetopt.h" #include "wgetopt.h"
#include "wutil.h" // IWYU pragma: keep #include "wutil.h" // IWYU pragma: keep
@ -120,14 +122,11 @@ static const wchar_t *get_desc(int what) {
/// Set the new value of the specified resource limit. This function does _not_ multiply the limit /// Set the new value of the specified resource limit. This function does _not_ multiply the limit
// value by the multiplier constant used by the commandline ulimit. // value by the multiplier constant used by the commandline ulimit.
static int set(int resource, int hard, int soft, rlim_t value, io_streams_t &streams) { static int set_limit(int resource, int hard, int soft, rlim_t value, io_streams_t &streams) {
struct rlimit ls; struct rlimit ls;
getrlimit(resource, &ls); getrlimit(resource, &ls);
if (hard) ls.rlim_max = value;
if (hard) {
ls.rlim_max = value;
}
if (soft) { if (soft) {
ls.rlim_cur = value; ls.rlim_cur = value;
@ -139,185 +138,170 @@ static int set(int resource, int hard, int soft, rlim_t value, io_streams_t &str
} }
if (setrlimit(resource, &ls)) { if (setrlimit(resource, &ls)) {
if (errno == EPERM) if (errno == EPERM) {
streams.err.append_format( streams.err.append_format(
L"ulimit: Permission denied when changing resource of type '%ls'\n", L"ulimit: Permission denied when changing resource of type '%ls'\n",
get_desc(resource)); get_desc(resource));
else } else {
builtin_wperror(L"ulimit", streams); builtin_wperror(L"ulimit", streams);
return 1; }
return STATUS_BUILTIN_ERROR;
} }
return 0; return STATUS_BUILTIN_OK;
} }
/// The ulimit builtin, used for setting resource limits. /// The ulimit builtin, used for setting resource limits.
int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) { int builtin_ulimit(parser_t &parser, io_streams_t &streams, wchar_t **argv) {
wgetopter_t w; wchar_t *cmd = argv[0];
int hard = 0;
int soft = 0;
int what = RLIMIT_FSIZE;
int report_all = 0;
int argc = builtin_count_args(argv); int argc = builtin_count_args(argv);
bool report_all = false;
bool hard = false;
bool soft = false;
int what = RLIMIT_FSIZE;
w.woptind = 0; const wchar_t *short_options = L":HSacdflmnstuvh";
const struct woption long_options[] = {{L"all", no_argument, NULL, 'a'},
while (1) { {L"hard", no_argument, NULL, 'H'},
static const struct woption long_options[] = { {L"soft", no_argument, NULL, 'S'},
{L"all", no_argument, 0, 'a'}, {L"core-size", no_argument, NULL, 'c'},
{L"hard", no_argument, 0, 'H'}, {L"data-size", no_argument, NULL, 'd'},
{L"soft", no_argument, 0, 'S'}, {L"file-size", no_argument, NULL, 'f'},
{L"core-size", no_argument, 0, 'c'}, {L"lock-size", no_argument, NULL, 'l'},
{L"data-size", no_argument, 0, 'd'}, {L"resident-set-size", no_argument, NULL, 'm'},
{L"file-size", no_argument, 0, 'f'}, {L"file-descriptor-count", no_argument, NULL, 'n'},
{L"lock-size", no_argument, 0, 'l'}, {L"stack-size", no_argument, NULL, 's'},
{L"resident-set-size", no_argument, 0, 'm'}, {L"cpu-time", no_argument, NULL, 't'},
{L"file-descriptor-count", no_argument, 0, 'n'}, {L"process-count", no_argument, NULL, 'u'},
{L"stack-size", no_argument, 0, 's'}, {L"virtual-memory-size", no_argument, NULL, 'v'},
{L"cpu-time", no_argument, 0, 't'}, {L"help", no_argument, NULL, 'h'},
{L"process-count", no_argument, 0, 'u'}, {NULL, 0, NULL, 0}};
{L"virtual-memory-size", no_argument, 0, 'v'},
{L"help", no_argument, 0, 'h'},
{0, 0, 0, 0}};
int opt_index = 0;
int opt = w.wgetopt_long(argc, argv, L"aHScdflmnstuvh", long_options, &opt_index);
if (opt == -1) break;
int opt;
wgetopter_t w;
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (opt) { switch (opt) {
case 0: { case 'a': {
if (long_options[opt_index].flag != 0) break; report_all = true;
streams.err.append_format(BUILTIN_ERR_UNKNOWN, argv[0],
long_options[opt_index].name);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
case L'a': {
report_all = 1;
break; break;
} }
case L'H': { case 'H': {
hard = 1; hard = true;
break; break;
} }
case L'S': { case 'S': {
soft = 1; soft = true;
break; break;
} }
case L'c': { case 'c': {
what = RLIMIT_CORE; what = RLIMIT_CORE;
break; break;
} }
case L'd': { case 'd': {
what = RLIMIT_DATA; what = RLIMIT_DATA;
break; break;
} }
case L'f': { case 'f': {
what = RLIMIT_FSIZE; what = RLIMIT_FSIZE;
break; break;
} }
#ifdef RLIMIT_MEMLOCK #ifdef RLIMIT_MEMLOCK
case L'l': { case 'l': {
what = RLIMIT_MEMLOCK; what = RLIMIT_MEMLOCK;
break; break;
} }
#endif #endif
#ifdef RLIMIT_RSS #ifdef RLIMIT_RSS
case L'm': { case 'm': {
what = RLIMIT_RSS; what = RLIMIT_RSS;
break; break;
} }
#endif #endif
case L'n': { case 'n': {
what = RLIMIT_NOFILE; what = RLIMIT_NOFILE;
break; break;
} }
case L's': { case 's': {
what = RLIMIT_STACK; what = RLIMIT_STACK;
break; break;
} }
case L't': { case 't': {
what = RLIMIT_CPU; what = RLIMIT_CPU;
break; break;
} }
#ifdef RLIMIT_NPROC #ifdef RLIMIT_NPROC
case L'u': { case 'u': {
what = RLIMIT_NPROC; what = RLIMIT_NPROC;
break; break;
} }
#endif #endif
#ifdef RLIMIT_AS #ifdef RLIMIT_AS
case L'v': { case 'v': {
what = RLIMIT_AS; what = RLIMIT_AS;
break; break;
} }
#endif #endif
case L'h': { case 'h': {
builtin_print_help(parser, streams, argv[0], streams.out); builtin_print_help(parser, streams, cmd, streams.out);
return 0; return 0;
} }
case L'?': { case ':': {
builtin_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]); streams.err.append_format(BUILTIN_ERR_MISSING, cmd, argv[w.woptind - 1]);
return 1; return STATUS_BUILTIN_ERROR;
}
case '?': {
builtin_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_BUILTIN_ERROR;
} }
default: { default: {
DIE("unexpected opt"); DIE("unexpected retval from wgetopt_long");
break; break;
} }
} }
} }
if (report_all) { if (report_all) {
if (argc - w.woptind == 0) { print_all(hard, streams);
print_all(hard, streams); return STATUS_BUILTIN_OK;
} else {
streams.err.append(argv[0]);
streams.err.append(L": Too many arguments\n");
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
return 0;
} }
int arg_count = argc - w.woptind; int arg_count = argc - w.woptind;
if (arg_count == 0) { if (arg_count == 0) {
// Show current limit value. // Show current limit value.
print(what, hard, streams); print(what, hard, streams);
} else if (arg_count == 1) { return STATUS_BUILTIN_OK;
// Change current limit value. } else if (arg_count != 1) {
rlim_t new_limit; streams.err.append_format(BUILTIN_ERR_TOO_MANY_ARGUMENTS, cmd);
wchar_t *end; builtin_print_help(parser, streams, cmd, streams.err);
return STATUS_BUILTIN_ERROR;
// Set both hard and soft limits if nothing else was specified.
if (!(hard + soft)) {
hard = soft = 1;
}
if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
new_limit = RLIM_INFINITY;
} else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
new_limit = get(what, 1);
} else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
new_limit = get(what, soft);
} else {
errno = 0;
new_limit = wcstol(argv[w.woptind], &end, 10);
if (errno || *end) {
streams.err.append_format(L"%ls: Invalid limit '%ls'\n", argv[0], argv[w.woptind]);
builtin_print_help(parser, streams, argv[0], streams.err);
return 1;
}
new_limit *= get_multiplier(what);
}
return set(what, hard, soft, new_limit, streams);
} }
streams.err.append(argv[0]); // Change current limit value.
streams.err.append(L": Too many arguments\n"); if (!hard && !soft) {
builtin_print_help(parser, streams, argv[0], streams.err); // Set both hard and soft limits if neither was specified.
return 1; hard = soft = true;
}
rlim_t new_limit;
if (*argv[w.woptind] == L'\0') {
streams.err.append_format(_(L"%ls: New limit cannot be an empty string\n"), cmd);
builtin_print_help(parser, streams, cmd, streams.err);
return STATUS_BUILTIN_ERROR;
} else if (wcscasecmp(argv[w.woptind], L"unlimited") == 0) {
new_limit = RLIM_INFINITY;
} else if (wcscasecmp(argv[w.woptind], L"hard") == 0) {
new_limit = get(what, 1);
} else if (wcscasecmp(argv[w.woptind], L"soft") == 0) {
new_limit = get(what, soft);
} else {
wchar_t *end;
new_limit = wcstol(argv[w.woptind], &end, 10);
if (*end) {
streams.err.append_format(_(L"%ls: Invalid limit '%ls'\n"), cmd, argv[w.woptind]);
builtin_print_help(parser, streams, cmd, streams.err);
return STATUS_BUILTIN_ERROR;
}
new_limit *= get_multiplier(what);
}
return set_limit(what, hard, soft, new_limit, streams);
} }