⬢ DragonFlyBSD Kernel Audit
DF-4958 / wr.c
← back to finding ↓ download raw
/*
 * DF-4958 userland driver: write(2) the given 2-byte spec string to
 * /dev/led/leaktest N times through a fresh open fd each time (exactly
 * what `echo sz > /dev/led/xxx` does on real LED hardware).
 *
 *   wr 20000 sz   -> each write leaks one sbuf (empty parsed spec)
 *   wr 20000 sA   -> control: valid spec, no net growth (steady-state swap)
 */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int
main(int argc, char **argv)
{
	long n, i;
	int fd;
	char spec[8];

	if (argc != 3) {
		fprintf(stderr, "usage: %s <n> <2-char-spec>\n", argv[0]);
		return (2);
	}
	n = atol(argv[1]);
	strlcpy(spec, argv[2], sizeof(spec));

	for (i = 0; i < n; i++) {
		fd = open("/dev/led/leaktest", O_WRONLY);
		if (fd < 0) {
			perror("open /dev/led/leaktest");
			return (1);
		}
		if (write(fd, spec, strlen(spec)) != (ssize_t)strlen(spec)) {
			perror("write");
			close(fd);
			return (1);
		}
		close(fd);
	}
	printf("wr: %ld writes of \"%s\" done\n", n, spec);
	return (0);
}