2000-08-12 12:48:44 +08:00
|
|
|
/* dtable.cc: file descriptor support.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000 Cygnus Solutions.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#define __INSIDE_CYGWIN_NET__
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#define Win32_Winsock
|
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <sys/cygwin.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-07-28 01:30:51 +08:00
|
|
|
#include <winsock.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "sync.h"
|
|
|
|
#include "sigproc.h"
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "dtable.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable fdtab;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-10-09 10:53:44 +08:00
|
|
|
static DWORD std_consts[] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
|
|
|
|
STD_ERROR_HANDLE};
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Set aside space for the table of fds */
|
|
|
|
void
|
2000-08-12 13:35:42 +08:00
|
|
|
dtable_init (void)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-08-12 12:48:44 +08:00
|
|
|
if (!fdtab.size)
|
|
|
|
fdtab.extend(NOFILE_INCR);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __stdcall
|
|
|
|
set_std_handle (int fd)
|
|
|
|
{
|
|
|
|
if (fd == 0)
|
2000-10-09 10:53:44 +08:00
|
|
|
SetStdHandle (std_consts[fd], fdtab[fd]->get_handle ());
|
|
|
|
else if (fd <= 2)
|
|
|
|
SetStdHandle (std_consts[fd], fdtab[fd]->get_output_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::extend (int howmuch)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int new_size = size + howmuch;
|
|
|
|
fhandler_base **newfds;
|
|
|
|
|
|
|
|
if (howmuch <= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Try to allocate more space for fd table. We can't call realloc()
|
|
|
|
here to preserve old table if memory allocation fails */
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
if (!(newfds = (fhandler_base **) ccalloc (HEAP_ARGV, new_size, sizeof newfds[0])))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
debug_printf ("calloc failed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fds)
|
|
|
|
{
|
|
|
|
memcpy (newfds, fds, size * sizeof (fds[0]));
|
2000-09-03 12:16:35 +08:00
|
|
|
cfree (fds);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size = new_size;
|
|
|
|
fds = newfds;
|
2000-02-24 11:00:37 +08:00
|
|
|
debug_printf ("size %d, fds %p", size, fds);
|
2000-02-18 03:38:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the file descriptor/handle mapping table.
|
|
|
|
We only initialize the parent table here. The child table is
|
|
|
|
initialized at each fork () call. */
|
|
|
|
|
|
|
|
void
|
2000-08-12 12:48:44 +08:00
|
|
|
stdio_init (void)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
/* Set these before trying to output anything from strace.
|
|
|
|
Also, always set them even if we're to pick up our parent's fds
|
|
|
|
in case they're missed. */
|
|
|
|
|
2000-11-15 14:27:48 +08:00
|
|
|
if (!myself->ppid_handle && NOTSTATE(myself, PID_CYGPARENT))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
|
|
|
|
|
2000-08-12 12:48:44 +08:00
|
|
|
fdtab.init_std_file_from_handle (0, in, GENERIC_READ, "{stdin}");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/* STD_ERROR_HANDLE has been observed to be the same as
|
|
|
|
STD_OUTPUT_HANDLE. We need separate handles (e.g. using pipes
|
|
|
|
to pass data from child to parent). */
|
|
|
|
if (out == err)
|
|
|
|
{
|
|
|
|
/* Since this code is not invoked for forked tasks, we don't have
|
|
|
|
to worry about the close-on-exec flag here. */
|
|
|
|
if (!DuplicateHandle (hMainProc, out, hMainProc, &err, 0,
|
|
|
|
1, DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
/* If that fails, do this as a fall back. */
|
|
|
|
err = out;
|
|
|
|
system_printf ("couldn't make stderr distinct from stdout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-12 12:48:44 +08:00
|
|
|
fdtab.init_std_file_from_handle (1, out, GENERIC_WRITE, "{stdout}");
|
|
|
|
fdtab.init_std_file_from_handle (2, err, GENERIC_WRITE, "{stderr}");
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::not_open (int fd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
SetResourceLock(LOCK_FD_LIST,READ_LOCK," not_open");
|
|
|
|
|
|
|
|
int res = fd < 0 || fd >= (int)size || fds[fd] == NULL;
|
|
|
|
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,READ_LOCK," not open");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::find_unused_handle (int start)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-08-04 12:04:46 +08:00
|
|
|
AssertResourceOwner(LOCK_FD_LIST, READ_LOCK);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
for (int i = start; i < (int) size; i++)
|
2000-08-04 12:04:46 +08:00
|
|
|
/* See if open -- no need for overhead of not_open */
|
|
|
|
if (fds[i] == NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
while (extend (NOFILE_INCR));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::release (int fd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
if (!not_open (fd))
|
|
|
|
{
|
2000-10-26 18:13:41 +08:00
|
|
|
if ((fds[fd]->get_device () & FH_DEVMASK) == FH_SOCKET)
|
|
|
|
dec_need_fixup_before ();
|
2000-10-17 07:55:58 +08:00
|
|
|
delete fds[fd];
|
2000-02-18 03:38:33 +08:00
|
|
|
fds[fd] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::init_std_file_from_handle (int fd, HANDLE handle,
|
2000-02-18 03:38:33 +08:00
|
|
|
DWORD myaccess, const char *name)
|
|
|
|
{
|
2000-07-30 00:01:23 +08:00
|
|
|
int bin = binmode ? O_BINARY : 0;
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Check to see if we're being redirected - if not then
|
|
|
|
we open then as consoles */
|
|
|
|
if (fd == 0 || fd == 1 || fd == 2)
|
|
|
|
{
|
|
|
|
first_fd_for_open = 0;
|
|
|
|
/* See if we can consoleify it - if it is a console,
|
|
|
|
don't open it in binary. That will screw up our crlfs*/
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO buf;
|
|
|
|
if (GetConsoleScreenBufferInfo (handle, &buf))
|
|
|
|
{
|
|
|
|
bin = 0;
|
|
|
|
if (ISSTATE (myself, PID_USETTY))
|
|
|
|
name = "/dev/tty";
|
|
|
|
else
|
|
|
|
name = "/dev/conout";
|
|
|
|
}
|
|
|
|
else if (FlushConsoleInputBuffer (handle))
|
|
|
|
{
|
|
|
|
bin = 0;
|
|
|
|
if (ISSTATE (myself, PID_USETTY))
|
|
|
|
name = "/dev/tty";
|
|
|
|
else
|
|
|
|
name = "/dev/conin";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
build_fhandler (fd, name, handle)->init (handle, myaccess, bin);
|
|
|
|
set_std_handle (fd);
|
|
|
|
paranoid_printf ("fd %d, handle %p", fd, handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
int
|
|
|
|
cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin,
|
|
|
|
DWORD myaccess)
|
|
|
|
{
|
|
|
|
if (fd == -1)
|
2000-08-12 12:48:44 +08:00
|
|
|
fd = fdtab.find_unused_handle();
|
|
|
|
fhandler_base *res = fdtab.build_fhandler (fd, name, handle);
|
2000-02-18 03:38:33 +08:00
|
|
|
res->init (handle, myaccess, bin);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::build_fhandler (int fd, const char *name, HANDLE handle)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int unit;
|
|
|
|
DWORD devn;
|
|
|
|
|
|
|
|
if ((devn = get_device_number (name, unit)) == FH_BAD)
|
|
|
|
{
|
|
|
|
struct sockaddr sa;
|
|
|
|
int sal = sizeof (sa);
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO cinfo;
|
|
|
|
DCB dcb;
|
|
|
|
|
|
|
|
if (handle == NULL)
|
|
|
|
devn = FH_DISK;
|
|
|
|
else if (GetNumberOfConsoleInputEvents (handle, (DWORD *) &cinfo))
|
|
|
|
devn = FH_CONIN;
|
|
|
|
else if (GetConsoleScreenBufferInfo (handle, &cinfo))
|
|
|
|
devn= FH_CONOUT;
|
|
|
|
else if (wsock32_handle && getpeername ((SOCKET) handle, &sa, &sal))
|
|
|
|
devn = FH_SOCKET;
|
|
|
|
else if (GetFileType (handle) == FILE_TYPE_PIPE)
|
|
|
|
devn = FH_PIPE;
|
|
|
|
else if (GetCommState (handle, &dcb))
|
|
|
|
devn = FH_SERIAL;
|
|
|
|
else
|
|
|
|
devn = FH_DISK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return build_fhandler (fd, devn, name, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::build_fhandler (int fd, DWORD dev, const char *name, int unit)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
fhandler_base *fh;
|
2000-09-03 12:16:35 +08:00
|
|
|
void *buf = ccalloc (HEAP_FHANDLER, 1, sizeof (fhandler_union) + 100);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2000-03-15 12:49:36 +08:00
|
|
|
dev &= FH_DEVMASK;
|
|
|
|
switch (dev)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
case FH_TTYM:
|
|
|
|
fh = new (buf) fhandler_tty_master (name, unit);
|
|
|
|
break;
|
|
|
|
case FH_CONSOLE:
|
|
|
|
case FH_CONIN:
|
|
|
|
case FH_CONOUT:
|
|
|
|
fh = new (buf) fhandler_console (name);
|
|
|
|
break;
|
|
|
|
case FH_PTYM:
|
|
|
|
fh = new (buf) fhandler_pty_master (name);
|
|
|
|
break;
|
|
|
|
case FH_TTYS:
|
|
|
|
if (unit < 0)
|
|
|
|
fh = new (buf) fhandler_tty_slave (name);
|
|
|
|
else
|
|
|
|
fh = new (buf) fhandler_tty_slave (unit, name);
|
|
|
|
break;
|
|
|
|
case FH_WINDOWS:
|
|
|
|
fh = new (buf) fhandler_windows (name);
|
|
|
|
break;
|
|
|
|
case FH_SERIAL:
|
2000-03-15 12:49:36 +08:00
|
|
|
fh = new (buf) fhandler_serial (name, dev, unit);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_PIPE:
|
|
|
|
case FH_PIPER:
|
|
|
|
case FH_PIPEW:
|
2000-03-15 12:49:36 +08:00
|
|
|
fh = new (buf) fhandler_pipe (name, dev);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_SOCKET:
|
|
|
|
fh = new (buf) fhandler_socket (name);
|
|
|
|
break;
|
|
|
|
case FH_DISK:
|
|
|
|
fh = new (buf) fhandler_disk_file (NULL);
|
|
|
|
break;
|
|
|
|
case FH_FLOPPY:
|
|
|
|
fh = new (buf) fhandler_dev_floppy (name, unit);
|
|
|
|
break;
|
|
|
|
case FH_TAPE:
|
|
|
|
fh = new (buf) fhandler_dev_tape (name, unit);
|
|
|
|
break;
|
|
|
|
case FH_NULL:
|
|
|
|
fh = new (buf) fhandler_dev_null (name);
|
|
|
|
break;
|
|
|
|
case FH_ZERO:
|
|
|
|
fh = new (buf) fhandler_dev_zero (name);
|
|
|
|
break;
|
2000-05-03 23:39:10 +08:00
|
|
|
case FH_RANDOM:
|
|
|
|
fh = new (buf) fhandler_dev_random (name, unit);
|
|
|
|
break;
|
2000-10-01 09:02:40 +08:00
|
|
|
case FH_MEM:
|
|
|
|
fh = new (buf) fhandler_dev_mem (name, unit);
|
|
|
|
break;
|
2000-10-17 09:46:26 +08:00
|
|
|
case FH_CLIPBOARD:
|
|
|
|
fh = new (buf) fhandler_dev_clipboard (name);
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
default:
|
|
|
|
/* FIXME - this could recurse forever */
|
|
|
|
return build_fhandler (fd, name, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_printf ("%s - cb %d, fd %d, fh %p", fh->get_name () ?: "", fh->cb,
|
|
|
|
fd, fh);
|
|
|
|
return fd >= 0 ? (fds[fd] = fh) : fh;
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::dup_worker (fhandler_base *oldfh)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
fhandler_base *newfh = build_fhandler (-1, oldfh->get_device (), NULL);
|
|
|
|
*newfh = *oldfh;
|
|
|
|
newfh->set_io_handle (NULL);
|
|
|
|
if (oldfh->dup (newfh))
|
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
cfree (newfh);
|
2000-02-18 03:38:33 +08:00
|
|
|
newfh = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
newfh->set_close_on_exec_flag (0);
|
|
|
|
MALLOC_CHECK;
|
|
|
|
return newfh;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::dup2 (int oldfd, int newfd)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
int res = -1;
|
|
|
|
fhandler_base *newfh = NULL; // = NULL to avoid an incorrect warning
|
|
|
|
|
|
|
|
MALLOC_CHECK;
|
|
|
|
debug_printf ("dup2 (%d, %d)", oldfd, newfd);
|
|
|
|
|
|
|
|
if (not_open (oldfd))
|
|
|
|
{
|
2000-06-26 23:27:12 +08:00
|
|
|
syscall_printf ("fd %d not open", oldfd);
|
2000-02-18 03:38:33 +08:00
|
|
|
set_errno (EBADF);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newfd == oldfd)
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((newfh = dup_worker (fds[oldfd])) == NULL)
|
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-08-12 12:48:44 +08:00
|
|
|
if ((size_t) newfd >= fdtab.size || newfd < 0)
|
2000-06-26 23:27:12 +08:00
|
|
|
{
|
|
|
|
syscall_printf ("new fd out of bounds: %d", newfd);
|
|
|
|
set_errno (EBADF);
|
|
|
|
goto done;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
|
|
|
if ((size_t) newfd >= fdtab.size)
|
|
|
|
{
|
|
|
|
int inc_size = NOFILE_INCR * ((newfd + NOFILE_INCR - 1) / NOFILE_INCR) -
|
|
|
|
fdtab.size;
|
|
|
|
fdtab.extend (inc_size);
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!not_open (newfd))
|
|
|
|
_close (newfd);
|
|
|
|
fds[newfd] = newfh;
|
2000-10-26 18:13:41 +08:00
|
|
|
|
|
|
|
/* Count sockets. */
|
|
|
|
if ((fds[newfd]->get_device () & FH_DEVMASK) == FH_SOCKET)
|
|
|
|
inc_need_fixup_before ();
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
MALLOC_CHECK;
|
|
|
|
|
|
|
|
if ((res = newfd) <= 2)
|
|
|
|
set_std_handle (res);
|
|
|
|
|
|
|
|
MALLOC_CHECK;
|
|
|
|
done:
|
|
|
|
syscall_printf ("%d = dup2 (%d, %d)", res, oldfd, newfd);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
select_record *
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::select_read (int fd, select_record *s)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
if (not_open (fd))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
fhandler_base *fh = fds[fd];
|
2000-02-18 03:38:33 +08:00
|
|
|
s = fh->select_read (s);
|
|
|
|
s->fd = fd;
|
|
|
|
s->fh = fh;
|
|
|
|
s->saw_error = 0;
|
|
|
|
debug_printf ("%s fd %d", fh->get_name (), fd);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
select_record *
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::select_write (int fd, select_record *s)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
if (not_open (fd))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
fhandler_base *fh = fds[fd];
|
2000-02-18 03:38:33 +08:00
|
|
|
s = fh->select_write (s);
|
|
|
|
s->fd = fd;
|
|
|
|
s->fh = fh;
|
|
|
|
s->saw_error = 0;
|
|
|
|
debug_printf ("%s fd %d", fh->get_name (), fd);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
select_record *
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::select_except (int fd, select_record *s)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
if (not_open (fd))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
fhandler_base *fh = fds[fd];
|
2000-02-18 03:38:33 +08:00
|
|
|
s = fh->select_except (s);
|
|
|
|
s->fd = fd;
|
|
|
|
s->fh = fh;
|
|
|
|
s->saw_error = 0;
|
|
|
|
debug_printf ("%s fd %d", fh->get_name (), fd);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Function to walk the fd table after an exec and perform
|
|
|
|
per-fhandler type fixups. */
|
2000-10-26 18:13:41 +08:00
|
|
|
void
|
|
|
|
dtable::fixup_before_fork (DWORD target_proc_id)
|
|
|
|
{
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
fhandler_base *fh;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
if ((fh = fds[i]) != NULL)
|
|
|
|
{
|
|
|
|
debug_printf ("fd %d(%s)", i, fh->get_name ());
|
|
|
|
fh->fixup_before_fork_exec (target_proc_id);
|
|
|
|
}
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dtable::fixup_before_exec (DWORD target_proc_id)
|
|
|
|
{
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
fhandler_base *fh;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
if ((fh = fds[i]) != NULL && (!fh->get_close_on_exec ()))
|
|
|
|
{
|
|
|
|
debug_printf ("fd %d(%s)", i, fh->get_name ());
|
|
|
|
fh->fixup_before_fork_exec (target_proc_id);
|
|
|
|
}
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
void
|
|
|
|
dtable::fixup_after_exec (HANDLE parent, size_t sz, fhandler_base **f)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
size = sz;
|
|
|
|
fds = f;
|
|
|
|
first_fd_for_open = 0;
|
2000-10-25 22:17:27 +08:00
|
|
|
fhandler_base *fh;
|
2000-09-03 12:16:35 +08:00
|
|
|
for (size_t i = 0; i < size; i++)
|
2000-10-25 22:17:27 +08:00
|
|
|
if ((fh = fds[i]) != NULL)
|
2000-09-11 00:43:47 +08:00
|
|
|
{
|
2000-10-25 22:17:27 +08:00
|
|
|
fh->clear_readahead ();
|
|
|
|
if (fh->get_close_on_exec ())
|
2000-09-11 00:43:47 +08:00
|
|
|
release (i);
|
|
|
|
else
|
2000-10-09 10:53:44 +08:00
|
|
|
{
|
2000-10-25 22:17:27 +08:00
|
|
|
fh->fixup_after_exec (parent);
|
2000-10-09 10:53:44 +08:00
|
|
|
if (i == 0)
|
2000-10-25 22:17:27 +08:00
|
|
|
SetStdHandle (std_consts[i], fh->get_io_handle ());
|
2000-10-09 10:53:44 +08:00
|
|
|
else if (i <= 2)
|
2000-10-25 22:17:27 +08:00
|
|
|
SetStdHandle (std_consts[i], fh->get_output_handle ());
|
2000-10-09 10:53:44 +08:00
|
|
|
}
|
2000-09-11 00:43:47 +08:00
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::fixup_after_fork (HANDLE parent)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
2000-09-03 12:16:35 +08:00
|
|
|
fhandler_base *fh;
|
2000-02-18 03:38:33 +08:00
|
|
|
for (size_t i = 0; i < size; i++)
|
2000-09-03 12:16:35 +08:00
|
|
|
if ((fh = fds[i]) != NULL)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-10-25 22:17:27 +08:00
|
|
|
if (fh->get_close_on_exec () || fh->get_need_fork_fixup ())
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
debug_printf ("fd %d(%s)", i, fh->get_name ());
|
|
|
|
fh->fixup_after_fork (parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::vfork_child_dup ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
fhandler_base **newtable;
|
2000-09-03 12:16:35 +08:00
|
|
|
newtable = (fhandler_base **) ccalloc (HEAP_ARGV, size, sizeof(fds[0]));
|
2000-02-18 03:38:33 +08:00
|
|
|
int res = 1;
|
|
|
|
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
if (not_open (i))
|
|
|
|
continue;
|
|
|
|
else if ((newtable[i] = dup_worker (fds[i])) == NULL)
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
set_errno (EBADF);
|
|
|
|
goto out;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
fds_on_hold = fds;
|
|
|
|
fds = newtable;
|
|
|
|
out:
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::vfork_parent_restore ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
|
|
|
|
close_all_files ();
|
|
|
|
fhandler_base **deleteme = fds;
|
|
|
|
fds = fds_on_hold;
|
|
|
|
fds_on_hold = NULL;
|
2000-09-03 12:16:35 +08:00
|
|
|
cfree (deleteme);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK,"dup");
|
|
|
|
return;
|
|
|
|
}
|