Description of problem: 12:00 < infernix> rwmjones: so one thing that i'm doing, for which i need that debug sh 12:00 < infernix> i'm running a custom tool and i need to capture its output and its return code 12:01 < infernix> it's a proprietary tool, statically compiled Notice that because infernix is using a proprietary program, adding it to the libguestfs API isn't really a good alternative. "debug sh" -- or an additional command -- should be able to capture all the output (even very large > 4 MB output) plus the return code and return it all to the caller. It's not clear if stdout and stderr should be captured separately. This might be useful in some circumstances. Version-Release number of selected component (if applicable): libguestfs 1.22
This also happened for a user trying to do: sh "rpm -Va"
The f28 version dies with oom on large output, The f29 with libguestfs-1.40.1-2.fc29.x86_64 fails with: realloc: Cannot allocate memory (OOM did not kicked in) Steps to reproduce: curl -Lo fedora.qcow2 https://download.fedoraproject.org/pub/fedora/linux/releases/29/Cloud/x86_64/images/Fedora-Cloud-Base-29-1.2.x86_64.qcow2 qemu-img create -b fedora.qcow2 -f qcow2 test.qcow2 time guestfish -a test.qcow2 -m /dev/sda1:/ sh 'cat /dev/sda1' libguestfs: error: sh: error running external command, see debug output for details The error code is not preserved, but 0 or 1 is ok. $ guestfish -a test.qcow2 -m /dev/sda1:/ sh 'exit 42' ; echo $? 1 Redirects does not seams to work: $ guestfish -a test.qcow2 -m /dev/sda1:/ sh 'echo foo >&2' (One line of nothing) $ guestfish -a test.qcow2 -m /dev/sda1:/ sh 'ls /moo 2>&1' (One line of nothing) $ guestfish -a test.qcow2 -m /dev/sda1:/ sh 'echo muu' >/tmp/muu $ hexdump -C /tmp/muu 00000000 6d 75 75 0a 0a |muu..| 00000005 Two new line instead of one. $ guestfish -a test.qcow2 -m /dev/sda1:/ sh "echo $'\0'" >/tmp/muu $ hexdump -C /tmp/muu 00000000 0a |.| 00000001 One new line instead of 0x00 . I did not find a way to pass stdin however, there are several copy in options.
The question, what would be better as default exit status in case there is failure outside the user script. The error codes above 125 is reserved for the shell, however only 64 signal defined in Linux (SIGRTMAX). Linux status field allows to have 8 bit user controller exit status, but the internal exit status has more bits. The shell just maps the higher bits into the the lower bits Typically the user space programs using 1..125 for indicating error, typically starting from 1. POSIX did not forbidden the creative usage of the above 128 statuses in any another way, the shell is mandated to use 128+N codes for terminating on signal. FreeBSD defined more signal, but 255 is still not used there. ssh using 255 in case of ssh failure. So the ssh behavior is the one which should be simulated.