diff --git a/checkin_notes b/checkin_notes index b5bc1e2705..b0d0c328e2 100755 --- a/checkin_notes +++ b/checkin_notes @@ -14229,3 +14229,12 @@ David 27 June 2004 hostinfo_unix.C lib/ app_ipc.h + +Rom 27 June 2004 + - Prevent the reuse of shared memory segments with ones that are already + in use on Windows. + + client/ + app.C + lib/ + shmem.C diff --git a/client/app.C b/client/app.C index f9b0c7df7e..f2095037b7 100644 --- a/client/app.C +++ b/client/app.C @@ -243,7 +243,23 @@ int ACTIVE_TASK::write_app_init_file(APP_INIT_DATA& aid) { // make a unique key for core/app shared memory segment // #ifdef _WIN32 - sprintf(aid.comm_obj_name, "boinc_%d", slot); + int i = 0; + char szSharedMemoryName[256]; + HANDLE hSharedMemoryHandle; + + do { + memset(szSharedMemoryName, '\0', sizeof(szSharedMemoryName)); + sprintf(szSharedMemoryName, "boinc_%d", slot); + i++; + } while((!(hSharedMemoryHandle = create_shmem(szSharedMemoryName, 1024, NULL))) || (1024 < i)); + + if (hSharedMemoryHandle) + CloseHandle(hSharedMemoryHandle); + + if (1024 < i) + return ERR_SEMOP; + + strcpy(aid.comm_obj_name, szSharedMemoryName); #elif HAVE_SYS_IPC_H aid.shm_key = ftok(init_data_path, slot); #else diff --git a/lib/shmem.C b/lib/shmem.C index bba11c923d..8d078c268f 100755 --- a/lib/shmem.C +++ b/lib/shmem.C @@ -57,6 +57,7 @@ HANDLE create_shmem(LPCTSTR seg_name, int size, void** pp) { hSharedMem = CreateFileMapping(INVALID_HANDLE_VALUE, &security, PAGE_READWRITE, 0, size, seg_name); if (!hSharedMem) return NULL; + if (hSharedMem && (ERROR_ALREADY_EXISTS == GetLastError())) return NULL; pMemPtr = MapViewOfFile( hSharedMem, FILE_MAP_ALL_ACCESS, 0, 0, 0 ); *pp = pMemPtr;