⬢ DragonFlyBSD Kernel Audit
DF-3161 / fifo_hold.c
← back to finding ↓ download raw
/*
 * DF-3161 helper: open a fifo O_RDWR (never blocks; fi_readers=1,
 * fi_writers=1) and hold it open until SIGTERM/SIGINT.
 * Used by umount_fifoleak.sh to keep a live fip across `umount -f`.
 */
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static volatile sig_atomic_t stop;

static void
onsig(int sig)
{
	stop = 1;
}

int
main(int argc, char **argv)
{
	int fd;

	if (argc != 2) {
		fprintf(stderr, "usage: %s <fifo-path>\n", argv[0]);
		exit(2);
	}
	signal(SIGTERM, onsig);
	signal(SIGINT, onsig);
	fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		perror("open(O_RDWR)");
		exit(1);
	}
	printf("HOLDING %s fd=%d\n", argv[1], fd);
	fflush(stdout);
	while (!stop)
		pause();
	close(fd);
	printf("RELEASED\n");
	fflush(stdout);
	return (0);
}