Path: blob/main/lib/libc/tests/stdlib/libatexit/libatexit.cc
39553 views
/*1* Copyright (C) 2025 Kyle Evans <[email protected]>2*3* SPDX-License-Identifier: BSD-2-Clause4*5*/67#include <unistd.h>89static int exit_code = -1;10static bool fatal_atexit;1112extern "C" {13void set_fatal_atexit(bool);14void set_exit_code(int);15}1617void18set_fatal_atexit(bool fexit)19{20fatal_atexit = fexit;21}2223void24set_exit_code(int code)25{26exit_code = code;27}2829struct other_object {30~other_object() {3132/*33* In previous versions of our __cxa_atexit handling, we would34* never actually execute this handler because it's added during35* ~object() below; __cxa_finalize would never revisit it. We36* will allow the caller to configure us to exit with a certain37* exit code so that it can run us twice: once to ensure we38* don't crash at the end, and again to make sure the handler39* actually ran.40*/41if (exit_code != -1)42_exit(exit_code);43}44};4546void47create_staticobj()48{49static other_object obj;50}5152struct object {53~object() {54/*55* If we're doing the fatal_atexit behavior (i.e., create an56* object that will add its own dtor for __cxa_finalize), then57* we don't exit here.58*/59if (fatal_atexit)60create_staticobj();61else if (exit_code != -1)62_exit(exit_code);63}64};6566static object obj;676869