* PGSharedMemoryIsInUse * * Is a previously-existing shmem segment still existing and in use? * * The point of this exercise is to detect the case where a prior postmaster * crashed, but it left child backends that are still running. Therefore * we only care about shmem segments that are associated with the intended * DataDir. This is an important consideration since accidental matches of
| 317 | * shmem segment IDs are reasonably common. |
| 318 | */ |
| 319 | bool |
| 320 | PGSharedMemoryIsInUse(unsigned long id1, unsigned long id2) |
| 321 | { |
| 322 | PGShmemHeader *memAddress; |
| 323 | IpcMemoryState state; |
| 324 | |
| 325 | state = PGSharedMemoryAttach((IpcMemoryId) id2, NULL, &memAddress); |
| 326 | if (memAddress && shmdt(memAddress) < 0) |
| 327 | elog(LOG, "shmdt(%p) failed: %m", memAddress); |
| 328 | switch (state) |
| 329 | { |
| 330 | case SHMSTATE_ENOENT: |
| 331 | case SHMSTATE_FOREIGN: |
| 332 | case SHMSTATE_UNATTACHED: |
| 333 | return false; |
| 334 | case SHMSTATE_ANALYSIS_FAILURE: |
| 335 | case SHMSTATE_ATTACHED: |
| 336 | return true; |
| 337 | } |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Test for a segment with id shmId; see comment at IpcMemoryState. |
no test coverage detected