Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!ut-sally!husc6!hao!noao!mcdsun!fnf
From: fnf@mcdsun.UUCP (Fred Fish)
Newsgroups: comp.os.minix
Subject: Re: Keeping exit(3) small but making it work for stdio -- atexit()
Message-ID: <333@mcdsun.UUCP>
Date: Tue, 7-Jul-87 17:57:29 EDT
Article-I.D.: mcdsun.333
Posted: Tue Jul  7 17:57:29 1987
Date-Received: Sat, 11-Jul-87 03:03:20 EDT
References: <3118@felix.UUCP> <2352@hoptoad.uucp> <3169@felix.UUCP> <2368@hoptoad.uucp>
Reply-To: fnf@mcdsun.UUCP (Fred Fish)
Organization: Motorola Microcomputer Division
Lines: 61

In article <2368@hoptoad.uucp> gnu@hoptoad.uucp (John Gilmore) writes:
>While Art's solution works, there is a cleaner solution than
>requiring the programmer to call _exit if she doesn't use stdio.
>Minix can remain small and still be fixed to work properly.
> ...
>This should be easy to implement; you make an array of 33 function

Yes, I just recently put a very similar fix in our version of SVR3 here,
and it was only about a 2 hour job, most of which was figuring out the
correct place in stdio to trigger the cleanup from exit.  The results
were well worth the work, a null program "main(){}" went from over 14Kb
linked to just 298 bytes.  The original 14Kb was because crt0.o references
exit(), which pulls in cleanup(), which pulls in ...

One possible problem with the implementation John outlined: what if
the user calls atexit() with his own functions before doing any stdio?

I.E., if he calls:

	atexit(func1);
	atexit(func2);
	do some stdio stuff for the first time
	atexit(func3);

The queue of functions will look like:

	func3 -> _cleanup -> func2 -> func1

which is dequeue'd and executed in the order

	func3()
	_cleanup()
	func2()
	func1()

with possibly unintentional results.

My solution was to not use the atexit() routine to register the cleanup
function, just set a global pointer (_stdio_cleanup) in exit and insure
that it got dereferenced *after* all the functions registered by atexit().

P.S.  I put my pointer initialization as the first statement in flsbuf()
in flsbuf.c:

	     unsigned char c1;
	+    extern VOID (*_stdio_cleanup)();	/* Found in exit() module */
	
	+    _stdio_cleanup = _cleanup;
	     do {
	

This may or may not be the theoretically correct place to put it, so if
someone more knowledgeable about stdio wants to speak up...

-Fred


-- 
= Drug tests; just say *NO*!
= Fred Fish  Motorola Computer Division, 3013 S 52nd St, Tempe, Az 85282  USA
= seismo!noao!mcdsun!fnf    (602) 438-5976