summaryrefslogtreecommitdiff
path: root/libc/stdlib
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2002-02-13 09:47:04 +0000
committerEric Andersen <andersen@codepoet.org>2002-02-13 09:47:04 +0000
commit3ab0557e5a3a81b5202334142326dd2e5edc5d28 (patch)
treefbc4cb8ae1dd73c49fdf4f898d0fb329e8551164 /libc/stdlib
parent80e6061ac5deca193759b979d34906bfc9b857ef (diff)
A number of naming updates in preparation for adding in
proper threading. Most of this is from Stefan Soucek, with additions and changes as needed from me.
Diffstat (limited to 'libc/stdlib')
-rw-r--r--libc/stdlib/Makefile2
-rw-r--r--libc/stdlib/atexit.c137
-rw-r--r--libc/stdlib/system.c3
3 files changed, 117 insertions, 25 deletions
diff --git a/libc/stdlib/Makefile b/libc/stdlib/Makefile
index 8e36fd18e..aaf959bcc 100644
--- a/libc/stdlib/Makefile
+++ b/libc/stdlib/Makefile
@@ -34,7 +34,7 @@ MSRC1=strto_ll.c
MOBJ1=strtoll.o strtoull.o strto_ll.o atoll.o
MSRC2=atexit.c
-MOBJ2=atexit.o exit.o
+MOBJ2=atexit.o on_exit.o __exit_handler.o exit.o
CSRC = abort.c getenv.c mktemp.c qsort.c realpath.c abs.c bsearch.c \
diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c
index 45ecefe9c..d7be35fbd 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -10,7 +10,7 @@
* Removed on_exit since it did not match gnu libc definition.
* Combined atexit and __do_exit into one object file.
*
- * Feb 2000 Manuel Novoa III
+ * Feb 2001 Manuel Novoa III
*
* Reworked file after addition of __uClibc_main.
* Changed name of __do_exit to atexit_handler.
@@ -18,58 +18,149 @@
* Moved declaration of __uClibc_cleanup to __uClibc_main
* where it is initialized with (possibly weak alias)
* __stdio_close_all.
+ *
+ * Jul 2001 Steve Thayer
+ *
+ * Added an on_exit implementation (that now matches gnu libc definition.)
+ * Pulled atexit_handler out of the atexit object since it is now required by
+ * on_exit as well. Renamed it to __exit_handler.
+ * Fixed a problem where exit functions stop getting called if one of
+ * them calls exit().
+ * As a side effect of these changes, abort() no longer calls the exit
+ * functions (it now matches the gnu libc definition).
+ *
*/
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
-typedef void (*vfuncp) (void);
-extern vfuncp __uClibc_cleanup;
+#define __MAX_EXIT __UCLIBC_MAX_ATEXIT
+
+typedef void (*aefuncp) (void); /* atexit function pointer */
+typedef void (*oefuncp) (int, void *); /* on_exit function pointer */
+typedef enum {
+ ef_atexit,
+ ef_on_exit
+} ef_type; /* exit function types */
-#ifdef L_atexit
extern void __stdio_close_all(void);
-static vfuncp __atexit_table[__UCLIBC_MAX_ATEXIT];
-static int __atexit_count = 0;
+/* this is in the L_exit object */
+extern void (*__exit_cleanup) (int);
-static void atexit_handler(void)
-{
- int count;
+/* these are in the L___do_exit object */
+extern int __exit_count;
+extern void __exit_handler(int);
+extern struct exit_function {
+ ef_type type; /* ef_atexit or ef_on_exit */
+ union {
+ aefuncp atexit;
+ struct {
+ oefuncp func;
+ void *arg;
+ } on_exit;
+ } funcs;
+} __exit_function_table[__MAX_EXIT];
+#ifdef L_atexit
/*
- * Guard against more functions being added and againt being reinvoked.
+ * register a function to be called at normal program termination
+ * (the registered function takes no arguments)
*/
- __uClibc_cleanup = 0;
+int atexit(aefuncp func)
+{
+ struct exit_function *efp;
- /* In reverse order */
- for (count = __atexit_count ; count-- ; ) {
- (*__atexit_table[count])();
+ if (__exit_count >= __MAX_EXIT) {
+ __set_errno(ENOMEM);
+ return -1;
}
- if (__stdio_close_all)
- __stdio_close_all();
+ if (func) {
+ __exit_cleanup = __exit_handler; /* enable cleanup */
+ efp = &__exit_function_table[__exit_count++];
+ efp->type = ef_atexit;
+ efp->funcs.atexit = func;
+ }
+ return 0;
}
+#endif
-int atexit(vfuncp ptr)
+#ifdef L_on_exit
+/*
+ * register a function to be called at normal program termination
+ * the registered function takes two arguments:
+ * status - the exit status that was passed to the exit() function
+ * arg - generic argument
+ */
+int on_exit(oefuncp func, void *arg)
{
- if ((__uClibc_cleanup == 0) || (__atexit_count >= __UCLIBC_MAX_ATEXIT)) {
+ struct exit_function *efp;
+
+ if (__exit_count >= __MAX_EXIT) {
__set_errno(ENOMEM);
return -1;
}
- if (ptr) {
- __uClibc_cleanup = atexit_handler;
- __atexit_table[__atexit_count++] = ptr;
+ if (func) {
+ __exit_cleanup = __exit_handler; /* enable cleanup */
+ efp = &__exit_function_table[__exit_count++];
+ efp->type = ef_on_exit;
+ efp->funcs.on_exit.func = func;
+ efp->funcs.on_exit.arg = arg;
}
return 0;
}
#endif
+#ifdef L___exit_handler
+struct exit_function __exit_function_table[__MAX_EXIT];
+int __exit_count = 0; /* Number of registered exit functions */
+
+/*
+ * Handle the work of executing the registered exit functions
+ */
+void __exit_handler(int status)
+{
+ struct exit_function *efp;
+
+ /* In reverse order */
+ for ( ; __exit_count-- ; ) {
+ efp = &__exit_function_table[__exit_count];
+ switch (efp->type) {
+ case ef_on_exit:
+ if (efp->funcs.on_exit.func) {
+ (efp->funcs.on_exit.func) (status, efp->funcs.on_exit.arg);
+ }
+ break;
+ case ef_atexit:
+ if (efp->funcs.atexit) {
+ (efp->funcs.atexit) ();
+ }
+ break;
+ }
+ }
+ if (__stdio_close_all)
+ __stdio_close_all();
+}
+#endif
+
#ifdef L_exit
+extern void (*__uClibc_cleanup) (void);
+void (*__exit_cleanup) (int) = 0;
+
+/*
+ * Normal program termination
+ */
void exit(int rv)
{
- if (__uClibc_cleanup) { /* Not already executing __uClibc_cleanup. */
- __uClibc_cleanup();
+ /* Perform exit-specific cleanup (atexit and on_exit) */
+ if (__exit_cleanup) {
+ __exit_cleanup(rv);
}
+
+ /* Clean up everything else */
+ __uClibc_cleanup();
+
_exit(rv);
}
#endif
diff --git a/libc/stdlib/system.c b/libc/stdlib/system.c
index 5b279976c..6126d8114 100644
--- a/libc/stdlib/system.c
+++ b/libc/stdlib/system.c
@@ -4,7 +4,7 @@
#include <unistd.h>
#include <sys/wait.h>
-int system(command)
+int __libc_system(command)
char *command;
{
int wait_val, pid;
@@ -47,3 +47,4 @@ char *command;
signal(SIGCHLD, save_chld);
return wait_val;
}
+weak_alias(__libc_system, system)