PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
common.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <unistd.h>
#include "common.h"
Include dependency graph for common.c:

Go to the source code of this file.

Macros

#define PG_YESLETTER   gettext_noop("y")
 
#define PG_NOLETTER   gettext_noop("n")
 

Functions

void handle_help_version_opts (int argc, char *argv[], const char *fixed_progname, help_handler hlp)
 
PGconnconnectDatabase (const char *dbname, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool fail_ok, bool allow_password_reuse)
 
PGconnconnectMaintenanceDatabase (const char *maintenance_db, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname)
 
PGresultexecuteQuery (PGconn *conn, const char *query, const char *progname, bool echo)
 
void executeCommand (PGconn *conn, const char *query, const char *progname, bool echo)
 
bool executeMaintenanceCommand (PGconn *conn, const char *query, bool echo)
 
bool yesno_prompt (const char *question)
 
void SetCancelConn (PGconn *conn)
 
void ResetCancelConn (void)
 
static void handle_sigint (SIGNAL_ARGS)
 
void setup_cancel_handler (void)
 

Variables

static PGcancel *volatile cancelConn = NULL
 
bool CancelRequested = false
 

Macro Definition Documentation

#define PG_NOLETTER   gettext_noop("n")

Definition at line 262 of file common.c.

Referenced by yesno_prompt().

#define PG_YESLETTER   gettext_noop("y")

Definition at line 260 of file common.c.

Referenced by yesno_prompt().

Function Documentation

PGconn* connectDatabase ( const char *  dbname,
const char *  pghost,
const char *  pgport,
const char *  pguser,
enum trivalue  prompt_password,
const char *  progname,
bool  fail_ok,
bool  allow_password_reuse 
)

Definition at line 66 of file common.c.

References _, conn, CONNECTION_BAD, dbname, have_password, password, pghost, pgport, PQconnectdbParams(), PQconnectionNeedsPassword(), PQerrorMessage(), PQfinish(), PQstatus(), progname, simple_prompt(), TRI_NO, TRI_YES, and values.

Referenced by connectMaintenanceDatabase().

69 {
70  PGconn *conn;
71  bool new_pass;
72  static bool have_password = false;
73  static char password[100];
74 
75  if (!allow_password_reuse)
76  have_password = false;
77 
78  if (!have_password && prompt_password == TRI_YES)
79  {
80  simple_prompt("Password: ", password, sizeof(password), false);
81  have_password = true;
82  }
83 
84  /*
85  * Start the connection. Loop until we have a password if requested by
86  * backend.
87  */
88  do
89  {
90  const char *keywords[7];
91  const char *values[7];
92 
93  keywords[0] = "host";
94  values[0] = pghost;
95  keywords[1] = "port";
96  values[1] = pgport;
97  keywords[2] = "user";
98  values[2] = pguser;
99  keywords[3] = "password";
100  values[3] = have_password ? password : NULL;
101  keywords[4] = "dbname";
102  values[4] = dbname;
103  keywords[5] = "fallback_application_name";
104  values[5] = progname;
105  keywords[6] = NULL;
106  values[6] = NULL;
107 
108  new_pass = false;
109  conn = PQconnectdbParams(keywords, values, true);
110 
111  if (!conn)
112  {
113  fprintf(stderr, _("%s: could not connect to database %s: out of memory\n"),
114  progname, dbname);
115  exit(1);
116  }
117 
118  /*
119  * No luck? Trying asking (again) for a password.
120  */
121  if (PQstatus(conn) == CONNECTION_BAD &&
123  prompt_password != TRI_NO)
124  {
125  PQfinish(conn);
126  simple_prompt("Password: ", password, sizeof(password), false);
127  have_password = true;
128  new_pass = true;
129  }
130  } while (new_pass);
131 
132  /* check to see that the backend connection was successfully made */
133  if (PQstatus(conn) == CONNECTION_BAD)
134  {
135  if (fail_ok)
136  {
137  PQfinish(conn);
138  return NULL;
139  }
140  fprintf(stderr, _("%s: could not connect to database %s: %s"),
141  progname, dbname, PQerrorMessage(conn));
142  exit(1);
143  }
144 
145  return conn;
146 }
static char password[100]
Definition: streamutil.c:48
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
const char * progname
Definition: pg_standby.c:37
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:529
PGconn * conn
Definition: streamutil.c:49
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
char * pghost
Definition: pgbench.c:180
static bool have_password
Definition: streamutil.c:47
char * dbname
Definition: streamutil.c:45
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6131
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
#define _(x)
Definition: elog.c:84
char * pgport
Definition: pgbench.c:181
PGconn* connectMaintenanceDatabase ( const char *  maintenance_db,
const char *  pghost,
const char *  pgport,
const char *  pguser,
enum trivalue  prompt_password,
const char *  progname 
)

Definition at line 152 of file common.c.

References conn, and connectDatabase().

Referenced by cluster_all_databases(), main(), reindex_all_databases(), and vacuum_all_databases().

156 {
157  PGconn *conn;
158 
159  /* If a maintenance database name was specified, just connect to it. */
160  if (maintenance_db)
161  return connectDatabase(maintenance_db, pghost, pgport, pguser,
162  prompt_password, progname, false, false);
163 
164  /* Otherwise, try postgres first and then template1. */
165  conn = connectDatabase("postgres", pghost, pgport, pguser, prompt_password,
166  progname, true, false);
167  if (!conn)
168  conn = connectDatabase("template1", pghost, pgport, pguser,
169  prompt_password, progname, false, false);
170 
171  return conn;
172 }
PGconn * connectDatabase(const char *dbname, const char *pghost, const char *pgport, const char *pguser, enum trivalue prompt_password, const char *progname, bool fail_ok, bool allow_password_reuse)
Definition: common.c:66
const char * progname
Definition: pg_standby.c:37
PGconn * conn
Definition: streamutil.c:49
char * pghost
Definition: pgbench.c:180
char * pgport
Definition: pgbench.c:181
void executeCommand ( PGconn conn,
const char *  query,
const char *  progname,
bool  echo 
)

Definition at line 205 of file common.c.

References _, PGRES_COMMAND_OK, PQclear(), PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().

207 {
208  PGresult *res;
209 
210  if (echo)
211  printf("%s\n", query);
212 
213  res = PQexec(conn, query);
214  if (!res ||
216  {
217  fprintf(stderr, _("%s: query failed: %s"),
218  progname, PQerrorMessage(conn));
219  fprintf(stderr, _("%s: query was: %s\n"),
220  progname, query);
221  PQfinish(conn);
222  exit(1);
223  }
224 
225  PQclear(res);
226 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
const char * progname
Definition: pg_standby.c:37
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84
bool executeMaintenanceCommand ( PGconn conn,
const char *  query,
bool  echo 
)

Definition at line 235 of file common.c.

References PGRES_COMMAND_OK, PQclear(), PQexec(), PQresultStatus(), ResetCancelConn(), and SetCancelConn().

Referenced by cluster_one_database(), reindex_one_database(), reindex_system_catalogs(), and run_vacuum_command().

236 {
237  PGresult *res;
238  bool r;
239 
240  if (echo)
241  printf("%s\n", query);
242 
243  SetCancelConn(conn);
244  res = PQexec(conn, query);
245  ResetCancelConn();
246 
247  r = (res && PQresultStatus(res) == PGRES_COMMAND_OK);
248 
249  if (res)
250  PQclear(res);
251 
252  return r;
253 }
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
void SetCancelConn(void)
Definition: common.c:446
void PQclear(PGresult *res)
Definition: fe-exec.c:671
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
void ResetCancelConn(void)
Definition: common.c:476
PGresult* executeQuery ( PGconn conn,
const char *  query,
const char *  progname,
bool  echo 
)

Definition at line 178 of file common.c.

References _, PGRES_TUPLES_OK, PQerrorMessage(), PQexec(), PQfinish(), and PQresultStatus().

179 {
180  PGresult *res;
181 
182  if (echo)
183  printf("%s\n", query);
184 
185  res = PQexec(conn, query);
186  if (!res ||
188  {
189  fprintf(stderr, _("%s: query failed: %s"),
190  progname, PQerrorMessage(conn));
191  fprintf(stderr, _("%s: query was: %s\n"),
192  progname, query);
193  PQfinish(conn);
194  exit(1);
195  }
196 
197  return res;
198 }
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
const char * progname
Definition: pg_standby.c:37
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:2647
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:1897
#define _(x)
Definition: elog.c:84
void handle_help_version_opts ( int  argc,
char *  argv[],
const char *  fixed_progname,
help_handler  hlp 
)

Definition at line 35 of file common.c.

References get_progname().

Referenced by main().

37 {
38  if (argc > 1)
39  {
40  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
41  {
42  hlp(get_progname(argv[0]));
43  exit(0);
44  }
45  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
46  {
47  printf("%s (PostgreSQL) " PG_VERSION "\n", fixed_progname);
48  exit(0);
49  }
50  }
51 }
const char * get_progname(const char *argv0)
Definition: path.c:453
static void handle_sigint ( SIGNAL_ARGS  )
static

Definition at line 354 of file common.c.

References _, CancelRequested, and PQcancel().

Referenced by setup_cancel_handler().

355 {
356  int save_errno = errno;
357  char errbuf[256];
358 
359  /* Send QueryCancel if we are processing a database query */
360  if (cancelConn != NULL)
361  {
362  if (PQcancel(cancelConn, errbuf, sizeof(errbuf)))
363  {
364  CancelRequested = true;
365  fprintf(stderr, _("Cancel request sent\n"));
366  }
367  else
368  fprintf(stderr, _("Could not send cancel request: %s"), errbuf);
369  }
370  else
371  CancelRequested = true;
372 
373  errno = save_errno; /* just in case the write changed it */
374 }
static PGcancel *volatile cancelConn
Definition: common.c:23
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
Definition: fe-connect.c:3906
bool CancelRequested
Definition: common.c:24
#define _(x)
Definition: elog.c:84
void ResetCancelConn ( void  )

Definition at line 327 of file common.c.

References cancelConn, and PQfreeCancel().

328 {
329  PGcancel *oldCancelConn;
330 
331 #ifdef WIN32
332  EnterCriticalSection(&cancelConnLock);
333 #endif
334 
335  oldCancelConn = cancelConn;
336 
337  /* be sure handle_sigint doesn't use pointer while freeing */
338  cancelConn = NULL;
339 
340  if (oldCancelConn != NULL)
341  PQfreeCancel(oldCancelConn);
342 
343 #ifdef WIN32
344  LeaveCriticalSection(&cancelConnLock);
345 #endif
346 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3774
static PGcancel *volatile cancelConn
Definition: common.c:23
void SetCancelConn ( PGconn conn)

Definition at line 297 of file common.c.

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

298 {
299  PGcancel *oldCancelConn;
300 
301 #ifdef WIN32
302  EnterCriticalSection(&cancelConnLock);
303 #endif
304 
305  /* Free the old one if we have one */
306  oldCancelConn = cancelConn;
307 
308  /* be sure handle_sigint doesn't use pointer while freeing */
309  cancelConn = NULL;
310 
311  if (oldCancelConn != NULL)
312  PQfreeCancel(oldCancelConn);
313 
314  cancelConn = PQgetCancel(conn);
315 
316 #ifdef WIN32
317  LeaveCriticalSection(&cancelConnLock);
318 #endif
319 }
void PQfreeCancel(PGcancel *cancel)
Definition: fe-connect.c:3774
PGcancel * PQgetCancel(PGconn *conn)
Definition: fe-connect.c:3751
static PGcancel *volatile cancelConn
Definition: common.c:23
void setup_cancel_handler ( void  )

Definition at line 377 of file common.c.

References handle_sigint(), and pqsignal().

378 {
379  pqsignal(SIGINT, handle_sigint);
380 }
static void handle_sigint(SIGNAL_ARGS)
Definition: common.c:354
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
bool yesno_prompt ( const char *  question)

Definition at line 265 of file common.c.

References _, PG_NOLETTER, PG_YESLETTER, simple_prompt(), and snprintf().

Referenced by main().

266 {
267  char prompt[256];
268 
269  /*------
270  translator: This is a question followed by the translated options for
271  "yes" and "no". */
272  snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
273  _(question), _(PG_YESLETTER), _(PG_NOLETTER));
274 
275  for (;;)
276  {
277  char resp[10];
278 
279  simple_prompt(prompt, resp, sizeof(resp), true);
280 
281  if (strcmp(resp, _(PG_YESLETTER)) == 0)
282  return true;
283  if (strcmp(resp, _(PG_NOLETTER)) == 0)
284  return false;
285 
286  printf(_("Please answer \"%s\" or \"%s\".\n"),
288  }
289 }
#define PG_YESLETTER
Definition: common.c:260
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_NOLETTER
Definition: common.c:262
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
#define _(x)
Definition: elog.c:84

Variable Documentation

PGcancel* volatile cancelConn = NULL
static

Definition at line 23 of file common.c.

Referenced by ResetCancelConn(), and SetCancelConn().

bool CancelRequested = false

Definition at line 24 of file common.c.

Referenced by handle_sigint(), select_loop(), and vacuum_one_database().