DragonFlyBSD Kernel Audit
DF-2996 / t2.c
← back to finding ↓ download raw
/*
 * t2.c - isolation probes for DF-2996 debugging.
 *   t2 create : mount-root flow WITHOUT any unlink (mkdir + open(O_CREAT) + close)
 *   t2 unlink : same + unlink while fd open
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/stat.h>

static void
hang(int sig __attribute__((unused)))
{
	printf("probe: HUNG\n");
	fflush(stdout);
	_exit(99);
}

int
main(int argc, char **argv)
{
	struct stat st;
	int fd;
	int dounlink = argc > 1 && strcmp(argv[1], "unlink") == 0;

	signal(SIGALRM, hang);
	fd = open("/mnt/d/f", O_CREAT | O_RDWR, 0666);
	if (fd < 0) {
		printf("t2: open failed: %s\n", strerror(errno));
		return 1;
	}
	printf("t2: fd=%d\n", fd);
	fflush(stdout);
	if (dounlink) {
		if (unlink("/mnt/d/f") < 0) {
			printf("t2: unlink failed: %d %s\n", errno,
			       strerror(errno));
			return 1;
		}
		printf("t2: unlink ok\n");
	}
	close(fd);
	printf("t2: closed; stat probe...\n");
	fflush(stdout);
	alarm(5);
	if (stat("/mnt", &st) < 0)
		printf("t2: stat(/mnt): %s\n", strerror(errno));
	else
		printf("t2: stat(/mnt) ok\n");
	printf("t2: exiting (reclaim happens here)\n");
	fflush(stdout);
	return 0;
}