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-2024, 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 "fe_utils/option_utils.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  {"no-sync", no_argument, NULL, 'N'},
47  {"old-options", required_argument, NULL, 'o'},
48  {"new-options", required_argument, NULL, 'O'},
49  {"old-port", required_argument, NULL, 'p'},
50  {"new-port", required_argument, NULL, 'P'},
51 
52  {"username", required_argument, NULL, 'U'},
53  {"check", no_argument, NULL, 'c'},
54  {"link", no_argument, NULL, 'k'},
55  {"retain", no_argument, NULL, 'r'},
56  {"jobs", required_argument, NULL, 'j'},
57  {"socketdir", required_argument, NULL, 's'},
58  {"verbose", no_argument, NULL, 'v'},
59  {"clone", no_argument, NULL, 1},
60  {"copy", no_argument, NULL, 2},
61  {"copy-file-range", no_argument, NULL, 3},
62  {"sync-method", required_argument, NULL, 4},
63 
64  {NULL, 0, NULL, 0}
65  };
66  int option; /* Command line option */
67  int optindex = 0; /* used by getopt_long */
68  int os_user_effective_id;
69  DataDirSyncMethod unused;
70 
71  user_opts.do_sync = true;
73 
74  os_info.progname = get_progname(argv[0]);
75 
76  /* Process libpq env. variables; load values here for usage() output */
77  old_cluster.port = getenv("PGPORTOLD") ? atoi(getenv("PGPORTOLD")) : DEF_PGUPORT;
78  new_cluster.port = getenv("PGPORTNEW") ? atoi(getenv("PGPORTNEW")) : DEF_PGUPORT;
79 
80  os_user_effective_id = get_user_info(&os_info.user);
81  /* we override just the database user name; we got the OS id above */
82  if (getenv("PGUSER"))
83  {
85  /* must save value, getenv()'s pointer is not stable */
86  os_info.user = pg_strdup(getenv("PGUSER"));
87  }
88 
89  if (argc > 1)
90  {
91  if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
92  {
93  usage();
94  exit(0);
95  }
96  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
97  {
98  puts("pg_upgrade (PostgreSQL) " PG_VERSION);
99  exit(0);
100  }
101  }
102 
103  /* Allow help and version to be run as root, so do the test here. */
104  if (os_user_effective_id == 0)
105  pg_fatal("%s: cannot be run as root", os_info.progname);
106 
107  while ((option = getopt_long(argc, argv, "b:B:cd:D:j:kNo:O:p:P:rs:U:v",
108  long_options, &optindex)) != -1)
109  {
110  switch (option)
111  {
112  case 'b':
114  break;
115 
116  case 'B':
118  break;
119 
120  case 'c':
121  user_opts.check = true;
122  break;
123 
124  case 'd':
126  break;
127 
128  case 'D':
130  break;
131 
132  case 'j':
133  user_opts.jobs = atoi(optarg);
134  break;
135 
136  case 'k':
138  break;
139 
140  case 'N':
141  user_opts.do_sync = false;
142  break;
143 
144  case 'o':
145  /* append option? */
146  if (!old_cluster.pgopts)
148  else
149  {
150  char *old_pgopts = old_cluster.pgopts;
151 
152  old_cluster.pgopts = psprintf("%s %s", old_pgopts, optarg);
153  free(old_pgopts);
154  }
155  break;
156 
157  case 'O':
158  /* append option? */
159  if (!new_cluster.pgopts)
161  else
162  {
163  char *new_pgopts = new_cluster.pgopts;
164 
165  new_cluster.pgopts = psprintf("%s %s", new_pgopts, optarg);
166  free(new_pgopts);
167  }
168  break;
169 
170  case 'p':
171  if ((old_cluster.port = atoi(optarg)) <= 0)
172  pg_fatal("invalid old port number");
173  break;
174 
175  case 'P':
176  if ((new_cluster.port = atoi(optarg)) <= 0)
177  pg_fatal("invalid new port number");
178  break;
179 
180  case 'r':
181  log_opts.retain = true;
182  break;
183 
184  case 's':
186  break;
187 
188  case 'U':
191  os_info.user_specified = true;
192  break;
193 
194  case 'v':
195  log_opts.verbose = true;
196  break;
197 
198  case 1:
200  break;
201 
202  case 2:
204  break;
205 
206  case 3:
208  break;
209  case 4:
210  if (!parse_sync_method(optarg, &unused))
211  exit(1);
213  break;
214 
215  default:
216  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
217  os_info.progname);
218  exit(1);
219  }
220  }
221 
222  if (optind < argc)
223  pg_fatal("too many command-line arguments (first is \"%s\")", argv[optind]);
224 
225  if (!user_opts.sync_method)
226  user_opts.sync_method = pg_strdup("fsync");
227 
228  if (log_opts.verbose)
229  pg_log(PG_REPORT, "Running in verbose mode");
230 
231  log_opts.isatty = isatty(fileno(stdout));
232 
233  /* Turn off read-only mode; add prefix to PGOPTIONS? */
234  if (getenv("PGOPTIONS"))
235  {
236  char *pgoptions = psprintf("%s %s", FIX_DEFAULT_READ_ONLY,
237  getenv("PGOPTIONS"));
238 
239  setenv("PGOPTIONS", pgoptions, 1);
240  pfree(pgoptions);
241  }
242  else
243  setenv("PGOPTIONS", FIX_DEFAULT_READ_ONLY, 1);
244 
245  /* Get values from env if not already set */
246  check_required_directory(&old_cluster.bindir, "PGBINOLD", false,
247  "-b", _("old cluster binaries reside"), false);
248  check_required_directory(&new_cluster.bindir, "PGBINNEW", false,
249  "-B", _("new cluster binaries reside"), true);
250  check_required_directory(&old_cluster.pgdata, "PGDATAOLD", false,
251  "-d", _("old cluster data resides"), false);
252  check_required_directory(&new_cluster.pgdata, "PGDATANEW", false,
253  "-D", _("new cluster data resides"), false);
254  check_required_directory(&user_opts.socketdir, "PGSOCKETDIR", true,
255  "-s", _("sockets will be created"), false);
256 
257 #ifdef WIN32
258 
259  /*
260  * On Windows, initdb --sync-only will fail with a "Permission denied"
261  * error on file pg_upgrade_utility.log if pg_upgrade is run inside the
262  * new cluster directory, so we do a check here.
263  */
264  {
265  char cwd[MAXPGPATH],
266  new_cluster_pgdata[MAXPGPATH];
267 
268  strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
269  canonicalize_path(new_cluster_pgdata);
270 
271  if (!getcwd(cwd, MAXPGPATH))
272  pg_fatal("could not determine current directory");
273  canonicalize_path(cwd);
274  if (path_is_prefix_of_path(new_cluster_pgdata, cwd))
275  pg_fatal("cannot run pg_upgrade from inside the new cluster data directory on Windows");
276  }
277 #endif
278 }
279 
280 
281 static void
282 usage(void)
283 {
284  printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\n"));
285  printf(_("Usage:\n"));
286  printf(_(" pg_upgrade [OPTION]...\n\n"));
287  printf(_("Options:\n"));
288  printf(_(" -b, --old-bindir=BINDIR old cluster executable directory\n"));
289  printf(_(" -B, --new-bindir=BINDIR new cluster executable directory (default\n"
290  " same directory as pg_upgrade)\n"));
291  printf(_(" -c, --check check clusters only, don't change any data\n"));
292  printf(_(" -d, --old-datadir=DATADIR old cluster data directory\n"));
293  printf(_(" -D, --new-datadir=DATADIR new cluster data directory\n"));
294  printf(_(" -j, --jobs=NUM number of simultaneous processes or threads to use\n"));
295  printf(_(" -k, --link link instead of copying files to new cluster\n"));
296  printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
297  printf(_(" -o, --old-options=OPTIONS old cluster options to pass to the server\n"));
298  printf(_(" -O, --new-options=OPTIONS new cluster options to pass to the server\n"));
299  printf(_(" -p, --old-port=PORT old cluster port number (default %d)\n"), old_cluster.port);
300  printf(_(" -P, --new-port=PORT new cluster port number (default %d)\n"), new_cluster.port);
301  printf(_(" -r, --retain retain SQL and log files after success\n"));
302  printf(_(" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
303  printf(_(" -U, --username=NAME cluster superuser (default \"%s\")\n"), os_info.user);
304  printf(_(" -v, --verbose enable verbose internal logging\n"));
305  printf(_(" -V, --version display version information, then exit\n"));
306  printf(_(" --clone clone instead of copying files to new cluster\n"));
307  printf(_(" --copy copy files to new cluster (default)\n"));
308  printf(_(" --copy-file-range copy files to new cluster with copy_file_range\n"));
309  printf(_(" --sync-method=METHOD set method for syncing files to disk\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");
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.",
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  int rc;
412 
413  /* Initially assume config dir and data dir are the same */
414  cluster->pgconfig = pg_strdup(cluster->pgdata);
415 
416  /* If there is no postgresql.conf, it can't be a config-only dir */
417  snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
418  if ((fp = fopen(filename, "r")) == NULL)
419  return;
420  fclose(fp);
421 
422  /* If PG_VERSION exists, it can't be a config-only dir */
423  snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
424  if ((fp = fopen(filename, "r")) != NULL)
425  {
426  fclose(fp);
427  return;
428  }
429 
430  /* Must be a configuration directory, so find the real data directory. */
431 
432  if (cluster == &old_cluster)
433  prep_status("Finding the real data directory for the source cluster");
434  else
435  prep_status("Finding the real data directory for the target cluster");
436 
437  /*
438  * We don't have a data directory yet, so we can't check the PG version,
439  * so this might fail --- only works for PG 9.2+. If this fails,
440  * pg_upgrade will fail anyway because the data files will not be found.
441  */
442  snprintf(cmd, sizeof(cmd), "\"%s/postgres\" -D \"%s\" -C data_directory",
443  cluster->bindir, cluster->pgconfig);
444  fflush(NULL);
445 
446  if ((output = popen(cmd, "r")) == NULL ||
447  fgets(cmd_output, sizeof(cmd_output), output) == NULL)
448  pg_fatal("could not get data directory using %s: %m", cmd);
449 
450  rc = pclose(output);
451  if (rc != 0)
452  pg_fatal("could not get data directory using %s: %s",
453  cmd, wait_result_to_str(rc));
454 
455  /* strip trailing newline and carriage return */
456  (void) pg_strip_crlf(cmd_output);
457 
458  cluster->pgdata = pg_strdup(cmd_output);
459 
460  check_ok();
461 }
462 
463 
464 /*
465  * get_sock_dir
466  *
467  * Identify the socket directory to use for this cluster. If we're doing
468  * a live check (old cluster only), we need to find out where the postmaster
469  * is listening. Otherwise, we're going to put the socket into the current
470  * directory.
471  */
472 void
473 get_sock_dir(ClusterInfo *cluster, bool live_check)
474 {
475 #if !defined(WIN32)
476  if (!live_check)
477  cluster->sockdir = user_opts.socketdir;
478  else
479  {
480  /*
481  * If we are doing a live check, we will use the old cluster's Unix
482  * domain socket directory so we can connect to the live server.
483  */
484  unsigned short orig_port = cluster->port;
485  char filename[MAXPGPATH],
486  line[MAXPGPATH];
487  FILE *fp;
488  int lineno;
489 
490  snprintf(filename, sizeof(filename), "%s/postmaster.pid",
491  cluster->pgdata);
492  if ((fp = fopen(filename, "r")) == NULL)
493  pg_fatal("could not open file \"%s\": %m", filename);
494 
495  for (lineno = 1;
497  lineno++)
498  {
499  if (fgets(line, sizeof(line), fp) == NULL)
500  pg_fatal("could not read line %d from file \"%s\": %m",
501  lineno, filename);
502 
503  /* potentially overwrite user-supplied value */
504  if (lineno == LOCK_FILE_LINE_PORT)
505  sscanf(line, "%hu", &old_cluster.port);
506  if (lineno == LOCK_FILE_LINE_SOCKET_DIR)
507  {
508  /* strip trailing newline and carriage return */
509  cluster->sockdir = pg_strdup(line);
510  (void) pg_strip_crlf(cluster->sockdir);
511  }
512  }
513  fclose(fp);
514 
515  /* warn of port number correction */
516  if (orig_port != DEF_PGUPORT && old_cluster.port != orig_port)
517  pg_log(PG_WARNING, "user-supplied old port number %hu corrected to %hu",
518  orig_port, cluster->port);
519  }
520 #else /* WIN32 */
521  cluster->sockdir = NULL;
522 #endif
523 }
#define Max(x, y)
Definition: c.h:998
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
Definition: cluster.c:108
#define _(x)
Definition: elog.c:90
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
DataDirSyncMethod
Definition: file_utils.h:28
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:24
#define required_argument
Definition: getopt_long.h:25
#define free(a)
Definition: header.h:65
FILE * output
static void check_ok(void)
Definition: initdb.c:2036
static void const char fflush(stdout)
exit(1)
void pfree(void *pointer)
Definition: mcxt.c:1520
bool parse_sync_method(const char *optarg, DataDirSyncMethod *sync_method)
Definition: option_utils.c:90
#define pg_fatal(...)
#define MAXPGPATH
static char * filename
Definition: pg_dumpall.c:119
PGDLLIMPORT int optind
Definition: getopt.c:50
PGDLLIMPORT char * optarg
Definition: getopt.c:52
OSInfo os_info
Definition: pg_upgrade.c:73
ClusterInfo new_cluster
Definition: pg_upgrade.c:72
ClusterInfo old_cluster
Definition: pg_upgrade.c:71
int get_user_info(char **user_name_p)
Definition: util.c:323
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
@ TRANSFER_MODE_COPY
Definition: pg_upgrade.h:258
@ TRANSFER_MODE_LINK
Definition: pg_upgrade.h:260
@ TRANSFER_MODE_CLONE
Definition: pg_upgrade.h:257
@ TRANSFER_MODE_COPY_FILE_RANGE
Definition: pg_upgrade.h:259
#define DEF_PGUPORT
Definition: pg_upgrade.h:20
LogOpts log_opts
Definition: util.c:17
@ PG_WARNING
Definition: pg_upgrade.h:272
@ PG_REPORT
Definition: pg_upgrade.h:271
#define MAX_STRING
Definition: pg_upgrade.h:22
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 snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242
#define printf(...)
Definition: port.h:244
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:27
void get_sock_dir(ClusterInfo *cluster, bool live_check)
Definition: option.c:473
UserOpts user_opts
Definition: option.c:30
void adjust_data_dir(ClusterInfo *cluster)
Definition: option.c:404
void parseCommandLine(int argc, char *argv[])
Definition: option.c:39
static void usage(void)
Definition: option.c:282
static void check_required_directory(char **dirpath, const char *envVarName, bool useCwd, const char *cmdLineOption, const char *description, bool missingOk)
Definition: option.c:359
int pg_strip_crlf(char *str)
Definition: string.c:155
char * pgdata
Definition: pg_upgrade.h:287
unsigned short port
Definition: pg_upgrade.h:294
char * bindir
Definition: pg_upgrade.h:290
char * pgopts
Definition: pg_upgrade.h:291
bool retain
Definition: pg_upgrade.h:309
bool isatty
Definition: pg_upgrade.h:315
bool verbose
Definition: pg_upgrade.h:308
char * user
Definition: pg_upgrade.h:345
const char * progname
Definition: pg_upgrade.h:344
bool user_specified
Definition: pg_upgrade.h:346
char * sync_method
Definition: pg_upgrade.h:330
bool do_sync
Definition: pg_upgrade.h:326
char * socketdir
Definition: pg_upgrade.h:329
transferMode transfer_mode
Definition: pg_upgrade.h:327
bool check
Definition: pg_upgrade.h:324
int jobs
Definition: pg_upgrade.h:328
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:33
const char * description
#define setenv(x, y, z)
Definition: win32_port.h:537