]> granicus.if.org Git - postgresql/blob - src/backend/port/posix_sema.c
Fix poll() implementation of WaitLatchOrSocket to notice postmaster death.
[postgresql] / src / backend / port / posix_sema.c
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  *
10  * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
11  * Portions Copyright (c) 1994, Regents of the University of California
12  *
13  * IDENTIFICATION
14  *        src/backend/port/posix_sema.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <unistd.h>
23
24 #include "miscadmin.h"
25 #include "storage/ipc.h"
26 #include "storage/pg_sema.h"
27
28
29 #ifdef USE_NAMED_POSIX_SEMAPHORES
30 /* PGSemaphore is pointer to pointer to sem_t */
31 #define PG_SEM_REF(x)   (*(x))
32 #else
33 /* PGSemaphore is pointer to sem_t */
34 #define PG_SEM_REF(x)   (x)
35 #endif
36
37
38 #define IPCProtection   (0600)  /* access/modify by user only */
39
40 static sem_t **mySemPointers;   /* keep track of created semaphores */
41 static int      numSems;                        /* number of semas acquired so far */
42 static int      maxSems;                        /* allocated size of mySemaPointers array */
43 static int      nextSemKey;                     /* next name to try */
44
45
46 static void ReleaseSemaphores(int status, Datum arg);
47
48
49 #ifdef USE_NAMED_POSIX_SEMAPHORES
50
51 /*
52  * PosixSemaphoreCreate
53  *
54  * Attempt to create a new named semaphore.
55  *
56  * If we fail with a failure code other than collision-with-existing-sema,
57  * print out an error and abort.  Other types of errors suggest nonrecoverable
58  * problems.
59  */
60 static sem_t *
61 PosixSemaphoreCreate(void)
62 {
63         int                     semKey;
64         char            semname[64];
65         sem_t      *mySem;
66
67         for (;;)
68         {
69                 semKey = nextSemKey++;
70
71                 snprintf(semname, sizeof(semname), "/pgsql-%d", semKey);
72
73                 mySem = sem_open(semname, O_CREAT | O_EXCL,
74                                                  (mode_t) IPCProtection, (unsigned) 1);
75
76 #ifdef SEM_FAILED
77                 if (mySem != (sem_t *) SEM_FAILED)
78                         break;
79 #else
80                 if (mySem != (sem_t *) (-1))
81                         break;
82 #endif
83
84                 /* Loop if error indicates a collision */
85                 if (errno == EEXIST || errno == EACCES || errno == EINTR)
86                         continue;
87
88                 /*
89                  * Else complain and abort
90                  */
91                 elog(FATAL, "sem_open(\"%s\") failed: %m", semname);
92         }
93
94         /*
95          * Unlink the semaphore immediately, so it can't be accessed externally.
96          * This also ensures that it will go away if we crash.
97          */
98         sem_unlink(semname);
99
100         return mySem;
101 }
102 #else                                                   /* !USE_NAMED_POSIX_SEMAPHORES */
103
104 /*
105  * PosixSemaphoreCreate
106  *
107  * Attempt to create a new unnamed semaphore.
108  */
109 static void
110 PosixSemaphoreCreate(sem_t * sem)
111 {
112         if (sem_init(sem, 1, 1) < 0)
113                 elog(FATAL, "sem_init failed: %m");
114 }
115 #endif   /* USE_NAMED_POSIX_SEMAPHORES */
116
117
118 /*
119  * PosixSemaphoreKill   - removes a semaphore
120  */
121 static void
122 PosixSemaphoreKill(sem_t * sem)
123 {
124 #ifdef USE_NAMED_POSIX_SEMAPHORES
125         /* Got to use sem_close for named semaphores */
126         if (sem_close(sem) < 0)
127                 elog(LOG, "sem_close failed: %m");
128 #else
129         /* Got to use sem_destroy for unnamed semaphores */
130         if (sem_destroy(sem) < 0)
131                 elog(LOG, "sem_destroy failed: %m");
132 #endif
133 }
134
135
136 /*
137  * PGReserveSemaphores --- initialize semaphore support
138  *
139  * This is called during postmaster start or shared memory reinitialization.
140  * It should do whatever is needed to be able to support up to maxSemas
141  * subsequent PGSemaphoreCreate calls.  Also, if any system resources
142  * are acquired here or in PGSemaphoreCreate, register an on_shmem_exit
143  * callback to release them.
144  *
145  * The port number is passed for possible use as a key (for Posix, we use
146  * it to generate the starting semaphore name).  In a standalone backend,
147  * zero will be passed.
148  *
149  * In the Posix implementation, we acquire semaphores on-demand; the
150  * maxSemas parameter is just used to size the array that keeps track of
151  * acquired semas for subsequent releasing.
152  */
153 void
154 PGReserveSemaphores(int maxSemas, int port)
155 {
156         mySemPointers = (sem_t **) malloc(maxSemas * sizeof(sem_t *));
157         if (mySemPointers == NULL)
158                 elog(PANIC, "out of memory");
159         numSems = 0;
160         maxSems = maxSemas;
161         nextSemKey = port * 1000;
162
163         on_shmem_exit(ReleaseSemaphores, 0);
164 }
165
166 /*
167  * Release semaphores at shutdown or shmem reinitialization
168  *
169  * (called as an on_shmem_exit callback, hence funny argument list)
170  */
171 static void
172 ReleaseSemaphores(int status, Datum arg)
173 {
174         int                     i;
175
176         for (i = 0; i < numSems; i++)
177                 PosixSemaphoreKill(mySemPointers[i]);
178         free(mySemPointers);
179 }
180
181 /*
182  * PGSemaphoreCreate
183  *
184  * Initialize a PGSemaphore structure to represent a sema with count 1
185  */
186 void
187 PGSemaphoreCreate(PGSemaphore sema)
188 {
189         sem_t      *newsem;
190
191         /* Can't do this in a backend, because static state is postmaster's */
192         Assert(!IsUnderPostmaster);
193
194         if (numSems >= maxSems)
195                 elog(PANIC, "too many semaphores created");
196
197 #ifdef USE_NAMED_POSIX_SEMAPHORES
198         *sema = newsem = PosixSemaphoreCreate();
199 #else
200         PosixSemaphoreCreate(sema);
201         newsem = sema;
202 #endif
203
204         /* Remember new sema for ReleaseSemaphores */
205         mySemPointers[numSems++] = newsem;
206 }
207
208 /*
209  * PGSemaphoreReset
210  *
211  * Reset a previously-initialized PGSemaphore to have count 0
212  */
213 void
214 PGSemaphoreReset(PGSemaphore sema)
215 {
216         /*
217          * There's no direct API for this in POSIX, so we have to ratchet the
218          * semaphore down to 0 with repeated trywait's.
219          */
220         for (;;)
221         {
222                 if (sem_trywait(PG_SEM_REF(sema)) < 0)
223                 {
224                         if (errno == EAGAIN || errno == EDEADLK)
225                                 break;                  /* got it down to 0 */
226                         if (errno == EINTR)
227                                 continue;               /* can this happen? */
228                         elog(FATAL, "sem_trywait failed: %m");
229                 }
230         }
231 }
232
233 /*
234  * PGSemaphoreLock
235  *
236  * Lock a semaphore (decrement count), blocking if count would be < 0
237  */
238 void
239 PGSemaphoreLock(PGSemaphore sema, bool interruptOK)
240 {
241         int                     errStatus;
242
243         /*
244          * See notes in sysv_sema.c's implementation of PGSemaphoreLock. Just as
245          * that code does for semop(), we handle both the case where sem_wait()
246          * returns errno == EINTR after a signal, and the case where it just keeps
247          * waiting.
248          */
249         do
250         {
251                 ImmediateInterruptOK = interruptOK;
252                 CHECK_FOR_INTERRUPTS();
253                 errStatus = sem_wait(PG_SEM_REF(sema));
254                 ImmediateInterruptOK = false;
255         } while (errStatus < 0 && errno == EINTR);
256
257         if (errStatus < 0)
258                 elog(FATAL, "sem_wait failed: %m");
259 }
260
261 /*
262  * PGSemaphoreUnlock
263  *
264  * Unlock a semaphore (increment count)
265  */
266 void
267 PGSemaphoreUnlock(PGSemaphore sema)
268 {
269         int                     errStatus;
270
271         /*
272          * Note: if errStatus is -1 and errno == EINTR then it means we returned
273          * from the operation prematurely because we were sent a signal.  So we
274          * try and unlock the semaphore again. Not clear this can really happen,
275          * but might as well cope.
276          */
277         do
278         {
279                 errStatus = sem_post(PG_SEM_REF(sema));
280         } while (errStatus < 0 && errno == EINTR);
281
282         if (errStatus < 0)
283                 elog(FATAL, "sem_post failed: %m");
284 }
285
286 /*
287  * PGSemaphoreTryLock
288  *
289  * Lock a semaphore only if able to do so without blocking
290  */
291 bool
292 PGSemaphoreTryLock(PGSemaphore sema)
293 {
294         int                     errStatus;
295
296         /*
297          * Note: if errStatus is -1 and errno == EINTR then it means we returned
298          * from the operation prematurely because we were sent a signal.  So we
299          * try and lock the semaphore again.
300          */
301         do
302         {
303                 errStatus = sem_trywait(PG_SEM_REF(sema));
304         } while (errStatus < 0 && errno == EINTR);
305
306         if (errStatus < 0)
307         {
308                 if (errno == EAGAIN || errno == EDEADLK)
309                         return false;           /* failed to lock it */
310                 /* Otherwise we got trouble */
311                 elog(FATAL, "sem_trywait failed: %m");
312         }
313
314         return true;
315 }