From ebfac917386d2be7073d5b808a3e7e93533dfffb Mon Sep 17 00:00:00 2001 From: Johannes Schauer Marin Rodrigues Date: Tue, 4 May 2021 15:01:25 +0200 Subject: [PATCH] also choose null format if stdout is /dev/null and check whether major and minor number of /dev/null are as expected to avoid false positives --- mmdebstrap | 70 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/mmdebstrap b/mmdebstrap index 0b826f1..e02bd31 100755 --- a/mmdebstrap +++ b/mmdebstrap @@ -42,6 +42,7 @@ use Carp; use Term::ANSIColor; use Socket; use Time::HiRes; +use Math::BigInt; use version; ## no critic (InputOutput::RequireBriefOpen) @@ -175,6 +176,25 @@ sub error { } } +# The encoding of dev_t is MMMM Mmmm mmmM MMmm, where M is a hex digit of +# the major number and m is a hex digit of the minor number. +sub major { + my $rdev = shift; + my $right + = Math::BigInt->from_hex("0x00000000000fff00")->band($rdev)->brsft(8); + my $left + = Math::BigInt->from_hex("0xfffff00000000000")->band($rdev)->brsft(32); + return $right->bior($left); +} + +sub minor { + my $rdev = shift; + my $right = Math::BigInt->from_hex("0x00000000000000ff")->band($rdev); + my $left + = Math::BigInt->from_hex("0x00000ffffff00000")->band($rdev)->brsft(12); + return $right->bior($left); +} + # check whether a directory is mounted by comparing the device number of the # directory itself with its parent sub is_mountpoint { @@ -5149,7 +5169,22 @@ sub main() { # figure out the right format if ($format eq 'auto') { - if ($options->{target} eq '/dev/null') { + # (stat(...))[6] is the device identifier which contains the major and + # minor numbers for character special files + # major 1 and minor 3 is /dev/null on Linux + if ( $options->{target} eq '/dev/null' + and $OSNAME eq 'linux' + and -c '/dev/null' + and major((stat("/dev/null"))[6]) == 1 + and minor((stat("/dev/null"))[6]) == 3) { + $format = 'null'; + } elsif ($options->{target} eq '-' + and $OSNAME eq 'linux' + and major((stat(STDOUT))[6]) == 1 + and minor((stat(STDOUT))[6]) == 3) { + # by checking the major and minor number of the STDOUT fd we also + # can detect redirections to /dev/null and choose the null format + # accordingly $format = 'null'; } elsif ($options->{target} ne '-' and -d $options->{target}) { $format = 'directory'; @@ -5226,6 +5261,11 @@ sub main() { error "the $format format is unable to write to standard output"; } + if ($format eq 'null' + and none { $_ eq $options->{target} } ('-', '/dev/null')) { + info "ignoring target $options->{target} with null format"; + } + if (any { $_ eq $format } ('tar', 'squashfs', 'ext2', 'null')) { if ($format ne 'null') { if ( any { $_ eq $options->{variant} } ('extract', 'custom') @@ -5777,9 +5817,9 @@ sub main() { # change signal handler message $waiting_for = "cleanup"; - if (any { $_ eq $format } ('directory', 'null')) { + if (any { $_ eq $format } ('directory')) { # nothing to do - } elsif (any { $_ eq $format } ('tar', 'squashfs', 'ext2')) { + } elsif (any { $_ eq $format } ('tar', 'squashfs', 'ext2', 'null')) { if (!-e $options->{root}) { error "$options->{root} does not exist"; } @@ -6405,16 +6445,17 @@ Without that option the default format is I. The following formats exist: When selecting this format (the default), the actual format will be inferred from the I positional argument. If I was not specified, then -the B format will be chosen. If I happens to be F, then -the B format will be chosen. If I is an existing directory, and -does not equal to C<->, then the B format will be chosen. If -I ends with C<.tar> or with one of the filename extensions listed in -the section B, or if I equals C<->, or if I is a -named pipe (fifo) or if I is a character special file like -F, then the B format will be chosen. If I ends with -C<.squashfs> or C<.sqfs>, then the B format will be chosen. If - ends with C<.ext2> then the B format will be chosen. If none of -these conditions apply, the B format will be chosen. +the B format will be chosen. If I happens to be F or if +standard output is F, then the B format will be chosen. If +I is an existing directory, and does not equal to C<->, then the +B format will be chosen. If I ends with C<.tar> or with one +of the filename extensions listed in the section B, or if +I equals C<->, or if I is a named pipe (fifo) or if I +is a character special file, then the B format will be chosen. If +I ends with C<.squashfs> or C<.sqfs>, then the B format will +be chosen. If ends with C<.ext2> then the B format will be +chosen. If none of these conditions apply, the B format will be +chosen. =item B, B @@ -6468,7 +6509,8 @@ A temporary chroot directory will be created in C<$TMPDIR> or F if C<$TMPDIR> is not set. After the bootstrap is complete, the temporary chroot will be deleted without being part of the output. This is most useful when the desired artifact is generated inside the chroot and it is transferred using -special hooks such as B. +special hooks such as B. It is also useful in situations where only +the exit code or stdout or stderr of a process run in a hook is of interest. =back