FDD-book/examples/part-07/ch31-security/lab05-ratelimit/test/flood.c
Edson Brandi 6e6dac94fe Add book companion files under examples/
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
2026-04-25 16:51:00 +00:00

40 lines
928 B
C

/*
* flood.c - companion test program for Chapter 31 Lab 5.
*
* Opens /dev/secdev and issues a million unknown ioctls as fast as
* possible. Without rate limiting this floods dmesg; with the fix
* applied, the kernel should log at most about five messages per
* second regardless of how fast the calls arrive.
*
* Must be run as root because the driver refuses unprivileged opens.
*
* Build: cc -Wall -O2 -o flood flood.c
* Usage: ./flood [iterations]
*/
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int fd;
long iters = 1000000L;
if (argc >= 2)
iters = atol(argv[1]);
fd = open("/dev/secdev", O_RDWR);
if (fd < 0) {
perror("open /dev/secdev");
return (1);
}
for (long i = 0; i < iters; i++)
(void)ioctl(fd, 0xdeadbeefUL, NULL);
close(fd);
printf("issued %ld unknown ioctls\n", iters);
return (0);
}