Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

libmgmt.c

Go to the documentation of this file.
00001 /****************************** Module Header ******************************\ 00002 * Module Name: libmgmt.c 00003 * 00004 * Copyright (c) 1985 - 1999, Microsoft Corporation 00005 * 00006 * This module contains the code to manage loading and freeing libraries 00007 * in use by USER. 00008 * 00009 * History: 00010 * 02-04-91 DavidPe Created. 00011 \***************************************************************************/ 00012 00013 #include "precomp.h" 00014 #pragma hdrstop 00015 00016 00017 /* 00018 * Globals local to this file 00019 * 00020 * 00021 * Number of hmodule entries in the module management table. 00022 */ 00023 int catomSysTableEntries; 00024 00025 /* 00026 * Array of atoms that are the fully qualified path names of each managed 00027 * module. 00028 */ 00029 ATOM aatomSysLoaded[CLIBS]; 00030 00031 /* 00032 * Count of processes that have LoadModule()'d each module. 00033 */ 00034 int acatomSysUse[CLIBS]; 00035 00036 /* 00037 * Count of hooks set into each module. 00038 */ 00039 int acatomSysDepends[CLIBS]; 00040 00041 00042 /****************************************************************************\ 00043 * GetHmodTableIndex 00044 * 00045 * This routine is used to return the index of a given atom within the system 00046 * wide hmod atom table. If the atom is not found, an attempt to allocate a 00047 * new table entry is made. If the attempt fails, -1 is returned. 00048 * 00049 * History: 00050 * 02-04-91 DavidPe Ported. 00051 \****************************************************************************/ 00052 00053 int GetHmodTableIndex( 00054 PUNICODE_STRING pstrLibName) 00055 { 00056 int i; 00057 ATOM atom; 00058 UNICODE_STRING strLibName; 00059 00060 /* 00061 * Probe string 00062 */ 00063 try { 00064 strLibName = ProbeAndReadUnicodeString(pstrLibName); 00065 ProbeForReadUnicodeStringBuffer(strLibName); 00066 atom = UserAddAtom(strLibName.Buffer, FALSE); 00067 } except (W32ExceptionHandler(FALSE, RIP_WARNING)) { 00068 return -1; 00069 } 00070 00071 /* 00072 * If we can't add the atom we're hosed 00073 * so return an error. 00074 */ 00075 if (atom == 0) { 00076 return -1; 00077 } 00078 00079 /* 00080 * Search for atom index 00081 */ 00082 for (i = 0; i < catomSysTableEntries && aatomSysLoaded[i] != atom; i++) 00083 ; 00084 00085 if (i == catomSysTableEntries) { 00086 00087 /* 00088 * Find empty entry for atom 00089 */ 00090 for (i = 0; i < catomSysTableEntries && aatomSysLoaded[i]; i++) 00091 ; 00092 00093 /* 00094 * Check if no empty entry found 00095 */ 00096 if (i == catomSysTableEntries) { 00097 if (i == CLIBS) { 00098 UserDeleteAtom(atom); 00099 RIPERR0(ERROR_NOT_ENOUGH_MEMORY, 00100 RIP_WARNING, 00101 "Memory allocation failed in GetHmodTableIndex"); 00102 00103 return -1; 00104 } 00105 00106 /* 00107 * Increase table size 00108 */ 00109 catomSysTableEntries++; 00110 } 00111 00112 /* 00113 * Set entry 00114 */ 00115 aatomSysLoaded[i] = atom; 00116 acatomSysUse[i] = 0; 00117 acatomSysDepends[i] = 0; 00118 } else { 00119 UserDeleteAtom(atom); 00120 } 00121 00122 return i; 00123 } 00124 00125 00126 /*****************************************************************************\ 00127 * AddHmodDependency 00128 * 00129 * This function merely increments the dependency count of a given hmod 00130 * atom table index. 00131 * 00132 * History: 00133 * 02-04-91 DavidPe Ported. 00134 \*****************************************************************************/ 00135 00136 VOID AddHmodDependency( 00137 int iatom) 00138 { 00139 UserAssert(iatom >= 0); 00140 if (iatom < catomSysTableEntries) { 00141 acatomSysDepends[iatom]++; 00142 } 00143 } 00144 00145 00146 /*****************************************************************************\ 00147 * RemoveHmodDependency 00148 * 00149 * This function removes a system dependency on a given index into the hmod 00150 * atom table. If all dependencies on the hmod have been removed (the Depends 00151 * count reaches zero) then the QS_SYSEXPUNGE bit is set in all message 00152 * queues so the eventually each process will do a free module on it. 00153 * 00154 * History: 00155 * 02-04-91 DavidPe Ported. 00156 \*****************************************************************************/ 00157 00158 VOID RemoveHmodDependency( 00159 int iatom) 00160 { 00161 00162 UserAssert(iatom >= 0); 00163 if (iatom < catomSysTableEntries && 00164 --acatomSysDepends[iatom] == 0) { 00165 00166 if (acatomSysUse[iatom]) { 00167 00168 /* 00169 * Cause each thread to check for expunged dlls 00170 * the next time they awake. 00171 */ 00172 gcSysExpunge++; 00173 gdwSysExpungeMask |= (1 << iatom); 00174 } else { 00175 aatomSysLoaded[iatom] = 0; 00176 } 00177 } 00178 } 00179 00180 00181 /*****************************************************************************\ 00182 * xxxLoadHmodIndex 00183 * 00184 * This function attempts to load the hmodule specified by iatom into the 00185 * system hmod table. Updates the per-process bitmap accordingly. Returns 00186 * NULL on success. 00187 * 00188 * History: 00189 * 02-04-91 DavidPe Ported. 00190 \*****************************************************************************/ 00191 00192 HANDLE xxxLoadHmodIndex( 00193 int iatom, 00194 BOOL bWx86KnownDll) 00195 { 00196 WCHAR pszLibName[MAX_PATH]; 00197 HANDLE hmod; 00198 UNICODE_STRING strLibrary; 00199 PTHREADINFO ptiCurrent = PtiCurrent(); 00200 00201 UserAssert((!gptiRit || gptiRit->ppi != PtiCurrent()->ppi) && 00202 "Shouldn't load global hooks on system process - gptiRit->ppi is the system process"); 00203 00204 if (iatom >= catomSysTableEntries) { 00205 RIPERR0(ERROR_INVALID_PARAMETER, RIP_WARNING, "Index out of range"); 00206 return NULL; 00207 } 00208 00209 UserGetAtomName(aatomSysLoaded[iatom], pszLibName, sizeof(pszLibName)/sizeof(WCHAR)); 00210 00211 /* 00212 * Call back the client to load the library. 00213 */ 00214 RtlInitUnicodeString(&strLibrary, pszLibName); 00215 hmod = ClientLoadLibrary(&strLibrary, bWx86KnownDll); 00216 00217 if (hmod != NULL) { 00218 /* 00219 * Check to make sure another thread hasn't loaded this library 00220 * while we were outside the critical section. 00221 */ 00222 if (!TESTHMODLOADED(ptiCurrent, iatom)) { 00223 /* 00224 * Go ahead and bump the reference count. 00225 */ 00226 acatomSysUse[iatom]++; 00227 SETHMODLOADED(ptiCurrent, iatom, hmod); 00228 00229 } else { 00230 /* 00231 * Another thread loaded it while we were outside the 00232 * critical section. Unload it so the system's 00233 * reference count is correct. 00234 */ 00235 ClientFreeLibrary(ptiCurrent->ppi->ahmodLibLoaded[iatom]); 00236 } 00237 } 00238 00239 return hmod; 00240 } 00241 00242 00243 /***********************************************************************\ 00244 * DoSysExpunge 00245 * 00246 * This function is called when a thread wakes up and finds its 00247 * QS_SYSEXPUNGE wakebit set. 00248 * 00249 * History: 00250 * 02-04-91 DavidPe Ported. 00251 \***********************************************************************/ 00252 00253 VOID xxxDoSysExpunge( 00254 PTHREADINFO pti) 00255 { 00256 int i; 00257 00258 /* 00259 * Clear this first before we potentially leave the critical section. 00260 */ 00261 pti->ppi->cSysExpunge = gcSysExpunge; 00262 00263 /* 00264 * Scan for libraries that have been freed 00265 */ 00266 for (i = 0; i < catomSysTableEntries; i++) { 00267 if ((acatomSysDepends[i] == 0) && (aatomSysLoaded[i] != 0) && 00268 TESTHMODLOADED(pti, i)) { 00269 00270 HANDLE hmodFree = pti->ppi->ahmodLibLoaded[i]; 00271 00272 /* 00273 * Clear this hmod for this process before we leave the 00274 * critical section. 00275 */ 00276 CLEARHMODLOADED(pti, i); 00277 00278 /* 00279 * Decrement the count of processes that have loaded this 00280 * .dll. If there are no more, then destroy the reference 00281 * to this .dll. 00282 */ 00283 if (--acatomSysUse[i] == 0) { 00284 UserDeleteAtom(aatomSysLoaded[i]); 00285 aatomSysLoaded[i] = 0; 00286 gdwSysExpungeMask &= ~(1 << i); 00287 } 00288 00289 /* 00290 * Call back the client to free the library... 00291 */ 00292 ClientFreeLibrary(hmodFree); 00293 } 00294 } 00295 }

Generated on Sat May 15 19:40:38 2004 for test by doxygen 1.3.7