public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Strange bug with /dev/console in mintty
@ 2017-08-22 16:49 Erik Bray
  2017-08-22 21:32 ` Thomas Wolff
  2017-08-23  8:37 ` Corinna Vinschen
  0 siblings, 2 replies; 5+ messages in thread
From: Erik Bray @ 2017-08-22 16:49 UTC (permalink / raw)
  To: cygwin

Hi folks,

I noticed a strange discrepancy when running Cygwin through mintty, vs
through a normal cmd.exe console.  This is on a build from the latest
git master, but also on Cygwin 2.8.0.  When run from a cmd.exe
console, we can see the following output from fhandler_dev::readdir:

$ ls -l /dev/con*
crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conin
crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conout
crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/cons0
crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/console

The same command when run in mintty returns:

crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 255 Aug 22 18:29 /dev/conin
crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 254 Aug 22 18:29 /dev/conout
crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5,   1 Aug 22 18:29 /dev/console

In the latter case, the device numbers are the default device numbers
for /dev/console, etc.  In the former case, the inodes for
/dev/console, /dev/conin, and /dev/conout are set to the same as
/dev/cons0.  This logic can be found in fhandler_dev::readdir:

203       if (cdev->get_major () == DEV_TTY_MAJOR
204       && (cdev->is_device (FH_CONIN)
205           || cdev->is_device (FH_CONOUT)
206           || cdev->is_device (FH_CONSOLE)))
207     {
208       /* Make sure conin, conout, and console have the same inode number
209          as the current consX. */
210       de->d_ino = myself->ctty;
211     }

where myself->ctty seems to depend largely on what file types the
stdio handles are attached to.

When running from cmd.exe, GetStdHandle(...) returns handles to a
character stream--i.e. the console itself.  However, when running from
mintty it returns pipes (specifically, to a pty, probably related to
the call to forkpty in mintty).  In Cygwin (particularly, in
dtable::init_std_file_from_handle) the result ends up being that when
the stdio handles are pipes, the /dev/cons0 device never gets created.
And thus /dev/console and friends never get rerouted to a real device.
This results in errors when trying to access /dev/console directly:

$ ls -l /dev/console
ls: cannot access '/dev/console': No such device or address

which is the error message for a ENXIO.  This is coming specifically
from the function build_fh_pc in dtable.cc.  The reason is that trying
to access /dev/console results in trying to create an fhandler_console
for a non-existent console.

I'm not really sure what the correct behavior should be here though,
and if it should be fixed on the mintty side or the Cygwin side.

Thanks,
Erik

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange bug with /dev/console in mintty
  2017-08-22 16:49 Strange bug with /dev/console in mintty Erik Bray
@ 2017-08-22 21:32 ` Thomas Wolff
  2017-08-23  8:37 ` Corinna Vinschen
  1 sibling, 0 replies; 5+ messages in thread
From: Thomas Wolff @ 2017-08-22 21:32 UTC (permalink / raw)
  To: cygwin

Am 22.08.2017 um 18:49 schrieb Erik Bray:
> Hi folks,
>
> I noticed a strange discrepancy when running Cygwin through mintty, vs
> through a normal cmd.exe console.  This is on a build from the latest
> git master, but also on Cygwin 2.8.0.  When run from a cmd.exe
> console, we can see the following output from fhandler_dev::readdir:
>
> $ ls -l /dev/con*
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conin
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conout
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/cons0
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/console
>
> The same command when run in mintty returns:
>
> crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 255 Aug 22 18:29 /dev/conin
> crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 254 Aug 22 18:29 /dev/conout
> crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5,   1 Aug 22 18:29 /dev/console
>
> In the latter case, the device numbers are the default device numbers
> for /dev/console, etc.  In the former case, the inodes for
> /dev/console, /dev/conin, and /dev/conout are set to the same as
> /dev/cons0.  This logic can be found in fhandler_dev::readdir:
>
> 203       if (cdev->get_major () == DEV_TTY_MAJOR
> 204       && (cdev->is_device (FH_CONIN)
> 205           || cdev->is_device (FH_CONOUT)
> 206           || cdev->is_device (FH_CONSOLE)))
> 207     {
> 208       /* Make sure conin, conout, and console have the same inode number
> 209          as the current consX. */
> 210       de->d_ino = myself->ctty;
> 211     }
>
> where myself->ctty seems to depend largely on what file types the
> stdio handles are attached to.
>
> When running from cmd.exe, GetStdHandle(...) returns handles to a
> character stream--i.e. the console itself.  However, when running from
> mintty it returns pipes (specifically, to a pty, probably related to
> the call to forkpty in mintty).  In Cygwin (particularly, in
> dtable::init_std_file_from_handle) the result ends up being that when
> the stdio handles are pipes, the /dev/cons0 device never gets created.
> And thus /dev/console and friends never get rerouted to a real device.
> This results in errors when trying to access /dev/console directly:
>
> $ ls -l /dev/console
> ls: cannot access '/dev/console': No such device or address
The device to refer to one's own session is /dev/tty, not /dev/console.
It is not quite obvious how to map the concept of a unique /dev/console 
to modern environments, esp. Cygwin.
Maybe it would help if you describe your intended use case.

> which is the error message for a ENXIO.  This is coming specifically
> from the function build_fh_pc in dtable.cc.  The reason is that trying
> to access /dev/console results in trying to create an fhandler_console
> for a non-existent console.
>
> I'm not really sure what the correct behavior should be here though,
> and if it should be fixed on the mintty side or the Cygwin side.
This is certainly not related to mintty, but likely to pty handling, as 
your own code findings already suggest.

Thomas

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange bug with /dev/console in mintty
  2017-08-22 16:49 Strange bug with /dev/console in mintty Erik Bray
  2017-08-22 21:32 ` Thomas Wolff
@ 2017-08-23  8:37 ` Corinna Vinschen
  2017-08-23 16:08   ` Corinna Vinschen
  1 sibling, 1 reply; 5+ messages in thread
From: Corinna Vinschen @ 2017-08-23  8:37 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 3063 bytes --]

On Aug 22 18:49, Erik Bray wrote:
> Hi folks,
> 
> I noticed a strange discrepancy when running Cygwin through mintty, vs
> through a normal cmd.exe console.  This is on a build from the latest
> git master, but also on Cygwin 2.8.0.  When run from a cmd.exe
> console, we can see the following output from fhandler_dev::readdir:
> 
> $ ls -l /dev/con*
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conin
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conout
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/cons0
> crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/console
> 
> The same command when run in mintty returns:
> 
> crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 255 Aug 22 18:29 /dev/conin
> crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 254 Aug 22 18:29 /dev/conout
> crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5,   1 Aug 22 18:29 /dev/console
> 
> In the latter case, the device numbers are the default device numbers
> for /dev/console, etc.  In the former case, the inodes for
> /dev/console, /dev/conin, and /dev/conout are set to the same as
> /dev/cons0.  This logic can be found in fhandler_dev::readdir:
> 
> 203       if (cdev->get_major () == DEV_TTY_MAJOR
> 204       && (cdev->is_device (FH_CONIN)
> 205           || cdev->is_device (FH_CONOUT)
> 206           || cdev->is_device (FH_CONSOLE)))
> 207     {
> 208       /* Make sure conin, conout, and console have the same inode number
> 209          as the current consX. */
> 210       de->d_ino = myself->ctty;
> 211     }
> 
> where myself->ctty seems to depend largely on what file types the
> stdio handles are attached to.
> 
> When running from cmd.exe, GetStdHandle(...) returns handles to a
> character stream--i.e. the console itself.  However, when running from
> mintty it returns pipes (specifically, to a pty, probably related to
> the call to forkpty in mintty).  In Cygwin (particularly, in
> dtable::init_std_file_from_handle) the result ends up being that when
> the stdio handles are pipes, the /dev/cons0 device never gets created.
> And thus /dev/console and friends never get rerouted to a real device.
> This results in errors when trying to access /dev/console directly:
> 
> $ ls -l /dev/console
> ls: cannot access '/dev/console': No such device or address

/dev/console is an old concept in Cygwin and it was always attached to
the current console window, *if* you were running inside a windows
console.  It was never implemented for pseudo ttys as used by mintty.

The fact that the /dev emulation shows the con devices is a bit
disappointing, given that they are basically unusable.  The existence
check only checks if GetConsoleCP() returns successfully, but that's
true for a pseudo tty as well.  A console is attached to the process
anyway.

I'll think about the existence check.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange bug with /dev/console in mintty
  2017-08-23  8:37 ` Corinna Vinschen
@ 2017-08-23 16:08   ` Corinna Vinschen
  2017-08-29 10:42     ` Erik Bray
  0 siblings, 1 reply; 5+ messages in thread
From: Corinna Vinschen @ 2017-08-23 16:08 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 3455 bytes --]

On Aug 23 10:33, Corinna Vinschen wrote:
> On Aug 22 18:49, Erik Bray wrote:
> > Hi folks,
> > 
> > I noticed a strange discrepancy when running Cygwin through mintty, vs
> > through a normal cmd.exe console.  This is on a build from the latest
> > git master, but also on Cygwin 2.8.0.  When run from a cmd.exe
> > console, we can see the following output from fhandler_dev::readdir:
> > 
> > $ ls -l /dev/con*
> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conin
> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conout
> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/cons0
> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/console
> > 
> > The same command when run in mintty returns:
> > 
> > crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 255 Aug 22 18:29 /dev/conin
> > crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 254 Aug 22 18:29 /dev/conout
> > crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5,   1 Aug 22 18:29 /dev/console
> > 
> > In the latter case, the device numbers are the default device numbers
> > for /dev/console, etc.  In the former case, the inodes for
> > /dev/console, /dev/conin, and /dev/conout are set to the same as
> > /dev/cons0.  This logic can be found in fhandler_dev::readdir:
> > 
> > 203       if (cdev->get_major () == DEV_TTY_MAJOR
> > 204       && (cdev->is_device (FH_CONIN)
> > 205           || cdev->is_device (FH_CONOUT)
> > 206           || cdev->is_device (FH_CONSOLE)))
> > 207     {
> > 208       /* Make sure conin, conout, and console have the same inode number
> > 209          as the current consX. */
> > 210       de->d_ino = myself->ctty;
> > 211     }
> > 
> > where myself->ctty seems to depend largely on what file types the
> > stdio handles are attached to.
> > 
> > When running from cmd.exe, GetStdHandle(...) returns handles to a
> > character stream--i.e. the console itself.  However, when running from
> > mintty it returns pipes (specifically, to a pty, probably related to
> > the call to forkpty in mintty).  In Cygwin (particularly, in
> > dtable::init_std_file_from_handle) the result ends up being that when
> > the stdio handles are pipes, the /dev/cons0 device never gets created.
> > And thus /dev/console and friends never get rerouted to a real device.
> > This results in errors when trying to access /dev/console directly:
> > 
> > $ ls -l /dev/console
> > ls: cannot access '/dev/console': No such device or address
> 
> /dev/console is an old concept in Cygwin and it was always attached to
> the current console window, *if* you were running inside a windows
> console.  It was never implemented for pseudo ttys as used by mintty.
> 
> The fact that the /dev emulation shows the con devices is a bit
> disappointing, given that they are basically unusable.  The existence
> check only checks if GetConsoleCP() returns successfully, but that's
> true for a pseudo tty as well.  A console is attached to the process
> anyway.
> 
> I'll think about the existence check.

I checked in a patch removing the /dev/conin, /dev/conout and
/dev/console files from /dev if the process is not running from
a console.  Check out the latest developer snapshot from
https://cygwin.com/snapshots/


Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange bug with /dev/console in mintty
  2017-08-23 16:08   ` Corinna Vinschen
@ 2017-08-29 10:42     ` Erik Bray
  0 siblings, 0 replies; 5+ messages in thread
From: Erik Bray @ 2017-08-29 10:42 UTC (permalink / raw)
  To: cygwin

On Wed, Aug 23, 2017 at 6:08 PM, Corinna Vinschen wrote:
> On Aug 23 10:33, Corinna Vinschen wrote:
>> On Aug 22 18:49, Erik Bray wrote:
>> > Hi folks,
>> >
>> > I noticed a strange discrepancy when running Cygwin through mintty, vs
>> > through a normal cmd.exe console.  This is on a build from the latest
>> > git master, but also on Cygwin 2.8.0.  When run from a cmd.exe
>> > console, we can see the following output from fhandler_dev::readdir:
>> >
>> > $ ls -l /dev/con*
>> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conin
>> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/conout
>> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/cons0
>> > crw-rw-rw- 4 Erik M. Bray Erik M. Bray 3, 0 Aug 22 18:29 /dev/console
>> >
>> > The same command when run in mintty returns:
>> >
>> > crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 255 Aug 22 18:29 /dev/conin
>> > crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5, 254 Aug 22 18:29 /dev/conout
>> > crw-rw-rw- 1 Erik M. Bray Erik M. Bray 5,   1 Aug 22 18:29 /dev/console
>> >
>> > In the latter case, the device numbers are the default device numbers
>> > for /dev/console, etc.  In the former case, the inodes for
>> > /dev/console, /dev/conin, and /dev/conout are set to the same as
>> > /dev/cons0.  This logic can be found in fhandler_dev::readdir:
>> >
>> > 203       if (cdev->get_major () == DEV_TTY_MAJOR
>> > 204       && (cdev->is_device (FH_CONIN)
>> > 205           || cdev->is_device (FH_CONOUT)
>> > 206           || cdev->is_device (FH_CONSOLE)))
>> > 207     {
>> > 208       /* Make sure conin, conout, and console have the same inode number
>> > 209          as the current consX. */
>> > 210       de->d_ino = myself->ctty;
>> > 211     }
>> >
>> > where myself->ctty seems to depend largely on what file types the
>> > stdio handles are attached to.
>> >
>> > When running from cmd.exe, GetStdHandle(...) returns handles to a
>> > character stream--i.e. the console itself.  However, when running from
>> > mintty it returns pipes (specifically, to a pty, probably related to
>> > the call to forkpty in mintty).  In Cygwin (particularly, in
>> > dtable::init_std_file_from_handle) the result ends up being that when
>> > the stdio handles are pipes, the /dev/cons0 device never gets created.
>> > And thus /dev/console and friends never get rerouted to a real device.
>> > This results in errors when trying to access /dev/console directly:
>> >
>> > $ ls -l /dev/console
>> > ls: cannot access '/dev/console': No such device or address
>>
>> /dev/console is an old concept in Cygwin and it was always attached to
>> the current console window, *if* you were running inside a windows
>> console.  It was never implemented for pseudo ttys as used by mintty.
>>
>> The fact that the /dev emulation shows the con devices is a bit
>> disappointing, given that they are basically unusable.  The existence
>> check only checks if GetConsoleCP() returns successfully, but that's
>> true for a pseudo tty as well.  A console is attached to the process
>> anyway.
>>
>> I'll think about the existence check.
>
> I checked in a patch removing the /dev/conin, /dev/conout and
> /dev/console files from /dev if the process is not running from
> a console.  Check out the latest developer snapshot from
> https://cygwin.com/snapshots/

Thanks Corinna, this patch makes sense.  Indeed, the existence check
just using GetConsoleCP() was a problem I had noted as well, and the
main problem for me was simply that /dev/con{in,out,sole} were being
listed under /dev/ at all when not attached to a Windows console (so
that code I had that was looping over /dev/ entries would break).

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2017-08-29 10:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-22 16:49 Strange bug with /dev/console in mintty Erik Bray
2017-08-22 21:32 ` Thomas Wolff
2017-08-23  8:37 ` Corinna Vinschen
2017-08-23 16:08   ` Corinna Vinschen
2017-08-29 10:42     ` Erik Bray

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).