| 1 | /* $Id: initterm.cpp,v 1.44 2000-08-11 10:56:16 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 <exitlist.h>
|
|---|
| 51 | #include "oslibdos.h"
|
|---|
| 52 | #include <cpuhlp.h>
|
|---|
| 53 |
|
|---|
| 54 | #define DBG_LOCALLOG DBG_initterm
|
|---|
| 55 | #include "dbglocal.h"
|
|---|
| 56 |
|
|---|
| 57 | /*-------------------------------------------------------------------*/
|
|---|
| 58 | /* A clean up routine registered with DosExitList must be used if */
|
|---|
| 59 | /* runtime calls are required and the runtime is dynamically linked. */
|
|---|
| 60 | /* This will guarantee that this clean up routine is run before the */
|
|---|
| 61 | /* library DLL is terminated. */
|
|---|
| 62 | /*-------------------------------------------------------------------*/
|
|---|
| 63 | static void APIENTRY cleanup(ULONG reason);
|
|---|
| 64 |
|
|---|
| 65 | extern "C" {
|
|---|
| 66 | void CDECL _ctordtorInit( void );
|
|---|
| 67 | void CDECL _ctordtorTerm( void );
|
|---|
| 68 |
|
|---|
| 69 | //Win32 resource table (produced by wrc)
|
|---|
| 70 | extern DWORD _Resource_PEResTab;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | //Global DLL Data
|
|---|
| 74 | #pragma data_seg(_GLOBALDATA)
|
|---|
| 75 | int globLoadNr = 0;
|
|---|
| 76 | #pragma data_seg()
|
|---|
| 77 |
|
|---|
| 78 | /* Tue 03.03.1998: knut */
|
|---|
| 79 | ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
|
|---|
| 80 | ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
|
|---|
| 81 | int loadNr = 0;
|
|---|
| 82 | char kernel32Path[CCHMAXPATH] = "";
|
|---|
| 83 | static HMODULE dllHandle = 0;
|
|---|
| 84 |
|
|---|
| 85 | /****************************************************************************/
|
|---|
| 86 | /* _DLL_InitTerm is the function that gets called by the operating system */
|
|---|
| 87 | /* loader when it loads and frees this DLL for each process that accesses */
|
|---|
| 88 | /* this DLL. However, it only gets called the first time the DLL is loaded */
|
|---|
| 89 | /* and the last time it is freed for a particular process. The system */
|
|---|
| 90 | /* linkage convention MUST be used because the operating system loader is */
|
|---|
| 91 | /* calling this function. */
|
|---|
| 92 | /****************************************************************************/
|
|---|
| 93 | unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long
|
|---|
| 94 | ulFlag)
|
|---|
| 95 | {
|
|---|
| 96 | size_t i;
|
|---|
| 97 | APIRET rc;
|
|---|
| 98 | ULONG ulSysinfo;
|
|---|
| 99 |
|
|---|
| 100 | /*-------------------------------------------------------------------------*/
|
|---|
| 101 | /* If ulFlag is zero then the DLL is being loaded so initialization should */
|
|---|
| 102 | /* be performed. If ulFlag is 1 then the DLL is being freed so */
|
|---|
| 103 | /* termination should be performed. */
|
|---|
| 104 | /*-------------------------------------------------------------------------*/
|
|---|
| 105 |
|
|---|
| 106 | switch (ulFlag)
|
|---|
| 107 | {
|
|---|
| 108 | case 0 :
|
|---|
| 109 | {
|
|---|
| 110 | ParseLogStatus();
|
|---|
| 111 |
|
|---|
| 112 | loadNr = globLoadNr++;
|
|---|
| 113 |
|
|---|
| 114 | strcpy(kernel32Path, OSLibGetDllName(hModule));
|
|---|
| 115 | char *endofpath = strrchr(kernel32Path, '\\');
|
|---|
| 116 | *(endofpath+1) = 0;
|
|---|
| 117 | dprintf(("kernel32 init\n"));
|
|---|
| 118 | _ctordtorInit();
|
|---|
| 119 |
|
|---|
| 120 | CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
|
|---|
| 121 |
|
|---|
| 122 | OpenPrivateLogFiles();
|
|---|
| 123 |
|
|---|
| 124 | if(InitializeSharedHeap() == FALSE)
|
|---|
| 125 | return 0UL;
|
|---|
| 126 |
|
|---|
| 127 | if(InitializeCodeHeap() == FALSE)
|
|---|
| 128 | return 0UL;
|
|---|
| 129 |
|
|---|
| 130 | PROFILE_LoadOdinIni();
|
|---|
| 131 | dllHandle = RegisterLxDll(hModule, 0, (PVOID)&_Resource_PEResTab);
|
|---|
| 132 | if(dllHandle == 0)
|
|---|
| 133 | return 0UL;
|
|---|
| 134 |
|
|---|
| 135 | //SvL: Kernel32 is a special case; pe.exe loads it, so increase
|
|---|
| 136 | // the reference count here
|
|---|
| 137 | Win32DllBase *module = Win32DllBase::findModule(dllHandle);
|
|---|
| 138 | if(module) {
|
|---|
| 139 | module->AddRef();
|
|---|
| 140 | module->DisableUnload();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | /*******************************************************************/
|
|---|
| 144 | /* A DosExitList routine must be used to clean up if runtime calls */
|
|---|
| 145 | /* are required and the runtime is dynamically linked. */
|
|---|
| 146 | /*******************************************************************/
|
|---|
| 147 |
|
|---|
| 148 | rc = DosExitList(EXITLIST_KERNEL32|EXLST_ADD, cleanup);
|
|---|
| 149 | if (rc)
|
|---|
| 150 | return 0UL;
|
|---|
| 151 |
|
|---|
| 152 | /* knut: check for high memory support */
|
|---|
| 153 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
|
|---|
| 154 | if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
|
|---|
| 155 | {
|
|---|
| 156 | flAllocMem = PAG_ANY; // high memory support. Let's use it!
|
|---|
| 157 | ulMaxAddr = ulSysinfo * (1024*1024);
|
|---|
| 158 | OSLibInitWSeBFileIO();
|
|---|
| 159 | }
|
|---|
| 160 | else
|
|---|
| 161 | flAllocMem = 0; // no high memory support
|
|---|
| 162 |
|
|---|
| 163 | OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
|
|---|
| 164 |
|
|---|
| 165 | //SvL: Do it here instead of during the exe object creation
|
|---|
| 166 | //(std handles can be used in win32 dll initialization routines
|
|---|
| 167 | HMInitialize(); /* store standard handles within HandleManager */
|
|---|
| 168 | InitializeTIB(TRUE); //MUST be done after HMInitialize!
|
|---|
| 169 | InitDirectories();
|
|---|
| 170 | RegisterDevices();
|
|---|
| 171 | Win32DllBase::setDefaultRenaming();
|
|---|
| 172 | rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
|
|---|
| 173 | if (rc != 0)
|
|---|
| 174 | ulSysinfo = 1;
|
|---|
| 175 |
|
|---|
| 176 | InitSystemInfo(ulSysinfo);
|
|---|
| 177 | //Set up environment as found in NT
|
|---|
| 178 | InitEnvironment(ulSysinfo);
|
|---|
| 179 | break;
|
|---|
| 180 | }
|
|---|
| 181 | case 1 :
|
|---|
| 182 | if(dllHandle) {
|
|---|
| 183 | UnregisterLxDll(dllHandle);
|
|---|
| 184 | }
|
|---|
| 185 | break;
|
|---|
| 186 | default :
|
|---|
| 187 | return 0UL;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /***********************************************************/
|
|---|
| 191 | /* A non-zero value must be returned to indicate success. */
|
|---|
| 192 | /***********************************************************/
|
|---|
| 193 | return 1UL;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | static void APIENTRY cleanup(ULONG ulReason)
|
|---|
| 198 | {
|
|---|
| 199 | dprintf(("kernel32 exit %d\n", ulReason));
|
|---|
| 200 | //Flush and delete all open memory mapped files
|
|---|
| 201 | Win32MemMap::deleteAll();
|
|---|
| 202 | WinExe = NULL;
|
|---|
| 203 |
|
|---|
| 204 | WriteOutProfiles();
|
|---|
| 205 | DestroyTIB();
|
|---|
| 206 | DestroySharedHeap();
|
|---|
| 207 | DestroyCodeHeap();
|
|---|
| 208 | _ctordtorTerm();
|
|---|
| 209 |
|
|---|
| 210 | //NOTE: Must be done after DestroyTIB
|
|---|
| 211 | ClosePrivateLogFiles();
|
|---|
| 212 | CloseLogFile();
|
|---|
| 213 |
|
|---|
| 214 | DosExitList(EXLST_EXIT, cleanup);
|
|---|
| 215 | return ;
|
|---|
| 216 | }
|
|---|