Replace free() with xfree().
[external/binutils.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 #ifdef HAVE_VFORK
68   pid = vfork ();
69 #else
70   pid = fork ();
71 #endif
72   
73   /* Error. */
74   if (pid == -1)
75     {
76       close (pdes[0]);
77       close (pdes[1]);
78       return -1;
79     }
80
81   /* Child. */
82   if (pid == 0)
83     {
84       /* re-wire pdes[1] to stdin/stdout */
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);
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
100       execl ("/bin/sh", "sh", "-c", name, NULL);
101       _exit (127);
102     }
103
104   /* Parent. */
105   close (pdes[1]);
106   /* :end chunk */
107   state = XMALLOC (struct pipe_state);
108   state->pid = pid;
109   scb->fd = pdes[0];
110   scb->state = state;
111
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
118 static void
119 pipe_close (serial_t scb)
120 {
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;
127       xfree (state);
128       scb->state = NULL;
129       kill (pid, SIGTERM);
130       /* Might be useful to check that the child does die. */
131     }
132 }
133
134 static struct serial_ops pipe_ops;
135
136 void
137 _initialize_ser_pipe (void)
138 {
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;
148   ops->flush_input = ser_unix_flush_input;
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);
160 }