public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* windows exit codes truncated to 1 Byte
       [not found] <1233154500.205789.1459429282664.JavaMail.yahoo.ref@mail.yahoo.com>
@ 2016-03-31 13:04 ` Cristian
  2016-03-31 13:31   ` Björn Stabel
  0 siblings, 1 reply; 5+ messages in thread
From: Cristian @ 2016-03-31 13:04 UTC (permalink / raw)
  To: cygwin

I noted that exit codes returned by CMD and other apps (msiexec) are truncated to 1 byte (0 .. 255).

Steps to reproduce:

1) Create a batch with this content:
$ cat ./test.bat 
@echo off 
rem ==================== 
echo Test exit code 266 
exit /B 266 
rem ==================== 

2) run the batch file:
$ ./test.bat 
Test exit code 266 

3) check exit code
$ echo $? 
10 


I would expect 266 but I got 10 instead. I checked for other codes and the result is the same. 
Windows exit codes are represented using 32 bits, so is this limitation intentional?


Best regards,
Cristi

--
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: windows exit codes truncated to 1 Byte
  2016-03-31 13:04 ` windows exit codes truncated to 1 Byte Cristian
@ 2016-03-31 13:31   ` Björn Stabel
  2016-03-31 15:24     ` Eric Blake
  0 siblings, 1 reply; 5+ messages in thread
From: Björn Stabel @ 2016-03-31 13:31 UTC (permalink / raw)
  To: cygwin

Am 31.03.2016 um 15:01 schrieb Cristian:
> I noted that exit codes returned by CMD and other apps (msiexec) are truncated to 1 byte (0 .. 255).
>
> Steps to reproduce:
>
> 1) Create a batch with this content:
> $ cat ./test.bat 
> @echo off 
> rem ==================== 
> echo Test exit code 266 
> exit /B 266 
> rem ==================== 
>
> 2) run the batch file:
> $ ./test.bat 
> Test exit code 266 
>
> 3) check exit code
> $ echo $? 
> 10 
>
>
> I would expect 266 but I got 10 instead. I checked for other codes and the result is the same. 
> Windows exit codes are represented using 32 bits, so is this limitation intentional?
POSIX specifies that the lower 8 bits of the exit status be made
available to the caller of wait/waitpid:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html

"WEXITSTATUS(stat_val) - If the value of WIFEXITED(stat_val) is
non-zero, this macro evaluates to the low-order 8 bits of the status
argument that the child process passed to _exit() or exit(), or the
value the child process returned from main()."

Maybe there's another way to get the entire status value, but this looks
like a POSIX limitation to me.
Cygwin is conformant here.

The more interesting problem is what happens if you do "exit /B 256" in
your batch.
In that case, bash would show a return status of zero.
That's what the WIFEXITED macro is used for. Not sure how you'd get that
from bash, though.

>
>
> Best regards,
> Cristi
>
> --
> 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
>
>


--
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: windows exit codes truncated to 1 Byte
  2016-03-31 13:31   ` Björn Stabel
@ 2016-03-31 15:24     ` Eric Blake
  2016-03-31 18:50       ` Corinna Vinschen
  0 siblings, 1 reply; 5+ messages in thread
From: Eric Blake @ 2016-03-31 15:24 UTC (permalink / raw)
  To: cygwin

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

On 03/31/2016 07:30 AM, Björn Stabel wrote:
> Am 31.03.2016 um 15:01 schrieb Cristian:
>> I noted that exit codes returned by CMD and other apps (msiexec) are truncated to 1 byte (0 .. 255).

This matches Linux, but not POSIX.

>> Windows exit codes are represented using 32 bits, so is this limitation intentional?

Part intentional (we do what Linux does), and part accidental (Cygwin
handles the return status slightly different than Windows, and that
handling makes it impossible to easily preserve all 32-bits, even if
Linux were to be patched to obey POSIX).

> POSIX specifies that the lower 8 bits of the exit status be made
> available to the caller of wait/waitpid:
> http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html

Correct: wait() and waitpid() can only return 8 bits; but POSIX states
that waitid() MUST be able to access all 32-bits untruncated; this is
where Linux gets it wrong (Linux truncates during exit(), so waitid()
can only return 8 bits; other operating systems do NOT truncate during
exit, but preserve all 32 bits; if you use wait() or waitpid() you only
get 8 bits, but if you use waitid() you can see the full 32 bits).

> Maybe there's another way to get the entire status value, but this looks
> like a POSIX limitation to me.
> Cygwin is conformant here.

Cygwin is only partially conformant here.

See also: http://austingroupbugs.net/view.php?id=947

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

* Re: windows exit codes truncated to 1 Byte
  2016-03-31 15:24     ` Eric Blake
@ 2016-03-31 18:50       ` Corinna Vinschen
  2016-03-31 18:58         ` Eric Blake
  0 siblings, 1 reply; 5+ messages in thread
From: Corinna Vinschen @ 2016-03-31 18:50 UTC (permalink / raw)
  To: cygwin

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

On Mar 31 09:24, Eric Blake wrote:
> On 03/31/2016 07:30 AM, Björn Stabel wrote:
> > Am 31.03.2016 um 15:01 schrieb Cristian:
> >> I noted that exit codes returned by CMD and other apps (msiexec) are truncated to 1 byte (0 .. 255).
> 
> This matches Linux, but not POSIX.

Why?  Does bash use waitid?

> >> Windows exit codes are represented using 32 bits, so is this limitation intentional?
> 
> Part intentional (we do what Linux does), and part accidental (Cygwin
> handles the return status slightly different than Windows, and that
> handling makes it impossible to easily preserve all 32-bits, even if
> Linux were to be patched to obey POSIX).

I don't quite understand this.  Right now the exitcode value is
truncated to 8 bits to fit into the 32 bit status value as defined
for wait/waitpid, but this is not set in stone.  We could change
the communication between parent and child to allow preserving the
full 32 bit value for waitid.

But...

> > POSIX specifies that the lower 8 bits of the exit status be made
> > available to the caller of wait/waitpid:
> > http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
> 
> Correct: wait() and waitpid() can only return 8 bits; but POSIX states
> that waitid() MUST be able to access all 32-bits untruncated; this is
> where Linux gets it wrong (Linux truncates during exit(), so waitid()
> can only return 8 bits; other operating systems do NOT truncate during
> exit, but preserve all 32 bits; if you use wait() or waitpid() you only
> get 8 bits, but if you use waitid() you can see the full 32 bits).

...that only makes sense if we implement waitid at all.  SHTDI and all
that...


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: windows exit codes truncated to 1 Byte
  2016-03-31 18:50       ` Corinna Vinschen
@ 2016-03-31 18:58         ` Eric Blake
  0 siblings, 0 replies; 5+ messages in thread
From: Eric Blake @ 2016-03-31 18:58 UTC (permalink / raw)
  To: cygwin

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

On 03/31/2016 12:50 PM, Corinna Vinschen wrote:
> On Mar 31 09:24, Eric Blake wrote:
>> On 03/31/2016 07:30 AM, Björn Stabel wrote:
>>> Am 31.03.2016 um 15:01 schrieb Cristian:
>>>> I noted that exit codes returned by CMD and other apps (msiexec) are truncated to 1 byte (0 .. 255).
>>
>> This matches Linux, but not POSIX.
> 
> Why?  Does bash use waitid?

Not yet, but with the link I referred to below, the thought is that
eventually bash WILL use waitid(), based on the direction POSIX is
headed in.

> But...
> 
>>> POSIX specifies that the lower 8 bits of the exit status be made
>>> available to the caller of wait/waitpid:
>>> http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html
>>
>> Correct: wait() and waitpid() can only return 8 bits; but POSIX states
>> that waitid() MUST be able to access all 32-bits untruncated; this is
>> where Linux gets it wrong (Linux truncates during exit(), so waitid()
>> can only return 8 bits; other operating systems do NOT truncate during
>> exit, but preserve all 32 bits; if you use wait() or waitpid() you only
>> get 8 bits, but if you use waitid() you can see the full 32 bits).
> 
> ...that only makes sense if we implement waitid at all.  SHTDI and all
> that...

Right - without waitid(), it's fairly obvious that cygwin isn't quite up
to POSIX compliance yet :)  And indeed, patches are welcome, if someone
who cares enough about more than 8 bits wants to step up and write it
(personally, I don't care enough as long as Linux is still truncating
early during exit()).

-- 
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:[~2016-03-31 18:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1233154500.205789.1459429282664.JavaMail.yahoo.ref@mail.yahoo.com>
2016-03-31 13:04 ` windows exit codes truncated to 1 Byte Cristian
2016-03-31 13:31   ` Björn Stabel
2016-03-31 15:24     ` Eric Blake
2016-03-31 18:50       ` Corinna Vinschen
2016-03-31 18:58         ` 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).