public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] Cygwin: fstat(): Fix st_rdev returned by fstat() for /dev/tty.
@ 2023-07-10  8:37 Takashi Yano
  0 siblings, 0 replies; only message in thread
From: Takashi Yano @ 2023-07-10  8:37 UTC (permalink / raw)
  To: cygwin-cvs

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

commit b03601a6713a6f7ef9ed3adea7d0e7715332f87d
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Fri Jul 7 06:39:13 2023 +0900

    Cygwin: fstat(): Fix st_rdev returned by fstat() for /dev/tty.
    
    While st_rdev returned by fstat() for /dev/tty should be FH_TTY,
    the current cygwin1.dll returns FH_PTYS+minor or FH_CONS+minor.
    Similarly, fstat() does not return correct value for /dev/console,
    /dev/conout, /dev/conin or /dev/ptmx.
    
    This patch fixes the issue by:
    1) Introduce dev_referred_via in fhandler_termios.
    2) Add new argument, which has dev_t value referred by open(),
      for constructors of fhandler_pty_slave and fhandler_pty_master to
      set the value of dev_referred_via.
    3) Set st_rdev using dev_referred_via in fhandler_termios::fstat()
      if it is available.
    
    Reviewed-by: Corinna Vinschen <corinna@vinschen.de>
    Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>

Diff:
---
 winsup/cygwin/dtable.cc                 |  5 +++--
 winsup/cygwin/fhandler/console.cc       |  8 ++++++--
 winsup/cygwin/fhandler/pty.cc           |  8 +++++---
 winsup/cygwin/fhandler/termios.cc       |  9 +++++++++
 winsup/cygwin/local_includes/fhandler.h | 12 ++++++++++--
 5 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index 2aae2fd65..156445119 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -509,7 +509,7 @@ fh_alloc (path_conv& pc)
 	  break;
 	case FH_PTMX:
 	  if (pc.isopen ())
-	    fh = cnew (fhandler_pty_master, -1);
+	    fh = cnew (fhandler_pty_master, -1, (dev_t) pc.dev);
 	  else
 	    fhraw = cnew_no_ctor (fhandler_pty_master, -1);
 	  break;
@@ -618,7 +618,8 @@ fh_alloc (path_conv& pc)
 	      if (iscons_dev (myself->ctty))
 		fh = cnew (fhandler_console, pc.dev);
 	      else
-		fh = cnew (fhandler_pty_slave, myself->ctty);
+		fh = cnew (fhandler_pty_slave,
+			   minor (myself->ctty), (dev_t) pc.dev);
 	      if (fh->dev () != FH_NADA)
 		fh->set_name ("/dev/tty");
 	    }
diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc
index 6aa3b50bf..b924a6bf3 100644
--- a/winsup/cygwin/fhandler/console.cc
+++ b/winsup/cygwin/fhandler/console.cc
@@ -2110,6 +2110,7 @@ fhandler_console::fhandler_console (fh_devices devunit) :
   fhandler_termios (), input_ready (false), thread_sync_event (NULL),
   input_mutex (NULL), output_mutex (NULL), unit (MAX_CONS_DEV)
 {
+  dev_referred_via = (dev_t) devunit;
   if (devunit > 0)
     dev ().parse (devunit);
   setup ();
@@ -4558,9 +4559,12 @@ fhandler_console::fstat (struct stat *st)
      the console instance. Due to this, get_ttyp() returns NULL here.
      So, calling set_unit() is necessary to access getsid(). */
   if (!get_ttyp ())
-    set_unit ();
+    {
+      dev_referred_via = get_device ();
+      set_unit ();
+    }
 
-  fhandler_base::fstat (st);
+  fhandler_termios::fstat (st);
   st->st_mode = S_IFCHR | S_IRUSR | S_IWUSR;
   pinfo p (get_ttyp ()->getsid ());
   if (p)
diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index 1f2b634a0..8b8c42bc1 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -765,10 +765,11 @@ out:
 
 /* pty slave stuff */
 
-fhandler_pty_slave::fhandler_pty_slave (int unit)
+fhandler_pty_slave::fhandler_pty_slave (int unit, dev_t via)
   : fhandler_pty_common (), inuse (NULL), output_handle_nat (NULL),
   io_handle_nat (NULL), slave_reading (NULL), num_reader (0)
 {
+  dev_referred_via = via;
   if (unit >= 0)
     dev ().parse (DEV_PTYS_MAJOR, unit);
 }
@@ -1769,7 +1770,7 @@ out:
 int
 fhandler_pty_slave::fstat (struct stat *st)
 {
-  fhandler_base::fstat (st);
+  fhandler_termios::fstat (st);
 
   bool to_close = false;
   if (!input_available_event)
@@ -1974,13 +1975,14 @@ errout:
 /*******************************************************
  fhandler_pty_master
 */
-fhandler_pty_master::fhandler_pty_master (int unit)
+fhandler_pty_master::fhandler_pty_master (int unit, dev_t via)
   : fhandler_pty_common (), pktmode (0), master_ctl (NULL),
     master_thread (NULL), from_master_nat (NULL), to_master_nat (NULL),
     from_slave_nat (NULL), to_slave_nat (NULL), echo_r (NULL), echo_w (NULL),
     dwProcessId (0), to_master (NULL), from_master (NULL),
     master_fwd_thread (NULL)
 {
+  dev_referred_via = via;
   if (unit >= 0)
     dev ().parse (DEV_PTYM_MAJOR, unit);
   set_name ("/dev/ptmx");
diff --git a/winsup/cygwin/fhandler/termios.cc b/winsup/cygwin/fhandler/termios.cc
index 1a5dfdd1b..789ae0179 100644
--- a/winsup/cygwin/fhandler/termios.cc
+++ b/winsup/cygwin/fhandler/termios.cc
@@ -691,6 +691,15 @@ fhandler_termios::tcgetsid ()
   return -1;
 }
 
+int
+fhandler_termios::fstat (struct stat *buf)
+{
+  fhandler_base::fstat (buf);
+  if (dev_referred_via > 0)
+    buf->st_rdev = dev_referred_via;
+  return 0;
+}
+
 static bool
 is_console_app (const WCHAR *filename)
 {
diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h
index f82f565cf..661fe23dc 100644
--- a/winsup/cygwin/local_includes/fhandler.h
+++ b/winsup/cygwin/local_includes/fhandler.h
@@ -1936,6 +1936,7 @@ class fhandler_termios: public fhandler_base
   virtual void acquire_input_mutex_if_necessary (DWORD ms) {};
   virtual void release_input_mutex_if_necessary (void) {};
   virtual void discard_input () {};
+  dev_t dev_referred_via;
 
   /* Result status of processing keys in process_sigs(). */
   enum process_sig_state {
@@ -1975,6 +1976,7 @@ class fhandler_termios: public fhandler_base
   void echo_erase (int force = 0);
   virtual off_t lseek (off_t, int);
   pid_t tcgetsid ();
+  virtual int fstat (struct stat *buf);
 
   fhandler_termios (void *) {}
 
@@ -2297,7 +2299,9 @@ private:
   void copy_from (fhandler_base *x)
   {
     pc.free_strings ();
+    dev_t via = this->dev_referred_via; /* Do not copy dev_referred_via */
     *this = *reinterpret_cast<fhandler_console *> (x);
+    this->dev_referred_via = via;
     _copy_from_reset_helper ();
   }
 
@@ -2424,7 +2428,7 @@ class fhandler_pty_slave: public fhandler_pty_common
   typedef ptys_handle_set_t handle_set_t;
 
   /* Constructor */
-  fhandler_pty_slave (int);
+  fhandler_pty_slave (int, dev_t via = 0);
 
   void set_output_handle_nat (HANDLE h) { output_handle_nat = h; }
   HANDLE& get_output_handle_nat () { return output_handle_nat; }
@@ -2462,7 +2466,9 @@ class fhandler_pty_slave: public fhandler_pty_common
   void copy_from (fhandler_base *x)
   {
     pc.free_strings ();
+    dev_t via = this->dev_referred_via; /* Do not copy dev_referred_via */
     *this = *reinterpret_cast<fhandler_pty_slave *> (x);
+    this->dev_referred_via = via;
     _copy_from_reset_helper ();
   }
 
@@ -2537,7 +2543,7 @@ private:
 public:
   HANDLE get_echo_handle () const { return echo_r; }
   /* Constructor */
-  fhandler_pty_master (int);
+  fhandler_pty_master (int, dev_t via = 0);
 
   static DWORD pty_master_thread (const master_thread_param_t *p);
   static DWORD pty_master_fwd_thread (const master_fwd_thread_param_t *p);
@@ -2572,7 +2578,9 @@ public:
   void copy_from (fhandler_base *x)
   {
     pc.free_strings ();
+    dev_t via = this->dev_referred_via; /* Do not copy dev_referred_via */
     *this = *reinterpret_cast<fhandler_pty_master *> (x);
+    this->dev_referred_via = via;
     _copy_from_reset_helper ();
   }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-10  8:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-10  8:37 [newlib-cygwin] Cygwin: fstat(): Fix st_rdev returned by fstat() for /dev/tty Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).