Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
PR #1798: ch-run: add --quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaudill authored Jan 9, 2024
1 parent e1637d9 commit 32b6e8d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 8 deletions.
3 changes: 1 addition & 2 deletions bin/ch-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,6 @@ _ch_image_complete () {
__ltrim_colon_completions "$cur"
;;
undelete)
# FIXME: Update with “ch-image undelete --list” once #1551 drops
COMPREPLY=( $(compgen -W "$(_ch_undelete_list "$strg_dir")" -- "$cur") )
;;
'')
Expand All @@ -281,7 +280,7 @@ _ch_image_complete () {

_run_opts="-b --bind -c --cd --env-no-expand --feature -g --gid
--home -j --join --join-pid --join-ct --join-tag -m
--mount --no-passwd -s --storage --seccomp -t
--mount --no-passwd -q --quiet -s --storage --seccomp -t
--private-tmp --set-env -u --uid --unsafe --unset-env
-v --verbose -w --write -? --help --usage -V --version"

Expand Down
20 changes: 18 additions & 2 deletions bin/ch-run.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ const struct argp_option options[] = {
{ "join-pid", -5, "PID", 0, "join a namespace using a PID" },
{ "join-ct", -3, "N", 0, "number of join peers (implies --join)" },
{ "join-tag", -4, "TAG", 0, "label for peer group (implies --join)" },
{ "test", -17, "TEST", 0, "do test TEST" },
{ "mount", 'm', "DIR", 0, "SquashFS mount point"},
{ "no-passwd", -9, 0, 0, "don't bind-mount /etc/{passwd,group}"},
{ "private-tmp", 't', 0, 0, "use container-private /tmp" },
{ "quiet", 'q', 0, 0, "print less output (can be repeated)"},
#ifdef HAVE_SECCOMP
{ "seccomp", -14, 0, 0,
"fake success for some syscalls with seccomp(2)"},
Expand Down Expand Up @@ -476,13 +478,21 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
args->seccomp_p = true;
break;
#endif
case -15: // --set-env0
parse_set_env(args, arg, '\0');
break;
case -16: // --warnings
for (int i = 1; i <= parse_int(arg, false, "--warnings"); i++)
WARNING("this is warning %d!", i);
exit(0);
break;
case -15: // --set-env0
parse_set_env(args, arg, '\0');
case -17: // --test
if (!strcmp(arg, "log"))
test_logging(false);
else if (!strcmp(arg, "log-fail"))
test_logging(true);
else
FATAL("invalid --test argument: %s; see source code", arg);
break;
case 'b': { // --bind
char *src, *dst;
Expand Down Expand Up @@ -525,6 +535,11 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
if (!path_exists(arg, NULL, false))
WARNING("storage directory not found: %s", arg);
break;
case 'q': // --quiet
Te(verbose <= 0, "--quiet incompatible with --verbose");
verbose--;
Te(verbose >= -3, "--quiet can be specified at most thrice");
break;
case 't': // --private-tmp
args->c.private_tmp = true;
break;
Expand All @@ -538,6 +553,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
exit(EXIT_SUCCESS);
break;
case 'v': // --verbose
Te(verbose >= 0, "--verbose incompatible with --quiet");
verbose++;
Te(verbose <= 3, "--verbose can be specified at most thrice");
break;
Expand Down
4 changes: 4 additions & 0 deletions bin/ch_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ void run_user_command(char *argv[], const char *initial_dir)
VERBOSE("executing: %s", argv_to_string(argv));

Zf (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), "can't set no_new_privs");
if (verbose < LL_INFO)
T_ (freopen("/dev/null", "w", stdout));
if (verbose < LL_STDERR)
T_ (freopen("/dev/null", "w", stderr));
execvp(argv[0], argv); // only returns if error
Tf (0, "can't execve(2): %s", argv[0]);
}
Expand Down
11 changes: 11 additions & 0 deletions bin/ch_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,17 @@ void log_ids(const char *func, int line)
}
}

void test_logging(bool fail) {
TRACE("trace");
DEBUG("debug");
VERBOSE("verbose");
INFO("info");
WARNING("warning");
if (fail)
FATAL("the program failed inexplicably (\"log-fail\" specified)");
exit(0);
}

/* Create the directory at path, despite its parent not allowing write access,
by overmounting a new, writeable directory atop it. We preserve the old
contents by bind-mounting the old directory as a subdirectory, then setting
Expand Down
6 changes: 4 additions & 2 deletions bin/ch_misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ struct env_delta {
} arg;
};

enum log_level { LL_FATAL = -2, // minimum number of -v to print the msg
enum log_level { LL_FATAL = -3,
LL_STDERR = -2,
LL_WARNING = -1,
LL_INFO = 0,
LL_INFO = 0, // minimum number of -v to print the msg
LL_VERBOSE = 1,
LL_DEBUG = 2,
LL_TRACE = 3 };
Expand Down Expand Up @@ -128,6 +129,7 @@ struct env_var env_var_parse(const char *line, const char *path, size_t lineno);
void list_append(void **ar, void *new, size_t size);
void *list_new(size_t size, size_t ct);
void log_ids(const char *func, int line);
void test_logging(bool fail);
void mkdirs(const char *base, const char *path, char **denylist,
const char *scratch);
void msg(enum log_level level, const char *file, int line, int errno_,
Expand Down
7 changes: 6 additions & 1 deletion doc/ch-run.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ mounting SquashFS images with FUSE.
bind-mounted into it. If this is specified, no such temporary files are
created and the image’s files are exposed.

:code:`-q`, :code:`--quiet`
Be quieter; can be repeated. Incompatible with :code:`-v`. See the
:ref:`faq_verbosity` for details.

:code:`-s`, :code:`--storage DIR`
Set the storage directory. Equivalent to the same option for
:code:`ch-image(1)`.
Expand Down Expand Up @@ -131,7 +135,8 @@ mounting SquashFS images with FUSE.
Unset environment variables whose names match :code:`GLOB`.

:code:`-v`, :code:`--verbose`
Be more verbose (can be repeated).
Print extra chatter; can be repeated. See the :ref:`FAQ entry on verbosity
<faq_verbosity>` for details.

:code:`-w`, :code:`--write`
Mount image read-write. By default, the image is mounted read-only. *This
Expand Down
7 changes: 6 additions & 1 deletion doc/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ indicates printed, ❌ suppressed).
- ✅
- [1]
- [1]
- [1]
- [1] [2]
- ❌
- ❌
* - warning
Expand Down Expand Up @@ -1324,6 +1324,10 @@ Notes:
:code:`RUN`) and sometimes it’s captured for internal use (e.g., many
:code:`git(1)` invocations).


2. In the case of :code:`ch-run`, the user command is considered a subprocess,
e.g. :code:`ch-run -q example -- echo foo` will produce no output.

.. _faq_xattrs:

How do I handle extended attributes in Charliecloud?
Expand Down Expand Up @@ -1357,6 +1361,7 @@ conversion. Important caveats include:
in the :code:`user` namespace prior to Linux kernel `upstream 6.6
<https://kernelnewbies.org/Linux_6.6#TMPFSe>`_ (Oct 2023).


.. LocalWords: CAs SY Gutmann AUTH rHsFFqwwqh MrieaQ Za loc mpihello mvo du
.. LocalWords: VirtualSize linuxcontainers jour uk lxd rwxr xr qq qqq drwxr
.. LocalWords: drwx
49 changes: 49 additions & 0 deletions test/run/ch-run_misc.bats
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,55 @@ EOF

}

@test 'ch-run --quiet' {
# test --logging-test
run ch-run --test=log
echo "$output"
[[ $status -eq 0 ]]
[[ $output = *'info'* ]]
[[ $output = *'warning: warning'* ]]

# quiet level 1
run ch-run -q --test=log
echo "$output"
[[ $status -eq 0 ]]
[[ $output != *'info'* ]]
[[ $output = *'warning: warning'* ]]

# quiet level 2
run ch-run -qq --test=log
echo "$output"
[[ $status -eq 0 ]]
[[ $output != *'info'* ]]
[[ $output != *'warning: warning'* ]]

# subprocess failure at quiet level 2
run ch-run -qq "$ch_timg" -- doesnotexist
echo "$output"
[[ $status -eq 1 ]]
[[ $output = *"error: can't execve(2): doesnotexist: No such file or directory"* ]]

# quiet level 3
run ch-run -qqq --test=log
echo "$output"
[[ $status -eq 0 ]]
[[ $output != *'info'* ]]
[[ $output != *"warning: warning"* ]]

# subprocess failure at quiet level 3
run ch-run -qqq "$ch_timg" -- doesnotexist
echo "$output"
[[ $status -eq 1 ]]
[[ $output != *"error: can't execve(2): doesnotexist: No such file or directory"* ]]

# failure at quiet level 3
run ch-run -qqq --test=log-fail
echo "$output"
[[ $status -eq 1 ]]
[[ $output != *'info'* ]]
[[ $output != *'warning: warning'* ]]
[[ $output = *'error: the program failed inexplicably'* ]]
}

@test 'ch-run --write-fake errors' {
demand-overlayfs
Expand Down

0 comments on commit 32b6e8d

Please sign in to comment.