selftests: media_tests: return Kselftest Skip code for skipped tests
[linux-2.6-block.git] / tools / testing / selftests / media_tests / media_device_test.c
CommitLineData
a4f0e38e
SK
1// SPDX-License-Identifier: GPL-2.0
2
9d22f6e1 3/*
e9c0d44f 4 * media_device_test.c - Media Controller Device ioctl loop Test
9d22f6e1
SK
5 *
6 * Copyright (c) 2016 Shuah Khan <shuahkh@osg.samsung.com>
7 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
8 *
9d22f6e1
SK
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:
6accd8e9 23 * sudo ./media_device_test -d /dev/mediaX
9d22f6e1
SK
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>
e9c0d44f 39#include <time.h>
9d22f6e1
SK
40#include <linux/media.h>
41
11381286
SKSO
42#include "../kselftest.h"
43
9d22f6e1
SK
44int main(int argc, char **argv)
45{
46 int opt;
47 char media_device[256];
e9c0d44f 48 int count;
9d22f6e1
SK
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
11381286
SKSO
71 if (getuid() != 0)
72 ksft_exit_skip("Please run the test as root - Exiting.\n");
9d22f6e1 73
e9c0d44f
SK
74 /* Generate random number of interations */
75 srand((unsigned int) time(NULL));
76 count = rand();
77
9d22f6e1
SK
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
16378efa 91 printf("Running test for %d iterations\n", count);
e9c0d44f
SK
92
93 while (count > 0) {
9d22f6e1
SK
94 ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
95 if (ret < 0)
96 printf("Media Device Info errno %s\n", strerror(errno));
36d3f7d8 97 else
e9c0d44f
SK
98 printf("Media device model %s driver %s - count %d\n",
99 mdi.model, mdi.driver, count);
9d22f6e1 100 sleep(10);
e9c0d44f 101 count--;
9d22f6e1
SK
102 }
103}