public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* ExitProcess does not work in Cygwin?
@ 2022-01-13  6:39 Jay K
  2022-01-13 13:40 ` Eliot Moss
  0 siblings, 1 reply; 8+ messages in thread
From: Jay K @ 2022-01-13  6:39 UTC (permalink / raw)
  To: cygwin

ExitProcess does not work in Cygwin?

$ rm *.exe

# u is for Unix
# w is for Windows

$ cat u.c
#include <stdlib.h>
int main()
{
 exit(1);
}

$ gcc u.c
$ ./a.exe

$ echo $?
1

 => as expected

$ cat w.c
#include <windows.h>

int main()
{
 ExitProcess(1);
}

$ gcc w.c
$ ./a.exe

$ echo $?
0

 => not expected

$ uname -a
CYGWIN_NT-10.0 jayk-tp4 3.3.3(0.341/5/3) 2021-12-03 16:35 x86_64 Cygwin

works in debugger:

$ /cygdrive/c/bin/amd64/windbg.exe .\\a.exe

$ echo $?
1

?

 - Jay

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

* Re: ExitProcess does not work in Cygwin?
  2022-01-13  6:39 ExitProcess does not work in Cygwin? Jay K
@ 2022-01-13 13:40 ` Eliot Moss
  2022-01-13 17:07   ` Kaz Kylheku (Cygwin)
  0 siblings, 1 reply; 8+ messages in thread
From: Eliot Moss @ 2022-01-13 13:40 UTC (permalink / raw)
  To: Jay K, cygwin

On 1/13/2022 1:39 AM, Jay K wrote:
> ExitProcess does not work in Cygwin?
> 
> $ rm *.exe
> 
> # u is for Unix
> # w is for Windows
> 
> $ cat u.c
> #include <stdlib.h>
> int main()
> {
>   exit(1);
> }
> 
> $ gcc u.c
> $ ./a.exe
> 
> $ echo $?
> 1
> 
>   => as expected
> 
> $ cat w.c
> #include <windows.h>
> 
> int main()
> {
>   ExitProcess(1);
> }
> 
> $ gcc w.c
> $ ./a.exe
> 
> $ echo $?
> 0
> 
>   => not expected
> 
> $ uname -a
> CYGWIN_NT-10.0 jayk-tp4 3.3.3(0.341/5/3) 2021-12-03 16:35 x86_64 Cygwin
> 
> works in debugger:
> 
> $ /cygdrive/c/bin/amd64/windbg.exe .\\a.exe
> 
> $ echo $?
> 1
> 
> ?
> 
>   - Jay

ExitProcess does not appear to be a POSIX function.  Cygwin strives to provide a
POSIX-like interface, not a Windows-like one.  However, if ExitProcess is a Windows
function, there is probably a library you can use to obtain it in Cygwin (maybe
the winsup (Windows support) library).

Eliot Moss

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

* Re: ExitProcess does not work in Cygwin?
  2022-01-13 13:40 ` Eliot Moss
@ 2022-01-13 17:07   ` Kaz Kylheku (Cygwin)
  2022-01-13 17:19     ` Brian Inglis
  0 siblings, 1 reply; 8+ messages in thread
From: Kaz Kylheku (Cygwin) @ 2022-01-13 17:07 UTC (permalink / raw)
  To: moss; +Cc: Jay K, cygwin

On 2022-01-13 05:40, Eliot Moss wrote:
> On 1/13/2022 1:39 AM, Jay K wrote:
>> ExitProcess does not work in Cygwin?
> 
> ExitProcess does not appear to be a POSIX function.

This is a real issue worth looking into. Though ExitProcess isn't a 
POSIX
function, Cygwin can capture the termination status of non-Cygwin 
programs.

The concept of termination status cannot be entirely walled off in a
private Cygwin garden; it belongs to the underlying platform.

In Cygwin, I can do this:

   C:\Cygwin\cygwin64\home\kaz>exit 1
   1:BLACKBOX:~$
   1:BLACKBOX:~$ echo $?
   1
   0:BLACKBOX:~$ cmd
   Microsoft Windows [Version 10.0.19042.1052]
   (c) Microsoft Corporation. All rights reserved.

   C:\Cygwin\cygwin64\home\kaz>exit 0
   0:BLACKBOX:~$

The number in my Bash prompt is the last exit code. As you can see,
the non-Cygwin CMD.EXE program produces a termination code which
is recognized in the Cygwin world.

Most likely it does that via ExitProcess.

It is odd if calling ExitProcess in a Cygwin process causes
a Cygwin parent not to similarly process the status, as seems
to be shown by Jay's test cases.

Cygwin supports non-POSIX programming; you can write GUI applications
using Win32 calls for instance.

(Now I agree that for exiting your process, even if it's a GUI
application using numerous win32 calls, you should probably do it the
Cygwin way, and use exit, or return from main. But still ...)



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

* Re: ExitProcess does not work in Cygwin?
  2022-01-13 17:07   ` Kaz Kylheku (Cygwin)
@ 2022-01-13 17:19     ` Brian Inglis
  2022-01-13 23:15       ` Jay K
  0 siblings, 1 reply; 8+ messages in thread
From: Brian Inglis @ 2022-01-13 17:19 UTC (permalink / raw)
  To: cygwin; +Cc: Jay K

On 2022-01-13 10:07, Kaz Kylheku (Cygwin) wrote:
> On 2022-01-13 05:40, Eliot Moss wrote:
>> On 1/13/2022 1:39 AM, Jay K wrote:
>>> ExitProcess does not work in Cygwin?

Just use POSIX exit(3)!

>> ExitProcess does not appear to be a POSIX function.
> 
> This is a real issue worth looking into. Though ExitProcess isn't a POSIX
> function, Cygwin can capture the termination status of non-Cygwin programs.
> 
> The concept of termination status cannot be entirely walled off in a
> private Cygwin garden; it belongs to the underlying platform.
> 
> In Cygwin, I can do this:
> 
>    C:\Cygwin\cygwin64\home\kaz>exit 1
>    1:BLACKBOX:~$
>    1:BLACKBOX:~$ echo $?
>    1
>    0:BLACKBOX:~$ cmd
>    Microsoft Windows [Version 10.0.19042.1052]
>    (c) Microsoft Corporation. All rights reserved.
> 
>    C:\Cygwin\cygwin64\home\kaz>exit 0
>    0:BLACKBOX:~$
> 
> The number in my Bash prompt is the last exit code. As you can see,
> the non-Cygwin CMD.EXE program produces a termination code which
> is recognized in the Cygwin world.
> 
> Most likely it does that via ExitProcess.
> 
> It is odd if calling ExitProcess in a Cygwin process causes
> a Cygwin parent not to similarly process the status, as seems
> to be shown by Jay's test cases.
> 
> Cygwin supports non-POSIX programming; you can write GUI applications
> using Win32 calls for instance.
> 
> (Now I agree that for exiting your process, even if it's a GUI
> application using numerous win32 calls, you should probably do it the
> Cygwin way, and use exit, or return from main. But still ...)

Cygwin installs at-exit handlers and it is likely that when these have 
finished, they return a Cygwin exit status if passed by the POSIX 
function, perhaps unless some error has occurred during at-exit handling.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.
[Data in binary units and prefixes, physical quantities in SI.]

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

* Re: ExitProcess does not work in Cygwin?
  2022-01-13 17:19     ` Brian Inglis
@ 2022-01-13 23:15       ` Jay K
  2022-01-14  6:19         ` Brian Inglis
  2022-01-26 14:23         ` Andrey Repin
  0 siblings, 2 replies; 8+ messages in thread
From: Jay K @ 2022-01-13 23:15 UTC (permalink / raw)
  To: cygwin

 > Just use POSIX exit(3)!


I did switch my code:

#ifdef __CYGWIN__
 exit(x);
#else
 ExitProcess(x);
#endif
.

I think the problem is actually in how "Cygwin bash"
aka Cygwin, computes the exit code in exec/spawn/system.

I.e. it recognizes it is running a Cygwin exe or a native
exe and does things differently.

I admit I didn't read or debug much.

In one run I was debugging I did seem to see a crash
in some DllMain(process detach), without symbols, and then I seemed to see
ExitProcess(1 or 2) become ExitProcess(0xXX00) and
then I started wondering if Cygwin somewhere is only
taking the lower 8 bits, since I know that is a thing in some code.

But I didn't dig into this further before trying the simple case,
which I don't think crashes and really does NtTerminateProcess(1).

 - Jay

________________________________
From: Brian Inglis <Brian.Inglis@SystematicSw.ab.ca>
Sent: Thursday, January 13, 2022 5:19 PM
To: cygwin@cygwin.com <cygwin@cygwin.com>
Cc: Jay K <jayk123@hotmail.com>
Subject: Re: ExitProcess does not work in Cygwin?

On 2022-01-13 10:07, Kaz Kylheku (Cygwin) wrote:
> On 2022-01-13 05:40, Eliot Moss wrote:
>> On 1/13/2022 1:39 AM, Jay K wrote:
>>> ExitProcess does not work in Cygwin?

Just use POSIX exit(3)!

>> ExitProcess does not appear to be a POSIX function.
>
> This is a real issue worth looking into. Though ExitProcess isn't a POSIX
> function, Cygwin can capture the termination status of non-Cygwin programs.
>
> The concept of termination status cannot be entirely walled off in a
> private Cygwin garden; it belongs to the underlying platform.
>
> In Cygwin, I can do this:
>
>    C:\Cygwin\cygwin64\home\kaz>exit 1
>    1:BLACKBOX:~$
>    1:BLACKBOX:~$ echo $?
>    1
>    0:BLACKBOX:~$ cmd
>    Microsoft Windows [Version 10.0.19042.1052]
>    (c) Microsoft Corporation. All rights reserved.
>
>    C:\Cygwin\cygwin64\home\kaz>exit 0
>    0:BLACKBOX:~$
>
> The number in my Bash prompt is the last exit code. As you can see,
> the non-Cygwin CMD.EXE program produces a termination code which
> is recognized in the Cygwin world.
>
> Most likely it does that via ExitProcess.
>
> It is odd if calling ExitProcess in a Cygwin process causes
> a Cygwin parent not to similarly process the status, as seems
> to be shown by Jay's test cases.
>
> Cygwin supports non-POSIX programming; you can write GUI applications
> using Win32 calls for instance.
>
> (Now I agree that for exiting your process, even if it's a GUI
> application using numerous win32 calls, you should probably do it the
> Cygwin way, and use exit, or return from main. But still ...)

Cygwin installs at-exit handlers and it is likely that when these have
finished, they return a Cygwin exit status if passed by the POSIX
function, perhaps unless some error has occurred during at-exit handling.

--
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.
[Data in binary units and prefixes, physical quantities in SI.]

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

* Re: ExitProcess does not work in Cygwin?
  2022-01-13 23:15       ` Jay K
@ 2022-01-14  6:19         ` Brian Inglis
  2022-01-26 14:23         ` Andrey Repin
  1 sibling, 0 replies; 8+ messages in thread
From: Brian Inglis @ 2022-01-14  6:19 UTC (permalink / raw)
  To: cygwin

On 2022-01-13 16:15, Jay K wrote:
> On Thursday, January 13, 2022 5:19 PM, Brian Inglis wrote:
>> On 2022-01-13 10:07, Kaz Kylheku wrote:
>>> On 2022-01-13 05:40, Eliot Moss wrote:
>>>> On 1/13/2022 1:39 AM, Jay K wrote:

>>>>> ExitProcess does not work in Cygwin?

>> Just use POSIX exit(3)!

> I did switch my code:
> 
> #ifdef __CYGWIN__
>   exit(x);
> #else
>   ExitProcess(x);
> #endif
> .
> 
> I think the problem is actually in how "Cygwin bash"
> aka Cygwin, computes the exit code in exec/spawn/system.
> 
> i.e. it recognizes it is running a Cygwin exe or a native
> exe and does things differently.

...attempts to determine whether...

> I admit I didn't read or debug much.
> 
> In one run I was debugging I did seem to see a crash
> in some DllMain(process detach), without symbols, and then I seemed to see
> ExitProcess(1 or 2) become ExitProcess(0xXX00) and
> then I started wondering if Cygwin somewhere is only
> taking the lower 8 bits, since I know that is a thing in some code.

Cygwin follows POSIX and returns only the lower eight bits ORed with the 
child exit wait status flags (see wait(3p)) which may be tested with the 
macros in sys/wait.h (see sys_wait.h(0p)).

> But I didn't dig into this further before trying the simple case,
> which I don't think crashes and really does NtTerminateProcess(1).

> Just use POSIX exit(3)!

>>> ExitProcess does not appear to be a POSIX function.

>> This is a real issue worth looking into. Though ExitProcess isn't a POSIX
>> function, Cygwin can capture the termination status of non-Cygwin programs.
>>
>> The concept of termination status cannot be entirely walled off in a
>> private Cygwin garden; it belongs to the underlying platform.

You can do a lot of things under Cygwin, but only POSIX/Linux-like 
operation should be expected; some other things work, but may not be 
consistent, and are unlikely to be considered bugs e.g. using Windows 
paths, TTY line terminators, or more than 8 bit exit codes.

>> In Cygwin, I can do this:
>>
>>     C:\Cygwin\cygwin64\home\kaz>exit 1
>>     1:BLACKBOX:~$
>>     1:BLACKBOX:~$ echo $?
>>     1
>>     0:BLACKBOX:~$ cmd
>>     Microsoft Windows [Version 10.0.19042.1052]
>>     (c) Microsoft Corporation. All rights reserved.
>>
>>     C:\Cygwin\cygwin64\home\kaz>exit 0
>>     0:BLACKBOX:~$
>>
>> The number in my Bash prompt is the last exit code. As you can see,
>> the non-Cygwin CMD.EXE program produces a termination code which
>> is recognized in the Cygwin world.
>>
>> Most likely it does that via ExitProcess.
>>
>> It is odd if calling ExitProcess in a Cygwin process causes
>> a Cygwin parent not to similarly process the status, as seems
>> to be shown by Jay's test cases.
>>
>> Cygwin supports non-POSIX programming; you can write GUI applications
>> using Win32 calls for instance.

GUI e.g. Cygwin Setup program setup-x86/_64 and also Windows console 
apps e.g. cygcheck, cygwin-console-helper, ldh, strace, but those are 
all built with {i686,x86_64}-w64-mingw32-g++ and linked to MS msvcrt.dll 
*NOT* Cygwin cygwin1.dll startup and runtime.

The default terminal console app mintty is a hybrid case.

>> (Now I agree that for exiting your process, even if it's a GUI
>> application using numerous win32 calls, you should probably do it the
>> Cygwin way, and use exit, or return from main. But still ...)
> 
> Cygwin installs at-exit handlers and it is likely that when these have
> finished, they return a Cygwin exit status if passed by the POSIX
> function, perhaps unless some error has occurred during at-exit handling.

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.
[Data in binary units and prefixes, physical quantities in SI.]

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

* Re: ExitProcess does not work in Cygwin?
  2022-01-13 23:15       ` Jay K
  2022-01-14  6:19         ` Brian Inglis
@ 2022-01-26 14:23         ` Andrey Repin
  2022-01-26 17:06           ` Jay K
  1 sibling, 1 reply; 8+ messages in thread
From: Andrey Repin @ 2022-01-26 14:23 UTC (permalink / raw)
  To: Jay K, cygwin

Greetings, Jay K!

 >> Just use POSIX exit(3)!


> I did switch my code:

> #ifdef __CYGWIN__
>  exit(x);
> #else
>  ExitProcess(x);
> #endif
> .

It is best to avoid Cygwin-specific code.
Use either Linux(POSIX) or Windows specific tests, and only test for Cygwin if
/absolutely/ necessary.


-- 
With best regards,
Andrey Repin
Wednesday, January 26, 2022 17:22:01

Sorry for my terrible english...


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

* Re: ExitProcess does not work in Cygwin?
  2022-01-26 14:23         ` Andrey Repin
@ 2022-01-26 17:06           ` Jay K
  0 siblings, 0 replies; 8+ messages in thread
From: Jay K @ 2022-01-26 17:06 UTC (permalink / raw)
  To: cygwin

 > It is best to avoid Cygwin-specific code.

Agreed.
The code is mostly only ifdef win32 vs. Posix but just a few spots are Cygwin-specific.
(and Apple specific, Solaris specific, IA64-specific etc.)

 - Jay

________________________________
From: Andrey Repin <anrdaemon@yandex.ru>
Sent: Wednesday, January 26, 2022 2:23 PM
To: Jay K <jayk123@hotmail.com>; cygwin@cygwin.com <cygwin@cygwin.com>
Subject: Re: ExitProcess does not work in Cygwin?

Greetings, Jay K!

 >> Just use POSIX exit(3)!


> I did switch my code:

> #ifdef __CYGWIN__
>  exit(x);
> #else
>  ExitProcess(x);
> #endif
> .

It is best to avoid Cygwin-specific code.
Use either Linux(POSIX) or Windows specific tests, and only test for Cygwin if
/absolutely/ necessary.


--
With best regards,
Andrey Repin
Wednesday, January 26, 2022 17:22:01

Sorry for my terrible english...


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

end of thread, other threads:[~2022-01-26 17:06 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-13  6:39 ExitProcess does not work in Cygwin? Jay K
2022-01-13 13:40 ` Eliot Moss
2022-01-13 17:07   ` Kaz Kylheku (Cygwin)
2022-01-13 17:19     ` Brian Inglis
2022-01-13 23:15       ` Jay K
2022-01-14  6:19         ` Brian Inglis
2022-01-26 14:23         ` Andrey Repin
2022-01-26 17:06           ` Jay K

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