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>
27 #include <sys/socket.h>
35 #include "gdb_string.h"
37 extern int (*ui_loop_hook) PARAMS ((int));
39 static int pipe_open PARAMS ((serial_t scb, const char *name));
40 static void pipe_raw PARAMS ((serial_t scb));
41 static int wait_for PARAMS ((serial_t scb, int timeout));
42 static int pipe_readchar PARAMS ((serial_t scb, int timeout));
43 static int pipe_setbaudrate PARAMS ((serial_t scb, int rate));
44 static int pipe_setstopbits PARAMS ((serial_t scb, int num));
45 static int pipe_write PARAMS ((serial_t scb, const char *str, int len));
46 /* FIXME: static void pipe_restore PARAMS ((serial_t scb)); */
47 static void pipe_close PARAMS ((serial_t scb));
48 static serial_ttystate pipe_get_tty_state PARAMS ((serial_t scb));
49 static int pipe_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
50 static int pipe_return_0 PARAMS ((serial_t));
51 static int pipe_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
53 static void pipe_print_tty_state PARAMS ((serial_t, serial_ttystate));
55 extern void _initialize_ser_pipe PARAMS ((void));
58 #define XMALLOC(T) ((T*) xmalloc (sizeof (T)))
66 /* Open up a raw pipe */
73 #if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL)
76 struct pipe_state *state;
78 /* Copyright (c) 1988, 1993
79 * The Regents of the University of California. All rights reserved.
81 * This code is derived from software written by Ken Arnold and
82 * published in UNIX Review, Vol. 6, No. 8.
86 if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
102 /* re-wire pdes[1] to stdin/stdout */
104 if (pdes[1] != STDOUT_FILENO)
106 dup2 (pdes[1], STDOUT_FILENO);
109 dup2 (STDOUT_FILENO, STDIN_FILENO);
111 /* close any stray FD's - FIXME - how? */
112 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
113 from previous popen() calls that remain open in the
114 parent process are closed in the new child process. */
115 for (old = pidlist; old; old = old->next)
116 close (fileno (old->fp)); /* don't allow a flush */
118 execl ("/bin/sh", "sh", "-c", name + 1, NULL);
125 state = XMALLOC (struct pipe_state);
130 /* Make it non-blocking */
132 int flags = fcntl (scb->fd, F_GETFL, 0);
133 if (fcntl (scb->fd, F_SETFL, flags | O_NONBLOCK) < 0)
141 /* If we don't do this, GDB simply exits when the remote side dies. */
142 signal (SIGPIPE, SIG_IGN);
147 static serial_ttystate
148 pipe_get_tty_state (scb)
152 return xmalloc (sizeof (int));
156 pipe_set_tty_state (scb, ttystate)
158 serial_ttystate ttystate;
174 return; /* Always in raw mode */
177 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
178 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
180 For termio{s}, we actually just setup VTIME if necessary, and let the
181 timeout occur in the read() in pipe_read().
185 wait_for (scb, timeout)
191 fd_set readfds, exceptfds;
194 FD_ZERO (&exceptfds);
199 FD_SET (scb->fd, &readfds);
200 FD_SET (scb->fd, &exceptfds);
205 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
207 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
212 return SERIAL_TIMEOUT;
213 else if (errno == EINTR)
216 return SERIAL_ERROR; /* Got an error from select or poll */
223 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
224 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
225 char if successful. Returns -2 if timeout expired, EOF if line dropped
226 dead, or -3 for any other error (see errno in that case). */
229 pipe_readchar (scb, timeout)
236 if (scb->bufcnt-- > 0)
239 /* We have to be able to keep the GUI alive here, so we break the original
240 timeout into steps of 1 second, running the "keep the GUI alive" hook
241 each time through the loop.
243 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
244 will only go through the loop once. */
246 delta = (timeout == 0 ? 0 : 1);
250 /* N.B. The UI may destroy our world (for instance by calling
251 remote_stop,) in which case we want to get out of here as
252 quickly as possible. It is not safe to touch scb, since
253 someone else might have freed it. The ui_loop_hook signals that
254 we should exit by returning 1. */
258 if (ui_loop_hook (0))
259 return SERIAL_TIMEOUT;
262 status = wait_for (scb, delta);
265 /* If we got a character or an error back from wait_for, then we can
266 break from the loop before the timeout is completed. */
268 if (status != SERIAL_TIMEOUT)
273 /* If we have exhausted the original timeout, then generate
274 a SERIAL_TIMEOUT, and pass it out of the loop. */
276 else if (timeout == 0)
278 status == SERIAL_TIMEOUT;
288 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
289 if (scb->bufcnt != -1 || errno != EINTR)
293 if (scb->bufcnt <= 0)
295 if (scb->bufcnt == 0)
296 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
297 distinguish between EOF & timeouts
300 return SERIAL_ERROR; /* Got an error from read */
304 scb->bufp = scb->buf;
309 pipe_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
311 serial_ttystate new_ttystate;
312 serial_ttystate old_ttystate;
318 pipe_print_tty_state (scb, ttystate)
320 serial_ttystate ttystate;
322 /* Nothing to print. */
327 pipe_setbaudrate (scb, rate)
331 return 0; /* Never fails! */
335 pipe_setstopbits (scb, num)
339 return 0; /* Never fails! */
343 pipe_write (scb, str, len)
352 cc = write (scb->fd, str, len);
366 struct pipe_state *state = scb->state;
369 int pid = state->pid;
375 /* Might be useful to check that the child does die. */
379 static struct serial_ops pipe_ops =
387 pipe_return_0, /* flush output */
388 pipe_return_0, /* flush input */
389 pipe_return_0, /* send break */
393 pipe_print_tty_state,
394 pipe_noflush_set_tty_state,
397 pipe_return_0, /* wait for output to drain */
401 _initialize_ser_pipe ()
403 serial_add_interface (&pipe_ops);