Blame stdlib/exit.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
#include <libc-lock.h>
Packit 6c4009
#include "exit.h"
Packit 6c4009
Packit 6c4009
#include "set-hooks.h"
Packit 6c4009
DEFINE_HOOK (__libc_atexit, (void))
Packit 6c4009
Packit 6c4009
/* Initialize the flag that indicates exit function processing
Packit 6c4009
   is complete. See concurrency notes in stdlib/exit.h where
Packit 6c4009
   __exit_funcs_lock is declared.  */
Packit 6c4009
bool __exit_funcs_done = false;
Packit 6c4009
Packit 6c4009
/* Call all functions registered with `atexit' and `on_exit',
Packit 6c4009
   in the reverse of the order in which they were registered
Packit 6c4009
   perform stdio cleanup, and terminate program execution with STATUS.  */
Packit 6c4009
void
Packit 6c4009
attribute_hidden
Packit 6c4009
__run_exit_handlers (int status, struct exit_function_list **listp,
Packit 6c4009
		     bool run_list_atexit, bool run_dtors)
Packit 6c4009
{
Packit 6c4009
  /* First, call the TLS destructors.  */
Packit 6c4009
#ifndef SHARED
Packit 6c4009
  if (&__call_tls_dtors != NULL)
Packit 6c4009
#endif
Packit 6c4009
    if (run_dtors)
Packit 6c4009
      __call_tls_dtors ();
Packit 6c4009
Packit 6c4009
  /* We do it this way to handle recursive calls to exit () made by
Packit 6c4009
     the functions registered with `atexit' and `on_exit'. We call
Packit 6c4009
     everyone on the list and use the status value in the last
Packit 6c4009
     exit (). */
Packit 6c4009
  while (true)
Packit 6c4009
    {
Packit 6c4009
      struct exit_function_list *cur;
Packit 6c4009
Packit 6c4009
      __libc_lock_lock (__exit_funcs_lock);
Packit 6c4009
Packit 6c4009
    restart:
Packit 6c4009
      cur = *listp;
Packit 6c4009
Packit 6c4009
      if (cur == NULL)
Packit 6c4009
	{
Packit 6c4009
	  /* Exit processing complete.  We will not allow any more
Packit 6c4009
	     atexit/on_exit registrations.  */
Packit 6c4009
	  __exit_funcs_done = true;
Packit 6c4009
	  __libc_lock_unlock (__exit_funcs_lock);
Packit 6c4009
	  break;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      while (cur->idx > 0)
Packit 6c4009
	{
Packit 6c4009
	  struct exit_function *const f = &cur->fns[--cur->idx];
Packit 6c4009
	  const uint64_t new_exitfn_called = __new_exitfn_called;
Packit 6c4009
Packit 6c4009
	  /* Unlock the list while we call a foreign function.  */
Packit 6c4009
	  __libc_lock_unlock (__exit_funcs_lock);
Packit 6c4009
	  switch (f->flavor)
Packit 6c4009
	    {
Packit 6c4009
	      void (*atfct) (void);
Packit 6c4009
	      void (*onfct) (int status, void *arg);
Packit 6c4009
	      void (*cxafct) (void *arg, int status);
Packit 6c4009
Packit 6c4009
	    case ef_free:
Packit 6c4009
	    case ef_us:
Packit 6c4009
	      break;
Packit 6c4009
	    case ef_on:
Packit 6c4009
	      onfct = f->func.on.fn;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
	      PTR_DEMANGLE (onfct);
Packit 6c4009
#endif
Packit 6c4009
	      onfct (status, f->func.on.arg);
Packit 6c4009
	      break;
Packit 6c4009
	    case ef_at:
Packit 6c4009
	      atfct = f->func.at;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
	      PTR_DEMANGLE (atfct);
Packit 6c4009
#endif
Packit 6c4009
	      atfct ();
Packit 6c4009
	      break;
Packit 6c4009
	    case ef_cxa:
Packit 6c4009
	      /* To avoid dlclose/exit race calling cxafct twice (BZ 22180),
Packit 6c4009
		 we must mark this function as ef_free.  */
Packit 6c4009
	      f->flavor = ef_free;
Packit 6c4009
	      cxafct = f->func.cxa.fn;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
	      PTR_DEMANGLE (cxafct);
Packit 6c4009
#endif
Packit 6c4009
	      cxafct (f->func.cxa.arg, status);
Packit 6c4009
	      break;
Packit 6c4009
	    }
Packit 6c4009
	  /* Re-lock again before looking at global state.  */
Packit 6c4009
	  __libc_lock_lock (__exit_funcs_lock);
Packit 6c4009
Packit 6c4009
	  if (__glibc_unlikely (new_exitfn_called != __new_exitfn_called))
Packit 6c4009
	    /* The last exit function, or another thread, has registered
Packit 6c4009
	       more exit functions.  Start the loop over.  */
Packit 6c4009
	    goto restart;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      *listp = cur->next;
Packit 6c4009
      if (*listp != NULL)
Packit 6c4009
	/* Don't free the last element in the chain, this is the statically
Packit 6c4009
	   allocate element.  */
Packit 6c4009
	free (cur);
Packit 6c4009
Packit 6c4009
      __libc_lock_unlock (__exit_funcs_lock);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (run_list_atexit)
Packit 6c4009
    RUN_HOOK (__libc_atexit, ());
Packit 6c4009
Packit 6c4009
  _exit (status);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
exit (int status)
Packit 6c4009
{
Packit 6c4009
  __run_exit_handlers (status, &__exit_funcs, true, true);
Packit 6c4009
}
Packit 6c4009
libc_hidden_def (exit)