#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#define CONTROL_ERROR 1
#define TEST_ERROR 2
int main(int argc, char **argv)
{
int bsize, quiet, usepid;
char *hoge, *buf;
char prop[] = "/\b-\b\\\b|\b";
int testfd, controlfd;
int c, i, j, k, n, p;
char *ctldir;
unsigned long rseed;
const char *prog = getprogname();
bsize = 8192;
n = 10000;
ctldir = "/var/tmp";
quiet = 0;
usepid = 0;
rseed = time(0);
while ((c = getopt(argc, argv, "b:c:n:pqs:")) != -1) {
switch(c) {
case 'b':
bsize = atoi(optarg);
break;
case 'c':
ctldir = optarg;
break;
case 'n':
n = atoi(optarg);
break;
case 'p':
++usepid;
break;
case 'q':
++quiet;
break;
case 's':
rseed = strtoul(optarg, NULL, 0);
break;
default:
errx(1, "usage: %s [-b bsize] [-c control-dir] [-n count] [-pq] [-s randseed]", prog);
}
}
srandom(rseed);
hoge = (char *)malloc(bsize);
buf = (char *)malloc(bsize);
for(i = 0; i < bsize; i++)
hoge[i] = random() & 0xff;
if (usepid)
sprintf(buf, "test.%d", getpid());
else
sprintf(buf, "test");
unlink(buf);
testfd = open(buf, O_RDWR | O_CREAT, 0644);
if (testfd < 0) {
perror("open");
exit(TEST_ERROR);
}
if (usepid)
sprintf(buf, "%s/control.%d", ctldir, getpid());
else
sprintf(buf, "%s/control", ctldir);
unlink(buf);
controlfd = open(buf, O_RDWR | O_CREAT, 0644);
if (controlfd < 0) {
perror("open");
exit(CONTROL_ERROR);
}
for(i = 0, p = 0; i < n; i++) {
if (!quiet) {
p = (p + 1) % 4;
write(1, prop + 2 * p, 2);
}
j = random() % 3;
k = random() & 0x3fffff;
if (j != 2) {
if (lseek(testfd, k, SEEK_SET) < 0) {
perror("read");
exit(TEST_ERROR);
}
if (lseek(controlfd, k, SEEK_SET) < 0) {
perror("read");
exit(CONTROL_ERROR);
}
}
switch(j) {
case 0:
if (read(testfd, buf, bsize) < 0) {
perror("read");
exit(TEST_ERROR);
}
if (read(controlfd, buf, bsize) < 0) {
perror("read");
exit(CONTROL_ERROR);
}
break;
case 1:
if (write(testfd, hoge, bsize) < 0) {
perror("write");
exit(TEST_ERROR);
}
if (write(controlfd, hoge, bsize) < 0) {
perror("write");
exit(CONTROL_ERROR);
}
break;
case 2:
if (ftruncate(testfd, k) < 0) {
perror("write");
exit(TEST_ERROR);
}
if (ftruncate(controlfd, k) < 0) {
perror("write");
exit(CONTROL_ERROR);
}
break;
}
j = random() % (100 * 1000);
usleep(j);
}
exit(0);
}