Skip to content

Commit

Permalink
Switched --limit => --rlimit for more natural naming
Browse files Browse the repository at this point in the history
  • Loading branch information
colindrewes committed Jul 29, 2022
1 parent 7685eee commit b7476a5
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 165 deletions.
14 changes: 7 additions & 7 deletions enter.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static inline size_t append_argv(char **argv, size_t argc, char *arg)

/* Applies the limit specified by `resource'. If value is NULL, then copy the
hard limit value to the soft limit and call `setrlimit'. */
static void apply_limit(int resource, struct rlimit const *value)
static void apply_rlimit(int resource, struct rlimit const *value)
{
struct rlimit new_limit;
if (value == NULL) {
Expand Down Expand Up @@ -659,16 +659,16 @@ int enter(struct entry_settings *opts)
}
}

for (size_t resource = 0; resource < lengthof(opts->limits); ++resource) {
for (size_t resource = 0; resource < lengthof(opts->rlimits); ++resource) {
struct rlimit const * value = NULL;
if (opts->limits[resource].present) {
value = &opts->limits[resource].rlim;
if (opts->rlimits[resource].present) {
value = &opts->rlimits[resource].rlim;
}
/* When no_copy_hard_limits is not set, we always want to call apply_limit, either
/* When no_copy_hard_rlimits is not set, we always want to call apply_rlimit, either
with the explicitly configured value (value != NULL), or by copying hard->soft
(value == NULL). */
if (value || !opts->no_copy_hard_limits) {
apply_limit(resource, value);
if (value || !opts->no_copy_hard_rlimits) {
apply_rlimit(resource, value);
}
}

Expand Down
6 changes: 3 additions & 3 deletions enter.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# include "userns.h"
# include "tty.h"

struct limit {
struct bst_rlimit {
bool present;
struct rlimit rlim;
};
Expand Down Expand Up @@ -77,7 +77,7 @@ struct entry_settings {

const char *arch;

struct limit limits[BST_NLIMIT];
struct bst_rlimit rlimits[BST_NLIMIT];

const char *setup_program;
char *const *setup_argv;
Expand All @@ -87,7 +87,7 @@ struct entry_settings {
bool tty;
struct tty_opts ttyopts;

int no_copy_hard_limits;
int no_copy_hard_rlimits;
int no_fake_devtmpfs;
int no_derandomize;
int no_proc_remount;
Expand Down
40 changes: 20 additions & 20 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum {
OPTION_GROUPS,
OPTION_WORKDIR,
OPTION_ARCH,
OPTION_LIMIT,
OPTION_RLIMIT,
OPTION_SHARE,
OPTION_UNSHARE,
OPTION_ARGV0,
Expand All @@ -61,7 +61,7 @@ enum {
OPTION_NO_LOOPBACK_SETUP,
OPTION_NO_INIT,
OPTION_NO_ENV,
OPTION_NO_COPY_HARD_LIMITS,
OPTION_NO_COPY_HARD_RLIMITS,
OPTION_TTY,
};

Expand Down Expand Up @@ -168,7 +168,7 @@ static void process_unshare(const char **out, char *nsnames)
}
}

static void handle_limit_arg(struct entry_settings *opts, char *optarg)
static void handle_rlimit_arg(struct entry_settings *opts, char *optarg)
{
struct opt {
int resource;
Expand Down Expand Up @@ -197,7 +197,7 @@ static void handle_limit_arg(struct entry_settings *opts, char *optarg)
char *arg = strtok(NULL, "");

if (arg == NULL) {
err(2, "--limit takes an argument in the form resource=value");
err(2, "--rlimit takes an argument in the form resource=value");
}

const struct opt *opt_ent = NULL;
Expand All @@ -208,7 +208,7 @@ static void handle_limit_arg(struct entry_settings *opts, char *optarg)
}

if (opt_ent == NULL) {
fprintf(stderr, "--limit: `%s` is not a valid resource name.\n", name);
fprintf(stderr, "--rlimit: `%s` is not a valid resource name.\n", name);
fprintf(stderr, "valid resources are: ");

for (const struct opt *opt = option_map; opt < option_map + lengthof(option_map); ++opt) {
Expand All @@ -218,10 +218,10 @@ static void handle_limit_arg(struct entry_settings *opts, char *optarg)
exit(2);
}

if (parse_rlimit(opt_ent->resource, &opts->limits[opt_ent->resource].rlim, arg)) {
err(1, "error in --limit %s value", opt_ent->name);
if (parse_rlimit(opt_ent->resource, &opts->rlimits[opt_ent->resource].rlim, arg)) {
err(1, "error in --rlimit %s value", opt_ent->name);
}
opts->limits[opt_ent->resource].present = true;
opts->rlimits[opt_ent->resource].present = true;
}

int usage(int error, char *argv0)
Expand Down Expand Up @@ -267,7 +267,7 @@ int main(int argc, char *argv[], char *envp[])
{ "gid", required_argument, NULL, OPTION_GID },
{ "groups", required_argument, NULL, OPTION_GROUPS },
{ "arch", required_argument, NULL, OPTION_ARCH },
{ "limit", required_argument, NULL, OPTION_LIMIT },
{ "rlimit", required_argument, NULL, OPTION_RLIMIT },
{ "share", required_argument, NULL, OPTION_SHARE },
{ "unshare", required_argument, NULL, OPTION_UNSHARE },
{ "argv0", required_argument, NULL, OPTION_ARGV0 },
Expand All @@ -288,13 +288,13 @@ int main(int argc, char *argv[], char *envp[])
{ "tty", optional_argument, NULL, OPTION_TTY },

/* Opt-out feature flags */
{ "no-copy-hard-limits", no_argument, NULL, OPTION_NO_COPY_HARD_LIMITS },
{ "no-fake-devtmpfs", no_argument, NULL, OPTION_NO_FAKE_DEVTMPFS },
{ "no-derandomize", no_argument, NULL, OPTION_NO_DERANDOMIZE },
{ "no-proc-remount", no_argument, NULL, OPTION_NO_PROC_REMOUNT },
{ "no-loopback-setup", no_argument, NULL, OPTION_NO_LOOPBACK_SETUP },
{ "no-init", no_argument, NULL, OPTION_NO_INIT },
{ "no-env", no_argument, NULL, OPTION_NO_ENV },
{ "no-copy-hard-rlimits", no_argument, NULL, OPTION_NO_COPY_HARD_RLIMITS },
{ "no-fake-devtmpfs", no_argument, NULL, OPTION_NO_FAKE_DEVTMPFS },
{ "no-derandomize", no_argument, NULL, OPTION_NO_DERANDOMIZE },
{ "no-proc-remount", no_argument, NULL, OPTION_NO_PROC_REMOUNT },
{ "no-loopback-setup", no_argument, NULL, OPTION_NO_LOOPBACK_SETUP },
{ "no-init", no_argument, NULL, OPTION_NO_INIT },
{ "no-env", no_argument, NULL, OPTION_NO_ENV },

{ 0, 0, 0, 0 }
};
Expand Down Expand Up @@ -388,8 +388,8 @@ int main(int argc, char *argv[], char *envp[])
opts.arch = optarg;
break;

case OPTION_LIMIT:
handle_limit_arg(&opts, optarg);
case OPTION_RLIMIT:
handle_rlimit_arg(&opts, optarg);
break;

case OPTION_SHARE:
Expand Down Expand Up @@ -634,8 +634,8 @@ int main(int argc, char *argv[], char *envp[])
opts.no_proc_remount = 1;
break;

case OPTION_NO_COPY_HARD_LIMITS:
opts.no_copy_hard_limits = 1;
case OPTION_NO_COPY_HARD_RLIMITS:
opts.no_copy_hard_rlimits = 1;
break;

case OPTION_INIT:
Expand Down
26 changes: 13 additions & 13 deletions man/bst.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ _VAR=value_ before the executable to run.

Supported options are described in more detail in the *NETWORKING* section.

\--limit <resource>=<value>++
\--limit <resource>=[hard]:[soft]
\--rlimit <resource>=<value>++
\--rlimit <resource>=[hard]:[soft]
Set the specified hard and soft resource limits of the specified resource
based on *<value>*.

Expand Down Expand Up @@ -262,17 +262,17 @@ _VAR=value_ before the executable to run.
soft resource limit to the current hard limit value.

Examples:++
\--limit nproc=100 hard=100, soft=100++
\--limit nproc=200:100 hard=200, soft=100++
\--limit nproc=:100 hard=(unchanged), soft=100++
\--limit nproc=100: hard=100, soft=(unchanged)++
\--limit nproc=: hard=(unchanged), soft=(hard limit)

If a value is not provided using a --limit option for a given resource,
the hard limit will be used as the soft limit, unless *--no-copy-hard-limits* has
\--rlimit nproc=100 hard=100, soft=100++
\--rlimit nproc=200:100 hard=200, soft=100++
\--rlimit nproc=:100 hard=(unchanged), soft=100++
\--rlimit nproc=100: hard=100, soft=(unchanged)++
\--rlimit nproc=: hard=(unchanged), soft=(hard limit)

If a value is not provided using a --rlimit option for a given resource,
the hard limit will be used as the soft limit, unless *--no-copy-hard-rlimits* has
been provided.

Full details for each limit value can be found in *getrlimit*(2).
Full details for each rlimit value can be found in *getrlimit*(2).

\--pidfile <path>
Write the PID of the newly spawned *<executable>* in *<path>*.
Expand Down Expand Up @@ -318,11 +318,11 @@ _VAR=value_ before the executable to run.
the --tty option to allocate a new pty for the child process.


\--no-copy-hard-limits
\--no-copy-hard-rlimits
Do not copy hard limit values to soft limits for all resources mentioned above.

By default, *bst* copies the hard limit to the soft limit for all of the resources mentioned
above. To avoid setting any resource values, except as directed by --limit-<limit> options,
above. To avoid setting any resource values, except as directed by --rlimit-<limit> options,
use this option.

\--no-fake-devtmpfs
Expand Down
96 changes: 48 additions & 48 deletions test/bst.t
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,23 @@ Testing persistence

$ mkdir -p foo bar; trap 'bst-unpersist foo && rmdir foo bar' EXIT; bst --persist=foo sh -c 'mount -t tmpfs none bar && echo hello > bar/greeting' && [ ! -f bar/greeting ] && bst --share mnt,user=foo sh -c '[ "$(cat '"$PWD"'/bar/greeting)" = "hello" ]'

Testing --limit core / general tests
$ bst --limit core=0 test/print_limits core
Testing --rlimit core / general tests
$ bst --rlimit core=0 test/print_limits core
core: hard=0 soft=0

$ bst --limit core=-1
bst: error in --limit core value: Invalid argument
$ bst --rlimit core=-1
bst: error in --rlimit core value: Invalid argument
[1]

$ bst --limit core=0:-1
bst: error in --limit core value: Invalid argument
$ bst --rlimit core=0:-1
bst: error in --rlimit core value: Invalid argument
[1]

$ bst --limit core=0xffffffffffffffffffffffffe 2>&1 | sed -e 's/Result not representable/Numerical result out of range/'
bst: error in --limit core value: Numerical result out of range
$ bst --rlimit core=0xffffffffffffffffffffffffe 2>&1 | sed -e 's/Result not representable/Numerical result out of range/'
bst: error in --rlimit core value: Numerical result out of range

Testing limit-copying
$ bst --no-copy-hard-limits true # smoke test
Testing rlimit-copying
$ bst --no-copy-hard-rlimits true # smoke test

$ bst test/print_limits --soft-only
as: soft=hard
Expand All @@ -158,82 +158,82 @@ Testing limit-copying
sigpending: soft=hard
stack: soft=hard

Testing --limit nofile
$ bst --limit nofile=750 test/print_limits nofile
Testing --rlimit nofile
$ bst --rlimit nofile=750 test/print_limits nofile
nofile: hard=750 soft=750

$ bst --limit nofile=750:740 test/print_limits nofile
$ bst --rlimit nofile=750:740 test/print_limits nofile
nofile: hard=750 soft=740

Testing --limit nproc
$ bst --limit nproc=3500 test/print_limits nproc
Testing --rlimit nproc
$ bst --rlimit nproc=3500 test/print_limits nproc
nproc: hard=3500 soft=3500

$ bst --limit nproc=3500:3499 test/print_limits nproc
$ bst --rlimit nproc=3500:3499 test/print_limits nproc
nproc: hard=3500 soft=3499

Testing --limit as
$ bst --limit as=: test/print_limits --soft-only as
Testing --rlimit as
$ bst --rlimit as=: test/print_limits --soft-only as
as: soft=hard

Testing --limit core
$ bst --limit core=: test/print_limits --soft-only core
Testing --rlimit core
$ bst --rlimit core=: test/print_limits --soft-only core
core: soft=hard

Testing --limit cpu
$ bst --limit cpu=: test/print_limits --soft-only cpu
Testing --rlimit cpu
$ bst --rlimit cpu=: test/print_limits --soft-only cpu
cpu: soft=hard

Testing --limit data
$ bst --limit data=: test/print_limits --soft-only data
Testing --rlimit data
$ bst --rlimit data=: test/print_limits --soft-only data
data: soft=hard

Testing --limit fsize
$ bst --limit fsize=: test/print_limits --soft-only fsize
Testing --rlimit fsize
$ bst --rlimit fsize=: test/print_limits --soft-only fsize
fsize: soft=hard

Testing --limit locks
$ bst --limit locks=: test/print_limits --soft-only locks
Testing --rlimit locks
$ bst --rlimit locks=: test/print_limits --soft-only locks
locks: soft=hard

Testing --limit memlock
$ bst --limit memlock=: test/print_limits --soft-only memlock
Testing --rlimit memlock
$ bst --rlimit memlock=: test/print_limits --soft-only memlock
memlock: soft=hard

Testing --limit msgqueue
$ bst --limit msgqueue=: test/print_limits --soft-only msgqueue
Testing --rlimit msgqueue
$ bst --rlimit msgqueue=: test/print_limits --soft-only msgqueue
msgqueue: soft=hard

Testing --limit nice
$ bst --limit nice=: test/print_limits --soft-only nice
Testing --rlimit nice
$ bst --rlimit nice=: test/print_limits --soft-only nice
nice: soft=hard

Testing --limit nofile
$ bst --limit nofile=: test/print_limits --soft-only nofile
Testing --rlimit nofile
$ bst --rlimit nofile=: test/print_limits --soft-only nofile
nofile: soft=hard

Testing --limit nproc
$ bst --limit nproc=: test/print_limits --soft-only nproc
Testing --rlimit nproc
$ bst --rlimit nproc=: test/print_limits --soft-only nproc
nproc: soft=hard

Testing --limit rss
$ bst --limit rss=: test/print_limits --soft-only rss
Testing --rlimit rss
$ bst --rlimit rss=: test/print_limits --soft-only rss
rss: soft=hard

Testing --limit rtprio
$ bst --limit rtprio=: test/print_limits --soft-only rtprio
Testing --rlimit rtprio
$ bst --rlimit rtprio=: test/print_limits --soft-only rtprio
rtprio: soft=hard

Testing --limit rttime
$ bst --limit rttime=: test/print_limits --soft-only rttime
Testing --rlimit rttime
$ bst --rlimit rttime=: test/print_limits --soft-only rttime
rttime: soft=hard

Testing --limit sigpending
$ bst --limit sigpending=: test/print_limits --soft-only sigpending
Testing --rlimit sigpending
$ bst --rlimit sigpending=: test/print_limits --soft-only sigpending
sigpending: soft=hard

Testing --limit stack
$ bst --limit stack=: test/print_limits --soft-only stack
Testing --rlimit stack
$ bst --rlimit stack=: test/print_limits --soft-only stack
stack: soft=hard

Testing Environment
Expand Down
Loading

0 comments on commit b7476a5

Please sign in to comment.