option.c 11.1 KB
Newer Older
1 2 3 4 5
/*
 *	opt.c
 *
 *	options functions
 *
B
Bruce Momjian 已提交
6
 *	Copyright (c) 2010-2011, PostgreSQL Global Development Group
7
 *	contrib/pg_upgrade/option.c
8 9 10 11 12 13 14 15 16 17 18
 */

#include "pg_upgrade.h"

#include "getopt_long.h"

#ifdef WIN32
#include <io.h>
#endif


19 20
static void usage(void);
static void validateDirectoryOption(char **dirpath,
21
				   char *envVarName, char *cmdLineOption, char *description);
22 23 24 25
static void get_pkglibdirs(void);
static char *get_pkglibdir(const char *bindir);


26
UserOpts	user_opts;
27 28 29 30 31


/*
 * parseCommandLine()
 *
32
 *	Parses the command line (argc, argv[]) and loads structures
33 34
 */
void
35
parseCommandLine(int argc, char *argv[])
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
{
	static struct option long_options[] = {
		{"old-datadir", required_argument, NULL, 'd'},
		{"new-datadir", required_argument, NULL, 'D'},
		{"old-bindir", required_argument, NULL, 'b'},
		{"new-bindir", required_argument, NULL, 'B'},
		{"old-port", required_argument, NULL, 'p'},
		{"new-port", required_argument, NULL, 'P'},

		{"user", required_argument, NULL, 'u'},
		{"check", no_argument, NULL, 'c'},
		{"debug", no_argument, NULL, 'g'},
		{"debugfile", required_argument, NULL, 'G'},
		{"link", no_argument, NULL, 'k'},
		{"logfile", required_argument, NULL, 'l'},
		{"verbose", no_argument, NULL, 'v'},
52
		{"progress", no_argument, NULL, 'X'},
53 54
		{"add-checksum", no_argument, NULL, 'J'},
		{"remove-checksum", no_argument, NULL, 'j'},
55 56 57

		{"dispatcher-mode", no_argument, NULL, 1},

58 59 60 61
		{NULL, 0, NULL, 0}
	};
	int			option;			/* Command line option */
	int			optindex = 0;	/* used by getopt_long */
62
	int			os_user_effective_id;
63

64
	user_opts.transfer_mode = TRANSFER_MODE_COPY;
65

66
	os_info.progname = get_progname(argv[0]);
67

68
	/* Process libpq env. variables; load values here for usage() output */
69 70
	old_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
	new_cluster.port = getenv("PGPORT") ? atoi(getenv("PGPORT")) : DEF_PGPORT;
71

72 73 74 75 76 77 78 79
	os_user_effective_id = get_user_info(&os_info.user);
	/* we override just the database user name;  we got the OS id above */
	if (getenv("PGUSER"))
	{
		pg_free(os_info.user);
		/* must save value, getenv()'s pointer is not stable */
		os_info.user = pg_strdup(getenv("PGUSER"));
	}
80 81 82 83 84 85

	if (argc > 1)
	{
		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ||
			strcmp(argv[1], "-?") == 0)
		{
86
			usage();
87
			exit(0);
88 89 90
		}
		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
		{
91
			puts("pg_upgrade (PostgreSQL) " PG_VERSION);
92
			exit(0);
93 94 95
		}
	}

96 97
	/* Allow help and version to be run as root, so do the test here. */
	if (os_user_effective_id == 0)
98
		pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
99

100
	getcwd(os_info.cwd, MAXPGPATH);
101

102
	while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:jJkl:p:P:u:v",
103 104 105 106
								 long_options, &optindex)) != -1)
	{
		switch (option)
		{
107
			case 'b':
108
				old_cluster.bindir = pg_strdup(optarg);
109 110
				break;

111
			case 'B':
112
				new_cluster.bindir = pg_strdup(optarg);
113 114
				break;

115
			case 'c':
116
				user_opts.check = true;
117 118
				break;

119 120
			case 'd':
				old_cluster.pgdata = pg_strdup(optarg);
121 122
				break;

123 124
			case 'D':
				new_cluster.pgdata = pg_strdup(optarg);
125 126 127
				break;

			case 'g':
128
				pg_log(PG_REPORT, "Running in debug mode\n");
129
				log_opts.debug = true;
130 131 132
				break;

			case 'G':
133
				if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL)
134
				{
135
					pg_log(PG_FATAL, "cannot open debug file\n");
136
					exit(1);
137 138 139
				}
				break;

140
			case 'j':
A
Asim R P 已提交
141
				user_opts.checksum_mode = CHECKSUM_REMOVE;
142 143 144
				break;

			case 'J':
A
Asim R P 已提交
145
				user_opts.checksum_mode = CHECKSUM_ADD;
146 147
				break;

148
			case 'k':
149
				user_opts.transfer_mode = TRANSFER_MODE_LINK;
150 151 152
				break;

			case 'l':
153
				log_opts.filename = pg_strdup(optarg);
154 155 156
				break;

			case 'p':
157
				if ((old_cluster.port = atoi(optarg)) <= 0)
158
				{
159
					pg_log(PG_FATAL, "invalid old port number\n");
160
					exit(1);
161 162 163 164
				}
				break;

			case 'P':
165
				if ((new_cluster.port = atoi(optarg)) <= 0)
166
				{
167
					pg_log(PG_FATAL, "invalid new port number\n");
168
					exit(1);
169 170 171 172
				}
				break;

			case 'u':
173 174
				pg_free(os_info.user);
				os_info.user = pg_strdup(optarg);
B
Bruce Momjian 已提交
175

176 177 178 179 180
				/*
				 * Push the user name into the environment so pre-9.1
				 * pg_ctl/libpq uses it.
				 */
				pg_putenv("PGUSER", os_info.user);
181 182 183
				break;

			case 'v':
184
				pg_log(PG_REPORT, "Running in verbose mode\n");
185
				log_opts.verbose = true;
186 187
				break;

188
			case 'X':
A
Asim R P 已提交
189 190
				pg_log(PG_REPORT, "Running in progress report mode\n");
				log_opts.progress = true;
191 192
				break;

193 194 195 196 197 198 199 200 201 202 203 204
			case 1:		/* --dispatcher-mode */
				/*
				 * XXX: Ideally, we could tell just by looking at the data
				 * directory, whether it's a QD node, or a QE node. You might
				 * think that we could look at Gp_role or gp_contentid, but
				 * alas, we pass those options on the pg_ctl command line,
				 * they are not stored permanently in the data directory
				 * itself, and we don't want to dig into the auxiliary
				 * config files created by gpinitsystem. So for now, the
				 * caller of pg_upgrade must use the --dispatcher-mode
				 * option, when upgrading the QD node.
				 */
A
Asim R P 已提交
205
				user_opts.dispatcher_mode = true;
206 207
				break;

208
			default:
209
				pg_log(PG_FATAL,
210
					   "Try \"%s --help\" for more information.\n",
211
					   os_info.progname);
212 213 214 215
				break;
		}
	}

216
	if (log_opts.filename != NULL)
217 218 219 220 221 222 223
	{
		/*
		 * We must use append mode so output generated by child processes via
		 * ">>" will not be overwritten, and we want the file truncated on
		 * start.
		 */
		/* truncate */
224
		if ((log_opts.fd = fopen(log_opts.filename, "w")) == NULL)
225
			pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename);
226 227
		fclose(log_opts.fd);
		if ((log_opts.fd = fopen(log_opts.filename, "a")) == NULL)
228
			pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename);
229 230
	}
	else
231
		log_opts.filename = strdup(DEVNULL);
232 233

	/* if no debug file name, output to the terminal */
234
	if (log_opts.debug && !log_opts.debug_fd)
235
	{
236 237
		log_opts.debug_fd = fopen(DEVTTY, "w");
		if (!log_opts.debug_fd)
238
			pg_log(PG_FATAL, "cannot write to terminal\n");
239 240
	}

241
	/* Ensure we are only adding checksums in copy mode */
A
Asim R P 已提交
242 243 244
	if (user_opts.transfer_mode != TRANSFER_MODE_COPY &&
		user_opts.checksum_mode != CHECKSUM_NONE)
		pg_log(PG_FATAL, "Adding and removing checksums only supported in copy mode.\n");
245

246
	/* Get values from env if not already set */
247
	validateDirectoryOption(&old_cluster.bindir, "OLDBINDIR", "-b",
248
							"old cluster binaries reside");
249
	validateDirectoryOption(&new_cluster.bindir, "NEWBINDIR", "-B",
250
							"new cluster binaries reside");
251 252 253 254
	validateDirectoryOption(&old_cluster.pgdata, "OLDDATADIR", "-d",
							"old cluster data resides");
	validateDirectoryOption(&new_cluster.pgdata, "NEWDATADIR", "-D",
							"new cluster data resides");
255

256
	get_pkglibdirs();
257 258 259 260
}


static void
261
usage(void)
262
{
263 264 265
	printf(_("pg_upgrade upgrades a PostgreSQL cluster to a different major version.\n\
\nUsage:\n\
  pg_upgrade [OPTIONS]...\n\
266 267
\n\
Options:\n\
268 269 270 271 272 273 274
  -b, --old-bindir=OLDBINDIR    old cluster executable directory\n\
  -B, --new-bindir=NEWBINDIR    new cluster executable directory\n\
  -c, --check                   check clusters only, don't change any data\n\
  -d, --old-datadir=OLDDATADIR  old cluster data directory\n\
  -D, --new-datadir=NEWDATADIR  new cluster data directory\n\
  -g, --debug                   enable debugging\n\
  -G, --debugfile=FILENAME      output debugging activity to file\n\
A
Asim R P 已提交
275 276
  -j, --remove-checksum         remove data checksums when creating new cluster\n\
  -J, --add-checksum            add data checksumming to the new cluster\n\
277 278 279 280 281 282
  -k, --link                    link instead of copying files to new cluster\n\
  -l, --logfile=FILENAME        log session activity to file\n\
  -p, --old-port=OLDPORT        old cluster port number (default %d)\n\
  -P, --new-port=NEWPORT        new cluster port number (default %d)\n\
  -u, --user=NAME               clusters superuser (default \"%s\")\n\
  -v, --verbose                 enable verbose output\n\
A
Asim R P 已提交
283
  -X, --progress                enable progress reporting\n\
284 285
  -V, --version                 display version information, then exit\n\
  -h, --help                    show this help, then exit\n\
286 287 288 289 290 291 292 293 294
\n\
Before running pg_upgrade you must:\n\
  create a new database cluster (using the new version of initdb)\n\
  shutdown the postmaster servicing the old cluster\n\
  shutdown the postmaster servicing the new cluster\n\
\n\
When you run pg_upgrade, you must provide the following information:\n\
  the data directory for the old cluster  (-d OLDDATADIR)\n\
  the data directory for the new cluster  (-D NEWDATADIR)\n\
295 296
  the \"bin\" directory for the old version (-b OLDBINDIR)\n\
  the \"bin\" directory for the new version (-B NEWBINDIR)\n\
297 298 299
\n\
For example:\n\
  pg_upgrade -d oldCluster/data -D newCluster/data -b oldCluster/bin -B newCluster/bin\n\
300
or\n"), old_cluster.port, new_cluster.port, os_info.user);
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
#ifndef WIN32
	printf(_("\
  $ export OLDDATADIR=oldCluster/data\n\
  $ export NEWDATADIR=newCluster/data\n\
  $ export OLDBINDIR=oldCluster/bin\n\
  $ export NEWBINDIR=newCluster/bin\n\
  $ pg_upgrade\n"));
#else
	printf(_("\
  C:\\> set OLDDATADIR=oldCluster/data\n\
  C:\\> set NEWDATADIR=newCluster/data\n\
  C:\\> set OLDBINDIR=oldCluster/bin\n\
  C:\\> set NEWBINDIR=newCluster/bin\n\
  C:\\> pg_upgrade\n"));
#endif
A
Asim R P 已提交
316
	printf(_("\nReport bugs to <bugs@greenplum.org>.\n"));
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
}


/*
 * validateDirectoryOption()
 *
 * Validates a directory option.
 *	dirpath		  - the directory name supplied on the command line
 *	envVarName	  - the name of an environment variable to get if dirpath is NULL
 *	cmdLineOption - the command line option corresponds to this directory (-o, -O, -n, -N)
 *	description   - a description of this directory option
 *
 * We use the last two arguments to construct a meaningful error message if the
 * user hasn't provided the required directory name.
 */
static void
333
validateDirectoryOption(char **dirpath,
334 335 336 337 338 339 340
					char *envVarName, char *cmdLineOption, char *description)
{
	if (*dirpath == NULL || (strlen(*dirpath) == 0))
	{
		const char *envVar;

		if ((envVar = getenv(envVarName)) && strlen(envVar))
341
			*dirpath = pg_strdup(envVar);
342 343
		else
		{
344
			pg_log(PG_FATAL, "You must identify the directory where the %s\n"
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
				   "Please use the %s command-line option or the %s environment variable\n",
				   description, cmdLineOption, envVarName);
		}
	}

	/*
	 * Trim off any trailing path separators
	 */
#ifndef WIN32
	if ((*dirpath)[strlen(*dirpath) - 1] == '/')
#else
	if ((*dirpath)[strlen(*dirpath) - 1] == '/' ||
		(*dirpath)[strlen(*dirpath) - 1] == '\\')
#endif
		(*dirpath)[strlen(*dirpath) - 1] = 0;
}


static void
364
get_pkglibdirs(void)
365 366 367 368 369
{
	/*
	 * we do not need to know the libpath in the old cluster, and might not
	 * have a working pg_config to ask for it anyway.
	 */
370
	old_cluster.libpath = NULL;
371
	new_cluster.libpath = get_pkglibdir(new_cluster.bindir);
372 373 374 375
}


static char *
376
get_pkglibdir(const char *bindir)
377 378 379 380 381 382 383 384 385
{
	char		cmd[MAXPGPATH];
	char		bufin[MAX_STRING];
	FILE	   *output;
	int			i;

	snprintf(cmd, sizeof(cmd), "\"%s/pg_config\" --pkglibdir", bindir);

	if ((output = popen(cmd, "r")) == NULL)
386
		pg_log(PG_FATAL, "Could not get pkglibdir data: %s\n",
387 388 389 390 391 392 393 394 395 396 397 398 399
			   getErrorText(errno));

	fgets(bufin, sizeof(bufin), output);

	if (output)
		pclose(output);

	/* Remove trailing newline */
	i = strlen(bufin) - 1;

	if (bufin[i] == '\n')
		bufin[i] = '\0';

400
	return pg_strdup(bufin);
401
}