Skip to content

Instantly share code, notes, and snippets.

@mcast
Last active August 29, 2015 14:19
Show Gist options
  • Save mcast/a6c00794f00442c99f86 to your computer and use it in GitHub Desktop.
Save mcast/a6c00794f00442c99f86 to your computer and use it in GitHub Desktop.
#! /usr/bin/perl
use strict;
use warnings;
my ($fn, $sleep) = @ARGV;
my $top = do { local $/ = \"65536"; my $fh = o($fn); <$fh> };
printf "Read %d bytes from %s\n", length($top), $fn;
sleep $sleep;
my @s = stat( o($fn) );
print "Stat $fn = (@s)\n";
sub o {
my ($fn) = @_;
open my $fh, '<', $fn or die "read $fn: $!";
return $fh;
}
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define READLEN 65536
int main(int argc, char** argv) {
int wait, ret, fd;
off_t retoff;
ssize_t readlen;
char* fn;
char buff[READLEN];
struct stat st;
/* avoid misleading noise in later parrot trace */
fprintf(stderr, "prime stderr\n");
printf("prime stdout\n");
if (argc != 3) exit(3);
fn = argv[1];
wait = atoi(argv[2]);
ret = open(fn, O_RDONLY);
if (ret < 0) {
perror("open");
exit(4);
} else {
fd = ret;
}
readlen = read(fd, &buff, READLEN);
/*
if (close(fd)) {
perror("close");
exit(5);
}
*/ printf(" NOT CLOSED %d\n", fd);
printf("Read %lld bytes from %s\n", (long long)readlen, fn);
sleep(wait);
ret = open(fn, O_RDONLY);
if (ret < 0) {
perror("open");
exit(6);
} else {
fd = ret;
}
/**** attempt to prevent the crash by doing stuff
*/
ret = ioctl(fd, TCGETS, &buff);
if (ret < 0) {
perror("ioctl(TCGETS)");
}
/* ignore the (struct termios)buff */
retoff = lseek(fd, 0, SEEK_CUR);
if (retoff < 0) {
perror("lseek");
} else {
printf("lseek(%d) => %lld\n", fd, (long long)retoff);
}
/*
**** end irrelevant stuff */
ret = fstat(fd, &st); /* BOOM! */
printf("Stat %s, size %lld bytes\n", fn, (long long) st.st_size);
if (close(fd)) {
perror("close");
exit(8);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment