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