[PATCH] Cygwin: connect: implement resetting a connected DGRAM socket

2021-04-26 Thread Ken Brown
Following POSIX and Linux, allow a connected DGRAM socket's connection
to be reset (so that the socket becomes unconnected).  This is done by
calling connect and specifing an address whose family is AF_UNSPEC.
---
 winsup/cygwin/fhandler_socket_inet.cc  | 21 --
 winsup/cygwin/fhandler_socket_local.cc | 30 +-
 winsup/cygwin/fhandler_socket_unix.cc  |  7 ++
 winsup/cygwin/release/3.2.1|  3 +++
 winsup/doc/new-features.xml|  6 ++
 5 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/fhandler_socket_inet.cc 
b/winsup/cygwin/fhandler_socket_inet.cc
index f6bb8c503..30eab4099 100644
--- a/winsup/cygwin/fhandler_socket_inet.cc
+++ b/winsup/cygwin/fhandler_socket_inet.cc
@@ -781,8 +781,20 @@ int
 fhandler_socket_inet::connect (const struct sockaddr *name, int namelen)
 {
   struct sockaddr_storage sst;
+  bool reset = (name->sa_family == AF_UNSPEC
+   && get_socket_type () == SOCK_DGRAM);
 
-  if (get_inet_addr_inet (name, namelen, &sst, &namelen) == SOCKET_ERROR)
+  if (reset)
+{
+  if (connect_state () == unconnected)
+   return 0;
+  /* To reset a connected DGRAM socket, call Winsock's connect
+function with the address member of the sockaddr structure
+filled with zeroes. */
+  memset (&sst, 0, sizeof sst);
+  sst.ss_family = get_addr_family ();
+}
+  else if (get_inet_addr_inet (name, namelen, &sst, &namelen) == SOCKET_ERROR)
 return SOCKET_ERROR;
 
   /* Initialize connect state to "connect_pending".  In the SOCK_STREAM
@@ -804,7 +816,12 @@ fhandler_socket_inet::connect (const struct sockaddr 
*name, int namelen)
 
   int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen);
   if (!res)
-connect_state (connected);
+{
+  if (reset)
+   connect_state (unconnected);
+  else
+   connect_state (connected);
+}
   else if (!is_nonblocking ()
   && res == SOCKET_ERROR
   && WSAGetLastError () == WSAEWOULDBLOCK)
diff --git a/winsup/cygwin/fhandler_socket_local.cc 
b/winsup/cygwin/fhandler_socket_local.cc
index 1c8d48b58..bd4081622 100644
--- a/winsup/cygwin/fhandler_socket_local.cc
+++ b/winsup/cygwin/fhandler_socket_local.cc
@@ -894,19 +894,34 @@ fhandler_socket_local::connect (const struct sockaddr 
*name, int namelen)
 {
   struct sockaddr_storage sst;
   int type = 0;
+  bool reset = (name->sa_family == AF_UNSPEC
+   && get_socket_type () == SOCK_DGRAM);
 
-  if (get_inet_addr_local (name, namelen, &sst, &namelen, &type, 
connect_secret)
-  == SOCKET_ERROR)
+  if (reset)
+{
+  if (connect_state () == unconnected)
+   return 0;
+  /* To reset a connected DGRAM socket, call Winsock's connect
+function with the address member of the sockaddr structure
+filled with zeroes. */
+  memset (&sst, 0, sizeof sst);
+  sst.ss_family = get_addr_family ();
+}
+  else if (get_inet_addr_local (name, namelen, &sst, &namelen, &type,
+   connect_secret) == SOCKET_ERROR)
 return SOCKET_ERROR;
 
-  if (get_socket_type () != type)
+  if (get_socket_type () != type && !reset)
 {
   WSASetLastError (WSAEPROTOTYPE);
   set_winsock_errno ();
   return SOCKET_ERROR;
 }
 
-  set_peer_sun_path (name->sa_data);
+  if (reset)
+set_peer_sun_path (NULL);
+  else
+set_peer_sun_path (name->sa_data);
 
   /* Don't move af_local_set_cred into af_local_connect which may be called
  via select, possibly running under another identity.  Call early here,
@@ -933,7 +948,12 @@ fhandler_socket_local::connect (const struct sockaddr 
*name, int namelen)
 
   int res = ::connect (get_socket (), (struct sockaddr *) &sst, namelen);
   if (!res)
-connect_state (connected);
+{
+  if (reset)
+   connect_state (unconnected);
+  else
+   connect_state (connected);
+}
   else if (!is_nonblocking ()
   && res == SOCKET_ERROR
   && WSAGetLastError () == WSAEWOULDBLOCK)
diff --git a/winsup/cygwin/fhandler_socket_unix.cc 
b/winsup/cygwin/fhandler_socket_unix.cc
index 252bcd9a9..a2428e952 100644
--- a/winsup/cygwin/fhandler_socket_unix.cc
+++ b/winsup/cygwin/fhandler_socket_unix.cc
@@ -1696,6 +1696,13 @@ fhandler_socket_unix::connect (const struct sockaddr 
*name, int namelen)
   conn_unlock ();
   return -1;
 }
+  if (name->sa_family == AF_UNSPEC && get_socket_type () == SOCK_DGRAM)
+{
+  connect_state (unconnected);
+  peer_sun_path (NULL);
+  conn_unlock ();
+  return 0;
+}
   connect_state (connect_pending);
   conn_unlock ();
   /* Check validity of name */
diff --git a/winsup/cygwin/release/3.2.1 b/winsup/cygwin/release/3.2.1
index 7662c7114..9edf509bb 100644
--- a/winsup/cygwin/release/3.2.1
+++ b/winsup/cygwin/release/3.2.1
@@ -1,6 +1,9 @@
 What's new:
 ---
 
+- A connected datagram socket can now have its connection reset.  As
+  specified by POSIX and Linux,

Re: [PATCH] Use automake (v5)

2021-04-26 Thread Corinna Vinschen
On Apr 22 13:57, Corinna Vinschen wrote:
> On Apr 21 18:40, Corinna Vinschen wrote:
> > On Apr 20 21:15, Jon Turney wrote:
> > > On 20/04/2021 21:13, Jon Turney wrote:
> > > > For ease of reviewing, this patch doesn't contain changes to generated
> > > > files which would be made by running ./autogen.sh.
> > > 
> > > Sorry about getting distracted from this.  To summarize what I believe 
> > > were
> > > the outstanding issues with v3 [1]:
> > > 
> > > [1] https://cygwin.com/pipermail/cygwin-patches/2020q4/010827.html
> > > 
> > > * 'INCLUDES' is the old name for 'AM_CPPFLAGS' warning from autogen.sh
> > > 
> > > I plan to clean this up in a future patch
> > > 
> > > * 'ps$(EXEEXT)' previously defined' warning from autogen.sh
> > > 
> > > It seems to be a shortcoming of automake that there's no way to suppress
> > > just that warning.
> > > 
> > > One possible solution is build ps.exe with a different name and rename it
> > > while installing, but I think that is counter-productive (in the sense 
> > > that
> > > it trades this warning for making the build more complex to understand)
> > > 
> > > * some object files are in a unexpected places in the build file hierarchy
> > > (compared to naive expectations and/or the non-automake build)
> > 
> > This is the only minor qualm I have with this patch.  It would be nice
> > to have the mingw sources and .o files in the mingw subdir.  It would
> > simply be a bit cleaner.  The files shared between cygwin and mingw
> > (that's only path.cc, I think) could be handled by an include, i. e.
> > 
> >   utils/
> > 
> > path.cc (full implementation)
> > 
> >   utils/mingw/
> > 
> > path.cc:
> > 
> >   #include "../path.cc"
> 
> I wonder if it wouldn't make sense to split out the mingw-only parts
> of path.cc entirely.  I had a quick view into the file and it turns
> out that of the almost 1000 lines in this file, only about 100 lines
> are used by mount.  All the rest is only used by mingw code, i. e.,
> cygcheck and strace.
> 
> That's obviously not part of this patch, but something we should keep
> in mind for a later cleanup.

I tried this as a POC and it's not much of a problem.  See the below
patch.  Cleaning up the includes is still to do.


Corinna


diff --git a/winsup/utils/Makefile.in b/winsup/utils/Makefile.in
index e4f55dd3c50e..a2d8c426fdac 100644
--- a/winsup/utils/Makefile.in
+++ b/winsup/utils/Makefile.in
@@ -58,10 +58,10 @@ endif
 # List all objects to be compiled in MinGW mode.  Any object not on this
 # list will will be compiled in Cygwin mode implicitly, so there is no
 # need for a CYGWIN_OBJS.
-MINGW_OBJS := bloda.o cygcheck.o cygwin-console-helper.o dump_setup.o ldh.o 
path.o strace.o
+MINGW_OBJS := bloda.o cygcheck.o cygwin-console-helper.o dump_setup.o ldh.o 
mingw-path.o strace.o
 MINGW_LDFLAGS:=-static
 
-CYGCHECK_OBJS:=cygcheck.o bloda.o path.o dump_setup.o
+CYGCHECK_OBJS:=cygcheck.o bloda.o mingw-path.o dump_setup.o
 ZLIB:=-lz
 
 .PHONY: all
@@ -69,12 +69,10 @@ all:
 
 # If a binary should link in any objects besides the .o with the same
 # name as the binary, then list those here.
-strace.exe: path.o
-cygcheck.exe: cygcheck.o bloda.o path.o dump_setup.o
+strace.exe: mingw-path.o
+cygcheck.exe: cygcheck.o bloda.o mingw-path.o dump_setup.o
 
-path-mount.o: path.cc
-   ${COMPILE.cc} -c -DFSTAB_ONLY -o $@ $<
-mount.exe: path-mount.o
+mount.exe: path.o
 
 .PHONY: tzmap
 tzmap:
diff --git a/winsup/utils/mingw-path.cc b/winsup/utils/mingw-path.cc
new file mode 100644
index ..6c60a8eb9bae
--- /dev/null
+++ b/winsup/utils/mingw-path.cc
@@ -0,0 +1,795 @@
+/* path.cc
+
+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. */
+
+/* The purpose of this file is to hide all the details about accessing
+   Cygwin's mount table, shortcuts, etc.  If the format or location of
+   the mount table, or the shortcut format changes, this is the file to
+   change to match it. */
+
+#include "path.cc"
+
+/* Used when treating / and \ as equivalent. */
+#define isslash(ch) \
+  ({ \
+  char __c = (ch); \
+  ((__c) == '/' || (__c) == '\\'); \
+   })
+
+
+static const GUID GUID_shortcut =
+  {0x00021401L, 0, 0, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}};
+
+enum {
+  WSH_FLAG_IDLIST = 0x01,  /* Contains an ITEMIDLIST. */
+  WSH_FLAG_FILE = 0x02,/* Contains a file locator element. */
+  WSH_FLAG_DESC = 0x04,/* Contains a description. */
+  WSH_FLAG_RELPATH = 0x08, /* Contains a relative path. */
+  WSH_FLAG_WD = 0x10,  /* Contains a working dir. */
+  WSH_FLAG_CMDLINE = 0x20, /* Contains command line args. */
+  WSH_FLAG_ICON = 0x40 /* Contains a custom icon. */
+};
+
+struct win_shortcut_hdr
+  {
+DWORD size;/* Header size in bytes.  Must contain 0x4c. */
+GUID magic;/* GUID of shortcut files. */
+DWORD flags;   /* Co

Re: [PATCH] Cygwin: connect: set connect state for DGRAM sockets

2021-04-26 Thread Corinna Vinschen
On Apr 23 14:51, Ken Brown wrote:
> When connect is called on a DGRAM socket, the call to Winsock's
> connect can immediately return successfully rather than failing with
> WSAEWOULDBLOCK.  Set the connect state to "connected" in this case.
> 
> Previously the connect state remained "connect_pending" after the
> successful connection.
> ---
>  winsup/cygwin/fhandler_socket_inet.cc  | 19 +++
>  winsup/cygwin/fhandler_socket_local.cc | 19 +++
>  2 files changed, 22 insertions(+), 16 deletions(-)

ACK.  Please push (a release msg entry would be nice, too).


Thanks,
Corinna