media_device_test.c 2.5 KB

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