PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
option.c File Reference
#include "postgres_fe.h"
#include "miscadmin.h"
#include "getopt_long.h"
#include "pg_upgrade.h"
#include <time.h>
#include <sys/types.h>
Include dependency graph for option.c:

Go to the source code of this file.

Macros

#define FIX_DEFAULT_READ_ONLY   "-c default_transaction_read_only=false"
 

Functions

static void usage (void)
 
static void check_required_directory (char **dirpath, char **configpath, char *envVarName, char *cmdLineOption, char *description)
 
void parseCommandLine (int argc, char *argv[])
 
void adjust_data_dir (ClusterInfo *cluster)
 
void get_sock_dir (ClusterInfo *cluster, bool live_check)
 

Variables

UserOpts user_opts
 

Macro Definition Documentation

#define FIX_DEFAULT_READ_ONLY   "-c default_transaction_read_only=false"

Definition at line 27 of file option.c.

Referenced by parseCommandLine().

Function Documentation

void adjust_data_dir ( ClusterInfo cluster)

Definition at line 385 of file option.c.

References ClusterInfo::bindir, check_ok(), CLUSTER_NAME, filename, MAX_STRING, MAXPGPATH, NULL, output(), pg_fatal(), pg_strdup(), ClusterInfo::pgconfig, ClusterInfo::pgdata, prep_status(), snprintf(), and strerror().

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 }
static void output(uint64 loop_count)
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
char * pgconfig
Definition: pg_upgrade.h:267
#define MAXPGPATH
#define CLUSTER_NAME(cluster)
Definition: pg_upgrade.h:97
void prep_status(const char *fmt,...) pg_attribute_printf(1
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void check_ok(void)
Definition: initdb.c:1988
char * bindir
Definition: pg_upgrade.h:269
#define MAX_STRING
Definition: pg_upgrade.h:21
#define NULL
Definition: c.h:226
static char * filename
Definition: pg_dumpall.c:84
char * pgdata
Definition: pg_upgrade.h:266
const char * strerror(int errnum)
Definition: strerror.c:19
static void check_required_directory ( char **  dirpath,
char **  configpath,
char *  envVarName,
char *  cmdLineOption,
char *  description 
)
static

Definition at line 344 of file option.c.

References NULL, pg_fatal(), and pg_strdup().

Referenced by parseCommandLine().

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 }
void pg_fatal(const char *fmt,...)
Definition: logging.c:83
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
void get_sock_dir ( ClusterInfo cluster,
bool  live_check 
)

Definition at line 446 of file option.c.

References DEF_PGUPORT, filename, GET_MAJOR_VERSION, LOCK_FILE_LINE_PORT, LOCK_FILE_LINE_SOCKET_DIR, ClusterInfo::major_version, Max, MAXPGPATH, NULL, old_cluster, pg_fatal(), pg_log(), pg_malloc(), pg_strdup(), PG_WARNING, ClusterInfo::pgdata, ClusterInfo::port, snprintf(), and ClusterInfo::sockdir.

Referenced by main().

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 }
uint32 major_version
Definition: pg_upgrade.h:274
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define GET_MAJOR_VERSION(v)
Definition: pg_upgrade.h:29
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
#define MAXPGPATH
#define LOCK_FILE_LINE_SOCKET_DIR
Definition: miscadmin.h:450
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
#define DEF_PGUPORT
Definition: pg_upgrade.h:16
#define Max(x, y)
Definition: c.h:795
#define NULL
Definition: c.h:226
#define LOCK_FILE_LINE_PORT
Definition: miscadmin.h:449
static char * filename
Definition: pg_dumpall.c:84
char * pgdata
Definition: pg_upgrade.h:266
char * sockdir
Definition: pg_upgrade.h:272
void parseCommandLine ( int  argc,
char *  argv[] 
)

Definition at line 39 of file option.c.

References _, ClusterInfo::bindir, canonicalize_path(), UserOpts::check, check_required_directory(), DEF_PGUPORT, filename, FIX_DEFAULT_READ_ONLY, fopen_priv(), free, get_progname(), get_user_info(), getopt_long(), LogOpts::internal, INTERNAL_LOG_FILE, UserOpts::jobs, log_opts, MAXPGPATH, new_cluster, no_argument, NULL, old_cluster, optarg, os_info, output_files, path_is_prefix_of_path(), pfree(), pg_fatal(), pg_free(), pg_log(), pg_putenv(), PG_REPORT, pg_strdup(), ClusterInfo::pgconfig, ClusterInfo::pgdata, ClusterInfo::pgopts, ClusterInfo::port, OSInfo::progname, psprintf(), required_argument, LogOpts::retain, strlcpy(), UserOpts::transfer_mode, TRANSFER_MODE_COPY, TRANSFER_MODE_LINK, usage(), OSInfo::user, OSInfo::user_specified, and LogOpts::verbose.

Referenced by main().

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':
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 }
const char * get_progname(const char *argv0)
Definition: path.c:453
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
void canonicalize_path(char *path)
Definition: path.c:254
unsigned short port
Definition: pg_upgrade.h:273
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
char * output_files[]
Definition: pg_upgrade.c:59
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_log(eLogType type, const char *fmt,...)
Definition: logging.c:69
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 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
const char * progname
Definition: pg_upgrade.h:309
#define NULL
Definition: c.h:226
FILE * internal
Definition: pg_upgrade.h:286
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 * optarg
Definition: getopt.c:53
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
static void usage ( void  )
static

Definition at line 276 of file option.c.

References _, new_cluster, old_cluster, os_info, ClusterInfo::port, and OSInfo::user.

Referenced by parseCommandLine().

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 }
unsigned short port
Definition: pg_upgrade.h:273
ClusterInfo new_cluster
Definition: pg_upgrade.c:55
ClusterInfo old_cluster
Definition: pg_upgrade.c:55
OSInfo os_info
Definition: pg_upgrade.c:57
char * user
Definition: pg_upgrade.h:311
#define _(x)
Definition: elog.c:84

Variable Documentation