Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * media_device_test.c - Media Controller Device ioctl loop Test
0005  *
0006  * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
0007  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
0008  *
0009  */
0010 
0011 /*
0012  * This file adds a test for Media Controller API.
0013  * This test should be run as root and should not be
0014  * included in the Kselftest run. This test should be
0015  * run when hardware and driver that makes use Media
0016  * Controller API are present in the system.
0017  *
0018  * This test opens user specified Media Device and calls
0019  * MEDIA_IOC_DEVICE_INFO ioctl in a loop once every 10
0020  * seconds.
0021  *
0022  * Usage:
0023  *  sudo ./media_device_test -d /dev/mediaX
0024  *
0025  *  While test is running, remove the device and
0026  *  ensure there are no use after free errors and
0027  *  other Oops in the dmesg. Enable KaSan kernel
0028  *  config option for use-after-free error detection.
0029 */
0030 
0031 #include <stdio.h>
0032 #include <unistd.h>
0033 #include <stdlib.h>
0034 #include <errno.h>
0035 #include <string.h>
0036 #include <fcntl.h>
0037 #include <sys/ioctl.h>
0038 #include <sys/stat.h>
0039 #include <time.h>
0040 #include <linux/media.h>
0041 
0042 #include "../kselftest.h"
0043 
0044 int main(int argc, char **argv)
0045 {
0046     int opt;
0047     char media_device[256];
0048     int count;
0049     struct media_device_info mdi;
0050     int ret;
0051     int fd;
0052 
0053     if (argc < 2) {
0054         printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
0055         exit(-1);
0056     }
0057 
0058     /* Process arguments */
0059     while ((opt = getopt(argc, argv, "d:")) != -1) {
0060         switch (opt) {
0061         case 'd':
0062             strncpy(media_device, optarg, sizeof(media_device) - 1);
0063             media_device[sizeof(media_device)-1] = '\0';
0064             break;
0065         default:
0066             printf("Usage: %s [-d </dev/mediaX>]\n", argv[0]);
0067             exit(-1);
0068         }
0069     }
0070 
0071     if (getuid() != 0)
0072         ksft_exit_skip("Please run the test as root - Exiting.\n");
0073 
0074     /* Generate random number of interations */
0075     srand((unsigned int) time(NULL));
0076     count = rand();
0077 
0078     /* Open Media device and keep it open */
0079     fd = open(media_device, O_RDWR);
0080     if (fd == -1) {
0081         printf("Media Device open errno %s\n", strerror(errno));
0082         exit(-1);
0083     }
0084 
0085     printf("\nNote:\n"
0086            "While test is running, remove the device and\n"
0087            "ensure there are no use after free errors and\n"
0088            "other Oops in the dmesg. Enable KaSan kernel\n"
0089            "config option for use-after-free error detection.\n\n");
0090 
0091     printf("Running test for %d iterations\n", count);
0092 
0093     while (count > 0) {
0094         ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
0095         if (ret < 0)
0096             printf("Media Device Info errno %s\n", strerror(errno));
0097         else
0098             printf("Media device model %s driver %s - count %d\n",
0099                 mdi.model, mdi.driver, count);
0100         sleep(10);
0101         count--;
0102     }
0103 }