From 611d92e24c092849635b8395ec97e9c7140a6324 Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Sun, 14 Oct 2001 04:14:24 +0000 Subject: [PATCH] * 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. --- winsup/cygwin/ChangeLog | 29 +++++++++++++++++ winsup/cygwin/autoload.cc | 1 + winsup/cygwin/dtable.cc | 3 +- winsup/cygwin/fhandler.cc | 10 +++--- winsup/cygwin/fhandler_socket.cc | 2 ++ winsup/cygwin/net.cc | 56 ++++++++++++++------------------ winsup/cygwin/select.cc | 1 + winsup/cygwin/syscalls.cc | 13 ++++---- winsup/cygwin/winsup.h | 1 + 9 files changed, 74 insertions(+), 42 deletions(-) diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog index 41d490d8e..446de968f 100644 --- a/winsup/cygwin/ChangeLog +++ b/winsup/cygwin/ChangeLog @@ -1,3 +1,32 @@ +2001-10-13 Christopher Faylor + + * 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 + + * 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 * fhandler_dsp.cc (fhandler_dsp::ioctl): Return 0 for successful diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc index bf8c61252..196276899 100644 --- a/winsup/cygwin/autoload.cc +++ b/winsup/cygwin/autoload.cc @@ -416,6 +416,7 @@ LoadDLLfunc (SetClipboardData, 8, user32) LoadDLLfunc (SetTimer, 16, user32) LoadDLLfunc (SetUserObjectSecurity, 12, user32) +LoadDLLfuncEx (load_wsock32, 0, wsock32, 1) // non-existent function forces wsock32 load LoadDLLfunc (WSAAsyncSelect, 16, wsock32) LoadDLLfunc (WSACleanup, 0, wsock32) LoadDLLfunc (WSAGetLastError, 0, wsock32) diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc index 694a7cd84..e63bad970 100644 --- a/winsup/cygwin/dtable.cc +++ b/winsup/cygwin/dtable.cc @@ -55,7 +55,8 @@ set_std_handle (int fd) void 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 (); } diff --git a/winsup/cygwin/fhandler.cc b/winsup/cygwin/fhandler.cc index bb0089df6..f087a95d0 100644 --- a/winsup/cygwin/fhandler.cc +++ b/winsup/cygwin/fhandler.cc @@ -1629,16 +1629,18 @@ fhandler_base::set_inheritance (HANDLE &h, int not_inheriting) void 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); - else if (!DuplicateHandle (parent, h, hMainProc, &h, 0, 0, + else if (!DuplicateHandle (parent, h, hMainProc, &h, 0, !get_close_on_exec (), DUPLICATE_SAME_ACCESS)) system_printf ("%s - %E, handle %s<%p>", get_name (), name, h); -#ifdef DEBUG +#ifdef DEBUGGING else { + debug_printf ("%s success - oldh %p, h %p", get_name (), oh, h); ProtectHandle1 (h, name); - setclexec_pid (h, 0); + setclexec_pid (h, !get_close_on_exec ()); } #endif } diff --git a/winsup/cygwin/fhandler_socket.cc b/winsup/cygwin/fhandler_socket.cc index 6c92d1e02..e8f0d1863 100644 --- a/winsup/cygwin/fhandler_socket.cc +++ b/winsup/cygwin/fhandler_socket.cc @@ -179,6 +179,7 @@ fhandler_socket::fixup_before_fork_exec (DWORD win_proc_id) } } +extern "C" void __stdcall load_wsock32 (); void fhandler_socket::fixup_after_fork (HANDLE parent) { @@ -197,6 +198,7 @@ fhandler_socket::fixup_after_fork (HANDLE parent) } else if (!new_sock && !winsock2_active) { + load_wsock32 (); fhandler_base::fixup_after_fork (parent); debug_printf ("Without Winsock 2.0"); } diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc index e14814c69..9e33bb3f8 100644 --- a/winsup/cygwin/net.cc +++ b/winsup/cygwin/net.cc @@ -121,20 +121,12 @@ WSADATA wsadata; static SOCKET __stdcall set_socket_inheritance (SOCKET sock) { - if (wincap.has_set_handle_information ()) - (void) SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT); + SOCKET osock = sock; + if (!DuplicateHandle (hMainProc, (HANDLE) sock, hMainProc, (HANDLE *) &sock, + 0, TRUE, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE)) + system_printf ("DuplicateHandle failed %E"); else - { - 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; - } - } + debug_printf ("DuplicateHandle succeeded osock %p, sock %p", osock, sock); return sock; } @@ -506,15 +498,16 @@ cygwin_getprotobynumber (int number) fhandler_socket * fdsock (int fd, const char *name, SOCKET soc) { - SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "fdsock"); - if (wsadata.wVersion < 512) /* < Winsock 2.0 */ + if (!winsock2_active) 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); fh->set_io_handle ((HANDLE) soc); fh->set_flags (O_RDWR); cygheap->fdtab.inc_need_fixup_before (); 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; } @@ -523,9 +516,9 @@ extern "C" int cygwin_socket (int af, int type, int protocol) { 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 (); @@ -555,7 +548,7 @@ cygwin_socket (int af, int type, int protocol) done: 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; } @@ -1217,12 +1210,9 @@ cygwin_accept (int fd, struct sockaddr *peer, int *len) int res_fd = cygheap->fdtab.find_unused_handle (); if (res_fd == -1) - { - /* FIXME: what is correct errno? */ - set_errno (EMFILE); - goto lock_done; - } - if ((SOCKET) res == (SOCKET) INVALID_SOCKET) + /* FIXME: what is correct errno? */ + set_errno (EMFILE); + else if ((SOCKET) res == (SOCKET) INVALID_SOCKET) set_winsock_errno (); else { @@ -1230,10 +1220,9 @@ cygwin_accept (int fd, struct sockaddr *peer, int *len) res_fh->set_addr_family (sock->get_addr_family ()); res = res_fd; } + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "accept"); } -lock_done: - ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "accept"); -done: + done: syscall_printf ("%d = accept (%d, %x, %x)", res, fd, peer, len); return res; } @@ -2127,6 +2116,7 @@ cygwin_rcmd (char **ahost, unsigned short inport, char *locuser, if (fd2p) { + SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rcmd"); *fd2p = cygheap->fdtab.find_unused_handle (res_fd + 1); if (*fd2p == -1) goto done; @@ -2141,10 +2131,10 @@ cygwin_rcmd (char **ahost, unsigned short inport, char *locuser, res = res_fd; } if (fd2p) - { - fdsock (*fd2p, "/dev/tcp", fd2s); - } + fdsock (*fd2p, "/dev/tcp", fd2s); done: + if (fd2p) + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK|READ_LOCK, "cygwin_rcmd"); syscall_printf ("%d = rcmd (...)", res); return res; } @@ -2156,6 +2146,7 @@ cygwin_rresvport (int *port) int res = -1; sigframe thisframe (mainthread); + SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rresvport"); int res_fd = cygheap->fdtab.find_unused_handle (); if (res_fd == -1) goto done; @@ -2169,6 +2160,7 @@ cygwin_rresvport (int *port) res = res_fd; } done: + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rresvport"); syscall_printf ("%d = rresvport (%d)", res, port ? *port : 0); return res; } @@ -2182,6 +2174,7 @@ cygwin_rexec (char **ahost, unsigned short inport, char *locuser, SOCKET fd2s; sigframe thisframe (mainthread); + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rexec"); int res_fd = cygheap->fdtab.find_unused_handle (); if (res_fd == -1) goto done; @@ -2203,6 +2196,7 @@ cygwin_rexec (char **ahost, unsigned short inport, char *locuser, fdsock (*fd2p, "/dev/tcp", fd2s); done: + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "cygwin_rexec"); syscall_printf ("%d = rexec (...)", res); return res; } diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index e66d078c8..8f1bab9ea 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -1205,6 +1205,7 @@ peek_socket (select_record *me, int) if (r == -1) { select_printf ("error %d", WSAGetLastError ()); + set_winsock_errno (); return 0; } diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index ebdd7674b..2bcee2c0f 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -65,19 +65,20 @@ close_all_files (void) cygwin_shared->delqueue.process_queue (); } -static BOOL __stdcall -check_ttys_fds (void) +BOOL __stdcall +check_pty_fds (void) { 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; 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; break; } - ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "close_all_files"); + ReleaseResourceLock (LOCK_FD_LIST, WRITE_LOCK, "check_pty_fds"); return res; } @@ -270,7 +271,7 @@ setsid (void) { if (myself->ctty == TTY_CONSOLE && !cygheap->fdtab.has_console_fds () && - !check_ttys_fds ()) + !check_pty_fds ()) FreeConsole (); myself->ctty = -1; myself->sid = _getpid (); diff --git a/winsup/cygwin/winsup.h b/winsup/cygwin/winsup.h index 9fcce3a4f..f244891b6 100644 --- a/winsup/cygwin/winsup.h +++ b/winsup/cygwin/winsup.h @@ -144,6 +144,7 @@ void events_init (void); void events_terminate (void); void __stdcall close_all_files (void); +BOOL __stdcall check_pty_fds (void); /* Invisible window initialization/termination. */ HWND __stdcall gethwnd (void);