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

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

From
"Michael C. Thornburgh"
Date:

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



*** 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 )    {

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

From
Bruce Momjian
Date:
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@armory.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@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026