Skip to content

Instantly share code, notes, and snippets.

@davidberard98
Created July 16, 2019 05:30
Show Gist options
  • Save davidberard98/3725516b29f0bdd76ad9260e69402f09 to your computer and use it in GitHub Desktop.
Save davidberard98/3725516b29f0bdd76ad9260e69402f09 to your computer and use it in GitHub Desktop.
atop fails while generating parseables when reading from a pipe
#!/bin/bash
# demo.sh: demonstrate read() issue
# Usage: bash demo.sh [atop binary path] [atop log path]
#
# What it does:
# - write and compile demo.c
# - makes a named pipe
# - starts a process for demo.c, which writes small chunks to the pipe periodically.
# - runs atop -r [pipe] -P cpu
#
# Atop exits with an exit code even with https://github.com/Atoptool/atop/pull/69
#
# Note: if this behavior isn't observed, try changing the number "1571" in the code below
if [[ "$#" -ne "2" ]]; then
echo "Usage: bash demo.sh [atop binary path] [atop log path]"
exit 1;
fi
bytes=1571
cat > demo.c << EOF
#define _BSD_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*
* usage: ./demo [infile] [outfifo] [number of bytes to write]
*/
int main(int argc, char ** argv)
{
if (argc != 4)
{
return 1;
}
size_t bytes = strtol(argv[3], NULL, 10);
FILE * f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END);
size_t sz = ftell(f);
rewind(f);
char * buf = malloc(sz);
fread(buf, 1, sz, f);
fclose(f);
FILE * g = fopen(argv[2], "wb");
size_t cur = 0;
while (cur < sz)
{
size_t amt = bytes;
if (sz - cur < bytes)
{
amt = sz - cur;
}
size_t n = fwrite(buf + cur, 1, amt, g);
cur += n;
printf("Just wrote %lu bytes\n", n);
usleep(100*1000);
}
fclose(g);
return 0;
}
EOF
gcc -std=c11 demo.c -o demo
echo "Done compiling"
rm demofifo
mkfifo demofifo
./demo $2 demofifo $bytes &
sleep 1
echo "Starting atop"
$1 -r demofifo -P cpu
ret=$?
echo "ATOP EXITED WITH $ret"
pkill -fo demofifo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment