PostgreSQL Source Code  git master
signalfuncs.c File Reference
#include "postgres.h"
#include <signal.h>
#include "catalog/pg_authid.h"
#include "miscadmin.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)
 
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 44 of file signalfuncs.c.

Referenced by pg_signal_backend().

◆ SIGNAL_BACKEND_NOPERMISSION

#define SIGNAL_BACKEND_NOPERMISSION   2

Definition at line 45 of file signalfuncs.c.

Referenced by pg_cancel_backend(), pg_signal_backend(), and pg_terminate_backend().

◆ SIGNAL_BACKEND_NOSUPERUSER

#define SIGNAL_BACKEND_NOSUPERUSER   3

Definition at line 46 of file signalfuncs.c.

Referenced by pg_cancel_backend(), pg_signal_backend(), and pg_terminate_backend().

◆ SIGNAL_BACKEND_SUCCESS

#define SIGNAL_BACKEND_SUCCESS   0

Definition at line 43 of file signalfuncs.c.

Referenced by pg_cancel_backend(), pg_signal_backend(), and pg_terminate_backend().

Function Documentation

◆ pg_cancel_backend()

Datum pg_cancel_backend ( PG_FUNCTION_ARGS  )

Definition at line 111 of file signalfuncs.c.

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

112 {
113  int r = pg_signal_backend(PG_GETARG_INT32(0), SIGINT);
114 
116  ereport(ERROR,
117  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
118  (errmsg("must be a superuser to cancel superuser query"))));
119 
121  ereport(ERROR,
122  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
123  (errmsg("must be a member of the role whose query is being canceled or member of pg_signal_backend"))));
124 
126 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int errcode(int sqlerrcode)
Definition: elog.c:570
#define SIGNAL_BACKEND_SUCCESS
Definition: signalfuncs.c:43
static int pg_signal_backend(int pid, int sig)
Definition: signalfuncs.c:48
#define SIGNAL_BACKEND_NOSUPERUSER
Definition: signalfuncs.c:46
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define SIGNAL_BACKEND_NOPERMISSION
Definition: signalfuncs.c:45
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_reload_conf()

Datum pg_reload_conf ( PG_FUNCTION_ARGS  )

Definition at line 159 of file signalfuncs.c.

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

160 {
161  if (kill(PostmasterPid, SIGHUP))
162  {
164  (errmsg("failed to send signal to postmaster: %m")));
165  PG_RETURN_BOOL(false);
166  }
167 
168  PG_RETURN_BOOL(true);
169 }
#define kill(pid, sig)
Definition: win32_port.h:435
#define SIGHUP
Definition: win32_port.h:163
#define ereport(elevel, rest)
Definition: elog.h:141
pid_t PostmasterPid
Definition: globals.c:95
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ pg_rotate_logfile()

Datum pg_rotate_logfile ( PG_FUNCTION_ARGS  )

Definition at line 178 of file signalfuncs.c.

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

179 {
180  if (!superuser())
181  ereport(ERROR,
182  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
183  (errmsg("must be superuser to rotate log files with adminpack 1.0"),
184  /* translator: %s is a SQL function name */
185  errhint("Consider using %s, which is part of core, instead.",
186  "pg_logfile_rotate()"))));
187 
188  if (!Logging_collector)
189  {
191  (errmsg("rotation not possible because log collection not active")));
192  PG_RETURN_BOOL(false);
193  }
194 
196  PG_RETURN_BOOL(true);
197 }
int errhint(const char *fmt,...)
Definition: elog.c:974
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
bool Logging_collector
Definition: syslogger.c:69
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:784
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:146

◆ pg_rotate_logfile_v2()

Datum pg_rotate_logfile_v2 ( PG_FUNCTION_ARGS  )

Definition at line 206 of file signalfuncs.c.

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

207 {
208  if (!Logging_collector)
209  {
211  (errmsg("rotation not possible because log collection not active")));
212  PG_RETURN_BOOL(false);
213  }
214 
216  PG_RETURN_BOOL(true);
217 }
bool Logging_collector
Definition: syslogger.c:69
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:784
void SendPostmasterSignal(PMSignalReason reason)
Definition: pmsignal.c:146

◆ pg_signal_backend()

static int pg_signal_backend ( int  pid,
int  sig 
)
static

Definition at line 48 of file signalfuncs.c.

References BackendPidGetProc(), ereport, errmsg(), GetUserId(), has_privs_of_role(), kill, PGPROC::roleId, 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().

49 {
50  PGPROC *proc = BackendPidGetProc(pid);
51 
52  /*
53  * BackendPidGetProc returns NULL if the pid isn't valid; but by the time
54  * we reach kill(), a process for which we get a valid proc here might
55  * have terminated on its own. There's no way to acquire a lock on an
56  * arbitrary process to prevent that. But since so far all the callers of
57  * this mechanism involve some request for ending the process anyway, that
58  * it might end on its own first is not a problem.
59  */
60  if (proc == NULL)
61  {
62  /*
63  * This is just a warning so a loop-through-resultset will not abort
64  * if one backend terminated on its own during the run.
65  */
67  (errmsg("PID %d is not a PostgreSQL server process", pid)));
68  return SIGNAL_BACKEND_ERROR;
69  }
70 
71  /* Only allow superusers to signal superuser-owned backends. */
72  if (superuser_arg(proc->roleId) && !superuser())
74 
75  /* Users can signal backends they have role membership in. */
76  if (!has_privs_of_role(GetUserId(), proc->roleId) &&
77  !has_privs_of_role(GetUserId(), DEFAULT_ROLE_SIGNAL_BACKENDID))
79 
80  /*
81  * Can the process we just validated above end, followed by the pid being
82  * recycled for a new process, before reaching here? Then we'd be trying
83  * to kill the wrong thing. Seems near impossible when sequential pid
84  * assignment and wraparound is used. Perhaps it could happen on a system
85  * where pid re-use is randomized. That race condition possibility seems
86  * too unlikely to worry about.
87  */
88 
89  /* If we have setsid(), signal the backend's whole process group */
90 #ifdef HAVE_SETSID
91  if (kill(-pid, sig))
92 #else
93  if (kill(pid, sig))
94 #endif
95  {
96  /* Again, just a warning to allow loops */
98  (errmsg("could not send signal to process %d: %m", pid)));
99  return SIGNAL_BACKEND_ERROR;
100  }
101  return SIGNAL_BACKEND_SUCCESS;
102 }
Oid GetUserId(void)
Definition: miscinit.c:380
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:2363
bool has_privs_of_role(Oid member, Oid role)
Definition: acl.c:4908
Oid roleId
Definition: proc.h:115
bool superuser(void)
Definition: superuser.c:47
#define kill(pid, sig)
Definition: win32_port.h:435
#define SIGNAL_BACKEND_SUCCESS
Definition: signalfuncs.c:43
#define SIGNAL_BACKEND_NOSUPERUSER
Definition: signalfuncs.c:46
#define ereport(elevel, rest)
Definition: elog.h:141
bool superuser_arg(Oid roleid)
Definition: superuser.c:57
#define SIGNAL_BACKEND_NOPERMISSION
Definition: signalfuncs.c:45
#define WARNING
Definition: elog.h:40
#define SIGNAL_BACKEND_ERROR
Definition: signalfuncs.c:44
static int sig
Definition: pg_ctl.c:84
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: proc.h:95

◆ pg_terminate_backend()

Datum pg_terminate_backend ( PG_FUNCTION_ARGS  )

Definition at line 135 of file signalfuncs.c.

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

136 {
137  int r = pg_signal_backend(PG_GETARG_INT32(0), SIGTERM);
138 
140  ereport(ERROR,
141  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
142  (errmsg("must be a superuser to terminate superuser process"))));
143 
145  ereport(ERROR,
146  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
147  (errmsg("must be a member of the role whose process is being terminated or member of pg_signal_backend"))));
148 
150 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
int errcode(int sqlerrcode)
Definition: elog.c:570
#define SIGNAL_BACKEND_SUCCESS
Definition: signalfuncs.c:43
static int pg_signal_backend(int pid, int sig)
Definition: signalfuncs.c:48
#define SIGNAL_BACKEND_NOSUPERUSER
Definition: signalfuncs.c:46
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define SIGNAL_BACKEND_NOPERMISSION
Definition: signalfuncs.c:45
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:349
int errmsg(const char *fmt,...)
Definition: elog.c:784