PostgreSQL Source Code  git master
cancel.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "common/connect.h"
#include "fe_utils/cancel.h"
#include "fe_utils/string_utils.h"
Include dependency graph for cancel.c:

Go to the source code of this file.

Macros

#define write_stderr(str)
 

Functions

void SetCancelConn (PGconn *conn)
 
void ResetCancelConn (void)
 
static void handle_sigint (SIGNAL_ARGS)
 
void setup_cancel_handler (void(*callback)(void))
 

Variables

static PGcancel *volatile cancelConn = NULL
 
volatile sig_atomic_t CancelRequested = false
 
static void(* cancel_callback )(void) = NULL
 

Macro Definition Documentation

◆ write_stderr

#define write_stderr (   str)
Value:
do { \
const char *str_ = (str); \
int rc_; \
rc_ = write(fileno(stderr), str_, strlen(str_)); \
(void) rc_; \
} while (0)
#define write(a, b, c)
Definition: win32.h:14

Definition at line 31 of file cancel.c.

Referenced by handle_sigint(), and setup_cancel_handler().

Function Documentation

◆ handle_sigint()

static void handle_sigint ( SIGNAL_ARGS  )
static

Definition at line 146 of file cancel.c.

References _, cancel_callback, CancelRequested, PQcancel(), and write_stderr.

Referenced by setup_cancel_handler().

147 {
148  int save_errno = errno;
149  char errbuf[256];
150 
151  CancelRequested = true;
152 
153  if (cancel_callback != NULL)
154  cancel_callback();
155 
156  /* Send QueryCancel if we are processing a database query */
157  if (cancelConn != NULL)
158  {
159  if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
160  {
161  write_stderr(_("Cancel request sent\n"));
162  }
163  else
164  {
165  write_stderr(_("Could not send cancel request: "));
166  write_stderr(errbuf);
167  }
168  }
169 
170  errno = save_errno; /* just in case the write changed it */
171 }
static void(* cancel_callback)(void)
Definition: cancel.c:61
#define write_stderr(str)
Definition: cancel.c:31
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4446
volatile sig_atomic_t CancelRequested
Definition: cancel.c:52
static PGcancel *volatile cancelConn
Definition: cancel.c:43
#define _(x)
Definition: elog.c:88

◆ ResetCancelConn()

void ResetCancelConn ( void  )

Definition at line 100 of file cancel.c.

References cancelConn, and PQfreeCancel().

Referenced by CheckConnection(), consumeQueryResult(), do_connect(), do_lo_export(), do_lo_import(), do_lo_unlink(), executeMaintenanceCommand(), ParallelSlotsGetIdle(), ProcessResult(), PSQLexec(), PSQLexecWatch(), runInitSteps(), and SendQuery().

101 {
102  PGcancel *oldCancelConn;
103 
104 #ifdef WIN32
105  EnterCriticalSection(&cancelConnLock);
106 #endif
107 
108  oldCancelConn = cancelConn;
109 
110  /* be sure handle_sigint doesn't use pointer while freeing */
111  cancelConn = NULL;
112 
113  if (oldCancelConn != NULL)
114  PQfreeCancel(oldCancelConn);
115 
116 #ifdef WIN32
117  LeaveCriticalSection(&cancelConnLock);
118 #endif
119 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4314
static PGcancel *volatile cancelConn
Definition: cancel.c:43

◆ SetCancelConn()

void SetCancelConn ( PGconn conn)

Definition at line 70 of file cancel.c.

References cancelConn, PQfreeCancel(), and PQgetCancel().

Referenced by consumeQueryResult(), do_lo_export(), do_lo_import(), do_lo_unlink(), executeMaintenanceCommand(), ParallelSlotsGetIdle(), ProcessResult(), PSQLexec(), PSQLexecWatch(), runInitSteps(), and SendQuery().

71 {
72  PGcancel *oldCancelConn;
73 
74 #ifdef WIN32
75  EnterCriticalSection(&cancelConnLock);
76 #endif
77 
78  /* Free the old one if we have one */
79  oldCancelConn = cancelConn;
80 
81  /* be sure handle_sigint doesn't use pointer while freeing */
82  cancelConn = NULL;
83 
84  if (oldCancelConn != NULL)
85  PQfreeCancel(oldCancelConn);
86 
87  cancelConn = PQgetCancel(conn);
88 
89 #ifdef WIN32
90  LeaveCriticalSection(&cancelConnLock);
91 #endif
92 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4314
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:4291
static PGcancel *volatile cancelConn
Definition: cancel.c:43

◆ setup_cancel_handler()

void setup_cancel_handler ( void(*)(void)  callback)

Definition at line 179 of file cancel.c.

References _, callback(), cancel_callback, CancelRequested, handle_sigint(), PQcancel(), pqsignal(), setup_cancel_handler(), and write_stderr.

Referenced by setup_cancel_handler().

180 {
182  pqsignal(SIGINT, handle_sigint);
183 }
static void(* cancel_callback)(void)
Definition: cancel.c:61
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static void handle_sigint(SIGNAL_ARGS)
Definition: cancel.c:146

Variable Documentation

◆ cancel_callback

void(* cancel_callback) (void) = NULL
static

Definition at line 61 of file cancel.c.

Referenced by handle_sigint(), and setup_cancel_handler().

◆ cancelConn

PGcancel* volatile cancelConn = NULL
static

Definition at line 43 of file cancel.c.

Referenced by ResetCancelConn(), and SetCancelConn().

◆ CancelRequested

volatile sig_atomic_t CancelRequested = false