PostgreSQL Source Code  git master
option.c File Reference
#include "postgres_fe.h"
#include "common/string.h"
#include "getopt_long.h"
#include "pg_upgrade.h"
#include "utils/pidfile.h"
Include dependency graph for option.c:

Go to the source code of this file.

Macros

#define FIX_DEFAULT_READ_ONLY   "-c default_transaction_read_only=false"
 

Functions

static void usage (void)
 
static void check_required_directory (char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
 
void parseCommandLine (int argc, char *argv[])
 
void adjust_data_dir (ClusterInfo *cluster)
 
void get_sock_dir (ClusterInfo *cluster, bool live_check)
 

Variables

UserOpts user_opts
 

Macro Definition Documentation

◆ FIX_DEFAULT_READ_ONLY

#define FIX_DEFAULT_READ_ONLY   "-c default_transaction_read_only=false"

Definition at line 26 of file option.c.

Function Documentation

◆ adjust_data_dir()

void adjust_data_dir ( ClusterInfo cluster)

Definition at line 386 of file option.c.

387 {
388  char filename[MAXPGPATH];
389  char cmd[MAXPGPATH],
390  cmd_output[MAX_STRING];
391  FILE *fp,
392  *output;
393  int rc;
394 
395  /* Initially assume config dir and data dir are the same */
396  cluster->pgconfig = pg_strdup(cluster->pgdata);
397 
398  /* If there is no postgresql.conf, it can't be a config-only dir */
399  snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
400  if ((fp = fopen(filename, "r")) == NULL)
401  return;
402  fclose(fp);
403 
404  /* If PG_VERSION exists, it can't be a config-only dir */
405  snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
406  if ((fp = fopen(filename, "r")) != NULL)
407  {
408  fclose(fp);
409  return;
410  }
411 
412  /* Must be a configuration directory, so find the real data directory. */
413 
414  if (cluster == &old_cluster)
415  prep_status("Finding the real data directory for the source cluster");
416  else
417  prep_status("Finding the real data directory for the target cluster");
418 
419  /*
420  * We don't have a data directory yet, so we can't check the PG version,
421  * so this might fail --- only works for PG 9.2+. If this fails,
422  * pg_upgrade will fail anyway because the data files will not be found.
423  */
424  snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C data_directory",
425  cluster->bindir, cluster->pgconfig);
426  fflush(NULL);
427 
428  if ((output = popen(cmd, "r")) == NULL ||
429  fgets(cmd_output, sizeof(cmd_output), output) == NULL)
430  pg_fatal("could not get data directory using %s: %s",
431  cmd, strerror(errno));
432 
433  rc = pclose(output);
434  if (rc != 0)
435  pg_fatal("could not get control data directory using %s: %s",
436  cmd, wait_result_to_str(rc));
437 
438  /* strip trailing newline and carriage return */
439  (void) pg_strip_crlf(cmd_output);
440 
441  cluster->pgdata = pg_strdup(cmd_output);
442 
443  check_ok();
444 }
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:111
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void check_ok(void)
Definition: initdb.c:1823
static void const char fflush(stdout)
#define pg_fatal(...)
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
static void output(uint64 loop_count)
ClusterInfo old_cluster
Definition: pg_upgrade.c:62
#define MAX_STRING
Definition: pg_upgrade.h:22
void prep_status(const char *fmt,...) pg_attribute_printf(1
#define strerror
Definition: port.h:251
#define snprintf
Definition: port.h:238
int pg_strip_crlf(char *str)
Definition: string.c:155
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:33

References check_ok(), cluster(), fflush(), filename, MAX_STRING, MAXPGPATH, old_cluster, output(), pg_fatal, pg_strdup(), pg_strip_crlf(), prep_status(), snprintf, strerror, and wait_result_to_str().

◆ check_required_directory()

static void check_required_directory ( char **  dirpath,
const char *  envVarName,
bool  useCwd,
const char *  cmdLineOption,
const char *  description,
bool  missingOk 
)
static

Definition at line 341 of file option.c.

344 {
345  if (*dirpath == NULL || strlen(*dirpath) == 0)
346  {
347  const char *envVar;
348 
349  if ((envVar = getenv(envVarName)) && strlen(envVar))
350  *dirpath = pg_strdup(envVar);
351  else if (useCwd)
352  {
353  char cwd[MAXPGPATH];
354 
355  if (!getcwd(cwd, MAXPGPATH))
356  pg_fatal("could not determine current directory");
357  *dirpath = pg_strdup(cwd);
358  }
359  else if (missingOk)
360  return;
361  else
362  pg_fatal("You must identify the directory where the %s.\n"
363  "Please use the %s command-line option or the %s environment variable.",
364  description, cmdLineOption, envVarName);
365  }
366 
367  /*
368  * Clean up the path, in particular trimming any trailing path separators,
369  * because we construct paths by appending to this path.
370  */
371  canonicalize_path(*dirpath);
372 }
void canonicalize_path(char *path)
Definition: path.c:264

References canonicalize_path(), MAXPGPATH, pg_fatal, and pg_strdup().

Referenced by parseCommandLine().

◆ get_sock_dir()

void get_sock_dir ( ClusterInfo cluster,
bool  live_check 
)

Definition at line 456 of file option.c.

457 {
458 #if !defined(WIN32)
459  if (!live_check)
460  cluster->sockdir = user_opts.socketdir;
461  else
462  {
463  /*
464  * If we are doing a live check, we will use the old cluster's Unix
465  * domain socket directory so we can connect to the live server.
466  */
467  unsigned short orig_port = cluster->port;
468  char filename[MAXPGPATH],
469  line[MAXPGPATH];
470  FILE *fp;
471  int lineno;
472 
473  snprintf(filename, sizeof(filename), "%s/postmaster.pid",
474  cluster->pgdata);
475  if ((fp = fopen(filename, "r")) == NULL)
476  pg_fatal("could not open file \"%s\": %s",
477  filename, strerror(errno));
478 
479  for (lineno = 1;
481  lineno++)
482  {
483  if (fgets(line, sizeof(line), fp) == NULL)
484  pg_fatal("could not read line %d from file \"%s\": %s",
485  lineno, filename, strerror(errno));
486 
487  /* potentially overwrite user-supplied value */
488  if (lineno == LOCK_FILE_LINE_PORT)
489  sscanf(line, "%hu", &old_cluster.port);
490  if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
491  {
492  /* strip trailing newline and carriage return */
493  cluster->sockdir = pg_strdup(line);
494  (void) pg_strip_crlf(cluster->sockdir);
495  }
496  }
497  fclose(fp);
498 
499  /* warn of port number correction */
500  if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
501  pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu",
502  orig_port, cluster->port);
503  }
504 #else /* WIN32 */
505  cluster->sockdir = NULL;
506 #endif
507 }
#define Max(x, y)
Definition: c.h:982
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
#define DEF_PGUPORT
Definition: pg_upgrade.h:20
@ PG_WARNING
Definition: pg_upgrade.h:242
#define LOCK_FILE_LINE_PORT
Definition: pidfile.h:40
#define LOCK_FILE_LINE_SOCKET_DIR
Definition: pidfile.h:41
UserOpts user_opts
Definition: option.c:29
unsigned short port
Definition: pg_upgrade.h:263
char * socketdir
Definition: pg_upgrade.h:298

References cluster(), DEF_PGUPORT, filename, LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR, Max, MAXPGPATH, old_cluster, pg_fatal, pg_log(), pg_strdup(), pg_strip_crlf(), PG_WARNING, ClusterInfo::port, snprintf, UserOpts::socketdir, strerror, and user_opts.

Referenced by main().

◆ parseCommandLine()

void parseCommandLine ( int  argc,
char *  argv[] 
)

Definition at line 38 of file option.c.

39 {
40  static struct option long_options[] = {
41  {"old-datadir", required_argument, NULL, 'd'},
42  {"new-datadir", required_argument, NULL, 'D'},
43  {"old-bindir", required_argument, NULL, 'b'},
44  {"new-bindir", required_argument, NULL, 'B'},
45  {"no-sync", no_argument, NULL, 'N'},
46  {"old-options", required_argument, NULL, 'o'},
47  {"new-options", required_argument, NULL, 'O'},
48  {"old-port", required_argument, NULL, 'p'},
49  {"new-port", required_argument, NULL, 'P'},
50 
51  {"username", required_argument, NULL, 'U'},
52  {"check", no_argument, NULL, 'c'},
53  {"link", no_argument, NULL, 'k'},
54  {"retain", no_argument, NULL, 'r'},
55  {"jobs", required_argument, NULL, 'j'},
56  {"socketdir", required_argument, NULL, 's'},
57  {"verbose", no_argument, NULL, 'v'},
58  {"clone", no_argument, NULL, 1},
59  {"copy", no_argument, NULL, 2},
60 
61  {NULL, 0, NULL, 0}
62  };
63  int option; /* Command line option */
64  int optindex = 0; /* used by getopt_long */
65  int os_user_effective_id;
66 
67  user_opts.do_sync = true;
69 
70  os_info.progname = get_progname(argv[0]);
71 
72  /* Process libpq env. variables; load values here for usage() output */
73  old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
74  new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
75 
76  os_user_effective_id = get_user_info(&os_info.user);
77  /* we override just the database user name; we got the OS id above */
78  if (getenv("PGUSER"))
79  {
81  /* must save value, getenv()'s pointer is not stable */
82  os_info.user = pg_strdup(getenv("PGUSER"));
83  }
84 
85  if (argc > 1)
86  {
87  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
88  {
89  usage();
90  exit(0);
91  }
92  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
93  {
94  puts("pg_upgrade (PostgreSQL) " PG_VERSION);
95  exit(0);
96  }
97  }
98 
99  /* Allow help and version to be run as root, so do the test here. */
100  if (os_user_effective_id == 0)
101  pg_fatal("%s: cannot be run as root", os_info.progname);
102 
103  while ((option = getopt_long(argc, argv, "b:B:cd:D:j:kNo:O:p:P:rs:U:v",
104  long_options, &optindex)) != -1)
105  {
106  switch (option)
107  {
108  case 'b':
110  break;
111 
112  case 'B':
114  break;
115 
116  case 'c':
117  user_opts.check = true;
118  break;
119 
120  case 'd':
122  break;
123 
124  case 'D':
126  break;
127 
128  case 'j':
129  user_opts.jobs = atoi(optarg);
130  break;
131 
132  case 'k':
134  break;
135 
136  case 'N':
137  user_opts.do_sync = false;
138  break;
139 
140  case 'o':
141  /* append option? */
142  if (!old_cluster.pgopts)
144  else
145  {
146  char *old_pgopts = old_cluster.pgopts;
147 
148  old_cluster.pgopts = psprintf("%s %s", old_pgopts, optarg);
149  free(old_pgopts);
150  }
151  break;
152 
153  case 'O':
154  /* append option? */
155  if (!new_cluster.pgopts)
157  else
158  {
159  char *new_pgopts = new_cluster.pgopts;
160 
161  new_cluster.pgopts = psprintf("%s %s", new_pgopts, optarg);
162  free(new_pgopts);
163  }
164  break;
165 
166  case 'p':
167  if ((old_cluster.port = atoi(optarg)) <= 0)
168  pg_fatal("invalid old port number");
169  break;
170 
171  case 'P':
172  if ((new_cluster.port = atoi(optarg)) <= 0)
173  pg_fatal("invalid new port number");
174  break;
175 
176  case 'r':
177  log_opts.retain = true;
178  break;
179 
180  case 's':
182  break;
183 
184  case 'U':
187  os_info.user_specified = true;
188  break;
189 
190  case 'v':
191  log_opts.verbose = true;
192  break;
193 
194  case 1:
196  break;
197 
198  case 2:
200  break;
201 
202  default:
203  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
204  os_info.progname);
205  exit(1);
206  }
207  }
208 
209  if (optind < argc)
210  pg_fatal("too many command-line arguments (first is \"%s\")", argv[optind]);
211 
212  if (log_opts.verbose)
213  pg_log(PG_REPORT, "Running in verbose mode");
214 
215  log_opts.isatty = isatty(fileno(stdout));
216 
217  /* Turn off read-only mode; add prefix to PGOPTIONS? */
218  if (getenv("PGOPTIONS"))
219  {
220  char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY,
221  getenv("PGOPTIONS"));
222 
223  setenv("PGOPTIONS", pgoptions, 1);
224  pfree(pgoptions);
225  }
226  else
227  setenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY, 1);
228 
229  /* Get values from env if not already set */
230  check_required_directory(&old_cluster.bindir, "PGBINOLD", false,
231  "-b", _("old cluster binaries reside"), false);
232  check_required_directory(&new_cluster.bindir, "PGBINNEW", false,
233  "-B", _("new cluster binaries reside"), true);
234  check_required_directory(&old_cluster.pgdata, "PGDATAOLD", false,
235  "-d", _("old cluster data resides"), false);
236  check_required_directory(&new_cluster.pgdata, "PGDATANEW", false,
237  "-D", _("new cluster data resides"), false);
238  check_required_directory(&user_opts.socketdir, "PGSOCKETDIR", true,
239  "-s", _("sockets will be created"), false);
240 
241 #ifdef WIN32
242 
243  /*
244  * On Windows, initdb --sync-only will fail with a "Permission denied"
245  * error on file pg_upgrade_utility.log if pg_upgrade is run inside the
246  * new cluster directory, so we do a check here.
247  */
248  {
249  char cwd[MAXPGPATH],
250  new_cluster_pgdata[MAXPGPATH];
251 
252  strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
253  canonicalize_path(new_cluster_pgdata);
254 
255  if (!getcwd(cwd, MAXPGPATH))
256  pg_fatal("could not determine current directory");
257  canonicalize_path(cwd);
258  if (path_is_prefix_of_path(new_cluster_pgdata, cwd))
259  pg_fatal("cannot run pg_upgrade from inside the new cluster data directory on Windows");
260  }
261 #endif
262 }
#define _(x)
Definition: elog.c:91
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
#define free(a)
Definition: header.h:65
exit(1)
void pfree(void *pointer)
Definition: mcxt.c:1436
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
OSInfo os_info
Definition: pg_upgrade.c:64
ClusterInfo new_cluster
Definition: pg_upgrade.c:63
int get_user_info(char **user_name_p)
Definition: util.c:323
@ TRANSFER_MODE_COPY
Definition: pg_upgrade.h:229
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:230
@ TRANSFER_MODE_CLONE
Definition: pg_upgrade.h:228
LogOpts log_opts
Definition: util.c:17
@ PG_REPORT
Definition: pg_upgrade.h:241
bool path_is_prefix_of_path(const char *path1, const char *path2)
Definition: path.c:559
const char * get_progname(const char *argv0)
Definition: path.c:574
#define fprintf
Definition: port.h:242
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define FIX_DEFAULT_READ_ONLY
Definition: option.c:26
static void usage(void)
Definition: option.c:266
static void check_required_directory(char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
Definition: option.c:341
char * pgdata
Definition: pg_upgrade.h:256
char * bindir
Definition: pg_upgrade.h:259
char * pgopts
Definition: pg_upgrade.h:260
bool retain
Definition: pg_upgrade.h:278
bool isatty
Definition: pg_upgrade.h:284
bool verbose
Definition: pg_upgrade.h:277
char * user
Definition: pg_upgrade.h:313
const char * progname
Definition: pg_upgrade.h:312
bool user_specified
Definition: pg_upgrade.h:314
bool do_sync
Definition: pg_upgrade.h:295
transferMode transfer_mode
Definition: pg_upgrade.h:296
bool check
Definition: pg_upgrade.h:293
int jobs
Definition: pg_upgrade.h:297
#define setenv(x, y, z)
Definition: win32_port.h:534

References _, ClusterInfo::bindir, canonicalize_path(), UserOpts::check, check_required_directory(), DEF_PGUPORT, UserOpts::do_sync, exit(), FIX_DEFAULT_READ_ONLY, fprintf, free, get_progname(), get_user_info(), getopt_long(), LogOpts::isatty, UserOpts::jobs, log_opts, MAXPGPATH, new_cluster, no_argument, old_cluster, optarg, optind, os_info, path_is_prefix_of_path(), pfree(), pg_fatal, pg_free(), pg_log(), PG_REPORT, pg_strdup(), ClusterInfo::pgdata, ClusterInfo::pgopts, ClusterInfo::port, OSInfo::progname, psprintf(), required_argument, LogOpts::retain, setenv, UserOpts::socketdir, generate_unaccent_rules::stdout, strlcpy(), UserOpts::transfer_mode, TRANSFER_MODE_CLONE, TRANSFER_MODE_COPY, TRANSFER_MODE_LINK, usage(), OSInfo::user, user_opts, OSInfo::user_specified, and LogOpts::verbose.

Referenced by main().

◆ usage()

static void usage ( void  )
static

Definition at line 266 of file option.c.

267 {
268  printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
269  printf(_("Usage:\n"));
270  printf(_(" pg_upgrade [OPTION]...\n\n"));
271  printf(_("Options:\n"));
272  printf(_(" -b, --old-bindir=BINDIR old cluster executable directory\n"));
273  printf(_(" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
274  " same directory as pg_upgrade)\n"));
275  printf(_(" -c, --check check clusters only, don't change any data\n"));
276  printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n"));
277  printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n"));
278  printf(_(" -j, --jobs=NUM number of simultaneous processes or threads to use\n"));
279  printf(_(" -k, --link link instead of copying files to new cluster\n"));
280  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
281  printf(_(" -o, --old-options=OPTIONS old cluster options to pass to the server\n"));
282  printf(_(" -O, --new-options=OPTIONS new cluster options to pass to the server\n"));
283  printf(_(" -p, --old-port=PORT old cluster port number (default %d)\n"), old_cluster.port);
284  printf(_(" -P, --new-port=PORT new cluster port number (default %d)\n"), new_cluster.port);
285  printf(_(" -r, --retain retain SQL and log files after success\n"));
286  printf(_(" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
287  printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user);
288  printf(_(" -v, --verbose enable verbose internal logging\n"));
289  printf(_(" -V, --version display version information, then exit\n"));
290  printf(_(" --clone clone instead of copying files to new cluster\n"));
291  printf(_(" --copy copy files to new cluster (default)\n"));
292  printf(_(" -?, --help show this help, then exit\n"));
293  printf(_("\n"
294  "Before running pg_upgrade you must:\n"
295  " create a new database cluster (using the new version of initdb)\n"
296  " shutdown the postmaster servicing the old cluster\n"
297  " shutdown the postmaster servicing the new cluster\n"));
298  printf(_("\n"
299  "When you run pg_upgrade, you must provide the following information:\n"
300  " the data directory for the old cluster (-d DATADIR)\n"
301  " the data directory for the new cluster (-D DATADIR)\n"
302  " the \"bin\" directory for the old version (-b BINDIR)\n"
303  " the \"bin\" directory for the new version (-B BINDIR)\n"));
304  printf(_("\n"
305  "For example:\n"
306  " pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n"
307  "or\n"));
308 #ifndef WIN32
309  printf(_(" $ export PGDATAOLD=oldCluster/data\n"
310  " $ export PGDATANEW=newCluster/data\n"
311  " $ export PGBINOLD=oldCluster/bin\n"
312  " $ export PGBINNEW=newCluster/bin\n"
313  " $ pg_upgrade\n"));
314 #else
315  printf(_(" C:\\> set PGDATAOLD=oldCluster/data\n"
316  " C:\\> set PGDATANEW=newCluster/data\n"
317  " C:\\> set PGBINOLD=oldCluster/bin\n"
318  " C:\\> set PGBINNEW=newCluster/bin\n"
319  " C:\\> pg_upgrade\n"));
320 #endif
321  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
322  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
323 }
#define printf(...)
Definition: port.h:244

References _, new_cluster, old_cluster, os_info, ClusterInfo::port, printf, and OSInfo::user.

Referenced by parseCommandLine().

Variable Documentation

◆ user_opts