Protect __atexit manipulation and traversal with a mutex.

This commit is contained in:
kleink 1998-10-18 14:36:30 +00:00
parent ff08129ca5
commit cd85b5e5ac
3 changed files with 23 additions and 7 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: atexit.c,v 1.9 1998/02/03 18:44:13 perry Exp $ */
/* $NetBSD: atexit.c,v 1.10 1998/10/18 14:36:30 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -41,15 +41,20 @@
#if 0
static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94";
#else
__RCSID("$NetBSD: atexit.c,v 1.9 1998/02/03 18:44:13 perry Exp $");
__RCSID("$NetBSD: atexit.c,v 1.10 1998/10/18 14:36:30 kleink Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <stdlib.h>
#include "reentrant.h"
#include "atexit.h"
struct atexit *__atexit; /* points to head of LIFO stack */
#ifdef _REENT
mutex_t __atexit_mutex = MUTEX_INITIALIZER;
#endif
/*
* Register a function to be performed at exit.
*/
@ -60,15 +65,19 @@ atexit(fn)
static struct atexit __atexit0; /* one guaranteed table */
struct atexit *p;
mutex_lock(&__atexit_mutex);
if ((p = __atexit) == NULL)
__atexit = p = &__atexit0;
else if (p->ind >= ATEXIT_SIZE) {
if ((p = malloc(sizeof(*p))) == NULL)
if ((p = malloc(sizeof(*p))) == NULL) {
mutex_unlock(&__atexit_mutex);
return (-1);
}
p->ind = 0;
p->next = __atexit;
__atexit = p;
}
p->fns[p->ind++] = fn;
mutex_unlock(&__atexit_mutex);
return (0);
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: atexit.h,v 1.6 1998/01/30 23:37:45 perry Exp $ */
/* $NetBSD: atexit.h,v 1.7 1998/10/18 14:36:30 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -45,3 +45,7 @@ struct atexit {
};
extern struct atexit *__atexit; /* points to head of LIFO stack */
#ifdef _REENT
extern mutex_t __atexit_mutex; /* protects __atexit pointer */
#endif

View File

@ -1,4 +1,4 @@
/* $NetBSD: exit.c,v 1.7 1998/02/03 18:44:14 perry Exp $ */
/* $NetBSD: exit.c,v 1.8 1998/10/18 14:36:30 kleink Exp $ */
/*-
* Copyright (c) 1990, 1993
@ -38,12 +38,13 @@
#if 0
static char sccsid[] = "@(#)exit.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: exit.c,v 1.7 1998/02/03 18:44:14 perry Exp $");
__RCSID("$NetBSD: exit.c,v 1.8 1998/10/18 14:36:30 kleink Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
#include <stdlib.h>
#include <unistd.h>
#include "reentrant.h"
#include "atexit.h"
void (*__cleanup) __P((void));
@ -57,10 +58,12 @@ exit(status)
{
struct atexit *p;
int n;
mutex_lock(&__atexit_mutex);
for (p = __atexit; p; p = p->next)
for (n = p->ind; --n >= 0;)
(*p->fns[n])();
mutex_unlock(&__atexit_mutex);
if (__cleanup)
(*__cleanup)();
_exit(status);