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