timestress.c 1.95 KB
#include "testlib.c"

char pname[256];
char fname[256];
char *myname;
char *dname = "/dev/usb/bbrdb0";

void
handler(int sig)
{	
	printf("got signal %d\n", sig);
	signal(sig, handler);
}

int
main(int argc, char **argv)
{
	BBCHandle h;
	int loops = 0;
	int cardsize, freeblks, reservedblks, badblks;
	time_t curtime;
	struct stat sb;
	char *buf;
	int len;
	int fd;
	int rv;
	int i = 0;

	myname = argv[0];
	strncpy(pname, myname, sizeof(pname));
	if (argc > 1)
		dname = argv[argc - 1];

	printf("%s: stress test BBCSetTime and file operations\n", pname);
	h = BBCInit(dname, BBC_SYNC);
	printf("%s: BBCInit(%s) returns %d\n", pname, dname, h);
	if (h < 0) exit(1);
	while (!BBCCardPresent(h)) {
		if ((++i & 0x1ff) == 0)
			printf("%s: no card present ... wait\n", pname);
	}
	printf("%s: card is present\n", pname);
	if ((rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks)) < 0) {
		printf("%s: BBCStats returns %d\n", pname, rv);
		exit(1);
	}
	printf("%s: BBCStats cardsize %d, freeblks %d\n", pname, cardsize, freeblks);

	/*
	 * Create one block file
	 */
	sprintf(fname, "tstest.%d", getpid());
	mkfile(fname, BLK_SIZE);

	if ((fd = open(fname, O_RDONLY)) < 0) {
		perror(fname);
		exit(1);
	}
	if (fstat(fd, &sb) < 0) {
		perror(fname);
		exit(1);
	}
	len = sb.st_size;
	if ((buf = malloc(len)) == NULL) {
		perror("malloc");
		exit(1);
	}
	if ((rv = read(fd, buf, len)) < len) {
		fprintf(stderr, "read %s len %d returns %d\n", fname, len, rv);
		exit(1);
	}
	close(fd);
	unlink(fname);

	/* Test receipt of signals during nanosleep in BBCSetTime */
	signal(SIGUSR1, handler);

	while (1) {
		loops++;
		curtime = time(NULL);
		if ((rv = BBCSetTime(h, curtime)) < 0) {
			printf("%s: BBCSetTime returns %d (loops %d)\n", pname, rv, loops);
			exit(1);
		}
		if ((rv = BBCStoreUserData(h, buf, len)) < 0) {
			printf("%s: BBCStoreUserData fails: %d\n", pname, rv);
			exit(1);
		}
		if ((loops & 0xf) == 0)
			printf("%s: completed loops %d\n", pname, loops);
	}

	exit(0);
}