Merge tag 'pull-request-2022-07-07' of https://gitlab.com/thuth/qemu into staging
[qemu/rayw.git] / linux-user / fd-trans.h
blob1b9fa2041c06a3fe73bddf49d6a2cd2c61d2ba54
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.
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/>.
16 #ifndef FD_TRANS_H
17 #define FD_TRANS_H
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;
27 } TargetFdTrans;
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)
41 if (fd < 0) {
42 return NULL;
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;
49 return NULL;
52 static inline TargetFdDataFunc fd_trans_host_to_target_data(int fd)
54 if (fd < 0) {
55 return NULL;
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;
62 return NULL;
65 static inline TargetFdAddrFunc fd_trans_target_to_host_addr(int fd)
67 if (fd < 0) {
68 return NULL;
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;
75 return NULL;
78 static inline void internal_fd_trans_register_unsafe(int fd,
79 TargetFdTrans *trans)
81 unsigned int oldmax;
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)
109 if (fd < 0) {
110 return;
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;
129 #endif
130 extern TargetFdTrans target_netlink_audit_trans;
131 extern TargetFdTrans target_signalfd_trans;
132 extern TargetFdTrans target_eventfd_trans;
133 #if (defined(TARGET_NR_inotify_init) && defined(__NR_inotify_init)) || \
134 (defined(CONFIG_INOTIFY1) && defined(TARGET_NR_inotify_init1) && \
135 defined(__NR_inotify_init1))
136 extern TargetFdTrans target_inotify_trans;
137 #endif
138 #endif