syscalltbl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * System call table mapper
  3. *
  4. * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include "syscalltbl.h"
  16. #include <stdlib.h>
  17. #ifdef HAVE_SYSCALL_TABLE
  18. #include <linux/compiler.h>
  19. #include <string.h>
  20. #include "util.h"
  21. #if defined(__x86_64__)
  22. #include <asm/syscalls_64.c>
  23. const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
  24. static const char **syscalltbl_native = syscalltbl_x86_64;
  25. #endif
  26. struct syscall {
  27. int id;
  28. const char *name;
  29. };
  30. static int syscallcmpname(const void *vkey, const void *ventry)
  31. {
  32. const char *key = vkey;
  33. const struct syscall *entry = ventry;
  34. return strcmp(key, entry->name);
  35. }
  36. static int syscallcmp(const void *va, const void *vb)
  37. {
  38. const struct syscall *a = va, *b = vb;
  39. return strcmp(a->name, b->name);
  40. }
  41. static int syscalltbl__init_native(struct syscalltbl *tbl)
  42. {
  43. int nr_entries = 0, i, j;
  44. struct syscall *entries;
  45. for (i = 0; i <= syscalltbl_native_max_id; ++i)
  46. if (syscalltbl_native[i])
  47. ++nr_entries;
  48. entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
  49. if (tbl->syscalls.entries == NULL)
  50. return -1;
  51. for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
  52. if (syscalltbl_native[i]) {
  53. entries[j].name = syscalltbl_native[i];
  54. entries[j].id = i;
  55. ++j;
  56. }
  57. }
  58. qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
  59. tbl->syscalls.nr_entries = nr_entries;
  60. return 0;
  61. }
  62. struct syscalltbl *syscalltbl__new(void)
  63. {
  64. struct syscalltbl *tbl = malloc(sizeof(*tbl));
  65. if (tbl) {
  66. if (syscalltbl__init_native(tbl)) {
  67. free(tbl);
  68. return NULL;
  69. }
  70. }
  71. return tbl;
  72. }
  73. void syscalltbl__delete(struct syscalltbl *tbl)
  74. {
  75. zfree(&tbl->syscalls.entries);
  76. free(tbl);
  77. }
  78. const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
  79. {
  80. return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
  81. }
  82. int syscalltbl__id(struct syscalltbl *tbl, const char *name)
  83. {
  84. struct syscall *sc = bsearch(name, tbl->syscalls.entries,
  85. tbl->syscalls.nr_entries, sizeof(*sc),
  86. syscallcmpname);
  87. return sc ? sc->id : -1;
  88. }
  89. #else /* HAVE_SYSCALL_TABLE */
  90. #include <libaudit.h>
  91. struct syscalltbl *syscalltbl__new(void)
  92. {
  93. struct syscalltbl *tbl = malloc(sizeof(*tbl));
  94. if (tbl)
  95. tbl->audit_machine = audit_detect_machine();
  96. return tbl;
  97. }
  98. void syscalltbl__delete(struct syscalltbl *tbl)
  99. {
  100. free(tbl);
  101. }
  102. const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
  103. {
  104. return audit_syscall_to_name(id, tbl->audit_machine);
  105. }
  106. int syscalltbl__id(struct syscalltbl *tbl, const char *name)
  107. {
  108. return audit_name_to_syscall(name, tbl->audit_machine);
  109. }
  110. #endif /* HAVE_SYSCALL_TABLE */