PostgreSQL Source Code  git master
signalfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * signalfuncs.c
4  * Functions for signaling backends
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/storage/ipc/signalfuncs.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <signal.h>
18 
19 #include "catalog/pg_authid.h"
20 #include "miscadmin.h"
21 #include "pgstat.h"
22 #include "postmaster/syslogger.h"
23 #include "storage/pmsignal.h"
24 #include "storage/proc.h"
25 #include "storage/procarray.h"
26 #include "utils/acl.h"
27 #include "utils/builtins.h"
28 
29 
30 /*
31  * Send a signal to another backend.
32  *
33  * The signal is delivered if the user is either a superuser or the same
34  * role as the backend being signaled. For "dangerous" signals, an explicit
35  * check for superuser needs to be done prior to calling this function.
36  *
37  * Returns 0 on success, 1 on general failure, 2 on normal permission error
38  * and 3 if the caller needs to be a superuser.
39  *
40  * In the event of a general failure (return code 1), a warning message will
41  * be emitted. For permission errors, doing that is the responsibility of
42  * the caller.
43  */
44 #define SIGNAL_BACKEND_SUCCESS 0
45 #define SIGNAL_BACKEND_ERROR 1
46 #define SIGNAL_BACKEND_NOPERMISSION 2
47 #define SIGNAL_BACKEND_NOSUPERUSER 3
48 static int
49 pg_signal_backend(int pid, int sig)
50 {
51  PGPROC *proc = BackendPidGetProc(pid);
52 
53  /*
54  * BackendPidGetProc returns NULL if the pid isn't valid; but by the time
55  * we reach kill(), a process for which we get a valid proc here might
56  * have terminated on its own. There's no way to acquire a lock on an
57  * arbitrary process to prevent that. But since so far all the callers of
58  * this mechanism involve some request for ending the process anyway, that
59  * it might end on its own first is not a problem.
60  *
61  * Note that proc will also be NULL if the pid refers to an auxiliary
62  * process or the postmaster (neither of which can be signaled via
63  * pg_signal_backend()).
64  */
65  if (proc == NULL)
66  {
67  /*
68  * This is just a warning so a loop-through-resultset will not abort
69  * if one backend terminated on its own during the run.
70  */
72  (errmsg("PID %d is not a PostgreSQL backend process", pid)));
73 
74  return SIGNAL_BACKEND_ERROR;
75  }
76 
77  /* Only allow superusers to signal superuser-owned backends. */
78  if (superuser_arg(proc->roleId) && !superuser())
80 
81  /* Users can signal backends they have role membership in. */
82  if (!has_privs_of_role(GetUserId(), proc->roleId) &&
83  !has_privs_of_role(GetUserId(), ROLE_PG_SIGNAL_BACKEND))
85 
86  /*
87  * Can the process we just validated above end, followed by the pid being
88  * recycled for a new process, before reaching here? Then we'd be trying
89  * to kill the wrong thing. Seems near impossible when sequential pid
90  * assignment and wraparound is used. Perhaps it could happen on a system
91  * where pid re-use is randomized. That race condition possibility seems
92  * too unlikely to worry about.
93  */
94 
95  /* If we have setsid(), signal the backend's whole process group */
96 #ifdef HAVE_SETSID
97  if (kill(-pid, sig))
98 #else
99  if (kill(pid, sig))
100 #endif
101  {
102  /* Again, just a warning to allow loops */
104  (errmsg("could not send signal to process %d: %m", pid)));
105  return SIGNAL_BACKEND_ERROR;
106  }
107  return SIGNAL_BACKEND_SUCCESS;
108 }
109 
110 /*
111  * Signal to cancel a backend process. This is allowed if you are a member of
112  * the role whose process is being canceled.
113  *
114  * Note that only superusers can signal superuser-owned processes.
115  */
116 Datum
118 {
119  int r = pg_signal_backend(PG_GETARG_INT32(0), SIGINT);
120 
122  ereport(ERROR,
123  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
124  errmsg("must be a superuser to cancel superuser query")));
125 
127  ereport(ERROR,
128  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
129  errmsg("must be a member of the role whose query is being canceled or member of pg_signal_backend")));
130 
132 }
133 
134 /*
135  * Wait until there is no backend process with the given PID and return true.
136  * On timeout, a warning is emitted and false is returned.
137  */
138 static bool
139 pg_wait_until_termination(int pid, int64 timeout)
140 {
141  /*
142  * Wait in steps of waittime milliseconds until this function exits or
143  * timeout.
144  */
145  int64 waittime = 100;
146 
147  /*
148  * Initially remaining time is the entire timeout specified by the user.
149  */
150  int64 remainingtime = timeout;
151 
152  /*
153  * Check existence of the backend. If the backend still exists, then wait
154  * for waittime milliseconds, again check for the existence. Repeat this
155  * until timeout or an error occurs or a pending interrupt such as query
156  * cancel gets processed.
157  */
158  do
159  {
160  if (remainingtime < waittime)
161  waittime = remainingtime;
162 
163  if (kill(pid, 0) == -1)
164  {
165  if (errno == ESRCH)
166  return true;
167  else
168  ereport(ERROR,
169  (errcode(ERRCODE_INTERNAL_ERROR),
170  errmsg("could not check the existence of the backend with PID %d: %m",
171  pid)));
172  }
173 
174  /* Process interrupts, if any, before waiting */
176 
177  (void) WaitLatch(MyLatch,
179  waittime,
181 
183 
184  remainingtime -= waittime;
185  } while (remainingtime > 0);
186 
188  (errmsg_plural("backend with PID %d did not terminate within %lld millisecond",
189  "backend with PID %d did not terminate within %lld milliseconds",
190  timeout,
191  pid, (long long int) timeout)));
192 
193  return false;
194 }
195 
196 /*
197  * Send a signal to terminate a backend process. This is allowed if you are a
198  * member of the role whose process is being terminated. If the timeout input
199  * argument is 0, then this function just signals the backend and returns
200  * true. If timeout is nonzero, then it waits until no process has the given
201  * PID; if the process ends within the timeout, true is returned, and if the
202  * timeout is exceeded, a warning is emitted and false is returned.
203  *
204  * Note that only superusers can signal superuser-owned processes.
205  */
206 Datum
208 {
209  int pid;
210  int r;
211  int timeout; /* milliseconds */
212 
213  pid = PG_GETARG_INT32(0);
214  timeout = PG_GETARG_INT64(1);
215 
216  if (timeout < 0)
217  ereport(ERROR,
218  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
219  errmsg("\"timeout\" must not be negative")));
220 
221  r = pg_signal_backend(pid, SIGTERM);
222 
224  ereport(ERROR,
225  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
226  errmsg("must be a superuser to terminate superuser process")));
227 
229  ereport(ERROR,
230  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
231  errmsg("must be a member of the role whose process is being terminated or member of pg_signal_backend")));
232 
233  /* Wait only on success and if actually requested */
234  if (r == SIGNAL_BACKEND_SUCCESS && timeout > 0)
236  else
238 }
239 
240 /*
241  * Signal to reload the database configuration
242  *
243  * Permission checking for this function is managed through the normal
244  * GRANT system.
245  */
246 Datum
248 {
249  if (kill(PostmasterPid, SIGHUP))
250  {
252  (errmsg("failed to send signal to postmaster: %m")));
253  PG_RETURN_BOOL(false);
254  }
255 
256  PG_RETURN_BOOL(true);
257 }
258 
259 
260 /*
261  * Rotate log file
262  *
263  * This function is kept to support adminpack 1.0.
264  */
265 Datum
267 {
268  if (!superuser())
269  ereport(ERROR,
270  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
271  errmsg("must be superuser to rotate log files with adminpack 1.0"),
272  /* translator: %s is a SQL function name */
273  errhint("Consider using %s, which is part of core, instead.",
274  "pg_logfile_rotate()")));
275 
276  if (!Logging_collector)
277  {
279  (errmsg("rotation not possible because log collection not active")));
280  PG_RETURN_BOOL(false);
281  }
282 
284  PG_RETURN_BOOL(true);
285 }
286 
287 /*
288  * Rotate log file
289  *
290  * Permission checking for this function is managed through the normal
291  * GRANT system.
292  */
293 Datum
295 {
296  if (!Logging_collector)
297  {
299  (errmsg("rotation not possible because log collection not active")));
300  PG_RETURN_BOOL(false);
301  }
302 
304  PG_RETURN_BOOL(true);
305 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4949
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1016
int errhint(const char *fmt,...)
Definition: elog.c:1153
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define WARNING
Definition: elog.h:32
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
pid_t PostmasterPid
Definition: globals.c:99
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:683
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:476
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
Oid GetUserId(void)
Definition: miscinit.c:497
static int sig
Definition: pg_ctl.c:79
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_ROTATE_LOGFILE
Definition: pmsignal.h:37
uintptr_t Datum
Definition: postgres.h:412
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3162
static int pg_signal_backend(int pid, int sig)
Definition: signalfuncs.c:49
Datum pg_cancel_backend(PG_FUNCTION_ARGS)
Definition: signalfuncs.c:117
#define SIGNAL_BACKEND_SUCCESS
Definition: signalfuncs.c:44
#define SIGNAL_BACKEND_NOPERMISSION
Definition: signalfuncs.c:46
Datum pg_rotate_logfile_v2(PG_FUNCTION_ARGS)
Definition: signalfuncs.c:294
Datum pg_rotate_logfile(PG_FUNCTION_ARGS)
Definition: signalfuncs.c:266
#define SIGNAL_BACKEND_NOSUPERUSER
Definition: signalfuncs.c:47
Datum pg_reload_conf(PG_FUNCTION_ARGS)
Definition: signalfuncs.c:247
Datum pg_terminate_backend(PG_FUNCTION_ARGS)
Definition: signalfuncs.c:207
#define SIGNAL_BACKEND_ERROR
Definition: signalfuncs.c:45
static bool pg_wait_until_termination(int pid, int64 timeout)
Definition: signalfuncs.c:139
Definition: proc.h:162
Oid roleId
Definition: proc.h:199
bool superuser_arg(Oid roleid)
Definition: superuser.c:56
bool superuser(void)
Definition: superuser.c:46
bool Logging_collector
Definition: syslogger.c:71
@ WAIT_EVENT_BACKEND_TERMINATION
Definition: wait_event.h:84
#define SIGHUP
Definition: win32_port.h:176
#define kill(pid, sig)
Definition: win32_port.h:482