]> granicus.if.org Git - postgresql/blob - src/backend/port/sysv_sema.c
Remove the option to service interrupts during PGSemaphoreLock().
[postgresql] / src / backend / port / sysv_sema.c
1 /*-------------------------------------------------------------------------
2  *
3  * sysv_sema.c
4  *        Implement PGSemaphores using SysV semaphore facilities
5  *
6  *
7  * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * IDENTIFICATION
11  *        src/backend/port/sysv_sema.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include <signal.h>
18 #include <unistd.h>
19 #include <sys/file.h>
20 #ifdef HAVE_SYS_IPC_H
21 #include <sys/ipc.h>
22 #endif
23 #ifdef HAVE_SYS_SEM_H
24 #include <sys/sem.h>
25 #endif
26
27 #include "miscadmin.h"
28 #include "storage/ipc.h"
29 #include "storage/pg_sema.h"
30
31
32 #ifndef HAVE_UNION_SEMUN
33 union semun
34 {
35         int                     val;
36         struct semid_ds *buf;
37         unsigned short *array;
38 };
39 #endif
40
41 typedef key_t IpcSemaphoreKey;  /* semaphore key passed to semget(2) */
42 typedef int IpcSemaphoreId;             /* semaphore ID returned by semget(2) */
43
44 /*
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.)
49  */
50 #define SEMAS_PER_SET   16
51
52 #define IPCProtection   (0600)  /* access/modify by user only */
53
54 #define PGSemaMagic             537             /* must be less than SEMVMX */
55
56
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 */
62
63
64 static IpcSemaphoreId InternalIpcSemaphoreCreate(IpcSemaphoreKey semKey,
65                                                    int numSems);
66 static void IpcSemaphoreInitialize(IpcSemaphoreId semId, int semNum,
67                                            int value);
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);
73
74
75 /*
76  * InternalIpcSemaphoreCreate
77  *
78  * Attempt to create a new semaphore set with the specified key.
79  * Will fail (return -1) if such a set already exists.
80  *
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
83  * problems.
84  */
85 static IpcSemaphoreId
86 InternalIpcSemaphoreCreate(IpcSemaphoreKey semKey, int numSems)
87 {
88         int                     semId;
89
90         semId = semget(semKey, numSems, IPC_CREAT | IPC_EXCL | IPCProtection);
91
92         if (semId < 0)
93         {
94                 int                     saved_errno = errno;
95
96                 /*
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.
101                  */
102                 if (saved_errno == EEXIST || saved_errno == EACCES
103 #ifdef EIDRM
104                         || saved_errno == EIDRM
105 #endif
106                         )
107                         return -1;
108
109                 /*
110                  * Else complain and abort
111                  */
112                 ereport(FATAL,
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));
126         }
127
128         return semId;
129 }
130
131 /*
132  * Initialize a semaphore to the specified value.
133  */
134 static void
135 IpcSemaphoreInitialize(IpcSemaphoreId semId, int semNum, int value)
136 {
137         union semun semun;
138
139         semun.val = value;
140         if (semctl(semId, semNum, SETVAL, semun) < 0)
141         {
142                 int                     saved_errno = errno;
143
144                 ereport(FATAL,
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.",
150                                                  value) : 0));
151         }
152 }
153
154 /*
155  * IpcSemaphoreKill(semId)      - removes a semaphore set
156  */
157 static void
158 IpcSemaphoreKill(IpcSemaphoreId semId)
159 {
160         union semun semun;
161
162         semun.val = 0;                          /* unused, but keep compiler quiet */
163
164         if (semctl(semId, 0, IPC_RMID, semun) < 0)
165                 elog(LOG, "semctl(%d, 0, IPC_RMID, ...) failed: %m", semId);
166 }
167
168 /* Get the current value (semval) of the semaphore */
169 static int
170 IpcSemaphoreGetValue(IpcSemaphoreId semId, int semNum)
171 {
172         union semun dummy;                      /* for Solaris */
173
174         dummy.val = 0;                          /* unused */
175
176         return semctl(semId, semNum, GETVAL, dummy);
177 }
178
179 /* Get the PID of the last process to do semop() on the semaphore */
180 static pid_t
181 IpcSemaphoreGetLastPID(IpcSemaphoreId semId, int semNum)
182 {
183         union semun dummy;                      /* for Solaris */
184
185         dummy.val = 0;                          /* unused */
186
187         return semctl(semId, semNum, GETPID, dummy);
188 }
189
190
191 /*
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.
196  *
197  * The idea here is to detect and re-use keys that may have been assigned
198  * by a crashed postmaster or backend.
199  */
200 static IpcSemaphoreId
201 IpcSemaphoreCreate(int numSems)
202 {
203         IpcSemaphoreId semId;
204         union semun semun;
205         PGSemaphoreData mysema;
206
207         /* Loop till we find a free IPC key */
208         for (nextSemaKey++;; nextSemaKey++)
209         {
210                 pid_t           creatorPID;
211
212                 /* Try to create new semaphore set */
213                 semId = InternalIpcSemaphoreCreate(nextSemaKey, numSems + 1);
214                 if (semId >= 0)
215                         break;                          /* successful create */
216
217                 /* See if it looks to be leftover from a dead Postgres process */
218                 semId = semget(nextSemaKey, numSems + 1, 0);
219                 if (semId < 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 */
223
224                 /*
225                  * If the creator PID is my own PID or does not belong to any extant
226                  * process, it's safe to zap it.
227                  */
228                 creatorPID = IpcSemaphoreGetLastPID(semId, numSems);
229                 if (creatorPID <= 0)
230                         continue;                       /* oops, GETPID failed */
231                 if (creatorPID != getpid())
232                 {
233                         if (kill(creatorPID, 0) == 0 || errno != ESRCH)
234                                 continue;               /* sema belongs to a live process */
235                 }
236
237                 /*
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.
242                  */
243                 semun.val = 0;                  /* unused, but keep compiler quiet */
244                 if (semctl(semId, 0, IPC_RMID, semun) < 0)
245                         continue;
246
247                 /*
248                  * Now try again to create the sema set.
249                  */
250                 semId = InternalIpcSemaphoreCreate(nextSemaKey, numSems + 1);
251                 if (semId >= 0)
252                         break;                          /* successful create */
253
254                 /*
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
257                  * next key.
258                  */
259         }
260
261         /*
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.
266          */
267         IpcSemaphoreInitialize(semId, numSems, PGSemaMagic - 1);
268         mysema.semId = semId;
269         mysema.semNum = numSems;
270         PGSemaphoreUnlock(&mysema);
271
272         return semId;
273 }
274
275
276 /*
277  * PGReserveSemaphores --- initialize semaphore support
278  *
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.
284  *
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.
288  *
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.
292  */
293 void
294 PGReserveSemaphores(int maxSemas, int port)
295 {
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");
301         numSemaSets = 0;
302         nextSemaKey = port * 1000;
303         nextSemaNumber = SEMAS_PER_SET;         /* force sema set alloc on 1st call */
304
305         on_shmem_exit(ReleaseSemaphores, 0);
306 }
307
308 /*
309  * Release semaphores at shutdown or shmem reinitialization
310  *
311  * (called as an on_shmem_exit callback, hence funny argument list)
312  */
313 static void
314 ReleaseSemaphores(int status, Datum arg)
315 {
316         int                     i;
317
318         for (i = 0; i < numSemaSets; i++)
319                 IpcSemaphoreKill(mySemaSets[i]);
320         free(mySemaSets);
321 }
322
323 /*
324  * PGSemaphoreCreate
325  *
326  * Initialize a PGSemaphore structure to represent a sema with count 1
327  */
328 void
329 PGSemaphoreCreate(PGSemaphore sema)
330 {
331         /* Can't do this in a backend, because static state is postmaster's */
332         Assert(!IsUnderPostmaster);
333
334         if (nextSemaNumber >= SEMAS_PER_SET)
335         {
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);
340                 numSemaSets++;
341                 nextSemaNumber = 0;
342         }
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);
348 }
349
350 /*
351  * PGSemaphoreReset
352  *
353  * Reset a previously-initialized PGSemaphore to have count 0
354  */
355 void
356 PGSemaphoreReset(PGSemaphore sema)
357 {
358         IpcSemaphoreInitialize(sema->semId, sema->semNum, 0);
359 }
360
361 /*
362  * PGSemaphoreLock
363  *
364  * Lock a semaphore (decrement count), blocking if count would be < 0
365  */
366 void
367 PGSemaphoreLock(PGSemaphore sema)
368 {
369         int                     errStatus;
370         struct sembuf sops;
371
372         sops.sem_op = -1;                       /* decrement */
373         sops.sem_flg = 0;
374         sops.sem_num = sema->semNum;
375
376         /*
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.
380          *
381          * We used to check interrupts here, but that required servicing
382          * interrupts directly from signal handlers. Which is hard to do safely
383          * and portably.
384          */
385         do
386         {
387                 errStatus = semop(sema->semId, &sops, 1);
388         } while (errStatus < 0 && errno == EINTR);
389
390         if (errStatus < 0)
391                 elog(FATAL, "semop(id=%d) failed: %m", sema->semId);
392 }
393
394 /*
395  * PGSemaphoreUnlock
396  *
397  * Unlock a semaphore (increment count)
398  */
399 void
400 PGSemaphoreUnlock(PGSemaphore sema)
401 {
402         int                     errStatus;
403         struct sembuf sops;
404
405         sops.sem_op = 1;                        /* increment */
406         sops.sem_flg = 0;
407         sops.sem_num = sema->semNum;
408
409         /*
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.
414          */
415         do
416         {
417                 errStatus = semop(sema->semId, &sops, 1);
418         } while (errStatus < 0 && errno == EINTR);
419
420         if (errStatus < 0)
421                 elog(FATAL, "semop(id=%d) failed: %m", sema->semId);
422 }
423
424 /*
425  * PGSemaphoreTryLock
426  *
427  * Lock a semaphore only if able to do so without blocking
428  */
429 bool
430 PGSemaphoreTryLock(PGSemaphore sema)
431 {
432         int                     errStatus;
433         struct sembuf sops;
434
435         sops.sem_op = -1;                       /* decrement */
436         sops.sem_flg = IPC_NOWAIT;      /* but don't block */
437         sops.sem_num = sema->semNum;
438
439         /*
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.
443          */
444         do
445         {
446                 errStatus = semop(sema->semId, &sops, 1);
447         } while (errStatus < 0 && errno == EINTR);
448
449         if (errStatus < 0)
450         {
451                 /* Expect EAGAIN or EWOULDBLOCK (platform-dependent) */
452 #ifdef EAGAIN
453                 if (errno == EAGAIN)
454                         return false;           /* failed to lock it */
455 #endif
456 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
457                 if (errno == EWOULDBLOCK)
458                         return false;           /* failed to lock it */
459 #endif
460                 /* Otherwise we got trouble */
461                 elog(FATAL, "semop(id=%d) failed: %m", sema->semId);
462         }
463
464         return true;
465 }