PostgreSQL Source Code  git master
signalfuncs.c File Reference
#include "postgres.h"
#include <signal.h>
#include "catalog/pg_authid.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/syslogger.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/acl.h"
#include "utils/builtins.h"
Include dependency graph for signalfuncs.c:

Go to the source code of this file.

Macros

#define SIGNAL_BACKEND_SUCCESS   0
 
#define SIGNAL_BACKEND_ERROR   1
 
#define SIGNAL_BACKEND_NOPERMISSION   2
 
#define SIGNAL_BACKEND_NOSUPERUSER   3
 

Functions

static int pg_signal_backend (int pid, int sig)
 
Datum pg_cancel_backend (PG_FUNCTION_ARGS)
 
static bool pg_wait_until_termination (int pid, int64 timeout)
 
Datum pg_terminate_backend (PG_FUNCTION_ARGS)
 
Datum pg_reload_conf (PG_FUNCTION_ARGS)
 
Datum pg_rotate_logfile (PG_FUNCTION_ARGS)
 
Datum pg_rotate_logfile_v2 (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ SIGNAL_BACKEND_ERROR

#define SIGNAL_BACKEND_ERROR   1

Definition at line 45 of file signalfuncs.c.

◆ SIGNAL_BACKEND_NOPERMISSION

#define SIGNAL_BACKEND_NOPERMISSION   2

Definition at line 46 of file signalfuncs.c.

◆ SIGNAL_BACKEND_NOSUPERUSER

#define SIGNAL_BACKEND_NOSUPERUSER   3

Definition at line 47 of file signalfuncs.c.

◆ SIGNAL_BACKEND_SUCCESS

#define SIGNAL_BACKEND_SUCCESS   0

Definition at line 44 of file signalfuncs.c.

Function Documentation

◆ pg_cancel_backend()

Datum pg_cancel_backend ( PG_FUNCTION_ARGS  )

Definition at line 117 of file signalfuncs.c.

118 {
119  int r = pg_signal_backend(PG_GETARG_INT32(0), SIGINT);
120 
122  ereport(ERROR,
123  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
124  errmsg("permission denied to cancel query"),
125  errdetail("Only roles with the %s attribute may cancel queries of roles with %s.",
126  "SUPERUSER", "SUPERUSER")));
127 
129  ereport(ERROR,
130  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
131  errmsg("permission denied to cancel query"),
132  errdetail("Only roles with privileges of the role whose query is being canceled or with privileges of the \"%s\" role may cancel this query.",
133  "pg_signal_backend")));
134 
136 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
static int pg_signal_backend(int pid, int sig)
Definition: signalfuncs.c:49
#define SIGNAL_BACKEND_SUCCESS
Definition: signalfuncs.c:44
#define SIGNAL_BACKEND_NOPERMISSION
Definition: signalfuncs.c:46
#define SIGNAL_BACKEND_NOSUPERUSER
Definition: signalfuncs.c:47

References ereport, errcode(), errdetail(), errmsg(), ERROR, PG_GETARG_INT32, PG_RETURN_BOOL, pg_signal_backend(), SIGNAL_BACKEND_NOPERMISSION, SIGNAL_BACKEND_NOSUPERUSER, and SIGNAL_BACKEND_SUCCESS.

◆ pg_reload_conf()

Datum pg_reload_conf ( PG_FUNCTION_ARGS  )

Definition at line 255 of file signalfuncs.c.

256 {
257  if (kill(PostmasterPid, SIGHUP))
258  {
260  (errmsg("failed to send signal to postmaster: %m")));
261  PG_RETURN_BOOL(false);
262  }
263 
264  PG_RETURN_BOOL(true);
265 }
#define WARNING
Definition: elog.h:36
pid_t PostmasterPid
Definition: globals.c:99
#define SIGHUP
Definition: win32_port.h:176
#define kill(pid, sig)
Definition: win32_port.h:495

References ereport, errmsg(), kill, PG_RETURN_BOOL, PostmasterPid, SIGHUP, and WARNING.

◆ pg_rotate_logfile()

Datum pg_rotate_logfile ( PG_FUNCTION_ARGS  )

Definition at line 274 of file signalfuncs.c.

275 {
276  if (!superuser())
277  ereport(ERROR,
278  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
279  errmsg("must be superuser to rotate log files with adminpack 1.0"),
280  /* translator: %s is a SQL function name */
281  errhint("Consider using %s, which is part of core, instead.",
282  "pg_logfile_rotate()")));
283 
284  if (!Logging_collector)
285  {
287  (errmsg("rotation not possible because log collection not active")));
288  PG_RETURN_BOOL(false);
289  }
290 
292  PG_RETURN_BOOL(true);
293 }
int errhint(const char *fmt,...)
Definition: elog.c:1316
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:181
@ PMSIGNAL_ROTATE_LOGFILE
Definition: pmsignal.h:37
bool superuser(void)
Definition: superuser.c:46
bool Logging_collector
Definition: syslogger.c:71

References ereport, errcode(), errhint(), errmsg(), ERROR, Logging_collector, PG_RETURN_BOOL, PMSIGNAL_ROTATE_LOGFILE, SendPostmasterSignal(), superuser(), and WARNING.

◆ pg_rotate_logfile_v2()

Datum pg_rotate_logfile_v2 ( PG_FUNCTION_ARGS  )

Definition at line 302 of file signalfuncs.c.

303 {
304  if (!Logging_collector)
305  {
307  (errmsg("rotation not possible because log collection not active")));
308  PG_RETURN_BOOL(false);
309  }
310 
312  PG_RETURN_BOOL(true);
313 }

References ereport, errmsg(), Logging_collector, PG_RETURN_BOOL, PMSIGNAL_ROTATE_LOGFILE, SendPostmasterSignal(), and WARNING.

◆ pg_signal_backend()

static int pg_signal_backend ( int  pid,
int  sig 
)
static

Definition at line 49 of file signalfuncs.c.

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 }
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4969
Oid GetUserId(void)
Definition: miscinit.c:510
static int sig
Definition: pg_ctl.c:79
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:3134
#define SIGNAL_BACKEND_ERROR
Definition: signalfuncs.c:45
Definition: proc.h:162
Oid roleId
Definition: proc.h:199
bool superuser_arg(Oid roleid)
Definition: superuser.c:56

References BackendPidGetProc(), ereport, errmsg(), GetUserId(), has_privs_of_role(), kill, PGPROC::roleId, sig, SIGNAL_BACKEND_ERROR, SIGNAL_BACKEND_NOPERMISSION, SIGNAL_BACKEND_NOSUPERUSER, SIGNAL_BACKEND_SUCCESS, superuser(), superuser_arg(), and WARNING.

Referenced by pg_cancel_backend(), and pg_terminate_backend().

◆ pg_terminate_backend()

Datum pg_terminate_backend ( PG_FUNCTION_ARGS  )

Definition at line 211 of file signalfuncs.c.

212 {
213  int pid;
214  int r;
215  int timeout; /* milliseconds */
216 
217  pid = PG_GETARG_INT32(0);
218  timeout = PG_GETARG_INT64(1);
219 
220  if (timeout < 0)
221  ereport(ERROR,
222  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
223  errmsg("\"timeout\" must not be negative")));
224 
225  r = pg_signal_backend(pid, SIGTERM);
226 
228  ereport(ERROR,
229  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
230  errmsg("permission denied to terminate process"),
231  errdetail("Only roles with the %s attribute may terminate processes of roles with %s.",
232  "SUPERUSER", "SUPERUSER")));
233 
235  ereport(ERROR,
236  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
237  errmsg("permission denied to terminate process"),
238  errdetail("Only roles with privileges of the role whose process is being terminated or with privileges of the \"%s\" role may terminate this process.",
239  "pg_signal_backend")));
240 
241  /* Wait only on success and if actually requested */
242  if (r == SIGNAL_BACKEND_SUCCESS && timeout > 0)
244  else
246 }
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283
static bool pg_wait_until_termination(int pid, int64 timeout)
Definition: signalfuncs.c:143

References ereport, errcode(), errdetail(), errmsg(), ERROR, PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_BOOL, pg_signal_backend(), pg_wait_until_termination(), SIGNAL_BACKEND_NOPERMISSION, SIGNAL_BACKEND_NOSUPERUSER, and SIGNAL_BACKEND_SUCCESS.

◆ pg_wait_until_termination()

static bool pg_wait_until_termination ( int  pid,
int64  timeout 
)
static

Definition at line 143 of file signalfuncs.c.

144 {
145  /*
146  * Wait in steps of waittime milliseconds until this function exits or
147  * timeout.
148  */
149  int64 waittime = 100;
150 
151  /*
152  * Initially remaining time is the entire timeout specified by the user.
153  */
154  int64 remainingtime = timeout;
155 
156  /*
157  * Check existence of the backend. If the backend still exists, then wait
158  * for waittime milliseconds, again check for the existence. Repeat this
159  * until timeout or an error occurs or a pending interrupt such as query
160  * cancel gets processed.
161  */
162  do
163  {
164  if (remainingtime < waittime)
165  waittime = remainingtime;
166 
167  if (kill(pid, 0) == -1)
168  {
169  if (errno == ESRCH)
170  return true;
171  else
172  ereport(ERROR,
173  (errcode(ERRCODE_INTERNAL_ERROR),
174  errmsg("could not check the existence of the backend with PID %d: %m",
175  pid)));
176  }
177 
178  /* Process interrupts, if any, before waiting */
180 
181  (void) WaitLatch(MyLatch,
183  waittime,
185 
187 
188  remainingtime -= waittime;
189  } while (remainingtime > 0);
190 
192  (errmsg_plural("backend with PID %d did not terminate within %lld millisecond",
193  "backend with PID %d did not terminate within %lld milliseconds",
194  timeout,
195  pid, (long long int) timeout)));
196 
197  return false;
198 }
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1179
struct Latch * MyLatch
Definition: globals.c:58
void ResetLatch(Latch *latch)
Definition: latch.c:699
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:492
#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
@ WAIT_EVENT_BACKEND_TERMINATION
Definition: wait_event.h:85

References CHECK_FOR_INTERRUPTS, ereport, errcode(), errmsg(), errmsg_plural(), ERROR, kill, MyLatch, ResetLatch(), WAIT_EVENT_BACKEND_TERMINATION, WaitLatch(), WARNING, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by pg_terminate_backend().