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.
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.
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/>.
19 #include "qemu/lockable.h"
21 typedef abi_long (*TargetFdDataFunc)(void *, size_t);
22 typedef abi_long (*TargetFdAddrFunc)(void *, abi_ulong, socklen_t);
23 typedef struct TargetFdTrans {
24 TargetFdDataFunc host_to_target_data;
25 TargetFdDataFunc target_to_host_data;
26 TargetFdAddrFunc target_to_host_addr;
29 extern TargetFdTrans **target_fd_trans;
30 extern QemuMutex target_fd_trans_lock;
32 extern unsigned int target_fd_max;
34 static inline void fd_trans_init(void)
36 qemu_mutex_init(&target_fd_trans_lock);
39 static inline TargetFdDataFunc fd_trans_target_to_host_data(int fd)
45 QEMU_LOCK_GUARD(&target_fd_trans_lock);
46 if (fd < target_fd_max && target_fd_trans[fd]) {
47 return target_fd_trans[fd]->target_to_host_data;
52 static inline TargetFdDataFunc fd_trans_host_to_target_data(int fd)
58 QEMU_LOCK_GUARD(&target_fd_trans_lock);
59 if (fd < target_fd_max && target_fd_trans[fd]) {
60 return target_fd_trans[fd]->host_to_target_data;
65 static inline TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
71 QEMU_LOCK_GUARD(&target_fd_trans_lock);
72 if (fd < target_fd_max && target_fd_trans[fd]) {
73 return target_fd_trans[fd]->target_to_host_addr;
78 static inline void internal_fd_trans_register_unsafe(int fd,
83 if (fd >= target_fd_max) {
84 oldmax = target_fd_max;
85 target_fd_max = ((fd >> 6) + 1) << 6; /* by slice of 64 entries */
86 target_fd_trans = g_renew(TargetFdTrans *,
87 target_fd_trans, target_fd_max);
88 memset((void *)(target_fd_trans + oldmax), 0,
89 (target_fd_max - oldmax) * sizeof(TargetFdTrans *));
91 target_fd_trans[fd] = trans;
94 static inline void fd_trans_register(int fd, TargetFdTrans *trans)
96 QEMU_LOCK_GUARD(&target_fd_trans_lock);
97 internal_fd_trans_register_unsafe(fd, trans);
100 static inline void internal_fd_trans_unregister_unsafe(int fd)
102 if (fd >= 0 && fd < target_fd_max) {
103 target_fd_trans[fd] = NULL;
107 static inline void fd_trans_unregister(int fd)
113 QEMU_LOCK_GUARD(&target_fd_trans_lock);
114 internal_fd_trans_unregister_unsafe(fd);
117 static inline void fd_trans_dup(int oldfd, int newfd)
119 QEMU_LOCK_GUARD(&target_fd_trans_lock);
120 internal_fd_trans_unregister_unsafe(newfd);
121 if (oldfd < target_fd_max && target_fd_trans[oldfd]) {
122 internal_fd_trans_register_unsafe(newfd, target_fd_trans[oldfd]);
126 extern TargetFdTrans target_packet_trans;
127 #ifdef CONFIG_RTNETLINK
128 extern TargetFdTrans target_netlink_route_trans;
130 extern TargetFdTrans target_netlink_audit_trans;
131 extern TargetFdTrans target_signalfd_trans;
132 extern TargetFdTrans target_eventfd_trans;
133 extern TargetFdTrans target_timerfd_trans;
134 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
135 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
136 defined(__NR_inotify_init1))
137 extern TargetFdTrans target_inotify_trans;