1 /* Serial interface for a pipe to a separate program
2 Copyright 1999 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
26 #ifdef HAVE_SYS_WAIT_H
29 #include <sys/socket.h>
34 #include "gdb_string.h"
36 extern int (*ui_loop_hook) PARAMS ((int));
38 static int pipe_open PARAMS ((serial_t scb, const char *name));
39 static void pipe_raw PARAMS ((serial_t scb));
40 static int wait_for PARAMS ((serial_t scb, int timeout));
41 static int pipe_readchar PARAMS ((serial_t scb, int timeout));
42 static int pipe_setbaudrate PARAMS ((serial_t scb, int rate));
43 static int pipe_setstopbits PARAMS ((serial_t scb, int num));
44 static int pipe_write PARAMS ((serial_t scb, const char *str, int len));
45 /* FIXME: static void pipe_restore PARAMS ((serial_t scb)); */
46 static void pipe_close PARAMS ((serial_t scb));
47 static serial_ttystate pipe_get_tty_state PARAMS ((serial_t scb));
48 static int pipe_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
49 static int pipe_return_0 PARAMS ((serial_t));
50 static int pipe_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
52 static void pipe_print_tty_state PARAMS ((serial_t, serial_ttystate));
54 extern void _initialize_ser_pipe PARAMS ((void));
57 #define XMALLOC(T) ((T*) xmalloc (sizeof (T)))
65 /* Open up a raw pipe */
72 #if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL) || !HAVE_SOCKETPAIR
75 struct pipe_state *state;
77 /* Copyright (c) 1988, 1993
78 * The Regents of the University of California. All rights reserved.
80 * This code is derived from software written by Ken Arnold and
81 * published in UNIX Review, Vol. 6, No. 8.
85 if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
101 /* re-wire pdes[1] to stdin/stdout */
103 if (pdes[1] != STDOUT_FILENO)
105 dup2 (pdes[1], STDOUT_FILENO);
108 dup2 (STDOUT_FILENO, STDIN_FILENO);
110 /* close any stray FD's - FIXME - how? */
111 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
112 from previous popen() calls that remain open in the
113 parent process are closed in the new child process. */
114 for (old = pidlist; old; old = old->next)
115 close (fileno (old->fp)); /* don't allow a flush */
117 execl ("/bin/sh", "sh", "-c", name + 1, NULL);
124 state = XMALLOC (struct pipe_state);
129 /* Make it non-blocking */
131 int flags = fcntl (scb->fd, F_GETFL, 0);
132 if (fcntl (scb->fd, F_SETFL, flags | O_NONBLOCK) < 0)
140 /* If we don't do this, GDB simply exits when the remote side dies. */
141 signal (SIGPIPE, SIG_IGN);
146 static serial_ttystate
147 pipe_get_tty_state (scb)
151 return xmalloc (sizeof (int));
155 pipe_set_tty_state (scb, ttystate)
157 serial_ttystate ttystate;
173 return; /* Always in raw mode */
176 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
177 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
179 For termio{s}, we actually just setup VTIME if necessary, and let the
180 timeout occur in the read() in pipe_read().
184 wait_for (scb, timeout)
190 fd_set readfds, exceptfds;
193 FD_ZERO (&exceptfds);
198 FD_SET (scb->fd, &readfds);
199 FD_SET (scb->fd, &exceptfds);
204 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
206 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
211 return SERIAL_TIMEOUT;
212 else if (errno == EINTR)
215 return SERIAL_ERROR; /* Got an error from select or poll */
222 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
223 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
224 char if successful. Returns -2 if timeout expired, EOF if line dropped
225 dead, or -3 for any other error (see errno in that case). */
228 pipe_readchar (scb, timeout)
235 if (scb->bufcnt-- > 0)
238 /* We have to be able to keep the GUI alive here, so we break the original
239 timeout into steps of 1 second, running the "keep the GUI alive" hook
240 each time through the loop.
242 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
243 will only go through the loop once. */
245 delta = (timeout == 0 ? 0 : 1);
249 /* N.B. The UI may destroy our world (for instance by calling
250 remote_stop,) in which case we want to get out of here as
251 quickly as possible. It is not safe to touch scb, since
252 someone else might have freed it. The ui_loop_hook signals that
253 we should exit by returning 1. */
257 if (ui_loop_hook (0))
258 return SERIAL_TIMEOUT;
261 status = wait_for (scb, delta);
264 /* If we got a character or an error back from wait_for, then we can
265 break from the loop before the timeout is completed. */
267 if (status != SERIAL_TIMEOUT)
272 /* If we have exhausted the original timeout, then generate
273 a SERIAL_TIMEOUT, and pass it out of the loop. */
275 else if (timeout == 0)
277 status == SERIAL_TIMEOUT;
287 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
288 if (scb->bufcnt != -1 || errno != EINTR)
292 if (scb->bufcnt <= 0)
294 if (scb->bufcnt == 0)
295 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
296 distinguish between EOF & timeouts
299 return SERIAL_ERROR; /* Got an error from read */
303 scb->bufp = scb->buf;
308 pipe_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
310 serial_ttystate new_ttystate;
311 serial_ttystate old_ttystate;
317 pipe_print_tty_state (scb, ttystate)
319 serial_ttystate ttystate;
321 /* Nothing to print. */
326 pipe_setbaudrate (scb, rate)
330 return 0; /* Never fails! */
334 pipe_setstopbits (scb, num)
338 return 0; /* Never fails! */
342 pipe_write (scb, str, len)
351 cc = write (scb->fd, str, len);
365 struct pipe_state *state = scb->state;
368 int pid = state->pid;
374 /* Might be useful to check that the child does die. */
378 static struct serial_ops pipe_ops =
386 pipe_return_0, /* flush output */
387 pipe_return_0, /* flush input */
388 pipe_return_0, /* send break */
392 pipe_print_tty_state,
393 pipe_noflush_set_tty_state,
396 pipe_return_0, /* wait for output to drain */
400 _initialize_ser_pipe ()
402 serial_add_interface (&pipe_ops);