#include "defs.h"
#include "serial.h"
#include "ser-unix.h"
#include "gdb_vfork.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <fcntl.h>
#include "gdb_string.h"
#include <signal.h>
static int pipe_open (struct serial *scb, const char *name);
static void pipe_close (struct serial *scb);
extern void _initialize_ser_pipe (void);
struct pipe_state
{
int pid;
};
static int
pipe_open (struct serial *scb, const char *name)
{
#if !HAVE_SOCKETPAIR
return -1;
#else
struct pipe_state *state;
int pdes[2];
int pid;
if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
return -1;
pid = vfork ();
if (pid == -1)
{
close (pdes[0]);
close (pdes[1]);
return -1;
}
if (pid == 0)
{
close (pdes[0]);
if (pdes[1] != STDOUT_FILENO)
{
dup2 (pdes[1], STDOUT_FILENO);
close (pdes[1]);
}
dup2 (STDOUT_FILENO, STDIN_FILENO);
#if 0
for (old = pidlist; old; old = old->next)
close (fileno (old->fp));
#endif
setgid (getgid ());
execl ("/bin/sh", "sh", "-c", name, (char *) 0);
_exit (127);
}
close (pdes[1]);
state = XMALLOC (struct pipe_state);
state->pid = pid;
scb->fd = pdes[0];
scb->state = state;
signal (SIGPIPE, SIG_IGN);
return 0;
#endif
}
static void
pipe_close (struct serial *scb)
{
struct pipe_state *state = scb->state;
if (state != NULL)
{
int pid = state->pid;
close (scb->fd);
scb->fd = -1;
xfree (state);
scb->state = NULL;
kill (pid, SIGTERM);
}
}
static struct serial_ops pipe_ops;
void
_initialize_ser_pipe (void)
{
struct serial_ops *ops = XMALLOC (struct serial_ops);
memset (ops, 0, sizeof (struct serial_ops));
ops->name = "pipe";
ops->next = 0;
ops->open = pipe_open;
ops->close = pipe_close;
ops->readchar = ser_unix_readchar;
ops->write = ser_unix_write;
ops->flush_output = ser_unix_nop_flush_output;
ops->flush_input = ser_unix_flush_input;
ops->send_break = ser_unix_nop_send_break;
ops->go_raw = ser_unix_nop_raw;
ops->get_tty_state = ser_unix_nop_get_tty_state;
ops->set_tty_state = ser_unix_nop_set_tty_state;
ops->print_tty_state = ser_unix_nop_print_tty_state;
ops->noflush_set_tty_state = ser_unix_nop_noflush_set_tty_state;
ops->setbaudrate = ser_unix_nop_setbaudrate;
ops->setstopbits = ser_unix_nop_setstopbits;
ops->drain_output = ser_unix_nop_drain_output;
ops->async = ser_unix_async;
serial_add_interface (ops);
}