improve hook-helper and hook-listener debug output

This commit is contained in:
Johannes 'josch' Schauer 2020-11-13 22:36:58 +01:00
parent bf87e83bdb
commit e71487af5e
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -3068,9 +3068,12 @@ sub hookhelper {
# open a tar process that extracts the tarfile that we # open a tar process that extracts the tarfile that we
# supply it with on stdin to the output directory inside # supply it with on stdin to the output directory inside
# the chroot # the chroot
open $fh, '|-', @cmdprefix, @tarcmd, '--xattrs-include=*', my @cmd = (
'--directory', $directory, '--extract', '--file', @cmdprefix, @tarcmd, '--xattrs-include=*',
'-' // error "failed to fork(): $!"; '--directory', $directory, '--extract', '--file', '-'
);
debug("helper: running " . (join " ", @cmd));
open($fh, '|-', @cmd) // error "failed to fork(): $!";
} else { } else {
error "unknown command: $command"; error "unknown command: $command";
} }
@ -3078,26 +3081,26 @@ sub hookhelper {
if ($command eq 'copy-in') { if ($command eq 'copy-in') {
# instruct the parent process to create a tarball of the # instruct the parent process to create a tarball of the
# requested path outside the chroot # requested path outside the chroot
debug "sending mktar"; debug "helper: sending mktar";
print STDOUT ( print STDOUT (
pack("n", length $ARGV[$i]) . "mktar" . $ARGV[$i]); pack("n", length $ARGV[$i]) . "mktar" . $ARGV[$i]);
} elsif ($command eq 'sync-in') { } elsif ($command eq 'sync-in') {
# instruct the parent process to create a tarball of the # instruct the parent process to create a tarball of the
# content of the requested path outside the chroot # content of the requested path outside the chroot
debug "sending mktac"; debug "helper: sending mktac";
print STDOUT ( print STDOUT (
pack("n", length $ARGV[$i]) . "mktac" . $ARGV[$i]); pack("n", length $ARGV[$i]) . "mktac" . $ARGV[$i]);
} elsif (any { $_ eq $command } ('upload', 'tar-in')) { } elsif (any { $_ eq $command } ('upload', 'tar-in')) {
# instruct parent process to open a tarball of the # instruct parent process to open a tarball of the
# requested path outside the chroot for reading # requested path outside the chroot for reading
debug "sending openr"; debug "helper: sending openr";
print STDOUT ( print STDOUT (
pack("n", length $ARGV[$i]) . "openr" . $ARGV[$i]); pack("n", length $ARGV[$i]) . "openr" . $ARGV[$i]);
} else { } else {
error "unknown command: $command"; error "unknown command: $command";
} }
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "helper: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
# handle "write" messages from the parent process and feed # handle "write" messages from the parent process and feed
@ -3111,13 +3114,13 @@ sub hookhelper {
error "received eof on socket"; error "received eof on socket";
} }
my ($len, $msg) = unpack("nA5", $buf); my ($len, $msg) = unpack("nA5", $buf);
debug "received message: $msg"; debug "helper: received message: $msg";
if ($msg eq "close") { if ($msg eq "close") {
# finish the loop # finish the loop
if ($len != 0) { if ($len != 0) {
error "expected no payload but got $len bytes"; error "expected no payload but got $len bytes";
} }
debug "sending okthx"; debug "helper: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3137,7 +3140,7 @@ sub hookhelper {
# write the payload to the tar process # write the payload to the tar process
print $fh $content print $fh $content
or error "cannot write to tar process: $!"; or error "cannot write to tar process: $!";
debug "sending okthx"; debug "helper: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3206,17 +3209,23 @@ sub hookhelper {
# Open a tar process that creates a tarfile of everything # Open a tar process that creates a tarfile of everything
# inside the requested directory inside the chroot and # inside the requested directory inside the chroot and
# writes it to stdout. # writes it to stdout.
open $fh, '-|', @cmdprefix, @tarcmd, '--directory', my @cmd = (
$directory, '--create', '--file', '-', @cmdprefix, @tarcmd, '--directory',
'.' // error "failed to fork(): $!"; $directory, '--create', '--file', '-', '.'
);
debug("helper: running " . (join " ", @cmd));
open($fh, '-|', @cmd) // error "failed to fork(): $!";
} elsif (any { $_ eq $command } ('copy-out', 'tar-out')) { } elsif (any { $_ eq $command } ('copy-out', 'tar-out')) {
# Open a tar process that creates a tarfile of the # Open a tar process that creates a tarfile of the
# requested directory inside the chroot and writes it to # requested directory inside the chroot and writes it to
# stdout. To emulate the behaviour of cp, change to the # stdout. To emulate the behaviour of cp, change to the
# dirname of the requested path first. # dirname of the requested path first.
open $fh, '-|', @cmdprefix, @tarcmd, '--directory', my @cmd = (
@cmdprefix, @tarcmd, '--directory',
dirname($directory), '--create', '--file', '-', dirname($directory), '--create', '--file', '-',
basename($directory) // error "failed to fork(): $!"; basename($directory));
debug("helper: running " . (join " ", @cmd));
open($fh, '-|', @cmd) // error "failed to fork(): $!";
} else { } else {
error "unknown command: $command"; error "unknown command: $command";
} }
@ -3224,20 +3233,20 @@ sub hookhelper {
if (any { $_ eq $command } ('copy-out', 'sync-out')) { if (any { $_ eq $command } ('copy-out', 'sync-out')) {
# instruct the parent process to extract a tarball to a # instruct the parent process to extract a tarball to a
# certain path outside the chroot # certain path outside the chroot
debug "sending untar"; debug "helper: sending untar";
print STDOUT ( print STDOUT (
pack("n", length $outpath) . "untar" . $outpath); pack("n", length $outpath) . "untar" . $outpath);
} elsif (any { $_ eq $command } ('download', 'tar-out')) { } elsif (any { $_ eq $command } ('download', 'tar-out')) {
# instruct parent process to open a tarball of the # instruct parent process to open a tarball of the
# requested path outside the chroot for writing # requested path outside the chroot for writing
debug "sending openw"; debug "helper: sending openw";
print STDOUT ( print STDOUT (
pack("n", length $outpath) . "openw" . $outpath); pack("n", length $outpath) . "openw" . $outpath);
} else { } else {
error "unknown command: $command"; error "unknown command: $command";
} }
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "helper: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
# read from the tar process and send as payload to the parent # read from the tar process and send as payload to the parent
@ -3247,20 +3256,20 @@ sub hookhelper {
my $ret = read($fh, my $cont, 4096) my $ret = read($fh, my $cont, 4096)
// error "cannot read from pipe: $!"; // error "cannot read from pipe: $!";
if ($ret == 0) { last; } if ($ret == 0) { last; }
debug "sending write"; debug "helper: sending write";
# send to parent # send to parent
print STDOUT pack("n", $ret) . "write" . $cont; print STDOUT pack("n", $ret) . "write" . $cont;
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "helper: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
if ($ret < 4096) { last; } if ($ret < 4096) { last; }
} }
# signal to the parent process that we are done # signal to the parent process that we are done
debug "sending close"; debug "helper: sending close";
print STDOUT pack("n", 0) . "close"; print STDOUT pack("n", 0) . "close";
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "helper: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
close $fh; close $fh;
@ -3291,17 +3300,17 @@ sub hooklistener {
my $msg = "error"; my $msg = "error";
my $len = -1; my $len = -1;
{ {
debug "reading next command"; debug "listener: reading next command";
my $ret = read(STDIN, my $buf, 2 + 5) my $ret = read(STDIN, my $buf, 2 + 5)
// error "cannot read from socket: $!"; // error "cannot read from socket: $!";
debug "finished reading command"; debug "listener: finished reading command";
if ($ret == 0) { if ($ret == 0) {
error "received eof on socket"; error "received eof on socket";
} }
($len, $msg) = unpack("nA5", $buf); ($len, $msg) = unpack("nA5", $buf);
} }
if ($msg eq "adios") { if ($msg eq "adios") {
debug "received message: adios"; debug "listener: received message: adios";
# setup finished, so we break out of the loop # setup finished, so we break out of the loop
if ($len != 0) { if ($len != 0) {
error "expected no payload but got $len bytes"; error "expected no payload but got $len bytes";
@ -3309,7 +3318,7 @@ sub hooklistener {
last; last;
} elsif ($msg eq "openr") { } elsif ($msg eq "openr") {
# handle the openr message # handle the openr message
debug "received message: openr"; debug "listener: received message: openr";
my $infile; my $infile;
{ {
my $ret = read(STDIN, $infile, $len) my $ret = read(STDIN, $infile, $len)
@ -3322,7 +3331,7 @@ sub hooklistener {
if (!-e $infile) { if (!-e $infile) {
error "$infile does not exist"; error "$infile does not exist";
} }
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3336,25 +3345,25 @@ sub hooklistener {
my $ret = read($fh, my $cont, 4096) my $ret = read($fh, my $cont, 4096)
// error "cannot read from pipe: $!"; // error "cannot read from pipe: $!";
if ($ret == 0) { last; } if ($ret == 0) { last; }
debug "sending write"; debug "listener: sending write";
# send to child # send to child
print STDOUT pack("n", $ret) . "write" . $cont; print STDOUT pack("n", $ret) . "write" . $cont;
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "listener: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
if ($ret < 4096) { last; } if ($ret < 4096) { last; }
} }
# signal to the child process that we are done # signal to the child process that we are done
debug "sending close"; debug "listener: sending close";
print STDOUT pack("n", 0) . "close"; print STDOUT pack("n", 0) . "close";
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "listener: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
close $fh; close $fh;
} elsif ($msg eq "openw") { } elsif ($msg eq "openw") {
debug "received message: openw"; debug "listener: received message: openw";
# payload is the output directory # payload is the output directory
my $outfile; my $outfile;
{ {
@ -3383,7 +3392,7 @@ sub hooklistener {
error "cannot create $outdir"; error "cannot create $outdir";
} }
} }
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3404,13 +3413,13 @@ sub hooklistener {
error "received eof on socket"; error "received eof on socket";
} }
my ($len, $msg) = unpack("nA5", $buf); my ($len, $msg) = unpack("nA5", $buf);
debug "received message: $msg"; debug "listener: received message: $msg";
if ($msg eq "close") { if ($msg eq "close") {
# finish the loop # finish the loop
if ($len != 0) { if ($len != 0) {
error "expected no payload but got $len bytes"; error "expected no payload but got $len bytes";
} }
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3431,7 +3440,7 @@ sub hooklistener {
# write the payload to the file handle # write the payload to the file handle
print $fh $content print $fh $content
or error "cannot write to file handle: $!"; or error "cannot write to file handle: $!";
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3439,7 +3448,7 @@ sub hooklistener {
close $fh; close $fh;
} elsif (any { $_ eq $msg } ('mktar', 'mktac')) { } elsif (any { $_ eq $msg } ('mktar', 'mktac')) {
# handle the mktar message # handle the mktar message
debug "received message: $msg"; debug "listener: received message: $msg";
my $indir; my $indir;
{ {
my $ret = read(STDIN, $indir, $len) my $ret = read(STDIN, $indir, $len)
@ -3452,7 +3461,7 @@ sub hooklistener {
if (!-e $indir) { if (!-e $indir) {
error "$indir does not exist"; error "$indir does not exist";
} }
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3460,15 +3469,22 @@ sub hooklistener {
# Open a tar process creating a tarfile of the instructed # Open a tar process creating a tarfile of the instructed
# path. To emulate the behaviour of cp, change to the # path. To emulate the behaviour of cp, change to the
# dirname of the requested path first. # dirname of the requested path first.
open my $fh, '-|', 'tar', '--numeric-owner', '--xattrs', my @cmd = (
'tar',
'--numeric-owner',
'--xattrs',
'--format=pax', '--format=pax',
'--pax-option=exthdr.name=%d/PaxHeaders/%f,' '--pax-option=exthdr.name=%d/PaxHeaders/%f,'
. 'delete=atime,delete=ctime', . 'delete=atime,delete=ctime',
'--directory', '--directory',
$msg eq 'mktar' ? dirname($indir) : $indir, $msg eq 'mktar' ? dirname($indir) : $indir,
'--create', '--file', '-', '--create',
'--file',
'-',
$msg eq 'mktar' ? basename($indir) : '.' $msg eq 'mktar' ? basename($indir) : '.'
// error "failed to fork(): $!"; );
debug("listener: running " . (join " ", @cmd));
open(my $fh, '-|', @cmd) // error "failed to fork(): $!";
# read from the tar process and send as payload to the child # read from the tar process and send as payload to the child
# process # process
@ -3477,20 +3493,20 @@ sub hooklistener {
my $ret = read($fh, my $cont, 4096) my $ret = read($fh, my $cont, 4096)
// error "cannot read from pipe: $!"; // error "cannot read from pipe: $!";
if ($ret == 0) { last; } if ($ret == 0) { last; }
debug "sending write"; debug "listener: sending write ($ret bytes)";
# send to child # send to child
print STDOUT pack("n", $ret) . "write" . $cont; print STDOUT pack("n", $ret) . "write" . $cont;
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "listener: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
if ($ret < 4096) { last; } if ($ret < 4096) { last; }
} }
# signal to the child process that we are done # signal to the child process that we are done
debug "sending close"; debug "listener: sending close";
print STDOUT pack("n", 0) . "close"; print STDOUT pack("n", 0) . "close";
STDOUT->flush(); STDOUT->flush();
debug "waiting for okthx"; debug "listener: waiting for okthx";
checkokthx \*STDIN; checkokthx \*STDIN;
close $fh; close $fh;
@ -3498,7 +3514,7 @@ sub hooklistener {
error "tar failed"; error "tar failed";
} }
} elsif ($msg eq "untar") { } elsif ($msg eq "untar") {
debug "received message: untar"; debug "listener: received message: untar";
# payload is the output directory # payload is the output directory
my $outdir; my $outdir;
{ {
@ -3526,7 +3542,7 @@ sub hooklistener {
error "cannot create $outdir"; error "cannot create $outdir";
} }
} }
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3549,13 +3565,13 @@ sub hooklistener {
error "received eof on socket"; error "received eof on socket";
} }
my ($len, $msg) = unpack("nA5", $buf); my ($len, $msg) = unpack("nA5", $buf);
debug "received message: $msg"; debug "listener: received message: $msg";
if ($msg eq "close") { if ($msg eq "close") {
# finish the loop # finish the loop
if ($len != 0) { if ($len != 0) {
error "expected no payload but got $len bytes"; error "expected no payload but got $len bytes";
} }
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();
@ -3576,7 +3592,7 @@ sub hooklistener {
# write the payload to the tar process # write the payload to the tar process
print $fh $content print $fh $content
or error "cannot write to tar process: $!"; or error "cannot write to tar process: $!";
debug "sending okthx"; debug "listener: sending okthx";
print STDOUT (pack("n", 0) . "okthx") print STDOUT (pack("n", 0) . "okthx")
or error "cannot write to socket: $!"; or error "cannot write to socket: $!";
STDOUT->flush(); STDOUT->flush();