1 /*-------------------------------------------------------------------------
4 * Implement PGSemaphores using SysV semaphore facilities
7 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/port/sysv_sema.c
13 *-------------------------------------------------------------------------
27 #include "miscadmin.h"
28 #include "storage/ipc.h"
29 #include "storage/pg_sema.h"
32 #ifndef HAVE_UNION_SEMUN
37 unsigned short *array;
41 typedef key_t IpcSemaphoreKey; /* semaphore key passed to semget(2) */
42 typedef int IpcSemaphoreId; /* semaphore ID returned by semget(2) */
45 * SEMAS_PER_SET is the number of useful semaphores in each semaphore set
46 * we allocate. It must be *less than* your kernel's SEMMSL (max semaphores
47 * per set) parameter, which is often around 25. (Less than, because we
48 * allocate one extra sema in each set for identification purposes.)
50 #define SEMAS_PER_SET 16
52 #define IPCProtection (0600) /* access/modify by user only */
54 #define PGSemaMagic 537 /* must be less than SEMVMX */
57 static IpcSemaphoreId *mySemaSets; /* IDs of sema sets acquired so far */
58 static int numSemaSets; /* number of sema sets acquired so far */
59 static int maxSemaSets; /* allocated size of mySemaSets array */
60 static IpcSemaphoreKey nextSemaKey; /* next key to try using */
61 static int nextSemaNumber; /* next free sem num in last sema set */
64 static IpcSemaphoreId InternalIpcSemaphoreCreate(IpcSemaphoreKey semKey,
66 static void IpcSemaphoreInitialize(IpcSemaphoreId semId, int semNum,
68 static void IpcSemaphoreKill(IpcSemaphoreId semId);
69 static int IpcSemaphoreGetValue(IpcSemaphoreId semId, int semNum);
70 static pid_t IpcSemaphoreGetLastPID(IpcSemaphoreId semId, int semNum);
71 static IpcSemaphoreId IpcSemaphoreCreate(int numSems);
72 static void ReleaseSemaphores(int status, Datum arg);
76 * InternalIpcSemaphoreCreate
78 * Attempt to create a new semaphore set with the specified key.
79 * Will fail (return -1) if such a set already exists.
81 * If we fail with a failure code other than collision-with-existing-set,
82 * print out an error and abort. Other types of errors suggest nonrecoverable
86 InternalIpcSemaphoreCreate(IpcSemaphoreKey semKey, int numSems)
90 semId = semget(semKey, numSems, IPC_CREAT | IPC_EXCL | IPCProtection);
94 int saved_errno = errno;
97 * Fail quietly if error indicates a collision with existing set. One
98 * would expect EEXIST, given that we said IPC_EXCL, but perhaps we
99 * could get a permission violation instead? Also, EIDRM might occur
100 * if an old set is slated for destruction but not gone yet.
102 if (saved_errno == EEXIST || saved_errno == EACCES
104 || saved_errno == EIDRM
110 * Else complain and abort
113 (errmsg("could not create semaphores: %m"),
114 errdetail("Failed system call was semget(%lu, %d, 0%o).",
115 (unsigned long) semKey, numSems,
116 IPC_CREAT | IPC_EXCL | IPCProtection),
117 (saved_errno == ENOSPC) ?
118 errhint("This error does *not* mean that you have run out of disk space. "
119 "It occurs when either the system limit for the maximum number of "
120 "semaphore sets (SEMMNI), or the system wide maximum number of "
121 "semaphores (SEMMNS), would be exceeded. You need to raise the "
122 "respective kernel parameter. Alternatively, reduce PostgreSQL's "
123 "consumption of semaphores by reducing its max_connections parameter.\n"
124 "The PostgreSQL documentation contains more information about "
125 "configuring your system for PostgreSQL.") : 0));
132 * Initialize a semaphore to the specified value.
135 IpcSemaphoreInitialize(IpcSemaphoreId semId, int semNum, int value)
140 if (semctl(semId, semNum, SETVAL, semun) < 0)
142 int saved_errno = errno;
145 (errmsg_internal("semctl(%d, %d, SETVAL, %d) failed: %m",
146 semId, semNum, value),
147 (saved_errno == ERANGE) ?
148 errhint("You possibly need to raise your kernel's SEMVMX value to be at least "
149 "%d. Look into the PostgreSQL documentation for details.",
155 * IpcSemaphoreKill(semId) - removes a semaphore set
158 IpcSemaphoreKill(IpcSemaphoreId semId)
162 semun.val = 0; /* unused, but keep compiler quiet */
164 if (semctl(semId, 0, IPC_RMID, semun) < 0)
165 elog(LOG, "semctl(%d, 0, IPC_RMID, ...) failed: %m", semId);
168 /* Get the current value (semval) of the semaphore */
170 IpcSemaphoreGetValue(IpcSemaphoreId semId, int semNum)
172 union semun dummy; /* for Solaris */
174 dummy.val = 0; /* unused */
176 return semctl(semId, semNum, GETVAL, dummy);
179 /* Get the PID of the last process to do semop() on the semaphore */
181 IpcSemaphoreGetLastPID(IpcSemaphoreId semId, int semNum)
183 union semun dummy; /* for Solaris */
185 dummy.val = 0; /* unused */
187 return semctl(semId, semNum, GETPID, dummy);
192 * Create a semaphore set with the given number of useful semaphores
193 * (an additional sema is actually allocated to serve as identifier).
194 * Dead Postgres sema sets are recycled if found, but we do not fail
195 * upon collision with non-Postgres sema sets.
197 * The idea here is to detect and re-use keys that may have been assigned
198 * by a crashed postmaster or backend.
200 static IpcSemaphoreId
201 IpcSemaphoreCreate(int numSems)
203 IpcSemaphoreId semId;
205 PGSemaphoreData mysema;
207 /* Loop till we find a free IPC key */
208 for (nextSemaKey++;; nextSemaKey++)
212 /* Try to create new semaphore set */
213 semId = InternalIpcSemaphoreCreate(nextSemaKey, numSems + 1);
215 break; /* successful create */
217 /* See if it looks to be leftover from a dead Postgres process */
218 semId = semget(nextSemaKey, numSems + 1, 0);
220 continue; /* failed: must be some other app's */
221 if (IpcSemaphoreGetValue(semId, numSems) != PGSemaMagic)
222 continue; /* sema belongs to a non-Postgres app */
225 * If the creator PID is my own PID or does not belong to any extant
226 * process, it's safe to zap it.
228 creatorPID = IpcSemaphoreGetLastPID(semId, numSems);
230 continue; /* oops, GETPID failed */
231 if (creatorPID != getpid())
233 if (kill(creatorPID, 0) == 0 || errno != ESRCH)
234 continue; /* sema belongs to a live process */
238 * The sema set appears to be from a dead Postgres process, or from a
239 * previous cycle of life in this same process. Zap it, if possible.
240 * This probably shouldn't fail, but if it does, assume the sema set
241 * belongs to someone else after all, and continue quietly.
243 semun.val = 0; /* unused, but keep compiler quiet */
244 if (semctl(semId, 0, IPC_RMID, semun) < 0)
248 * Now try again to create the sema set.
250 semId = InternalIpcSemaphoreCreate(nextSemaKey, numSems + 1);
252 break; /* successful create */
255 * Can only get here if some other process managed to create the same
256 * sema key before we did. Let him have that one, loop around to try
262 * OK, we created a new sema set. Mark it as created by this process. We
263 * do this by setting the spare semaphore to PGSemaMagic-1 and then
264 * incrementing it with semop(). That leaves it with value PGSemaMagic
265 * and sempid referencing this process.
267 IpcSemaphoreInitialize(semId, numSems, PGSemaMagic - 1);
268 mysema.semId = semId;
269 mysema.semNum = numSems;
270 PGSemaphoreUnlock(&mysema);
277 * PGReserveSemaphores --- initialize semaphore support
279 * This is called during postmaster start or shared memory reinitialization.
280 * It should do whatever is needed to be able to support up to maxSemas
281 * subsequent PGSemaphoreCreate calls. Also, if any system resources
282 * are acquired here or in PGSemaphoreCreate, register an on_shmem_exit
283 * callback to release them.
285 * The port number is passed for possible use as a key (for SysV, we use
286 * it to generate the starting semaphore key). In a standalone backend,
287 * zero will be passed.
289 * In the SysV implementation, we acquire semaphore sets on-demand; the
290 * maxSemas parameter is just used to size the array that keeps track of
291 * acquired sets for subsequent releasing.
294 PGReserveSemaphores(int maxSemas, int port)
296 maxSemaSets = (maxSemas + SEMAS_PER_SET - 1) / SEMAS_PER_SET;
297 mySemaSets = (IpcSemaphoreId *)
298 malloc(maxSemaSets * sizeof(IpcSemaphoreId));
299 if (mySemaSets == NULL)
300 elog(PANIC, "out of memory");
302 nextSemaKey = port * 1000;
303 nextSemaNumber = SEMAS_PER_SET; /* force sema set alloc on 1st call */
305 on_shmem_exit(ReleaseSemaphores, 0);
309 * Release semaphores at shutdown or shmem reinitialization
311 * (called as an on_shmem_exit callback, hence funny argument list)
314 ReleaseSemaphores(int status, Datum arg)
318 for (i = 0; i < numSemaSets; i++)
319 IpcSemaphoreKill(mySemaSets[i]);
326 * Initialize a PGSemaphore structure to represent a sema with count 1
329 PGSemaphoreCreate(PGSemaphore sema)
331 /* Can't do this in a backend, because static state is postmaster's */
332 Assert(!IsUnderPostmaster);
334 if (nextSemaNumber >= SEMAS_PER_SET)
336 /* Time to allocate another semaphore set */
337 if (numSemaSets >= maxSemaSets)
338 elog(PANIC, "too many semaphores created");
339 mySemaSets[numSemaSets] = IpcSemaphoreCreate(SEMAS_PER_SET);
343 /* Assign the next free semaphore in the current set */
344 sema->semId = mySemaSets[numSemaSets - 1];
345 sema->semNum = nextSemaNumber++;
346 /* Initialize it to count 1 */
347 IpcSemaphoreInitialize(sema->semId, sema->semNum, 1);
353 * Reset a previously-initialized PGSemaphore to have count 0
356 PGSemaphoreReset(PGSemaphore sema)
358 IpcSemaphoreInitialize(sema->semId, sema->semNum, 0);
364 * Lock a semaphore (decrement count), blocking if count would be < 0
367 PGSemaphoreLock(PGSemaphore sema)
372 sops.sem_op = -1; /* decrement */
374 sops.sem_num = sema->semNum;
377 * Note: if errStatus is -1 and errno == EINTR then it means we returned
378 * from the operation prematurely because we were sent a signal. So we
379 * try and lock the semaphore again.
381 * We used to check interrupts here, but that required servicing
382 * interrupts directly from signal handlers. Which is hard to do safely
387 errStatus = semop(sema->semId, &sops, 1);
388 } while (errStatus < 0 && errno == EINTR);
391 elog(FATAL, "semop(id=%d) failed: %m", sema->semId);
397 * Unlock a semaphore (increment count)
400 PGSemaphoreUnlock(PGSemaphore sema)
405 sops.sem_op = 1; /* increment */
407 sops.sem_num = sema->semNum;
410 * Note: if errStatus is -1 and errno == EINTR then it means we returned
411 * from the operation prematurely because we were sent a signal. So we
412 * try and unlock the semaphore again. Not clear this can really happen,
413 * but might as well cope.
417 errStatus = semop(sema->semId, &sops, 1);
418 } while (errStatus < 0 && errno == EINTR);
421 elog(FATAL, "semop(id=%d) failed: %m", sema->semId);
427 * Lock a semaphore only if able to do so without blocking
430 PGSemaphoreTryLock(PGSemaphore sema)
435 sops.sem_op = -1; /* decrement */
436 sops.sem_flg = IPC_NOWAIT; /* but don't block */
437 sops.sem_num = sema->semNum;
440 * Note: if errStatus is -1 and errno == EINTR then it means we returned
441 * from the operation prematurely because we were sent a signal. So we
442 * try and lock the semaphore again.
446 errStatus = semop(sema->semId, &sops, 1);
447 } while (errStatus < 0 && errno == EINTR);
451 /* Expect EAGAIN or EWOULDBLOCK (platform-dependent) */
454 return false; /* failed to lock it */
456 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
457 if (errno == EWOULDBLOCK)
458 return false; /* failed to lock it */
460 /* Otherwise we got trouble */
461 elog(FATAL, "semop(id=%d) failed: %m", sema->semId);