summaryrefslogtreecommitdiff
path: root/minix/tests/tvnd.c
blob: f868800ba31b8a5ba4023f3b3a0acde583451c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* Tests for the VND driver API.  Part of testvnd.sh. */
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

/*
 * For now, just test the most dreaded case: the driver being told to use the
 * file descriptor used to configure the device.  Without appropriate checks,
 * this would cause a deadlock in VFS, since the corresponding filp object is
 * locked to perform the ioctl(2) call when VFS gets the copyfd(2) back-call.
 */
int
main(int argc, char **argv)
{
	struct vnd_ioctl vnd;
	int fd;

	if (argc != 2) {
		fprintf(stderr, "usage: %s /dev/vnd0\n", argv[0]);
		return EXIT_FAILURE;
	}

	if ((fd = open(argv[1], O_RDONLY)) < 0) {
		perror("open");
		return EXIT_FAILURE;
	}

	memset(&vnd, 0, sizeof(vnd));

	if (ioctl(fd, VNDIOCCLR, &vnd) < 0) {
		perror("ioctl(VNDIOCCLR)");
		return EXIT_FAILURE;
	}

	vnd.vnd_fildes = fd;

	if (ioctl(fd, VNDIOCSET, &vnd) >= 0) {
		fprintf(stderr, "ioctl(VNDIOCSET): unexpected success\n");
		return EXIT_FAILURE;
	}

	if (errno != EBADF) {
		perror("ioctl(VNDIOCSET)");
		return EXIT_FAILURE;
	}

	close(fd);

	return EXIT_SUCCESS;
}