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
 
static const char * cancel_sent_msg = NULL
 
static const char * cancel_not_sent_msg = 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.

Function Documentation

◆ handle_sigint()

static void handle_sigint ( SIGNAL_ARGS  )
static

Definition at line 153 of file cancel.c.

154 {
155  int save_errno = errno;
156  char errbuf[256];
157 
158  CancelRequested = true;
159 
160  if (cancel_callback != NULL)
161  cancel_callback();
162 
163  /* Send QueryCancel if we are processing a database query */
164  if (cancelConn != NULL)
165  {
166  if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
167  {
169  }
170  else
171  {
173  write_stderr(errbuf);
174  }
175  }
176 
177  errno = save_errno; /* just in case the write changed it */
178 }
volatile sig_atomic_t CancelRequested
Definition: cancel.c:59
static void(* cancel_callback)(void)
Definition: cancel.c:68
static const char * cancel_sent_msg
Definition: cancel.c:49
static PGcancel *volatile cancelConn
Definition: cancel.c:43
#define write_stderr(str)
Definition: cancel.c:31
static const char * cancel_not_sent_msg
Definition: cancel.c:50
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:4417

References cancel_callback, cancel_not_sent_msg, cancel_sent_msg, cancelConn, CancelRequested, PQcancel(), and write_stderr.

Referenced by setup_cancel_handler().

◆ ResetCancelConn()

void ResetCancelConn ( void  )

Definition at line 107 of file cancel.c.

108 {
109  PGcancel *oldCancelConn;
110 
111 #ifdef WIN32
112  EnterCriticalSection(&cancelConnLock);
113 #endif
114 
115  oldCancelConn = cancelConn;
116 
117  /* be sure handle_sigint doesn't use pointer while freeing */
118  cancelConn = NULL;
119 
120  if (oldCancelConn != NULL)
121  PQfreeCancel(oldCancelConn);
122 
123 #ifdef WIN32
124  LeaveCriticalSection(&cancelConnLock);
125 #endif
126 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:4371

References cancelConn, and PQfreeCancel().

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

◆ SetCancelConn()

void SetCancelConn ( PGconn conn)

Definition at line 77 of file cancel.c.

78 {
79  PGcancel *oldCancelConn;
80 
81 #ifdef WIN32
82  EnterCriticalSection(&cancelConnLock);
83 #endif
84 
85  /* Free the old one if we have one */
86  oldCancelConn = cancelConn;
87 
88  /* be sure handle_sigint doesn't use pointer while freeing */
89  cancelConn = NULL;
90 
91  if (oldCancelConn != NULL)
92  PQfreeCancel(oldCancelConn);
93 
95 
96 #ifdef WIN32
97  LeaveCriticalSection(&cancelConnLock);
98 #endif
99 }
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:4303
PGconn * conn
Definition: streamutil.c:54

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

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

◆ setup_cancel_handler()

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

Definition at line 186 of file cancel.c.

187 {
189  cancel_sent_msg = _("Cancel request sent\n");
190  cancel_not_sent_msg = _("Could not send cancel request: ");
191 
192  pqsignal(SIGINT, handle_sigint);
193 }
static void handle_sigint(SIGNAL_ARGS)
Definition: cancel.c:153
#define _(x)
Definition: elog.c:89
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
static void callback(struct sockaddr *addr, struct sockaddr *mask, void *unused)
Definition: test_ifaddrs.c:48

References _, callback(), cancel_callback, cancel_not_sent_msg, cancel_sent_msg, handle_sigint(), and pqsignal().

Variable Documentation

◆ cancel_callback

void(* cancel_callback) (void) ( void  ) = NULL
static

Definition at line 68 of file cancel.c.

Referenced by handle_sigint(), and setup_cancel_handler().

◆ cancel_not_sent_msg

const char* cancel_not_sent_msg = NULL
static

Definition at line 50 of file cancel.c.

Referenced by handle_sigint(), and setup_cancel_handler().

◆ cancel_sent_msg

const char* cancel_sent_msg = NULL
static

Definition at line 49 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 handle_sigint(), ResetCancelConn(), and SetCancelConn().

◆ CancelRequested

volatile sig_atomic_t CancelRequested = false