1 /*-------------------------------------------------------------------------
4 * Routines for interprocess signalling
7 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/storage/ipc/procsignal.c
13 *-------------------------------------------------------------------------
20 #include "access/parallel.h"
21 #include "commands/async.h"
22 #include "miscadmin.h"
23 #include "storage/latch.h"
24 #include "storage/ipc.h"
25 #include "storage/proc.h"
26 #include "storage/shmem.h"
27 #include "storage/sinval.h"
28 #include "tcop/tcopprot.h"
32 * The SIGUSR1 signal is multiplexed to support signalling multiple event
33 * types. The specific reason is communicated via flags in shared memory.
34 * We keep a boolean flag for each possible "reason", so that different
35 * reasons can be signaled to a process concurrently. (However, if the same
36 * reason is signaled more than once nearly simultaneously, the process may
37 * observe it only once.)
39 * Each process that wants to receive signals registers its process ID
40 * in the ProcSignalSlots array. The array is indexed by backend ID to make
41 * slot allocation simple, and to avoid having to search the array when you
42 * know the backend ID of the process you're signalling. (We do support
43 * signalling without backend ID, but it's a bit less efficient.)
45 * The flags are actually declared as "volatile sig_atomic_t" for maximum
46 * portability. This should ensure that loads and stores of the flag
47 * values are atomic, allowing us to dispense with any explicit locking.
52 sig_atomic_t pss_signalFlags[NUM_PROCSIGNALS];
56 * We reserve a slot for each possible BackendId, plus one for each
57 * possible auxiliary process type. (This scheme assumes there is not
58 * more than one of any auxiliary process type at a time.)
60 #define NumProcSignalSlots (MaxBackends + NUM_AUXPROCTYPES)
62 static ProcSignalSlot *ProcSignalSlots = NULL;
63 static volatile ProcSignalSlot *MyProcSignalSlot = NULL;
65 static bool CheckProcSignal(ProcSignalReason reason);
66 static void CleanupProcSignalState(int status, Datum arg);
70 * Compute space needed for procsignal's shared memory
73 ProcSignalShmemSize(void)
75 return NumProcSignalSlots * sizeof(ProcSignalSlot);
80 * Allocate and initialize procsignal's shared memory
83 ProcSignalShmemInit(void)
85 Size size = ProcSignalShmemSize();
88 ProcSignalSlots = (ProcSignalSlot *)
89 ShmemInitStruct("ProcSignalSlots", size, &found);
91 /* If we're first, set everything to zeroes */
93 MemSet(ProcSignalSlots, 0, size);
98 * Register the current process in the procsignal array
100 * The passed index should be my BackendId if the process has one,
101 * or MaxBackends + aux process type if not.
104 ProcSignalInit(int pss_idx)
106 volatile ProcSignalSlot *slot;
108 Assert(pss_idx >= 1 && pss_idx <= NumProcSignalSlots);
110 slot = &ProcSignalSlots[pss_idx - 1];
113 if (slot->pss_pid != 0)
114 elog(LOG, "process %d taking over ProcSignal slot %d, but it's not empty",
117 /* Clear out any leftover signal reasons */
118 MemSet(slot->pss_signalFlags, 0, NUM_PROCSIGNALS * sizeof(sig_atomic_t));
120 /* Mark slot with my PID */
121 slot->pss_pid = MyProcPid;
123 /* Remember slot location for CheckProcSignal */
124 MyProcSignalSlot = slot;
126 /* Set up to release the slot on process exit */
127 on_shmem_exit(CleanupProcSignalState, Int32GetDatum(pss_idx));
131 * CleanupProcSignalState
132 * Remove current process from ProcSignalSlots
134 * This function is called via on_shmem_exit() during backend shutdown.
137 CleanupProcSignalState(int status, Datum arg)
139 int pss_idx = DatumGetInt32(arg);
140 volatile ProcSignalSlot *slot;
142 slot = &ProcSignalSlots[pss_idx - 1];
143 Assert(slot == MyProcSignalSlot);
146 * Clear MyProcSignalSlot, so that a SIGUSR1 received after this point
147 * won't try to access it after it's no longer ours (and perhaps even
148 * after we've unmapped the shared memory segment).
150 MyProcSignalSlot = NULL;
153 if (slot->pss_pid != MyProcPid)
156 * don't ERROR here. We're exiting anyway, and don't want to get into
157 * infinite loop trying to exit
159 elog(LOG, "process %d releasing ProcSignal slot %d, but it contains %d",
160 MyProcPid, pss_idx, (int) slot->pss_pid);
161 return; /* XXX better to zero the slot anyway? */
169 * Send a signal to a Postgres process
171 * Providing backendId is optional, but it will speed up the operation.
173 * On success (a signal was sent), zero is returned.
174 * On error, -1 is returned, and errno is set (typically to ESRCH or EPERM).
176 * Not to be confused with ProcSendSignal
179 SendProcSignal(pid_t pid, ProcSignalReason reason, BackendId backendId)
181 volatile ProcSignalSlot *slot;
183 if (backendId != InvalidBackendId)
185 slot = &ProcSignalSlots[backendId - 1];
188 * Note: Since there's no locking, it's possible that the target
189 * process detaches from shared memory and exits right after this
190 * test, before we set the flag and send signal. And the signal slot
191 * might even be recycled by a new process, so it's remotely possible
192 * that we set a flag for a wrong process. That's OK, all the signals
193 * are such that no harm is done if they're mistakenly fired.
195 if (slot->pss_pid == pid)
197 /* Atomically set the proper flag */
198 slot->pss_signalFlags[reason] = true;
200 return kill(pid, SIGUSR1);
206 * BackendId not provided, so search the array using pid. We search
207 * the array back to front so as to reduce search overhead. Passing
208 * InvalidBackendId means that the target is most likely an auxiliary
209 * process, which will have a slot near the end of the array.
213 for (i = NumProcSignalSlots - 1; i >= 0; i--)
215 slot = &ProcSignalSlots[i];
217 if (slot->pss_pid == pid)
219 /* the above note about race conditions applies here too */
221 /* Atomically set the proper flag */
222 slot->pss_signalFlags[reason] = true;
224 return kill(pid, SIGUSR1);
234 * CheckProcSignal - check to see if a particular reason has been
235 * signaled, and clear the signal flag. Should be called after receiving
239 CheckProcSignal(ProcSignalReason reason)
241 volatile ProcSignalSlot *slot = MyProcSignalSlot;
245 /* Careful here --- don't clear flag if we haven't seen it set */
246 if (slot->pss_signalFlags[reason])
248 slot->pss_signalFlags[reason] = false;
257 * procsignal_sigusr1_handler - handle SIGUSR1 signal.
260 procsignal_sigusr1_handler(SIGNAL_ARGS)
262 int save_errno = errno;
264 if (CheckProcSignal(PROCSIG_CATCHUP_INTERRUPT))
265 HandleCatchupInterrupt();
267 if (CheckProcSignal(PROCSIG_NOTIFY_INTERRUPT))
268 HandleNotifyInterrupt();
270 if (CheckProcSignal(PROCSIG_PARALLEL_MESSAGE))
271 HandleParallelMessageInterrupt();
273 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_DATABASE))
274 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_DATABASE);
276 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_TABLESPACE))
277 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_TABLESPACE);
279 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_LOCK))
280 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_LOCK);
282 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT))
283 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_SNAPSHOT);
285 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK))
286 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK);
288 if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
289 RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
293 latch_sigusr1_handler();