473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 11517
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ivan Voras | last post by:
In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a...
32
by: Protoman | last post by:
I have a function that calculates the mean of the some numbers; I need it to accept any number of parameters (one needs to be the number of the other parameters) Can you help me out here? Here's...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
19
by: ern | last post by:
Right now I'm using exit(0) to terminate my program. My program is a console .exe application. After the "exit(0)" line of code is encountered, the console application waits for an enter press,...
6
by: Vicky | last post by:
Please tell me at vdkhakhkhar@gmail.com
7
by: chris | last post by:
I am writing a C++ program for which the guts of the program is about 100,000 lines of straight c code. This c code is peppered with hundreds of exit() statements which, unfortunately, cause a...
1
by: Marty | last post by:
I need to catch exceptions thrown by programs started by the os.system function, as indicated by a non-zero return code (e.g. the mount utility). For example, if I get the following results in a...
11
by: Rahul | last post by:
Hi Everyone, I have seen code in different styles like main(argc,argv) int argc; char **argv; { if(argc != 2) exit(1); else exit(0);
39
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main'...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.