fd-trans.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef FD_TRANS_H
  16. #define FD_TRANS_H
  17. #include "qemu/lockable.h"
  18. typedef abi_long (*TargetFdDataFunc)(void *, size_t);
  19. typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
  20. typedef struct TargetFdTrans {
  21. TargetFdDataFunc host_to_target_data;
  22. TargetFdDataFunc target_to_host_data;
  23. TargetFdAddrFunc target_to_host_addr;
  24. } TargetFdTrans;
  25. extern TargetFdTrans **target_fd_trans;
  26. extern QemuMutex target_fd_trans_lock;
  27. extern unsigned int target_fd_max;
  28. static inline void fd_trans_init(void)
  29. {
  30. qemu_mutex_init(&target_fd_trans_lock);
  31. }
  32. static inline TargetFdDataFunc fd_trans_target_to_host_data(int fd)
  33. {
  34. if (fd < 0) {
  35. return NULL;
  36. }
  37. QEMU_LOCK_GUARD(&target_fd_trans_lock);
  38. if (fd < target_fd_max && target_fd_trans[fd]) {
  39. return target_fd_trans[fd]->target_to_host_data;
  40. }
  41. return NULL;
  42. }
  43. static inline TargetFdDataFunc fd_trans_host_to_target_data(int fd)
  44. {
  45. if (fd < 0) {
  46. return NULL;
  47. }
  48. QEMU_LOCK_GUARD(&target_fd_trans_lock);
  49. if (fd < target_fd_max && target_fd_trans[fd]) {
  50. return target_fd_trans[fd]->host_to_target_data;
  51. }
  52. return NULL;
  53. }
  54. static inline TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
  55. {
  56. if (fd < 0) {
  57. return NULL;
  58. }
  59. QEMU_LOCK_GUARD(&target_fd_trans_lock);
  60. if (fd < target_fd_max && target_fd_trans[fd]) {
  61. return target_fd_trans[fd]->target_to_host_addr;
  62. }
  63. return NULL;
  64. }
  65. static inline void internal_fd_trans_register_unsafe(int fd,
  66. TargetFdTrans *trans)
  67. {
  68. unsigned int oldmax;
  69. if (fd >= target_fd_max) {
  70. oldmax = target_fd_max;
  71. target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
  72. target_fd_trans = g_renew(TargetFdTrans *,
  73. target_fd_trans, target_fd_max);
  74. memset((void *)(target_fd_trans + oldmax), 0,
  75. (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
  76. }
  77. target_fd_trans[fd] = trans;
  78. }
  79. static inline void fd_trans_register(int fd, TargetFdTrans *trans)
  80. {
  81. QEMU_LOCK_GUARD(&target_fd_trans_lock);
  82. internal_fd_trans_register_unsafe(fd, trans);
  83. }
  84. static inline void internal_fd_trans_unregister_unsafe(int fd)
  85. {
  86. if (fd >= 0 && fd < target_fd_max) {
  87. target_fd_trans[fd] = NULL;
  88. }
  89. }
  90. static inline void fd_trans_unregister(int fd)
  91. {
  92. if (fd < 0) {
  93. return;
  94. }
  95. QEMU_LOCK_GUARD(&target_fd_trans_lock);
  96. internal_fd_trans_unregister_unsafe(fd);
  97. }
  98. static inline void fd_trans_dup(int oldfd, int newfd)
  99. {
  100. QEMU_LOCK_GUARD(&target_fd_trans_lock);
  101. internal_fd_trans_unregister_unsafe(newfd);
  102. if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
  103. internal_fd_trans_register_unsafe(newfd, target_fd_trans[oldfd]);
  104. }
  105. }
  106. extern TargetFdTrans target_packet_trans;
  107. #ifdef CONFIG_RTNETLINK
  108. extern TargetFdTrans target_netlink_route_trans;
  109. #endif
  110. extern TargetFdTrans target_netlink_audit_trans;
  111. extern TargetFdTrans target_signalfd_trans;
  112. extern TargetFdTrans target_eventfd_trans;
  113. extern TargetFdTrans target_timerfd_trans;
  114. #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
  115. (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
  116. defined(__NR_inotify_init1))
  117. extern TargetFdTrans target_inotify_trans;
  118. #endif
  119. #endif