Replace ../include/wait.h with gdb_wait.h.
[deliverable/binutils-gdb.git] / gdb / ser-pipe.c
CommitLineData
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
36static int pipe_open (serial_t scb, const char *name);
37static void pipe_close (serial_t scb);
adf40b2e 38
c2c6d25f 39extern void _initialize_ser_pipe (void);
adf40b2e
JM
40
41struct pipe_state
42 {
43 int pid;
44 };
45
daf3f280
JM
46/* Open up a raw pipe */
47
48static int
c2c6d25f 49pipe_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
adf40b2e
JM
67 pid = vfork ();
68
69 /* Error. */
70 if (pid == -1)
daf3f280 71 {
daf3f280
JM
72 close (pdes[0]);
73 close (pdes[1]);
74 return -1;
adf40b2e
JM
75 }
76
77 /* Child. */
78 if (pid == 0)
79 {
80 /* re-wire pdes[1] to stdin/stdout */
daf3f280
JM
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);
adf40b2e
JM
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
c2c6d25f 96 execl ("/bin/sh", "sh", "-c", name, NULL);
daf3f280
JM
97 _exit (127);
98 }
99
adf40b2e 100 /* Parent. */
daf3f280 101 close (pdes[1]);
adf40b2e
JM
102 /* :end chunk */
103 state = XMALLOC (struct pipe_state);
104 state->pid = pid;
daf3f280 105 scb->fd = pdes[0];
adf40b2e 106 scb->state = state;
daf3f280 107
daf3f280
JM
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
daf3f280 114static void
c2c6d25f 115pipe_close (serial_t scb)
daf3f280 116{
adf40b2e
JM
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 }
daf3f280
JM
128}
129
c2c6d25f 130static struct serial_ops pipe_ops;
daf3f280
JM
131
132void
c2c6d25f 133_initialize_ser_pipe (void)
daf3f280 134{
c2c6d25f
JM
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;
2acceee2 144 ops->flush_input = ser_unix_flush_input;
c2c6d25f
JM
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);
daf3f280 156}
This page took 0.048659 seconds and 4 git commands to generate.