Catching exit code before program termination

T

titanandrews

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

Martin York

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 $?
 
T

titanandrews

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. :(
 
J

Joe Greer

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
 
R

Rolf Magnus

titanandrews said:
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?
 
T

titanandrews

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

kasthurirangan.balaji

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

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top