mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Organize companion code and materials by book part: part-01/ Foundations part-02/ Kernel & driver fundamentals part-03/ Character & block devices part-04/ Networking & storage part-05/ Buses & advanced topics part-06/ Debugging & performance part-07/ Real-world drivers appendices/ Supplementary material README.md Overview and build instructions
71 lines
1.4 KiB
C
71 lines
1.4 KiB
C
/*
|
|
* bugdemo_test - Challenge 1 (silent-bug) companion.
|
|
*
|
|
* Drives the driver through INCREMENT and READ ioctls. On a
|
|
* single thread the bug is hard to observe; it shows up under
|
|
* concurrency. Try running several copies of this program in
|
|
* parallel and compare the reported totals against the number
|
|
* of increments.
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <err.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "bugdemo.h"
|
|
|
|
#define DEVPATH "/dev/bugdemo"
|
|
|
|
static void
|
|
usage(const char *prog)
|
|
{
|
|
fprintf(stderr,
|
|
"usage: %s increment [N]\n"
|
|
" %s read\n",
|
|
prog, prog);
|
|
exit(2);
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
struct bugdemo_command cmd;
|
|
int fd, n = 1;
|
|
|
|
if (argc < 2)
|
|
usage(argv[0]);
|
|
|
|
fd = open(DEVPATH, O_RDWR);
|
|
if (fd < 0)
|
|
err(1, "open %s", DEVPATH);
|
|
|
|
memset(&cmd, 0, sizeof(cmd));
|
|
|
|
if (strcmp(argv[1], "increment") == 0) {
|
|
if (argc == 3)
|
|
n = atoi(argv[2]);
|
|
cmd.op = BUGDEMO_OP_INCREMENT;
|
|
for (int i = 0; i < n; i++) {
|
|
if (ioctl(fd, BUGDEMO_TRIGGER, &cmd) != 0)
|
|
err(1, "ioctl increment");
|
|
}
|
|
printf("issued %d increments\n", n);
|
|
} else if (strcmp(argv[1], "read") == 0) {
|
|
cmd.op = BUGDEMO_OP_READ;
|
|
if (ioctl(fd, BUGDEMO_TRIGGER, &cmd) != 0)
|
|
err(1, "ioctl read");
|
|
printf("counter = %llu\n",
|
|
(unsigned long long)cmd.arg);
|
|
} else {
|
|
usage(argv[0]);
|
|
}
|
|
|
|
close(fd);
|
|
return (0);
|
|
}
|