LCOV - code coverage report
Current view: top level - src/backend/port - pg_sema.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 70.7 % 58 41
Test Date: 2026-07-25 22:15:46 Functions: 90.0 % 10 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 23.2 % 56 13

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * posix_sema.c
       4                 :             :  *    Implement PGSemaphores using POSIX semaphore facilities
       5                 :             :  *
       6                 :             :  * We prefer the unnamed style of POSIX semaphore (the kind made with
       7                 :             :  * sem_init).  We can cope with the kind made with sem_open, however.
       8                 :             :  *
       9                 :             :  * In either implementation, typedef PGSemaphore is equivalent to "sem_t *".
      10                 :             :  * With unnamed semaphores, the sem_t structs live in an array in shared
      11                 :             :  * memory.  With named semaphores, that's not true because we cannot persuade
      12                 :             :  * sem_open to do its allocation there.  Therefore, the named-semaphore code
      13                 :             :  * *does not cope with EXEC_BACKEND*.  The sem_t structs will just be in the
      14                 :             :  * postmaster's private memory, where they are successfully inherited by
      15                 :             :  * forked backends, but they could not be accessed by exec'd backends.
      16                 :             :  *
      17                 :             :  *
      18                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      19                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      20                 :             :  *
      21                 :             :  * IDENTIFICATION
      22                 :             :  *    src/backend/port/posix_sema.c
      23                 :             :  *
      24                 :             :  *-------------------------------------------------------------------------
      25                 :             :  */
      26                 :             : #include "postgres.h"
      27                 :             : 
      28                 :             : #include <fcntl.h>
      29                 :             : #include <semaphore.h>
      30                 :             : #include <signal.h>
      31                 :             : #include <unistd.h>
      32                 :             : #include <sys/stat.h>
      33                 :             : 
      34                 :             : #include "miscadmin.h"
      35                 :             : #include "storage/ipc.h"
      36                 :             : #include "storage/pg_sema.h"
      37                 :             : #include "storage/shmem.h"
      38                 :             : 
      39                 :             : 
      40                 :             : /* see file header comment */
      41                 :             : #if defined(USE_NAMED_POSIX_SEMAPHORES) && defined(EXEC_BACKEND)
      42                 :             : #error cannot use named POSIX semaphores with EXEC_BACKEND
      43                 :             : #endif
      44                 :             : 
      45                 :             : typedef union SemTPadded
      46                 :             : {
      47                 :             :     sem_t       pgsem;
      48                 :             :     char        pad[PG_CACHE_LINE_SIZE];
      49                 :             : } SemTPadded;
      50                 :             : 
      51                 :             : /* typedef PGSemaphore is equivalent to pointer to sem_t */
      52                 :             : typedef struct PGSemaphoreData
      53                 :             : {
      54                 :             :     SemTPadded  sem_padded;
      55                 :             : } PGSemaphoreData;
      56                 :             : 
      57                 :             : #define PG_SEM_REF(x)   (&(x)->sem_padded.pgsem)
      58                 :             : 
      59                 :             : #define IPCProtection   (0600)  /* access/modify by user only */
      60                 :             : 
      61                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
      62                 :             : static sem_t **mySemPointers;   /* keep track of created semaphores */
      63                 :             : #else
      64                 :             : static PGSemaphore sharedSemas; /* array of PGSemaphoreData in shared memory */
      65                 :             : #endif
      66                 :             : static int  numSems;            /* number of semas acquired so far */
      67                 :             : static int  maxSems;            /* allocated size of above arrays */
      68                 :             : static int  nextSemKey;         /* next name to try */
      69                 :             : 
      70                 :             : 
      71                 :             : static void ReleaseSemaphores(int status, Datum arg);
      72                 :             : 
      73                 :             : 
      74                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
      75                 :             : 
      76                 :             : /*
      77                 :             :  * PosixSemaphoreCreate
      78                 :             :  *
      79                 :             :  * Attempt to create a new named semaphore.
      80                 :             :  *
      81                 :             :  * If we fail with a failure code other than collision-with-existing-sema,
      82                 :             :  * print out an error and abort.  Other types of errors suggest nonrecoverable
      83                 :             :  * problems.
      84                 :             :  */
      85                 :             : static sem_t *
      86                 :             : PosixSemaphoreCreate(void)
      87                 :             : {
      88                 :             :     int         semKey;
      89                 :             :     char        semname[64];
      90                 :             :     sem_t      *mySem;
      91                 :             : 
      92                 :             :     for (;;)
      93                 :             :     {
      94                 :             :         semKey = nextSemKey++;
      95                 :             : 
      96                 :             :         snprintf(semname, sizeof(semname), "/pgsql-%d", semKey);
      97                 :             : 
      98                 :             :         mySem = sem_open(semname, O_CREAT | O_EXCL,
      99                 :             :                          (mode_t) IPCProtection, (unsigned) 1);
     100                 :             : 
     101                 :             : #ifdef SEM_FAILED
     102                 :             :         if (mySem != (sem_t *) SEM_FAILED)
     103                 :             :             break;
     104                 :             : #else
     105                 :             :         if (mySem != (sem_t *) (-1))
     106                 :             :             break;
     107                 :             : #endif
     108                 :             : 
     109                 :             :         /* Loop if error indicates a collision */
     110                 :             :         if (errno == EEXIST || errno == EACCES || errno == EINTR)
     111                 :             :             continue;
     112                 :             : 
     113                 :             :         /*
     114                 :             :          * Else complain and abort
     115                 :             :          */
     116                 :             :         elog(FATAL, "sem_open(\"%s\") failed: %m", semname);
     117                 :             :     }
     118                 :             : 
     119                 :             :     /*
     120                 :             :      * Unlink the semaphore immediately, so it can't be accessed externally.
     121                 :             :      * This also ensures that it will go away if we crash.
     122                 :             :      */
     123                 :             :     sem_unlink(semname);
     124                 :             : 
     125                 :             :     return mySem;
     126                 :             : }
     127                 :             : #else                           /* !USE_NAMED_POSIX_SEMAPHORES */
     128                 :             : 
     129                 :             : /*
     130                 :             :  * PosixSemaphoreCreate
     131                 :             :  *
     132                 :             :  * Attempt to create a new unnamed semaphore.
     133                 :             :  */
     134                 :             : static void
     135                 :      164035 : PosixSemaphoreCreate(sem_t *sem)
     136                 :             : {
     137         [ -  + ]:      164035 :     if (sem_init(sem, 1, 1) < 0)
     138         [ #  # ]:           0 :         elog(FATAL, "sem_init failed: %m");
     139                 :      164035 : }
     140                 :             : #endif                          /* USE_NAMED_POSIX_SEMAPHORES */
     141                 :             : 
     142                 :             : 
     143                 :             : /*
     144                 :             :  * PosixSemaphoreKill   - removes a semaphore
     145                 :             :  */
     146                 :             : static void
     147                 :      164035 : PosixSemaphoreKill(sem_t *sem)
     148                 :             : {
     149                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
     150                 :             :     /* Got to use sem_close for named semaphores */
     151                 :             :     if (sem_close(sem) < 0)
     152                 :             :         elog(LOG, "sem_close failed: %m");
     153                 :             : #else
     154                 :             :     /* Got to use sem_destroy for unnamed semaphores */
     155         [ -  + ]:      164035 :     if (sem_destroy(sem) < 0)
     156         [ #  # ]:           0 :         elog(LOG, "sem_destroy failed: %m");
     157                 :             : #endif
     158                 :      164035 : }
     159                 :             : 
     160                 :             : 
     161                 :             : /*
     162                 :             :  * Request shared memory needed for semaphores
     163                 :             :  */
     164                 :             : void
     165                 :        1260 : PGSemaphoreShmemRequest(int maxSemas)
     166                 :             : {
     167                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
     168                 :             :     /* No shared memory needed in this case */
     169                 :             : #else
     170                 :             :     /* Need a PGSemaphoreData per semaphore */
     171                 :        1260 :     ShmemRequestStruct(.name = "Semaphores",
     172                 :             :                        .size = mul_size(maxSemas, sizeof(PGSemaphoreData)),
     173                 :             :                        .ptr = (void **) &sharedSemas,
     174                 :             :         );
     175                 :             : #endif
     176                 :        1260 : }
     177                 :             : 
     178                 :             : /*
     179                 :             :  * PGSemaphoreInit --- initialize semaphore support
     180                 :             :  *
     181                 :             :  * This is called during postmaster start or shared memory reinitialization.
     182                 :             :  * It should do whatever is needed to be able to support up to maxSemas
     183                 :             :  * subsequent PGSemaphoreCreate calls.  Also, if any system resources
     184                 :             :  * are acquired here or in PGSemaphoreCreate, register an on_shmem_exit
     185                 :             :  * callback to release them.
     186                 :             :  *
     187                 :             :  * In the Posix implementation, we acquire semaphores on-demand; the
     188                 :             :  * maxSemas parameter is just used to size the arrays.  For unnamed
     189                 :             :  * semaphores, there is an array of PGSemaphoreData structs in shared memory.
     190                 :             :  * For named semaphores, we keep a postmaster-local array of sem_t pointers,
     191                 :             :  * which we use for releasing the semaphores when done.
     192                 :             :  * (This design minimizes the dependency of postmaster shutdown on the
     193                 :             :  * contents of shared memory, which a failed backend might have clobbered.
     194                 :             :  * We can't do much about the possibility of sem_destroy() crashing, but
     195                 :             :  * we don't have to expose the counters to other processes.)
     196                 :             :  */
     197                 :             : void
     198                 :        1257 : PGSemaphoreInit(int maxSemas)
     199                 :             : {
     200                 :             :     struct stat statbuf;
     201                 :             : 
     202                 :             :     /*
     203                 :             :      * We use the data directory's inode number to seed the search for free
     204                 :             :      * semaphore keys.  This minimizes the odds of collision with other
     205                 :             :      * postmasters, while maximizing the odds that we will detect and clean up
     206                 :             :      * semaphores left over from a crashed postmaster in our own directory.
     207                 :             :      */
     208         [ -  + ]:        1257 :     if (stat(DataDir, &statbuf) < 0)
     209         [ #  # ]:           0 :         ereport(FATAL,
     210                 :             :                 (errcode_for_file_access(),
     211                 :             :                  errmsg("could not stat data directory \"%s\": %m",
     212                 :             :                         DataDir)));
     213                 :             : 
     214                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
     215                 :             :     mySemPointers = (sem_t **) malloc(maxSemas * sizeof(sem_t *));
     216                 :             :     if (mySemPointers == NULL)
     217                 :             :         elog(PANIC, "out of memory");
     218                 :             : #endif
     219                 :             : 
     220                 :        1257 :     numSems = 0;
     221                 :        1257 :     maxSems = maxSemas;
     222                 :        1257 :     nextSemKey = statbuf.st_ino;
     223                 :             : 
     224                 :        1257 :     on_shmem_exit(ReleaseSemaphores, 0);
     225                 :        1257 : }
     226                 :             : 
     227                 :             : /*
     228                 :             :  * Release semaphores at shutdown or shmem reinitialization
     229                 :             :  *
     230                 :             :  * (called as an on_shmem_exit callback, hence funny argument list)
     231                 :             :  */
     232                 :             : static void
     233                 :        1257 : ReleaseSemaphores(int status, Datum arg)
     234                 :             : {
     235                 :             :     int         i;
     236                 :             : 
     237                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
     238                 :             :     for (i = 0; i < numSems; i++)
     239                 :             :         PosixSemaphoreKill(mySemPointers[i]);
     240                 :             :     free(mySemPointers);
     241                 :             : #endif
     242                 :             : 
     243                 :             : #ifdef USE_UNNAMED_POSIX_SEMAPHORES
     244         [ +  + ]:      165292 :     for (i = 0; i < numSems; i++)
     245                 :      164035 :         PosixSemaphoreKill(PG_SEM_REF(sharedSemas + i));
     246                 :             : #endif
     247                 :        1257 : }
     248                 :             : 
     249                 :             : /*
     250                 :             :  * PGSemaphoreCreate
     251                 :             :  *
     252                 :             :  * Allocate a PGSemaphore structure with initial count 1
     253                 :             :  */
     254                 :             : PGSemaphore
     255                 :      164035 : PGSemaphoreCreate(void)
     256                 :             : {
     257                 :             :     PGSemaphore sema;
     258                 :             :     sem_t      *newsem;
     259                 :             : 
     260                 :             :     /* Can't do this in a backend, because static state is postmaster's */
     261                 :             :     Assert(!IsUnderPostmaster);
     262                 :             : 
     263         [ -  + ]:      164035 :     if (numSems >= maxSems)
     264         [ #  # ]:           0 :         elog(PANIC, "too many semaphores created");
     265                 :             : 
     266                 :             : #ifdef USE_NAMED_POSIX_SEMAPHORES
     267                 :             :     newsem = PosixSemaphoreCreate();
     268                 :             :     /* Remember new sema for ReleaseSemaphores */
     269                 :             :     mySemPointers[numSems] = newsem;
     270                 :             :     sema = (PGSemaphore) newsem;
     271                 :             : #else
     272                 :      164035 :     sema = &sharedSemas[numSems];
     273                 :      164035 :     newsem = PG_SEM_REF(sema);
     274                 :      164035 :     PosixSemaphoreCreate(newsem);
     275                 :             : #endif
     276                 :             : 
     277                 :      164035 :     numSems++;
     278                 :             : 
     279                 :      164035 :     return sema;
     280                 :             : }
     281                 :             : 
     282                 :             : /*
     283                 :             :  * PGSemaphoreReset
     284                 :             :  *
     285                 :             :  * Reset a previously-initialized PGSemaphore to have count 0
     286                 :             :  */
     287                 :             : void
     288                 :       24822 : PGSemaphoreReset(PGSemaphore sema)
     289                 :             : {
     290                 :             :     /*
     291                 :             :      * There's no direct API for this in POSIX, so we have to ratchet the
     292                 :             :      * semaphore down to 0 with repeated trywait's.
     293                 :             :      */
     294                 :             :     for (;;)
     295                 :             :     {
     296         [ +  + ]:       39942 :         if (sem_trywait(PG_SEM_REF(sema)) < 0)
     297                 :             :         {
     298   [ -  +  -  - ]:       24822 :             if (errno == EAGAIN || errno == EDEADLK)
     299                 :             :                 break;          /* got it down to 0 */
     300         [ #  # ]:           0 :             if (errno == EINTR)
     301                 :           0 :                 continue;       /* can this happen? */
     302         [ #  # ]:           0 :             elog(FATAL, "sem_trywait failed: %m");
     303                 :             :         }
     304                 :             :     }
     305                 :       24822 : }
     306                 :             : 
     307                 :             : /*
     308                 :             :  * PGSemaphoreLock
     309                 :             :  *
     310                 :             :  * Lock a semaphore (decrement count), blocking if count would be < 0
     311                 :             :  */
     312                 :             : void
     313                 :     5464708 : PGSemaphoreLock(PGSemaphore sema)
     314                 :             : {
     315                 :             :     int         errStatus;
     316                 :             : 
     317                 :             :     /* See notes in sysv_sema.c's implementation of PGSemaphoreLock. */
     318                 :             :     do
     319                 :             :     {
     320                 :     5464708 :         errStatus = sem_wait(PG_SEM_REF(sema));
     321   [ -  +  -  - ]:     5464708 :     } while (errStatus < 0 && errno == EINTR);
     322                 :             : 
     323         [ -  + ]:     5464708 :     if (errStatus < 0)
     324         [ #  # ]:           0 :         elog(FATAL, "sem_wait failed: %m");
     325                 :     5464708 : }
     326                 :             : 
     327                 :             : /*
     328                 :             :  * PGSemaphoreUnlock
     329                 :             :  *
     330                 :             :  * Unlock a semaphore (increment count)
     331                 :             :  */
     332                 :             : void
     333                 :     5465089 : PGSemaphoreUnlock(PGSemaphore sema)
     334                 :             : {
     335                 :             :     int         errStatus;
     336                 :             : 
     337                 :             :     /*
     338                 :             :      * Note: if errStatus is -1 and errno == EINTR then it means we returned
     339                 :             :      * from the operation prematurely because we were sent a signal.  So we
     340                 :             :      * try and unlock the semaphore again. Not clear this can really happen,
     341                 :             :      * but might as well cope.
     342                 :             :      */
     343                 :             :     do
     344                 :             :     {
     345                 :     5465089 :         errStatus = sem_post(PG_SEM_REF(sema));
     346   [ -  +  -  - ]:     5465089 :     } while (errStatus < 0 && errno == EINTR);
     347                 :             : 
     348         [ -  + ]:     5465089 :     if (errStatus < 0)
     349         [ #  # ]:           0 :         elog(FATAL, "sem_post failed: %m");
     350                 :     5465089 : }
     351                 :             : 
     352                 :             : /*
     353                 :             :  * PGSemaphoreTryLock
     354                 :             :  *
     355                 :             :  * Lock a semaphore only if able to do so without blocking
     356                 :             :  */
     357                 :             : bool
     358                 :           0 : PGSemaphoreTryLock(PGSemaphore sema)
     359                 :             : {
     360                 :             :     int         errStatus;
     361                 :             : 
     362                 :             :     /*
     363                 :             :      * Note: if errStatus is -1 and errno == EINTR then it means we returned
     364                 :             :      * from the operation prematurely because we were sent a signal.  So we
     365                 :             :      * try and lock the semaphore again.
     366                 :             :      */
     367                 :             :     do
     368                 :             :     {
     369                 :           0 :         errStatus = sem_trywait(PG_SEM_REF(sema));
     370   [ #  #  #  # ]:           0 :     } while (errStatus < 0 && errno == EINTR);
     371                 :             : 
     372         [ #  # ]:           0 :     if (errStatus < 0)
     373                 :             :     {
     374   [ #  #  #  # ]:           0 :         if (errno == EAGAIN || errno == EDEADLK)
     375                 :           0 :             return false;       /* failed to lock it */
     376                 :             :         /* Otherwise we got trouble */
     377         [ #  # ]:           0 :         elog(FATAL, "sem_trywait failed: %m");
     378                 :             :     }
     379                 :             : 
     380                 :           0 :     return true;
     381                 :             : }
        

Generated by: LCOV version 2.0-1