exit.c revision 304592
1234370Sjasone/*- 2234370Sjasone * Copyright (c) 1990, 1993 3234370Sjasone * The Regents of the University of California. All rights reserved. 4234370Sjasone * 5234370Sjasone * Redistribution and use in source and binary forms, with or without 6242844Sjasone * modification, are permitted provided that the following conditions 7242844Sjasone * are met: 8242844Sjasone * 1. Redistributions of source code must retain the above copyright 9242844Sjasone * notice, this list of conditions and the following disclaimer. 10242844Sjasone * 2. Redistributions in binary form must reproduce the above copyright 11242844Sjasone * notice, this list of conditions and the following disclaimer in the 12242844Sjasone * documentation and/or other materials provided with the distribution. 13242844Sjasone * 3. Neither the name of the University nor the names of its contributors 14242844Sjasone * may be used to endorse or promote products derived from this software 15242844Sjasone * without specific prior written permission. 16234370Sjasone * 17234370Sjasone * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18234370Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19234370Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20234370Sjasone * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21234370Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22234370Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23234370Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24234370Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25234370Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26234370Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27234370Sjasone * SUCH DAMAGE. 28234370Sjasone */ 29234370Sjasone 30234370Sjasone#if defined(LIBC_SCCS) && !defined(lint) 31234370Sjasonestatic char sccsid[] = "@(#)exit.c 8.1 (Berkeley) 6/4/93"; 32234370Sjasone#endif /* LIBC_SCCS and not lint */ 33234370Sjasone#include <sys/cdefs.h> 34234370Sjasone__FBSDID("$FreeBSD: releng/11.0/lib/libc/stdlib/exit.c 304592 2016-08-22 07:38:44Z kib $"); 35234370Sjasone 36234370Sjasone#include "namespace.h" 37234370Sjasone#include <stdlib.h> 38234370Sjasone#include <unistd.h> 39234370Sjasone#include "un-namespace.h" 40234370Sjasone 41234370Sjasone#include "atexit.h" 42242844Sjasone#include "libc_private.h" 43242844Sjasone 44242844Sjasonevoid (*__cleanup)(void); 45242844Sjasone 46242844Sjasone/* 47242844Sjasone * This variable is zero until a process has created a thread. 48242844Sjasone * It is used to avoid calling locking functions in libc when they 49242844Sjasone * are not required. By default, libc is intended to be(come) 50242844Sjasone * thread-safe, but without a (significant) penalty to non-threaded 51242844Sjasone * processes. 52242844Sjasone */ 53242844Sjasoneint __isthreaded = 0; 54242844Sjasone 55242844Sjasone/* 56242844Sjasone * Exit, flushing stdio buffers if necessary. 57242844Sjasone */ 58242844Sjasonevoid 59242844Sjasoneexit(int status) 60242844Sjasone{ 61242844Sjasone /* Ensure that the auto-initialization routine is linked in: */ 62242844Sjasone extern int _thread_autoinit_dummy_decl; 63242844Sjasone 64242844Sjasone _thread_autoinit_dummy_decl = 1; 65242844Sjasone 66242844Sjasone /* 67234370Sjasone * We're dealing with cleaning up thread_local destructors in the case of 68234370Sjasone * the process termination through main() exit. 69234370Sjasone * Other cases are handled elsewhere. 70234370Sjasone */ 71234370Sjasone __cxa_thread_call_dtors(); 72234370Sjasone __cxa_finalize(NULL); 73234370Sjasone if (__cleanup) 74234370Sjasone (*__cleanup)(); 75234370Sjasone _exit(status); 76234370Sjasone} 77234370Sjasone