Re: patch for src/backend/port/darwin/sem.c

Lists: pgsql-patches
From: "Michael C(dot) Thornburgh" <zenomt(at)armory(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: patch for src/backend/port/darwin/sem.c
Date: 2001-01-14 16:14:38
Message-ID: 10101141614.aa04571@aldebaran.armory.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

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(at)armory(dot)com

*** src/backend/port/darwin/sem.c.orig Sun Jan 14 15:11:28 2001
--- src/backend/port/darwin/sem.c Sun Jan 14 15:32:18 2001
***************
*** 155,165 ****
fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
if (fd == -1 && errno == EEXIST)
{
! exist = 1;
! fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT, MODE);
}
if (fd == -1)
return fd;
/* The size may only be set once. Ignore errors. */
ftruncate(fd, sizeof(struct sem_info));
SemInfo = mmap(NULL, sizeof(struct sem_info),
--- 155,167 ----
fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
if (fd == -1 && errno == EEXIST)
{
! /* exist = 1; */
! shm_unlink(SHM_INFO_NAME);
! fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
}
if (fd == -1)
return fd;
+ shm_unlink(SHM_INFO_NAME);
/* The size may only be set once. Ignore errors. */
ftruncate(fd, sizeof(struct sem_info));
SemInfo = mmap(NULL, sizeof(struct sem_info),
***************
*** 174,179 ****
--- 176,182 ----
fprintf(stderr, "darwin creating sem %s to cover shared mem.\n", semname);
#endif
SemInfo->sem = sem_open(semname, O_CREAT, semflg & 0777, 1);
+ sem_unlink(semname);
sem_wait(SemInfo->sem);
/* initilize shared memory */
memset(SemInfo->set, 0, sizeof(SemInfo->set));
***************
*** 244,249 ****
--- 247,253 ----
fprintf(stderr, "darwin creating sem %s to cover set %d num %dm.\n", semname, semid, semnum);
#endif
SemInfo->set[semid].sem[semnum] = sem_open(semname, O_CREAT, semflg & 0777, 0);
+ sem_unlink(semname);

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


From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: "Michael C(dot) Thornburgh" <zenomt(at)armory(dot)com>
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: patch for src/backend/port/darwin/sem.c
Date: 2001-01-17 22:11:15
Message-ID: 200101172211.RAA11570@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Thanks. Applied.

>
>
> 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(at)armory(dot)com
>
>
>
> *** src/backend/port/darwin/sem.c.orig Sun Jan 14 15:11:28 2001
> --- src/backend/port/darwin/sem.c Sun Jan 14 15:32:18 2001
> ***************
> *** 155,165 ****
> fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
> if (fd == -1 && errno == EEXIST)
> {
> ! exist = 1;
> ! fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT, MODE);
> }
> if (fd == -1)
> return fd;
> /* The size may only be set once. Ignore errors. */
> ftruncate(fd, sizeof(struct sem_info));
> SemInfo = mmap(NULL, sizeof(struct sem_info),
> --- 155,167 ----
> fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
> if (fd == -1 && errno == EEXIST)
> {
> ! /* exist = 1; */
> ! shm_unlink(SHM_INFO_NAME);
> ! fd = shm_open(SHM_INFO_NAME, O_RDWR | O_CREAT | O_EXCL, MODE);
> }
> if (fd == -1)
> return fd;
> + shm_unlink(SHM_INFO_NAME);
> /* The size may only be set once. Ignore errors. */
> ftruncate(fd, sizeof(struct sem_info));
> SemInfo = mmap(NULL, sizeof(struct sem_info),
> ***************
> *** 174,179 ****
> --- 176,182 ----
> fprintf(stderr, "darwin creating sem %s to cover shared mem.\n", semname);
> #endif
> SemInfo->sem = sem_open(semname, O_CREAT, semflg & 0777, 1);
> + sem_unlink(semname);
> sem_wait(SemInfo->sem);
> /* initilize shared memory */
> memset(SemInfo->set, 0, sizeof(SemInfo->set));
> ***************
> *** 244,249 ****
> --- 247,253 ----
> fprintf(stderr, "darwin creating sem %s to cover set %d num %dm.\n", semname, semid, semnum);
> #endif
> SemInfo->set[semid].sem[semnum] = sem_open(semname, O_CREAT, semflg & 0777, 0);
> + sem_unlink(semname);
>
> /* Currently sem_init always returns -1.
> if( sem_init( &SemInfo->set[semid].sem[semnum], 1, 0 ) == -1 ) {
>

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000
+ If your life is a hard drive, | 830 Blythe Avenue
+ Christ can be your backup. | Drexel Hill, Pennsylvania 19026