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

Go to the source code of this file.

Macros

#define DEFAULT_CONNECT_TIMEOUT   "3"
 
#define PARAMS_ARRAY_SIZE   7
 

Functions

static void help (const char *progname)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

#define DEFAULT_CONNECT_TIMEOUT   "3"

Definition at line 15 of file pg_isready.c.

Referenced by help(), and main().

#define PARAMS_ARRAY_SIZE   7

Referenced by main().

Function Documentation

static void help ( const char *  progname)
static

Definition at line 219 of file pg_isready.c.

References _, and DEFAULT_CONNECT_TIMEOUT.

Referenced by main().

220 {
221  printf(_("%s issues a connection check to a PostgreSQL database.\n\n"), progname);
222  printf(_("Usage:\n"));
223  printf(_(" %s [OPTION]...\n"), progname);
224 
225  printf(_("\nOptions:\n"));
226  printf(_(" -d, --dbname=DBNAME database name\n"));
227  printf(_(" -q, --quiet run quietly\n"));
228  printf(_(" -V, --version output version information, then exit\n"));
229  printf(_(" -?, --help show this help, then exit\n"));
230 
231  printf(_("\nConnection options:\n"));
232  printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
233  printf(_(" -p, --port=PORT database server port\n"));
234  printf(_(" -t, --timeout=SECS seconds to wait when attempting connection, 0 disables (default: %s)\n"), DEFAULT_CONNECT_TIMEOUT);
235  printf(_(" -U, --username=USERNAME user name to connect as\n"));
236  printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
237 }
const char * progname
Definition: pg_standby.c:37
#define DEFAULT_CONNECT_TIMEOUT
Definition: pg_isready.c:15
#define _(x)
Definition: elog.c:84
int main ( int  argc,
char **  argv 
)

Definition at line 21 of file pg_isready.c.

References _, DEFAULT_CONNECT_TIMEOUT, DEFAULT_PGSOCKET_DIR, errmsg(), get_progname(), getopt_long(), handle_help_version_opts(), help(), _PQconninfoOption::keyword, no_argument, NULL, optarg, optind, PARAMS_ARRAY_SIZE, pg_strdup(), PG_TEXTDOMAIN, pghost, pgport, PQconndefaults(), PQconninfoParse(), PQPING_NO_ATTEMPT, PQPING_NO_RESPONSE, PQPING_OK, PQPING_REJECT, PQpingParams(), progname, required_argument, set_pglocale_pgservice(), _PQconninfoOption::val, and values.

22 {
23  int c;
24 
25  const char *progname;
26 
27  const char *pghost = NULL;
28  const char *pgport = NULL;
29  const char *pguser = NULL;
30  const char *pgdbname = NULL;
31  const char *connect_timeout = DEFAULT_CONNECT_TIMEOUT;
32 
33  const char *pghost_str = NULL;
34  const char *pghostaddr_str = NULL;
35  const char *pgport_str = NULL;
36 
37 #define PARAMS_ARRAY_SIZE 7
38 
39  const char *keywords[PARAMS_ARRAY_SIZE];
40  const char *values[PARAMS_ARRAY_SIZE];
41 
42  bool quiet = false;
43 
44  PGPing rv;
45  PQconninfoOption *opts = NULL;
46  PQconninfoOption *defs = NULL;
47  PQconninfoOption *opt;
48  PQconninfoOption *def;
49  char *errmsg = NULL;
50 
51  /*
52  * We accept user and database as options to avoid useless errors from
53  * connecting with invalid params
54  */
55 
56  static struct option long_options[] = {
57  {"dbname", required_argument, NULL, 'd'},
58  {"host", required_argument, NULL, 'h'},
59  {"port", required_argument, NULL, 'p'},
60  {"quiet", no_argument, NULL, 'q'},
61  {"timeout", required_argument, NULL, 't'},
62  {"username", required_argument, NULL, 'U'},
63  {NULL, 0, NULL, 0}
64  };
65 
66  progname = get_progname(argv[0]);
67  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pgscripts"));
68  handle_help_version_opts(argc, argv, progname, help);
69 
70  while ((c = getopt_long(argc, argv, "d:h:p:qt:U:", long_options, NULL)) != -1)
71  {
72  switch (c)
73  {
74  case 'd':
75  pgdbname = pg_strdup(optarg);
76  break;
77  case 'h':
78  pghost = pg_strdup(optarg);
79  break;
80  case 'p':
81  pgport = pg_strdup(optarg);
82  break;
83  case 'q':
84  quiet = true;
85  break;
86  case 't':
87  connect_timeout = pg_strdup(optarg);
88  break;
89  case 'U':
90  pguser = pg_strdup(optarg);
91  break;
92  default:
93  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
94 
95  /*
96  * We need to make sure we don't return 1 here because someone
97  * checking the return code might infer unintended meaning
98  */
99  exit(PQPING_NO_ATTEMPT);
100  }
101  }
102 
103  if (optind < argc)
104  {
105  fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
106  progname, argv[optind]);
107  fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
108 
109  /*
110  * We need to make sure we don't return 1 here because someone
111  * checking the return code might infer unintended meaning
112  */
113  exit(PQPING_NO_ATTEMPT);
114  }
115 
116  keywords[0] = "host";
117  values[0] = pghost;
118  keywords[1] = "port";
119  values[1] = pgport;
120  keywords[2] = "user";
121  values[2] = pguser;
122  keywords[3] = "dbname";
123  values[3] = pgdbname;
124  keywords[4] = "connect_timeout";
125  values[4] = connect_timeout;
126  keywords[5] = "fallback_application_name";
127  values[5] = progname;
128  keywords[6] = NULL;
129  values[6] = NULL;
130 
131  /*
132  * Get the host and port so we can display them in our output
133  */
134  if (pgdbname &&
135  (strncmp(pgdbname, "postgresql://", 13) == 0 ||
136  strncmp(pgdbname, "postgres://", 11) == 0 ||
137  strchr(pgdbname, '=') != NULL))
138  {
139  opts = PQconninfoParse(pgdbname, &errmsg);
140  if (opts == NULL)
141  {
142  fprintf(stderr, _("%s: %s"), progname, errmsg);
143  exit(PQPING_NO_ATTEMPT);
144  }
145  }
146 
147  defs = PQconndefaults();
148  if (defs == NULL)
149  {
150  fprintf(stderr, _("%s: could not fetch default options\n"), progname);
151  exit(PQPING_NO_ATTEMPT);
152  }
153 
154  for (opt = opts, def = defs; def->keyword; def++)
155  {
156  if (strcmp(def->keyword, "host") == 0)
157  {
158  if (opt && opt->val)
159  pghost_str = opt->val;
160  else if (pghost)
161  pghost_str = pghost;
162  else if (def->val)
163  pghost_str = def->val;
164  else
165  pghost_str = DEFAULT_PGSOCKET_DIR;
166  }
167  else if (strcmp(def->keyword, "hostaddr") == 0)
168  {
169  if (opt && opt->val)
170  pghostaddr_str = opt->val;
171  else if (def->val)
172  pghostaddr_str = def->val;
173  }
174  else if (strcmp(def->keyword, "port") == 0)
175  {
176  if (opt && opt->val)
177  pgport_str = opt->val;
178  else if (pgport)
179  pgport_str = pgport;
180  else if (def->val)
181  pgport_str = def->val;
182  }
183 
184  if (opt)
185  opt++;
186  }
187 
188  rv = PQpingParams(keywords, values, 1);
189 
190  if (!quiet)
191  {
192  printf("%s:%s - ",
193  pghostaddr_str != NULL ? pghostaddr_str : pghost_str,
194  pgport_str);
195 
196  switch (rv)
197  {
198  case PQPING_OK:
199  printf(_("accepting connections\n"));
200  break;
201  case PQPING_REJECT:
202  printf(_("rejecting connections\n"));
203  break;
204  case PQPING_NO_RESPONSE:
205  printf(_("no response\n"));
206  break;
207  case PQPING_NO_ATTEMPT:
208  printf(_("no attempt\n"));
209  break;
210  default:
211  printf(_("unknown\n"));
212  }
213  }
214 
215  exit(rv);
216 }
PGPing PQpingParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:548
#define PARAMS_ARRAY_SIZE
const char * get_progname(const char *argv0)
Definition: path.c:453
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
static void help(const char *progname)
Definition: pg_isready.c:219
const char * progname
Definition: pg_standby.c:37
#define required_argument
Definition: getopt_long.h:25
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:4717
int optind
Definition: getopt.c:51
char * c
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void handle_help_version_opts(int argc, char *argv[], const char *fixed_progname, help_handler hlp)
Definition: common.c:35
char * pghost
Definition: pgbench.c:180
#define no_argument
Definition: getopt_long.h:24
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1013
#define NULL
Definition: c.h:229
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1224
static Datum values[MAXATTR]
Definition: bootstrap.c:163
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
int errmsg(const char *fmt,...)
Definition: elog.c:797
char * optarg
Definition: getopt.c:53
PGPing
Definition: libpq-fe.h:129
#define DEFAULT_CONNECT_TIMEOUT
Definition: pg_isready.c:15
#define _(x)
Definition: elog.c:84
char * pgport
Definition: pgbench.c:181
#define DEFAULT_PGSOCKET_DIR