* autoload.cc (load_wsock32): Declare dummy function to force loading of

winsock.
* fhandler.cc (fhandler_base::set_inheritance): Make debugging output more
verbose.
* fhandler_socket.cc (fhandler_socket::fixup_after_fork): Force loading of
winsock32 if winsock2 not available.
* net.cc (set_socket_inheritance): Use DuplicateHandle in all cases to set
inheritance correctly.
(fdsock): Use winsock2_active macro to determine when to set socket
inheritance.  Remove fdtab resource locking since this function should already
be protected.
(cygwin_accept): Simplify logic.  Ensure that fdtab unlock is not called
inappropriately.
(cygwin_rcmd): Use fdtab locking.
(cygwin_rresvport): Ditto.
(cygwin_rexec): Ditto.
* select.cc (peek_socket): Set errno appropriately if winsock select fails.
* winsup.h: Declare check_pty_fds.
* syscalls.cc (check_pty_fds): Rename from check_ttys_fds.  Also check pty
master.
(setsid): Use check_pty_fds.
* dtable.cc (dtable::dec_console_fds): Add check on pty fds.
This commit is contained in:
Christopher Faylor 2001-10-14 04:14:24 +00:00
parent 0476bae576
commit 611d92e24c
9 changed files with 74 additions and 42 deletions

View File

@ -1,3 +1,32 @@
2001-10-13 Christopher Faylor <cgf@redhat.com>
* autoload.cc (load_wsock32): Declare dummy function to force loading
of winsock.
* fhandler.cc (fhandler_base::set_inheritance): Make debugging output
more verbose.
* fhandler_socket.cc (fhandler_socket::fixup_after_fork): Force loading
of winsock32 if winsock2 not available.
* net.cc (set_socket_inheritance): Use DuplicateHandle in all cases to
set inheritance correctly.
(fdsock): Use winsock2_active macro to determine when to set socket
inheritance. Remove fdtab resource locking since this function should
already be protected.
(cygwin_accept): Simplify logic. Ensure that fdtab unlock is not
called inappropriately.
(cygwin_rcmd): Use fdtab locking.
(cygwin_rresvport): Ditto.
(cygwin_rexec): Ditto.
* select.cc (peek_socket): Set errno appropriately if winsock select
fails.
2001-10-13 Kazuhiro Fujieda <fujieda@jaist.ac.jp>
* winsup.h: Declare check_pty_fds.
* syscalls.cc (check_pty_fds): Rename from check_ttys_fds. Also check
pty master.
(setsid): Use check_pty_fds.
* dtable.cc (dtable::dec_console_fds): Add check on pty fds.
2001-10-13 Ralf Habacker <Ralf.Habacker@freenet.de> 2001-10-13 Ralf Habacker <Ralf.Habacker@freenet.de>
* fhandler_dsp.cc (fhandler_dsp::ioctl): Return 0 for successful * fhandler_dsp.cc (fhandler_dsp::ioctl): Return 0 for successful

View File

@ -416,6 +416,7 @@ LoadDLLfunc (SetClipboardData, 8, user32)
LoadDLLfunc (SetTimer, 16, user32) LoadDLLfunc (SetTimer, 16, user32)
LoadDLLfunc (SetUserObjectSecurity, 12, user32) LoadDLLfunc (SetUserObjectSecurity, 12, user32)
LoadDLLfuncEx (load_wsock32, 0, wsock32, 1) // non-existent function forces wsock32 load
LoadDLLfunc (WSAAsyncSelect, 16, wsock32) LoadDLLfunc (WSAAsyncSelect, 16, wsock32)
LoadDLLfunc (WSACleanup, 0, wsock32) LoadDLLfunc (WSACleanup, 0, wsock32)
LoadDLLfunc (WSAGetLastError, 0, wsock32) LoadDLLfunc (WSAGetLastError, 0, wsock32)

View File

@ -55,7 +55,8 @@ set_std_handle (int fd)
void void
dtable::dec_console_fds () dtable::dec_console_fds ()
{ {
if (console_fds > 0 && !--console_fds && myself->ctty != TTY_CONSOLE) if (console_fds > 0 && !--console_fds &&
myself->ctty != TTY_CONSOLE && !check_pty_fds())
FreeConsole (); FreeConsole ();
} }

View File

@ -1629,16 +1629,18 @@ fhandler_base::set_inheritance (HANDLE &h, int not_inheriting)
void void
fhandler_base::fork_fixup (HANDLE parent, HANDLE &h, const char *name) fhandler_base::fork_fixup (HANDLE parent, HANDLE &h, const char *name)
{ {
if (!get_close_on_exec ()) HANDLE oh = h;
if (/* !is_socket () && */ !get_close_on_exec ())
debug_printf ("handle %p already opened", h); debug_printf ("handle %p already opened", h);
else if (!DuplicateHandle (parent, h, hMainProc, &h, 0, 0, else if (!DuplicateHandle (parent, h, hMainProc, &h, 0, !get_close_on_exec (),
DUPLICATE_SAME_ACCESS)) DUPLICATE_SAME_ACCESS))
system_printf ("%s - %E, handle %s<%p>", get_name (), name, h); system_printf ("%s - %E, handle %s<%p>", get_name (), name, h);
#ifdef DEBUG #ifdef DEBUGGING
else else
{ {
debug_printf ("%s success - oldh %p, h %p", get_name (), oh, h);
ProtectHandle1 (h, name); ProtectHandle1 (h, name);
setclexec_pid (h, 0); setclexec_pid (h, !get_close_on_exec ());
} }
#endif #endif
} }

View File

@ -179,6 +179,7 @@ fhandler_socket::fixup_before_fork_exec (DWORD win_proc_id)
} }
} }
extern "C" void __stdcall load_wsock32 ();
void void
fhandler_socket::fixup_after_fork (HANDLE parent) fhandler_socket::fixup_after_fork (HANDLE parent)
{ {
@ -197,6 +198,7 @@ fhandler_socket::fixup_after_fork (HANDLE parent)
} }
else if (!new_sock && !winsock2_active) else if (!new_sock && !winsock2_active)
{ {
load_wsock32 ();
fhandler_base::fixup_after_fork (parent); fhandler_base::fixup_after_fork (parent);
debug_printf ("Without Winsock 2.0"); debug_printf ("Without Winsock 2.0");
} }

View File

@ -121,20 +121,12 @@ WSADATA wsadata;
static SOCKET __stdcall static SOCKET __stdcall
set_socket_inheritance (SOCKET sock) set_socket_inheritance (SOCKET sock)
{ {
if (wincap.has_set_handle_information ()) SOCKET osock = sock;
(void) SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); if (!DuplicateHandle (hMainProc, (HANDLE) sock, hMainProc, (HANDLE *) &sock,
0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE))
system_printf ("DuplicateHandle failed %E");
else else
{ debug_printf ("DuplicateHandle succeeded osock %p, sock %p", osock, sock);
SOCKET newsock;
if (!DuplicateHandle (hMainProc, (HANDLE) sock, hMainProc, (HANDLE *) &newsock,
0, TRUE, DUPLICATE_SAME_ACCESS))
small_printf ("DuplicateHandle failed %E");
else
{
closesocket (sock);
sock = newsock;
}
}
return sock; return sock;
} }
@ -506,15 +498,16 @@ cygwin_getprotobynumber (int number)
fhandler_socket * fhandler_socket *
fdsock (int fd, const char *name, SOCKET soc) fdsock (int fd, const char *name, SOCKET soc)
{ {
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fdsock"); if (!winsock2_active)
if (wsadata.wVersion < 512) /* < Winsock 2.0 */
soc = set_socket_inheritance (soc); soc = set_socket_inheritance (soc);
else
debug_printf ("not setting socket inheritance since winsock2_active %d", winsock2_active);
fhandler_socket *fh = (fhandler_socket *) cygheap->fdtab.build_fhandler (fd, FH_SOCKET, name); fhandler_socket *fh = (fhandler_socket *) cygheap->fdtab.build_fhandler (fd, FH_SOCKET, name);
fh->set_io_handle ((HANDLE) soc); fh->set_io_handle ((HANDLE) soc);
fh->set_flags (O_RDWR); fh->set_flags (O_RDWR);
cygheap->fdtab.inc_need_fixup_before (); cygheap->fdtab.inc_need_fixup_before ();
fh->set_name (name, name); fh->set_name (name, name);
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fdsock"); debug_printf ("fd %d, name '%s', soc %p", fd, name, soc);
return fh; return fh;
} }
@ -523,9 +516,9 @@ extern "C" int
cygwin_socket (int af, int type, int protocol) cygwin_socket (int af, int type, int protocol)
{ {
int res = -1; int res = -1;
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "socket"); SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "socket");
SOCKET soc; SOCKET soc = 0;
int fd = cygheap->fdtab.find_unused_handle (); int fd = cygheap->fdtab.find_unused_handle ();
@ -555,7 +548,7 @@ cygwin_socket (int af, int type, int protocol)
done: done:
syscall_printf ("%d = socket (%d, %d, %d)", res, af, type, protocol); syscall_printf ("%d = socket (%d, %d, %d)", res, af, type, protocol);
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "socket"); ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "socket");
return res; return res;
} }
@ -1217,12 +1210,9 @@ cygwin_accept (int fd, struct sockaddr *peer, int *len)
int res_fd = cygheap->fdtab.find_unused_handle (); int res_fd = cygheap->fdtab.find_unused_handle ();
if (res_fd == -1) if (res_fd == -1)
{
/* FIXME: what is correct errno? */ /* FIXME: what is correct errno? */
set_errno (EMFILE); set_errno (EMFILE);
goto lock_done; else if ((SOCKET) res == (SOCKET) INVALID_SOCKET)
}
if ((SOCKET) res == (SOCKET) INVALID_SOCKET)
set_winsock_errno (); set_winsock_errno ();
else else
{ {
@ -1230,10 +1220,9 @@ cygwin_accept (int fd, struct sockaddr *peer, int *len)
res_fh->set_addr_family (sock->get_addr_family ()); res_fh->set_addr_family (sock->get_addr_family ());
res = res_fd; res = res_fd;
} }
}
lock_done:
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "accept"); ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "accept");
done: }
done:
syscall_printf ("%d = accept (%d, %x, %x)", res, fd, peer, len); syscall_printf ("%d = accept (%d, %x, %x)", res, fd, peer, len);
return res; return res;
} }
@ -2127,6 +2116,7 @@ cygwin_rcmd (char **ahost, unsigned short inport, char *locuser,
if (fd2p) if (fd2p)
{ {
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rcmd");
*fd2p = cygheap->fdtab.find_unused_handle (res_fd + 1); *fd2p = cygheap->fdtab.find_unused_handle (res_fd + 1);
if (*fd2p == -1) if (*fd2p == -1)
goto done; goto done;
@ -2141,10 +2131,10 @@ cygwin_rcmd (char **ahost, unsigned short inport, char *locuser,
res = res_fd; res = res_fd;
} }
if (fd2p) if (fd2p)
{
fdsock (*fd2p, "/dev/tcp", fd2s); fdsock (*fd2p, "/dev/tcp", fd2s);
}
done: done:
if (fd2p)
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "cygwin_rcmd");
syscall_printf ("%d = rcmd (...)", res); syscall_printf ("%d = rcmd (...)", res);
return res; return res;
} }
@ -2156,6 +2146,7 @@ cygwin_rresvport (int *port)
int res = -1; int res = -1;
sigframe thisframe (mainthread); sigframe thisframe (mainthread);
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rresvport");
int res_fd = cygheap->fdtab.find_unused_handle (); int res_fd = cygheap->fdtab.find_unused_handle ();
if (res_fd == -1) if (res_fd == -1)
goto done; goto done;
@ -2169,6 +2160,7 @@ cygwin_rresvport (int *port)
res = res_fd; res = res_fd;
} }
done: done:
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rresvport");
syscall_printf ("%d = rresvport (%d)", res, port ? *port : 0); syscall_printf ("%d = rresvport (%d)", res, port ? *port : 0);
return res; return res;
} }
@ -2182,6 +2174,7 @@ cygwin_rexec (char **ahost, unsigned short inport, char *locuser,
SOCKET fd2s; SOCKET fd2s;
sigframe thisframe (mainthread); sigframe thisframe (mainthread);
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rexec");
int res_fd = cygheap->fdtab.find_unused_handle (); int res_fd = cygheap->fdtab.find_unused_handle ();
if (res_fd == -1) if (res_fd == -1)
goto done; goto done;
@ -2203,6 +2196,7 @@ cygwin_rexec (char **ahost, unsigned short inport, char *locuser,
fdsock (*fd2p, "/dev/tcp", fd2s); fdsock (*fd2p, "/dev/tcp", fd2s);
done: done:
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rexec");
syscall_printf ("%d = rexec (...)", res); syscall_printf ("%d = rexec (...)", res);
return res; return res;
} }

View File

@ -1205,6 +1205,7 @@ peek_socket (select_record *me, int)
if (r == -1) if (r == -1)
{ {
select_printf ("error %d", WSAGetLastError ()); select_printf ("error %d", WSAGetLastError ());
set_winsock_errno ();
return 0; return 0;
} }

View File

@ -65,19 +65,20 @@ close_all_files (void)
cygwin_shared->delqueue.process_queue (); cygwin_shared->delqueue.process_queue ();
} }
static BOOL __stdcall BOOL __stdcall
check_ttys_fds (void) check_pty_fds (void)
{ {
int res = FALSE; int res = FALSE;
SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "close_all_files"); SetResourceLock (LOCK_FD_LIST, WRITE_LOCK, "check_pty_fds");
fhandler_base *fh; fhandler_base *fh;
for (int i = 0; i < (int) cygheap->fdtab.size; i++) for (int i = 0; i < (int) cygheap->fdtab.size; i++)
if ((fh = cygheap->fdtab[i]) != NULL && fh->get_device() == FH_TTYS) if ((fh = cygheap->fdtab[i]) != NULL &&
(fh->get_device() == FH_TTYS || fh->get_device() == FH_PTYM))
{ {
res = TRUE; res = TRUE;
break; break;
} }
ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "close_all_files"); ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK, "check_pty_fds");
return res; return res;
} }
@ -270,7 +271,7 @@ setsid (void)
{ {
if (myself->ctty == TTY_CONSOLE && if (myself->ctty == TTY_CONSOLE &&
!cygheap->fdtab.has_console_fds () && !cygheap->fdtab.has_console_fds () &&
!check_ttys_fds ()) !check_pty_fds ())
FreeConsole (); FreeConsole ();
myself->ctty = -1; myself->ctty = -1;
myself->sid = _getpid (); myself->sid = _getpid ();

View File

@ -144,6 +144,7 @@ void events_init (void);
void events_terminate (void); void events_terminate (void);
void __stdcall close_all_files (void); void __stdcall close_all_files (void);
BOOL __stdcall check_pty_fds (void);
/* Invisible window initialization/termination. */ /* Invisible window initialization/termination. */
HWND __stdcall gethwnd (void); HWND __stdcall gethwnd (void);