Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 978c03f

Browse files
committed
attached is a patch that makes SysV semaphore emulation
using POSIX semaphores more robust on Darwin 1.2/Mac OS X Public Beta. this is for the version of 7.1 available via anon cvs as of Jan 14 2001 14:00 PST. since the semaphores and shared memory created by this emulator are shared with the backends via fork(), their persistent names are not necessary. removing their names with shm_unlink() and sem_unlink() after creation obviates the need for any "ipcclean" function. further, without these changes, the shared memory (and, therefore, the semaphores) will not be re-initialized/re-created after the first execution of the postmaster, until reboot or until some (non-existent) ipcclean function is executed. this patch does the following: 1) if the shared memory segment "SysV_Sem_Info" already existed, it is cleaned up. it shouldn't be there anyways. 2) the real indicator for whether the shared memory/semaphore emulator has been initialized is if "SemInfo" has been initialized. the shared memory and semaphores must be initialized regardless of whether there was a garbage shared memory segment lying around. 3) the shared memory segment "SysV_Sem_Info" is created with "O_EXCL" to catch the case where two postmasters might be starting simultaneously, so they don't both end up with the same shared memory (one will fail). note that this can't be done with the semaphores because Darwin 1.2 has a bug where attempting to open an existing semaphore with "O_EXCL" set will ruin the semaphore until the next reboot. 4) the shared memory segment "SysV_Sem_Info" is unlinked after it is created. it will then exist without a name until the postmaster and all backend children exit. 5) all semaphores are unlinked after they are created. they'll then exist without names until the postmaster and all backend children exit. -michael thornburgh, zenomt@armory.com
1 parent 5088f07 commit 978c03f

File tree

1 file changed

+7
-3
lines changed
  • src/backend/port/darwin

1 file changed

+7
-3
lines changed

src/backend/port/darwin/sem.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* - this required changing sem_info from containig an array of sem_t to an array of sem_t*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/port/darwin/Attic/sem.c,v 1.1 2000/12/11 00:49:54 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/port/darwin/Attic/sem.c,v 1.2 2001/01/17 22:11:19 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -155,11 +155,13 @@ semget(key_t key, int nsems, int semflg)
155155
fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
156156
if (fd == -1 && errno == EEXIST)
157157
{
158-
exist = 1;
159-
fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT, MODE);
158+
/* exist = 1; */
159+
shm_unlink(SHM_INFO_NAME);
160+
fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
160161
}
161162
if (fd == -1)
162163
return fd;
164+
shm_unlink(SHM_INFO_NAME);
163165
/* The size may only be set once. Ignore errors. */
164166
ftruncate(fd, sizeof(struct sem_info));
165167
SemInfo = mmap(NULL, sizeof(struct sem_info),
@@ -174,6 +176,7 @@ semget(key_t key, int nsems, int semflg)
174176
fprintf(stderr, "darwin creating sem %s to cover shared mem.\n", semname);
175177
#endif
176178
SemInfo->sem = sem_open(semname, O_CREAT, semflg & 0777, 1);
179+
sem_unlink(semname);
177180
sem_wait(SemInfo->sem);
178181
/* initilize shared memory */
179182
memset(SemInfo->set, 0, sizeof(SemInfo->set));
@@ -244,6 +247,7 @@ fprintf(stderr, "darwin semget failed because if (nsems != 0 && SemInfo->set[sem
244247
fprintf(stderr, "darwin creating sem %s to cover set %d num %dm.\n", semname, semid, semnum);
245248
#endif
246249
SemInfo->set[semid].sem[semnum] = sem_open(semname, O_CREAT, semflg & 0777, 0);
250+
sem_unlink(semname);
247251

248252
/* Currently sem_init always returns -1.
249253
if( sem_init( &SemInfo->set[semid].sem[semnum], 1, 0 ) == -1 ) {

0 commit comments

Comments
 (0)