]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/ser-pipe.c
This commit was generated by cvs2svn to track changes on a CVS vendor
[thirdparty/binutils-gdb.git] / gdb / ser-pipe.c
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"
25 #include "ser-unix.h"
26
27 #include <sys/types.h>
28 #include "gdb_wait.h"
29 #include <sys/socket.h>
30 #include <sys/time.h>
31 #include <fcntl.h>
32 #include <string.h>
33
34 #include "signals.h"
35
36 static int pipe_open (serial_t scb, const char *name);
37 static void pipe_close (serial_t scb);
38
39 extern void _initialize_ser_pipe (void);
40
41 struct pipe_state
42 {
43 int pid;
44 };
45
46 /* Open up a raw pipe */
47
48 static int
49 pipe_open (serial_t scb, const char *name)
50 {
51 #if !HAVE_SOCKETPAIR
52 return -1;
53 #else
54 struct pipe_state *state;
55 /* This chunk: */
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
67 pid = vfork ();
68
69 /* Error. */
70 if (pid == -1)
71 {
72 close (pdes[0]);
73 close (pdes[1]);
74 return -1;
75 }
76
77 /* Child. */
78 if (pid == 0)
79 {
80 /* re-wire pdes[1] to stdin/stdout */
81 close (pdes[0]);
82 if (pdes[1] != STDOUT_FILENO)
83 {
84 dup2 (pdes[1], STDOUT_FILENO);
85 close (pdes[1]);
86 }
87 dup2 (STDOUT_FILENO, STDIN_FILENO);
88 #if 0
89 /* close any stray FD's - FIXME - how? */
90 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
91 from previous popen() calls that remain open in the
92 parent process are closed in the new child process. */
93 for (old = pidlist; old; old = old->next)
94 close (fileno (old->fp)); /* don't allow a flush */
95 #endif
96 execl ("/bin/sh", "sh", "-c", name, NULL);
97 _exit (127);
98 }
99
100 /* Parent. */
101 close (pdes[1]);
102 /* :end chunk */
103 state = XMALLOC (struct pipe_state);
104 state->pid = pid;
105 scb->fd = pdes[0];
106 scb->state = state;
107
108 /* If we don't do this, GDB simply exits when the remote side dies. */
109 signal (SIGPIPE, SIG_IGN);
110 return 0;
111 #endif
112 }
113
114 static void
115 pipe_close (serial_t scb)
116 {
117 struct pipe_state *state = scb->state;
118 if (state != NULL)
119 {
120 int pid = state->pid;
121 close (scb->fd);
122 scb->fd = -1;
123 free (state);
124 scb->state = NULL;
125 kill (pid, SIGTERM);
126 /* Might be useful to check that the child does die. */
127 }
128 }
129
130 static struct serial_ops pipe_ops;
131
132 void
133 _initialize_ser_pipe (void)
134 {
135 struct serial_ops *ops = XMALLOC (struct serial_ops);
136 memset (ops, sizeof (struct serial_ops), 0);
137 ops->name = "pipe";
138 ops->next = 0;
139 ops->open = pipe_open;
140 ops->close = pipe_close;
141 ops->readchar = ser_unix_readchar;
142 ops->write = ser_unix_write;
143 ops->flush_output = ser_unix_nop_flush_output;
144 ops->flush_input = ser_unix_flush_input;
145 ops->send_break = ser_unix_nop_send_break;
146 ops->go_raw = ser_unix_nop_raw;
147 ops->get_tty_state = ser_unix_nop_get_tty_state;
148 ops->set_tty_state = ser_unix_nop_set_tty_state;
149 ops->print_tty_state = ser_unix_nop_print_tty_state;
150 ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
151 ops->setbaudrate = ser_unix_nop_setbaudrate;
152 ops->setstopbits = ser_unix_nop_setstopbits;
153 ops->drain_output = ser_unix_nop_drain_output;
154 ops->async = ser_unix_async;
155 serial_add_interface (ops);
156 }