DragonFlyBSD Kernel Audit
DF-3048 / vntool.c
← back to finding ↓ download raw
/*
 * vntool.c - minimal vn(4) attach/detach (guest lacks vnconfig)
 *   vntool attach <unit> <file>   -> attaches file to /dev/vn<unit>
 *   vntool detach <unit>
 */
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <sys/vnioctl.h>

int
main(int argc, char **argv)
{
	struct vn_ioctl vio;
	char path[128];
	int fd, unit;

	if (argc < 3)
		errx(1, "usage: vntool attach|detach <unit> [file]");

	unit = atoi(argv[2]);
	snprintf(path, sizeof(path), "/dev/vn%d", unit);
	fd = open(path, O_RDWR);
	if (fd < 0)
		err(1, "open %s", path);

	memset(&vio, 0, sizeof(vio));
	if (strcmp(argv[1], "attach") == 0) {
		if (argc < 4)
			errx(1, "attach needs file");
		vio.vn_file = argv[3];
		if (ioctl(fd, VNIOCATTACH, &vio) < 0)
			err(1, "VNIOCATTACH");
		printf("attached %s size=%lld\n", argv[3],
		    (long long)vio.vn_size);
	} else if (strcmp(argv[1], "detach") == 0) {
		if (ioctl(fd, VNIOCDETACH, &vio) < 0)
			err(1, "VNIOCDETACH");
		printf("detached\n");
	} else {
		errx(1, "bad op");
	}
	return (0);
}