Skip to content

Instantly share code, notes, and snippets.

@JonJagger
Created May 20, 2020 08:04
Show Gist options
  • Save JonJagger/70ddb173b177c767c10098865160e112 to your computer and use it in GitHub Desktop.
Save JonJagger/70ddb173b177c767c10098865160e112 to your computer and use it in GitHub Desktop.
Code showing effect when putting head -c into a redirection pipe
This the C source file from my test
#include "hiker.h"
#include <stdio.h>
int answer(void)
{
for(int i = 0; i != 100; i++)
fputs("Hello\\n", stdout);
for(;;);
return 6 * 7;
}
This is being compiled (gcc 10) and run from the bash script.
This is happening from inside a docker container (Debian 10).
This is inside VirtualBox being run on my MacBook Pro.
The redirected stdout/stderr are added to a tar file and gzipped to stdout.
This tgz file is captured by the Ruby program that ran the
docker container in a Process.spawn() call.
The reason for the spawn is so it can add a timeout.
Notice the 2nd to last line of the C program is an infinite loop.
The test is for whether any stdout comes back when the container times out.
The Ruby looks like this...
options = { pgroup:true, in:r_stdin, out:w_stdout, err:w_stderr }
pid = process.spawn(docker_run_cyber_dojo_sh, options)
timed_out = false
begin
Timeout::timeout(max_seconds) { process.wait(pid) } # [C]
rescue Timeout::Error
timed_out = true
docker_stop_container
kill_process_group(pid)
ensure
tgz_out = pipe_close(r_stdout, w_stdout)
stderr_out = pipe_close(r_stderr, w_stderr)
end
logger.write(stderr_out)
[ tgz_out, timed_out ]
Phew! This _does_ return something (Hellos) in the tgz coming back.
If I add the head filters then I get nothing coming back.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment