Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ioctlsort.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2001 Wichert Akkerman <wichert@cistron.nl>
  3. * Copyright (c) 2004-2015 Dmitry V. Levin <ldv@altlinux.org>
  4. * Copyright (c) 1999-2018 The strace developers.
  5. * All rights reserved.
  6. *
  7. * SPDX-License-Identifier: LGPL-2.1-or-later
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. # include "config.h"
  11. #endif
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "ioctl_iocdef.h"
  16. struct ioctlent {
  17. const char *info;
  18. const char *name;
  19. unsigned int dir;
  20. unsigned int type_nr;
  21. unsigned int size;
  22. };
  23. static int
  24. is_prefix(const char *s1, const char *s2)
  25. {
  26. size_t len = strlen(s1);
  27. if (len > strlen(s2))
  28. return 0;
  29. return !memcmp(s1, s2, len);
  30. }
  31. static int
  32. compare_name_info(const void *a, const void *b)
  33. {
  34. int rc;
  35. const char *name1 = ((struct ioctlent *) a)->name;
  36. const char *name2 = ((struct ioctlent *) b)->name;
  37. const char *info1 = ((struct ioctlent *) a)->info;
  38. const char *info2 = ((struct ioctlent *) b)->info;
  39. rc = strcmp(name1, name2);
  40. if (rc)
  41. return rc;
  42. /*
  43. * exception from lexicographical order:
  44. * "asm/" < "asm-generic/"
  45. */
  46. if (is_prefix("asm/", info1) &&
  47. is_prefix("asm-generic/", info2))
  48. return -1;
  49. if (is_prefix("asm/", info2) &&
  50. is_prefix("asm-generic/", info1))
  51. return 1;
  52. return strcmp(info1, info2);
  53. }
  54. static unsigned int
  55. code(const struct ioctlent *e)
  56. {
  57. return e->type_nr |
  58. (e->size << _IOC_SIZESHIFT) |
  59. (e->dir << _IOC_DIRSHIFT);
  60. }
  61. static int
  62. compare_code_name(const void *a, const void *b)
  63. {
  64. unsigned int code1 = code((struct ioctlent *) a);
  65. unsigned int code2 = code((struct ioctlent *) b);
  66. const char *name1 = ((struct ioctlent *) a)->name;
  67. const char *name2 = ((struct ioctlent *) b)->name;
  68. return (code1 > code2) ?
  69. 1 : (code1 < code2) ? -1 : strcmp(name1, name2);
  70. }
  71. static void
  72. ioctlsort(struct ioctlent *ioctls, size_t nioctls)
  73. {
  74. size_t i;
  75. qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_name_info);
  76. for (i = 1; i < nioctls; ++i)
  77. if (!strcmp(ioctls[i-1].name, ioctls[i].name)) {
  78. /*
  79. * If there are multiple definitions for the same
  80. * name, keep the first one and mark all the rest
  81. * for deletion.
  82. */
  83. ioctls[i].info = NULL;
  84. }
  85. for (i = 1; i < nioctls; ++i)
  86. if (!ioctls[i].info) {
  87. /*
  88. * Change ioctl code of marked elements
  89. * to make them sorted to the end of array.
  90. */
  91. ioctls[i].dir =
  92. ioctls[i].type_nr =
  93. ioctls[i].size = 0xffffffffu;
  94. }
  95. qsort(ioctls, nioctls, sizeof(ioctls[0]), compare_code_name);
  96. puts("/* Generated by ioctlsort. */");
  97. for (i = 0; i < nioctls; ++i) {
  98. if (!ioctls[i].info) {
  99. /*
  100. * We've reached the first element marked for deletion.
  101. */
  102. break;
  103. }
  104. if (i == 0 || code(&ioctls[i-1]) != code(&ioctls[i]) ||
  105. !is_prefix(ioctls[i-1].name, ioctls[i].name))
  106. printf("{ \"%s\", %#010x },\n",
  107. ioctls[i].name, code(ioctls+i));
  108. }
  109. }
  110. static struct ioctlent ioctls[] = {
  111. #ifdef IOCTLSORT_INC
  112. # include IOCTLSORT_INC
  113. #else
  114. # include "ioctls_arch.h"
  115. # include "ioctls_inc.h"
  116. #endif
  117. };
  118. int
  119. main(void)
  120. {
  121. ioctlsort(ioctls, sizeof(ioctls) / sizeof(ioctls[0]));
  122. return 0;
  123. }