Skip to content

Commit

Permalink
nss_mcdb - register atexit() to cleanup open nss mcdb if -D_FORTIFY_S…
Browse files Browse the repository at this point in the history
…OURCE
  • Loading branch information
gstrauss committed Jul 25, 2010
1 parent 116e0d2 commit a17d095
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion mcdb.c
Expand Up @@ -160,8 +160,10 @@ mcdb_mmap_free(struct mcdb_mmap * const restrict map)
{
mcdb_mmap_unmap(map);
if (map->fn_free) {
if (map->fname != NULL && map->fname != map->fnamebuf)
if (map->fname != NULL && map->fname != map->fnamebuf) {
map->fn_free(map->fname);
map->fname = NULL;
}
map->fn_free(map);
}
}
Expand Down
20 changes: 20 additions & 0 deletions nss_mcdb.c
Expand Up @@ -77,6 +77,21 @@ static struct mcdb_mmap *_nss_mcdb_mmap[_nss_num_dbs];

static __thread struct mcdb _nss_mcdb_st[_nss_num_dbs];

#ifdef _FORTIFY_SOURCE
static void _nss_mcdb_atexit(void)
{
struct mcdb_mmap * restrict map;
for (uintptr_t i = 0; i < _nss_num_dbs; ++i) {
map = &_nss_mcdb_mmap_st[i];
if ( _nss_mcdb_mmap_st[i].ptr != NULL
|| _nss_mcdb_mmap_st[i].fname != NULL ) {
mcdb_mmap_destroy(&_nss_mcdb_mmap_st[i]);
_nss_mcdb_mmap_st[i].ptr = NULL;
_nss_mcdb_mmap_st[i].fname = NULL;
}
}
}
#endif

/* custom free for mcdb_mmap_* routines to not free initial static storage */
static void
Expand Down Expand Up @@ -111,6 +126,11 @@ _nss_mcdb_db_openshared(const enum nss_dbtype dbtype)
return true;
}

#ifdef _FORTIFY_SOURCE
{ static bool atexit_once = true;
if (atexit_once) { atexit_once = false; atexit(_nss_mcdb_atexit); } }
#endif

/* pass full path in fname instead of separate dirname and basename
* (not using openat(), fstatat() where someone might close dfd on us)
* use static storage for initial struct mcdb_mmap for each dbtype
Expand Down

0 comments on commit a17d095

Please sign in to comment.