slirp: Move g_spawn_async_with_fds_qemu compatibility to slirp/
[qemu/ar7.git] / slirp / misc.c
blobee77aff337ef62c5ce7cd3335542a9c31f04626d
1 /*
2 * Copyright (c) 1995 Danny Gasparovski.
4 * Please read the file COPYRIGHT for the
5 * terms and conditions of the copyright.
6 */
8 #include "qemu/osdep.h"
9 #include "slirp.h"
10 #include "libslirp.h"
12 inline void
13 insque(void *a, void *b)
15 register struct quehead *element = (struct quehead *) a;
16 register struct quehead *head = (struct quehead *) b;
17 element->qh_link = head->qh_link;
18 head->qh_link = (struct quehead *)element;
19 element->qh_rlink = (struct quehead *)head;
20 ((struct quehead *)(element->qh_link))->qh_rlink
21 = (struct quehead *)element;
24 inline void
25 remque(void *a)
27 register struct quehead *element = (struct quehead *) a;
28 ((struct quehead *)(element->qh_link))->qh_rlink = element->qh_rlink;
29 ((struct quehead *)(element->qh_rlink))->qh_link = element->qh_link;
30 element->qh_rlink = NULL;
33 struct gfwd_list *
34 add_guestfwd(struct gfwd_list **ex_ptr,
35 SlirpWriteCb write_cb, void *opaque,
36 struct in_addr addr, int port)
38 struct gfwd_list *f = g_new0(struct gfwd_list, 1);
40 f->write_cb = write_cb;
41 f->opaque = opaque;
42 f->ex_fport = port;
43 f->ex_addr = addr;
44 f->ex_next = *ex_ptr;
45 *ex_ptr = f;
47 return f;
50 struct gfwd_list *
51 add_exec(struct gfwd_list **ex_ptr, const char *cmdline,
52 struct in_addr addr, int port)
54 struct gfwd_list *f = add_guestfwd(ex_ptr, NULL, NULL, addr, port);
56 f->ex_exec = g_strdup(cmdline);
58 return f;
61 static int
62 slirp_socketpair_with_oob(int sv[2])
64 struct sockaddr_in addr = {
65 .sin_family = AF_INET,
66 .sin_port = 0,
67 .sin_addr.s_addr = INADDR_ANY,
69 socklen_t addrlen = sizeof(addr);
70 int ret, s;
72 sv[1] = -1;
73 s = slirp_socket(AF_INET, SOCK_STREAM, 0);
74 if (s < 0 || bind(s, (struct sockaddr *)&addr, addrlen) < 0 ||
75 listen(s, 1) < 0 ||
76 getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0) {
77 goto err;
80 sv[1] = slirp_socket(AF_INET, SOCK_STREAM, 0);
81 if (sv[1] < 0) {
82 goto err;
85 * This connect won't block because we've already listen()ed on
86 * the server end (even though we won't accept() the connection
87 * until later on).
89 do {
90 ret = connect(sv[1], (struct sockaddr *)&addr, addrlen);
91 } while (ret < 0 && errno == EINTR);
92 if (ret < 0) {
93 goto err;
96 do {
97 sv[0] = accept(s, (struct sockaddr *)&addr, &addrlen);
98 } while (sv[0] < 0 && errno == EINTR);
99 if (sv[0] < 0) {
100 goto err;
103 slirp_closesocket(s);
104 return 0;
106 err:
107 g_critical("slirp_socketpair(): %s", strerror(errno));
108 if (s >= 0) {
109 slirp_closesocket(s);
111 if (sv[1] >= 0) {
112 slirp_closesocket(sv[1]);
114 return -1;
117 static void
118 fork_exec_child_setup(gpointer data)
120 #ifndef _WIN32
121 setsid();
122 #endif
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
128 #if !GLIB_CHECK_VERSION(2, 58, 0)
129 typedef struct SlirpGSpawnFds {
130 GSpawnChildSetupFunc child_setup;
131 gpointer user_data;
132 gint stdin_fd;
133 gint stdout_fd;
134 gint stderr_fd;
135 } SlirpGSpawnFds;
137 static inline void
138 slirp_gspawn_fds_setup(gpointer user_data)
140 SlirpGSpawnFds *q = (SlirpGSpawnFds *)user_data;
142 dup2(q->stdin_fd, 0);
143 dup2(q->stdout_fd, 1);
144 dup2(q->stderr_fd, 2);
145 q->child_setup(q->user_data);
147 #endif
149 static inline gboolean
150 g_spawn_async_with_fds_slirp(const gchar *working_directory,
151 gchar **argv,
152 gchar **envp,
153 GSpawnFlags flags,
154 GSpawnChildSetupFunc child_setup,
155 gpointer user_data,
156 GPid *child_pid,
157 gint stdin_fd,
158 gint stdout_fd,
159 gint stderr_fd,
160 GError **error)
162 #if GLIB_CHECK_VERSION(2, 58, 0)
163 return g_spawn_async_with_fds(working_directory, argv, envp, flags,
164 child_setup, user_data,
165 child_pid, stdin_fd, stdout_fd, stderr_fd,
166 error);
167 #else
168 SlirpGSpawnFds setup = {
169 .child_setup = child_setup,
170 .user_data = user_data,
171 .stdin_fd = stdin_fd,
172 .stdout_fd = stdout_fd,
173 .stderr_fd = stderr_fd,
176 return g_spawn_async(working_directory, argv, envp, flags,
177 slirp_gspawn_fds_setup, &setup,
178 child_pid, error);
179 #endif
182 #define g_spawn_async_with_fds(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) \
183 g_spawn_async_with_fds_slirp(wd, argv, env, f, c, d, p, ifd, ofd, efd, err)
185 #pragma GCC diagnostic pop
188 fork_exec(struct socket *so, const char *ex)
190 GError *err = NULL;
191 char **argv;
192 int opt, sp[2];
194 DEBUG_CALL("fork_exec");
195 DEBUG_ARG("so = %p", so);
196 DEBUG_ARG("ex = %p", ex);
198 if (slirp_socketpair_with_oob(sp) < 0) {
199 return 0;
202 argv = g_strsplit(ex, " ", -1);
203 g_spawn_async_with_fds(NULL /* cwd */,
204 argv,
205 NULL /* env */,
206 G_SPAWN_SEARCH_PATH,
207 fork_exec_child_setup, NULL /* data */,
208 NULL /* child_pid */,
209 sp[1], sp[1], sp[1],
210 &err);
211 g_strfreev(argv);
213 if (err) {
214 g_critical("fork_exec: %s", err->message);
215 g_error_free(err);
216 slirp_closesocket(sp[0]);
217 slirp_closesocket(sp[1]);
218 return 0;
221 so->s = sp[0];
222 slirp_closesocket(sp[1]);
223 slirp_socket_set_fast_reuse(so->s);
224 opt = 1;
225 slirp_setsockopt(so->s, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(int));
226 slirp_set_nonblock(so->s);
227 so->slirp->cb->register_poll_fd(so->s);
228 return 1;
231 char *slirp_connection_info(Slirp *slirp)
233 GString *str = g_string_new(NULL);
234 const char * const tcpstates[] = {
235 [TCPS_CLOSED] = "CLOSED",
236 [TCPS_LISTEN] = "LISTEN",
237 [TCPS_SYN_SENT] = "SYN_SENT",
238 [TCPS_SYN_RECEIVED] = "SYN_RCVD",
239 [TCPS_ESTABLISHED] = "ESTABLISHED",
240 [TCPS_CLOSE_WAIT] = "CLOSE_WAIT",
241 [TCPS_FIN_WAIT_1] = "FIN_WAIT_1",
242 [TCPS_CLOSING] = "CLOSING",
243 [TCPS_LAST_ACK] = "LAST_ACK",
244 [TCPS_FIN_WAIT_2] = "FIN_WAIT_2",
245 [TCPS_TIME_WAIT] = "TIME_WAIT",
247 struct in_addr dst_addr;
248 struct sockaddr_in src;
249 socklen_t src_len;
250 uint16_t dst_port;
251 struct socket *so;
252 const char *state;
253 char buf[20];
255 g_string_append_printf(str,
256 " Protocol[State] FD Source Address Port "
257 "Dest. Address Port RecvQ SendQ\n");
259 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
260 if (so->so_state & SS_HOSTFWD) {
261 state = "HOST_FORWARD";
262 } else if (so->so_tcpcb) {
263 state = tcpstates[so->so_tcpcb->t_state];
264 } else {
265 state = "NONE";
267 if (so->so_state & (SS_HOSTFWD | SS_INCOMING)) {
268 src_len = sizeof(src);
269 getsockname(so->s, (struct sockaddr *)&src, &src_len);
270 dst_addr = so->so_laddr;
271 dst_port = so->so_lport;
272 } else {
273 src.sin_addr = so->so_laddr;
274 src.sin_port = so->so_lport;
275 dst_addr = so->so_faddr;
276 dst_port = so->so_fport;
278 snprintf(buf, sizeof(buf), " TCP[%s]", state);
279 g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s,
280 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
281 ntohs(src.sin_port));
282 g_string_append_printf(str, "%15s %5d %5d %5d\n",
283 inet_ntoa(dst_addr), ntohs(dst_port),
284 so->so_rcv.sb_cc, so->so_snd.sb_cc);
287 for (so = slirp->udb.so_next; so != &slirp->udb; so = so->so_next) {
288 if (so->so_state & SS_HOSTFWD) {
289 snprintf(buf, sizeof(buf), " UDP[HOST_FORWARD]");
290 src_len = sizeof(src);
291 getsockname(so->s, (struct sockaddr *)&src, &src_len);
292 dst_addr = so->so_laddr;
293 dst_port = so->so_lport;
294 } else {
295 snprintf(buf, sizeof(buf), " UDP[%d sec]",
296 (so->so_expire - curtime) / 1000);
297 src.sin_addr = so->so_laddr;
298 src.sin_port = so->so_lport;
299 dst_addr = so->so_faddr;
300 dst_port = so->so_fport;
302 g_string_append_printf(str, "%-19s %3d %15s %5d ", buf, so->s,
303 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*",
304 ntohs(src.sin_port));
305 g_string_append_printf(str, "%15s %5d %5d %5d\n",
306 inet_ntoa(dst_addr), ntohs(dst_port),
307 so->so_rcv.sb_cc, so->so_snd.sb_cc);
310 for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so->so_next) {
311 snprintf(buf, sizeof(buf), " ICMP[%d sec]",
312 (so->so_expire - curtime) / 1000);
313 src.sin_addr = so->so_laddr;
314 dst_addr = so->so_faddr;
315 g_string_append_printf(str, "%-19s %3d %15s - ", buf, so->s,
316 src.sin_addr.s_addr ? inet_ntoa(src.sin_addr) : "*");
317 g_string_append_printf(str, "%15s - %5d %5d\n", inet_ntoa(dst_addr),
318 so->so_rcv.sb_cc, so->so_snd.sb_cc);
321 return g_string_free(str, FALSE);