469,579 Members | 1,098 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

Catching exit code before program termination

Hi All,

Is there any way to catch the exit code from someone calling
exit(status) and check the value before the program terminates?

Just for an idea, the code I'm thinking of is something like this:

void exithandler()
{
DWORD exitCode = 0;
::GetExitCodeProcess(GetCurrentProcess(),&exitCode );
std::cout << exitCode << std::endl;
// prints 259, NOT 55
}
int _tmain(int argc, _TCHAR* argv[])
{
atexit(exithandler);
exit(55);
return 0;
}

This is for Windows, but I am looking for a solution for UNIX/Linux.
BTW... this solution does NOT work because the process has not yet
terminated when my exit handler is called, thus resulting in exitCode
of 259 instead of 55. 259 on Windows means the process is still
active.

Does anyone know if this can be done?
Use case: Imagine you have a library and you want to check if someone
is calling exit with a particular code.
Jun 27 '08 #1
9 10891
Sam
titanandrews writes:
This is for Windows, but I am looking for a solution for UNIX/Linux.
TMK, POSIX does not provide a means for an atexit handler to receive the
exit status parameter that was passed to exit().

If I feel especially motivated, I'd go take a peek at glibc sources, and see
what's going on, and if glibc gives you a mechanism for this. Given that
your question concerns Linux, you should acquire the habit of grabbing the
sources and looking at these kinds of things yourself, if you want to know
how they work. I understand that, coming from the Windows noosphere, this
may be an exotic experience. But, the answer to these kinds of questions can
be located much faster just by looking at the corresponding source code,
rather than waiting for someone to respond on a Usenet group.
Use case: Imagine you have a library and you want to check if someone
is calling exit with a particular code.
A shared library on Linux can be unloaded by the running process. The
library gets unloaded, and the process continues to run (without the benefit
of the library's functionality, of course). If your code requires this, it's
time for you to redesign whatever you're trying to design.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIIyucx9p3GYHlUOIRAkxUAJ4/VpibwEgIOqsi9TsfPLyDkB9v9ACfW8DQ
ZHb0Y7K8gzX9vGBHeX/HEGU=
=9d10
-----END PGP SIGNATURE-----

Jun 27 '08 #2
Sam
Sam writes:
titanandrews writes:
>This is for Windows, but I am looking for a solution for UNIX/Linux.

TMK, POSIX does not provide a means for an atexit handler to receive the
exit status parameter that was passed to exit().
Following to myself -- I do see that Linux glibc has the nonstandard
on_exit(), that can be used to install a handler that receives the pending
exit code, but, as the man page indicates, this is a legacy thing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIIy3px9p3GYHlUOIRAvbAAJ9CX7x7A2CrtjtcmA28fG 0p0oL69wCeNRRz
6lEMq1JzIncf7vWPZL75tsA=
=+ccw
-----END PGP SIGNATURE-----

Jun 27 '08 #3
On May 8, 9:05 am, titanandrews <titanandr...@hotmail.comwrote:
Does anyone know if this can be done?
Yes. But not that way.
If you bring up cmd.exe

run the application in cmd.exe
Then you can print the exit code via the variabel %ERRORLEVEL%

<command>
echo %ERRORLEVEL%
If you want to do it automatically just wrap it all in a small script
that runs the code and then prints %ERRORLEVEL%

On Unix/Linux systems you can do the same thing.
The exact code depends on what shell you are using:

In tcsh
../<COMMAND>
echo $?
Jun 27 '08 #4
On May 8, 2:30 pm, Martin York <Martin.YorkAma...@gmail.comwrote:
On May 8, 9:05 am, titanandrews <titanandr...@hotmail.comwrote:
Does anyone know if this can be done?

Yes. But not that way.
If you bring up cmd.exe

run the application in cmd.exe
Then you can print the exit code via the variabel %ERRORLEVEL%

<command>
echo %ERRORLEVEL%

If you want to do it automatically just wrap it all in a small script
that runs the code and then prints %ERRORLEVEL%

On Unix/Linux systems you can do the same thing.
The exact code depends on what shell you are using:

In tcsh
./<COMMAND>
echo $?
Thanks. But thats after the process has ended. By then, it's too
late. :(
Jun 27 '08 #5
titanandrews <ti**********@hotmail.comwrote in news:774c45d4-97a5-43a9-
ae***************@l42g2000hsc.googlegroups.com:
Hi All,

Is there any way to catch the exit code from someone calling
exit(status) and check the value before the program terminates?

Just for an idea, the code I'm thinking of is something like this:

void exithandler()
{
DWORD exitCode = 0;
::GetExitCodeProcess(GetCurrentProcess(),&exitCode );
std::cout << exitCode << std::endl;
// prints 259, NOT 55
}
int _tmain(int argc, _TCHAR* argv[])
{
atexit(exithandler);
exit(55);
return 0;
}

This is for Windows, but I am looking for a solution for UNIX/Linux.
BTW... this solution does NOT work because the process has not yet
terminated when my exit handler is called, thus resulting in exitCode
of 259 instead of 55. 259 on Windows means the process is still
active.

Does anyone know if this can be done?
Use case: Imagine you have a library and you want to check if someone
is calling exit with a particular code.
atexit() can be used to register a function which will be called as part of
the exit() processing (but not the abort() processing), I don't believe
there is a standard way to get the status set by exit() though.

joe
Jun 27 '08 #6
titanandrews wrote:
On May 8, 2:30 pm, Martin York <Martin.YorkAma...@gmail.comwrote:
>On May 8, 9:05 am, titanandrews <titanandr...@hotmail.comwrote:
Does anyone know if this can be done?

Yes. But not that way.
If you bring up cmd.exe

run the application in cmd.exe
Then you can print the exit code via the variabel %ERRORLEVEL%

<command>
echo %ERRORLEVEL%

If you want to do it automatically just wrap it all in a small script
that runs the code and then prints %ERRORLEVEL%

On Unix/Linux systems you can do the same thing.
The exact code depends on what shell you are using:

In tcsh
./<COMMAND>
echo $?

Thanks. But thats after the process has ended. By then, it's too
late. :(
I don't understand that. The process has no exit code before it has ended,
so how would you expect to be able to get one?

Jun 27 '08 #7
On May 8, 5:28 pm, Rolf Magnus <ramag...@t-online.dewrote:
titanandrews wrote:
On May 8, 2:30 pm, Martin York <Martin.YorkAma...@gmail.comwrote:
On May 8, 9:05 am, titanandrews <titanandr...@hotmail.comwrote:
Does anyone know if this can be done?
Yes. But not that way.
If you bring up cmd.exe
run the application in cmd.exe
Then you can print the exit code via the variabel %ERRORLEVEL%
<command>
echo %ERRORLEVEL%
If you want to do it automatically just wrap it all in a small script
that runs the code and then prints %ERRORLEVEL%
On Unix/Linux systems you can do the same thing.
The exact code depends on what shell you are using:
In tcsh
./<COMMAND>
echo $?
Thanks. But thats after the process has ended. By then, it's too
late. :(

I don't understand that. The process has no exit code before it has ended,
so how would you expect to be able to get one?
Well the exit code that was used for the exit function call is stored
in memory somewhere. The exit function doesn't just immediately
terminate the program. A whole bunch of other stuff happens first,
i.e. resources freed, exit handlers called, etc.
That variable has to be stored somewhere so that you end up with value
1 when the process finally ends.
Jun 27 '08 #8
On May 8, 9:05*pm, titanandrews <titanandr...@hotmail.comwrote:
Hi All,

Is there any way to catch the exit code from someone calling
exit(status) and check the value before the program terminates?

Just for an idea, the code I'm thinking of is something like this:

void exithandler()
{
* * DWORD exitCode = 0;
* * ::GetExitCodeProcess(GetCurrentProcess(),&exitCode );
* * std::cout << exitCode << std::endl;
* * * * // prints 259, NOT 55

}

int _tmain(int argc, _TCHAR* argv[])
{
* * atexit(exithandler);
* * exit(55);
* * return 0;

}

This is for Windows, but I am looking for a solution for UNIX/Linux.
BTW... this solution does NOT work because the process has not yet
terminated when my exit handler is called, thus resulting in exitCode
of 259 instead of 55. 259 on Windows means the process is still
active.

Does anyone know if this can be done?
Use case: Imagine you have a library and you want to check if someone
is calling exit with a particular code.
Your post looks like OS/platform specific and bit OT here. Still i
would like to share my ideas.

As others have suggested, you may have a wrapper script and use $? to
find the exit code. But if you would like to do it using a program,
then the way i think is you need to use fork() and have control of the
child process. Whenever the child exits, you need to use waitpid to
know about the child exit status. The child details are stored in /
proc(may differ on platforms) as long as the child is alive.

If you like to know more, i would suggest you reading
Advance programming in the Unix environment - Richard W Stevens.

Of course, based on your interest, you may go thru all books by
Richard W Stevens - my guru. Alternately, you may even refer
comp.unix.programmer.

Thanks,
Balaji.
Jun 27 '08 #9
In article <0593f31f-ef79-42c3-a425-
9c**********@b64g2000hsa.googlegroups.com>, ti**********@hotmail.com
says...

[ ... ]
Well the exit code that was used for the exit function call is stored
in memory somewhere. The exit function doesn't just immediately
terminate the program. A whole bunch of other stuff happens first,
i.e. resources freed, exit handlers called, etc.
That variable has to be stored somewhere so that you end up with value
1 when the process finally ends.
Not necessarily -- the exit code is just the return value from main, and
the return value is stored in a register on many architectures.
Depending on how much else needs to be done, there might be code to
store it somewhere else temporarily as you suggest -- but if so, there's
no standard place to do so (though I'd imagine that somewhere close to
top of the stack (at that time) would be fairly common.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Ivan Voras | last post: by
20 posts views Thread by lovecreatesbeauty | last post: by
19 posts views Thread by ern | last post: by
6 posts views Thread by Vicky | last post: by
7 posts views Thread by chris | last post: by
1 post views Thread by Marty | last post: by
11 posts views Thread by Rahul | last post: by
39 posts views Thread by mathieu | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.