public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Return codes over 1 byte
@ 2015-07-09 23:30 Michael DePaulo
  2015-07-09 23:42 ` Yaakov Selkowitz
  2015-07-14 21:26 ` Eric Blake
  0 siblings, 2 replies; 5+ messages in thread
From: Michael DePaulo @ 2015-07-09 23:30 UTC (permalink / raw)
  To: The Cygwin Mailing List

mark06 mentioned this on IRC today and then left the channel about 1 hour later:

<mark06> has anyone ever discussed exit codes above one byte? they are
valid on modern windows, but cygwin's bash will mess them

I was curious, so I googled it (I could not find an answer) and then
tried it out. I can confirm the what he said.

Cygwin Bash:

mike@executor ~
$ uname -a
CYGWIN_NT-10.0 executor 2.0.4(0.287/5/3) 2015-06-09 12:22 x86_64 Cygwin

mike@executor ~
$ cat return.c
int main (){ return 512; }

mike@executor ~
$ x86_64-w64-mingw32-gcc return.c -o return.exe

mike@executor ~
$ ./return.exe

mike@executor ~
$ echo $?
0

cmd.exe:

C:\cygwin64\home\mike>return.exe

C:\cygwin64\home\mike>echo %errorlevel%
512

-Mike

--
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: Return codes over 1 byte
  2015-07-09 23:30 Return codes over 1 byte Michael DePaulo
@ 2015-07-09 23:42 ` Yaakov Selkowitz
  2015-07-10  0:21   ` Michael DePaulo
  2015-07-14 21:26 ` Eric Blake
  1 sibling, 1 reply; 5+ messages in thread
From: Yaakov Selkowitz @ 2015-07-09 23:42 UTC (permalink / raw)
  To: cygwin

On Thu, 2015-07-09 at 19:30 -0400, Michael DePaulo wrote:
> mark06 mentioned this on IRC today and then left the channel about 1 hour later:
> 
> <mark06> has anyone ever discussed exit codes above one byte? they are
> valid on modern windows, but cygwin's bash will mess them
> 
> I was curious, so I googled it (I could not find an answer) and then
> tried it out. I can confirm the what he said.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html
https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html

--
Yaakov



--
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: Return codes over 1 byte
  2015-07-09 23:42 ` Yaakov Selkowitz
@ 2015-07-10  0:21   ` Michael DePaulo
  2015-07-10  8:28     ` Corinna Vinschen
  0 siblings, 1 reply; 5+ messages in thread
From: Michael DePaulo @ 2015-07-10  0:21 UTC (permalink / raw)
  To: The Cygwin Mailing List

On Thu, Jul 9, 2015 at 7:42 PM, Yaakov Selkowitz <yselkowitz@cygwin.com> wrote:
> On Thu, 2015-07-09 at 19:30 -0400, Michael DePaulo wrote:
>> mark06 mentioned this on IRC today and then left the channel about 1 hour later:
>>
>> <mark06> has anyone ever discussed exit codes above one byte? they are
>> valid on modern windows, but cygwin's bash will mess them
>>
>> I was curious, so I googled it (I could not find an answer) and then
>> tried it out. I can confirm the what he said.
>
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html
> https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
>
> --
> Yaakov

Right, only the least significant 8 bits are outputted. So "257" becomes "1".

I thought I read somewhere that the Cygwin mintty terminal + bash
shell is supposed to be suitable for running native windows apps.
Maybe I was thinking of the 1st paragraph on this page after the list
of features:
https://code.google.com/p/mintty/

Or maybe I was thinking about this reply:
https://cygwin.com/ml/cygwin/2007-03/msg00758.html

Either way, I feel like this should be documented somewhere. Perhaps I
should submit a patch to add a section like "return codes" to to this
page?
https://cygwin.com/cygwin-ug-net/using-effectively.html
https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/doc/effectively.xml

-Mike

--
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: Return codes over 1 byte
  2015-07-10  0:21   ` Michael DePaulo
@ 2015-07-10  8:28     ` Corinna Vinschen
  0 siblings, 0 replies; 5+ messages in thread
From: Corinna Vinschen @ 2015-07-10  8:28 UTC (permalink / raw)
  To: cygwin

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

On Jul  9 20:20, Michael DePaulo wrote:
> On Thu, Jul 9, 2015 at 7:42 PM, Yaakov Selkowitz <yselkowitz@cygwin.com> wrote:
> > On Thu, 2015-07-09 at 19:30 -0400, Michael DePaulo wrote:
> >> mark06 mentioned this on IRC today and then left the channel about 1 hour later:
> >>
> >> <mark06> has anyone ever discussed exit codes above one byte? they are
> >> valid on modern windows, but cygwin's bash will mess them
> >>
> >> I was curious, so I googled it (I could not find an answer) and then
> >> tried it out. I can confirm the what he said.
> >
> > http://pubs.opengroup.org/onlinepubs/9699919799/functions/exit.html
> > https://www.gnu.org/software/libc/manual/html_node/Exit-Status.html
> >
> > --
> > Yaakov
> 
> Right, only the least significant 8 bits are outputted. So "257" becomes "1".
> 
> I thought I read somewhere that the Cygwin mintty terminal + bash
> shell is supposed to be suitable for running native windows apps.
> Maybe I was thinking of the 1st paragraph on this page after the list
> of features:
> https://code.google.com/p/mintty/
> 
> Or maybe I was thinking about this reply:
> https://cygwin.com/ml/cygwin/2007-03/msg00758.html
> 
> Either way, I feel like this should be documented somewhere. Perhaps I
> should submit a patch to add a section like "return codes" to to this
> page?
> https://cygwin.com/cygwin-ug-net/using-effectively.html
> https://cygwin.com/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=winsup/doc/effectively.xml

Feel free.  Please send patches to the cygwin-patches ML.  While you're
at it, you might want to scratch the entire section called "Cygwin and
Windows Networking".  It's so 20th century...


Thanks,
Corinna

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

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

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

* Re: Return codes over 1 byte
  2015-07-09 23:30 Return codes over 1 byte Michael DePaulo
  2015-07-09 23:42 ` Yaakov Selkowitz
@ 2015-07-14 21:26 ` Eric Blake
  1 sibling, 0 replies; 5+ messages in thread
From: Eric Blake @ 2015-07-14 21:26 UTC (permalink / raw)
  To: cygwin

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

On 07/09/2015 05:30 PM, Michael DePaulo wrote:
> mark06 mentioned this on IRC today and then left the channel about 1 hour later:
> 
> <mark06> has anyone ever discussed exit codes above one byte? they are
> valid on modern windows, but cygwin's bash will mess them

POSIX requires that all sizeof(int) bytes in exit() be visible to
calling apps that use waitid(); however, while Solaris has implemented
this, Linux has not (Linux intentionally truncates exit() values to 1
byte before storing it in the kernel task information, so later waitid()
has no way to reconstruct the three truncated bytes).

Since Cygwin is emulating Linux, we can also get by with truncating exit
status to one byte, although it would be nice for POSIX reasons to
eventually reach the point where waitid() can return all four bytes.


> mike@executor ~
> $ ./return.exe
> 
> mike@executor ~
> $ echo $?
> 0

Most shells (bash included) are NOT using waitid() internally, but are
still sticking to the older wait() and waitpid() interfaces.  Per POSIX,
those older interfaces MUST truncate the exit status into just 8 bits,
because it is being combined with other pieces of information (hence the
WIFEXITED() macro and friends).  It is only waitid() that can return
more than 8 bits, but that in turn requires the kernel to track more
than 8 bits.  And therein lies a bootstrap problem: since Linux doesn't
yet track more than 8 bits in the kernel, most open source shell authors
have no incentive to try and use newer interfaces; but until someone
actively complains that the newer interfaces are not following POSIX,
the kernel authors have no incentive to change the kernel process
information.  And even if shell authors did switch to waitid(), current
POSIX is vague enough to state that a shell's $? will reflect only the
lower 8 bits even if the shell were wired to use waitid() internally -
that is, there is no requirement that exit(256) be mapped to a non-zero
$? rather than the normal 0 you'd get from 8-bit truncation (although
there has at least been a discussion of whether a future version of
POSIX should add extensions to the shell to expose full 32-bit exit
information [1]).

[1] http://thread.gmane.org/gmane.comp.standards.posix.austin.general/11060

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]

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

end of thread, other threads:[~2015-07-14 21:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-09 23:30 Return codes over 1 byte Michael DePaulo
2015-07-09 23:42 ` Yaakov Selkowitz
2015-07-10  0:21   ` Michael DePaulo
2015-07-10  8:28     ` Corinna Vinschen
2015-07-14 21:26 ` Eric Blake

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