[newlib-cygwin] Cygwin: proc fd: fix handling of pipes, sockets, etc

Corinna Vinschen corinna@sourceware.org
Tue Feb 5 14:37:00 GMT 2019


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=5628399c8470d3bbf6ca05b3d0f7f0366f1c16f8

commit 5628399c8470d3bbf6ca05b3d0f7f0366f1c16f8
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Tue Feb 5 15:32:08 2019 +0100

    Cygwin: proc fd: fix handling of pipes, sockets, etc
    
    The symlink target of /proc/PID/fd files pointing to pipes and
    sockets are just artificial filenames referencing the object using
    some internal number.  The pipe open code expects a path specifying
    process pid and the internal number so it access the right process
    and pipe.
    
    - Set the posix path of the pipe to the simple pipe name only,
      as it shows up in /proc/PID/fd.  A /proc/self prefix is just
      as wrong as a /dev/fd prefix.
    
    - Revert thinko in fhandler_pipe::open expecting the name as
      /proc/self/fd/...  In fact this should never happen.
    
    - Fix up the path before re-opening the pipe instead.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/fhandler_pipe.cc       |  7 ++-----
 winsup/cygwin/fhandler_process_fd.cc | 11 +++++++++++
 winsup/cygwin/syscalls.cc            |  4 ++--
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/fhandler_pipe.cc b/winsup/cygwin/fhandler_pipe.cc
index 3a70565..31e73ce 100644
--- a/winsup/cygwin/fhandler_pipe.cc
+++ b/winsup/cygwin/fhandler_pipe.cc
@@ -73,11 +73,8 @@ fhandler_pipe::open (int flags, mode_t mode)
   bool inh;
   bool got_one = false;
 
-  if (sscanf (get_name (), "/proc/self/fd/pipe:[%llu]",
-	      (long long *) &uniq_id) == 1)
-    pid = myself->pid;
-  else if (sscanf (get_name (), "/proc/%d/fd/pipe:[%llu]",
-		   &pid, (long long *) &uniq_id) < 2)
+  if (sscanf (get_name (), "/proc/%d/fd/pipe:[%llu]",
+	      &pid, (long long *) &uniq_id) < 2)
     {
       set_errno (ENOENT);
       return 0;
diff --git a/winsup/cygwin/fhandler_process_fd.cc b/winsup/cygwin/fhandler_process_fd.cc
index 4f326b6..3bf8b74 100644
--- a/winsup/cygwin/fhandler_process_fd.cc
+++ b/winsup/cygwin/fhandler_process_fd.cc
@@ -13,6 +13,7 @@ details. */
 #include "pinfo.h"
 #include "dtable.h"
 #include "cygheap.h"
+#include "tls_pbuf.h"
 
 fhandler_base *
 fhandler_process_fd::fetch_fh (HANDLE &out_hdl, uint32_t flags)
@@ -86,6 +87,16 @@ fhandler_process_fd::fetch_fh (HANDLE &out_hdl, uint32_t flags)
       CloseHandle (hdl);
       return NULL;
     }
+  /* relative path?  This happens for special types like pipes and sockets. */
+  if (*pc.get_posix () != '/')
+    {
+      tmp_pathbuf tp;
+      char *fullpath = tp.c_get ();
+
+      stpcpy (stpncpy (fullpath, get_name (), path - get_name ()),
+	      pc.get_posix ());
+      pc.set_posix (fullpath);
+    }
   fhandler_base *fh = build_fh_pc (pc);
   if (!fh)
     {
diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc
index d1a1312..62b9638 100644
--- a/winsup/cygwin/syscalls.cc
+++ b/winsup/cygwin/syscalls.cc
@@ -5061,8 +5061,8 @@ pipe_worker (int filedes[2], unsigned int psize, int mode)
     {
       cygheap_fdnew fdin;
       cygheap_fdnew fdout (fdin, false);
-      char buf[sizeof ("/proc/self/fd/pipe:[9223372036854775807]")];
-      __small_sprintf (buf, "/proc/self/fd/pipe:[%D]", fhs[0]->get_plain_ino ());
+      char buf[sizeof ("pipe:[9223372036854775807]")];
+      __small_sprintf (buf, "pipe:[%D]", fhs[0]->get_plain_ino ());
       fhs[0]->pc.set_posix (buf);
       __small_sprintf (buf, "pipe:[%D]", fhs[1]->get_plain_ino ());
       fhs[1]->pc.set_posix (buf);



More information about the Cygwin-cvs mailing list