From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 7E975385802F; Mon, 10 Jul 2023 08:37:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7E975385802F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688978272; bh=vNVSfmsjwsCxswsDic/M6G0256Eiv+ef8K+ujyC92Xw=; h=From:To:Subject:Date:From; b=Q6nG0/GdP2LyY7Zz0OE65nxNGqQg81+n8f//N6iCdC8bV8tAf401++olimgRtF7Sg K98wO3hOebyrgTk6BRPdReCuJAld4fEsUv6rSi1jGqS26OaMVpjDG4D3crqt2G1MYt 61P3JdiBlwqvuipjmrTX/eqV7WWSBQgzYKwGbBVI= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: fstat(): Fix st_rdev returned by fstat() for /dev/tty. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 3edb55af820025013b87fdb53249676067a8b8ee X-Git-Newrev: b03601a6713a6f7ef9ed3adea7d0e7715332f87d Message-Id: <20230710083752.7E975385802F@sourceware.org> Date: Mon, 10 Jul 2023 08:37:52 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Db03601a6713= a6f7ef9ed3adea7d0e7715332f87d commit b03601a6713a6f7ef9ed3adea7d0e7715332f87d Author: Takashi Yano Date: Fri Jul 7 06:39:13 2023 +0900 Cygwin: fstat(): Fix st_rdev returned by fstat() for /dev/tty. =20 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. =20 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. =20 Reviewed-by: Corinna Vinschen Signed-off-by: Takashi Yano 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 =3D cnew (fhandler_pty_master, -1); + fh =3D cnew (fhandler_pty_master, -1, (dev_t) pc.dev); else fhraw =3D cnew_no_ctor (fhandler_pty_master, -1); break; @@ -618,7 +618,8 @@ fh_alloc (path_conv& pc) if (iscons_dev (myself->ctty)) fh =3D cnew (fhandler_console, pc.dev); else - fh =3D cnew (fhandler_pty_slave, myself->ctty); + fh =3D cnew (fhandler_pty_slave, + minor (myself->ctty), (dev_t) pc.dev); if (fh->dev () !=3D FH_NADA) fh->set_name ("/dev/tty"); } diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/con= sole.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 devuni= t) : fhandler_termios (), input_ready (false), thread_sync_event (NULL), input_mutex (NULL), output_mutex (NULL), unit (MAX_CONS_DEV) { + dev_referred_via =3D (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 =3D get_device (); + set_unit (); + } =20 - fhandler_base::fstat (st); + fhandler_termios::fstat (st); st->st_mode =3D 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: =20 /* pty slave stuff */ =20 -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 =3D via; if (unit >=3D 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); =20 bool to_close =3D 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 (NUL= L), dwProcessId (0), to_master (NULL), from_master (NULL), master_fwd_thread (NULL) { + dev_referred_via =3D via; if (unit >=3D 0) dev ().parse (DEV_PTYM_MAJOR, unit); set_name ("/dev/ptmx"); diff --git a/winsup/cygwin/fhandler/termios.cc b/winsup/cygwin/fhandler/ter= mios.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; } =20 +int +fhandler_termios::fstat (struct stat *buf) +{ + fhandler_base::fstat (buf); + if (dev_referred_via > 0) + buf->st_rdev =3D 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; =20 /* 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 =3D 0); virtual off_t lseek (off_t, int); pid_t tcgetsid (); + virtual int fstat (struct stat *buf); =20 fhandler_termios (void *) {} =20 @@ -2297,7 +2299,9 @@ private: void copy_from (fhandler_base *x) { pc.free_strings (); + dev_t via =3D this->dev_referred_via; /* Do not copy dev_referred_via = */ *this =3D *reinterpret_cast (x); + this->dev_referred_via =3D via; _copy_from_reset_helper (); } =20 @@ -2424,7 +2428,7 @@ class fhandler_pty_slave: public fhandler_pty_common typedef ptys_handle_set_t handle_set_t; =20 /* Constructor */ - fhandler_pty_slave (int); + fhandler_pty_slave (int, dev_t via =3D 0); =20 void set_output_handle_nat (HANDLE h) { output_handle_nat =3D 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 =3D this->dev_referred_via; /* Do not copy dev_referred_via = */ *this =3D *reinterpret_cast (x); + this->dev_referred_via =3D via; _copy_from_reset_helper (); } =20 @@ -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 =3D 0); =20 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 =3D this->dev_referred_via; /* Do not copy dev_referred_via = */ *this =3D *reinterpret_cast (x); + this->dev_referred_via =3D via; _copy_from_reset_helper (); }