1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include "postgres.h"
#include <unistd.h>
#include <errno.h>
#include <sys/resource.h>
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/proc.h"
#include "storage/procarray.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(set_backend_priority);
PG_FUNCTION_INFO_V1(get_backend_priority);
extern Datum get_backend_priority(PG_FUNCTION_ARGS);
extern Datum set_backend_priority(PG_FUNCTION_ARGS);
Datum
get_backend_priority(PG_FUNCTION_ARGS)
{
int pid = PG_GETARG_INT32(0);
int priority;
int save_errno = errno;
if (!IsBackendPid(pid)) {
ereport(WARNING,
(errmsg("PID %d is not a PostgreSQL server process", pid)));
PG_RETURN_NULL();
}
errno = 0;
priority = getpriority(PRIO_PROCESS, pid);
if (priority == -1) {
/* We need to check errno to determine whether an error has occurred
or if the priority of the process is just '-1'.
*/
if (errno == ESRCH || errno == EINVAL) {
errno = save_errno;
ereport(ERROR,
(errcode(ERRCODE_IO_ERROR),
(errmsg("getpriority() could not find the requested backend"))));
}
}
errno = save_errno;
PG_RETURN_INT32(priority);
}
/* Set the 'nice' priority of the given backend. The priority passed in should
* typically be between 1 and 20, inclusive, since priorities may only
* be adjusted upwards by non-root users. If the backend had been manually
* set (by a root user) to a negative nice value, it may be possible to pass
* in a greater-but-still-negative value as the new priority.
*/
Datum
set_backend_priority(PG_FUNCTION_ARGS)
{
PGPROC *proc;
int pid = PG_GETARG_INT32(0);
int prio = PG_GETARG_INT32(1);
int save_errno = errno;
bool success = true;
if (pid == MyProcPid) {
/* Quick check: if we are setting the priority of our own backend,
* skip permissions checks and chekcs of whether 'pid' is a valid
* backend.
*/
}
else if (!superuser()) {
/*
* Since the user is not superuser, check for matching roles. Trust
* that BackendPidGetProc will return NULL if the pid isn't valid,
* even though the check for whether it's a backend process is below.
* The IsBackendPid check can't be relied on as definitive even if it
* was first. The process might end between successive checks
* regardless of their order. There's no way to acquire a lock on an
* arbitrary process to prevent that.
*/
proc = BackendPidGetProc(pid);
if (proc == NULL) {
/*
* This is just a warning so a loop-through-resultset will not
* abort if one backend terminated on its own during the run
*/
ereport(WARNING,
(errmsg("PID %d is not a PostgreSQL server process", pid)));
success = false;
}
else if (proc->roleId != GetUserId())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
(errmsg("must be superuser to nice arbitrary backends"))));
/* Otherwise, the backend PID is valid and our user is allowed
* to set its priority.
*/
}
else if (!IsBackendPid(pid))
{
ereport(WARNING,
(errmsg("PID %d is not a PostgreSQL server process", pid)));
success = false;
}
if (success) {
errno = 0;
if (setpriority(PRIO_PROCESS, pid, prio) == 0) {
ereport(NOTICE,
(errmsg("Set priority of backend %d to %d", pid, prio)));
}
else {
if (errno == ESRCH || errno == EINVAL) {
errno = save_errno;
ereport(ERROR,
(errcode(ERRCODE_IO_ERROR),
(errmsg("setpriority(): could not find the requested backend"))));
}
else {
/* Assume EPERM or EACCES */
ereport(WARNING,
(errmsg("setpriority(): permission denied")));
success = false;
}
}
}
errno = save_errno;
PG_RETURN_BOOL(success);
}
|