2
0

fd-trans.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. typedef abi_long (*TargetFdDataFunc)(void *, size_t);
  18. typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
  19. typedef struct TargetFdTrans {
  20. TargetFdDataFunc host_to_target_data;
  21. TargetFdDataFunc target_to_host_data;
  22. TargetFdAddrFunc target_to_host_addr;
  23. } TargetFdTrans;
  24. extern TargetFdTrans **target_fd_trans;
  25. extern unsigned int target_fd_max;
  26. static inline TargetFdDataFunc fd_trans_target_to_host_data(int fd)
  27. {
  28. if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
  29. return target_fd_trans[fd]->target_to_host_data;
  30. }
  31. return NULL;
  32. }
  33. static inline TargetFdDataFunc fd_trans_host_to_target_data(int fd)
  34. {
  35. if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
  36. return target_fd_trans[fd]->host_to_target_data;
  37. }
  38. return NULL;
  39. }
  40. static inline TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
  41. {
  42. if (fd >= 0 && fd < target_fd_max && target_fd_trans[fd]) {
  43. return target_fd_trans[fd]->target_to_host_addr;
  44. }
  45. return NULL;
  46. }
  47. static inline void fd_trans_register(int fd, TargetFdTrans *trans)
  48. {
  49. unsigned int oldmax;
  50. if (fd >= target_fd_max) {
  51. oldmax = target_fd_max;
  52. target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
  53. target_fd_trans = g_renew(TargetFdTrans *,
  54. target_fd_trans, target_fd_max);
  55. memset((void *)(target_fd_trans + oldmax), 0,
  56. (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
  57. }
  58. target_fd_trans[fd] = trans;
  59. }
  60. static inline void fd_trans_unregister(int fd)
  61. {
  62. if (fd >= 0 && fd < target_fd_max) {
  63. target_fd_trans[fd] = NULL;
  64. }
  65. }
  66. static inline void fd_trans_dup(int oldfd, int newfd)
  67. {
  68. fd_trans_unregister(newfd);
  69. if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
  70. fd_trans_register(newfd, target_fd_trans[oldfd]);
  71. }
  72. }
  73. extern TargetFdTrans target_packet_trans;
  74. #ifdef CONFIG_RTNETLINK
  75. extern TargetFdTrans target_netlink_route_trans;
  76. #endif
  77. extern TargetFdTrans target_netlink_audit_trans;
  78. extern TargetFdTrans target_signalfd_trans;
  79. extern TargetFdTrans target_eventfd_trans;
  80. #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
  81. (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
  82. defined(__NR_inotify_init1))
  83. extern TargetFdTrans target_inotify_trans;
  84. #endif
  85. #endif