1*b725ae77Skettenis /* Serial interface for a pipe to a separate program
2*b725ae77Skettenis Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3*b725ae77Skettenis
4*b725ae77Skettenis Contributed by Cygnus Solutions.
5*b725ae77Skettenis
6*b725ae77Skettenis This file is part of GDB.
7*b725ae77Skettenis
8*b725ae77Skettenis This program is free software; you can redistribute it and/or modify
9*b725ae77Skettenis it under the terms of the GNU General Public License as published by
10*b725ae77Skettenis the Free Software Foundation; either version 2 of the License, or
11*b725ae77Skettenis (at your option) any later version.
12*b725ae77Skettenis
13*b725ae77Skettenis This program is distributed in the hope that it will be useful,
14*b725ae77Skettenis but WITHOUT ANY WARRANTY; without even the implied warranty of
15*b725ae77Skettenis MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*b725ae77Skettenis GNU General Public License for more details.
17*b725ae77Skettenis
18*b725ae77Skettenis You should have received a copy of the GNU General Public License
19*b725ae77Skettenis along with this program; if not, write to the Free Software
20*b725ae77Skettenis Foundation, Inc., 59 Temple Place - Suite 330,
21*b725ae77Skettenis Boston, MA 02111-1307, USA. */
22*b725ae77Skettenis
23*b725ae77Skettenis #include "defs.h"
24*b725ae77Skettenis #include "serial.h"
25*b725ae77Skettenis #include "ser-unix.h"
26*b725ae77Skettenis
27*b725ae77Skettenis #include "gdb_vfork.h"
28*b725ae77Skettenis
29*b725ae77Skettenis #include <sys/types.h>
30*b725ae77Skettenis #include <sys/socket.h>
31*b725ae77Skettenis #include <sys/time.h>
32*b725ae77Skettenis #include <fcntl.h>
33*b725ae77Skettenis #include "gdb_string.h"
34*b725ae77Skettenis
35*b725ae77Skettenis #include <signal.h>
36*b725ae77Skettenis
37*b725ae77Skettenis static int pipe_open (struct serial *scb, const char *name);
38*b725ae77Skettenis static void pipe_close (struct serial *scb);
39*b725ae77Skettenis
40*b725ae77Skettenis extern void _initialize_ser_pipe (void);
41*b725ae77Skettenis
42*b725ae77Skettenis struct pipe_state
43*b725ae77Skettenis {
44*b725ae77Skettenis int pid;
45*b725ae77Skettenis };
46*b725ae77Skettenis
47*b725ae77Skettenis /* Open up a raw pipe */
48*b725ae77Skettenis
49*b725ae77Skettenis static int
pipe_open(struct serial * scb,const char * name)50*b725ae77Skettenis pipe_open (struct serial *scb, const char *name)
51*b725ae77Skettenis {
52*b725ae77Skettenis #if !HAVE_SOCKETPAIR
53*b725ae77Skettenis return -1;
54*b725ae77Skettenis #else
55*b725ae77Skettenis struct pipe_state *state;
56*b725ae77Skettenis /* This chunk: */
57*b725ae77Skettenis /* Copyright (c) 1988, 1993
58*b725ae77Skettenis * The Regents of the University of California. All rights reserved.
59*b725ae77Skettenis *
60*b725ae77Skettenis * This code is derived from software written by Ken Arnold and
61*b725ae77Skettenis * published in UNIX Review, Vol. 6, No. 8.
62*b725ae77Skettenis */
63*b725ae77Skettenis int pdes[2];
64*b725ae77Skettenis int pid;
65*b725ae77Skettenis if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
66*b725ae77Skettenis return -1;
67*b725ae77Skettenis
68*b725ae77Skettenis /* Create the child process to run the command in. Note that the
69*b725ae77Skettenis apparent call to vfork() below *might* actually be a call to
70*b725ae77Skettenis fork() due to the fact that autoconf will ``#define vfork fork''
71*b725ae77Skettenis on certain platforms. */
72*b725ae77Skettenis pid = vfork ();
73*b725ae77Skettenis
74*b725ae77Skettenis /* Error. */
75*b725ae77Skettenis if (pid == -1)
76*b725ae77Skettenis {
77*b725ae77Skettenis close (pdes[0]);
78*b725ae77Skettenis close (pdes[1]);
79*b725ae77Skettenis return -1;
80*b725ae77Skettenis }
81*b725ae77Skettenis
82*b725ae77Skettenis /* Child. */
83*b725ae77Skettenis if (pid == 0)
84*b725ae77Skettenis {
85*b725ae77Skettenis /* re-wire pdes[1] to stdin/stdout */
86*b725ae77Skettenis close (pdes[0]);
87*b725ae77Skettenis if (pdes[1] != STDOUT_FILENO)
88*b725ae77Skettenis {
89*b725ae77Skettenis dup2 (pdes[1], STDOUT_FILENO);
90*b725ae77Skettenis close (pdes[1]);
91*b725ae77Skettenis }
92*b725ae77Skettenis dup2 (STDOUT_FILENO, STDIN_FILENO);
93*b725ae77Skettenis #if 0
94*b725ae77Skettenis /* close any stray FD's - FIXME - how? */
95*b725ae77Skettenis /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
96*b725ae77Skettenis from previous popen() calls that remain open in the
97*b725ae77Skettenis parent process are closed in the new child process. */
98*b725ae77Skettenis for (old = pidlist; old; old = old->next)
99*b725ae77Skettenis close (fileno (old->fp)); /* don't allow a flush */
100*b725ae77Skettenis #endif
101*b725ae77Skettenis execl ("/bin/sh", "sh", "-c", name, (char *) 0);
102*b725ae77Skettenis _exit (127);
103*b725ae77Skettenis }
104*b725ae77Skettenis
105*b725ae77Skettenis /* Parent. */
106*b725ae77Skettenis close (pdes[1]);
107*b725ae77Skettenis /* :end chunk */
108*b725ae77Skettenis state = XMALLOC (struct pipe_state);
109*b725ae77Skettenis state->pid = pid;
110*b725ae77Skettenis scb->fd = pdes[0];
111*b725ae77Skettenis scb->state = state;
112*b725ae77Skettenis
113*b725ae77Skettenis /* If we don't do this, GDB simply exits when the remote side dies. */
114*b725ae77Skettenis signal (SIGPIPE, SIG_IGN);
115*b725ae77Skettenis return 0;
116*b725ae77Skettenis #endif
117*b725ae77Skettenis }
118*b725ae77Skettenis
119*b725ae77Skettenis static void
pipe_close(struct serial * scb)120*b725ae77Skettenis pipe_close (struct serial *scb)
121*b725ae77Skettenis {
122*b725ae77Skettenis struct pipe_state *state = scb->state;
123*b725ae77Skettenis if (state != NULL)
124*b725ae77Skettenis {
125*b725ae77Skettenis int pid = state->pid;
126*b725ae77Skettenis close (scb->fd);
127*b725ae77Skettenis scb->fd = -1;
128*b725ae77Skettenis xfree (state);
129*b725ae77Skettenis scb->state = NULL;
130*b725ae77Skettenis kill (pid, SIGTERM);
131*b725ae77Skettenis /* Might be useful to check that the child does die. */
132*b725ae77Skettenis }
133*b725ae77Skettenis }
134*b725ae77Skettenis
135*b725ae77Skettenis static struct serial_ops pipe_ops;
136*b725ae77Skettenis
137*b725ae77Skettenis void
_initialize_ser_pipe(void)138*b725ae77Skettenis _initialize_ser_pipe (void)
139*b725ae77Skettenis {
140*b725ae77Skettenis struct serial_ops *ops = XMALLOC (struct serial_ops);
141*b725ae77Skettenis memset (ops, 0, sizeof (struct serial_ops));
142*b725ae77Skettenis ops->name = "pipe";
143*b725ae77Skettenis ops->next = 0;
144*b725ae77Skettenis ops->open = pipe_open;
145*b725ae77Skettenis ops->close = pipe_close;
146*b725ae77Skettenis ops->readchar = ser_unix_readchar;
147*b725ae77Skettenis ops->write = ser_unix_write;
148*b725ae77Skettenis ops->flush_output = ser_unix_nop_flush_output;
149*b725ae77Skettenis ops->flush_input = ser_unix_flush_input;
150*b725ae77Skettenis ops->send_break = ser_unix_nop_send_break;
151*b725ae77Skettenis ops->go_raw = ser_unix_nop_raw;
152*b725ae77Skettenis ops->get_tty_state = ser_unix_nop_get_tty_state;
153*b725ae77Skettenis ops->set_tty_state = ser_unix_nop_set_tty_state;
154*b725ae77Skettenis ops->print_tty_state = ser_unix_nop_print_tty_state;
155*b725ae77Skettenis ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
156*b725ae77Skettenis ops->setbaudrate = ser_unix_nop_setbaudrate;
157*b725ae77Skettenis ops->setstopbits = ser_unix_nop_setstopbits;
158*b725ae77Skettenis ops->drain_output = ser_unix_nop_drain_output;
159*b725ae77Skettenis ops->async = ser_unix_async;
160*b725ae77Skettenis serial_add_interface (ops);
161*b725ae77Skettenis }
162