Changeset 2941


Ignore:
Timestamp:
Jan 8, 2007, 12:28:02 AM (19 years ago)
Author:
bird
Message:

bugfixing init/term.

Location:
trunk/libc
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/libc/include/klibc/backend.h

    r2935 r2941  
    7676 * Called when a frontend shuts down.
    7777 *
    78  * @returns 0 on success, non-zero on failure.
    7978 * @param   hmod        The handle of the module that's causing the frontend to shut down.
     79 *                      When invoked via _exit() it's 0.
    8080 * @param   pvOS        OS specific argument.
     81 *                      When invoked via _exit() it's NULL.
    8182 * @param   fFlags      Termination flags.
    82  */
    83 int __libc_Back_term(uintptr_t hmod, void *pvOS, unsigned fFlags);
     83 *                      When invoked via _exit() it's 0.
     84 */
     85void __libc_Back_term(uintptr_t hmod, void *pvOS, unsigned fFlags);
    8486
    8587/**
  • trunk/libc/include/klibc/initterm.h

    r2915 r2941  
    5151int  __libc_InitDll(uintptr_t hmod, void *pvOS, unsigned fFlags);
    5252void __libc_TermDll(uintptr_t hmod, void *pvOS, unsigned fFlags);
     53void __libc_TermProcess(void);
    5354
    5455extern char ** _org_environ;
  • trunk/libc/src/kNIX/b_initterm.c

    r2937 r2941  
    153153 * Called when a frontend shuts down.
    154154 *
    155  * @returns 0 on success, non-zero on failure.
    156155 * @param   hmod        The handle of the module that's causing the frontend to shut down.
     156 *                      When invoked via _exit() it's 0.
    157157 * @param   pvOS        OS specific argument.
     158 *                      When invoked via _exit() it's NULL.
    158159 * @param   fFlags      Termination flags.
    159  */
    160 int __libc_Back_term(uintptr_t hmod, void *pvOS, unsigned fFlags)
    161 {
    162     /** @todo */
    163     return 0;
    164 }
     160 *                      When invoked via _exit() it's 0.
     161 */
     162static void __libc_back_term(uintptr_t hmod, void *pvOS, unsigned fFlags)
     163{
     164    LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOS, fFlags);
     165
     166    /** @todo more! */
     167
     168    /*
     169     * OS specific termination.
     170     */
     171    __libc_back_termOS(hmod, pvOS, fFlags);
     172
     173    LIBCLOG_RETURN_VOID();
     174}
     175
     176
     177/**
     178 * Terminates the backend.
     179 * Called when a frontend shuts down.
     180 *
     181 * @param   hmod        The handle of the module that's causing the frontend to shut down.
     182 *                      When invoked via _exit() it's 0.
     183 * @param   pvOS        OS specific argument.
     184 *                      When invoked via _exit() it's NULL.
     185 * @param   fFlags      Termination flags.
     186 *                      When invoked via _exit() it's 0.
     187 */
     188void __libc_Back_term(uintptr_t hmod, void *pvOS, unsigned fFlags)
     189{
     190    LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOS, fFlags);
     191
     192    /*
     193     * Don't do anything until the last time we're called.
     194     */
     195    const int32_t cRefs = __atomic_decrement_s32(&g_ckNIXUsers);
     196    if (cRefs == 0)
     197        __libc_back_term(hmod, pvOS, fFlags);
     198
     199    LIBCLOG_RETURN_MSG_VOID("ret void (g_ckNIXUsers=%d)\n", cRefs);
     200}
     201
  • trunk/libc/src/kNIX/os2/b_initterm-os2.c

    r2937 r2941  
    103103            {
    104104                __libc_spmRelease(pSelf);
     105
     106                /*
     107                 * If we're residing in a DLL, we will add a dynamic load reference
     108                 * to the DLL to avoid DosFreeModule trouble caused by our exit list handler.
     109                 */
     110                if (hmod != 0 && fibGetExeHandle() != hmod)
     111                {
     112                    char szDllName[CCHMAXPATH];
     113                    int rc2 = DosQueryModuleName(hmod, sizeof(szDllName), szDllName);
     114                    if (!rc2)
     115                    {
     116                        HMODULE hmodDll = NULLHANDLE;
     117                        rc2 = DosLoadModule(NULL, 0, szDllName, &hmodDll);
     118                        LIBC_ASSERTM(rc2 == NO_ERROR, "rc2=%d szDllName='%s'\n", rc, szDllName);
     119                    }
     120                }
    105121
    106122                /*
  • trunk/libc/src/kNIX/os2/libcfork.c

    r2937 r2941  
    355355        pProcess = __libc_spmSelf(1 /* auto init */);
    356356        if (!pProcess)
    357         {
    358             static char szMsg[] = "LIBC Error: Couldn't register process in shared memory!\r\n";
    359             ULONG       ul;
    360             LIBC_ASSERTM_FAILED("couldn't register process!\n");
    361 
    362             DosWrite(2, szMsg, sizeof(szMsg), &ul);
    363             while (fExecutable)
    364             {
    365                 LIBCLOG_MSG("Calling DosExit(EXIT_PROCESS, 0xffff)...\n");
    366                 DosExit(EXIT_PROCESS, /*fixme*/0xffff);
    367             }
    368             pTib->tib_pexchain = XcptRegRec.Core.prev_structure;
    369             LIBCLOG_RETURN_INT(-1);
    370         }
     357            __libc_Back_panic(0, NULL, "kLIBC fork: Couldn't register process in shared memory!\r\n");
    371358
    372359        /*
     
    500487        /*
    501488         * Don't deregister modules which has already been deregistered.
    502          * Don't deregister if we're in shutting down the process (waste of time and SPM might already be shut down).
     489         * Don't deregister if we're in shutting down the process (waste
     490         * of time and SPM will already be shut down).
    503491         */
    504492        if (pModule->fFlags & __LIBC_FORKMODULE_FLAGS_DEREGISTERED)
     
    31843172     * Exit process.
    31853173     */
    3186     __libc_Back_panic(__LIBC_PANIC_NO_SPM_TERM, pvCtx, "LIBC fork: Child aborting fork()! rc=%x\n", rc);
     3174    __libc_Back_panic(__LIBC_PANIC_NO_SPM_TERM, pvCtx, "kLIBC fork: Child aborting fork()! rc=%x\n", rc);
    31873175}
    31883176
  • trunk/libc/src/libc/libc.def

    r2937 r2941  
    19631963    "__std_getrusage" @1959
    19641964    "__std_ttyname_r" @1960
     1965    "___libc_Back_gcArgs" @1961
     1966    "___libc_Back_gpapszArgs" @1962
     1967    "___libc_Back_init" @1963
     1968    "___libc_Back_term" @1964
     1969    "___libc_Back_initArgv" @1965
     1970    "___libc_Back_fhImport" @1966
     1971    "___libc_Back_fhValidate" @1967
     1972    "___libc_Back_fsUMask" @1968
     1973    "___libc_Back_ioClose" @1969
     1974    "___libc_Back_ioControl" @1970
     1975    "___libc_Back_ioDuplicate" @1971
     1976    "___libc_Back_ioRead" @1972
     1977    "___libc_Back_ioSync" @1973
     1978    "___libc_Back_ioTTYName" @1974
     1979    "___libc_Back_ioWrite" @1975
     1980    "___libc_Back_mmanBrk" @1976
     1981    "___libc_Back_mmanSBrk" @1977
     1982    "___libc_Back_mmanSBrkGetModel" @1978
     1983    "___libc_Back_mmanSBrkSetModel" @1979
     1984    "___libc_Back_processGetResourceLimit" @1980
     1985    "___libc_Back_processSetResourceLimit" @1981
     1986    "___libc_FHFree" @1982
     1987    "___libc_FHImportFile" @1983
     1988    "___libc_Back_initDllLoadException" @1984
     1989    "___libc_Back_ioPipe" @1985
     1990    "___libc_Back_ldrExeName" @1986
     1991    "___libc_Back_miscConsoleSize" @1987
     1992    "___libc_Back_processExit" @1988
     1993    "___libc_Back_processGetResourceUsage" @1989
     1994    "___libc_Back_timeAdjust" @1990
     1995    "___libc_Back_timeGet" @1991
     1996    "___libc_Back_timeSet" @1992
     1997    "___libc_InitDll" @1993
     1998    "___libc_InitExe" @1994
     1999    "___libc_TermDll" @1995
     2000    "___libc_TermProcess" @1996
  • trunk/libc/src/libc/startup/exit.c

    r2254 r2941  
    8383     * Terminate the CRT and do the real exit.
    8484     */
    85     _CRT_term();
     85    __libc_TermProcess();
    8686    LIBCLOG_MSG("calling _exit(%d)\n", ret);
    8787    _exit(ret);
  • trunk/libc/src/libc/startup/initterm.c

    r2937 r2941  
    2626
    2727
    28 /** @page pg_init
    29  *
    30  */
    31 
    32 
    33 /** @page pg_term
    34  *
    35  */
    36 
     28/** @page pg_initterm   Initialization and Termination
     29 *
     30 * kLIBC tries to provides a well defined, portable and hopefully relativly stable
     31 * interface for initialization and termination of it's operation. The dll0 and
     32 * crt0 objects are considered mere users of this interface. The days where the
     33 * __init() function did cool stuff to ESP is over.
     34 *
     35 * The general event flow is as follows:
     36 *      -# dll0/crt0 calls __libc_InitDll()/__libc_initExe().
     37 *      -# kLIBC initializes the backend (__libc_Back_init()).
     38 *      -# kLIBC initializes the frontend (__libc_init()).
     39 *      -# If __libc_InitExe() was invoked, the argument vector is initialized.
     40 *      -# exit() is called.
     41 *
     42 * Both the backend and the frontend performs usage accounting most of the work
     43 * is done on the first call. Likewise for termination.
     44 *
     45 */
    3746
    3847/*******************************************************************************
     
    5261/** Kind of CRT usage count. Used to figure out when we should terminate. */
    5362static int32_t  g_cCrtUsers = 0;
     63/** Times __libc_InitExe was called. This is used to guard against early exit() calls. */
     64static int32_t  g_cExecUsers = 0;
    5465
    5566
     
    8091
    8192/**
    82  * LIBC termination entry point used by a DLL in the process of being unloaded.
    83  *
    84  * This must be exactly paried with a previous __libc_InitDll() call.
    85  *
    86  * @param   hmod    The native module handle of the DLL.
    87  * @param   pvOS    OS specific argument.
    88  * @param   fFlags  Initialization flags, a combination of the __LIBC_INIT_FLAGS_* \#defines.
    89  */
    90 void __libc_TermDll(uintptr_t hmod, void *pvOS, unsigned fFlags)
    91 {
    92     LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOS, fFlags);
    93 
    94     /*
    95      * Invalidate or free any registered atexit or on_exit callbacks residing in this module
    96      */
    97     __libc_atexit_unload(hmod);
    98 
    99     /*
    100      * Do normal LIBC termination.
    101      */
    102     __libc_term(hmod, pvOS, fFlags);
    103 
    104     LIBCLOG_RETURN_VOID();
    105 }
    106 
    107 
    108 /**
    10993 * LIBC initialization entry point used by an EXE.
    11094 *
     
    119103{
    120104    LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x pArgs=%p\n", hmod, pvOS, fFlags, (void *)pArgs);
     105
     106    /* Flag that we've been called. */
     107    const int32_t cRefs = __atomic_increment_s32(&g_cExecUsers); (void)cRefs;
     108    LIBC_ASSERTM(cRefs == 1, "cRefs=%d\n", cRefs);
    121109
    122110    /*
     
    135123    pArgs->envp = _org_environ;
    136124
    137     LIBCLOG_RETURN_VOID();
     125    LIBCLOG_RETURN_MSG_VOID("ret void (g_cExecUsers=%d)\n", cRefs);
    138126}
    139127
     
    196184
    197185/**
     186 * LIBC termination entry point used by a DLL in the process of being unloaded.
     187 *
     188 * This must be exactly paired with a previous __libc_InitDll() call.
     189 *
     190 * @param   hmod    The native module handle of the DLL.
     191 * @param   pvOS    OS specific argument.
     192 * @param   fFlags  Initialization flags, a combination of the __LIBC_INIT_FLAGS_* \#defines.
     193 */
     194void __libc_TermDll(uintptr_t hmod, void *pvOS, unsigned fFlags)
     195{
     196    LIBCLOG_ENTER("hmod=%tx pvOS=%p fFlags=%#x\n", hmod, pvOS, fFlags);
     197
     198    /*
     199     * Invalidate or free any registered atexit or on_exit callbacks residing in this module
     200     */
     201    __libc_atexit_unload(hmod);
     202
     203    /*
     204     * Do normal LIBC termination.
     205     */
     206    __libc_term(hmod, pvOS, fFlags);
     207
     208    LIBCLOG_RETURN_VOID();
     209}
     210
     211
     212/**
     213 * LIBC termination entry point used by an 'exit' function.
     214 *
     215 * This call is 'paired' with the __libc_InitExe call.
     216 */
     217void __libc_TermProcess(void)
     218{
     219    LIBCLOG_ENTER("\n");
     220
     221    /*
     222     * Do normal LIBC termination.
     223     */
     224    const int32_t cRefs = __atomic_decrement_s32(&g_cExecUsers);
     225    LIBC_ASSERTM(cRefs >= 0, "cRefs=%d\n", cRefs);
     226    if (cRefs >= 0)
     227        __libc_term(0, NULL, 0);
     228
     229    LIBCLOG_RETURN_MSG_VOID("ret void (g_cExecUsers=%d)\n", cRefs);
     230}
     231
     232
     233
     234/**
    198235 * LIBC termination worker.
    199236 *
     
    201238 *
    202239 * @param   hmod    The native module handle of the DLL/EXE.
    203  * @param   pvOS    OS specific argument.
    204  * @param   fFlags  Initialization flags, a combination of the __LIBC_INIT_FLAGS_* \#defines.
     240 *                  When invoked via _exit() it's 0.
     241 * @param   pvOS    OS specific argument.
     242 *                  When invoked via _exit() it's NULL.
     243 * @param   fFlags  Initialization flags, a combination of the __LIBC_INIT_FLAGS_* \#defines.
     244 *                  When invoked via _exit() it's 0.
    205245 */
    206246static void __libc_term(uintptr_t hmod, void *pvOS, unsigned fFlags)
     
    215255     */
    216256    const int32_t cRefs = __atomic_decrement_s32(&g_cCrtUsers);
    217     if (cRefs != 1)
     257    if (cRefs != 0)
    218258        LIBCLOG_RETURN_MSG_VOID("ret void (g_cCrtUsers=%d)\n", cRefs);
    219259
  • trunk/libc/src/libc/startup/os2/dllinit-os2.c

    r2717 r2941  
    44 */
    55
    6 #include <emx/startup.h>
     6#include <klibc/initterm.h>
    77#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_INITTERM
    88#include <InnoTekLIBC/logstrict.h>
  • trunk/libc/src/libc/startup/os2/x86/crt0.s

    r2806 r2941  
    6161    mov     %esp, %fs:0
    6262
    63     andl    0xfffffff0, %esp            /* make sure it's aligned. */
     63    andl    $0xfffffff0, %esp           /* make sure it's properly aligned. */
    6464    subl    $0x10, %esp
    6565
     
    109109    pushl   %eax                        /* hmod */
    110110    call    ___libc_InitExe
    111     addl    $0x0c, %esp
     111    addl    $0x10, %esp
    112112
    113113    /*
  • trunk/libc/src/libc/startup/os2/x86/dll0.s

    r2937 r2941  
    194194    movl    $g_ForkModule, (%esp)
    195195    movb    $0, g_fCalledForkRegisterModule
    196     call    ___libc_ForkRegisterModule
     196    call    ___libc_ForkDeregisterModule
    197197
    198198dll0_term_done:
Note: See TracChangeset for help on using the changeset viewer.