xref: /linux/tools/testing/selftests/media_tests/media_device_test.c (revision 3eb66e91a25497065c5322b1268cbc3953642227)
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 #include "../kselftest.h"
43 
main(int argc,char ** argv)44 int main(int argc, char **argv)
45 {
46 	int opt;
47 	char media_device[256];
48 	int count;
49 	struct media_device_info mdi;
50 	int ret;
51 	int fd;
52 
53 	if (argc < 2) {
54 		printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
55 		exit(-1);
56 	}
57 
58 	/* Process arguments */
59 	while ((opt = getopt(argc, argv, "d:")) != -1) {
60 		switch (opt) {
61 		case 'd':
62 			strncpy(media_device, optarg, sizeof(media_device) - 1);
63 			media_device[sizeof(media_device)-1] = '\0';
64 			break;
65 		default:
66 			printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
67 			exit(-1);
68 		}
69 	}
70 
71 	if (getuid() != 0)
72 		ksft_exit_skip("Please run the test as root - Exiting.\n");
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 iterations\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