commit a4575a99132f24e42f4de6cae10d3663832615fa
parent 5431186e5c82220b2db96bf653a1fd91730ba172
Author: Nils Durner <durner@gnunet.org>
Date: Sun, 13 Mar 2005 20:34:12 +0000
initialize POSIX emulation properly and close mmaped() file under Windows
Diffstat:
2 files changed, 83 insertions(+), 10 deletions(-)
diff --git a/src/main/extractor.c b/src/main/extractor.c
@@ -181,16 +181,6 @@ const char * EXTRACTOR_getDefaultLibraries() {
/* ************library initialization ***************** */
-#ifdef MINGW
-void __attribute__ ((constructor)) le_win_init(void) {
- InitWinEnv();
-}
-
-void __attribute__ ((destructor)) le_win_fini(void) {
- ShutdownWinEnv();
-}
-#endif
-
static char * old_dlsearchpath = NULL;
/* using libtool, needs init! */
@@ -221,12 +211,19 @@ void __attribute__ ((constructor)) le_ltdl_init(void) {
if (strstr (lt_dlgetsearchpath (), PLUGIN_PATH) == NULL)
lt_dladdsearchdir (PLUGIN_PATH);
#endif
+#ifdef MINGW
+ InitWinEnv();
+#endif
}
void __attribute__ ((destructor)) le_ltdl_fini(void) {
lt_dlsetsearchpath(old_dlsearchpath);
if (old_dlsearchpath != NULL)
free(old_dlsearchpath);
+#ifdef MINGW
+ ShutdownWinEnv();
+#endif
+
lt_dlexit ();
}
diff --git a/src/main/winproc.c b/src/main/winproc.c
@@ -42,6 +42,11 @@ const char *errlist[] = {
gettext_noop("Unknown resolver error") /* errno > 4 */
};
+typedef struct {
+ char *pStart;
+ HANDLE hMapping;
+} TMapping;
+
static char szRootDir[_MAX_PATH + 1];
static long lRootDirLen;
static char szHomeDir[_MAX_PATH + 2];
@@ -51,6 +56,9 @@ static OSVERSIONINFO theWinVersion;
unsigned int uiSockCount = 0;
Winsock *pSocks;
static char __langinfo[251];
+static unsigned int uiMappingsCount = 0;
+static TMapping *pMappings;
+HANDLE hMappingsLock;
static HINSTANCE hNTDLL, hIphlpapi;
TNtQuerySystemInformation GNNtQuerySystemInformation;
@@ -428,6 +436,11 @@ void InitWinEnv()
exit(1);
}
+ /* To keep track of mapped files */
+ pMappings = (TMapping *) malloc(sizeof(TMapping));
+ pMappings[0].pStart = NULL;
+ hMappingsLock = CreateMutex(NULL, FALSE, NULL);
+
/* Open files in binary mode */
_fmode = _O_BINARY;
@@ -475,6 +488,9 @@ void InitWinEnv()
*/
void ShutdownWinEnv()
{
+ free(pMappings);
+ CloseHandle(hMappingsLock);
+
FreeLibrary(hNTDLL);
FreeLibrary(hIphlpapi);
}
@@ -1090,6 +1106,8 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
HANDLE h, hFile;
SECURITY_ATTRIBUTES sec_none;
void *base;
+ BOOL bFound = FALSE;
+ unsigned int uiIndex;
errno = 0;
@@ -1144,6 +1162,42 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
CloseHandle(h);
return (void *) -1;
}
+
+ /* Save mapping handle */
+ WaitForSingleObject(hMappingsLock, INFINITE);
+
+ for(uiIndex = 0; uiIndex <= uiMappingsCount; uiIndex++)
+ {
+ if (pMappings[uiIndex].pStart == base)
+ {
+ bFound = 1;
+ break;
+ }
+ }
+
+ if (! bFound)
+ {
+ uiIndex = 0;
+
+ while(TRUE)
+ {
+ if (pMappings[uiIndex].pStart == NULL)
+ {
+ pMappings[uiIndex].pStart = base;
+ pMappings[uiIndex].hMapping = h;
+ }
+ if (uiIndex == uiMappingsCount)
+ {
+ uiMappingsCount++;
+ pMappings = (TMapping *) realloc(pMappings, (uiMappingsCount + 1) * sizeof(TMapping));
+ pMappings[uiMappingsCount].pStart = NULL;
+
+ break;
+ }
+ uiIndex++;
+ }
+ }
+ ReleaseMutex(hMappingsLock);
return base;
}
@@ -1155,8 +1209,30 @@ void *_win_mmap(void *start, size_t len, int access, int flags, int fd,
*/
int _win_munmap(void *start, size_t length)
{
+ unsigned uiIndex;
BOOL success = UnmapViewOfFile(start);
SetErrnoFromWinError(GetLastError());
+
+ if (success)
+ {
+ /* Release mapping handle */
+ WaitForSingleObject(hMappingsLock, INFINITE);
+
+ for(uiIndex = 0; uiIndex <= uiMappingsCount; uiIndex++)
+ {
+ if (pMappings[uiIndex].pStart == start)
+ {
+ success = CloseHandle(pMappings[uiIndex].hMapping);
+ SetErrnoFromWinError(GetLastError());
+ pMappings[uiIndex].pStart = NULL;
+ pMappings[uiIndex].hMapping = NULL;
+
+ break;
+ }
+ }
+
+ ReleaseMutex(hMappingsLock);
+ }
return success ? 0 : -1;
}