FDD-book/examples/part-02/ch08-working-with-device-files/userland/stress_probe.c
Abdelkader Boudih 127eb3863c refactor: adopt current style(9) license header order (copyright before SPDX)
Update all example sources, man pages, and chapter code blocks to the
current FreeBSD header convention: plain /* opening, copyright lines
first, SPDX-License-Identifier after them. Add missing headers to
ch21/ch22 example files and missing copyright lines in ch08/ch16/ch36.
Rewrite chapter 37 prose (en, pt_BR, es_ES, zh_CN) and the submission
checklist, which previously taught the /*- marker and SPDX-first order.
2026-08-26 02:32:55 +01:00

44 lines
955 B
C

/*
* Copyright (c) 2026 Edson Brandi
*
* SPDX-License-Identifier: BSD-2-Clause
*
* stress_probe: loop open() and close() on a device node many times.
* Used as a rough correctness and leak test for the per-open state
* path in Chapter 8.
*
* Usage:
* stress_probe [path] [iterations]
*
* Default: path=/dev/myfirst/0, iterations=1000.
*
* Compile:
* cc -Wall -Werror -o stress_probe stress_probe.c
*/
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
const char *path = (argc > 1) ? argv[1] : "/dev/myfirst/0";
int iters = (argc > 2) ? atoi(argv[2]) : 1000;
int i, fd;
if (iters < 1)
errx(1, "iterations must be >= 1");
for (i = 0; i < iters; i++) {
fd = open(path, O_RDWR);
if (fd < 0)
err(1, "open (iter %d)", i);
if (close(fd) != 0)
err(1, "close (iter %d)", i);
}
printf("%d iterations completed\n", iters);
return (0);
}