| 1 | /* $Id: initterm.cpp,v 1.39 2000-03-16 19:20:39 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * KERNEL32 DLL entry point | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen | 
|---|
| 7 | * Copyright 1998 Peter Fitzsimmons | 
|---|
| 8 | * | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | /*-------------------------------------------------------------*/ | 
|---|
| 15 | /* INITERM.C -- Source for a custom dynamic link library       */ | 
|---|
| 16 | /*              initialization and termination (_DLL_InitTerm) */ | 
|---|
| 17 | /*              function.                                      */ | 
|---|
| 18 | /*                                                             */ | 
|---|
| 19 | /* When called to perform initialization, this sample function */ | 
|---|
| 20 | /* gets storage for an array of integers, and initializes its  */ | 
|---|
| 21 | /* elements with random integers.  At termination time, it     */ | 
|---|
| 22 | /* frees the array.  Substitute your own special processing.   */ | 
|---|
| 23 | /*-------------------------------------------------------------*/ | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | /* Include files */ | 
|---|
| 27 | #define  INCL_DOSMODULEMGR | 
|---|
| 28 | #define  INCL_DOSMISC | 
|---|
| 29 | #define  INCL_DOSPROCESS | 
|---|
| 30 | #define  INCL_DOSSEMAPHORES | 
|---|
| 31 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 32 | #include <stdlib.h> | 
|---|
| 33 | #include <stdio.h> | 
|---|
| 34 | #include <string.h> | 
|---|
| 35 | #include <misc.h> | 
|---|
| 36 | #include <wprocess.h> | 
|---|
| 37 | #include "handlemanager.h" | 
|---|
| 38 | #include "profile.h" | 
|---|
| 39 | #include "initterm.h" | 
|---|
| 40 | #include <win32type.h> | 
|---|
| 41 | #include <odinlx.h> | 
|---|
| 42 | #include "oslibmisc.h" | 
|---|
| 43 | #include <heapshared.h> | 
|---|
| 44 | #include <heapcode.h> | 
|---|
| 45 | #include "mmap.h" | 
|---|
| 46 | #include "directory.h" | 
|---|
| 47 | #include "hmdevio.h" | 
|---|
| 48 | #include <windllbase.h> | 
|---|
| 49 | #include "winexepe2lx.h" | 
|---|
| 50 | #include "initsystem.h" | 
|---|
| 51 | #include <exitlist.h> | 
|---|
| 52 | #include "oslibdos.h" | 
|---|
| 53 | #define DBG_LOCALLOG    DBG_initterm | 
|---|
| 54 | #include "dbglocal.h" | 
|---|
| 55 |  | 
|---|
| 56 | /*-------------------------------------------------------------------*/ | 
|---|
| 57 | /* A clean up routine registered with DosExitList must be used if    */ | 
|---|
| 58 | /* runtime calls are required and the runtime is dynamically linked. */ | 
|---|
| 59 | /* This will guarantee that this clean up routine is run before the  */ | 
|---|
| 60 | /* library DLL is terminated.                                        */ | 
|---|
| 61 | /*-------------------------------------------------------------------*/ | 
|---|
| 62 | static void APIENTRY cleanup(ULONG reason); | 
|---|
| 63 |  | 
|---|
| 64 | extern "C" { | 
|---|
| 65 | void CDECL _ctordtorInit( void ); | 
|---|
| 66 | void CDECL _ctordtorTerm( void ); | 
|---|
| 67 |  | 
|---|
| 68 | //Win32 resource table (produced by wrc) | 
|---|
| 69 | extern DWORD _Resource_PEResTab; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | //Global DLL Data | 
|---|
| 73 | #pragma data_seg(_GLOBALDATA) | 
|---|
| 74 | int globLoadNr = 0; | 
|---|
| 75 | #pragma data_seg() | 
|---|
| 76 |  | 
|---|
| 77 | /* Tue 03.03.1998: knut */ | 
|---|
| 78 | ULONG flAllocMem = 0;    /* flag to optimize DosAllocMem to use all the memory on SMP machines */ | 
|---|
| 79 | ULONG ulMaxAddr = 0x20000000; /* end of user address space. */ | 
|---|
| 80 | int   loadNr = 0; | 
|---|
| 81 | char  kernel32Path[CCHMAXPATH] = ""; | 
|---|
| 82 |  | 
|---|
| 83 | /****************************************************************************/ | 
|---|
| 84 | /* _DLL_InitTerm is the function that gets called by the operating system   */ | 
|---|
| 85 | /* loader when it loads and frees this DLL for each process that accesses   */ | 
|---|
| 86 | /* this DLL.  However, it only gets called the first time the DLL is loaded */ | 
|---|
| 87 | /* and the last time it is freed for a particular process.  The system      */ | 
|---|
| 88 | /* linkage convention MUST be used because the operating system loader is   */ | 
|---|
| 89 | /* calling this function.                                                   */ | 
|---|
| 90 | /****************************************************************************/ | 
|---|
| 91 | unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long | 
|---|
| 92 | ulFlag) | 
|---|
| 93 | { | 
|---|
| 94 | size_t i; | 
|---|
| 95 | APIRET rc; | 
|---|
| 96 | ULONG  ulSysinfo; | 
|---|
| 97 |  | 
|---|
| 98 | /*-------------------------------------------------------------------------*/ | 
|---|
| 99 | /* If ulFlag is zero then the DLL is being loaded so initialization should */ | 
|---|
| 100 | /* be performed.  If ulFlag is 1 then the DLL is being freed so            */ | 
|---|
| 101 | /* termination should be performed.                                        */ | 
|---|
| 102 | /*-------------------------------------------------------------------------*/ | 
|---|
| 103 |  | 
|---|
| 104 | switch (ulFlag) | 
|---|
| 105 | { | 
|---|
| 106 | case 0 : | 
|---|
| 107 | { | 
|---|
| 108 | ParseLogStatus(); | 
|---|
| 109 |  | 
|---|
| 110 | loadNr = globLoadNr++; | 
|---|
| 111 |  | 
|---|
| 112 | strcpy(kernel32Path, OSLibGetDllName(hModule)); | 
|---|
| 113 | char *endofpath = strrchr(kernel32Path, '\\'); | 
|---|
| 114 | *(endofpath+1) = 0; | 
|---|
| 115 | dprintf(("kernel32 init\n")); | 
|---|
| 116 | _ctordtorInit(); | 
|---|
| 117 |  | 
|---|
| 118 | CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed  98-03-18 05:28:48*/ | 
|---|
| 119 |  | 
|---|
| 120 | OpenPrivateLogFiles(); | 
|---|
| 121 |  | 
|---|
| 122 | if(InitializeSharedHeap() == FALSE) | 
|---|
| 123 | return 0UL; | 
|---|
| 124 |  | 
|---|
| 125 | if(InitializeCodeHeap() == FALSE) | 
|---|
| 126 | return 0UL; | 
|---|
| 127 |  | 
|---|
| 128 | PROFILE_LoadOdinIni(); | 
|---|
| 129 | if(RegisterLxDll(hModule, 0, (PVOID)&_Resource_PEResTab) == FALSE) | 
|---|
| 130 | return 0UL; | 
|---|
| 131 |  | 
|---|
| 132 | //SvL: Kernel32 is a special case; pe.exe loads it, so increase | 
|---|
| 133 | //     the reference count here | 
|---|
| 134 | Win32DllBase *module = Win32DllBase::findModule(hModule); | 
|---|
| 135 | if(module && !fPe2Lx) { | 
|---|
| 136 | module->AddRef(); | 
|---|
| 137 | module->DisableUnload(); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | /*******************************************************************/ | 
|---|
| 141 | /* A DosExitList routine must be used to clean up if runtime calls */ | 
|---|
| 142 | /* are required and the runtime is dynamically linked.             */ | 
|---|
| 143 | /*******************************************************************/ | 
|---|
| 144 |  | 
|---|
| 145 | rc = DosExitList(EXITLIST_KERNEL32|EXLST_ADD, cleanup); | 
|---|
| 146 | if (rc) | 
|---|
| 147 | return 0UL; | 
|---|
| 148 |  | 
|---|
| 149 | /* knut: check for high memory support */ | 
|---|
| 150 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo)); | 
|---|
| 151 | if (rc == 0 && ulSysinfo > 512)   //VirtualAddresslimit is in MB | 
|---|
| 152 | { | 
|---|
| 153 | flAllocMem = PAG_ANY;      // high memory support. Let's use it! | 
|---|
| 154 | ulMaxAddr = ulSysinfo * (1024*1024); | 
|---|
| 155 | } | 
|---|
| 156 | else | 
|---|
| 157 | flAllocMem = 0;        // no high memory support | 
|---|
| 158 |  | 
|---|
| 159 | OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES); | 
|---|
| 160 |  | 
|---|
| 161 | //SvL: Do it here instead of during the exe object creation | 
|---|
| 162 | //(std handles can be used in win32 dll initialization routines | 
|---|
| 163 | HMInitialize();             /* store standard handles within HandleManager */ | 
|---|
| 164 | InitializeTIB(TRUE);        //MUST be done after HMInitialize! | 
|---|
| 165 | InitDirectories(); | 
|---|
| 166 | RegisterDevices(); | 
|---|
| 167 | Win32DllBase::setDefaultRenaming(); | 
|---|
| 168 | rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo)); | 
|---|
| 169 | if (rc != 0) | 
|---|
| 170 | ulSysinfo = 1; | 
|---|
| 171 |  | 
|---|
| 172 | InitSystemEnvironment(ulSysinfo); | 
|---|
| 173 | break; | 
|---|
| 174 | } | 
|---|
| 175 | case 1 : | 
|---|
| 176 | UnregisterLxDll(hModule); | 
|---|
| 177 | break; | 
|---|
| 178 | default  : | 
|---|
| 179 | return 0UL; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | /***********************************************************/ | 
|---|
| 183 | /* A non-zero value must be returned to indicate success.  */ | 
|---|
| 184 | /***********************************************************/ | 
|---|
| 185 | return 1UL; | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 |  | 
|---|
| 189 | static void APIENTRY cleanup(ULONG ulReason) | 
|---|
| 190 | { | 
|---|
| 191 | dprintf(("kernel32 exit %d\n", ulReason)); | 
|---|
| 192 | //Flush and delete all open memory mapped files | 
|---|
| 193 | Win32MemMap::deleteAll(); | 
|---|
| 194 | WinExe = NULL; | 
|---|
| 195 |  | 
|---|
| 196 | WriteOutProfiles(); | 
|---|
| 197 | DestroyTIB(); | 
|---|
| 198 | DestroySharedHeap(); | 
|---|
| 199 | DestroyCodeHeap(); | 
|---|
| 200 | _ctordtorTerm(); | 
|---|
| 201 |  | 
|---|
| 202 | //NOTE: Must be done after DestroyTIB | 
|---|
| 203 | ClosePrivateLogFiles(); | 
|---|
| 204 | CloseLogFile(); | 
|---|
| 205 |  | 
|---|
| 206 | DosExitList(EXLST_EXIT, cleanup); | 
|---|
| 207 | return ; | 
|---|
| 208 | } | 
|---|