]>
Commit | Line | Data |
---|---|---|
daf3f280 JM |
1 | /* Serial interface for a pipe to a separate program |
2 | Copyright 1999 Free Software Foundation, Inc. | |
3 | ||
4 | Contributed by Cygnus Solutions. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "serial.h" | |
c2c6d25f JM |
25 | #include "ser-unix.h" |
26 | ||
daf3f280 | 27 | #include <sys/types.h> |
03f2053f | 28 | #include "gdb_wait.h" |
daf3f280 JM |
29 | #include <sys/socket.h> |
30 | #include <sys/time.h> | |
31 | #include <fcntl.h> | |
c2d11a7d | 32 | #include <string.h> |
daf3f280 JM |
33 | |
34 | #include "signals.h" | |
daf3f280 | 35 | |
c2c6d25f JM |
36 | static int pipe_open (serial_t scb, const char *name); |
37 | static void pipe_close (serial_t scb); | |
adf40b2e | 38 | |
c2c6d25f | 39 | extern void _initialize_ser_pipe (void); |
adf40b2e JM |
40 | |
41 | struct pipe_state | |
42 | { | |
43 | int pid; | |
44 | }; | |
45 | ||
daf3f280 JM |
46 | /* Open up a raw pipe */ |
47 | ||
48 | static int | |
c2c6d25f | 49 | pipe_open (serial_t scb, const char *name) |
daf3f280 | 50 | { |
2acceee2 | 51 | #if !HAVE_SOCKETPAIR |
daf3f280 JM |
52 | return -1; |
53 | #else | |
adf40b2e | 54 | struct pipe_state *state; |
daf3f280 | 55 | /* This chunk: */ |
daf3f280 JM |
56 | /* Copyright (c) 1988, 1993 |
57 | * The Regents of the University of California. All rights reserved. | |
58 | * | |
59 | * This code is derived from software written by Ken Arnold and | |
60 | * published in UNIX Review, Vol. 6, No. 8. | |
61 | */ | |
62 | int pdes[2]; | |
63 | int pid; | |
64 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0) | |
65 | return -1; | |
66 | ||
818fa2bf | 67 | #ifdef HAVE_VFORK |
adf40b2e | 68 | pid = vfork (); |
818fa2bf AC |
69 | #else |
70 | pid = fork (); | |
71 | #endif | |
adf40b2e JM |
72 | |
73 | /* Error. */ | |
74 | if (pid == -1) | |
daf3f280 | 75 | { |
daf3f280 JM |
76 | close (pdes[0]); |
77 | close (pdes[1]); | |
78 | return -1; | |
adf40b2e JM |
79 | } |
80 | ||
81 | /* Child. */ | |
82 | if (pid == 0) | |
83 | { | |
84 | /* re-wire pdes[1] to stdin/stdout */ | |
daf3f280 JM |
85 | close (pdes[0]); |
86 | if (pdes[1] != STDOUT_FILENO) | |
87 | { | |
88 | dup2 (pdes[1], STDOUT_FILENO); | |
89 | close (pdes[1]); | |
90 | } | |
91 | dup2 (STDOUT_FILENO, STDIN_FILENO); | |
adf40b2e JM |
92 | #if 0 |
93 | /* close any stray FD's - FIXME - how? */ | |
94 | /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams | |
95 | from previous popen() calls that remain open in the | |
96 | parent process are closed in the new child process. */ | |
97 | for (old = pidlist; old; old = old->next) | |
98 | close (fileno (old->fp)); /* don't allow a flush */ | |
99 | #endif | |
c2c6d25f | 100 | execl ("/bin/sh", "sh", "-c", name, NULL); |
daf3f280 JM |
101 | _exit (127); |
102 | } | |
103 | ||
adf40b2e | 104 | /* Parent. */ |
daf3f280 | 105 | close (pdes[1]); |
adf40b2e JM |
106 | /* :end chunk */ |
107 | state = XMALLOC (struct pipe_state); | |
108 | state->pid = pid; | |
daf3f280 | 109 | scb->fd = pdes[0]; |
adf40b2e | 110 | scb->state = state; |
daf3f280 | 111 | |
daf3f280 JM |
112 | /* If we don't do this, GDB simply exits when the remote side dies. */ |
113 | signal (SIGPIPE, SIG_IGN); | |
114 | return 0; | |
115 | #endif | |
116 | } | |
117 | ||
daf3f280 | 118 | static void |
c2c6d25f | 119 | pipe_close (serial_t scb) |
daf3f280 | 120 | { |
adf40b2e JM |
121 | struct pipe_state *state = scb->state; |
122 | if (state != NULL) | |
123 | { | |
124 | int pid = state->pid; | |
125 | close (scb->fd); | |
126 | scb->fd = -1; | |
b8c9b27d | 127 | xfree (state); |
adf40b2e JM |
128 | scb->state = NULL; |
129 | kill (pid, SIGTERM); | |
130 | /* Might be useful to check that the child does die. */ | |
131 | } | |
daf3f280 JM |
132 | } |
133 | ||
c2c6d25f | 134 | static struct serial_ops pipe_ops; |
daf3f280 JM |
135 | |
136 | void | |
c2c6d25f | 137 | _initialize_ser_pipe (void) |
daf3f280 | 138 | { |
c2c6d25f JM |
139 | struct serial_ops *ops = XMALLOC (struct serial_ops); |
140 | memset (ops, sizeof (struct serial_ops), 0); | |
141 | ops->name = "pipe"; | |
142 | ops->next = 0; | |
143 | ops->open = pipe_open; | |
144 | ops->close = pipe_close; | |
145 | ops->readchar = ser_unix_readchar; | |
146 | ops->write = ser_unix_write; | |
147 | ops->flush_output = ser_unix_nop_flush_output; | |
2acceee2 | 148 | ops->flush_input = ser_unix_flush_input; |
c2c6d25f JM |
149 | ops->send_break = ser_unix_nop_send_break; |
150 | ops->go_raw = ser_unix_nop_raw; | |
151 | ops->get_tty_state = ser_unix_nop_get_tty_state; | |
152 | ops->set_tty_state = ser_unix_nop_set_tty_state; | |
153 | ops->print_tty_state = ser_unix_nop_print_tty_state; | |
154 | ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state; | |
155 | ops->setbaudrate = ser_unix_nop_setbaudrate; | |
156 | ops->setstopbits = ser_unix_nop_setstopbits; | |
157 | ops->drain_output = ser_unix_nop_drain_output; | |
158 | ops->async = ser_unix_async; | |
159 | serial_add_interface (ops); | |
daf3f280 | 160 | } |