PostgreSQL Source Code  git master
option.c
Go to the documentation of this file.
1 /*
2  * option.c
3  *
4  * options functions
5  *
6  * Copyright (c) 2010-2020, PostgreSQL Global Development Group
7  * src/bin/pg_upgrade/option.c
8  */
9 
10 #include "postgres_fe.h"
11 
12 #include <time.h>
13 #ifdef WIN32
14 #include <io.h>
15 #endif
16 
17 #include "common/string.h"
18 #include "getopt_long.h"
19 #include "pg_upgrade.h"
20 #include "utils/pidfile.h"
21 
22 static void usage(void);
23 static void check_required_directory(char **dirpath,
24  const char *envVarName, bool useCwd,
25  const char *cmdLineOption, const char *description,
26  bool missingOk);
27 #define FIX_DEFAULT_READ_ONLY "-c default_transaction_read_only=false"
28 
29 
31 
32 
33 /*
34  * parseCommandLine()
35  *
36  * Parses the command line (argc, argv[]) and loads structures
37  */
38 void
39 parseCommandLine(int argc, char *argv[])
40 {
41  static struct option long_options[] = {
42  {"old-datadir", required_argument, NULL, 'd'},
43  {"new-datadir", required_argument, NULL, 'D'},
44  {"old-bindir", required_argument, NULL, 'b'},
45  {"new-bindir", required_argument, NULL, 'B'},
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 
60  {NULL, 0, NULL, 0}
61  };
62  int option; /* Command line option */
63  int optindex = 0; /* used by getopt_long */
64  int os_user_effective_id;
65  FILE *fp;
66  char **filename;
67  time_t run_time = time(NULL);
68 
70 
71  os_info.progname = get_progname(argv[0]);
72 
73  /* Process libpq env. variables; load values here for usage() output */
74  old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
75  new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
76 
77  os_user_effective_id = get_user_info(&os_info.user);
78  /* we override just the database user name; we got the OS id above */
79  if (getenv("PGUSER"))
80  {
82  /* must save value, getenv()'s pointer is not stable */
83  os_info.user = pg_strdup(getenv("PGUSER"));
84  }
85 
86  if (argc > 1)
87  {
88  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
89  {
90  usage();
91  exit(0);
92  }
93  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
94  {
95  puts("pg_upgrade (PostgreSQL) " PG_VERSION);
96  exit(0);
97  }
98  }
99 
100  /* Allow help and version to be run as root, so do the test here. */
101  if (os_user_effective_id == 0)
102  pg_fatal("%s: cannot be run as root\n", os_info.progname);
103 
104  while ((option = getopt_long(argc, argv, "d:D:b:B:cj:ko:O:p:P:rs:U:v",
105  long_options, &optindex)) != -1)
106  {
107  switch (option)
108  {
109  case 'b':
111  break;
112 
113  case 'B':
115  break;
116 
117  case 'c':
118  user_opts.check = true;
119  break;
120 
121  case 'd':
123  break;
124 
125  case 'D':
127  break;
128 
129  case 'j':
130  user_opts.jobs = atoi(optarg);
131  break;
132 
133  case 'k':
134  user_opts.transfer_mode = TRANSFER_MODE_LINK;
135  break;
136 
137  case 'o':
138  /* append option? */
139  if (!old_cluster.pgopts)
141  else
142  {
143  char *old_pgopts = old_cluster.pgopts;
144 
145  old_cluster.pgopts = psprintf("%s %s", old_pgopts, optarg);
146  free(old_pgopts);
147  }
148  break;
149 
150  case 'O':
151  /* append option? */
152  if (!new_cluster.pgopts)
154  else
155  {
156  char *new_pgopts = new_cluster.pgopts;
157 
158  new_cluster.pgopts = psprintf("%s %s", new_pgopts, optarg);
159  free(new_pgopts);
160  }
161  break;
162 
163  /*
164  * Someday, the port number option could be removed and passed
165  * using -o/-O, but that requires postmaster -C to be
166  * supported on all old/new versions (added in PG 9.2).
167  */
168  case 'p':
169  if ((old_cluster.port = atoi(optarg)) <= 0)
170  pg_fatal("invalid old port number\n");
171  break;
172 
173  case 'P':
174  if ((new_cluster.port = atoi(optarg)) <= 0)
175  pg_fatal("invalid new port number\n");
176  break;
177 
178  case 'r':
179  log_opts.retain = true;
180  break;
181 
182  case 's':
183  user_opts.socketdir = pg_strdup(optarg);
184  break;
185 
186  case 'U':
189  os_info.user_specified = true;
190 
191  /*
192  * Push the user name into the environment so pre-9.1
193  * pg_ctl/libpq uses it.
194  */
195  pg_putenv("PGUSER", os_info.user);
196  break;
197 
198  case 'v':
199  log_opts.verbose = true;
200  break;
201 
202  case 1:
204  break;
205 
206  default:
207  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
208  os_info.progname);
209  exit(1);
210  }
211  }
212 
213  if (optind < argc)
214  pg_fatal("too many command-line arguments (first is \"%s\")\n", argv[optind]);
215 
216  if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
217  pg_fatal("could not open log file \"%s\": %m\n", INTERNAL_LOG_FILE);
218 
219  if (log_opts.verbose)
220  pg_log(PG_REPORT, "Running in verbose mode\n");
221 
222  /* label start of upgrade in logfiles */
223  for (filename = output_files; *filename != NULL; filename++)
224  {
225  if ((fp = fopen_priv(*filename, "a")) == NULL)
226  pg_fatal("could not write to log file \"%s\": %m\n", *filename);
227 
228  /* Start with newline because we might be appending to a file. */
229  fprintf(fp, "\n"
230  "-----------------------------------------------------------------\n"
231  " pg_upgrade run on %s"
232  "-----------------------------------------------------------------\n\n",
233  ctime(&run_time));
234  fclose(fp);
235  }
236 
237  /* Turn off read-only mode; add prefix to PGOPTIONS? */
238  if (getenv("PGOPTIONS"))
239  {
240  char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY,
241  getenv("PGOPTIONS"));
242 
243  pg_putenv("PGOPTIONS", pgoptions);
244  pfree(pgoptions);
245  }
246  else
247  pg_putenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY);
248 
249  /* Get values from env if not already set */
250  check_required_directory(&old_cluster.bindir, "PGBINOLD", false,
251  "-b", _("old cluster binaries reside"), false);
252  check_required_directory(&new_cluster.bindir, "PGBINNEW", false,
253  "-B", _("new cluster binaries reside"), true);
254  check_required_directory(&old_cluster.pgdata, "PGDATAOLD", false,
255  "-d", _("old cluster data resides"), false);
256  check_required_directory(&new_cluster.pgdata, "PGDATANEW", false,
257  "-D", _("new cluster data resides"), false);
258  check_required_directory(&user_opts.socketdir, "PGSOCKETDIR", true,
259  "-s", _("sockets will be created"), false);
260 
261 #ifdef WIN32
262 
263  /*
264  * On Windows, initdb --sync-only will fail with a "Permission denied"
265  * error on file pg_upgrade_utility.log if pg_upgrade is run inside the
266  * new cluster directory, so we do a check here.
267  */
268  {
269  char cwd[MAXPGPATH],
270  new_cluster_pgdata[MAXPGPATH];
271 
272  strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
273  canonicalize_path(new_cluster_pgdata);
274 
275  if (!getcwd(cwd, MAXPGPATH))
276  pg_fatal("could not determine current directory\n");
277  canonicalize_path(cwd);
278  if (path_is_prefix_of_path(new_cluster_pgdata, cwd))
279  pg_fatal("cannot run pg_upgrade from inside the new cluster data directory on Windows\n");
280  }
281 #endif
282 }
283 
284 
285 static void
286 usage(void)
287 {
288  printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
289  printf(_("Usage:\n"));
290  printf(_(" pg_upgrade [OPTION]...\n\n"));
291  printf(_("Options:\n"));
292  printf(_(" -b, --old-bindir=BINDIR old cluster executable directory\n"));
293  printf(_(" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
294  " same directory as pg_upgrade)\n"));
295  printf(_(" -c, --check check clusters only, don't change any data\n"));
296  printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n"));
297  printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n"));
298  printf(_(" -j, --jobs=NUM number of simultaneous processes or threads to use\n"));
299  printf(_(" -k, --link link instead of copying files to new cluster\n"));
300  printf(_(" -o, --old-options=OPTIONS old cluster options to pass to the server\n"));
301  printf(_(" -O, --new-options=OPTIONS new cluster options to pass to the server\n"));
302  printf(_(" -p, --old-port=PORT old cluster port number (default %d)\n"), old_cluster.port);
303  printf(_(" -P, --new-port=PORT new cluster port number (default %d)\n"), new_cluster.port);
304  printf(_(" -r, --retain retain SQL and log files after success\n"));
305  printf(_(" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
306  printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user);
307  printf(_(" -v, --verbose enable verbose internal logging\n"));
308  printf(_(" -V, --version display version information, then exit\n"));
309  printf(_(" --clone clone instead of copying files to new cluster\n"));
310  printf(_(" -?, --help show this help, then exit\n"));
311  printf(_("\n"
312  "Before running pg_upgrade you must:\n"
313  " create a new database cluster (using the new version of initdb)\n"
314  " shutdown the postmaster servicing the old cluster\n"
315  " shutdown the postmaster servicing the new cluster\n"));
316  printf(_("\n"
317  "When you run pg_upgrade, you must provide the following information:\n"
318  " the data directory for the old cluster (-d DATADIR)\n"
319  " the data directory for the new cluster (-D DATADIR)\n"
320  " the \"bin\" directory for the old version (-b BINDIR)\n"
321  " the \"bin\" directory for the new version (-B BINDIR)\n"));
322  printf(_("\n"
323  "For example:\n"
324  " pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n"
325  "or\n"));
326 #ifndef WIN32
327  printf(_(" $ export PGDATAOLD=oldCluster/data\n"
328  " $ export PGDATANEW=newCluster/data\n"
329  " $ export PGBINOLD=oldCluster/bin\n"
330  " $ export PGBINNEW=newCluster/bin\n"
331  " $ pg_upgrade\n"));
332 #else
333  printf(_(" C:\\> set PGDATAOLD=oldCluster/data\n"
334  " C:\\> set PGDATANEW=newCluster/data\n"
335  " C:\\> set PGBINOLD=oldCluster/bin\n"
336  " C:\\> set PGBINNEW=newCluster/bin\n"
337  " C:\\> pg_upgrade\n"));
338 #endif
339  printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
340  printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
341 }
342 
343 
344 /*
345  * check_required_directory()
346  *
347  * Checks a directory option.
348  * dirpath - the directory name supplied on the command line, or NULL
349  * envVarName - the name of an environment variable to get if dirpath is NULL
350  * useCwd - true if OK to default to CWD
351  * cmdLineOption - the command line option for this directory
352  * description - a description of this directory option
353  * missingOk - true if OK that both dirpath and envVarName are not existing
354  *
355  * We use the last two arguments to construct a meaningful error message if the
356  * user hasn't provided the required directory name.
357  */
358 static void
359 check_required_directory(char **dirpath, const char *envVarName, bool useCwd,
360  const char *cmdLineOption, const char *description,
361  bool missingOk)
362 {
363  if (*dirpath == NULL || strlen(*dirpath) == 0)
364  {
365  const char *envVar;
366 
367  if ((envVar = getenv(envVarName)) && strlen(envVar))
368  *dirpath = pg_strdup(envVar);
369  else if (useCwd)
370  {
371  char cwd[MAXPGPATH];
372 
373  if (!getcwd(cwd, MAXPGPATH))
374  pg_fatal("could not determine current directory\n");
375  *dirpath = pg_strdup(cwd);
376  }
377  else if (missingOk)
378  return;
379  else
380  pg_fatal("You must identify the directory where the %s.\n"
381  "Please use the %s command-line option or the %s environment variable.\n",
382  description, cmdLineOption, envVarName);
383  }
384 
385  /*
386  * Clean up the path, in particular trimming any trailing path separators,
387  * because we construct paths by appending to this path.
388  */
389  canonicalize_path(*dirpath);
390 }
391 
392 /*
393  * adjust_data_dir
394  *
395  * If a configuration-only directory was specified, find the real data dir
396  * by querying the running server. This has limited checking because we
397  * can't check for a running server because we can't find postmaster.pid.
398  *
399  * On entry, cluster->pgdata has been set from command line or env variable,
400  * but cluster->pgconfig isn't set. We fill both variables with corrected
401  * values.
402  */
403 void
405 {
406  char filename[MAXPGPATH];
407  char cmd[MAXPGPATH],
408  cmd_output[MAX_STRING];
409  FILE *fp,
410  *output;
411 
412  /* Initially assume config dir and data dir are the same */
413  cluster->pgconfig = pg_strdup(cluster->pgdata);
414 
415  /* If there is no postgresql.conf, it can't be a config-only dir */
416  snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
417  if ((fp = fopen(filename, "r")) == NULL)
418  return;
419  fclose(fp);
420 
421  /* If PG_VERSION exists, it can't be a config-only dir */
422  snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
423  if ((fp = fopen(filename, "r")) != NULL)
424  {
425  fclose(fp);
426  return;
427  }
428 
429  /* Must be a configuration directory, so find the real data directory. */
430 
431  if (cluster == &old_cluster)
432  prep_status("Finding the real data directory for the source cluster");
433  else
434  prep_status("Finding the real data directory for the target cluster");
435 
436  /*
437  * We don't have a data directory yet, so we can't check the PG version,
438  * so this might fail --- only works for PG 9.2+. If this fails,
439  * pg_upgrade will fail anyway because the data files will not be found.
440  */
441  snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C data_directory",
442  cluster->bindir, cluster->pgconfig);
443 
444  if ((output = popen(cmd, "r")) == NULL ||
445  fgets(cmd_output, sizeof(cmd_output), output) == NULL)
446  pg_fatal("could not get data directory using %s: %s\n",
447  cmd, strerror(errno));
448 
449  pclose(output);
450 
451  /* strip trailing newline and carriage return */
452  (void) pg_strip_crlf(cmd_output);
453 
454  cluster->pgdata = pg_strdup(cmd_output);
455 
456  check_ok();
457 }
458 
459 
460 /*
461  * get_sock_dir
462  *
463  * Identify the socket directory to use for this cluster. If we're doing
464  * a live check (old cluster only), we need to find out where the postmaster
465  * is listening. Otherwise, we're going to put the socket into the current
466  * directory.
467  */
468 void
469 get_sock_dir(ClusterInfo *cluster, bool live_check)
470 {
471 #if defined(HAVE_UNIX_SOCKETS) && !defined(WIN32)
472 
473  /*
474  * sockdir and port were added to postmaster.pid in PG 9.1. Pre-9.1 cannot
475  * process pg_ctl -w for sockets in non-default locations.
476  */
477  if (GET_MAJOR_VERSION(cluster->major_version) >= 901)
478  {
479  if (!live_check)
480  cluster->sockdir = user_opts.socketdir;
481  else
482  {
483  /*
484  * If we are doing a live check, we will use the old cluster's
485  * Unix domain socket directory so we can connect to the live
486  * server.
487  */
488  unsigned short orig_port = cluster->port;
489  char filename[MAXPGPATH],
490  line[MAXPGPATH];
491  FILE *fp;
492  int lineno;
493 
494  snprintf(filename, sizeof(filename), "%s/postmaster.pid",
495  cluster->pgdata);
496  if ((fp = fopen(filename, "r")) == NULL)
497  pg_fatal("could not open file \"%s\": %s\n",
498  filename, strerror(errno));
499 
500  for (lineno = 1;
502  lineno++)
503  {
504  if (fgets(line, sizeof(line), fp) == NULL)
505  pg_fatal("could not read line %d from file \"%s\": %s\n",
506  lineno, filename, strerror(errno));
507 
508  /* potentially overwrite user-supplied value */
509  if (lineno == LOCK_FILE_LINE_PORT)
510  sscanf(line, "%hu", &old_cluster.port);
511  if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
512  {
513  /* strip trailing newline and carriage return */
514  cluster->sockdir = pg_strdup(line);
515  (void) pg_strip_crlf(cluster->sockdir);
516  }
517  }
518  fclose(fp);
519 
520  /* warn of port number correction */
521  if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
522  pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu\n",
523  orig_port, cluster->port);
524  }
525  }
526  else
527 
528  /*
529  * Can't get sockdir and pg_ctl -w can't use a non-default, use
530  * default
531  */
532  cluster->sockdir = NULL;
533 #else /* !HAVE_UNIX_SOCKETS || WIN32 */
534  cluster->sockdir = NULL;
535 #endif
536 }
void parseCommandLine(int argc, char *argv[])
Definition: option.c:39
uint32 major_version
Definition: pg_upgrade.h:267
int pg_strip_crlf(char *str)
Definition: string.c:105
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:469
const char * get_progname(const char *argv0)
Definition: path.c:453
static void output(uint64 loop_count)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
bool path_is_prefix_of_path(const char *path1, const char *path2)
Definition: path.c:438
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
int get_user_info(char **user_name_p)
Definition: util.c:211
#define FIX_DEFAULT_READ_ONLY
Definition: option.c:27
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:23
void canonicalize_path(char *path)
Definition: path.c:254
#define pg_fatal(...)
Definition: pg_rewind.h:41
unsigned short port
Definition: pg_upgrade.h:266
#define printf(...)
Definition: port.h:221
LogOpts log_opts
Definition: util.c:17
int jobs
Definition: pg_upgrade.h:293
#define fprintf
Definition: port.h:219
bool user_specified
Definition: pg_upgrade.h:310
char * pgopts
Definition: pg_upgrade.h:263
static void check_required_directory(char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
Definition: option.c:359
ClusterInfo new_cluster
Definition: pg_upgrade.c:59
#define required_argument
Definition: getopt_long.h:25
void pfree(void *pointer)
Definition: mcxt.c:1057
char * pgconfig
Definition: pg_upgrade.h:260
int optind
Definition: getopt.c:50
#define LOCK_FILE_LINE_SOCKET_DIR
Definition: pidfile.h:41
#define MAXPGPATH
char * output_files[]
Definition: pg_upgrade.c:63
void prep_status(const char *fmt,...) pg_attribute_printf(1
ClusterInfo old_cluster
Definition: pg_upgrade.c:59
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void cluster(ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:102
void adjust_data_dir(ClusterInfo *cluster)
Definition: option.c:404
static void check_ok(void)
Definition: initdb.c:2018
transferMode transfer_mode
Definition: pg_upgrade.h:292
UserOpts user_opts
Definition: option.c:30
bool verbose
Definition: pg_upgrade.h:280
char * bindir
Definition: pg_upgrade.h:262
#define MAX_STRING
Definition: pg_upgrade.h:18
#define no_argument
Definition: getopt_long.h:24
#define DEF_PGUPORT
Definition: pg_upgrade.h:16
#define INTERNAL_LOG_FILE
Definition: pg_upgrade.h:32
#define free(a)
Definition: header.h:65
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define Max(x, y)
Definition: c.h:922
const char * progname
Definition: pg_upgrade.h:308
#define LOCK_FILE_LINE_PORT
Definition: pidfile.h:40
FILE * internal
Definition: pg_upgrade.h:279
#define strerror
Definition: port.h:228
bool check
Definition: pg_upgrade.h:290
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static char * filename
Definition: pg_dumpall.c:91
#define fopen_priv(path, mode)
Definition: pg_upgrade.h:380
char * pgdata
Definition: pg_upgrade.h:259
char * sockdir
Definition: pg_upgrade.h:265
char * optarg
Definition: getopt.c:52
OSInfo os_info
Definition: pg_upgrade.c:61
void pg_putenv(const char *var, const char *val)
Definition: util.c:253
bool retain
Definition: pg_upgrade.h:281
#define snprintf
Definition: port.h:215
char * user
Definition: pg_upgrade.h:309
char * socketdir
Definition: pg_upgrade.h:294
#define _(x)
Definition: elog.c:88
static void usage(void)
Definition: option.c:286