1 /*-------------------------------------------------------------------------
4 * Functions for signalling backends
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/storage/ipc/signalfuncs.c
13 *-------------------------------------------------------------------------
19 #include "catalog/pg_authid.h"
20 #include "miscadmin.h"
21 #include "postmaster/syslogger.h"
22 #include "storage/pmsignal.h"
23 #include "storage/proc.h"
24 #include "storage/procarray.h"
25 #include "utils/acl.h"
26 #include "utils/builtins.h"
30 * Send a signal to another backend.
32 * The signal is delivered if the user is either a superuser or the same
33 * role as the backend being signaled. For "dangerous" signals, an explicit
34 * check for superuser needs to be done prior to calling this function.
36 * Returns 0 on success, 1 on general failure, 2 on normal permission error
37 * and 3 if the caller needs to be a superuser.
39 * In the event of a general failure (return code 1), a warning message will
40 * be emitted. For permission errors, doing that is the responsibility of
43 #define SIGNAL_BACKEND_SUCCESS 0
44 #define SIGNAL_BACKEND_ERROR 1
45 #define SIGNAL_BACKEND_NOPERMISSION 2
46 #define SIGNAL_BACKEND_NOSUPERUSER 3
48 pg_signal_backend(int pid, int sig)
50 PGPROC *proc = BackendPidGetProc(pid);
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.
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.
67 (errmsg("PID %d is not a PostgreSQL server process", pid)));
68 return SIGNAL_BACKEND_ERROR;
71 /* Only allow superusers to signal superuser-owned backends. */
72 if (superuser_arg(proc->roleId) && !superuser())
73 return SIGNAL_BACKEND_NOSUPERUSER;
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))
78 return SIGNAL_BACKEND_NOPERMISSION;
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.
89 /* If we have setsid(), signal the backend's whole process group */
96 /* Again, just a warning to allow loops */
98 (errmsg("could not send signal to process %d: %m", pid)));
99 return SIGNAL_BACKEND_ERROR;
101 return SIGNAL_BACKEND_SUCCESS;
105 * Signal to cancel a backend process. This is allowed if you are a member of
106 * the role whose process is being canceled.
108 * Note that only superusers can signal superuser-owned processes.
111 pg_cancel_backend(PG_FUNCTION_ARGS)
113 int r = pg_signal_backend(PG_GETARG_INT32(0), SIGINT);
115 if (r == SIGNAL_BACKEND_NOSUPERUSER)
117 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
118 (errmsg("must be a superuser to cancel superuser query"))));
120 if (r == SIGNAL_BACKEND_NOPERMISSION)
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"))));
125 PG_RETURN_BOOL(r == SIGNAL_BACKEND_SUCCESS);
129 * Signal to terminate a backend process. This is allowed if you are a member
130 * of the role whose process is being terminated.
132 * Note that only superusers can signal superuser-owned processes.
135 pg_terminate_backend(PG_FUNCTION_ARGS)
137 int r = pg_signal_backend(PG_GETARG_INT32(0), SIGTERM);
139 if (r == SIGNAL_BACKEND_NOSUPERUSER)
141 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
142 (errmsg("must be a superuser to terminate superuser process"))));
144 if (r == SIGNAL_BACKEND_NOPERMISSION)
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"))));
149 PG_RETURN_BOOL(r == SIGNAL_BACKEND_SUCCESS);
153 * Signal to reload the database configuration
155 * Permission checking for this function is managed through the normal
159 pg_reload_conf(PG_FUNCTION_ARGS)
161 if (kill(PostmasterPid, SIGHUP))
164 (errmsg("failed to send signal to postmaster: %m")));
165 PG_RETURN_BOOL(false);
168 PG_RETURN_BOOL(true);
175 * This function is kept to support adminpack 1.0.
178 pg_rotate_logfile(PG_FUNCTION_ARGS)
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()"))));
188 if (!Logging_collector)
191 (errmsg("rotation not possible because log collection not active")));
192 PG_RETURN_BOOL(false);
195 SendPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE);
196 PG_RETURN_BOOL(true);
202 * Permission checking for this function is managed through the normal
206 pg_rotate_logfile_v2(PG_FUNCTION_ARGS)
208 if (!Logging_collector)
211 (errmsg("rotation not possible because log collection not active")));
212 PG_RETURN_BOOL(false);
215 SendPostmasterSignal(PMSIGNAL_ROTATE_LOGFILE);
216 PG_RETURN_BOOL(true);