473,789 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
::GetExitCodePr ocess(GetCurren tProcess(),&exi tCode);
std::cout << exitCode << std::endl;
// prints 259, NOT 55
}
int _tmain(int argc, _TCHAR* argv[])
{
atexit(exithand ler);
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 11581
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)

iD8DBQBIIyucx9p 3GYHlUOIRAkxUAJ 4/VpibwEgIOqsi9Ts fPLyDkB9v9ACfW8 DQ
ZHb0Y7K8gzX9vGB HeX/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)

iD8DBQBIIy3px9p 3GYHlUOIRAvbAAJ 9CX7x7A2Crtjtcm A28fG0p0oL69wCe NRRz
6lEMq1JzIncf7vW PZL75tsA=
=+ccw
-----END PGP SIGNATURE-----

Jun 27 '08 #3
On May 8, 9:05 am, titanandrews <titanandr...@h otmail.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.comwr ote:
On May 8, 9:05 am, titanandrews <titanandr...@h otmail.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**********@h otmail.comwrote in news:774c45d4-97a5-43a9-
ae************* **@l42g2000hsc. googlegroups.co m:
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;
::GetExitCodePr ocess(GetCurren tProcess(),&exi tCode);
std::cout << exitCode << std::endl;
// prints 259, NOT 55
}
int _tmain(int argc, _TCHAR* argv[])
{
atexit(exithand ler);
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.comwr ote:
>On May 8, 9:05 am, titanandrews <titanandr...@h otmail.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.comwr ote:
On May 8, 9:05 am, titanandrews <titanandr...@h otmail.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...@h otmail.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;
* * ::GetExitCodePr ocess(GetCurren tProcess(),&exi tCode);
* * std::cout << exitCode << std::endl;
* * * * // prints 259, NOT 55

}

int _tmain(int argc, _TCHAR* argv[])
{
* * atexit(exithand ler);
* * 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.progr ammer.

Thanks,
Balaji.
Jun 27 '08 #9
In article <0593f31f-ef79-42c3-a425-
9c**********@b6 4g2000hsa.googl egroups.com>, ti**********@ho tmail.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
88323
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 function)
32
5893
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 the function: const long double& mean(long long x) { vector<int> v(x); for(int i= 1; i <=x; ++i) { v.push_back(i);
20
3615
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 exit(n)) statement provide both function call and return features of the C programming language? /* headers omitted */ int main (void)
19
11505
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, before terminating. I want it to terminate completely without having to press enter manually. Anybody know what I might be missing here ?
6
66510
by: Vicky | last post by:
Please tell me at vdkhakhkhar@gmail.com
7
6180
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 pretty ungraceful termination of my program. Someone likened the exit() to be like pulling the plug on an appliance when used within C++. Without changing each instance of exit to something else, I would like to change things so that my program...
1
2424
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 bash shell: $mount test mount: can't find /home/marty/test in /etc/fstab or /etc/mtab then I want to catch the same exception from the corresponding os.system() call, i.e. "os.system('mount test')", but it doesn't work as expected:
11
2849
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
2828
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' function into 'mymain', but I am stuck as to what I should do for the 'exit'. AFAIK there is no portable way to catch the exit. The only thing I can think of is atexit, but that does not work since 'exit' is still called afterward.
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5548
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4090
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.