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>
37 #include "gdb_string.h"
39 extern int (*ui_loop_hook) PARAMS ((int));
41 static int pipe_open PARAMS ((serial_t scb, const char *name));
42 static void pipe_raw PARAMS ((serial_t scb));
43 static int wait_for PARAMS ((serial_t scb, int timeout));
44 static int pipe_readchar PARAMS ((serial_t scb, int timeout));
45 static int pipe_setbaudrate PARAMS ((serial_t scb, int rate));
46 static int pipe_setstopbits PARAMS ((serial_t scb, int num));
47 static int pipe_write PARAMS ((serial_t scb, const char *str, int len));
48 /* FIXME: static void pipe_restore PARAMS ((serial_t scb)); */
49 static void pipe_close PARAMS ((serial_t scb));
50 static serial_ttystate pipe_get_tty_state PARAMS ((serial_t scb));
51 static int pipe_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
52 static int pipe_return_0 PARAMS ((serial_t));
53 static int pipe_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
55 static void pipe_print_tty_state PARAMS ((serial_t, serial_ttystate));
57 extern void _initialize_ser_pipe PARAMS ((void));
60 #define XMALLOC(T) ((T*) xmalloc (sizeof (T)))
68 /* Open up a raw pipe */
75 #if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL) || !HAVE_SOCKETPAIR
78 struct pipe_state *state;
80 /* Copyright (c) 1988, 1993
81 * The Regents of the University of California. All rights reserved.
83 * This code is derived from software written by Ken Arnold and
84 * published in UNIX Review, Vol. 6, No. 8.
88 if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
104 /* re-wire pdes[1] to stdin/stdout */
106 if (pdes[1] != STDOUT_FILENO)
108 dup2 (pdes[1], STDOUT_FILENO);
111 dup2 (STDOUT_FILENO, STDIN_FILENO);
113 /* close any stray FD's - FIXME - how? */
114 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
115 from previous popen() calls that remain open in the
116 parent process are closed in the new child process. */
117 for (old = pidlist; old; old = old->next)
118 close (fileno (old->fp)); /* don't allow a flush */
120 execl ("/bin/sh", "sh", "-c", name + 1, NULL);
127 state = XMALLOC (struct pipe_state);
132 /* Make it non-blocking */
134 int flags = fcntl (scb->fd, F_GETFL, 0);
135 if (fcntl (scb->fd, F_SETFL, flags | O_NONBLOCK) < 0)
143 /* If we don't do this, GDB simply exits when the remote side dies. */
144 signal (SIGPIPE, SIG_IGN);
149 static serial_ttystate
150 pipe_get_tty_state (scb)
154 return xmalloc (sizeof (int));
158 pipe_set_tty_state (scb, ttystate)
160 serial_ttystate ttystate;
176 return; /* Always in raw mode */
179 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
180 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
182 For termio{s}, we actually just setup VTIME if necessary, and let the
183 timeout occur in the read() in pipe_read().
187 wait_for (scb, timeout)
193 fd_set readfds, exceptfds;
196 FD_ZERO (&exceptfds);
201 FD_SET (scb->fd, &readfds);
202 FD_SET (scb->fd, &exceptfds);
207 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
209 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
214 return SERIAL_TIMEOUT;
215 else if (errno == EINTR)
218 return SERIAL_ERROR; /* Got an error from select or poll */
225 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
226 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
227 char if successful. Returns -2 if timeout expired, EOF if line dropped
228 dead, or -3 for any other error (see errno in that case). */
231 pipe_readchar (scb, timeout)
238 if (scb->bufcnt-- > 0)
241 /* We have to be able to keep the GUI alive here, so we break the original
242 timeout into steps of 1 second, running the "keep the GUI alive" hook
243 each time through the loop.
245 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
246 will only go through the loop once. */
248 delta = (timeout == 0 ? 0 : 1);
252 /* N.B. The UI may destroy our world (for instance by calling
253 remote_stop,) in which case we want to get out of here as
254 quickly as possible. It is not safe to touch scb, since
255 someone else might have freed it. The ui_loop_hook signals that
256 we should exit by returning 1. */
260 if (ui_loop_hook (0))
261 return SERIAL_TIMEOUT;
264 status = wait_for (scb, delta);
267 /* If we got a character or an error back from wait_for, then we can
268 break from the loop before the timeout is completed. */
270 if (status != SERIAL_TIMEOUT)
275 /* If we have exhausted the original timeout, then generate
276 a SERIAL_TIMEOUT, and pass it out of the loop. */
278 else if (timeout == 0)
280 status == SERIAL_TIMEOUT;
290 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
291 if (scb->bufcnt != -1 || errno != EINTR)
295 if (scb->bufcnt <= 0)
297 if (scb->bufcnt == 0)
298 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
299 distinguish between EOF & timeouts
302 return SERIAL_ERROR; /* Got an error from read */
306 scb->bufp = scb->buf;
311 pipe_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
313 serial_ttystate new_ttystate;
314 serial_ttystate old_ttystate;
320 pipe_print_tty_state (scb, ttystate)
322 serial_ttystate ttystate;
324 /* Nothing to print. */
329 pipe_setbaudrate (scb, rate)
333 return 0; /* Never fails! */
337 pipe_setstopbits (scb, num)
341 return 0; /* Never fails! */
345 pipe_write (scb, str, len)
354 cc = write (scb->fd, str, len);
368 struct pipe_state *state = scb->state;
371 int pid = state->pid;
377 /* Might be useful to check that the child does die. */
381 static struct serial_ops pipe_ops =
389 pipe_return_0, /* flush output */
390 pipe_return_0, /* flush input */
391 pipe_return_0, /* send break */
395 pipe_print_tty_state,
396 pipe_noflush_set_tty_state,
399 pipe_return_0, /* wait for output to drain */
403 _initialize_ser_pipe ()
405 serial_add_interface (&pipe_ops);