[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

catching exit

mathieu

8/13/2008 5:13:00 PM

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.

What I am thinking now is that I need to replace all exit(val) with
longjmp(env, val). And make 'env' global.

Comments ?

Thanks
-Mathieu
44 Answers

Eric Sosman

8/13/2008 6:17:00 PM

0

mathieu wrote:
> 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.
>
> What I am thinking now is that I need to replace all exit(val) with
> longjmp(env, val). And make 'env' global.
>
> Comments ?

If you can change the source of the application, you can replace
exit() calls with myexit() calls and then do what you like, including
calling longjmp() to transfer back to a "wrapper." But that's not
likely to solve everything, unless the application has already cleaned
itself up before calling myexit(). If it hasn't, there may still be
open files and un-free()d memory hanging around ...

If the application uses atexit(), you're going to have to decide
what to do about the functions it's registered and that it thinks are
going to run when it calls exit(). Should they run even though the
application isn't really exiting? Should they run only when the whole
wrapped program exits? Either way, you've got some work to do: You'll
probably need a myatexit() that registers functions that myexit() can
run (and de-register), or a myatexit() that recognizes double-registered
functions when the wrapped application re-runs and suppresses the extra
calls to atexit(). (This carries some risk of changing the semantics,
by the way: exit() runs the functions in reverse order of registration,
so if the application registers f() and then g() the first time, but
g() and then f() the second, it's not clear what should happen.)

Yet another thing to beware of is "run-once" code that initializes
something the first time a function is called. These things often
test a local static variable inside the function to see whether this
is the first call, and your problem is to figure out which of these
should really be "run-once" and which should be "run-once per cycle,"
and to make arrangements to reset the triggers of the latter.

... and there's probably some other gotchas I haven't thought of.
This has the potential to become a very messy job indeed.

One alternative is to rewrite the code, keeping in mind that it
will be invoked multiple times in the new framework and writing
accordingly. This might be a lot of work, but it will be less work
than writing the code from scratch took (because you can study the
old code while you write the new), and it might turn out to be less
work than trying to hack the exit() calls.

Another possibility is to leave the code as it stands, and run
it as a separate program. You could use the system() function to
launch the application and wait for it to finish, possibly preparing
input files for it first and reading its output files afterward.

Changing a program's "framework" is seldom easy. Good luck!

--
Eric.Sosman@sun.com

CBFalconer

8/13/2008 9:34:00 PM

0

mathieu wrote:
>
> 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.
>
> What I am thinking now is that I need to replace all exit(val)
> with longjmp(env, val). And make 'env' global.

Just leave it calling exit. The result is as follows:

7.20.4.3 The exit function

Synopsis
[#1]
#include <stdlib.h>
void exit(int status);

Description

[#2] The exit function causes normal program termination to
occur. If more than one call to the exit function is
executed by a program, the behavior is undefined.

[#3] First, all functions registered by the atexit function
are called, in the reverse order of their registration.239)

[#4] Next, all open streams with unwritten buffered data are
flushed, all open streams are closed, and all files created
by the tmpfile function are removed.

[#5] Finally, control is returned to the host environment.
If the value of status is zero or EXIT_SUCCESS, an
implementation-defined form of the status successful
termination is returned. If the value of status is
EXIT_FAILURE, an implementation-defined form of the status
unsuccessful termination is returned. Otherwise the status
returned is implementation-defined.

Returns
[#6] The exit function cannot return to its caller.

____________________

239Each function is called as many times as it was
registered.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.


John B. Matthews

8/13/2008 10:18:00 PM

0

On 13 Aug 2008 at 21:33, CBFalconer wrote:
> mathieu wrote:
>> 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'.
>>
> Just leave it calling exit.

Brilliant.

Remind me never to use any library you write.

Oh, yes, hell would have frozen over first anyway, even without this
latest idiotic post.

Keith Thompson

8/13/2008 11:00:00 PM

0

CBFalconer <cbfalconer@yahoo.com> writes:
> mathieu wrote:
>> 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.
>>
>> What I am thinking now is that I need to replace all exit(val)
>> with longjmp(env, val). And make 'env' global.
>
> Just leave it calling exit. The result is as follows:
>
> 7.20.4.3 The exit function
[C standard's description of the exit function snipped]

And how does this answer the OP's question?

He has a main program; a call to exit terminates the program.

He wants to turn this main program into a function within a larger
program. Wherever there's a call to exit, he wants to terminate the
function, not the entire (now larger) program.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer

8/14/2008 12:10:00 AM

0

Keith Thompson wrote:
> CBFalconer <cbfalconer@yahoo.com> writes:
>> mathieu wrote:
>>
>>> 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.
>>>
>>> What I am thinking now is that I need to replace all exit(val)
>>> with longjmp(env, val). And make 'env' global.
>>
>> Just leave it calling exit. The result is as follows:
>>
>> 7.20.4.3 The exit function
> [C standard's description of the exit function snipped]
>
> And how does this answer the OP's question?
>
> He has a main program; a call to exit terminates the program.
>
> He wants to turn this main program into a function within a larger
> program. Wherever there's a call to exit, he wants to terminate the
> function, not the entire (now larger) program.

If that's what he wants to do that is another case. But the
previous effect was to terminate the program, whch is what exit
does.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.a...
Try the download section.

mathieu

8/14/2008 7:00:00 AM

0

On Aug 13, 8:17 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
> mathieu wrote:
> > 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.
>
> > What I am thinking now is that I need to replace all exit(val) with
> > longjmp(env, val). And make 'env' global.
>
> > Comments ?
>
> If you can change the source of the application, you can replace
> exit() calls with myexit() calls and then do what you like, including
> calling longjmp() to transfer back to a "wrapper." But that's not
> likely to solve everything, unless the application has already cleaned
> itself up before calling myexit(). If it hasn't, there may still be
> open files and un-free()d memory hanging around ...
>
> If the application uses atexit(), you're going to have to decide
> what to do about the functions it's registered and that it thinks are
> going to run when it calls exit(). Should they run even though the
> application isn't really exiting? Should they run only when the whole
> wrapped program exits? Either way, you've got some work to do: You'll
> probably need a myatexit() that registers functions that myexit() can
> run (and de-register), or a myatexit() that recognizes double-registered
> functions when the wrapped application re-runs and suppresses the extra
> calls to atexit(). (This carries some risk of changing the semantics,
> by the way: exit() runs the functions in reverse order of registration,
> so if the application registers f() and then g() the first time, but
> g() and then f() the second, it's not clear what should happen.)
>
> Yet another thing to beware of is "run-once" code that initializes
> something the first time a function is called. These things often
> test a local static variable inside the function to see whether this
> is the first call, and your problem is to figure out which of these
> should really be "run-once" and which should be "run-once per cycle,"
> and to make arrangements to reset the triggers of the latter.
>
> ... and there's probably some other gotchas I haven't thought of.
> This has the potential to become a very messy job indeed.
>
> One alternative is to rewrite the code, keeping in mind that it
> will be invoked multiple times in the new framework and writing
> accordingly. This might be a lot of work, but it will be less work
> than writing the code from scratch took (because you can study the
> old code while you write the new), and it might turn out to be less
> work than trying to hack the exit() calls.
>
> Another possibility is to leave the code as it stands, and run
> it as a separate program. You could use the system() function to
> launch the application and wait for it to finish, possibly preparing
> input files for it first and reading its output files afterward.
>
> Changing a program's "framework" is seldom easy. Good luck!

Hi Eric,

Thanks for your input, esp. the issue with globals. This is
something I have not thought about.

As a first step, I'll replace exit with myexit+longjmp. Then as a
second step, I'll rewrite the portion that is doing the initialization
of the globals (within a func). Calling this init func will simply
reproduce the old behavior. Hopefully the library is simple enough so
that I can get rid of all leaks quickly (thanks to valgrind).

Thanks, that was very helpful !
-Mathieu

mathieu

8/14/2008 7:00:00 AM

0

On Aug 13, 11:33 pm, CBFalconer <cbfalco...@yahoo.com> wrote:
> mathieu wrote:
>
> > 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.
>
> > What I am thinking now is that I need to replace all exit(val)
> > with longjmp(env, val). And make 'env' global.
>
> Just leave it calling exit. The result is as follows:
>
> 7.20.4.3 The exit function
>
> Synopsis
> [#1]
> #include <stdlib.h>
> void exit(int status);
>
> Description
>
> [#2] The exit function causes normal program termination to
> occur. If more than one call to the exit function is
> executed by a program, the behavior is undefined.
>
> [#3] First, all functions registered by the atexit function
> are called, in the reverse order of their registration.239)
>
> [#4] Next, all open streams with unwritten buffered data are
> flushed, all open streams are closed, and all files created
> by the tmpfile function are removed.
>
> [#5] Finally, control is returned to the host environment.
> If the value of status is zero or EXIT_SUCCESS, an
> implementation-defined form of the status successful
> termination is returned. If the value of status is
> EXIT_FAILURE, an implementation-defined form of the status
> unsuccessful termination is returned. Otherwise the status
> returned is implementation-defined.
>
> Returns
> [#6] The exit function cannot return to its caller.
>
> ____________________
>
> 239Each function is called as many times as it was
> registered.


Thanks I also know how to read the man page of exit :)

-M

Keith Thompson

8/14/2008 7:02:00 AM

0

CBFalconer <cbfalconer@yahoo.com> writes:
> Keith Thompson wrote:
>> CBFalconer <cbfalconer@yahoo.com> writes:
>>> mathieu wrote:
>>>
>>>> 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.
>>>>
>>>> What I am thinking now is that I need to replace all exit(val)
>>>> with longjmp(env, val). And make 'env' global.
>>>
>>> Just leave it calling exit. The result is as follows:
>>>
>>> 7.20.4.3 The exit function
>> [C standard's description of the exit function snipped]
>>
>> And how does this answer the OP's question?
>>
>> He has a main program; a call to exit terminates the program.
>>
>> He wants to turn this main program into a function within a larger
>> program. Wherever there's a call to exit, he wants to terminate the
>> function, not the entire (now larger) program.
>
> If that's what he wants to do that is another case. But the
> previous effect was to terminate the program, whch is what exit
> does.

He knew that.

If he wanted the existing exit calls to terminate the program, why
would he have asked what to do about them?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Richard Heathfield

8/14/2008 7:32:00 AM

0

CBFalconer said:

> Keith Thompson wrote:
<snip>
>>
>> He has a main program; a call to exit terminates the program.
>>
>> He wants to turn this main program into a function within a larger
>> program. Wherever there's a call to exit, he wants to terminate the
>> function, not the entire (now larger) program.
>
> If that's what he wants to do that is another case.

No, it's the original question. To avoid making such mistakes in future, I
recommend that you save up your replies for an hour or so, and then:

(1) Re-read the original question, and then re-read your original reply. At
this stage, it will sometimes become clear that your reply was based on a
mis-reading of the question which your second reading makes obvious.
Solution: delete your reply without sending it.

(2) By this time, there may well be one or two other replies to the
question, by people whose opinion you respect. Read them.

If they are, in essence, the same as your own reply, delete your reply
without sending it, since it would be pointlessly duplicative (unless your
reply adds something useful that was overlooked by the others).

If they are *not* in essence the same as your reply, consider seriously the
possibility that you have mis-read the question. Read their replies, and
the original question, again, and find out why they have replied as they
did. If you genuinely interpret the question differently to them, then
edit your reply so that it says something like "I've read A's and B's
replies, and they've assumed you meant such-and-such. If so, fine. But it
seems to me that you might mean /this/ instead. If so, then my advice
is..." Then, having changed your reply, save it up for an hour or so, and
then start back at (1) again.

(3) Now send any undeleted replies.

I suggest that using this strategy could save you from a huge amount of
embarrassment.

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999

Richard Heathfield

8/14/2008 7:56:00 AM

0

mathieu said:

> 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.
>
> What I am thinking now is that I need to replace all exit(val) with
> longjmp(env, val). And make 'env' global.
>
> Comments ?

Eric has already given you some excellent advice, and there is no point in
duplicating it, obviously.

My comment is more in the nature of a smug, satisfied grin, I'm afraid. You
see, I don't use exit() - or at least, if I do, I consider it to be a bug
that has to be fixed. I've just searched my personal code base for exit()
calls. I found 24 altogether in 14 different C files (out of - well,
thousands), dating back in some cases to the mid-1990s. That's 24 bugs I
have to fix, which isn't too huge a job, really.

Why don't I use exit()? Well, it's a style thing, I suppose. I like to
think of big chunks being made up of little chunks, and that means that
little chunks have to behave like little chunks and not take arbitrary
decisions like "quit the program", leaving those decisions to bigger
chunks. And whilst I will grant a big chunk (think "main()") the right to
make such a decision, it is easy to revoke it (think "edit and rename one
function") if that big chunk becomes a mere part of an enormous chunk.

Unfortunately, it is not always possible to persuade other people of the
advantages of this kind of modularity. In 1994-5, I was working at V, and
the code I was working on was littered with mallocs, which were never
freed. (No, it wasn't me that did that, and I didn't have time to fix it
because I was too busy doing other stuff.) It was no big deal, said the
principal culprit, because the memory was in any case released when the
program exited. In due course, I left, and went on to work at W, X, and Y.
And then I was hired by Z, who had bought the code from X. And guess what?
They had to do precisely what you need to do - turn a program into a
function. And suddenly all these leaks mattered a great deal.

Fellow regulars in comp.lang.c often smile indulgently when I wax lyrical
about my strange stylistic habits - such as one entry point and one exit
point for each function and each loop, always initialise, always give
control back to your caller, and so on. But I have my reasons, and they
are good reasons. In this case: if the original programmer had been me,
your task would have been trivial, and we would never even have heard
about it because you wouldn't have had to ask.

Why am I telling you all this? Well, think about it the next time you write
a program. What if, one day, you need *this* program to become a function?
How hard would it be to do that? How can you make it easier?

--
Richard Heathfield <http://www.cpax....
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/goog...
"Usenet is a strange place" - dmr 29 July 1999