1 // SPDX-License-Identifier: GPL-2.0
2 
3 /*
4  * media_device_test.c - Media Controller Device ioctl loop Test
5  *
6  * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
7  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
8  *
9  */
10 
11 /*
12  * This file adds a test for Media Controller API.
13  * This test should be run as root and should not be
14  * included in the Kselftest run. This test should be
15  * run when hardware and driver that makes use Media
16  * Controller API are present in the system.
17  *
18  * This test opens user specified Media Device and calls
19  * MEDIA_IOC_DEVICE_INFO ioctl in a loop once every 10
20  * seconds.
21  *
22  * Usage:
23  *	sudo ./media_device_test -d /dev/mediaX
24  *
25  *	While test is running, remove the device and
26  *	ensure there are no use after free errors and
27  *	other Oops in the dmesg. Enable KaSan kernel
28  *	config option for use-after-free error detection.
29 */
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <fcntl.h>
37 #include <sys/ioctl.h>
38 #include <sys/stat.h>
39 #include <time.h>
40 #include <linux/media.h>
41 
42 int main(int argc, char **argv)
43 {
44 	int opt;
45 	char media_device[256];
46 	int count;
47 	struct media_device_info mdi;
48 	int ret;
49 	int fd;
50 
51 	if (argc < 2) {
52 		printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
53 		exit(-1);
54 	}
55 
56 	/* Process arguments */
57 	while ((opt = getopt(argc, argv, "d:")) != -1) {
58 		switch (opt) {
59 		case 'd':
60 			strncpy(media_device, optarg, sizeof(media_device) - 1);
61 			media_device[sizeof(media_device)-1] = '\0';
62 			break;
63 		default:
64 			printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
65 			exit(-1);
66 		}
67 	}
68 
69 	if (getuid() != 0) {
70 		printf("Please run the test as root - Exiting.\n");
71 		exit(-1);
72 	}
73 
74 	/* Generate random number of interations */
75 	srand((unsigned int) time(NULL));
76 	count = rand();
77 
78 	/* Open Media device and keep it open */
79 	fd = open(media_device, O_RDWR);
80 	if (fd == -1) {
81 		printf("Media Device open errno %s\n", strerror(errno));
82 		exit(-1);
83 	}
84 
85 	printf("\nNote:\n"
86 	       "While test is running, remove the device and\n"
87 	       "ensure there are no use after free errors and\n"
88 	       "other Oops in the dmesg. Enable KaSan kernel\n"
89 	       "config option for use-after-free error detection.\n\n");
90 
91 	printf("Running test for %d iternations\n", count);
92 
93 	while (count > 0) {
94 		ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
95 		if (ret < 0)
96 			printf("Media Device Info errno %s\n", strerror(errno));
97 		else
98 			printf("Media device model %s driver %s - count %d\n",
99 				mdi.model, mdi.driver, count);
100 		sleep(10);
101 		count--;
102 	}
103 }
104