2000-08-12 12:48:44 +08:00
|
|
|
/* dtable.cc: file descriptor support.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-01-14 04:03:03 +08:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
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
|
|
|
#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>
|
2000-09-08 10:56:55 +08:00
|
|
|
#include <sys/cygwin.h>
|
2001-11-24 11:11:39 +08:00
|
|
|
#include <assert.h>
|
2002-05-28 09:55:40 +08:00
|
|
|
#include <ntdef.h>
|
|
|
|
#include <winnls.h>
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-03-21 03:50:28 +08:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
2000-07-28 01:30:51 +08:00
|
|
|
#include <winsock.h>
|
2000-08-12 13:35:42 +08:00
|
|
|
#include "pinfo.h"
|
2000-08-22 11:58:47 +08:00
|
|
|
#include "cygerrno.h"
|
2001-03-05 14:28:25 +08:00
|
|
|
#include "perprocess.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "dtable.h"
|
2001-04-19 05:10:15 +08:00
|
|
|
#include "cygheap.h"
|
2002-05-28 09:55:40 +08:00
|
|
|
#include "ntdll.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-09-06 13:17:22 +08:00
|
|
|
static const NO_COPY DWORD std_consts[] = {STD_INPUT_HANDLE, STD_OUTPUT_HANDLE,
|
2002-02-23 03:33:41 +08:00
|
|
|
STD_ERROR_HANDLE};
|
2000-10-09 10:53:44 +08:00
|
|
|
|
2002-10-08 04:06:16 +08:00
|
|
|
static const char *handle_to_fn (HANDLE, char *);
|
|
|
|
|
|
|
|
static const char NO_COPY unknown_file[] = "some disk file";
|
2002-05-28 09:55:40 +08:00
|
|
|
|
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
|
|
|
{
|
2001-04-19 05:10:15 +08:00
|
|
|
if (!cygheap->fdtab.size)
|
2001-09-05 04:48:45 +08:00
|
|
|
cygheap->fdtab.extend (NOFILE_INCR);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __stdcall
|
|
|
|
set_std_handle (int fd)
|
|
|
|
{
|
|
|
|
if (fd == 0)
|
2001-04-19 05:10:15 +08:00
|
|
|
SetStdHandle (std_consts[fd], cygheap->fdtab[fd]->get_handle ());
|
2000-10-09 10:53:44 +08:00
|
|
|
else if (fd <= 2)
|
2001-04-19 05:10:15 +08:00
|
|
|
SetStdHandle (std_consts[fd], cygheap->fdtab[fd]->get_output_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2001-07-26 08:10:52 +08:00
|
|
|
void
|
|
|
|
dtable::dec_console_fds ()
|
|
|
|
{
|
2001-10-14 12:14:24 +08:00
|
|
|
if (console_fds > 0 && !--console_fds &&
|
|
|
|
myself->ctty != TTY_CONSOLE && !check_pty_fds())
|
2001-07-26 08:10:52 +08:00
|
|
|
FreeConsole ();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2001-09-05 04:48:45 +08:00
|
|
|
/* Try to allocate more space for fd table. We can't call realloc ()
|
2000-02-18 03:38:33 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
void
|
|
|
|
dtable::get_debugger_info ()
|
|
|
|
{
|
2002-05-27 10:25:28 +08:00
|
|
|
if (being_debugged ())
|
2002-02-23 03:33:41 +08:00
|
|
|
{
|
|
|
|
char std[3][sizeof ("/dev/ttyNNNN")];
|
|
|
|
std[0][0] = std[1][0] = std [2][0] = '\0';
|
|
|
|
char buf[sizeof ("cYgstd %x") + 32];
|
|
|
|
sprintf (buf, "cYgstd %x %x %x", (unsigned) &std, sizeof (std[0]), 3);
|
|
|
|
OutputDebugString (buf);
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
if (std[i][0])
|
|
|
|
{
|
|
|
|
path_conv pc;
|
|
|
|
HANDLE h = GetStdHandle (std_consts[i]);
|
|
|
|
fhandler_base *fh = build_fhandler_from_name (i, std[i], NULL, pc);
|
|
|
|
if (!fh)
|
|
|
|
continue;
|
|
|
|
if (!fh->open (&pc, (i ? O_WRONLY : O_RDONLY) | O_BINARY, 0777))
|
|
|
|
release (i);
|
|
|
|
else
|
|
|
|
CloseHandle (h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/* Initialize the file descriptor/handle mapping table.
|
2002-01-07 01:29:41 +08:00
|
|
|
This function should only be called when a cygwin function is invoked
|
|
|
|
by a non-cygwin function, i.e., it should only happen very rarely. */
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
void
|
2002-02-23 03:33:41 +08:00
|
|
|
dtable::stdio_init ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-04-29 07:48:28 +08:00
|
|
|
extern void set_console_ctty ();
|
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. */
|
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
if (myself->ppid_handle || ISSTATE (myself, PID_CYGPARENT))
|
|
|
|
return;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2002-10-15 04:25:52 +08:00
|
|
|
init_std_file_from_handle (0, in);
|
2002-02-23 03:33:41 +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))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-02-23 03:33:41 +08:00
|
|
|
/* If that fails, do this as a fall back. */
|
|
|
|
err = out;
|
|
|
|
system_printf ("couldn't make stderr distinct from stdout");
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
2002-02-23 03:33:41 +08:00
|
|
|
|
2002-10-15 04:25:52 +08:00
|
|
|
init_std_file_from_handle (1, out);
|
|
|
|
init_std_file_from_handle (2, err);
|
2002-02-23 03:33:41 +08:00
|
|
|
/* Assign the console as the controlling tty for this process if we actually
|
|
|
|
have a console and no other controlling tty has been assigned. */
|
|
|
|
if (myself->ctty < 0 && GetConsoleCP () > 0)
|
|
|
|
set_console_ctty ();
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::find_unused_handle (int start)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-09-05 04:48:45 +08:00
|
|
|
AssertResourceOwner (LOCK_FD_LIST, READ_LOCK);
|
2000-02-18 03:38:33 +08:00
|
|
|
do
|
|
|
|
{
|
2002-06-25 05:14:04 +08:00
|
|
|
for (size_t i = start; i < 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))
|
|
|
|
{
|
2001-07-26 08:10:52 +08:00
|
|
|
switch (fds[fd]->get_device ())
|
|
|
|
{
|
|
|
|
case FH_SOCKET:
|
|
|
|
dec_need_fixup_before ();
|
2001-08-05 02:53:53 +08:00
|
|
|
break;
|
2001-07-26 08:10:52 +08:00
|
|
|
case FH_CONSOLE:
|
|
|
|
dec_console_fds ();
|
2001-08-05 02:53:53 +08:00
|
|
|
break;
|
2001-07-26 08:10:52 +08:00
|
|
|
}
|
2000-10-17 07:55:58 +08:00
|
|
|
delete fds[fd];
|
2000-02-18 03:38:33 +08:00
|
|
|
fds[fd] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-03 11:20:50 +08:00
|
|
|
extern "C" int
|
2001-10-03 11:49:26 +08:00
|
|
|
cygwin_attach_handle_to_fd (char *name, int fd, HANDLE handle, mode_t bin,
|
|
|
|
DWORD myaccess)
|
|
|
|
{
|
|
|
|
if (fd == -1)
|
|
|
|
fd = cygheap->fdtab.find_unused_handle ();
|
|
|
|
path_conv pc;
|
|
|
|
fhandler_base *res = cygheap->fdtab.build_fhandler_from_name (fd, name, handle,
|
2001-11-05 14:09:15 +08:00
|
|
|
pc);
|
2002-07-04 02:02:54 +08:00
|
|
|
res->init (handle, myaccess, bin ?: pc.binmode ());
|
2001-10-03 11:49:26 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
void
|
2002-10-15 04:25:52 +08:00
|
|
|
dtable::init_std_file_from_handle (int fd, HANDLE handle)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-10-03 11:49:26 +08:00
|
|
|
const char *name;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO buf;
|
|
|
|
struct sockaddr sa;
|
|
|
|
int sal = sizeof (sa);
|
|
|
|
DCB dcb;
|
2002-10-18 09:28:48 +08:00
|
|
|
unsigned bin = O_BINARY;
|
2001-10-03 11:49:26 +08:00
|
|
|
|
|
|
|
first_fd_for_open = 0;
|
2001-03-28 11:42:58 +08:00
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
if (!not_open (fd))
|
|
|
|
return;
|
|
|
|
|
2002-05-29 13:15:43 +08:00
|
|
|
SetLastError (0);
|
|
|
|
DWORD ft = GetFileType (handle);
|
|
|
|
if (ft == FILE_TYPE_UNKNOWN && GetLastError () == ERROR_INVALID_HANDLE)
|
|
|
|
name = NULL;
|
2001-03-28 11:42:58 +08:00
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-06-05 09:42:28 +08:00
|
|
|
/* See if we can consoleify it */
|
2002-05-29 13:15:43 +08:00
|
|
|
if (GetConsoleScreenBufferInfo (handle, &buf))
|
|
|
|
{
|
|
|
|
if (ISSTATE (myself, PID_USETTY))
|
|
|
|
name = "/dev/tty";
|
|
|
|
else
|
|
|
|
name = "/dev/conout";
|
|
|
|
}
|
|
|
|
else if (GetNumberOfConsoleInputEvents (handle, (DWORD *) &buf))
|
|
|
|
{
|
|
|
|
if (ISSTATE (myself, PID_USETTY))
|
|
|
|
name = "/dev/tty";
|
|
|
|
else
|
|
|
|
name = "/dev/conin";
|
|
|
|
}
|
|
|
|
else if (ft == FILE_TYPE_PIPE)
|
|
|
|
{
|
|
|
|
if (fd == 0)
|
|
|
|
name = "/dev/piper";
|
|
|
|
else
|
|
|
|
name = "/dev/pipew";
|
|
|
|
}
|
|
|
|
else if (wsock_started && getpeername ((SOCKET) handle, &sa, &sal) == 0)
|
|
|
|
name = "/dev/socket";
|
|
|
|
else if (GetCommState (handle, &dcb))
|
|
|
|
name = "/dev/ttyS0"; // FIXME - determine correct device
|
2001-10-02 09:58:06 +08:00
|
|
|
else
|
2002-10-18 09:28:48 +08:00
|
|
|
{
|
|
|
|
name = handle_to_fn (handle, (char *) alloca (MAX_PATH + 100));
|
|
|
|
bin = 0;
|
|
|
|
}
|
2001-10-02 09:58:06 +08:00
|
|
|
}
|
2002-05-29 13:15:43 +08:00
|
|
|
|
|
|
|
if (!name)
|
|
|
|
fds[fd] = NULL;
|
|
|
|
else
|
2001-10-02 09:58:06 +08:00
|
|
|
{
|
2002-05-29 13:15:43 +08:00
|
|
|
path_conv pc;
|
2002-10-08 10:44:35 +08:00
|
|
|
fhandler_base *fh = build_fhandler_from_name (fd, name, handle, pc);
|
2002-10-18 09:28:48 +08:00
|
|
|
if (!bin)
|
|
|
|
{
|
|
|
|
bin = fh->get_default_fmode (O_RDWR);
|
|
|
|
if (!bin && name != unknown_file)
|
|
|
|
bin = pc.binmode ();
|
|
|
|
}
|
2002-10-08 10:44:35 +08:00
|
|
|
|
2002-10-15 04:25:52 +08:00
|
|
|
fh->init (handle, GENERIC_READ | GENERIC_WRITE, bin);
|
2002-05-29 13:15:43 +08:00
|
|
|
set_std_handle (fd);
|
|
|
|
paranoid_printf ("fd %d, handle %p", fd, handle);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
2001-10-03 11:49:26 +08:00
|
|
|
dtable::build_fhandler_from_name (int fd, const char *name, HANDLE handle,
|
2001-11-05 14:09:15 +08:00
|
|
|
path_conv& pc, unsigned opt, suffix_info *si)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2002-05-02 12:13:48 +08:00
|
|
|
pc.check (name, opt | PC_NULLEMPTY | PC_FULL | PC_POSIX, si);
|
2001-10-02 09:58:06 +08:00
|
|
|
if (pc.error)
|
|
|
|
{
|
|
|
|
set_errno (pc.error);
|
|
|
|
return NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-05-28 09:55:40 +08:00
|
|
|
if (!pc.exists () && handle)
|
|
|
|
pc.fillin (handle);
|
|
|
|
|
2002-05-24 13:44:10 +08:00
|
|
|
fhandler_base *fh = build_fhandler (fd, pc.get_devn (),
|
|
|
|
pc.return_and_clear_normalized_path (),
|
|
|
|
pc, pc.get_unitn ());
|
2002-05-03 10:43:45 +08:00
|
|
|
return fh;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_base *
|
2002-02-23 03:33:41 +08:00
|
|
|
dtable::build_fhandler (int fd, DWORD dev, const char *unix_name,
|
|
|
|
const char *win32_name, int unit)
|
2002-05-24 13:44:10 +08:00
|
|
|
{
|
|
|
|
return build_fhandler (fd, dev, cstrdup (unix_name), win32_name, unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define cnew(name) new ((void *) ccalloc (HEAP_FHANDLER, 1, sizeof (name))) name
|
|
|
|
fhandler_base *
|
|
|
|
dtable::build_fhandler (int fd, DWORD dev, char *unix_name,
|
|
|
|
const char *win32_name, int unit)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
fhandler_base *fh;
|
|
|
|
|
2000-03-15 12:49:36 +08:00
|
|
|
dev &= FH_DEVMASK;
|
|
|
|
switch (dev)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
case FH_TTYM:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_tty_master) (unit);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_CONSOLE:
|
|
|
|
case FH_CONIN:
|
|
|
|
case FH_CONOUT:
|
2001-11-11 03:46:15 +08:00
|
|
|
if ((fh = cnew (fhandler_console) ()))
|
|
|
|
inc_console_fds ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_PTYM:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_pty_master) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_TTYS:
|
|
|
|
if (unit < 0)
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_tty_slave) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
else
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_tty_slave) (unit);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_WINDOWS:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_windows) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_SERIAL:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_serial) (unit);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_PIPE:
|
|
|
|
case FH_PIPER:
|
|
|
|
case FH_PIPEW:
|
2001-11-24 11:11:39 +08:00
|
|
|
fh = cnew (fhandler_pipe) (dev);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_SOCKET:
|
2001-11-11 03:46:15 +08:00
|
|
|
if ((fh = cnew (fhandler_socket) ()))
|
|
|
|
inc_need_fixup_before ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_DISK:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_disk_file) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2001-11-22 13:59:07 +08:00
|
|
|
case FH_CYGDRIVE:
|
|
|
|
fh = cnew (fhandler_cygdrive) (unit);
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
case FH_FLOPPY:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_floppy) (unit);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_TAPE:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_tape) (unit);
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_NULL:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_null) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
|
|
|
case FH_ZERO:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_zero) ();
|
2000-02-18 03:38:33 +08:00
|
|
|
break;
|
2000-05-03 23:39:10 +08:00
|
|
|
case FH_RANDOM:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_random) (unit);
|
2000-05-03 23:39:10 +08:00
|
|
|
break;
|
2000-10-01 09:02:40 +08:00
|
|
|
case FH_MEM:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_mem) (unit);
|
2000-10-01 09:02:40 +08:00
|
|
|
break;
|
2000-10-17 09:46:26 +08:00
|
|
|
case FH_CLIPBOARD:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_clipboard) ();
|
2000-10-17 09:46:26 +08:00
|
|
|
break;
|
2001-04-16 11:27:16 +08:00
|
|
|
case FH_OSS_DSP:
|
2001-10-14 01:23:35 +08:00
|
|
|
fh = cnew (fhandler_dev_dsp) ();
|
2001-04-16 11:27:16 +08:00
|
|
|
break;
|
2002-05-02 12:13:48 +08:00
|
|
|
case FH_PROC:
|
2002-05-28 09:55:40 +08:00
|
|
|
fh = cnew (fhandler_proc) ();
|
|
|
|
break;
|
2002-05-02 12:13:48 +08:00
|
|
|
case FH_REGISTRY:
|
2002-05-28 09:55:40 +08:00
|
|
|
fh = cnew (fhandler_registry) ();
|
|
|
|
break;
|
2002-05-02 12:13:48 +08:00
|
|
|
case FH_PROCESS:
|
2002-05-28 09:55:40 +08:00
|
|
|
fh = cnew (fhandler_process) ();
|
|
|
|
break;
|
2000-02-18 03:38:33 +08:00
|
|
|
default:
|
2001-11-02 05:15:53 +08:00
|
|
|
system_printf ("internal error -- unknown device - %p", dev);
|
|
|
|
fh = NULL;
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
2002-02-23 03:33:41 +08:00
|
|
|
if (unix_name)
|
|
|
|
{
|
|
|
|
char new_win32_name[strlen (unix_name) + 1];
|
|
|
|
if (!win32_name)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
/* FIXME: ? Should we call win32_device_name here?
|
|
|
|
It seems like overkill, but... */
|
|
|
|
win32_name = strcpy (new_win32_name, unix_name);
|
|
|
|
for (p = (char *) win32_name; (p = strchr (p, '/')); p++)
|
|
|
|
*p = '\\';
|
|
|
|
}
|
|
|
|
fh->set_name (unix_name, win32_name, fh->get_unit ());
|
|
|
|
}
|
2001-11-05 10:45:42 +08:00
|
|
|
debug_printf ("fd %d, fh %p", fd, fh);
|
2000-02-18 03:38:33 +08:00
|
|
|
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
|
|
|
{
|
2002-05-24 13:44:10 +08:00
|
|
|
fhandler_base *newfh = build_fhandler (-1, oldfh->get_device ());
|
2000-02-18 03:38:33 +08:00
|
|
|
*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;
|
2001-07-21 11:20:01 +08:00
|
|
|
debug_printf ("duped '%s' old %p, new %p", oldfh->get_name (), oldfh->get_io_handle (), newfh->get_io_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
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);
|
2002-06-25 04:05:52 +08:00
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2002-06-25 04:05:52 +08:00
|
|
|
if (newfd < 0)
|
|
|
|
{
|
|
|
|
syscall_printf ("new fd out of bounds: %d", newfd);
|
|
|
|
set_errno (EBADF);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if (newfd == oldfd)
|
|
|
|
{
|
|
|
|
res = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((newfh = dup_worker (fds[oldfd])) == NULL)
|
|
|
|
{
|
|
|
|
res = -1;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-06-25 04:05:52 +08:00
|
|
|
debug_printf ("newfh->io_handle %p, oldfh->io_handle %p",
|
|
|
|
newfh->get_io_handle (), fds[oldfd]->get_io_handle ());
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2002-06-25 04:05:52 +08:00
|
|
|
if (!not_open (newfd))
|
2002-10-21 09:00:58 +08:00
|
|
|
close (newfd);
|
2002-06-25 04:05:52 +08:00
|
|
|
else if ((size_t) newfd < size)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (find_unused_handle (newfd) < 0)
|
2000-06-26 23:27:12 +08:00
|
|
|
{
|
2002-06-25 04:05:52 +08:00
|
|
|
newfh->close ();
|
|
|
|
res = -1;
|
2000-06-26 23:27:12 +08:00
|
|
|
goto done;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
fds[newfd] = newfh;
|
2000-10-26 18:13:41 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
if ((res = newfd) <= 2)
|
|
|
|
set_std_handle (res);
|
|
|
|
|
|
|
|
done:
|
2002-06-25 04:05:52 +08:00
|
|
|
MALLOC_CHECK;
|
|
|
|
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
|
2000-02-18 03:38:33 +08:00
|
|
|
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)
|
|
|
|
{
|
2001-09-05 04:48:45 +08:00
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_fork");
|
2000-10-26 18:13:41 +08:00
|
|
|
fhandler_base *fh;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
if ((fh = fds[i]) != NULL)
|
|
|
|
{
|
2001-09-05 04:48:45 +08:00
|
|
|
debug_printf ("fd %d (%s)", i, fh->get_name ());
|
2000-10-26 18:13:41 +08:00
|
|
|
fh->fixup_before_fork_exec (target_proc_id);
|
|
|
|
}
|
2001-09-05 04:48:45 +08:00
|
|
|
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_fork");
|
2000-10-26 18:13:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dtable::fixup_before_exec (DWORD target_proc_id)
|
|
|
|
{
|
2001-04-19 05:10:15 +08:00
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");
|
2000-10-26 18:13:41 +08:00
|
|
|
fhandler_base *fh;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
2001-04-19 05:10:15 +08:00
|
|
|
if ((fh = fds[i]) != NULL && !fh->get_close_on_exec ())
|
2000-10-26 18:13:41 +08:00
|
|
|
{
|
2001-09-05 04:48:45 +08:00
|
|
|
debug_printf ("fd %d (%s)", i, fh->get_name ());
|
2000-10-26 18:13:41 +08:00
|
|
|
fh->fixup_before_fork_exec (target_proc_id);
|
|
|
|
}
|
2001-04-19 05:10:15 +08:00
|
|
|
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");
|
2000-10-26 18:13:41 +08:00
|
|
|
}
|
|
|
|
|
2002-10-18 01:45:09 +08:00
|
|
|
void
|
|
|
|
dtable::set_file_pointers_for_exec ()
|
|
|
|
{
|
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "set_file_pointers_for_exec");
|
|
|
|
fhandler_base *fh;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
if ((fh = fds[i]) != NULL && fh->get_flags () & O_APPEND)
|
|
|
|
SetFilePointer (fh->get_handle (), 0, 0, FILE_END);
|
|
|
|
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fixup_before_exec");
|
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
void
|
2001-04-19 05:10:15 +08:00
|
|
|
dtable::fixup_after_exec (HANDLE parent)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2000-09-03 12:16:35 +08:00
|
|
|
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
|
|
|
{
|
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
|
|
|
{
|
2001-09-05 04:48:45 +08:00
|
|
|
debug_printf ("fd %d (%s)", i, fh->get_name ());
|
2000-02-18 03:38:33 +08:00
|
|
|
fh->fixup_after_fork (parent);
|
|
|
|
}
|
2001-09-10 10:11:59 +08:00
|
|
|
if (i == 0)
|
|
|
|
SetStdHandle (std_consts[i], fh->get_io_handle ());
|
|
|
|
else if (i <= 2)
|
|
|
|
SetStdHandle (std_consts[i], fh->get_output_handle ());
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::vfork_child_dup ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
fhandler_base **newtable;
|
2001-09-05 04:48:45 +08:00
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
|
|
|
|
newtable = (fhandler_base **) ccalloc (HEAP_ARGV, size, sizeof (fds[0]));
|
2000-02-18 03:38:33 +08:00
|
|
|
int res = 1;
|
|
|
|
|
2001-10-31 20:03:45 +08:00
|
|
|
/* Remove impersonation */
|
2002-06-17 07:34:43 +08:00
|
|
|
if (cygheap->user.issetuid ())
|
2001-10-31 20:03:45 +08:00
|
|
|
RevertToSelf ();
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
for (size_t i = 0; i < size; i++)
|
|
|
|
if (not_open (i))
|
|
|
|
continue;
|
2001-07-21 11:20:01 +08:00
|
|
|
else if ((newtable[i] = dup_worker (fds[i])) != NULL)
|
|
|
|
newtable[i]->set_close_on_exec (fds[i]->get_close_on_exec ());
|
|
|
|
else
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
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;
|
2001-09-05 04:48:45 +08:00
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
out:
|
2002-06-11 10:08:00 +08:00
|
|
|
/* Restore impersonation */
|
2002-06-17 07:34:43 +08:00
|
|
|
if (cygheap->user.issetuid ())
|
2002-06-11 10:08:00 +08:00
|
|
|
ImpersonateLoggedOnUser (cygheap->user.token);
|
|
|
|
|
2001-09-05 04:48:45 +08:00
|
|
|
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "dup");
|
2000-02-18 03:38:33 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-12 12:48:44 +08:00
|
|
|
dtable::vfork_parent_restore ()
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
2001-09-05 04:48:45 +08:00
|
|
|
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore");
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
close_all_files ();
|
|
|
|
fhandler_base **deleteme = fds;
|
2001-11-24 11:11:39 +08:00
|
|
|
assert (fds_on_hold != NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
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
|
|
|
|
2001-09-05 04:48:45 +08:00
|
|
|
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "restore");
|
2001-04-19 05:10:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dtable::vfork_child_fixup ()
|
|
|
|
{
|
|
|
|
if (!fds_on_hold)
|
|
|
|
return;
|
2001-07-21 11:20:01 +08:00
|
|
|
debug_printf ("here");
|
2001-04-19 05:10:15 +08:00
|
|
|
fhandler_base **saveme = fds;
|
|
|
|
fds = fds_on_hold;
|
|
|
|
|
|
|
|
fhandler_base *fh;
|
|
|
|
for (int i = 0; i < (int) cygheap->fdtab.size; i++)
|
|
|
|
if ((fh = cygheap->fdtab[i]) != NULL)
|
|
|
|
{
|
|
|
|
fh->clear_readahead ();
|
2001-04-20 23:50:03 +08:00
|
|
|
if (fh->get_close_on_exec ())
|
|
|
|
release (i);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fh->close ();
|
|
|
|
cygheap->fdtab.release (i);
|
|
|
|
}
|
2001-04-19 05:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fds = saveme;
|
|
|
|
cfree (fds_on_hold);
|
|
|
|
fds_on_hold = NULL;
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
return;
|
|
|
|
}
|
2002-05-28 09:55:40 +08:00
|
|
|
|
|
|
|
#define DEVICE_PREFIX "\\device\\"
|
2002-09-23 08:31:31 +08:00
|
|
|
#define DEVICE_PREFIX_LEN sizeof (DEVICE_PREFIX) - 1
|
2002-06-05 12:01:43 +08:00
|
|
|
#define REMOTE "\\Device\\LanmanRedirector\\"
|
|
|
|
#define REMOTE_LEN sizeof (REMOTE) - 1
|
|
|
|
|
2002-10-08 04:06:16 +08:00
|
|
|
static const char *
|
2002-05-28 09:55:40 +08:00
|
|
|
handle_to_fn (HANDLE h, char *posix_fn)
|
|
|
|
{
|
|
|
|
OBJECT_NAME_INFORMATION *ntfn;
|
|
|
|
char fnbuf[32768];
|
|
|
|
|
|
|
|
memset (fnbuf, 0, sizeof (fnbuf));
|
|
|
|
ntfn = (OBJECT_NAME_INFORMATION *) fnbuf;
|
2002-06-05 10:42:15 +08:00
|
|
|
ntfn->Name.MaximumLength = sizeof (fnbuf) - sizeof (*ntfn);
|
|
|
|
ntfn->Name.Buffer = (WCHAR *) (ntfn + 1);
|
2002-05-28 09:55:40 +08:00
|
|
|
|
|
|
|
DWORD res = NtQueryObject (h, ObjectNameInformation, ntfn, sizeof (fnbuf), NULL);
|
2002-05-29 13:15:43 +08:00
|
|
|
|
2002-05-28 09:55:40 +08:00
|
|
|
if (res)
|
|
|
|
{
|
2002-10-08 04:06:16 +08:00
|
|
|
strcpy (posix_fn, unknown_file);
|
2002-06-07 07:53:45 +08:00
|
|
|
debug_printf ("NtQueryObject failed");
|
2002-10-08 04:06:16 +08:00
|
|
|
return unknown_file;
|
2002-05-28 09:55:40 +08:00
|
|
|
}
|
2002-06-05 23:43:49 +08:00
|
|
|
|
|
|
|
// NT seems to do this on an unopened file
|
|
|
|
if (!ntfn->Name.Buffer)
|
|
|
|
{
|
|
|
|
debug_printf ("nt->Name.Buffer == NULL");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-28 09:55:40 +08:00
|
|
|
ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = 0;
|
|
|
|
|
|
|
|
char win32_fn[MAX_PATH + 100];
|
|
|
|
sys_wcstombs (win32_fn, ntfn->Name.Buffer, ntfn->Name.Length);
|
2002-06-03 01:48:05 +08:00
|
|
|
debug_printf ("nt name '%s'", win32_fn);
|
2002-05-28 09:55:40 +08:00
|
|
|
if (!strncasematch (win32_fn, DEVICE_PREFIX, DEVICE_PREFIX_LEN)
|
|
|
|
|| !QueryDosDevice (NULL, fnbuf, sizeof (fnbuf)))
|
|
|
|
return strcpy (posix_fn, win32_fn);
|
2002-09-19 11:30:20 +08:00
|
|
|
|
2002-05-28 09:55:40 +08:00
|
|
|
char *p = strchr (win32_fn + DEVICE_PREFIX_LEN, '\\');
|
|
|
|
if (!p)
|
|
|
|
p = strchr (win32_fn + DEVICE_PREFIX_LEN, '\0');
|
|
|
|
|
|
|
|
int n = p - win32_fn;
|
2002-06-03 01:48:05 +08:00
|
|
|
int maxmatchlen = 0;
|
|
|
|
char *maxmatchdos = NULL;
|
2002-05-28 09:55:40 +08:00
|
|
|
for (char *s = fnbuf; *s; s = strchr (s, '\0') + 1)
|
|
|
|
{
|
|
|
|
char device[MAX_PATH + 10];
|
|
|
|
device[MAX_PATH + 9] = '\0';
|
|
|
|
if (strchr (s, ':') == NULL)
|
|
|
|
continue;
|
|
|
|
if (!QueryDosDevice (s, device, sizeof (device) - 1))
|
|
|
|
continue;
|
2002-06-03 01:48:05 +08:00
|
|
|
char *q = strrchr (device, ';');
|
|
|
|
if (q)
|
|
|
|
{
|
|
|
|
char *r = strchr (q, '\\');
|
|
|
|
if (r)
|
|
|
|
strcpy (q, r + 1);
|
|
|
|
}
|
|
|
|
int devlen = strlen (device);
|
|
|
|
if (device[devlen - 1] == '\\')
|
|
|
|
device[--devlen] = '\0';
|
|
|
|
if (devlen < maxmatchlen)
|
|
|
|
continue;
|
|
|
|
if (!strncasematch (device, win32_fn, devlen) ||
|
|
|
|
(win32_fn[devlen] != '\0' && win32_fn[devlen] != '\\'))
|
2002-05-28 09:55:40 +08:00
|
|
|
continue;
|
2002-06-03 01:48:05 +08:00
|
|
|
maxmatchlen = devlen;
|
|
|
|
maxmatchdos = s;
|
|
|
|
debug_printf ("current match '%s'", device);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *w32 = win32_fn;
|
|
|
|
if (maxmatchlen)
|
|
|
|
{
|
|
|
|
n = strlen (maxmatchdos);
|
|
|
|
if (maxmatchdos[n - 1] == '\\')
|
|
|
|
n--;
|
|
|
|
w32 += maxmatchlen - n;
|
|
|
|
memcpy (w32, maxmatchdos, n);
|
|
|
|
w32[n] = '\\';
|
2002-05-28 09:55:40 +08:00
|
|
|
}
|
2002-06-05 12:01:43 +08:00
|
|
|
else if (strncasematch (w32, REMOTE, REMOTE_LEN))
|
|
|
|
{
|
|
|
|
w32 += REMOTE_LEN - 2;
|
|
|
|
*w32 = '\\';
|
|
|
|
debug_printf ("remote drive");
|
|
|
|
}
|
|
|
|
|
2002-05-28 09:55:40 +08:00
|
|
|
|
2002-06-03 01:48:05 +08:00
|
|
|
debug_printf ("derived path '%s'", w32);
|
2002-05-28 09:55:40 +08:00
|
|
|
cygwin_conv_to_full_posix_path (w32, posix_fn);
|
|
|
|
return posix_fn;
|
|
|
|
}
|