Skip to content
This repository was archived by the owner on May 27, 2025. It is now read-only.

Commit 32b6e8d

Browse files
authored
PR #1798: ch-run: add --quiet
1 parent e1637d9 commit 32b6e8d

File tree

8 files changed

+99
-8
lines changed

8 files changed

+99
-8
lines changed

bin/ch-completion.bash

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ _ch_image_complete () {
258258
__ltrim_colon_completions "$cur"
259259
;;
260260
undelete)
261-
# FIXME: Update with “ch-image undelete --list” once #1551 drops
262261
COMPREPLY=( $(compgen -W "$(_ch_undelete_list "$strg_dir")" -- "$cur") )
263262
;;
264263
'')
@@ -281,7 +280,7 @@ _ch_image_complete () {
281280

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

bin/ch-run.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ const struct argp_option options[] = {
6060
{ "join-pid", -5, "PID", 0, "join a namespace using a PID" },
6161
{ "join-ct", -3, "N", 0, "number of join peers (implies --join)" },
6262
{ "join-tag", -4, "TAG", 0, "label for peer group (implies --join)" },
63+
{ "test", -17, "TEST", 0, "do test TEST" },
6364
{ "mount", 'm', "DIR", 0, "SquashFS mount point"},
6465
{ "no-passwd", -9, 0, 0, "don't bind-mount /etc/{passwd,group}"},
6566
{ "private-tmp", 't', 0, 0, "use container-private /tmp" },
67+
{ "quiet", 'q', 0, 0, "print less output (can be repeated)"},
6668
#ifdef HAVE_SECCOMP
6769
{ "seccomp", -14, 0, 0,
6870
"fake success for some syscalls with seccomp(2)"},
@@ -476,13 +478,21 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
476478
args->seccomp_p = true;
477479
break;
478480
#endif
481+
case -15: // --set-env0
482+
parse_set_env(args, arg, '\0');
483+
break;
479484
case -16: // --warnings
480485
for (int i = 1; i <= parse_int(arg, false, "--warnings"); i++)
481486
WARNING("this is warning %d!", i);
482487
exit(0);
483488
break;
484-
case -15: // --set-env0
485-
parse_set_env(args, arg, '\0');
489+
case -17: // --test
490+
if (!strcmp(arg, "log"))
491+
test_logging(false);
492+
else if (!strcmp(arg, "log-fail"))
493+
test_logging(true);
494+
else
495+
FATAL("invalid --test argument: %s; see source code", arg);
486496
break;
487497
case 'b': { // --bind
488498
char *src, *dst;
@@ -525,6 +535,11 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
525535
if (!path_exists(arg, NULL, false))
526536
WARNING("storage directory not found: %s", arg);
527537
break;
538+
case 'q': // --quiet
539+
Te(verbose <= 0, "--quiet incompatible with --verbose");
540+
verbose--;
541+
Te(verbose >= -3, "--quiet can be specified at most thrice");
542+
break;
528543
case 't': // --private-tmp
529544
args->c.private_tmp = true;
530545
break;
@@ -538,6 +553,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
538553
exit(EXIT_SUCCESS);
539554
break;
540555
case 'v': // --verbose
556+
Te(verbose >= 0, "--verbose incompatible with --quiet");
541557
verbose++;
542558
Te(verbose <= 3, "--verbose can be specified at most thrice");
543559
break;

bin/ch_core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ void run_user_command(char *argv[], const char *initial_dir)
536536
VERBOSE("executing: %s", argv_to_string(argv));
537537

538538
Zf (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0), "can't set no_new_privs");
539+
if (verbose < LL_INFO)
540+
T_ (freopen("/dev/null", "w", stdout));
541+
if (verbose < LL_STDERR)
542+
T_ (freopen("/dev/null", "w", stderr));
539543
execvp(argv[0], argv); // only returns if error
540544
Tf (0, "can't execve(2): %s", argv[0]);
541545
}

bin/ch_misc.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,17 @@ void log_ids(const char *func, int line)
442442
}
443443
}
444444

445+
void test_logging(bool fail) {
446+
TRACE("trace");
447+
DEBUG("debug");
448+
VERBOSE("verbose");
449+
INFO("info");
450+
WARNING("warning");
451+
if (fail)
452+
FATAL("the program failed inexplicably (\"log-fail\" specified)");
453+
exit(0);
454+
}
455+
445456
/* Create the directory at path, despite its parent not allowing write access,
446457
by overmounting a new, writeable directory atop it. We preserve the old
447458
contents by bind-mounting the old directory as a subdirectory, then setting

bin/ch_misc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ struct env_delta {
9595
} arg;
9696
};
9797

98-
enum log_level { LL_FATAL = -2, // minimum number of -v to print the msg
98+
enum log_level { LL_FATAL = -3,
99+
LL_STDERR = -2,
99100
LL_WARNING = -1,
100-
LL_INFO = 0,
101+
LL_INFO = 0, // minimum number of -v to print the msg
101102
LL_VERBOSE = 1,
102103
LL_DEBUG = 2,
103104
LL_TRACE = 3 };
@@ -128,6 +129,7 @@ struct env_var env_var_parse(const char *line, const char *path, size_t lineno);
128129
void list_append(void **ar, void *new, size_t size);
129130
void *list_new(size_t size, size_t ct);
130131
void log_ids(const char *func, int line);
132+
void test_logging(bool fail);
131133
void mkdirs(const char *base, const char *path, char **denylist,
132134
const char *scratch);
133135
void msg(enum log_level level, const char *file, int line, int errno_,

doc/ch-run.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ mounting SquashFS images with FUSE.
9797
bind-mounted into it. If this is specified, no such temporary files are
9898
created and the image’s files are exposed.
9999

100+
:code:`-q`, :code:`--quiet`
101+
Be quieter; can be repeated. Incompatible with :code:`-v`. See the
102+
:ref:`faq_verbosity` for details.
103+
100104
:code:`-s`, :code:`--storage DIR`
101105
Set the storage directory. Equivalent to the same option for
102106
:code:`ch-image(1)`.
@@ -131,7 +135,8 @@ mounting SquashFS images with FUSE.
131135
Unset environment variables whose names match :code:`GLOB`.
132136

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

136141
:code:`-w`, :code:`--write`
137142
Mount image read-write. By default, the image is mounted read-only. *This

doc/faq.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ indicates printed, ❌ suppressed).
12891289
- ✅
12901290
- [1]
12911291
- [1]
1292-
- [1]
1292+
- [1] [2]
12931293
- ❌
12941294
- ❌
12951295
* - warning
@@ -1324,6 +1324,10 @@ Notes:
13241324
:code:`RUN`) and sometimes it’s captured for internal use (e.g., many
13251325
:code:`git(1)` invocations).
13261326

1327+
1328+
2. In the case of :code:`ch-run`, the user command is considered a subprocess,
1329+
e.g. :code:`ch-run -q example -- echo foo` will produce no output.
1330+
13271331
.. _faq_xattrs:
13281332

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

1364+
13601365
.. LocalWords: CAs SY Gutmann AUTH rHsFFqwwqh MrieaQ Za loc mpihello mvo du
13611366
.. LocalWords: VirtualSize linuxcontainers jour uk lxd rwxr xr qq qqq drwxr
13621367
.. LocalWords: drwx

test/run/ch-run_misc.bats

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,55 @@ EOF
11181118

11191119
}
11201120

1121+
@test 'ch-run --quiet' {
1122+
# test --logging-test
1123+
run ch-run --test=log
1124+
echo "$output"
1125+
[[ $status -eq 0 ]]
1126+
[[ $output = *'info'* ]]
1127+
[[ $output = *'warning: warning'* ]]
1128+
1129+
# quiet level 1
1130+
run ch-run -q --test=log
1131+
echo "$output"
1132+
[[ $status -eq 0 ]]
1133+
[[ $output != *'info'* ]]
1134+
[[ $output = *'warning: warning'* ]]
1135+
1136+
# quiet level 2
1137+
run ch-run -qq --test=log
1138+
echo "$output"
1139+
[[ $status -eq 0 ]]
1140+
[[ $output != *'info'* ]]
1141+
[[ $output != *'warning: warning'* ]]
1142+
1143+
# subprocess failure at quiet level 2
1144+
run ch-run -qq "$ch_timg" -- doesnotexist
1145+
echo "$output"
1146+
[[ $status -eq 1 ]]
1147+
[[ $output = *"error: can't execve(2): doesnotexist: No such file or directory"* ]]
1148+
1149+
# quiet level 3
1150+
run ch-run -qqq --test=log
1151+
echo "$output"
1152+
[[ $status -eq 0 ]]
1153+
[[ $output != *'info'* ]]
1154+
[[ $output != *"warning: warning"* ]]
1155+
1156+
# subprocess failure at quiet level 3
1157+
run ch-run -qqq "$ch_timg" -- doesnotexist
1158+
echo "$output"
1159+
[[ $status -eq 1 ]]
1160+
[[ $output != *"error: can't execve(2): doesnotexist: No such file or directory"* ]]
1161+
1162+
# failure at quiet level 3
1163+
run ch-run -qqq --test=log-fail
1164+
echo "$output"
1165+
[[ $status -eq 1 ]]
1166+
[[ $output != *'info'* ]]
1167+
[[ $output != *'warning: warning'* ]]
1168+
[[ $output = *'error: the program failed inexplicably'* ]]
1169+
}
11211170

11221171
@test 'ch-run --write-fake errors' {
11231172
demand-overlayfs

0 commit comments

Comments
 (0)