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