Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * initdb --- initialize a PostgreSQL installation
4 : : *
5 : : * initdb creates (initializes) a PostgreSQL database cluster (site,
6 : : * instance, installation, whatever). A database cluster is a
7 : : * collection of PostgreSQL databases all managed by the same server.
8 : : *
9 : : * To create the database cluster, we create the directory that contains
10 : : * all its data, create the files that hold the global tables, create
11 : : * a few other control files for it, and create three databases: the
12 : : * template databases "template0" and "template1", and a default user
13 : : * database "postgres".
14 : : *
15 : : * The template databases are ordinary PostgreSQL databases. template0
16 : : * is never supposed to change after initdb, whereas template1 can be
17 : : * changed to add site-local standard data. Either one can be copied
18 : : * to produce a new database.
19 : : *
20 : : * For largely-historical reasons, the template1 database is the one built
21 : : * by the basic bootstrap process. After it is complete, template0 and
22 : : * the default database, postgres, are made just by copying template1.
23 : : *
24 : : * To create template1, we run the postgres (backend) program in bootstrap
25 : : * mode and feed it data from the postgres.bki library file. After this
26 : : * initial bootstrap phase, some additional stuff is created by normal
27 : : * SQL commands fed to a standalone backend. Some of those commands are
28 : : * just embedded into this program (yeah, it's ugly), but larger chunks
29 : : * are taken from script files.
30 : : *
31 : : *
32 : : * Note:
33 : : * The program has some memory leakage - it isn't worth cleaning it up.
34 : : *
35 : : * This is a C implementation of the previous shell script for setting up a
36 : : * PostgreSQL cluster location, and should be highly compatible with it.
37 : : * author of C translation: Andrew Dunstan mailto:andrew@dunslane.net
38 : : *
39 : : * This code is released under the terms of the PostgreSQL License.
40 : : *
41 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
42 : : * Portions Copyright (c) 1994, Regents of the University of California
43 : : *
44 : : * src/bin/initdb/initdb.c
45 : : *
46 : : *-------------------------------------------------------------------------
47 : : */
48 : :
49 : : #include "postgres_fe.h"
50 : :
51 : : #include <dirent.h>
52 : : #include <fcntl.h>
53 : : #include <netdb.h>
54 : : #include <sys/socket.h>
55 : : #include <sys/stat.h>
56 : : #ifdef USE_ICU
57 : : #include <unicode/ucol.h>
58 : : #endif
59 : : #include <unistd.h>
60 : : #include <signal.h>
61 : : #include <time.h>
62 : :
63 : : #ifdef HAVE_SHM_OPEN
64 : : #include <sys/mman.h>
65 : : #endif
66 : :
67 : : #include "access/xlog_internal.h"
68 : : #include "catalog/pg_authid_d.h"
69 : : #include "catalog/pg_class_d.h"
70 : : #include "catalog/pg_collation_d.h"
71 : : #include "catalog/pg_database_d.h"
72 : : #include "common/file_perm.h"
73 : : #include "common/file_utils.h"
74 : : #include "common/logging.h"
75 : : #include "common/pg_prng.h"
76 : : #include "common/restricted_token.h"
77 : : #include "common/string.h"
78 : : #include "common/username.h"
79 : : #include "fe_utils/option_utils.h"
80 : : #include "fe_utils/string_utils.h"
81 : : #include "getopt_long.h"
82 : : #include "mb/pg_wchar.h"
83 : : #include "miscadmin.h"
84 : :
85 : :
86 : : /* Ideally this would be in a .h file, but it hardly seems worth the trouble */
87 : : extern const char *select_default_timezone(const char *share_path);
88 : :
89 : : /* simple list of strings */
90 : : typedef struct _stringlist
91 : : {
92 : : char *str;
93 : : struct _stringlist *next;
94 : : } _stringlist;
95 : :
96 : : static const char *const auth_methods_host[] = {
97 : : "trust", "reject", "scram-sha-256", "md5", "password", "ident", "radius",
98 : : #ifdef ENABLE_GSS
99 : : "gss",
100 : : #endif
101 : : #ifdef ENABLE_SSPI
102 : : "sspi",
103 : : #endif
104 : : #ifdef USE_PAM
105 : : "pam",
106 : : #endif
107 : : #ifdef USE_BSD_AUTH
108 : : "bsd",
109 : : #endif
110 : : #ifdef USE_LDAP
111 : : "ldap",
112 : : #endif
113 : : #ifdef USE_SSL
114 : : "cert",
115 : : #endif
116 : : NULL
117 : : };
118 : : static const char *const auth_methods_local[] = {
119 : : "trust", "reject", "scram-sha-256", "md5", "password", "peer", "radius",
120 : : #ifdef USE_PAM
121 : : "pam",
122 : : #endif
123 : : #ifdef USE_BSD_AUTH
124 : : "bsd",
125 : : #endif
126 : : #ifdef USE_LDAP
127 : : "ldap",
128 : : #endif
129 : : NULL
130 : : };
131 : :
132 : : /*
133 : : * these values are passed in by makefile defines
134 : : */
135 : : static char *share_path = NULL;
136 : :
137 : : /* values to be obtained from arguments */
138 : : static char *pg_data = NULL;
139 : : static char *encoding = NULL;
140 : : static char *locale = NULL;
141 : : static char *lc_collate = NULL;
142 : : static char *lc_ctype = NULL;
143 : : static char *lc_monetary = NULL;
144 : : static char *lc_numeric = NULL;
145 : : static char *lc_time = NULL;
146 : : static char *lc_messages = NULL;
147 : : static char locale_provider = COLLPROVIDER_LIBC;
148 : : static bool builtin_locale_specified = false;
149 : : static char *datlocale = NULL;
150 : : static bool icu_locale_specified = false;
151 : : static char *icu_rules = NULL;
152 : : static const char *default_text_search_config = NULL;
153 : : static char *username = NULL;
154 : : static bool pwprompt = false;
155 : : static char *pwfilename = NULL;
156 : : static char *superuser_password = NULL;
157 : : static const char *authmethodhost = NULL;
158 : : static const char *authmethodlocal = NULL;
159 : : static _stringlist *extra_guc_names = NULL;
160 : : static _stringlist *extra_guc_values = NULL;
161 : : static bool debug = false;
162 : : static bool noclean = false;
163 : : static bool noinstructions = false;
164 : : static bool do_sync = true;
165 : : static bool sync_only = false;
166 : : static bool show_setting = false;
167 : : static bool data_checksums = true;
168 : : static char *xlog_dir = NULL;
169 : : static int wal_segment_size_mb = (DEFAULT_XLOG_SEG_SIZE) / (1024 * 1024);
170 : : static DataDirSyncMethod sync_method = DATA_DIR_SYNC_METHOD_FSYNC;
171 : : static bool sync_data_files = true;
172 : :
173 : :
174 : : /* internal vars */
175 : : static const char *progname;
176 : : static int encodingid;
177 : : static char *bki_file;
178 : : static char *hba_file;
179 : : static char *ident_file;
180 : : static char *hosts_file;
181 : : static char *conf_file;
182 : : static char *dictionary_file;
183 : : static char *info_schema_file;
184 : : static char *features_file;
185 : : static char *system_constraints_file;
186 : : static char *system_functions_file;
187 : : static char *system_views_file;
188 : : static bool success = false;
189 : : static bool made_new_pgdata = false;
190 : : static bool found_existing_pgdata = false;
191 : : static bool made_new_xlogdir = false;
192 : : static bool found_existing_xlogdir = false;
193 : : static char infoversion[100];
194 : : static bool caught_signal = false;
195 : : static bool output_failed = false;
196 : : static int output_errno = 0;
197 : : static char *pgdata_native;
198 : :
199 : : /* defaults */
200 : : static int n_connections = 10;
201 : : static int n_av_slots = 16;
202 : : static int n_buffers = 50;
203 : : static const char *dynamic_shared_memory_type = NULL;
204 : : static const char *default_timezone = NULL;
205 : :
206 : : /*
207 : : * Warning messages for authentication methods
208 : : */
209 : : #define AUTHTRUST_WARNING \
210 : : "# CAUTION: Configuring the system for local \"trust\" authentication\n" \
211 : : "# allows any local user to connect as any PostgreSQL user, including\n" \
212 : : "# the database superuser. If you do not trust all your local users,\n" \
213 : : "# use another authentication method.\n"
214 : : static bool authwarning = false;
215 : :
216 : : /*
217 : : * Centralized knowledge of switches to pass to backend
218 : : *
219 : : * Note: we run the backend with -F (fsync disabled) and then do a single
220 : : * pass of fsync'ing at the end. This is faster than fsync'ing each step.
221 : : *
222 : : * Note: in the shell-script version, we also passed PGDATA as a -D switch,
223 : : * but here it is more convenient to pass it as an environment variable
224 : : * (no quoting to worry about).
225 : : */
226 : : static const char *const boot_options = "-F -c log_checkpoints=false";
227 : : static const char *const backend_options = "--single -F -O -j -c search_path=pg_catalog -c exit_on_error=true -c log_checkpoints=false";
228 : :
229 : : /* Additional switches to pass to backend (either boot or standalone) */
230 : : static char *extra_options = "";
231 : :
232 : : static const char *const subdirs[] = {
233 : : "global",
234 : : "pg_wal/archive_status",
235 : : "pg_wal/summaries",
236 : : "pg_commit_ts",
237 : : "pg_dynshmem",
238 : : "pg_notify",
239 : : "pg_serial",
240 : : "pg_snapshots",
241 : : "pg_subtrans",
242 : : "pg_twophase",
243 : : "pg_multixact",
244 : : "pg_multixact/members",
245 : : "pg_multixact/offsets",
246 : : "base",
247 : : "base/1",
248 : : "pg_replslot",
249 : : "pg_tblspc",
250 : : "pg_stat",
251 : : "pg_stat_tmp",
252 : : "pg_xact",
253 : : "pg_logical",
254 : : "pg_logical/snapshots",
255 : : "pg_logical/mappings"
256 : : };
257 : :
258 : :
259 : : /* path to 'initdb' binary directory */
260 : : static char bin_path[MAXPGPATH];
261 : : static char backend_exec[MAXPGPATH];
262 : :
263 : : static char **replace_token(char **lines,
264 : : const char *token, const char *replacement);
265 : : static char **replace_guc_value(char **lines,
266 : : const char *guc_name, const char *guc_value,
267 : : bool mark_as_comment);
268 : : static bool guc_value_requires_quotes(const char *guc_value);
269 : : static char **readfile(const char *path);
270 : : static void writefile(char *path, char **lines);
271 : : static FILE *popen_check(const char *command, const char *mode);
272 : : static char *get_id(void);
273 : : static int get_encoding_id(const char *encoding_name);
274 : : static void set_input(char **dest, const char *filename);
275 : : static void check_input(char *path);
276 : : static void write_version_file(const char *extrapath);
277 : : static void set_null_conf(void);
278 : : static void test_config_settings(void);
279 : : static bool test_specific_config_settings(int test_conns, int test_av_slots,
280 : : int test_buffs);
281 : : static void setup_config(void);
282 : : static void bootstrap_template1(void);
283 : : static void setup_auth(FILE *cmdfd);
284 : : static void get_su_pwd(void);
285 : : static void setup_depend(FILE *cmdfd);
286 : : static void setup_run_file(FILE *cmdfd, const char *filename);
287 : : static void setup_description(FILE *cmdfd);
288 : : static void setup_collation(FILE *cmdfd);
289 : : static void setup_privileges(FILE *cmdfd);
290 : : static void set_info_version(void);
291 : : static void setup_schema(FILE *cmdfd);
292 : : static void load_plpgsql(FILE *cmdfd);
293 : : static void vacuum_db(FILE *cmdfd);
294 : : static void make_template0(FILE *cmdfd);
295 : : static void make_postgres(FILE *cmdfd);
296 : : static void trapsig(SIGNAL_ARGS);
297 : : static void check_ok(void);
298 : : static char *escape_quotes(const char *src);
299 : : static char *escape_quotes_bki(const char *src);
300 : : static int locale_date_order(const char *locale);
301 : : static void check_locale_name(int category, const char *locale,
302 : : char **canonname);
303 : : static bool check_locale_encoding(const char *locale, int user_enc);
304 : : static void setlocales(void);
305 : : static void usage(const char *progname);
306 : : void setup_pgdata(void);
307 : : void setup_bin_paths(const char *argv0);
308 : : void setup_data_file_paths(void);
309 : : void setup_locale_encoding(void);
310 : : void setup_signals(void);
311 : : void setup_text_search(void);
312 : : void create_data_directory(void);
313 : : void create_xlog_or_symlink(void);
314 : : void warn_on_mount_point(int error);
315 : : void initialize_data_directory(void);
316 : :
317 : : /*
318 : : * macros for running pipes to postgres
319 : : */
320 : : #define PG_CMD_DECL FILE *cmdfd
321 : :
322 : : #define PG_CMD_OPEN(cmd) \
323 : : do { \
324 : : cmdfd = popen_check(cmd, "w"); \
325 : : if (cmdfd == NULL) \
326 : : exit(1); /* message already printed by popen_check */ \
327 : : } while (0)
328 : :
329 : : #define PG_CMD_CLOSE() \
330 : : do { \
331 : : if (pclose_check(cmdfd)) \
332 : : exit(1); /* message already printed by pclose_check */ \
333 : : } while (0)
334 : :
335 : : #define PG_CMD_PUTS(line) \
336 : : do { \
337 : : if (fputs(line, cmdfd) < 0 || fflush(cmdfd) < 0) \
338 : : output_failed = true, output_errno = errno; \
339 : : } while (0)
340 : :
341 : : #define PG_CMD_PRINTF(fmt, ...) \
342 : : do { \
343 : : if (fprintf(cmdfd, fmt, __VA_ARGS__) < 0 || fflush(cmdfd) < 0) \
344 : : output_failed = true, output_errno = errno; \
345 : : } while (0)
346 : :
347 : : #ifdef WIN32
348 : : typedef wchar_t *save_locale_t;
349 : : #else
350 : : typedef char *save_locale_t;
351 : : #endif
352 : :
353 : : /*
354 : : * Save a copy of the current global locale's name, for the given category.
355 : : * The returned value must be passed to restore_global_locale().
356 : : *
357 : : * Since names from the environment haven't been vetted for non-ASCII
358 : : * characters, we use the wchar_t variant of setlocale() on Windows. Otherwise
359 : : * they might not survive a save-restore round trip: when restoring, the name
360 : : * itself might be interpreted with a different encoding by plain setlocale(),
361 : : * after we switch to another locale in between. (This is a problem only in
362 : : * initdb, not in similar backend code where the global locale's name should
363 : : * already have been verified as ASCII-only.)
364 : : */
365 : : static save_locale_t
577 tmunro@postgresql.or 366 :CBC 454 : save_global_locale(int category)
367 : : {
368 : : save_locale_t save;
369 : :
370 : : #ifdef WIN32
371 : : save = _wsetlocale(category, NULL);
372 : : if (!save)
373 : : pg_fatal("_wsetlocale() failed");
374 : : save = wcsdup(save);
375 : : if (!save)
376 : : pg_fatal("out of memory");
377 : : #else
378 : 454 : save = setlocale(category, NULL);
379 [ - + ]: 454 : if (!save)
577 tmunro@postgresql.or 380 :UBC 0 : pg_fatal("setlocale() failed");
577 tmunro@postgresql.or 381 :CBC 454 : save = pg_strdup(save);
382 : : #endif
383 : 454 : return save;
384 : : }
385 : :
386 : : /*
387 : : * Restore the global locale returned by save_global_locale().
388 : : */
389 : : static void
390 : 454 : restore_global_locale(int category, save_locale_t save)
391 : : {
392 : : #ifdef WIN32
393 : : if (!_wsetlocale(category, save))
394 : : pg_fatal("failed to restore old locale");
395 : : #else
396 [ - + ]: 454 : if (!setlocale(category, save))
577 tmunro@postgresql.or 397 :UBC 0 : pg_fatal("failed to restore old locale \"%s\"", save);
398 : : #endif
577 tmunro@postgresql.or 399 :CBC 454 : free(save);
400 : 454 : }
401 : :
402 : : /*
403 : : * Escape single quotes and backslashes, suitably for insertions into
404 : : * configuration files or SQL E'' strings.
405 : : */
406 : : static char *
4868 magnus@hagander.net 407 : 671 : escape_quotes(const char *src)
408 : : {
4724 bruce@momjian.us 409 : 671 : char *result = escape_single_quotes_ascii(src);
410 : :
4868 magnus@hagander.net 411 [ - + ]: 671 : if (!result)
1488 tgl@sss.pgh.pa.us 412 :UBC 0 : pg_fatal("out of memory");
4868 magnus@hagander.net 413 :CBC 671 : return result;
414 : : }
415 : :
416 : : /*
417 : : * Escape a field value to be inserted into the BKI data.
418 : : * Run the value through escape_quotes (which will be inverted
419 : : * by the backend's DeescapeQuotedString() function), then wrap
420 : : * the value in single quotes, even if that isn't strictly necessary.
421 : : */
422 : : static char *
2940 tgl@sss.pgh.pa.us 423 : 183 : escape_quotes_bki(const char *src)
424 : : {
425 : : char *result;
426 : 183 : char *data = escape_quotes(src);
427 : : char *resultp;
428 : : char *datap;
429 : :
2039 430 : 183 : result = (char *) pg_malloc(strlen(data) + 3);
2940 431 : 183 : resultp = result;
2039 432 : 183 : *resultp++ = '\'';
2940 433 [ + + ]: 1172 : for (datap = data; *datap; datap++)
2039 434 : 989 : *resultp++ = *datap;
435 : 183 : *resultp++ = '\'';
2940 436 : 183 : *resultp = '\0';
437 : :
438 : 183 : free(data);
439 : 183 : return result;
440 : : }
441 : :
442 : : /*
443 : : * Add an item at the end of a stringlist.
444 : : */
445 : : static void
1140 446 : 18 : add_stringlist_item(_stringlist **listhead, const char *str)
447 : : {
67 michael@paquier.xyz 448 :GNC 18 : _stringlist *newentry = pg_malloc_object(_stringlist);
449 : : _stringlist *oldentry;
450 : :
1140 tgl@sss.pgh.pa.us 451 :CBC 18 : newentry->str = pg_strdup(str);
452 : 18 : newentry->next = NULL;
453 [ + + ]: 18 : if (*listhead == NULL)
454 : 14 : *listhead = newentry;
455 : : else
456 : : {
457 [ + + ]: 6 : for (oldentry = *listhead; oldentry->next; oldentry = oldentry->next)
458 : : /* skip */ ;
459 : 4 : oldentry->next = newentry;
460 : : }
461 : 18 : }
462 : :
463 : : /*
464 : : * Modify the array of lines, replacing "token" by "replacement"
465 : : * the first time it occurs on each line. To prevent false matches, the
466 : : * occurrence of "token" must be surrounded by whitespace or line start/end.
467 : : *
468 : : * The array must be a malloc'd array of individually malloc'd strings.
469 : : * We free any discarded strings.
470 : : *
471 : : * This does most of what sed was used for in the shell script, but
472 : : * doesn't need any regexp stuff.
473 : : */
474 : : static char **
7799 475 : 754 : replace_token(char **lines, const char *token, const char *replacement)
476 : : {
477 : : int toklen,
478 : : replen,
479 : : diff;
480 : :
8212 bruce@momjian.us 481 : 754 : toklen = strlen(token);
482 : 754 : replen = strlen(replacement);
483 : 754 : diff = replen - toklen;
484 : :
1140 tgl@sss.pgh.pa.us 485 [ + + ]: 6973862 : for (int i = 0; lines[i]; i++)
486 : : {
487 : : char *where;
488 : : char *endwhere;
489 : : char *newline;
490 : : int pre;
491 : :
492 : : /* nothing to do if no change needed */
493 [ + + ]: 6973108 : if ((where = strstr(lines[i], token)) == NULL)
8212 bruce@momjian.us 494 : 6967772 : continue;
495 : :
496 : : /*
497 : : * Reject false match. Note a blind spot: we don't check for a valid
498 : : * match following a false match. That case can't occur at present,
499 : : * so not worth complicating this code for it.
500 : : */
61 tgl@sss.pgh.pa.us 501 [ + + + + ]:GNC 5336 : if (!(where == lines[i] || isspace((unsigned char) where[-1])))
502 : 4002 : continue;
503 : 1334 : endwhere = where + strlen(token);
504 [ + - - + ]: 1334 : if (!(*endwhere == '\0' || isspace((unsigned char) *endwhere)))
61 tgl@sss.pgh.pa.us 505 :UNC 0 : continue;
506 : :
507 : : /* if we get here a change is needed - set up new line */
508 : :
7604 bruce@momjian.us 509 :CBC 1334 : newline = (char *) pg_malloc(strlen(lines[i]) + diff + 1);
510 : :
8212 511 : 1334 : pre = where - lines[i];
512 : :
4119 tgl@sss.pgh.pa.us 513 : 1334 : memcpy(newline, lines[i], pre);
514 : :
515 : 1334 : memcpy(newline + pre, replacement, replen);
516 : :
8212 bruce@momjian.us 517 : 1334 : strcpy(newline + pre + replen, lines[i] + pre + toklen);
518 : :
1140 tgl@sss.pgh.pa.us 519 : 1334 : free(lines[i]);
520 : 1334 : lines[i] = newline;
521 : : }
522 : :
523 : 754 : return lines;
524 : : }
525 : :
526 : : /*
527 : : * Modify the array of lines, replacing the possibly-commented-out
528 : : * assignment of parameter guc_name with a live assignment of guc_value.
529 : : * The value will be suitably quoted.
530 : : *
531 : : * If mark_as_comment is true, the replacement line is prefixed with '#'.
532 : : * This is used for fixing up cases where the effective default might not
533 : : * match what is in postgresql.conf.sample.
534 : : *
535 : : * We assume there's at most one matching assignment. If we find no match,
536 : : * append a new line with the desired assignment.
537 : : *
538 : : * The array must be a malloc'd array of individually malloc'd strings.
539 : : * We free any discarded strings.
540 : : */
541 : : static char **
542 : 1116 : replace_guc_value(char **lines, const char *guc_name, const char *guc_value,
543 : : bool mark_as_comment)
544 : : {
545 : 1116 : int namelen = strlen(guc_name);
546 : 1116 : PQExpBuffer newline = createPQExpBuffer();
547 : : int i;
548 : :
549 : : /* prepare the replacement line, except for possible comment and newline */
550 [ + + ]: 1116 : if (mark_as_comment)
551 : 290 : appendPQExpBufferChar(newline, '#');
552 : 1116 : appendPQExpBuffer(newline, "%s = ", guc_name);
553 [ + + ]: 1116 : if (guc_value_requires_quotes(guc_value))
554 : 374 : appendPQExpBuffer(newline, "'%s'", escape_quotes(guc_value));
555 : : else
556 : 742 : appendPQExpBufferStr(newline, guc_value);
557 : :
558 [ + + ]: 564452 : for (i = 0; lines[i]; i++)
559 : : {
560 : : const char *where;
561 : : const char *namestart;
562 : :
563 : : /*
564 : : * Look for a line assigning to guc_name. Typically it will be
565 : : * preceded by '#', but that might not be the case if a -c switch
566 : : * overrides a previous assignment. We allow leading whitespace too,
567 : : * although normally there wouldn't be any.
568 : : */
569 : 564451 : where = lines[i];
570 [ + + + + ]: 7971536 : while (*where == '#' || isspace((unsigned char) *where))
571 : 7407085 : where++;
792 572 [ + + ]: 564451 : if (pg_strncasecmp(where, guc_name, namelen) != 0)
1140 573 : 563336 : continue;
792 574 : 1115 : namestart = where;
1140 575 : 1115 : where += namelen;
576 [ + + ]: 2230 : while (isspace((unsigned char) *where))
577 : 1115 : where++;
578 [ - + ]: 1115 : if (*where != '=')
1140 tgl@sss.pgh.pa.us 579 :UBC 0 : continue;
580 : :
581 : : /* found it -- let's use the canonical casing shown in the file */
792 tgl@sss.pgh.pa.us 582 [ + + ]:CBC 1115 : memcpy(&newline->data[mark_as_comment ? 1 : 0], namestart, namelen);
583 : :
584 : : /* now append the original comment if any */
1140 585 : 1115 : where = strrchr(where, '#');
586 [ + + ]: 1115 : if (where)
587 : : {
588 : : /*
589 : : * We try to preserve original indentation, which is tedious.
590 : : * oldindent and newindent are measured in de-tab-ified columns.
591 : : */
592 : : const char *ptr;
593 : 766 : int oldindent = 0;
594 : : int newindent;
595 : :
596 [ + + ]: 31354 : for (ptr = lines[i]; ptr < where; ptr++)
597 : : {
598 [ + + ]: 30588 : if (*ptr == '\t')
599 : 8 : oldindent += 8 - (oldindent % 8);
600 : : else
601 : 30580 : oldindent++;
602 : : }
603 : : /* ignore the possibility of tabs in guc_value */
604 : 766 : newindent = newline->len;
605 : : /* append appropriate tabs and spaces, forcing at least one */
606 : 766 : oldindent = Max(oldindent, newindent + 1);
607 [ + + ]: 2750 : while (newindent < oldindent)
608 : : {
609 : 1984 : int newindent_if_tab = newindent + 8 - (newindent % 8);
610 : :
611 [ + - ]: 1984 : if (newindent_if_tab <= oldindent)
612 : : {
613 : 1984 : appendPQExpBufferChar(newline, '\t');
614 : 1984 : newindent = newindent_if_tab;
615 : : }
616 : : else
617 : : {
1140 tgl@sss.pgh.pa.us 618 :UBC 0 : appendPQExpBufferChar(newline, ' ');
619 : 0 : newindent++;
620 : : }
621 : : }
622 : : /* and finally append the old comment */
1140 tgl@sss.pgh.pa.us 623 :CBC 766 : appendPQExpBufferStr(newline, where);
624 : : /* we'll have appended the original newline; don't add another */
625 : : }
626 : : else
627 : 349 : appendPQExpBufferChar(newline, '\n');
628 : :
629 : 1115 : free(lines[i]);
630 : 1115 : lines[i] = newline->data;
631 : :
632 : 1115 : break; /* assume there's only one match */
633 : : }
634 : :
635 [ + + ]: 1116 : if (lines[i] == NULL)
636 : : {
637 : : /*
638 : : * No match, so append a new entry. (We rely on the bootstrap server
639 : : * to complain if it's not a valid GUC name.)
640 : : */
641 : 1 : appendPQExpBufferChar(newline, '\n');
642 : 1 : lines = pg_realloc_array(lines, char *, i + 2);
643 : 1 : lines[i++] = newline->data;
644 : 1 : lines[i] = NULL; /* keep the array null-terminated */
645 : : }
646 : :
647 : 1116 : free(newline); /* but don't free newline->data */
648 : :
649 : 1116 : return lines;
650 : : }
651 : :
652 : : /*
653 : : * Decide if we should quote a replacement GUC value. We aren't too tense
654 : : * here, but we'd like to avoid quoting simple identifiers and numbers
655 : : * with units, which are common cases.
656 : : */
657 : : static bool
658 : 1116 : guc_value_requires_quotes(const char *guc_value)
659 : : {
660 : : /* Don't use <ctype.h> macros here, they might accept too much */
661 : : #define LETTERS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
662 : : #define DIGITS "0123456789"
663 : :
664 [ - + ]: 1116 : if (*guc_value == '\0')
1140 tgl@sss.pgh.pa.us 665 :UBC 0 : return true; /* empty string must be quoted */
1140 tgl@sss.pgh.pa.us 666 [ + + ]:CBC 1116 : if (strchr(LETTERS, *guc_value))
667 : : {
668 [ + + ]: 586 : if (strspn(guc_value, LETTERS DIGITS) == strlen(guc_value))
669 : 270 : return false; /* it's an identifier */
670 : 316 : return true; /* nope */
671 : : }
672 [ + + ]: 530 : if (strchr(DIGITS, *guc_value))
673 : : {
674 : : /* skip over digits */
675 : 472 : guc_value += strspn(guc_value, DIGITS);
676 : : /* there can be zero or more unit letters after the digits */
677 [ + - ]: 472 : if (strspn(guc_value, LETTERS) == strlen(guc_value))
678 : 472 : return false; /* it's a number, possibly with units */
1140 tgl@sss.pgh.pa.us 679 :UBC 0 : return true; /* nope */
680 : : }
1140 tgl@sss.pgh.pa.us 681 :CBC 58 : return true; /* all else must be quoted */
682 : : }
683 : :
684 : : /*
685 : : * get the lines from a text file
686 : : *
687 : : * The result is a malloc'd array of individually malloc'd strings.
688 : : */
689 : : static char **
6089 690 : 575 : readfile(const char *path)
691 : : {
692 : : char **result;
693 : : FILE *infile;
694 : : StringInfoData line;
695 : : int maxlines;
696 : : int n;
697 : :
8212 bruce@momjian.us 698 [ - + ]: 575 : if ((infile = fopen(path, "r")) == NULL)
1488 tgl@sss.pgh.pa.us 699 :UBC 0 : pg_fatal("could not open file \"%s\" for reading: %m", path);
700 : :
2067 tgl@sss.pgh.pa.us 701 :CBC 575 : initStringInfo(&line);
702 : :
2068 703 : 575 : maxlines = 1024;
67 michael@paquier.xyz 704 :GNC 575 : result = pg_malloc_array(char *, maxlines);
705 : :
2068 tgl@sss.pgh.pa.us 706 :CBC 575 : n = 0;
2051 707 [ + + ]: 1153289 : while (pg_get_line_buf(infile, &line))
708 : : {
709 : : /* make sure there will be room for a trailing NULL pointer */
2068 710 [ + + ]: 1152714 : if (n >= maxlines - 1)
711 : : {
712 : 460 : maxlines *= 2;
67 michael@paquier.xyz 713 :GNC 460 : result = pg_realloc_array(result, char *, maxlines);
714 : : }
715 : :
2067 tgl@sss.pgh.pa.us 716 :CBC 1152714 : result[n++] = pg_strdup(line.data);
717 : : }
2068 718 : 575 : result[n] = NULL;
719 : :
2067 720 : 575 : pfree(line.data);
721 : :
8212 bruce@momjian.us 722 : 575 : fclose(infile);
723 : :
724 : 575 : return result;
725 : : }
726 : :
727 : : /*
728 : : * write an array of lines to a file
729 : : *
730 : : * "lines" must be a malloc'd array of individually malloc'd strings.
731 : : * All that data is freed here.
732 : : *
733 : : * This is only used to write text files. Use fopen "w" not PG_BINARY_W
734 : : * so that the resulting configuration files are nicely editable on Windows.
735 : : */
736 : : static void
737 : 290 : writefile(char *path, char **lines)
738 : : {
739 : : FILE *out_file;
740 : : char **line;
741 : :
7863 tgl@sss.pgh.pa.us 742 [ - + ]: 290 : if ((out_file = fopen(path, "w")) == NULL)
1488 tgl@sss.pgh.pa.us 743 :UBC 0 : pg_fatal("could not open file \"%s\" for writing: %m", path);
8212 bruce@momjian.us 744 [ + + ]:CBC 65773 : for (line = lines; *line != NULL; line++)
745 : : {
746 [ - + ]: 65483 : if (fputs(*line, out_file) < 0)
1488 tgl@sss.pgh.pa.us 747 :UBC 0 : pg_fatal("could not write file \"%s\": %m", path);
8212 bruce@momjian.us 748 :CBC 65483 : free(*line);
749 : : }
750 [ - + ]: 290 : if (fclose(out_file))
1488 tgl@sss.pgh.pa.us 751 :UBC 0 : pg_fatal("could not close file \"%s\": %m", path);
1140 tgl@sss.pgh.pa.us 752 :CBC 290 : free(lines);
7827 753 : 290 : }
754 : :
755 : : /*
756 : : * Open a subcommand with suitable error messaging
757 : : */
758 : : static FILE *
759 : 115 : popen_check(const char *command, const char *mode)
760 : : {
761 : : FILE *cmdfd;
762 : :
1345 763 : 115 : fflush(NULL);
7827 764 : 115 : errno = 0;
765 : 115 : cmdfd = popen(command, mode);
766 [ - + ]: 115 : if (cmdfd == NULL)
2591 peter@eisentraut.org 767 :UBC 0 : pg_log_error("could not execute command \"%s\": %m", command);
7827 tgl@sss.pgh.pa.us 768 :CBC 115 : return cmdfd;
769 : : }
770 : :
771 : : /*
772 : : * clean up any files we created on failure
773 : : * if we created the data directory remove it too
774 : : */
775 : : static void
2684 peter@eisentraut.org 776 : 71 : cleanup_directories_atexit(void)
777 : : {
778 [ + + ]: 71 : if (success)
779 : 55 : return;
780 : :
8212 bruce@momjian.us 781 [ + - ]: 16 : if (!noclean)
782 : : {
783 [ + + ]: 16 : if (made_new_pgdata)
784 : : {
2591 peter@eisentraut.org 785 : 5 : pg_log_info("removing data directory \"%s\"", pg_data);
8212 bruce@momjian.us 786 [ - + ]: 5 : if (!rmtree(pg_data, true))
2591 peter@eisentraut.org 787 :UBC 0 : pg_log_error("failed to remove data directory");
788 : : }
8208 tgl@sss.pgh.pa.us 789 [ - + ]:CBC 11 : else if (found_existing_pgdata)
790 : : {
2591 peter@eisentraut.org 791 :UBC 0 : pg_log_info("removing contents of data directory \"%s\"",
792 : : pg_data);
8212 bruce@momjian.us 793 [ # # ]: 0 : if (!rmtree(pg_data, false))
2591 peter@eisentraut.org 794 : 0 : pg_log_error("failed to remove contents of data directory");
795 : : }
796 : :
7059 bruce@momjian.us 797 [ - + ]:CBC 16 : if (made_new_xlogdir)
798 : : {
2591 peter@eisentraut.org 799 :UBC 0 : pg_log_info("removing WAL directory \"%s\"", xlog_dir);
7059 bruce@momjian.us 800 [ # # ]: 0 : if (!rmtree(xlog_dir, true))
2591 peter@eisentraut.org 801 : 0 : pg_log_error("failed to remove WAL directory");
802 : : }
7059 bruce@momjian.us 803 [ - + ]:CBC 16 : else if (found_existing_xlogdir)
804 : : {
2591 peter@eisentraut.org 805 :UBC 0 : pg_log_info("removing contents of WAL directory \"%s\"", xlog_dir);
7059 bruce@momjian.us 806 [ # # ]: 0 : if (!rmtree(xlog_dir, false))
2591 peter@eisentraut.org 807 : 0 : pg_log_error("failed to remove contents of WAL directory");
808 : : }
809 : : /* otherwise died during startup, do nothing! */
810 : : }
811 : : else
812 : : {
8208 tgl@sss.pgh.pa.us 813 [ # # # # ]: 0 : if (made_new_pgdata || found_existing_pgdata)
2591 peter@eisentraut.org 814 : 0 : pg_log_info("data directory \"%s\" not removed at user's request",
815 : : pg_data);
816 : :
7059 bruce@momjian.us 817 [ # # # # ]: 0 : if (made_new_xlogdir || found_existing_xlogdir)
2591 peter@eisentraut.org 818 : 0 : pg_log_info("WAL directory \"%s\" not removed at user's request",
819 : : xlog_dir);
820 : : }
821 : : }
822 : :
823 : : /*
824 : : * find the current user
825 : : *
826 : : * on unix make sure it isn't root
827 : : */
828 : : static char *
8212 bruce@momjian.us 829 :CBC 67 : get_id(void)
830 : : {
831 : : const char *username;
832 : :
833 : : #ifndef WIN32
7787 tgl@sss.pgh.pa.us 834 [ - + ]: 67 : if (geteuid() == 0) /* 0 is root's uid */
835 : : {
2591 peter@eisentraut.org 836 :UBC 0 : pg_log_error("cannot be run as root");
1488 tgl@sss.pgh.pa.us 837 : 0 : pg_log_error_hint("Please log in (using, e.g., \"su\") as the (unprivileged) user that will own the server process.");
8212 bruce@momjian.us 838 : 0 : exit(1);
839 : : }
840 : : #endif
841 : :
4521 bruce@momjian.us 842 :CBC 67 : username = get_user_name_or_exit(progname);
843 : :
844 : 67 : return pg_strdup(username);
845 : : }
846 : :
847 : : static char *
7965 peter_e@gmx.net 848 : 58 : encodingid_to_string(int enc)
849 : : {
850 : : char result[20];
851 : :
852 : 58 : sprintf(result, "%d", enc);
4963 tgl@sss.pgh.pa.us 853 : 58 : return pg_strdup(result);
854 : : }
855 : :
856 : : /*
857 : : * get the encoding id for a given encoding name
858 : : */
859 : : static int
3108 peter_e@gmx.net 860 : 16 : get_encoding_id(const char *encoding_name)
861 : : {
862 : : int enc;
863 : :
8212 bruce@momjian.us 864 [ + - + - ]: 16 : if (encoding_name && *encoding_name)
865 : : {
6977 866 [ + - ]: 16 : if ((enc = pg_valid_server_encoding(encoding_name)) >= 0)
3170 peter_e@gmx.net 867 : 16 : return enc;
868 : : }
1488 tgl@sss.pgh.pa.us 869 [ # # ]:UBC 0 : pg_fatal("\"%s\" is not a valid server encoding name",
870 : : encoding_name ? encoding_name : "(null)");
871 : : }
872 : :
873 : : /*
874 : : * Support for determining the best default text search configuration.
875 : : * We key this off the first part of LC_CTYPE (ie, the language name).
876 : : */
877 : : struct tsearch_config_match
878 : : {
879 : : const char *tsconfname;
880 : : const char *langname;
881 : : };
882 : :
883 : : static const struct tsearch_config_match tsearch_config_languages[] =
884 : : {
885 : : {"arabic", "ar"},
886 : : {"arabic", "Arabic"},
887 : : {"armenian", "hy"},
888 : : {"armenian", "Armenian"},
889 : : {"basque", "eu"},
890 : : {"basque", "Basque"},
891 : : {"catalan", "ca"},
892 : : {"catalan", "Catalan"},
893 : : {"danish", "da"},
894 : : {"danish", "Danish"},
895 : : {"dutch", "nl"},
896 : : {"dutch", "Dutch"},
897 : : {"english", "C"},
898 : : {"english", "POSIX"},
899 : : {"english", "en"},
900 : : {"english", "English"},
901 : : {"estonian", "et"},
902 : : {"estonian", "Estonian"},
903 : : {"finnish", "fi"},
904 : : {"finnish", "Finnish"},
905 : : {"french", "fr"},
906 : : {"french", "French"},
907 : : {"german", "de"},
908 : : {"german", "German"},
909 : : {"greek", "el"},
910 : : {"greek", "Greek"},
911 : : {"hindi", "hi"},
912 : : {"hindi", "Hindi"},
913 : : {"hungarian", "hu"},
914 : : {"hungarian", "Hungarian"},
915 : : {"indonesian", "id"},
916 : : {"indonesian", "Indonesian"},
917 : : {"irish", "ga"},
918 : : {"irish", "Irish"},
919 : : {"italian", "it"},
920 : : {"italian", "Italian"},
921 : : {"lithuanian", "lt"},
922 : : {"lithuanian", "Lithuanian"},
923 : : {"nepali", "ne"},
924 : : {"nepali", "Nepali"},
925 : : {"norwegian", "no"},
926 : : {"norwegian", "Norwegian"},
927 : : {"polish", "pl"},
928 : : {"polish", "Polish"},
929 : : {"portuguese", "pt"},
930 : : {"portuguese", "Portuguese"},
931 : : {"romanian", "ro"},
932 : : {"russian", "ru"},
933 : : {"russian", "Russian"},
934 : : {"serbian", "sr"},
935 : : {"serbian", "Serbian"},
936 : : {"spanish", "es"},
937 : : {"spanish", "Spanish"},
938 : : {"swedish", "sv"},
939 : : {"swedish", "Swedish"},
940 : : {"tamil", "ta"},
941 : : {"tamil", "Tamil"},
942 : : {"turkish", "tr"},
943 : : {"turkish", "Turkish"},
944 : : {"yiddish", "yi"},
945 : : {"yiddish", "Yiddish"},
946 : : {NULL, NULL} /* end marker */
947 : : };
948 : :
949 : : /*
950 : : * Look for a text search configuration matching lc_ctype, and return its
951 : : * name; return NULL if no match.
952 : : */
953 : : static const char *
6832 tgl@sss.pgh.pa.us 954 :CBC 61 : find_matching_ts_config(const char *lc_type)
955 : : {
956 : : int i;
957 : : char *langname,
958 : : *ptr;
959 : :
960 : : /*
961 : : * Convert lc_ctype to a language name by stripping everything after an
962 : : * underscore (usual case) or a hyphen (Windows "locale name"; see
963 : : * comments at IsoLocaleName()).
964 : : *
965 : : * XXX Should ' ' be a stop character? This would select "norwegian" for
966 : : * the Windows locale "Norwegian (Nynorsk)_Norway.1252". If we do so, we
967 : : * should also accept the "nn" and "nb" Unix locales.
968 : : *
969 : : * Just for paranoia, we also stop at '.' or '@'.
970 : : */
971 [ - + ]: 61 : if (lc_type == NULL)
4963 tgl@sss.pgh.pa.us 972 :UBC 0 : langname = pg_strdup("");
973 : : else
974 : : {
4963 tgl@sss.pgh.pa.us 975 :CBC 61 : ptr = langname = pg_strdup(lc_type);
4836 andrew@dunslane.net 976 : 61 : while (*ptr &&
977 [ + + + - : 122 : *ptr != '_' && *ptr != '-' && *ptr != '.' && *ptr != '@')
+ - + + +
- ]
6832 tgl@sss.pgh.pa.us 978 : 61 : ptr++;
979 : 61 : *ptr = '\0';
980 : : }
981 : :
982 [ + - ]: 793 : for (i = 0; tsearch_config_languages[i].tsconfname; i++)
983 : : {
984 [ + + ]: 793 : if (pg_strcasecmp(tsearch_config_languages[i].langname, langname) == 0)
985 : : {
986 : 61 : free(langname);
987 : 61 : return tsearch_config_languages[i].tsconfname;
988 : : }
989 : : }
990 : :
6832 tgl@sss.pgh.pa.us 991 :UBC 0 : free(langname);
992 : 0 : return NULL;
993 : : }
994 : :
995 : :
996 : : /*
997 : : * set name of given input file variable under data directory
998 : : */
999 : : static void
3108 peter_e@gmx.net 1000 :CBC 726 : set_input(char **dest, const char *filename)
1001 : : {
4578 tgl@sss.pgh.pa.us 1002 : 726 : *dest = psprintf("%s/%s", share_path, filename);
8212 bruce@momjian.us 1003 : 726 : }
1004 : :
1005 : : /*
1006 : : * check that given input file exists
1007 : : */
1008 : : static void
1009 : 726 : check_input(char *path)
1010 : : {
1011 : : struct stat statbuf;
1012 : :
7034 tgl@sss.pgh.pa.us 1013 [ - + ]: 726 : if (stat(path, &statbuf) != 0)
1014 : : {
7034 tgl@sss.pgh.pa.us 1015 [ # # ]:UBC 0 : if (errno == ENOENT)
1016 : : {
2591 peter@eisentraut.org 1017 : 0 : pg_log_error("file \"%s\" does not exist", path);
1488 tgl@sss.pgh.pa.us 1018 : 0 : pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
1019 : : }
1020 : : else
1021 : : {
2591 peter@eisentraut.org 1022 : 0 : pg_log_error("could not access file \"%s\": %m", path);
1488 tgl@sss.pgh.pa.us 1023 : 0 : pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
1024 : : }
7034 1025 : 0 : exit(1);
1026 : : }
7034 tgl@sss.pgh.pa.us 1027 [ - + ]:CBC 726 : if (!S_ISREG(statbuf.st_mode))
1028 : : {
2591 peter@eisentraut.org 1029 :UBC 0 : pg_log_error("file \"%s\" is not a regular file", path);
1488 tgl@sss.pgh.pa.us 1030 : 0 : pg_log_error_hint("This might mean you have a corrupted installation or identified the wrong directory with the invocation option -L.");
8212 bruce@momjian.us 1031 : 0 : exit(1);
1032 : : }
8212 bruce@momjian.us 1033 :CBC 726 : }
1034 : :
1035 : : /*
1036 : : * write out the PG_VERSION file in the data dir, or its subdirectory
1037 : : * if extrapath is not NULL
1038 : : */
1039 : : static void
3108 peter_e@gmx.net 1040 : 115 : write_version_file(const char *extrapath)
1041 : : {
1042 : : FILE *version_file;
1043 : : char *path;
1044 : :
8212 bruce@momjian.us 1045 [ + + ]: 115 : if (extrapath == NULL)
4578 tgl@sss.pgh.pa.us 1046 : 58 : path = psprintf("%s/PG_VERSION", pg_data);
1047 : : else
1048 : 57 : path = psprintf("%s/%s/PG_VERSION", pg_data, extrapath);
1049 : :
5963 bruce@momjian.us 1050 [ - + ]: 115 : if ((version_file = fopen(path, PG_BINARY_W)) == NULL)
1488 tgl@sss.pgh.pa.us 1051 :UBC 0 : pg_fatal("could not open file \"%s\" for writing: %m", path);
5963 bruce@momjian.us 1052 [ + - - + ]:CBC 230 : if (fprintf(version_file, "%s\n", PG_MAJORVERSION) < 0 ||
7827 tgl@sss.pgh.pa.us 1053 : 115 : fclose(version_file))
1488 tgl@sss.pgh.pa.us 1054 :UBC 0 : pg_fatal("could not write file \"%s\": %m", path);
7198 meskes@postgresql.or 1055 :CBC 115 : free(path);
8212 bruce@momjian.us 1056 : 115 : }
1057 : :
1058 : : /*
1059 : : * set up an empty config file so we can check config settings by launching
1060 : : * a test backend
1061 : : */
1062 : : static void
1063 : 58 : set_null_conf(void)
1064 : : {
1065 : : FILE *conf_file;
1066 : : char *path;
1067 : :
4578 tgl@sss.pgh.pa.us 1068 : 58 : path = psprintf("%s/postgresql.conf", pg_data);
8212 bruce@momjian.us 1069 : 58 : conf_file = fopen(path, PG_BINARY_W);
7827 tgl@sss.pgh.pa.us 1070 [ - + ]: 58 : if (conf_file == NULL)
1488 tgl@sss.pgh.pa.us 1071 :UBC 0 : pg_fatal("could not open file \"%s\" for writing: %m", path);
7827 tgl@sss.pgh.pa.us 1072 [ - + ]:CBC 58 : if (fclose(conf_file))
1488 tgl@sss.pgh.pa.us 1073 :UBC 0 : pg_fatal("could not write file \"%s\": %m", path);
7198 meskes@postgresql.or 1074 :CBC 58 : free(path);
8212 bruce@momjian.us 1075 : 58 : }
1076 : :
1077 : : /*
1078 : : * Determine which dynamic shared memory implementation should be used on
1079 : : * this platform. POSIX shared memory is preferable because the default
1080 : : * allocation limits are much higher than the limits for System V on most
1081 : : * systems that support both, but the fact that a platform has shm_open
1082 : : * doesn't guarantee that that call will succeed when attempted. So, we
1083 : : * attempt to reproduce what the postmaster will do when allocating a POSIX
1084 : : * segment in dsm_impl.c; if it doesn't work, we assume it won't work for
1085 : : * the postmaster either, and configure the cluster for System V shared
1086 : : * memory instead.
1087 : : *
1088 : : * We avoid choosing Solaris's implementation of shm_open() by default. It
1089 : : * can sleep and fail spuriously under contention.
1090 : : */
1091 : : static const char *
4590 rhaas@postgresql.org 1092 : 58 : choose_dsm_implementation(void)
1093 : : {
1094 : : #if defined(HAVE_SHM_OPEN) && !defined(__sun__)
4382 bruce@momjian.us 1095 : 58 : int ntries = 10;
1096 : : pg_prng_state prng_state;
1097 : :
1098 : : /* Initialize prng; this function is its only user in this program. */
1619 tgl@sss.pgh.pa.us 1099 : 58 : pg_prng_seed(&prng_state, (uint64) (getpid() ^ time(NULL)));
1100 : :
4590 rhaas@postgresql.org 1101 [ + - ]: 58 : while (ntries > 0)
1102 : : {
1103 : : uint32 handle;
1104 : : char name[64];
1105 : : int fd;
1106 : :
1619 tgl@sss.pgh.pa.us 1107 : 58 : handle = pg_prng_uint32(&prng_state);
4590 rhaas@postgresql.org 1108 : 58 : snprintf(name, 64, "/PostgreSQL.%u", handle);
1109 [ + - ]: 58 : if ((fd = shm_open(name, O_CREAT | O_RDWR | O_EXCL, 0600)) != -1)
1110 : : {
1111 : 58 : close(fd);
1112 : 58 : shm_unlink(name);
1113 : 58 : return "posix";
1114 : : }
4590 rhaas@postgresql.org 1115 [ # # ]:UBC 0 : if (errno != EEXIST)
1116 : 0 : break;
1117 : 0 : --ntries;
1118 : : }
1119 : : #endif
1120 : :
1121 : : #ifdef WIN32
1122 : : return "windows";
1123 : : #else
1124 : 0 : return "sysv";
1125 : : #endif
1126 : : }
1127 : :
1128 : : /*
1129 : : * Determine platform-specific config settings
1130 : : *
1131 : : * Use reasonable values if kernel will let us, else scale back.
1132 : : */
1133 : : static void
7430 tgl@sss.pgh.pa.us 1134 :CBC 58 : test_config_settings(void)
1135 : : {
1136 : : /*
1137 : : * This macro defines the minimum shared_buffers we want for a given
1138 : : * max_connections value. The arrays show the settings to try.
1139 : : */
1140 : : #define MIN_BUFS_FOR_CONNS(nconns) ((nconns) * 10)
1141 : :
1142 : : /*
1143 : : * This macro defines the default value of autovacuum_worker_slots we want
1144 : : * for a given max_connections value. Note that it has been carefully
1145 : : * crafted to provide specific values for the associated values in
1146 : : * trial_conns. We want it to return autovacuum_worker_slots's initial
1147 : : * default value (16) for the maximum value in trial_conns[] (100), while
1148 : : * it mustn't return less than the default value of autovacuum_max_workers
1149 : : * (3) for the minimum value in trial_conns[].
1150 : : */
1151 : : #define AV_SLOTS_FOR_CONNS(nconns) ((nconns) / 6)
1152 : :
1153 : : static const int trial_conns[] = {
1154 : : 100, 50, 40, 30, 20
1155 : : };
1156 : : static const int trial_bufs[] = {
1157 : : 16384, 8192, 4096, 3584, 3072, 2560, 2048, 1536,
1158 : : 1000, 900, 800, 700, 600, 500,
1159 : : 400, 300, 200, 100, 50
1160 : : };
1161 : :
7153 bruce@momjian.us 1162 : 58 : const int connslen = sizeof(trial_conns) / sizeof(int);
1163 : 58 : const int bufslen = sizeof(trial_bufs) / sizeof(int);
1164 : : int i,
1165 : : test_conns,
1166 : : test_buffs,
1167 : 58 : ok_buffers = 0;
1168 : :
1169 : : /*
1170 : : * Need to determine working DSM implementation first so that subsequent
1171 : : * tests don't fail because DSM setting doesn't work.
1172 : : */
2856 peter_e@gmx.net 1173 : 58 : printf(_("selecting dynamic shared memory implementation ... "));
1174 : 58 : fflush(stdout);
1175 : 58 : dynamic_shared_memory_type = choose_dsm_implementation();
1176 : 58 : printf("%s\n", dynamic_shared_memory_type);
1177 : :
1178 : : /*
1179 : : * Probe for max_connections before shared_buffers, since it is subject to
1180 : : * more constraints than shared_buffers. We also choose the default
1181 : : * autovacuum_worker_slots here.
1182 : : */
718 peter@eisentraut.org 1183 : 58 : printf(_("selecting default \"max_connections\" ... "));
8209 tgl@sss.pgh.pa.us 1184 : 58 : fflush(stdout);
1185 : :
7430 1186 [ + + ]: 63 : for (i = 0; i < connslen; i++)
1187 : : {
1188 : 62 : test_conns = trial_conns[i];
483 nathan@postgresql.or 1189 : 62 : n_av_slots = AV_SLOTS_FOR_CONNS(test_conns);
7430 tgl@sss.pgh.pa.us 1190 : 62 : test_buffs = MIN_BUFS_FOR_CONNS(test_conns);
1191 : :
483 nathan@postgresql.or 1192 [ + + ]: 62 : if (test_specific_config_settings(test_conns, n_av_slots, test_buffs))
1193 : : {
7428 andrew@dunslane.net 1194 : 57 : ok_buffers = test_buffs;
8212 bruce@momjian.us 1195 : 57 : break;
1196 : : }
1197 : : }
7430 tgl@sss.pgh.pa.us 1198 [ + + ]: 58 : if (i >= connslen)
1199 : 1 : i = connslen - 1;
1200 : 58 : n_connections = trial_conns[i];
1201 : :
8209 1202 : 58 : printf("%d\n", n_connections);
1203 : :
718 peter@eisentraut.org 1204 : 58 : printf(_("selecting default \"shared_buffers\" ... "));
8209 tgl@sss.pgh.pa.us 1205 : 58 : fflush(stdout);
1206 : :
7430 1207 [ + + ]: 77 : for (i = 0; i < bufslen; i++)
1208 : : {
1209 : : /* Use same amount of memory, independent of BLCKSZ */
7014 bruce@momjian.us 1210 : 76 : test_buffs = (trial_bufs[i] * 8192) / BLCKSZ;
7428 andrew@dunslane.net 1211 [ - + ]: 76 : if (test_buffs <= ok_buffers)
1212 : : {
7428 andrew@dunslane.net 1213 :UBC 0 : test_buffs = ok_buffers;
1214 : 0 : break;
1215 : : }
1216 : :
483 nathan@postgresql.or 1217 [ + + ]:CBC 76 : if (test_specific_config_settings(n_connections, n_av_slots, test_buffs))
8212 bruce@momjian.us 1218 : 57 : break;
1219 : : }
7428 andrew@dunslane.net 1220 : 58 : n_buffers = test_buffs;
1221 : :
6746 bruce@momjian.us 1222 [ + + ]: 58 : if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
6426 heikki.linnakangas@i 1223 : 57 : printf("%dMB\n", (n_buffers * (BLCKSZ / 1024)) / 1024);
1224 : : else
1225 : 1 : printf("%dkB\n", n_buffers * (BLCKSZ / 1024));
1226 : :
2492 peter@eisentraut.org 1227 : 58 : printf(_("selecting default time zone ... "));
2791 tgl@sss.pgh.pa.us 1228 : 58 : fflush(stdout);
1229 : 58 : default_timezone = select_default_timezone(share_path);
1230 [ + - ]: 58 : printf("%s\n", default_timezone ? default_timezone : "GMT");
8212 bruce@momjian.us 1231 : 58 : }
1232 : :
1233 : : /*
1234 : : * Test a specific combination of configuration settings.
1235 : : */
1236 : : static bool
483 nathan@postgresql.or 1237 : 138 : test_specific_config_settings(int test_conns, int test_av_slots, int test_buffs)
1238 : : {
1239 : : PQExpBufferData cmd;
1240 : : _stringlist *gnames,
1241 : : *gvalues;
1242 : : int status;
1243 : :
1035 peter@eisentraut.org 1244 : 138 : initPQExpBuffer(&cmd);
1245 : :
1246 : : /* Set up the test postmaster invocation */
1247 : 138 : printfPQExpBuffer(&cmd,
1248 : : "\"%s\" --check %s %s "
1249 : : "-c max_connections=%d "
1250 : : "-c autovacuum_worker_slots=%d "
1251 : : "-c shared_buffers=%d "
1252 : : "-c dynamic_shared_memory_type=%s",
1253 : : backend_exec, boot_options, extra_options,
1254 : : test_conns, test_av_slots, test_buffs,
1255 : : dynamic_shared_memory_type);
1256 : :
1257 : : /* Add any user-given setting overrides */
1140 tgl@sss.pgh.pa.us 1258 : 138 : for (gnames = extra_guc_names, gvalues = extra_guc_values;
1259 [ + + ]: 178 : gnames != NULL; /* assume lists have the same length */
1260 : 40 : gnames = gnames->next, gvalues = gvalues->next)
1261 : : {
1035 peter@eisentraut.org 1262 : 40 : appendPQExpBuffer(&cmd, " -c %s=", gnames->str);
1263 : 40 : appendShellString(&cmd, gvalues->str);
1264 : : }
1265 : :
1266 : 138 : appendPQExpBuffer(&cmd,
1267 : : " < \"%s\" > \"%s\" 2>&1",
1268 : : DEVNULL, DEVNULL);
1269 : :
1140 tgl@sss.pgh.pa.us 1270 : 138 : fflush(NULL);
1035 peter@eisentraut.org 1271 : 138 : status = system(cmd.data);
1272 : :
1273 : 138 : termPQExpBuffer(&cmd);
1274 : :
1140 tgl@sss.pgh.pa.us 1275 : 138 : return (status == 0);
1276 : : }
1277 : :
1278 : : /*
1279 : : * Calculate the default wal_size with a "pretty" unit.
1280 : : */
1281 : : static char *
3150 andres@anarazel.de 1282 : 116 : pretty_wal_size(int segment_count)
1283 : : {
1284 : 116 : int sz = wal_segment_size_mb * segment_count;
2973 peter_e@gmx.net 1285 : 116 : char *result = pg_malloc(14);
1286 : :
3150 andres@anarazel.de 1287 [ + + ]: 116 : if ((sz % 1024) == 0)
2973 peter_e@gmx.net 1288 : 52 : snprintf(result, 14, "%dGB", sz / 1024);
1289 : : else
1290 : 64 : snprintf(result, 14, "%dMB", sz);
1291 : :
3150 andres@anarazel.de 1292 : 116 : return result;
1293 : : }
1294 : :
1295 : : /*
1296 : : * set up all the config files
1297 : : */
1298 : : static void
8212 bruce@momjian.us 1299 : 58 : setup_config(void)
1300 : : {
1301 : : char **conflines;
1302 : : char repltok[MAXPGPATH];
1303 : : char path[MAXPGPATH];
1304 : : _stringlist *gnames,
1305 : : *gvalues;
1306 : :
8199 peter_e@gmx.net 1307 : 58 : fputs(_("creating configuration files ... "), stdout);
8209 tgl@sss.pgh.pa.us 1308 : 58 : fflush(stdout);
1309 : :
1310 : : /* postgresql.conf */
1311 : :
8212 bruce@momjian.us 1312 : 58 : conflines = readfile(conf_file);
1313 : :
1140 tgl@sss.pgh.pa.us 1314 : 58 : snprintf(repltok, sizeof(repltok), "%d", n_connections);
1315 : 58 : conflines = replace_guc_value(conflines, "max_connections",
1316 : : repltok, false);
1317 : :
483 nathan@postgresql.or 1318 : 58 : snprintf(repltok, sizeof(repltok), "%d", n_av_slots);
1319 : 58 : conflines = replace_guc_value(conflines, "autovacuum_worker_slots",
1320 : : repltok, false);
1321 : :
6746 bruce@momjian.us 1322 [ + + ]: 58 : if ((n_buffers * (BLCKSZ / 1024)) % 1024 == 0)
1140 tgl@sss.pgh.pa.us 1323 : 57 : snprintf(repltok, sizeof(repltok), "%dMB",
6746 bruce@momjian.us 1324 : 57 : (n_buffers * (BLCKSZ / 1024)) / 1024);
1325 : : else
1140 tgl@sss.pgh.pa.us 1326 : 1 : snprintf(repltok, sizeof(repltok), "%dkB",
1327 : : n_buffers * (BLCKSZ / 1024));
1328 : 58 : conflines = replace_guc_value(conflines, "shared_buffers",
1329 : : repltok, false);
1330 : :
846 1331 : 58 : conflines = replace_guc_value(conflines, "lc_messages",
1332 : : lc_messages, false);
1333 : :
1334 : 58 : conflines = replace_guc_value(conflines, "lc_monetary",
1335 : : lc_monetary, false);
1336 : :
1337 : 58 : conflines = replace_guc_value(conflines, "lc_numeric",
1338 : : lc_numeric, false);
1339 : :
1340 : 58 : conflines = replace_guc_value(conflines, "lc_time",
1341 : : lc_time, false);
1342 : :
7153 bruce@momjian.us 1343 [ - - + ]: 58 : switch (locale_date_order(lc_time))
1344 : : {
7452 peter_e@gmx.net 1345 :UBC 0 : case DATEORDER_YMD:
1140 tgl@sss.pgh.pa.us 1346 : 0 : strcpy(repltok, "iso, ymd");
7452 peter_e@gmx.net 1347 : 0 : break;
1348 : 0 : case DATEORDER_DMY:
1140 tgl@sss.pgh.pa.us 1349 : 0 : strcpy(repltok, "iso, dmy");
7452 peter_e@gmx.net 1350 : 0 : break;
7452 peter_e@gmx.net 1351 :CBC 58 : case DATEORDER_MDY:
1352 : : default:
1140 tgl@sss.pgh.pa.us 1353 : 58 : strcpy(repltok, "iso, mdy");
7452 peter_e@gmx.net 1354 : 58 : break;
1355 : : }
1140 tgl@sss.pgh.pa.us 1356 : 58 : conflines = replace_guc_value(conflines, "datestyle",
1357 : : repltok, false);
1358 : :
1359 : 58 : snprintf(repltok, sizeof(repltok), "pg_catalog.%s",
1360 : : default_text_search_config);
1361 : 58 : conflines = replace_guc_value(conflines, "default_text_search_config",
1362 : : repltok, false);
1363 : :
5352 1364 [ + - ]: 58 : if (default_timezone)
1365 : : {
1140 1366 : 58 : conflines = replace_guc_value(conflines, "timezone",
1367 : : default_timezone, false);
1368 : 58 : conflines = replace_guc_value(conflines, "log_timezone",
1369 : : default_timezone, false);
1370 : : }
1371 : :
1372 : 58 : conflines = replace_guc_value(conflines, "dynamic_shared_memory_type",
1373 : : dynamic_shared_memory_type, false);
1374 : :
1375 : : /* Caution: these depend on wal_segment_size_mb, they're not constants */
1376 : 58 : conflines = replace_guc_value(conflines, "min_wal_size",
1377 : 58 : pretty_wal_size(DEFAULT_MIN_WAL_SEGS), false);
1378 : :
1379 : 58 : conflines = replace_guc_value(conflines, "max_wal_size",
1380 : 58 : pretty_wal_size(DEFAULT_MAX_WAL_SEGS), false);
1381 : :
1382 : : /*
1383 : : * Fix up various entries to match the true compile-time defaults. Since
1384 : : * these are indeed defaults, keep the postgresql.conf lines commented.
1385 : : */
1386 : 58 : conflines = replace_guc_value(conflines, "unix_socket_directories",
1387 : : DEFAULT_PGSOCKET_DIR, true);
1388 : :
1389 : 58 : conflines = replace_guc_value(conflines, "port",
1390 : : DEF_PGPORT_STR, true);
1391 : :
1392 : : #if DEFAULT_BACKEND_FLUSH_AFTER > 0
1393 : : snprintf(repltok, sizeof(repltok), "%dkB",
1394 : : DEFAULT_BACKEND_FLUSH_AFTER * (BLCKSZ / 1024));
1395 : : conflines = replace_guc_value(conflines, "backend_flush_after",
1396 : : repltok, true);
1397 : : #endif
1398 : :
1399 : : #if DEFAULT_BGWRITER_FLUSH_AFTER > 0
1400 : 58 : snprintf(repltok, sizeof(repltok), "%dkB",
1401 : : DEFAULT_BGWRITER_FLUSH_AFTER * (BLCKSZ / 1024));
1402 : 58 : conflines = replace_guc_value(conflines, "bgwriter_flush_after",
1403 : : repltok, true);
1404 : : #endif
1405 : :
1406 : : #if DEFAULT_CHECKPOINT_FLUSH_AFTER > 0
1407 : 58 : snprintf(repltok, sizeof(repltok), "%dkB",
1408 : : DEFAULT_CHECKPOINT_FLUSH_AFTER * (BLCKSZ / 1024));
1409 : 58 : conflines = replace_guc_value(conflines, "checkpoint_flush_after",
1410 : : repltok, true);
1411 : : #endif
1412 : :
1413 : : #ifdef WIN32
1414 : : conflines = replace_guc_value(conflines, "update_process_title",
1415 : : "off", true);
1416 : : #endif
1417 : :
1418 : : /*
1419 : : * Change password_encryption setting to md5 if md5 was chosen as an
1420 : : * authentication method, unless scram-sha-256 was also chosen.
1421 : : */
2155 peter@eisentraut.org 1422 [ - + ]: 58 : if ((strcmp(authmethodlocal, "md5") == 0 &&
2155 peter@eisentraut.org 1423 [ # # ]:UBC 0 : strcmp(authmethodhost, "scram-sha-256") != 0) ||
2155 peter@eisentraut.org 1424 [ - + ]:CBC 58 : (strcmp(authmethodhost, "md5") == 0 &&
2155 peter@eisentraut.org 1425 [ # # ]:UBC 0 : strcmp(authmethodlocal, "scram-sha-256") != 0))
1426 : : {
1140 tgl@sss.pgh.pa.us 1427 : 0 : conflines = replace_guc_value(conflines, "password_encryption",
1428 : : "md5", false);
1429 : : }
1430 : :
1431 : : /*
1432 : : * If group access has been enabled for the cluster then it makes sense to
1433 : : * ensure that the log files also allow group access. Otherwise a backup
1434 : : * from a user in the group would fail if the log files were not
1435 : : * relocated.
1436 : : */
2950 sfrost@snowman.net 1437 [ + + ]:CBC 58 : if (pg_dir_create_mode == PG_DIR_MODE_GROUP)
1438 : : {
1140 tgl@sss.pgh.pa.us 1439 : 5 : conflines = replace_guc_value(conflines, "log_file_mode",
1440 : : "0640", false);
1441 : : }
1442 : :
1443 : : #if USE_LZ4
61 michael@paquier.xyz 1444 :GNC 58 : conflines = replace_guc_value(conflines, "default_toast_compression",
1445 : : "lz4", true);
1446 : : #endif
1447 : :
1448 : : /*
1449 : : * Now replace anything that's overridden via -c switches.
1450 : : */
1140 tgl@sss.pgh.pa.us 1451 :CBC 58 : for (gnames = extra_guc_names, gvalues = extra_guc_values;
1452 [ + + ]: 67 : gnames != NULL; /* assume lists have the same length */
1453 : 9 : gnames = gnames->next, gvalues = gvalues->next)
1454 : : {
1455 : 9 : conflines = replace_guc_value(conflines, gnames->str,
1456 : 9 : gvalues->str, false);
1457 : : }
1458 : :
1459 : : /* ... and write out the finished postgresql.conf file */
8209 1460 : 58 : snprintf(path, sizeof(path), "%s/postgresql.conf", pg_data);
1461 : :
8212 bruce@momjian.us 1462 : 58 : writefile(path, conflines);
2950 sfrost@snowman.net 1463 [ - + ]: 58 : if (chmod(path, pg_file_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 1464 :UBC 0 : pg_fatal("could not change permissions of \"%s\": %m", path);
1465 : :
1466 : :
1467 : : /* postgresql.auto.conf */
1468 : :
1140 tgl@sss.pgh.pa.us 1469 :CBC 58 : conflines = pg_malloc_array(char *, 3);
1470 : 58 : conflines[0] = pg_strdup("# Do not edit this file manually!\n");
1471 : 58 : conflines[1] = pg_strdup("# It will be overwritten by the ALTER SYSTEM command.\n");
1472 : 58 : conflines[2] = NULL;
1473 : :
4171 peter_e@gmx.net 1474 : 58 : sprintf(path, "%s/postgresql.auto.conf", pg_data);
1475 : :
1140 tgl@sss.pgh.pa.us 1476 : 58 : writefile(path, conflines);
2950 sfrost@snowman.net 1477 [ - + ]: 58 : if (chmod(path, pg_file_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 1478 :UBC 0 : pg_fatal("could not change permissions of \"%s\": %m", path);
1479 : :
1480 : :
1481 : : /* pg_hba.conf */
1482 : :
8212 bruce@momjian.us 1483 :CBC 58 : conflines = readfile(hba_file);
1484 : :
1485 : : /*
1486 : : * Probe to see if there is really any platform support for IPv6, and
1487 : : * comment out the relevant pg_hba line if not. This avoids runtime
1488 : : * warnings if getaddrinfo doesn't actually cope with IPv6. Particularly
1489 : : * useful on Windows, where executables built on a machine with IPv6 may
1490 : : * have to run on a machine without.
1491 : : */
1492 : : {
1493 : : struct addrinfo *gai_result;
1494 : : struct addrinfo hints;
7507 1495 : 58 : int err = 0;
1496 : :
1497 : : #ifdef WIN32
1498 : : /* need to call WSAStartup before calling getaddrinfo */
1499 : : WSADATA wsaData;
1500 : :
1501 : : err = WSAStartup(MAKEWORD(2, 2), &wsaData);
1502 : : #endif
1503 : :
1504 : : /* for best results, this code should match parse_hba_line() */
7561 tgl@sss.pgh.pa.us 1505 : 58 : hints.ai_flags = AI_NUMERICHOST;
4402 1506 : 58 : hints.ai_family = AF_UNSPEC;
7561 1507 : 58 : hints.ai_socktype = 0;
1508 : 58 : hints.ai_protocol = 0;
1509 : 58 : hints.ai_addrlen = 0;
1510 : 58 : hints.ai_canonname = NULL;
1511 : 58 : hints.ai_addr = NULL;
1512 : 58 : hints.ai_next = NULL;
1513 : :
7556 1514 [ + - - + ]: 116 : if (err != 0 ||
1515 : 58 : getaddrinfo("::1", NULL, &hints, &gai_result) != 0)
1516 : : {
7561 tgl@sss.pgh.pa.us 1517 :UBC 0 : conflines = replace_token(conflines,
1518 : : "host all all ::1/128",
1519 : : "#host all all ::1/128");
3343 1520 : 0 : conflines = replace_token(conflines,
1521 : : "host replication all ::1/128",
1522 : : "#host replication all ::1/128");
1523 : : }
1524 : : }
1525 : :
1526 : : /* Replace default authentication methods */
7947 bruce@momjian.us 1527 :CBC 58 : conflines = replace_token(conflines,
1528 : : "@authmethodhost@",
1529 : : authmethodhost);
5526 magnus@hagander.net 1530 : 58 : conflines = replace_token(conflines,
1531 : : "@authmethodlocal@",
1532 : : authmethodlocal);
1533 : :
7947 bruce@momjian.us 1534 : 58 : conflines = replace_token(conflines,
1535 : : "@authcomment@",
5207 peter_e@gmx.net 1536 [ - + - - ]: 58 : (strcmp(authmethodlocal, "trust") == 0 || strcmp(authmethodhost, "trust") == 0) ? AUTHTRUST_WARNING : "");
1537 : :
8209 tgl@sss.pgh.pa.us 1538 : 58 : snprintf(path, sizeof(path), "%s/pg_hba.conf", pg_data);
1539 : :
8212 bruce@momjian.us 1540 : 58 : writefile(path, conflines);
2950 sfrost@snowman.net 1541 [ - + ]: 58 : if (chmod(path, pg_file_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 1542 :UBC 0 : pg_fatal("could not change permissions of \"%s\": %m", path);
1543 : :
1544 : :
1545 : : /* pg_ident.conf */
1546 : :
8212 bruce@momjian.us 1547 :CBC 58 : conflines = readfile(ident_file);
1548 : :
8209 tgl@sss.pgh.pa.us 1549 : 58 : snprintf(path, sizeof(path), "%s/pg_ident.conf", pg_data);
1550 : :
48 dgustafsson@postgres 1551 :GNC 58 : writefile(path, conflines);
1552 [ - + ]: 58 : if (chmod(path, pg_file_create_mode) != 0)
48 dgustafsson@postgres 1553 :UNC 0 : pg_fatal("could not change permissions of \"%s\": %m", path);
1554 : :
1555 : : /* pg_hosts.conf */
48 dgustafsson@postgres 1556 :GNC 58 : conflines = readfile(hosts_file);
1557 : 58 : snprintf(path, sizeof(path), "%s/pg_hosts.conf", pg_data);
1558 : :
8212 bruce@momjian.us 1559 :CBC 58 : writefile(path, conflines);
2950 sfrost@snowman.net 1560 [ - + ]: 58 : if (chmod(path, pg_file_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 1561 :UBC 0 : pg_fatal("could not change permissions of \"%s\": %m", path);
1562 : :
8212 bruce@momjian.us 1563 :CBC 58 : check_ok();
1564 : 58 : }
1565 : :
1566 : :
1567 : : /*
1568 : : * run the BKI script in bootstrap mode to create template1
1569 : : */
1570 : : static void
5963 1571 : 58 : bootstrap_template1(void)
1572 : : {
1573 : : PG_CMD_DECL;
1574 : : PQExpBufferData cmd;
1575 : : char **line;
1576 : : char **bki_lines;
1577 : : char headerline[MAXPGPATH];
1578 : : char buf[64];
1579 : :
3792 tgl@sss.pgh.pa.us 1580 : 58 : printf(_("running bootstrap script ... "));
8209 1581 : 58 : fflush(stdout);
1582 : :
8212 bruce@momjian.us 1583 : 58 : bki_lines = readfile(bki_file);
1584 : :
1585 : : /* Check that bki file appears to be of the right version */
1586 : :
8209 tgl@sss.pgh.pa.us 1587 : 58 : snprintf(headerline, sizeof(headerline), "# PostgreSQL %s\n",
1588 : : PG_MAJORVERSION);
1589 : :
8212 bruce@momjian.us 1590 [ - + ]: 58 : if (strcmp(headerline, *bki_lines) != 0)
1591 : : {
2591 peter@eisentraut.org 1592 :UBC 0 : pg_log_error("input file \"%s\" does not belong to PostgreSQL %s",
1593 : : bki_file, PG_VERSION);
1488 tgl@sss.pgh.pa.us 1594 : 0 : pg_log_error_hint("Specify the correct path using the option -L.");
2684 peter@eisentraut.org 1595 : 0 : exit(1);
1596 : : }
1597 : :
1598 : : /* Substitute for various symbols used in the BKI file */
1599 : :
6499 tgl@sss.pgh.pa.us 1600 :CBC 58 : sprintf(buf, "%d", NAMEDATALEN);
1601 : 58 : bki_lines = replace_token(bki_lines, "NAMEDATALEN", buf);
1602 : :
6381 1603 : 58 : sprintf(buf, "%d", (int) sizeof(Pointer));
1604 : 58 : bki_lines = replace_token(bki_lines, "SIZEOF_POINTER", buf);
1605 : :
1606 : 58 : bki_lines = replace_token(bki_lines, "ALIGNOF_POINTER",
1607 : : (sizeof(Pointer) == 4) ? "i" : "d");
1608 : :
2940 1609 : 58 : bki_lines = replace_token(bki_lines, "POSTGRES",
1610 : 58 : escape_quotes_bki(username));
1611 : :
1612 : 58 : bki_lines = replace_token(bki_lines, "ENCODING",
1613 : 58 : encodingid_to_string(encodingid));
1614 : :
1615 : 58 : bki_lines = replace_token(bki_lines, "LC_COLLATE",
1616 : 58 : escape_quotes_bki(lc_collate));
1617 : :
1618 : 58 : bki_lines = replace_token(bki_lines, "LC_CTYPE",
1619 : 58 : escape_quotes_bki(lc_ctype));
1620 : :
787 jdavis@postgresql.or 1621 : 58 : bki_lines = replace_token(bki_lines, "DATLOCALE",
1622 [ + + ]: 58 : datlocale ? escape_quotes_bki(datlocale) : "_null_");
1623 : :
1154 peter@eisentraut.org 1624 : 58 : bki_lines = replace_token(bki_lines, "ICU_RULES",
1625 [ - + ]: 58 : icu_rules ? escape_quotes_bki(icu_rules) : "_null_");
1626 : :
1510 1627 : 58 : sprintf(buf, "%c", locale_provider);
1628 : 58 : bki_lines = replace_token(bki_lines, "LOCALE_PROVIDER", buf);
1629 : :
1630 : : /* Also ensure backend isn't confused by this environment var: */
8035 tgl@sss.pgh.pa.us 1631 : 58 : unsetenv("PGCLIENTENCODING");
1632 : :
1035 peter@eisentraut.org 1633 : 58 : initPQExpBuffer(&cmd);
1634 : :
1635 : 58 : printfPQExpBuffer(&cmd, "\"%s\" --boot %s %s", backend_exec, boot_options, extra_options);
1636 : 58 : appendPQExpBuffer(&cmd, " -X %d", wal_segment_size_mb * (1024 * 1024));
1637 [ + + ]: 58 : if (data_checksums)
382 drowley@postgresql.o 1638 : 46 : appendPQExpBufferStr(&cmd, " -k");
1035 peter@eisentraut.org 1639 [ - + ]: 58 : if (debug)
382 drowley@postgresql.o 1640 :UBC 0 : appendPQExpBufferStr(&cmd, " -d 5");
1641 : :
1642 : :
1035 peter@eisentraut.org 1643 [ - + ]:CBC 58 : PG_CMD_OPEN(cmd.data);
1644 : :
8212 bruce@momjian.us 1645 [ + + ]: 695246 : for (line = bki_lines; *line != NULL; line++)
1646 : : {
7827 tgl@sss.pgh.pa.us 1647 [ + - + + ]: 695188 : PG_CMD_PUTS(*line);
8212 bruce@momjian.us 1648 : 695188 : free(*line);
1649 : : }
1650 : :
1035 peter@eisentraut.org 1651 [ + + ]: 58 : PG_CMD_CLOSE();
1652 : :
1653 : 57 : termPQExpBuffer(&cmd);
8212 bruce@momjian.us 1654 : 57 : free(bki_lines);
1655 : :
1656 : 57 : check_ok();
1657 : 57 : }
1658 : :
1659 : : /*
1660 : : * set up the shadow password table
1661 : : */
1662 : : static void
3792 tgl@sss.pgh.pa.us 1663 : 57 : setup_auth(FILE *cmdfd)
1664 : : {
1665 : : /*
1666 : : * The authid table shouldn't be readable except through views, to ensure
1667 : : * passwords are not publicly visible.
1668 : : */
1247 peter@eisentraut.org 1669 [ + - - + ]: 57 : PG_CMD_PUTS("REVOKE ALL ON pg_authid FROM public;\n\n");
1670 : :
3535 tgl@sss.pgh.pa.us 1671 [ - + ]: 57 : if (superuser_password)
2462 peter@eisentraut.org 1672 [ # # # # ]:UBC 0 : PG_CMD_PRINTF("ALTER USER \"%s\" WITH PASSWORD E'%s';\n\n",
1673 : : username, escape_quotes(superuser_password));
8212 bruce@momjian.us 1674 :CBC 57 : }
1675 : :
1676 : : /*
1677 : : * get the superuser password if required
1678 : : */
1679 : : static void
3535 tgl@sss.pgh.pa.us 1680 :UBC 0 : get_su_pwd(void)
1681 : : {
1682 : : char *pwd1;
1683 : :
7985 1684 [ # # ]: 0 : if (pwprompt)
1685 : : {
1686 : : /*
1687 : : * Read password from terminal
1688 : : */
1689 : : char *pwd2;
1690 : :
3535 1691 : 0 : printf("\n");
1692 : 0 : fflush(stdout);
2070 1693 : 0 : pwd1 = simple_prompt("Enter new superuser password: ", false);
1694 : 0 : pwd2 = simple_prompt("Enter it again: ", false);
7985 1695 [ # # ]: 0 : if (strcmp(pwd1, pwd2) != 0)
1696 : : {
1697 : 0 : fprintf(stderr, _("Passwords didn't match.\n"));
2684 peter@eisentraut.org 1698 : 0 : exit(1);
1699 : : }
2070 tgl@sss.pgh.pa.us 1700 : 0 : free(pwd2);
1701 : : }
1702 : : else
1703 : : {
1704 : : /*
1705 : : * Read password from file
1706 : : *
1707 : : * Ideally this should insist that the file not be world-readable.
1708 : : * However, this option is mainly intended for use on Windows where
1709 : : * file permissions may not exist at all, so we'll skip the paranoia
1710 : : * for now.
1711 : : */
7919 bruce@momjian.us 1712 : 0 : FILE *pwf = fopen(pwfilename, "r");
1713 : :
7985 tgl@sss.pgh.pa.us 1714 [ # # ]: 0 : if (!pwf)
1488 1715 : 0 : pg_fatal("could not open file \"%s\" for reading: %m",
1716 : : pwfilename);
1630 1717 : 0 : pwd1 = pg_get_line(pwf, NULL);
2070 1718 [ # # ]: 0 : if (!pwd1)
1719 : : {
4169 heikki.linnakangas@i 1720 [ # # ]: 0 : if (ferror(pwf))
1488 tgl@sss.pgh.pa.us 1721 : 0 : pg_fatal("could not read password from file \"%s\": %m",
1722 : : pwfilename);
1723 : : else
1724 : 0 : pg_fatal("password file \"%s\" is empty",
1725 : : pwfilename);
1726 : : }
7985 1727 : 0 : fclose(pwf);
1728 : :
2070 1729 : 0 : (void) pg_strip_crlf(pwd1);
1730 : : }
1731 : :
1732 : 0 : superuser_password = pwd1;
8212 bruce@momjian.us 1733 : 0 : }
1734 : :
1735 : : /*
1736 : : * set up pg_depend
1737 : : */
1738 : : static void
3792 tgl@sss.pgh.pa.us 1739 :CBC 57 : setup_depend(FILE *cmdfd)
1740 : : {
1741 : : /*
1742 : : * Advance the OID counter so that subsequently-created objects aren't
1743 : : * pinned.
1744 : : */
1247 peter@eisentraut.org 1745 [ + - - + ]: 57 : PG_CMD_PUTS("SELECT pg_stop_making_pinned_objects();\n\n");
8212 bruce@momjian.us 1746 : 57 : }
1747 : :
1748 : : /*
1749 : : * Run external file
1750 : : */
1751 : : static void
1921 peter@eisentraut.org 1752 : 285 : setup_run_file(FILE *cmdfd, const char *filename)
1753 : : {
1754 : : char **lines;
1755 : :
1756 : 285 : lines = readfile(filename);
1757 : :
1758 [ + + ]: 392445 : for (char **line = lines; *line != NULL; line++)
1759 : : {
7827 tgl@sss.pgh.pa.us 1760 [ + - + + ]: 392160 : PG_CMD_PUTS(*line);
8212 bruce@momjian.us 1761 : 392160 : free(*line);
1762 : : }
1763 : :
2919 tgl@sss.pgh.pa.us 1764 [ + - + + ]: 285 : PG_CMD_PUTS("\n\n");
1765 : :
1921 peter@eisentraut.org 1766 : 285 : free(lines);
8212 bruce@momjian.us 1767 : 285 : }
1768 : :
1769 : : /*
1770 : : * fill in extra description data
1771 : : */
1772 : : static void
3792 tgl@sss.pgh.pa.us 1773 : 57 : setup_description(FILE *cmdfd)
1774 : : {
1775 : : /* Create default descriptions for operator implementation functions */
5542 1776 [ + - + + ]: 57 : PG_CMD_PUTS("WITH funcdescs AS ( "
1777 : : "SELECT p.oid as p_oid, o.oid as o_oid, oprname "
1778 : : "FROM pg_proc p JOIN pg_operator o ON oprcode = p.oid ) "
1779 : : "INSERT INTO pg_description "
1780 : : " SELECT p_oid, 'pg_proc'::regclass, 0, "
1781 : : " 'implementation of ' || oprname || ' operator' "
1782 : : " FROM funcdescs "
1783 : : " WHERE NOT EXISTS (SELECT 1 FROM pg_description "
1784 : : " WHERE objoid = p_oid AND classoid = 'pg_proc'::regclass) "
1785 : : " AND NOT EXISTS (SELECT 1 FROM pg_description "
1786 : : " WHERE objoid = o_oid AND classoid = 'pg_operator'::regclass"
1787 : : " AND description LIKE 'deprecated%');\n\n");
7387 bruce@momjian.us 1788 : 57 : }
1789 : :
1790 : : /*
1791 : : * populate pg_collation
1792 : : */
1793 : : static void
3792 tgl@sss.pgh.pa.us 1794 : 57 : setup_collation(FILE *cmdfd)
1795 : : {
1796 : : /*
1797 : : * Set the collation version for collations defined in pg_collation.dat,
1798 : : * but not the ones where we know that the collation behavior will never
1799 : : * change.
1800 : : */
1089 peter@eisentraut.org 1801 [ + - + + ]: 57 : PG_CMD_PUTS("UPDATE pg_collation SET collversion = pg_collation_actual_version(oid) WHERE collname = 'unicode';\n\n");
1802 : :
1803 : : /* Import all collations we can find in the operating system */
3238 tgl@sss.pgh.pa.us 1804 [ + - + + ]: 57 : PG_CMD_PUTS("SELECT pg_import_system_collations('pg_catalog');\n\n");
5565 peter_e@gmx.net 1805 : 57 : }
1806 : :
1807 : : /*
1808 : : * Set up privileges
1809 : : *
1810 : : * We mark most system catalogs as world-readable. We don't currently have
1811 : : * to touch functions, languages, or databases, because their default
1812 : : * permissions are OK.
1813 : : *
1814 : : * Some objects may require different permissions by default, so we
1815 : : * make sure we don't overwrite privilege sets that have already been
1816 : : * set (NOT NULL).
1817 : : *
1818 : : * Also populate pg_init_privs to save what the privileges are at init
1819 : : * time. This is used by pg_dump to allow users to change privileges
1820 : : * on catalog objects and to have those privilege changes preserved
1821 : : * across dump/reload and pg_upgrade.
1822 : : *
1823 : : * Note that pg_init_privs is only for per-database objects and therefore
1824 : : * we don't include databases or tablespaces.
1825 : : */
1826 : : static void
3792 tgl@sss.pgh.pa.us 1827 : 57 : setup_privileges(FILE *cmdfd)
1828 : : {
1247 peter@eisentraut.org 1829 [ + - + + ]: 57 : PG_CMD_PRINTF("UPDATE pg_class "
1830 : : " SET relacl = (SELECT array_agg(a.acl) FROM "
1831 : : " (SELECT E'=r/\"%s\"' as acl "
1832 : : " UNION SELECT unnest(pg_catalog.acldefault("
1833 : : " CASE WHEN relkind = " CppAsString2(RELKIND_SEQUENCE) " THEN 's' "
1834 : : " ELSE 'r' END::\"char\"," CppAsString2(BOOTSTRAP_SUPERUSERID) "::oid))"
1835 : : " ) as a) "
1836 : : " WHERE relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1837 : : CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1838 : : CppAsString2(RELKIND_SEQUENCE) ")"
1839 : : " AND relacl IS NULL;\n\n",
1840 : : escape_quotes(username));
1841 [ + - + + ]: 57 : PG_CMD_PUTS("GRANT USAGE ON SCHEMA pg_catalog, public TO PUBLIC;\n\n");
1842 [ + - + + ]: 57 : PG_CMD_PUTS("REVOKE ALL ON pg_largeobject FROM PUBLIC;\n\n");
1843 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1844 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1845 : : " SELECT"
1846 : : " oid,"
1847 : : " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1848 : : " 0,"
1849 : : " relacl,"
1850 : : " 'i'"
1851 : : " FROM"
1852 : : " pg_class"
1853 : : " WHERE"
1854 : : " relacl IS NOT NULL"
1855 : : " AND relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1856 : : CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1857 : : CppAsString2(RELKIND_SEQUENCE) ");\n\n");
1858 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1859 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1860 : : " SELECT"
1861 : : " pg_class.oid,"
1862 : : " (SELECT oid FROM pg_class WHERE relname = 'pg_class'),"
1863 : : " pg_attribute.attnum,"
1864 : : " pg_attribute.attacl,"
1865 : : " 'i'"
1866 : : " FROM"
1867 : : " pg_class"
1868 : : " JOIN pg_attribute ON (pg_class.oid = pg_attribute.attrelid)"
1869 : : " WHERE"
1870 : : " pg_attribute.attacl IS NOT NULL"
1871 : : " AND pg_class.relkind IN (" CppAsString2(RELKIND_RELATION) ", "
1872 : : CppAsString2(RELKIND_VIEW) ", " CppAsString2(RELKIND_MATVIEW) ", "
1873 : : CppAsString2(RELKIND_SEQUENCE) ");\n\n");
1874 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1875 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1876 : : " SELECT"
1877 : : " oid,"
1878 : : " (SELECT oid FROM pg_class WHERE relname = 'pg_proc'),"
1879 : : " 0,"
1880 : : " proacl,"
1881 : : " 'i'"
1882 : : " FROM"
1883 : : " pg_proc"
1884 : : " WHERE"
1885 : : " proacl IS NOT NULL;\n\n");
1886 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1887 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1888 : : " SELECT"
1889 : : " oid,"
1890 : : " (SELECT oid FROM pg_class WHERE relname = 'pg_type'),"
1891 : : " 0,"
1892 : : " typacl,"
1893 : : " 'i'"
1894 : : " FROM"
1895 : : " pg_type"
1896 : : " WHERE"
1897 : : " typacl IS NOT NULL;\n\n");
1898 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1899 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1900 : : " SELECT"
1901 : : " oid,"
1902 : : " (SELECT oid FROM pg_class WHERE relname = 'pg_language'),"
1903 : : " 0,"
1904 : : " lanacl,"
1905 : : " 'i'"
1906 : : " FROM"
1907 : : " pg_language"
1908 : : " WHERE"
1909 : : " lanacl IS NOT NULL;\n\n");
1910 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1911 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1912 : : " SELECT"
1913 : : " oid,"
1914 : : " (SELECT oid FROM pg_class WHERE "
1915 : : " relname = 'pg_largeobject_metadata'),"
1916 : : " 0,"
1917 : : " lomacl,"
1918 : : " 'i'"
1919 : : " FROM"
1920 : : " pg_largeobject_metadata"
1921 : : " WHERE"
1922 : : " lomacl IS NOT NULL;\n\n");
1923 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1924 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1925 : : " SELECT"
1926 : : " oid,"
1927 : : " (SELECT oid FROM pg_class WHERE relname = 'pg_namespace'),"
1928 : : " 0,"
1929 : : " nspacl,"
1930 : : " 'i'"
1931 : : " FROM"
1932 : : " pg_namespace"
1933 : : " WHERE"
1934 : : " nspacl IS NOT NULL;\n\n");
1935 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1936 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1937 : : " SELECT"
1938 : : " oid,"
1939 : : " (SELECT oid FROM pg_class WHERE "
1940 : : " relname = 'pg_foreign_data_wrapper'),"
1941 : : " 0,"
1942 : : " fdwacl,"
1943 : : " 'i'"
1944 : : " FROM"
1945 : : " pg_foreign_data_wrapper"
1946 : : " WHERE"
1947 : : " fdwacl IS NOT NULL;\n\n");
1948 [ + - + + ]: 57 : PG_CMD_PUTS("INSERT INTO pg_init_privs "
1949 : : " (objoid, classoid, objsubid, initprivs, privtype)"
1950 : : " SELECT"
1951 : : " oid,"
1952 : : " (SELECT oid FROM pg_class "
1953 : : " WHERE relname = 'pg_foreign_server'),"
1954 : : " 0,"
1955 : : " srvacl,"
1956 : : " 'i'"
1957 : : " FROM"
1958 : : " pg_foreign_server"
1959 : : " WHERE"
1960 : : " srvacl IS NOT NULL;\n\n");
8212 bruce@momjian.us 1961 : 57 : }
1962 : :
1963 : : /*
1964 : : * extract the strange version of version required for information schema
1965 : : * (09.08.0007abc)
1966 : : */
1967 : : static void
1968 : 66 : set_info_version(void)
1969 : : {
1970 : : char *letterversion;
1971 : 66 : long major = 0,
1972 : 66 : minor = 0,
1973 : 66 : micro = 0;
1974 : : char *endptr;
4963 tgl@sss.pgh.pa.us 1975 : 66 : char *vstr = pg_strdup(PG_VERSION);
1976 : : char *ptr;
1977 : :
8212 bruce@momjian.us 1978 : 66 : ptr = vstr + (strlen(vstr) - 1);
1979 [ + - - + : 396 : while (ptr != vstr && (*ptr < '0' || *ptr > '9'))
+ + ]
8212 bruce@momjian.us 1980 :GBC 330 : ptr--;
8212 bruce@momjian.us 1981 :CBC 66 : letterversion = ptr + 1;
1982 : 66 : major = strtol(vstr, &endptr, 10);
1983 [ + - ]: 66 : if (*endptr)
1984 : 66 : minor = strtol(endptr + 1, &endptr, 10);
1985 [ + - ]: 66 : if (*endptr)
8212 bruce@momjian.us 1986 :GBC 66 : micro = strtol(endptr + 1, &endptr, 10);
8212 bruce@momjian.us 1987 :CBC 66 : snprintf(infoversion, sizeof(infoversion), "%02ld.%02ld.%04ld%s",
1988 : : major, minor, micro, letterversion);
1989 : 66 : }
1990 : :
1991 : : /*
1992 : : * load info schema and populate from features file
1993 : : */
1994 : : static void
3792 tgl@sss.pgh.pa.us 1995 : 57 : setup_schema(FILE *cmdfd)
1996 : : {
1921 peter@eisentraut.org 1997 : 57 : setup_run_file(cmdfd, info_schema_file);
1998 : :
2462 1999 [ + - + + ]: 57 : PG_CMD_PRINTF("UPDATE information_schema.sql_implementation_info "
2000 : : " SET character_value = '%s' "
2001 : : " WHERE implementation_info_name = 'DBMS VERSION';\n\n",
2002 : : infoversion);
2003 : :
2004 [ + - + + ]: 57 : PG_CMD_PRINTF("COPY information_schema.sql_features "
2005 : : " (feature_id, feature_name, sub_feature_id, "
2006 : : " sub_feature_name, is_supported, comments) "
2007 : : " FROM E'%s';\n\n",
2008 : : escape_quotes(features_file));
8212 bruce@momjian.us 2009 : 57 : }
2010 : :
2011 : : /*
2012 : : * load PL/pgSQL server-side language
2013 : : */
2014 : : static void
3792 tgl@sss.pgh.pa.us 2015 : 57 : load_plpgsql(FILE *cmdfd)
2016 : : {
2017 [ + - + + ]: 57 : PG_CMD_PUTS("CREATE EXTENSION plpgsql;\n\n");
5982 bruce@momjian.us 2018 : 57 : }
2019 : :
2020 : : /*
2021 : : * clean everything up in template1
2022 : : */
2023 : : static void
3792 tgl@sss.pgh.pa.us 2024 : 57 : vacuum_db(FILE *cmdfd)
2025 : : {
2026 : : /* Run analyze before VACUUM so the statistics are frozen. */
2027 [ + - + + ]: 57 : PG_CMD_PUTS("ANALYZE;\n\nVACUUM FREEZE;\n\n");
8212 bruce@momjian.us 2028 : 57 : }
2029 : :
2030 : : /*
2031 : : * copy template1 to template0
2032 : : */
2033 : : static void
3792 tgl@sss.pgh.pa.us 2034 : 57 : make_template0(FILE *cmdfd)
2035 : : {
2036 : : /*
2037 : : * pg_upgrade tries to preserve database OIDs across upgrades. It's smart
2038 : : * enough to drop and recreate a conflicting database with the same name,
2039 : : * but if the same OID were used for one system-created database in the
2040 : : * old cluster and a different system-created database in the new cluster,
2041 : : * it would fail. To avoid that, assign a fixed OID to template0 rather
2042 : : * than letting the server choose one.
2043 : : *
2044 : : * (Note that, while the user could have dropped and recreated these
2045 : : * objects in the old cluster, the problem scenario only exists if the OID
2046 : : * that is in use in the old cluster is also used in the new cluster - and
2047 : : * the new cluster should be the result of a fresh initdb.)
2048 : : *
2049 : : * We use "STRATEGY = file_copy" here because checkpoints during initdb
2050 : : * are cheap. "STRATEGY = wal_log" would generate more WAL, which would be
2051 : : * a little bit slower and make the new cluster a little bit bigger.
2052 : : */
1247 peter@eisentraut.org 2053 [ + - + + ]: 57 : PG_CMD_PUTS("CREATE DATABASE template0 IS_TEMPLATE = true ALLOW_CONNECTIONS = false"
2054 : : " OID = " CppAsString2(Template0DbOid)
2055 : : " STRATEGY = file_copy;\n\n");
2056 : :
2057 : : /*
2058 : : * template0 shouldn't have any collation-dependent objects, so unset the
2059 : : * collation version. This disables collation version checks when making
2060 : : * a new database from it.
2061 : : */
2062 [ + - + + ]: 57 : PG_CMD_PUTS("UPDATE pg_database SET datcollversion = NULL WHERE datname = 'template0';\n\n");
2063 : :
2064 : : /*
2065 : : * While we are here, do set the collation version on template1.
2066 : : */
2067 [ + - + + ]: 57 : PG_CMD_PUTS("UPDATE pg_database SET datcollversion = pg_database_collation_actual_version(oid) WHERE datname = 'template1';\n\n");
2068 : :
2069 : : /*
2070 : : * Explicitly revoke public create-schema and create-temp-table privileges
2071 : : * in template1 and template0; else the latter would be on by default
2072 : : */
2073 [ + - + + ]: 57 : PG_CMD_PUTS("REVOKE CREATE,TEMPORARY ON DATABASE template1 FROM public;\n\n");
2074 [ + - + + ]: 57 : PG_CMD_PUTS("REVOKE CREATE,TEMPORARY ON DATABASE template0 FROM public;\n\n");
2075 : :
2076 [ + - + + ]: 57 : PG_CMD_PUTS("COMMENT ON DATABASE template0 IS 'unmodifiable empty database';\n\n");
2077 : :
2078 : : /*
2079 : : * Finally vacuum to clean up dead rows in pg_database
2080 : : */
2081 [ + - + + ]: 57 : PG_CMD_PUTS("VACUUM pg_database;\n\n");
8212 bruce@momjian.us 2082 : 57 : }
2083 : :
2084 : : /*
2085 : : * copy template1 to postgres
2086 : : */
2087 : : static void
3792 tgl@sss.pgh.pa.us 2088 : 57 : make_postgres(FILE *cmdfd)
2089 : : {
2090 : : /*
2091 : : * Just as we did for template0, and for the same reasons, assign a fixed
2092 : : * OID to postgres and select the file_copy strategy.
2093 : : */
1247 peter@eisentraut.org 2094 [ + - + + ]: 57 : PG_CMD_PUTS("CREATE DATABASE postgres OID = " CppAsString2(PostgresDbOid)
2095 : : " STRATEGY = file_copy;\n\n");
2096 [ + - + + ]: 57 : PG_CMD_PUTS("COMMENT ON DATABASE postgres IS 'default administrative connection database';\n\n");
7623 tgl@sss.pgh.pa.us 2097 : 57 : }
2098 : :
2099 : : /*
2100 : : * signal handler in case we are interrupted.
2101 : : *
2102 : : * The Windows runtime docs at
2103 : : * https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/signal
2104 : : * specifically forbid a number of things being done from a signal handler,
2105 : : * including IO, memory allocation and system calls, and only allow jmpbuf
2106 : : * if you are handling SIGFPE.
2107 : : *
2108 : : * I avoided doing the forbidden things by setting a flag instead of calling
2109 : : * exit() directly.
2110 : : *
2111 : : * Also note the behaviour of Windows with SIGINT, which says this:
2112 : : * SIGINT is not supported for any Win32 application. When a CTRL+C interrupt
2113 : : * occurs, Win32 operating systems generate a new thread to specifically
2114 : : * handle that interrupt. This can cause a single-thread application, such as
2115 : : * one in UNIX, to become multithreaded and cause unexpected behavior.
2116 : : *
2117 : : * I have no idea how to handle this. (Strange they call UNIX an application!)
2118 : : * So this will need some testing on Windows.
2119 : : */
2120 : : static void
1329 tgl@sss.pgh.pa.us 2121 :UBC 0 : trapsig(SIGNAL_ARGS)
2122 : : {
2123 : : /* handle systems that reset the handler, like Windows (grr) */
2124 : 0 : pqsignal(postgres_signal_arg, trapsig);
8208 2125 : 0 : caught_signal = true;
8212 bruce@momjian.us 2126 : 0 : }
2127 : :
2128 : : /*
2129 : : * call exit() if we got a signal, or else output "ok".
2130 : : */
2131 : : static void
7877 neilc@samurai.com 2132 :CBC 294 : check_ok(void)
2133 : : {
8208 tgl@sss.pgh.pa.us 2134 [ - + ]: 294 : if (caught_signal)
2135 : : {
8199 peter_e@gmx.net 2136 :UBC 0 : printf(_("caught signal\n"));
7827 tgl@sss.pgh.pa.us 2137 : 0 : fflush(stdout);
2684 peter@eisentraut.org 2138 : 0 : exit(1);
2139 : : }
8208 tgl@sss.pgh.pa.us 2140 [ - + ]:CBC 294 : else if (output_failed)
2141 : : {
7827 tgl@sss.pgh.pa.us 2142 :UBC 0 : printf(_("could not write to child process: %s\n"),
2143 : : strerror(output_errno));
2144 : 0 : fflush(stdout);
2684 peter@eisentraut.org 2145 : 0 : exit(1);
2146 : : }
2147 : : else
2148 : : {
2149 : : /* all seems well */
8199 peter_e@gmx.net 2150 :CBC 294 : printf(_("ok\n"));
7827 tgl@sss.pgh.pa.us 2151 : 294 : fflush(stdout);
2152 : : }
8212 bruce@momjian.us 2153 : 294 : }
2154 : :
2155 : : /* Hack to suppress a warning about %x from some versions of gcc */
2156 : : static inline size_t
3240 tgl@sss.pgh.pa.us 2157 : 58 : my_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
2158 : : {
6873 2159 : 58 : return strftime(s, max, fmt, tm);
2160 : : }
2161 : :
2162 : : /*
2163 : : * Determine likely date order from locale
2164 : : */
2165 : : static int
7452 peter_e@gmx.net 2166 : 58 : locale_date_order(const char *locale)
2167 : : {
2168 : : struct tm testtime;
2169 : : char buf[128];
2170 : : char *posD;
2171 : : char *posM;
2172 : : char *posY;
2173 : : save_locale_t save;
2174 : : size_t res;
2175 : : int result;
2176 : :
2177 : 58 : result = DATEORDER_MDY; /* default */
2178 : :
577 tmunro@postgresql.or 2179 : 58 : save = save_global_locale(LC_TIME);
2180 : :
7452 peter_e@gmx.net 2181 : 58 : setlocale(LC_TIME, locale);
2182 : :
2183 : 58 : memset(&testtime, 0, sizeof(testtime));
2184 : 58 : testtime.tm_mday = 22;
2185 : 58 : testtime.tm_mon = 10; /* November, should come out as "11" */
2186 : 58 : testtime.tm_year = 133; /* 2033 */
2187 : :
6873 tgl@sss.pgh.pa.us 2188 : 58 : res = my_strftime(buf, sizeof(buf), "%x", &testtime);
2189 : :
577 tmunro@postgresql.or 2190 : 58 : restore_global_locale(LC_TIME, save);
2191 : :
7452 peter_e@gmx.net 2192 [ - + ]: 58 : if (res == 0)
7452 peter_e@gmx.net 2193 :UBC 0 : return result;
2194 : :
7452 peter_e@gmx.net 2195 :CBC 58 : posM = strstr(buf, "11");
2196 : 58 : posD = strstr(buf, "22");
2197 : 58 : posY = strstr(buf, "33");
2198 : :
2199 [ + - + - : 58 : if (!posM || !posD || !posY)
- + ]
7452 peter_e@gmx.net 2200 :UBC 0 : return result;
2201 : :
7452 peter_e@gmx.net 2202 [ - + - - ]:CBC 58 : if (posY < posM && posM < posD)
7452 peter_e@gmx.net 2203 :UBC 0 : result = DATEORDER_YMD;
7452 peter_e@gmx.net 2204 [ - + ]:CBC 58 : else if (posD < posM)
7452 peter_e@gmx.net 2205 :UBC 0 : result = DATEORDER_DMY;
2206 : : else
7452 peter_e@gmx.net 2207 :CBC 58 : result = DATEORDER_MDY;
2208 : :
2209 : 58 : return result;
2210 : : }
2211 : :
2212 : : /*
2213 : : * Verify that locale name is valid for the locale category.
2214 : : *
2215 : : * If successful, and canonname isn't NULL, a malloc'd copy of the locale's
2216 : : * canonical name is stored there. This is especially useful for figuring out
2217 : : * what locale name "" means (ie, the environment value). (Actually,
2218 : : * it seems that on most implementations that's the only thing it's good for;
2219 : : * we could wish that setlocale gave back a canonically spelled version of
2220 : : * the locale name, but typically it doesn't.)
2221 : : *
2222 : : * this should match the backend's check_locale() function
2223 : : */
2224 : : static void
5154 tgl@sss.pgh.pa.us 2225 : 396 : check_locale_name(int category, const char *locale, char **canonname)
2226 : : {
2227 : : save_locale_t save;
2228 : : char *res;
2229 : :
2230 : : /* Don't let Windows' non-ASCII locale names in. */
577 tmunro@postgresql.or 2231 [ + + - + ]: 396 : if (locale && !pg_is_ascii(locale))
577 tmunro@postgresql.or 2232 :UBC 0 : pg_fatal("locale name \"%s\" contains non-ASCII characters", locale);
2233 : :
5154 tgl@sss.pgh.pa.us 2234 [ + - ]:CBC 396 : if (canonname)
2235 : 396 : *canonname = NULL; /* in case of failure */
2236 : :
577 tmunro@postgresql.or 2237 : 396 : save = save_global_locale(category);
2238 : :
2239 : : /* for setlocale() call */
3170 peter_e@gmx.net 2240 [ + + ]: 396 : if (!locale)
2241 : 287 : locale = "";
2242 : :
2243 : : /* set the locale with setlocale, to see if it accepts it. */
5154 tgl@sss.pgh.pa.us 2244 : 396 : res = setlocale(category, locale);
2245 : :
2246 : : /* save canonical name if requested. */
2247 [ + - + - ]: 396 : if (res && canonname)
4963 2248 : 396 : *canonname = pg_strdup(res);
2249 : :
2250 : : /* restore old value. */
577 tmunro@postgresql.or 2251 : 396 : restore_global_locale(category, save);
2252 : :
2253 : : /* complain if locale wasn't valid */
5154 tgl@sss.pgh.pa.us 2254 [ - + ]: 396 : if (res == NULL)
2255 : : {
4374 tgl@sss.pgh.pa.us 2256 [ # # ]:UBC 0 : if (*locale)
2257 : : {
1054 jdavis@postgresql.or 2258 : 0 : pg_log_error("invalid locale name \"%s\"", locale);
2259 : 0 : pg_log_error_hint("If the locale name is specific to ICU, use --icu-locale.");
2260 : 0 : exit(1);
2261 : : }
2262 : : else
2263 : : {
2264 : : /*
2265 : : * If no relevant switch was given on command line, locale is an
2266 : : * empty string, which is not too helpful to report. Presumably
2267 : : * setlocale() found something it did not like in the environment.
2268 : : * Ideally we'd report the bad environment variable, but since
2269 : : * setlocale's behavior is implementation-specific, it's hard to
2270 : : * be sure what it didn't like. Print a safe generic message.
2271 : : */
1488 tgl@sss.pgh.pa.us 2272 : 0 : pg_fatal("invalid locale settings; check LANG and LC_* environment variables");
2273 : : }
2274 : : }
2275 : :
2276 : : /* Don't let Windows' non-ASCII locale names out. */
577 tmunro@postgresql.or 2277 [ + - - + ]:CBC 396 : if (canonname && !pg_is_ascii(*canonname))
577 tmunro@postgresql.or 2278 :UBC 0 : pg_fatal("locale name \"%s\" contains non-ASCII characters",
2279 : : *canonname);
8212 bruce@momjian.us 2280 :CBC 396 : }
2281 : :
2282 : : /*
2283 : : * check if the chosen encoding matches the encoding required by the locale
2284 : : *
2285 : : * this should match the similar check in the backend createdb() function
2286 : : */
2287 : : static bool
6433 heikki.linnakangas@i 2288 : 126 : check_locale_encoding(const char *locale, int user_enc)
2289 : : {
2290 : : int locale_enc;
2291 : :
5565 peter_e@gmx.net 2292 : 126 : locale_enc = pg_get_encoding_from_locale(locale, true);
2293 : :
2294 : : /* See notes in createdb() to understand these tests */
6433 heikki.linnakangas@i 2295 [ + + + + : 130 : if (!(locale_enc == user_enc ||
- + ]
2296 [ + - ]: 4 : locale_enc == PG_SQL_ASCII ||
2297 : : locale_enc == -1 ||
2298 : : #ifdef WIN32
2299 : : user_enc == PG_UTF8 ||
2300 : : #endif
2301 : : user_enc == PG_SQL_ASCII))
2302 : : {
2591 peter@eisentraut.org 2303 :UBC 0 : pg_log_error("encoding mismatch");
1488 tgl@sss.pgh.pa.us 2304 : 0 : pg_log_error_detail("The encoding you selected (%s) and the encoding that the "
2305 : : "selected locale uses (%s) do not match. This would lead to "
2306 : : "misbehavior in various character string processing functions.",
2307 : : pg_encoding_to_char(user_enc),
2308 : : pg_encoding_to_char(locale_enc));
2309 : 0 : pg_log_error_hint("Rerun %s and either do not specify an encoding explicitly, "
2310 : : "or choose a matching combination.",
2311 : : progname);
6433 heikki.linnakangas@i 2312 : 0 : return false;
2313 : : }
6433 heikki.linnakangas@i 2314 :CBC 126 : return true;
2315 : : }
2316 : :
2317 : : /*
2318 : : * check if the chosen encoding matches is supported by ICU
2319 : : *
2320 : : * this should match the similar check in the backend createdb() function
2321 : : */
2322 : : static bool
1327 peter@eisentraut.org 2323 : 6 : check_icu_locale_encoding(int user_enc)
2324 : : {
2325 [ + + ]: 6 : if (!(is_encoding_supported_by_icu(user_enc)))
2326 : : {
2327 : 1 : pg_log_error("encoding mismatch");
2328 : 1 : pg_log_error_detail("The encoding you selected (%s) is not supported with the ICU provider.",
2329 : : pg_encoding_to_char(user_enc));
2330 : 1 : pg_log_error_hint("Rerun %s and either do not specify an encoding explicitly, "
2331 : : "or choose a matching combination.",
2332 : : progname);
2333 : 1 : return false;
2334 : : }
2335 : 5 : return true;
2336 : : }
2337 : :
2338 : : /*
2339 : : * Convert to canonical BCP47 language tag. Must be consistent with
2340 : : * icu_language_tag().
2341 : : */
2342 : : static char *
1127 jdavis@postgresql.or 2343 : 7 : icu_language_tag(const char *loc_str)
2344 : : {
2345 : : #ifdef USE_ICU
2346 : : UErrorCode status;
2347 : : char *langtag;
1082 tgl@sss.pgh.pa.us 2348 : 7 : size_t buflen = 32; /* arbitrary starting buffer size */
2349 : 7 : const bool strict = true;
2350 : :
2351 : : /*
2352 : : * A BCP47 language tag doesn't have a clearly-defined upper limit (cf.
2353 : : * RFC5646 section 4.4). Additionally, in older ICU versions,
2354 : : * uloc_toLanguageTag() doesn't always return the ultimate length on the
2355 : : * first call, necessitating a loop.
2356 : : */
1127 jdavis@postgresql.or 2357 : 7 : langtag = pg_malloc(buflen);
2358 : : while (true)
2359 : : {
2360 : 7 : status = U_ZERO_ERROR;
1084 2361 : 7 : uloc_toLanguageTag(loc_str, langtag, buflen, strict, &status);
2362 : :
2363 : : /* try again if the buffer is not large enough */
1127 2364 [ + - ]: 7 : if (status == U_BUFFER_OVERFLOW_ERROR ||
1084 2365 [ - + ]: 7 : status == U_STRING_NOT_TERMINATED_WARNING)
2366 : : {
1127 jdavis@postgresql.or 2367 :UBC 0 : buflen = buflen * 2;
2368 : 0 : langtag = pg_realloc(langtag, buflen);
2369 : 0 : continue;
2370 : : }
2371 : :
1127 jdavis@postgresql.or 2372 :CBC 7 : break;
2373 : : }
2374 : :
2375 [ - + ]: 7 : if (U_FAILURE(status))
2376 : : {
1127 jdavis@postgresql.or 2377 :UBC 0 : pg_free(langtag);
2378 : :
2379 : 0 : pg_fatal("could not convert locale name \"%s\" to language tag: %s",
2380 : : loc_str, u_errorName(status));
2381 : : }
2382 : :
1127 jdavis@postgresql.or 2383 :CBC 7 : return langtag;
2384 : : #else
2385 : : pg_fatal("ICU is not supported in this build");
2386 : : return NULL; /* keep compiler quiet */
2387 : : #endif
2388 : : }
2389 : :
2390 : : /*
2391 : : * Perform best-effort check that the locale is a valid one. Should be
2392 : : * consistent with pg_locale.c, except that it doesn't need to open the
2393 : : * collator (that will happen during post-bootstrap initialization).
2394 : : */
2395 : : static void
1134 2396 : 7 : icu_validate_locale(const char *loc_str)
2397 : : {
2398 : : #ifdef USE_ICU
2399 : : UErrorCode status;
2400 : : char lang[ULOC_LANG_CAPACITY];
1082 tgl@sss.pgh.pa.us 2401 : 7 : bool found = false;
2402 : :
2403 : : /* validate that we can extract the language */
1134 jdavis@postgresql.or 2404 : 7 : status = U_ZERO_ERROR;
2405 : 7 : uloc_getLanguage(loc_str, lang, ULOC_LANG_CAPACITY, &status);
21 2406 [ + - - + ]: 7 : if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
2407 : : {
1134 jdavis@postgresql.or 2408 :UBC 0 : pg_fatal("could not get language from locale \"%s\": %s",
2409 : : loc_str, u_errorName(status));
2410 : : return;
2411 : : }
2412 : :
2413 : : /* check for special language name */
1134 jdavis@postgresql.or 2414 [ + + ]:CBC 7 : if (strcmp(lang, "") == 0 ||
1049 2415 [ + - - + ]: 4 : strcmp(lang, "root") == 0 || strcmp(lang, "und") == 0)
1134 2416 : 3 : found = true;
2417 : :
2418 : : /* search for matching language within ICU */
2419 [ + + + + ]: 1287 : for (int32_t i = 0; !found && i < uloc_countAvailable(); i++)
2420 : : {
1082 tgl@sss.pgh.pa.us 2421 : 1280 : const char *otherloc = uloc_getAvailable(i);
2422 : : char otherlang[ULOC_LANG_CAPACITY];
2423 : :
1134 jdavis@postgresql.or 2424 : 1280 : status = U_ZERO_ERROR;
2425 : 1280 : uloc_getLanguage(otherloc, otherlang, ULOC_LANG_CAPACITY, &status);
21 2426 [ + - - + ]: 1280 : if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
1134 jdavis@postgresql.or 2427 :UBC 0 : continue;
2428 : :
1134 jdavis@postgresql.or 2429 [ + + ]:CBC 1280 : if (strcmp(lang, otherlang) == 0)
2430 : 3 : found = true;
2431 : : }
2432 : :
2433 [ + + ]: 7 : if (!found)
2434 : 1 : pg_fatal("locale \"%s\" has unknown language \"%s\"",
2435 : : loc_str, lang);
2436 : : #else
2437 : : pg_fatal("ICU is not supported in this build");
2438 : : #endif
2439 : : }
2440 : :
2441 : : /*
2442 : : * set up the locale variables
2443 : : *
2444 : : * assumes we have called setlocale(LC_ALL, "") -- see set_pglocale_pgservice
2445 : : */
2446 : : static void
8212 bruce@momjian.us 2447 : 66 : setlocales(void)
2448 : : {
2449 : : char *canonname;
2450 : :
2451 : : /* set empty lc_* and datlocale values to locale config if set */
2452 : :
3170 peter_e@gmx.net 2453 [ + + ]: 66 : if (locale)
2454 : : {
2455 [ + + ]: 17 : if (!lc_ctype)
8212 bruce@momjian.us 2456 : 15 : lc_ctype = locale;
3170 peter_e@gmx.net 2457 [ + + ]: 17 : if (!lc_collate)
8212 bruce@momjian.us 2458 : 16 : lc_collate = locale;
3170 peter_e@gmx.net 2459 [ + + ]: 17 : if (!lc_numeric)
8212 bruce@momjian.us 2460 : 16 : lc_numeric = locale;
3170 peter_e@gmx.net 2461 [ + + ]: 17 : if (!lc_time)
8212 bruce@momjian.us 2462 : 16 : lc_time = locale;
3170 peter_e@gmx.net 2463 [ + + ]: 17 : if (!lc_monetary)
8212 bruce@momjian.us 2464 : 16 : lc_monetary = locale;
3170 peter_e@gmx.net 2465 [ + + ]: 17 : if (!lc_messages)
8212 bruce@momjian.us 2466 : 16 : lc_messages = locale;
783 jdavis@postgresql.or 2467 [ + - + + ]: 17 : if (!datlocale && locale_provider != COLLPROVIDER_LIBC)
787 2468 : 3 : datlocale = locale;
2469 : : }
2470 : :
2471 : : /*
2472 : : * canonicalize locale names, and obtain any missing values from our
2473 : : * current environment
2474 : : */
4374 tgl@sss.pgh.pa.us 2475 : 66 : check_locale_name(LC_CTYPE, lc_ctype, &canonname);
2476 : 66 : lc_ctype = canonname;
2477 : 66 : check_locale_name(LC_COLLATE, lc_collate, &canonname);
2478 : 66 : lc_collate = canonname;
2479 : 66 : check_locale_name(LC_NUMERIC, lc_numeric, &canonname);
2480 : 66 : lc_numeric = canonname;
2481 : 66 : check_locale_name(LC_TIME, lc_time, &canonname);
2482 : 66 : lc_time = canonname;
2483 : 66 : check_locale_name(LC_MONETARY, lc_monetary, &canonname);
2484 : 66 : lc_monetary = canonname;
2485 : : #if defined(LC_MESSAGES) && !defined(WIN32)
2486 : 66 : check_locale_name(LC_MESSAGES, lc_messages, &canonname);
2487 : 66 : lc_messages = canonname;
2488 : : #else
2489 : : /* when LC_MESSAGES is not available, use the LC_CTYPE setting */
2490 : : check_locale_name(LC_CTYPE, lc_messages, &canonname);
2491 : : lc_messages = canonname;
2492 : : #endif
2493 : :
783 jdavis@postgresql.or 2494 [ + + + + ]: 66 : if (locale_provider != COLLPROVIDER_LIBC && datlocale == NULL)
2495 : 2 : pg_fatal("locale must be specified if provider is %s",
2496 : : collprovider_name(locale_provider));
2497 : :
2498 [ + + ]: 64 : if (locale_provider == COLLPROVIDER_BUILTIN)
2499 : : {
777 2500 [ + + ]: 5 : if (strcmp(datlocale, "C") == 0)
2501 : 2 : canonname = "C";
2502 [ - + ]: 3 : else if (strcmp(datlocale, "C.UTF-8") == 0 ||
777 jdavis@postgresql.or 2503 [ # # ]:UBC 0 : strcmp(datlocale, "C.UTF8") == 0)
777 jdavis@postgresql.or 2504 :CBC 3 : canonname = "C.UTF-8";
473 jdavis@postgresql.or 2505 [ # # ]:UBC 0 : else if (strcmp(datlocale, "PG_UNICODE_FAST") == 0)
2506 : 0 : canonname = "PG_UNICODE_FAST";
2507 : : else
783 2508 : 0 : pg_fatal("invalid locale name \"%s\" for builtin provider",
2509 : : datlocale);
2510 : :
777 jdavis@postgresql.or 2511 :CBC 5 : datlocale = canonname;
2512 : : }
783 2513 [ + + ]: 59 : else if (locale_provider == COLLPROVIDER_ICU)
2514 : : {
2515 : : char *langtag;
2516 : :
2517 : : /* canonicalize to a language tag */
787 2518 : 7 : langtag = icu_language_tag(datlocale);
1127 2519 : 7 : printf(_("Using language tag \"%s\" for ICU locale \"%s\".\n"),
2520 : : langtag, datlocale);
787 2521 : 7 : pg_free(datlocale);
2522 : 7 : datlocale = langtag;
2523 : :
2524 : 7 : icu_validate_locale(datlocale);
2525 : :
2526 : : /*
2527 : : * In supported builds, the ICU locale ID will be opened during
2528 : : * post-bootstrap initialization, which will perform extra checks.
2529 : : */
2530 : : #ifndef USE_ICU
2531 : : pg_fatal("ICU is not supported in this build");
2532 : : #endif
2533 : : }
8212 bruce@momjian.us 2534 : 63 : }
2535 : :
2536 : : /*
2537 : : * print help text
2538 : : */
2539 : : static void
8199 peter_e@gmx.net 2540 : 1 : usage(const char *progname)
2541 : : {
2542 : 1 : printf(_("%s initializes a PostgreSQL database cluster.\n\n"), progname);
2543 : 1 : printf(_("Usage:\n"));
2544 : 1 : printf(_(" %s [OPTION]... [DATADIR]\n"), progname);
2545 : 1 : printf(_("\nOptions:\n"));
6278 2546 : 1 : printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
5207 2547 : 1 : printf(_(" --auth-host=METHOD default authentication method for local TCP/IP connections\n"));
2548 : 1 : printf(_(" --auth-local=METHOD default authentication method for local-socket connections\n"));
8199 2549 : 1 : printf(_(" [-D, --pgdata=]DATADIR location for this database cluster\n"));
2550 : 1 : printf(_(" -E, --encoding=ENCODING set default encoding for new databases\n"));
2950 sfrost@snowman.net 2551 : 1 : printf(_(" -g, --allow-group-access allow group read/execute on data directory\n"));
1510 peter@eisentraut.org 2552 : 1 : printf(_(" --icu-locale=LOCALE set ICU locale ID for new databases\n"));
1154 2553 : 1 : printf(_(" --icu-rules=RULES set additional ICU collation rules for new databases\n"));
1945 michael@paquier.xyz 2554 : 1 : printf(_(" -k, --data-checksums use data page checksums\n"));
6278 peter_e@gmx.net 2555 : 1 : printf(_(" --locale=LOCALE set default locale for new databases\n"));
2556 : 1 : printf(_(" --lc-collate=, --lc-ctype=, --lc-messages=LOCALE\n"
2557 : : " --lc-monetary=, --lc-numeric=, --lc-time=LOCALE\n"
2558 : : " set default locale in the respective category for\n"
2559 : : " new databases (default taken from environment)\n"));
2560 : 1 : printf(_(" --no-locale equivalent to --locale=C\n"));
778 jdavis@postgresql.or 2561 : 1 : printf(_(" --builtin-locale=LOCALE\n"
2562 : : " set builtin locale name for new databases\n"));
783 2563 : 1 : printf(_(" --locale-provider={builtin|libc|icu}\n"
2564 : : " set default locale provider for new databases\n"));
581 peter@eisentraut.org 2565 : 1 : printf(_(" --no-data-checksums do not use data page checksums\n"));
1955 bruce@momjian.us 2566 : 1 : printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
6797 peter_e@gmx.net 2567 : 1 : printf(_(" -T, --text-search-config=CFG\n"
2568 : : " default text search configuration\n"));
8199 2569 : 1 : printf(_(" -U, --username=NAME database superuser name\n"));
1955 bruce@momjian.us 2570 : 1 : printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
3372 rhaas@postgresql.org 2571 : 1 : printf(_(" -X, --waldir=WALDIR location for the write-ahead log directory\n"));
2964 peter_e@gmx.net 2572 : 1 : printf(_(" --wal-segsize=SIZE size of WAL segments, in megabytes\n"));
8199 2573 : 1 : printf(_("\nLess commonly used options:\n"));
1140 tgl@sss.pgh.pa.us 2574 : 1 : printf(_(" -c, --set NAME=VALUE override default setting for server parameter\n"));
8199 peter_e@gmx.net 2575 : 1 : printf(_(" -d, --debug generate lots of debugging output\n"));
1757 tgl@sss.pgh.pa.us 2576 : 1 : printf(_(" --discard-caches set debug_discard_caches=1\n"));
8199 peter_e@gmx.net 2577 : 1 : printf(_(" -L DIRECTORY where to find the input files\n"));
3485 2578 : 1 : printf(_(" -n, --no-clean do not clean up after errors\n"));
2579 : 1 : printf(_(" -N, --no-sync do not wait for changes to be written safely to disk\n"));
406 nathan@postgresql.or 2580 : 1 : printf(_(" --no-sync-data-files do not sync files within database directories\n"));
1934 magnus@hagander.net 2581 : 1 : printf(_(" --no-instructions do not print instructions for next steps\n"));
691 peter@eisentraut.org 2582 : 1 : printf(_(" -s, --show show internal settings, then exit\n"));
972 nathan@postgresql.or 2583 : 1 : printf(_(" --sync-method=METHOD set method for syncing files to disk\n"));
1723 dgustafsson@postgres 2584 : 1 : printf(_(" -S, --sync-only only sync database files to disk, then exit\n"));
6278 peter_e@gmx.net 2585 : 1 : printf(_("\nOther options:\n"));
2586 : 1 : printf(_(" -V, --version output version information, then exit\n"));
5069 2587 : 1 : printf(_(" -?, --help show this help, then exit\n"));
8199 2588 : 1 : printf(_("\nIf the data directory is not specified, the environment variable PGDATA\n"
2589 : : "is used.\n"));
2258 peter@eisentraut.org 2590 : 1 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
2591 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
8199 peter_e@gmx.net 2592 : 1 : }
2593 : :
2594 : : static void
2479 peter@eisentraut.org 2595 : 134 : check_authmethod_unspecified(const char **authmethod)
2596 : : {
2597 [ + + ]: 134 : if (*authmethod == NULL)
2598 : : {
2599 : 46 : authwarning = true;
2600 : 46 : *authmethod = "trust";
2601 : : }
2602 : 134 : }
2603 : :
2604 : : static void
3240 tgl@sss.pgh.pa.us 2605 : 134 : check_authmethod_valid(const char *authmethod, const char *const *valid_methods, const char *conntype)
2606 : : {
2607 : : const char *const *p;
2608 : :
5207 peter_e@gmx.net 2609 [ + - ]: 134 : for (p = valid_methods; *p; p++)
2610 : : {
2611 [ + - ]: 134 : if (strcmp(authmethod, *p) == 0)
2612 : 134 : return;
2613 : : }
2614 : :
1488 tgl@sss.pgh.pa.us 2615 :UBC 0 : pg_fatal("invalid authentication method \"%s\" for \"%s\" connections",
2616 : : authmethod, conntype);
2617 : : }
2618 : :
2619 : : static void
5057 peter_e@gmx.net 2620 :CBC 67 : check_need_password(const char *authmethodlocal, const char *authmethodhost)
2621 : : {
2622 [ + - ]: 67 : if ((strcmp(authmethodlocal, "md5") == 0 ||
3346 heikki.linnakangas@i 2623 [ + - ]: 67 : strcmp(authmethodlocal, "password") == 0 ||
3304 2624 [ - + ]: 67 : strcmp(authmethodlocal, "scram-sha-256") == 0) &&
5057 peter_e@gmx.net 2625 [ # # ]:UBC 0 : (strcmp(authmethodhost, "md5") == 0 ||
3346 heikki.linnakangas@i 2626 [ # # ]: 0 : strcmp(authmethodhost, "password") == 0 ||
3304 2627 [ # # ]: 0 : strcmp(authmethodhost, "scram-sha-256") == 0) &&
5207 peter_e@gmx.net 2628 [ # # # # ]: 0 : !(pwprompt || pwfilename))
1488 tgl@sss.pgh.pa.us 2629 : 0 : pg_fatal("must specify a password for the superuser to enable password authentication");
5207 peter_e@gmx.net 2630 :CBC 67 : }
2631 : :
2632 : :
2633 : : void
4904 bruce@momjian.us 2634 : 71 : setup_pgdata(void)
2635 : : {
2636 : : char *pgdata_get_env;
2637 : :
3170 peter_e@gmx.net 2638 [ - + ]: 71 : if (!pg_data)
2639 : : {
4904 bruce@momjian.us 2640 :UBC 0 : pgdata_get_env = getenv("PGDATA");
2641 [ # # # # ]: 0 : if (pgdata_get_env && strlen(pgdata_get_env))
2642 : : {
2643 : : /* PGDATA found */
2644 : 0 : pg_data = pg_strdup(pgdata_get_env);
2645 : : }
2646 : : else
2647 : : {
2591 peter@eisentraut.org 2648 : 0 : pg_log_error("no data directory specified");
1488 tgl@sss.pgh.pa.us 2649 : 0 : pg_log_error_hint("You must identify the directory where the data for this database system "
2650 : : "will reside. Do this with either the invocation option -D or the "
2651 : : "environment variable PGDATA.");
4904 bruce@momjian.us 2652 : 0 : exit(1);
2653 : : }
2654 : : }
2655 : :
4904 bruce@momjian.us 2656 :CBC 71 : pgdata_native = pg_strdup(pg_data);
2657 : 71 : canonicalize_path(pg_data);
2658 : :
2659 : : /*
2660 : : * we have to set PGDATA for postgres rather than pass it on the command
2661 : : * line to avoid dumb quoting problems on Windows, and we would especially
2662 : : * need quotes otherwise on Windows because paths there are most likely to
2663 : : * have embedded spaces.
2664 : : */
1952 tgl@sss.pgh.pa.us 2665 [ - + ]: 71 : if (setenv("PGDATA", pg_data, 1) != 0)
1488 tgl@sss.pgh.pa.us 2666 :UBC 0 : pg_fatal("could not set environment");
4904 bruce@momjian.us 2667 :CBC 71 : }
2668 : :
2669 : :
2670 : : void
2671 : 67 : setup_bin_paths(const char *argv0)
2672 : : {
2673 : : int ret;
2674 : :
2675 [ - + ]: 67 : if ((ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
2676 : : backend_exec)) < 0)
2677 : : {
2678 : : char full_path[MAXPGPATH];
2679 : :
4904 bruce@momjian.us 2680 [ # # ]:UBC 0 : if (find_my_exec(argv0, full_path) < 0)
7024 peter_e@gmx.net 2681 : 0 : strlcpy(full_path, progname, sizeof(full_path));
2682 : :
8029 bruce@momjian.us 2683 [ # # ]: 0 : if (ret == -1)
1488 tgl@sss.pgh.pa.us 2684 : 0 : pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
2685 : : "postgres", progname, full_path);
2686 : : else
2687 : 0 : pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
2688 : : "postgres", full_path, progname);
2689 : : }
2690 : :
2691 : : /* store binary directory */
8023 bruce@momjian.us 2692 :CBC 67 : strcpy(bin_path, backend_exec);
7999 2693 : 67 : *last_dir_separator(bin_path) = '\0';
7939 tgl@sss.pgh.pa.us 2694 : 67 : canonicalize_path(bin_path);
2695 : :
8023 bruce@momjian.us 2696 [ + - ]: 67 : if (!share_path)
2697 : : {
7604 2698 : 67 : share_path = pg_malloc(MAXPGPATH);
8023 2699 : 67 : get_share_path(backend_exec, share_path);
2700 : : }
7932 bruce@momjian.us 2701 [ # # ]:UBC 0 : else if (!is_absolute_path(share_path))
1488 tgl@sss.pgh.pa.us 2702 : 0 : pg_fatal("input file location must be an absolute path");
2703 : :
8022 bruce@momjian.us 2704 :CBC 67 : canonicalize_path(share_path);
4904 2705 : 67 : }
2706 : :
2707 : : void
2708 : 66 : setup_locale_encoding(void)
2709 : : {
7965 peter_e@gmx.net 2710 : 66 : setlocales();
2711 : :
1510 peter@eisentraut.org 2712 [ + + ]: 63 : if (locale_provider == COLLPROVIDER_LIBC &&
2713 [ + - ]: 52 : strcmp(lc_ctype, lc_collate) == 0 &&
8212 bruce@momjian.us 2714 [ + - ]: 52 : strcmp(lc_ctype, lc_time) == 0 &&
2715 [ + + ]: 52 : strcmp(lc_ctype, lc_numeric) == 0 &&
2716 [ + - ]: 15 : strcmp(lc_ctype, lc_monetary) == 0 &&
1510 peter@eisentraut.org 2717 [ + + ]: 15 : strcmp(lc_ctype, lc_messages) == 0 &&
787 jdavis@postgresql.or 2718 [ - + - - ]: 14 : (!datlocale || strcmp(lc_ctype, datlocale) == 0))
5135 peter_e@gmx.net 2719 : 14 : printf(_("The database cluster will be initialized with locale \"%s\".\n"), lc_ctype);
2720 : : else
2721 : : {
1510 peter@eisentraut.org 2722 : 49 : printf(_("The database cluster will be initialized with this locale configuration:\n"));
778 jdavis@postgresql.or 2723 : 49 : printf(_(" locale provider: %s\n"), collprovider_name(locale_provider));
783 2724 [ + + ]: 49 : if (locale_provider != COLLPROVIDER_LIBC)
778 2725 : 11 : printf(_(" default collation: %s\n"), datlocale);
1510 peter@eisentraut.org 2726 : 49 : printf(_(" LC_COLLATE: %s\n"
2727 : : " LC_CTYPE: %s\n"
2728 : : " LC_MESSAGES: %s\n"
2729 : : " LC_MONETARY: %s\n"
2730 : : " LC_NUMERIC: %s\n"
2731 : : " LC_TIME: %s\n"),
2732 : : lc_collate,
2733 : : lc_ctype,
2734 : : lc_messages,
2735 : : lc_monetary,
2736 : : lc_numeric,
2737 : : lc_time);
2738 : : }
2739 : :
1152 jdavis@postgresql.or 2740 [ + + ]: 63 : if (!encoding)
2741 : : {
2742 : : int ctype_enc;
2743 : :
5565 peter_e@gmx.net 2744 : 47 : ctype_enc = pg_get_encoding_from_locale(lc_ctype, true);
2745 : :
2746 : : /*
2747 : : * If ctype_enc=SQL_ASCII, it's compatible with any encoding. ICU does
2748 : : * not support SQL_ASCII, so select UTF-8 instead.
2749 : : */
1152 jdavis@postgresql.or 2750 [ + + + + ]: 47 : if (locale_provider == COLLPROVIDER_ICU && ctype_enc == PG_SQL_ASCII)
2751 : 1 : ctype_enc = PG_UTF8;
2752 : :
6018 tgl@sss.pgh.pa.us 2753 [ - + ]: 47 : if (ctype_enc == -1)
2754 : : {
2755 : : /* Couldn't recognize the locale's codeset */
2591 peter@eisentraut.org 2756 :UBC 0 : pg_log_error("could not find suitable encoding for locale \"%s\"",
2757 : : lc_ctype);
1488 tgl@sss.pgh.pa.us 2758 : 0 : pg_log_error_hint("Rerun %s with the -E option.", progname);
2759 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
6794 2760 : 0 : exit(1);
2761 : : }
6779 tgl@sss.pgh.pa.us 2762 [ - + ]:CBC 47 : else if (!pg_valid_server_encoding_id(ctype_enc))
2763 : : {
2764 : : /*
2765 : : * We recognized it, but it's not a legal server encoding. On
2766 : : * Windows, UTF-8 works with any locale, so we can fall back to
2767 : : * UTF-8.
2768 : : */
2769 : : #ifdef WIN32
2770 : : encodingid = PG_UTF8;
2771 : : printf(_("Encoding \"%s\" implied by locale is not allowed as a server-side encoding.\n"
2772 : : "The default database encoding will be set to \"%s\" instead.\n"),
2773 : : pg_encoding_to_char(ctype_enc),
2774 : : pg_encoding_to_char(encodingid));
2775 : : #else
2591 peter@eisentraut.org 2776 :UBC 0 : pg_log_error("locale \"%s\" requires unsupported encoding \"%s\"",
2777 : : lc_ctype, pg_encoding_to_char(ctype_enc));
1488 tgl@sss.pgh.pa.us 2778 : 0 : pg_log_error_detail("Encoding \"%s\" is not allowed as a server-side encoding.",
2779 : : pg_encoding_to_char(ctype_enc));
2780 : 0 : pg_log_error_hint("Rerun %s with a different locale selection.",
2781 : : progname);
6793 2782 : 0 : exit(1);
2783 : : #endif
2784 : : }
2785 : : else
2786 : : {
3170 peter_e@gmx.net 2787 :CBC 47 : encodingid = ctype_enc;
5135 2788 : 47 : printf(_("The default database encoding has accordingly been set to \"%s\".\n"),
2789 : : pg_encoding_to_char(encodingid));
2790 : : }
2791 : : }
2792 : : else
6794 tgl@sss.pgh.pa.us 2793 : 16 : encodingid = get_encoding_id(encoding);
2794 : :
3170 peter_e@gmx.net 2795 [ + - ]: 63 : if (!check_locale_encoding(lc_ctype, encodingid) ||
2796 [ - + ]: 63 : !check_locale_encoding(lc_collate, encodingid))
6172 bruce@momjian.us 2797 :UBC 0 : exit(1); /* check_locale_encoding printed the error */
2798 : :
777 jdavis@postgresql.or 2799 [ + + ]:CBC 63 : if (locale_provider == COLLPROVIDER_BUILTIN)
2800 : : {
473 2801 [ + + ]: 5 : if ((strcmp(datlocale, "C.UTF-8") == 0 ||
2802 [ - + ]: 2 : strcmp(datlocale, "PG_UNICODE_FAST") == 0) &&
2803 [ + + ]: 3 : encodingid != PG_UTF8)
777 2804 : 1 : pg_fatal("builtin provider locale \"%s\" requires encoding \"%s\"",
2805 : : datlocale, "UTF-8");
2806 : : }
2807 : :
1327 peter@eisentraut.org 2808 [ + + ]: 62 : if (locale_provider == COLLPROVIDER_ICU &&
2809 [ + + ]: 6 : !check_icu_locale_encoding(encodingid))
2810 : 1 : exit(1);
4904 bruce@momjian.us 2811 : 61 : }
2812 : :
2813 : :
2814 : : void
2815 : 66 : setup_data_file_paths(void)
2816 : : {
2817 : 66 : set_input(&bki_file, "postgres.bki");
2818 : 66 : set_input(&hba_file, "pg_hba.conf.sample");
2819 : 66 : set_input(&ident_file, "pg_ident.conf.sample");
48 dgustafsson@postgres 2820 :GNC 66 : set_input(&hosts_file, "pg_hosts.conf.sample");
4904 bruce@momjian.us 2821 :CBC 66 : set_input(&conf_file, "postgresql.conf.sample");
2822 : 66 : set_input(&dictionary_file, "snowball_create.sql");
2823 : 66 : set_input(&info_schema_file, "information_schema.sql");
2824 : 66 : set_input(&features_file, "sql_features.txt");
1921 peter@eisentraut.org 2825 : 66 : set_input(&system_constraints_file, "system_constraints.sql");
1845 tgl@sss.pgh.pa.us 2826 : 66 : set_input(&system_functions_file, "system_functions.sql");
4904 bruce@momjian.us 2827 : 66 : set_input(&system_views_file, "system_views.sql");
2828 : :
2829 [ + - - + ]: 66 : if (show_setting || debug)
2830 : : {
4904 bruce@momjian.us 2831 :UBC 0 : fprintf(stderr,
2832 : : "VERSION=%s\n"
2833 : : "PGDATA=%s\nshare_path=%s\nPGPATH=%s\n"
2834 : : "POSTGRES_SUPERUSERNAME=%s\nPOSTGRES_BKI=%s\n"
2835 : : "POSTGRESQL_CONF_SAMPLE=%s\n"
2836 : : "PG_HBA_SAMPLE=%s\nPG_IDENT_SAMPLE=%s\nPG_HOSTS_SAMPLE=%s\n",
2837 : : PG_VERSION,
2838 : : pg_data, share_path, bin_path,
2839 : : username, bki_file,
2840 : : conf_file,
2841 : : hba_file, ident_file, hosts_file);
2842 [ # # ]: 0 : if (show_setting)
2843 : 0 : exit(0);
2844 : : }
2845 : :
4904 bruce@momjian.us 2846 :CBC 66 : check_input(bki_file);
2847 : 66 : check_input(hba_file);
2848 : 66 : check_input(ident_file);
48 dgustafsson@postgres 2849 :GNC 66 : check_input(hosts_file);
4904 bruce@momjian.us 2850 :CBC 66 : check_input(conf_file);
2851 : 66 : check_input(dictionary_file);
2852 : 66 : check_input(info_schema_file);
2853 : 66 : check_input(features_file);
1845 tgl@sss.pgh.pa.us 2854 : 66 : check_input(system_constraints_file);
2855 : 66 : check_input(system_functions_file);
4904 bruce@momjian.us 2856 : 66 : check_input(system_views_file);
2857 : 66 : }
2858 : :
2859 : :
2860 : : void
2861 : 61 : setup_text_search(void)
2862 : : {
3170 peter_e@gmx.net 2863 [ + + ]: 61 : if (!default_text_search_config)
2864 : : {
6832 tgl@sss.pgh.pa.us 2865 : 60 : default_text_search_config = find_matching_ts_config(lc_ctype);
3170 peter_e@gmx.net 2866 [ - + ]: 60 : if (!default_text_search_config)
2867 : : {
2500 peter@eisentraut.org 2868 :UBC 0 : pg_log_info("could not find suitable text search configuration for locale \"%s\"",
2869 : : lc_ctype);
6832 tgl@sss.pgh.pa.us 2870 : 0 : default_text_search_config = "simple";
2871 : : }
2872 : : }
2873 : : else
2874 : : {
6832 tgl@sss.pgh.pa.us 2875 :CBC 1 : const char *checkmatch = find_matching_ts_config(lc_ctype);
2876 : :
2877 [ - + ]: 1 : if (checkmatch == NULL)
2878 : : {
2500 peter@eisentraut.org 2879 :UBC 0 : pg_log_warning("suitable text search configuration for locale \"%s\" is unknown",
2880 : : lc_ctype);
2881 : : }
6832 tgl@sss.pgh.pa.us 2882 [ + - ]:CBC 1 : else if (strcmp(checkmatch, default_text_search_config) != 0)
2883 : : {
2500 peter@eisentraut.org 2884 : 1 : pg_log_warning("specified text search configuration \"%s\" might not match locale \"%s\"",
2885 : : default_text_search_config, lc_ctype);
2886 : : }
2887 : : }
2888 : :
6832 tgl@sss.pgh.pa.us 2889 : 61 : printf(_("The default text search configuration will be set to \"%s\".\n"),
2890 : : default_text_search_config);
4904 bruce@momjian.us 2891 : 61 : }
2892 : :
2893 : :
2894 : : void
4901 2895 : 61 : setup_signals(void)
2896 : : {
8212 2897 : 61 : pqsignal(SIGINT, trapsig);
2898 : 61 : pqsignal(SIGTERM, trapsig);
2899 : :
2900 : : /* the following are not valid on Windows */
2901 : : #ifndef WIN32
474 nathan@postgresql.or 2902 : 61 : pqsignal(SIGHUP, trapsig);
2903 : 61 : pqsignal(SIGQUIT, trapsig);
2904 : :
2905 : : /* Ignore SIGPIPE when writing to backend, so we can clean up */
21 andrew@dunslane.net 2906 :GNC 61 : pqsignal(SIGPIPE, PG_SIG_IGN);
2907 : :
2908 : : /* Prevent SIGSYS so we can probe for kernel calls that might not work */
2909 : 61 : pqsignal(SIGSYS, PG_SIG_IGN);
2910 : : #endif
4904 bruce@momjian.us 2911 :CBC 61 : }
2912 : :
2913 : :
2914 : : void
4901 2915 : 61 : create_data_directory(void)
2916 : : {
2917 : : int ret;
2918 : :
4826 2919 [ + + + - ]: 61 : switch ((ret = pg_check_dir(pg_data)))
2920 : : {
8208 tgl@sss.pgh.pa.us 2921 : 59 : case 0:
2922 : : /* PGDATA not there, must create it */
8199 peter_e@gmx.net 2923 : 59 : printf(_("creating directory %s ... "),
2924 : : pg_data);
8208 tgl@sss.pgh.pa.us 2925 : 59 : fflush(stdout);
2926 : :
2950 sfrost@snowman.net 2927 [ - + ]: 59 : if (pg_mkdir_p(pg_data, pg_dir_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 2928 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", pg_data);
2929 : : else
8208 tgl@sss.pgh.pa.us 2930 :CBC 59 : check_ok();
2931 : :
2932 : 59 : made_new_pgdata = true;
2933 : 59 : break;
2934 : :
2935 : 1 : case 1:
2936 : : /* Present but empty, fix permissions and use it */
8199 peter_e@gmx.net 2937 : 1 : printf(_("fixing permissions on existing directory %s ... "),
2938 : : pg_data);
8208 tgl@sss.pgh.pa.us 2939 : 1 : fflush(stdout);
2940 : :
2950 sfrost@snowman.net 2941 [ - + ]: 1 : if (chmod(pg_data, pg_dir_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 2942 :UBC 0 : pg_fatal("could not change permissions of directory \"%s\": %m",
2943 : : pg_data);
2944 : : else
8208 tgl@sss.pgh.pa.us 2945 :CBC 1 : check_ok();
2946 : :
2947 : 1 : found_existing_pgdata = true;
2948 : 1 : break;
2949 : :
2950 : 1 : case 2:
2951 : : case 3:
2952 : : case 4:
2953 : : /* Present and not empty */
2591 peter@eisentraut.org 2954 : 1 : pg_log_error("directory \"%s\" exists but is not empty", pg_data);
4826 bruce@momjian.us 2955 [ - + ]: 1 : if (ret != 4)
4826 bruce@momjian.us 2956 :UBC 0 : warn_on_mount_point(ret);
2957 : : else
1488 tgl@sss.pgh.pa.us 2958 :CBC 1 : pg_log_error_hint("If you want to create a new database system, either remove or empty "
2959 : : "the directory \"%s\" or run %s "
2960 : : "with an argument other than \"%s\".",
2961 : : pg_data, progname, pg_data);
8208 2962 : 1 : exit(1); /* no further message needed */
2963 : :
8208 tgl@sss.pgh.pa.us 2964 :UBC 0 : default:
2965 : : /* Trouble accessing directory */
1488 2966 : 0 : pg_fatal("could not access directory \"%s\": %m", pg_data);
2967 : : }
4901 bruce@momjian.us 2968 :CBC 60 : }
2969 : :
2970 : :
2971 : : /* Create WAL directory, and symlink if required */
2972 : : void
3771 tgl@sss.pgh.pa.us 2973 : 60 : create_xlog_or_symlink(void)
2974 : : {
2975 : : char *subdirloc;
2976 : :
2977 : : /* form name of the place for the subdirectory or symlink */
3484 rhaas@postgresql.org 2978 : 60 : subdirloc = psprintf("%s/pg_wal", pg_data);
2979 : :
3170 peter_e@gmx.net 2980 [ + + ]: 60 : if (xlog_dir)
2981 : : {
2982 : : int ret;
2983 : :
2984 : : /* clean up xlog directory name, check it's absolute */
6546 tgl@sss.pgh.pa.us 2985 : 3 : canonicalize_path(xlog_dir);
2986 [ + + ]: 3 : if (!is_absolute_path(xlog_dir))
1488 2987 : 1 : pg_fatal("WAL directory location must be an absolute path");
2988 : :
2989 : : /* check if the specified xlog directory exists/is empty */
4826 bruce@momjian.us 2990 [ - + + - ]: 2 : switch ((ret = pg_check_dir(xlog_dir)))
2991 : : {
7059 bruce@momjian.us 2992 :UBC 0 : case 0:
2993 : : /* xlog directory not there, must create it */
2994 : 0 : printf(_("creating directory %s ... "),
2995 : : xlog_dir);
2996 : 0 : fflush(stdout);
2997 : :
2950 sfrost@snowman.net 2998 [ # # ]: 0 : if (pg_mkdir_p(xlog_dir, pg_dir_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 2999 : 0 : pg_fatal("could not create directory \"%s\": %m",
3000 : : xlog_dir);
3001 : : else
7059 bruce@momjian.us 3002 : 0 : check_ok();
3003 : :
3004 : 0 : made_new_xlogdir = true;
3005 : 0 : break;
3006 : :
7059 bruce@momjian.us 3007 :CBC 1 : case 1:
3008 : : /* Present but empty, fix permissions and use it */
3009 : 1 : printf(_("fixing permissions on existing directory %s ... "),
3010 : : xlog_dir);
3011 : 1 : fflush(stdout);
3012 : :
2950 sfrost@snowman.net 3013 [ - + ]: 1 : if (chmod(xlog_dir, pg_dir_create_mode) != 0)
1488 tgl@sss.pgh.pa.us 3014 :UBC 0 : pg_fatal("could not change permissions of directory \"%s\": %m",
3015 : : xlog_dir);
3016 : : else
7059 bruce@momjian.us 3017 :CBC 1 : check_ok();
3018 : :
3019 : 1 : found_existing_xlogdir = true;
3020 : 1 : break;
3021 : :
3022 : 1 : case 2:
3023 : : case 3:
3024 : : case 4:
3025 : : /* Present and not empty */
2591 peter@eisentraut.org 3026 : 1 : pg_log_error("directory \"%s\" exists but is not empty", xlog_dir);
4826 bruce@momjian.us 3027 [ + - ]: 1 : if (ret != 4)
3028 : 1 : warn_on_mount_point(ret);
3029 : : else
1488 tgl@sss.pgh.pa.us 3030 :UBC 0 : pg_log_error_hint("If you want to store the WAL there, either remove or empty the directory \"%s\".",
3031 : : xlog_dir);
2684 peter@eisentraut.org 3032 :CBC 1 : exit(1);
3033 : :
7059 bruce@momjian.us 3034 :UBC 0 : default:
3035 : : /* Trouble accessing directory */
1488 tgl@sss.pgh.pa.us 3036 : 0 : pg_fatal("could not access directory \"%s\": %m", xlog_dir);
3037 : : }
3038 : :
3771 tgl@sss.pgh.pa.us 3039 [ - + ]:CBC 1 : if (symlink(xlog_dir, subdirloc) != 0)
1488 tgl@sss.pgh.pa.us 3040 :UBC 0 : pg_fatal("could not create symbolic link \"%s\": %m",
3041 : : subdirloc);
3042 : : }
3043 : : else
3044 : : {
3045 : : /* Without -X option, just make the subdirectory normally */
2950 sfrost@snowman.net 3046 [ - + ]:CBC 57 : if (mkdir(subdirloc, pg_dir_create_mode) < 0)
1488 tgl@sss.pgh.pa.us 3047 :UBC 0 : pg_fatal("could not create directory \"%s\": %m",
3048 : : subdirloc);
3049 : : }
3050 : :
3771 tgl@sss.pgh.pa.us 3051 :CBC 58 : free(subdirloc);
4901 bruce@momjian.us 3052 : 58 : }
3053 : :
3054 : :
3055 : : void
4826 3056 : 1 : warn_on_mount_point(int error)
3057 : : {
3058 [ - + ]: 1 : if (error == 2)
1488 tgl@sss.pgh.pa.us 3059 :UBC 0 : pg_log_error_detail("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.");
4826 bruce@momjian.us 3060 [ + - ]:CBC 1 : else if (error == 3)
1488 tgl@sss.pgh.pa.us 3061 : 1 : pg_log_error_detail("It contains a lost+found directory, perhaps due to it being a mount point.");
3062 : :
3063 : 1 : pg_log_error_hint("Using a mount point directly as the data directory is not recommended.\n"
3064 : : "Create a subdirectory under the mount point.");
4826 bruce@momjian.us 3065 : 1 : }
3066 : :
3067 : :
3068 : : void
4901 3069 : 61 : initialize_data_directory(void)
3070 : : {
3071 : : PG_CMD_DECL;
3072 : : PQExpBufferData cmd;
3073 : : int i;
3074 : :
3075 : 61 : setup_signals();
3076 : :
3077 : : /*
3078 : : * Set mask based on requested PGDATA permissions. pg_mode_mask, and
3079 : : * friends like pg_dir_create_mode, are set to owner-only by default and
3080 : : * then updated if -g is passed in by calling SetDataDirectoryCreatePerm()
3081 : : * when parsing our options (see above).
3082 : : */
2950 sfrost@snowman.net 3083 : 61 : umask(pg_mode_mask);
3084 : :
4901 bruce@momjian.us 3085 : 61 : create_data_directory();
3086 : :
3771 tgl@sss.pgh.pa.us 3087 : 60 : create_xlog_or_symlink();
3088 : :
3089 : : /* Create required subdirectories (other than pg_wal) */
7403 3090 : 58 : printf(_("creating subdirectories ... "));
3091 : 58 : fflush(stdout);
3092 : :
3771 3093 [ + + ]: 1392 : for (i = 0; i < lengthof(subdirs); i++)
3094 : : {
3095 : : char *path;
3096 : :
3097 : 1334 : path = psprintf("%s/%s", pg_data, subdirs[i]);
3098 : :
3099 : : /*
3100 : : * The parent directory already exists, so we only need mkdir() not
3101 : : * pg_mkdir_p() here, which avoids some failure modes; cf bug #13853.
3102 : : */
2950 sfrost@snowman.net 3103 [ - + ]: 1334 : if (mkdir(path, pg_dir_create_mode) < 0)
1488 tgl@sss.pgh.pa.us 3104 :UBC 0 : pg_fatal("could not create directory \"%s\": %m", path);
3105 : :
3771 tgl@sss.pgh.pa.us 3106 :CBC 1334 : free(path);
3107 : : }
3108 : :
7403 3109 : 58 : check_ok();
3110 : :
3111 : : /* Top level PG_VERSION is checked by bootstrapper, so make it first */
5963 bruce@momjian.us 3112 : 58 : write_version_file(NULL);
3113 : :
3114 : : /* Select suitable configuration settings */
8212 3115 : 58 : set_null_conf();
7430 tgl@sss.pgh.pa.us 3116 : 58 : test_config_settings();
3117 : :
3118 : : /* Now create all the text config files */
8212 bruce@momjian.us 3119 : 58 : setup_config();
3120 : :
3121 : : /* Bootstrap template1 */
5963 3122 : 58 : bootstrap_template1();
3123 : :
3124 : : /*
3125 : : * Make the per-database PG_VERSION for template1 only after init'ing it
3126 : : */
3127 : 57 : write_version_file("base/1");
3128 : :
3129 : : /*
3130 : : * Create the stuff we don't need to use bootstrap mode for, using a
3131 : : * backend running in simple standalone mode.
3132 : : */
3792 tgl@sss.pgh.pa.us 3133 : 57 : fputs(_("performing post-bootstrap initialization ... "), stdout);
3134 : 57 : fflush(stdout);
3135 : :
1035 peter@eisentraut.org 3136 : 57 : initPQExpBuffer(&cmd);
3137 : 57 : printfPQExpBuffer(&cmd, "\"%s\" %s %s template1 >%s",
3138 : : backend_exec, backend_options, extra_options, DEVNULL);
3139 : :
3140 [ - + ]: 57 : PG_CMD_OPEN(cmd.data);
3141 : :
3792 tgl@sss.pgh.pa.us 3142 : 57 : setup_auth(cmdfd);
3143 : :
1921 peter@eisentraut.org 3144 : 57 : setup_run_file(cmdfd, system_constraints_file);
3145 : :
1845 tgl@sss.pgh.pa.us 3146 : 57 : setup_run_file(cmdfd, system_functions_file);
3147 : :
3792 3148 : 57 : setup_depend(cmdfd);
3149 : :
3150 : : /*
3151 : : * Note that no objects created after setup_depend() will be "pinned".
3152 : : * They are all droppable at the whim of the DBA.
3153 : : */
3154 : :
1921 peter@eisentraut.org 3155 : 57 : setup_run_file(cmdfd, system_views_file);
3156 : :
3792 tgl@sss.pgh.pa.us 3157 : 57 : setup_description(cmdfd);
3158 : :
3159 : 57 : setup_collation(cmdfd);
3160 : :
1921 peter@eisentraut.org 3161 : 57 : setup_run_file(cmdfd, dictionary_file);
3162 : :
3792 tgl@sss.pgh.pa.us 3163 : 57 : setup_privileges(cmdfd);
3164 : :
3165 : 57 : setup_schema(cmdfd);
3166 : :
3167 : 57 : load_plpgsql(cmdfd);
3168 : :
3169 : 57 : vacuum_db(cmdfd);
3170 : :
3171 : 57 : make_template0(cmdfd);
3172 : :
3173 : 57 : make_postgres(cmdfd);
3174 : :
1035 peter@eisentraut.org 3175 [ + + ]: 57 : PG_CMD_CLOSE();
3176 : 55 : termPQExpBuffer(&cmd);
3177 : :
3792 tgl@sss.pgh.pa.us 3178 : 55 : check_ok();
4904 bruce@momjian.us 3179 : 55 : }
3180 : :
3181 : :
3182 : : int
3183 : 98 : main(int argc, char *argv[])
3184 : : {
3185 : : static struct option long_options[] = {
3186 : : {"pgdata", required_argument, NULL, 'D'},
3187 : : {"encoding", required_argument, NULL, 'E'},
3188 : : {"locale", required_argument, NULL, 1},
3189 : : {"lc-collate", required_argument, NULL, 2},
3190 : : {"lc-ctype", required_argument, NULL, 3},
3191 : : {"lc-monetary", required_argument, NULL, 4},
3192 : : {"lc-numeric", required_argument, NULL, 5},
3193 : : {"lc-time", required_argument, NULL, 6},
3194 : : {"lc-messages", required_argument, NULL, 7},
3195 : : {"no-locale", no_argument, NULL, 8},
3196 : : {"text-search-config", required_argument, NULL, 'T'},
3197 : : {"auth", required_argument, NULL, 'A'},
3198 : : {"auth-local", required_argument, NULL, 10},
3199 : : {"auth-host", required_argument, NULL, 11},
3200 : : {"pwprompt", no_argument, NULL, 'W'},
3201 : : {"pwfile", required_argument, NULL, 9},
3202 : : {"username", required_argument, NULL, 'U'},
3203 : : {"help", no_argument, NULL, '?'},
3204 : : {"version", no_argument, NULL, 'V'},
3205 : : {"debug", no_argument, NULL, 'd'},
3206 : : {"show", no_argument, NULL, 's'},
3207 : : {"noclean", no_argument, NULL, 'n'}, /* for backwards compatibility */
3208 : : {"no-clean", no_argument, NULL, 'n'},
3209 : : {"nosync", no_argument, NULL, 'N'}, /* for backwards compatibility */
3210 : : {"no-sync", no_argument, NULL, 'N'},
3211 : : {"no-instructions", no_argument, NULL, 13},
3212 : : {"set", required_argument, NULL, 'c'},
3213 : : {"sync-only", no_argument, NULL, 'S'},
3214 : : {"waldir", required_argument, NULL, 'X'},
3215 : : {"wal-segsize", required_argument, NULL, 12},
3216 : : {"data-checksums", no_argument, NULL, 'k'},
3217 : : {"allow-group-access", no_argument, NULL, 'g'},
3218 : : {"discard-caches", no_argument, NULL, 14},
3219 : : {"locale-provider", required_argument, NULL, 15},
3220 : : {"builtin-locale", required_argument, NULL, 16},
3221 : : {"icu-locale", required_argument, NULL, 17},
3222 : : {"icu-rules", required_argument, NULL, 18},
3223 : : {"sync-method", required_argument, NULL, 19},
3224 : : {"no-data-checksums", no_argument, NULL, 20},
3225 : : {"no-sync-data-files", no_argument, NULL, 21},
3226 : : {NULL, 0, NULL, 0}
3227 : : };
3228 : :
3229 : : /*
3230 : : * options with no short version return a low integer, the rest return
3231 : : * their short version value
3232 : : */
3233 : : int c;
3234 : : int option_index;
3235 : : char *effective_user;
3236 : : PQExpBuffer start_db_cmd;
3237 : : char pg_ctl_path[MAXPGPATH];
3238 : :
3239 : : /*
3240 : : * Ensure that buffering behavior of stdout matches what it is in
3241 : : * interactive usage (at least on most platforms). This prevents
3242 : : * unexpected output ordering when, eg, output is redirected to a file.
3243 : : * POSIX says we must do this before any other usage of these files.
3244 : : */
4373 tgl@sss.pgh.pa.us 3245 : 98 : setvbuf(stdout, NULL, PG_IOLBF, 0);
3246 : :
2591 peter@eisentraut.org 3247 : 98 : pg_logging_init(argv[0]);
4904 bruce@momjian.us 3248 : 98 : progname = get_progname(argv[0]);
3249 : 98 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("initdb"));
3250 : :
3251 [ + - ]: 98 : if (argc > 1)
3252 : : {
3253 [ + + - + ]: 98 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
3254 : : {
3255 : 1 : usage(progname);
3256 : 1 : exit(0);
3257 : : }
3258 [ + + + + ]: 97 : if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
3259 : : {
3260 : 21 : puts("initdb (PostgreSQL) " PG_VERSION);
3261 : 21 : exit(0);
3262 : : }
3263 : : }
3264 : :
3265 : : /* process command-line options */
3266 : :
1140 tgl@sss.pgh.pa.us 3267 : 359 : while ((c = getopt_long(argc, argv, "A:c:dD:E:gkL:nNsST:U:WX:",
3268 [ + + ]: 359 : long_options, &option_index)) != -1)
3269 : : {
4904 bruce@momjian.us 3270 [ + - - + : 285 : switch (c)
+ + - + -
+ + + + -
+ + + + +
+ + + - -
+ + + + +
- + + + +
+ + + + ]
3271 : : {
3272 : 44 : case 'A':
3273 : 44 : authmethodlocal = authmethodhost = pg_strdup(optarg);
3274 : :
3275 : : /*
3276 : : * When ident is specified, use peer for local connections.
3277 : : * Mirrored, when peer is specified, use ident for TCP/IP
3278 : : * connections.
3279 : : */
3280 [ - + ]: 44 : if (strcmp(authmethodhost, "ident") == 0)
4904 bruce@momjian.us 3281 :UBC 0 : authmethodlocal = "peer";
4904 bruce@momjian.us 3282 [ - + ]:CBC 44 : else if (strcmp(authmethodlocal, "peer") == 0)
4904 bruce@momjian.us 3283 :UBC 0 : authmethodhost = "ident";
4904 bruce@momjian.us 3284 :CBC 44 : break;
4904 bruce@momjian.us 3285 :UBC 0 : case 10:
3286 : 0 : authmethodlocal = pg_strdup(optarg);
3287 : 0 : break;
3288 : 0 : case 11:
3289 : 0 : authmethodhost = pg_strdup(optarg);
3290 : 0 : break;
1140 tgl@sss.pgh.pa.us 3291 :CBC 9 : case 'c':
3292 : : {
3293 : 9 : char *buf = pg_strdup(optarg);
3294 : 9 : char *equals = strchr(buf, '=');
3295 : :
3296 [ - + ]: 9 : if (!equals)
3297 : : {
1140 tgl@sss.pgh.pa.us 3298 :UBC 0 : pg_log_error("-c %s requires a value", buf);
3299 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.",
3300 : : progname);
3301 : 0 : exit(1);
3302 : : }
1140 tgl@sss.pgh.pa.us 3303 :CBC 9 : *equals++ = '\0'; /* terminate variable name */
3304 : 9 : add_stringlist_item(&extra_guc_names, buf);
3305 : 9 : add_stringlist_item(&extra_guc_values, equals);
3306 : 9 : pfree(buf);
3307 : : }
3308 : 9 : break;
4904 bruce@momjian.us 3309 : 45 : case 'D':
3310 : 45 : pg_data = pg_strdup(optarg);
3311 : 45 : break;
3312 : 16 : case 'E':
3313 : 16 : encoding = pg_strdup(optarg);
3314 : 16 : break;
4904 bruce@momjian.us 3315 :UBC 0 : case 'W':
3316 : 0 : pwprompt = true;
3317 : 0 : break;
4904 bruce@momjian.us 3318 :CBC 4 : case 'U':
3319 : 4 : username = pg_strdup(optarg);
3320 : 4 : break;
4904 bruce@momjian.us 3321 :UBC 0 : case 'd':
3322 : 0 : debug = true;
3323 : 0 : printf(_("Running in debug mode.\n"));
3324 : 0 : break;
4904 bruce@momjian.us 3325 :CBC 3 : case 'n':
3326 : 3 : noclean = true;
3485 peter_e@gmx.net 3327 : 3 : printf(_("Running in no-clean mode. Mistakes will not be cleaned up.\n"));
4904 bruce@momjian.us 3328 : 3 : break;
3329 : 65 : case 'N':
3330 : 65 : do_sync = false;
3331 : 65 : break;
4901 3332 : 4 : case 'S':
3333 : 4 : sync_only = true;
3334 : 4 : break;
4792 simon@2ndQuadrant.co 3335 : 2 : case 'k':
3336 : 2 : data_checksums = true;
3337 : 2 : break;
4904 bruce@momjian.us 3338 :UBC 0 : case 'L':
3339 : 0 : share_path = pg_strdup(optarg);
3340 : 0 : break;
4904 bruce@momjian.us 3341 :CBC 15 : case 1:
3342 : 15 : locale = pg_strdup(optarg);
3343 : 15 : break;
3344 : 4 : case 2:
3345 : 4 : lc_collate = pg_strdup(optarg);
3346 : 4 : break;
3347 : 5 : case 3:
3348 : 5 : lc_ctype = pg_strdup(optarg);
3349 : 5 : break;
3350 : 1 : case 4:
3351 : 1 : lc_monetary = pg_strdup(optarg);
3352 : 1 : break;
3353 : 1 : case 5:
3354 : 1 : lc_numeric = pg_strdup(optarg);
3355 : 1 : break;
3356 : 1 : case 6:
3357 : 1 : lc_time = pg_strdup(optarg);
3358 : 1 : break;
3359 : 2 : case 7:
3360 : 2 : lc_messages = pg_strdup(optarg);
3361 : 2 : break;
3362 : 2 : case 8:
3363 : 2 : locale = "C";
3364 : 2 : break;
4904 bruce@momjian.us 3365 :UBC 0 : case 9:
3366 : 0 : pwfilename = pg_strdup(optarg);
3367 : 0 : break;
3368 : 0 : case 's':
3369 : 0 : show_setting = true;
3370 : 0 : break;
4904 bruce@momjian.us 3371 :CBC 1 : case 'T':
3372 : 1 : default_text_search_config = pg_strdup(optarg);
3373 : 1 : break;
3374 : 3 : case 'X':
3375 : 3 : xlog_dir = pg_strdup(optarg);
3376 : 3 : break;
3150 andres@anarazel.de 3377 : 6 : case 12:
981 peter@eisentraut.org 3378 [ - + ]: 6 : if (!option_parse_int(optarg, "--wal-segsize", 1, 1024, &wal_segment_size_mb))
981 peter@eisentraut.org 3379 :UBC 0 : exit(1);
3150 andres@anarazel.de 3380 :CBC 6 : break;
1934 magnus@hagander.net 3381 : 1 : case 13:
3382 : 1 : noinstructions = true;
3383 : 1 : break;
2950 sfrost@snowman.net 3384 : 5 : case 'g':
3385 : 5 : SetDataDirectoryCreatePerm(PG_DIR_MODE_GROUP);
3386 : 5 : break;
1769 tgl@sss.pgh.pa.us 3387 :UBC 0 : case 14:
3388 : 0 : extra_options = psprintf("%s %s",
3389 : : extra_options,
3390 : : "-c debug_discard_caches=1");
3391 : 0 : break;
1510 peter@eisentraut.org 3392 :CBC 19 : case 15:
783 jdavis@postgresql.or 3393 [ + + ]: 19 : if (strcmp(optarg, "builtin") == 0)
3394 : 8 : locale_provider = COLLPROVIDER_BUILTIN;
3395 [ + + ]: 11 : else if (strcmp(optarg, "icu") == 0)
1510 peter@eisentraut.org 3396 : 8 : locale_provider = COLLPROVIDER_ICU;
3397 [ + + ]: 3 : else if (strcmp(optarg, "libc") == 0)
3398 : 2 : locale_provider = COLLPROVIDER_LIBC;
3399 : : else
1488 tgl@sss.pgh.pa.us 3400 : 1 : pg_fatal("unrecognized locale provider: %s", optarg);
1510 peter@eisentraut.org 3401 : 18 : break;
3402 : 3 : case 16:
787 jdavis@postgresql.or 3403 : 3 : datlocale = pg_strdup(optarg);
783 3404 : 3 : builtin_locale_specified = true;
1510 peter@eisentraut.org 3405 : 3 : break;
1154 3406 : 8 : case 17:
783 jdavis@postgresql.or 3407 : 8 : datlocale = pg_strdup(optarg);
3408 : 8 : icu_locale_specified = true;
1154 peter@eisentraut.org 3409 : 8 : break;
972 nathan@postgresql.or 3410 : 1 : case 18:
783 jdavis@postgresql.or 3411 : 1 : icu_rules = pg_strdup(optarg);
3412 : 1 : break;
3413 : 1 : case 19:
972 nathan@postgresql.or 3414 [ - + ]: 1 : if (!parse_sync_method(optarg, &sync_method))
972 nathan@postgresql.or 3415 :UBC 0 : exit(1);
972 nathan@postgresql.or 3416 :CBC 1 : break;
581 peter@eisentraut.org 3417 : 12 : case 20:
3418 : 12 : data_checksums = false;
3419 : 12 : break;
406 nathan@postgresql.or 3420 : 1 : case 21:
3421 : 1 : sync_data_files = false;
3422 : 1 : break;
4904 bruce@momjian.us 3423 : 1 : default:
3424 : : /* getopt_long already emitted a complaint */
1488 tgl@sss.pgh.pa.us 3425 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4904 bruce@momjian.us 3426 : 1 : exit(1);
3427 : : }
3428 : : }
3429 : :
3430 : :
3431 : : /*
3432 : : * Non-option argument specifies data directory as long as it wasn't
3433 : : * already specified with -D / --pgdata
3434 : : */
3170 peter_e@gmx.net 3435 [ + + + - ]: 74 : if (optind < argc && !pg_data)
3436 : : {
4904 bruce@momjian.us 3437 : 29 : pg_data = pg_strdup(argv[optind]);
3438 : 29 : optind++;
3439 : : }
3440 : :
3441 [ - + ]: 74 : if (optind < argc)
3442 : : {
2591 peter@eisentraut.org 3443 :UBC 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
3444 : : argv[optind]);
1488 tgl@sss.pgh.pa.us 3445 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
4904 bruce@momjian.us 3446 : 0 : exit(1);
3447 : : }
3448 : :
783 jdavis@postgresql.or 3449 [ + + - + ]:CBC 74 : if (builtin_locale_specified && locale_provider != COLLPROVIDER_BUILTIN)
783 jdavis@postgresql.or 3450 :UBC 0 : pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3451 : : "--builtin-locale", "builtin");
3452 : :
783 jdavis@postgresql.or 3453 [ + + + + ]:CBC 74 : if (icu_locale_specified && locale_provider != COLLPROVIDER_ICU)
1488 tgl@sss.pgh.pa.us 3454 : 2 : pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3455 : : "--icu-locale", "icu");
3456 : :
1154 peter@eisentraut.org 3457 [ + + + - ]: 72 : if (icu_rules && locale_provider != COLLPROVIDER_ICU)
3458 : 1 : pg_fatal("%s cannot be specified unless locale provider \"%s\" is chosen",
3459 : : "--icu-rules", "icu");
3460 : :
2684 3461 : 71 : atexit(cleanup_directories_atexit);
3462 : :
3463 : : /* If we only need to sync, just do it and exit */
4901 bruce@momjian.us 3464 [ + + ]: 71 : if (sync_only)
3465 : : {
3466 : 4 : setup_pgdata();
3467 : :
3468 : : /* must check that directory is readable */
3994 tgl@sss.pgh.pa.us 3469 [ + + ]: 4 : if (pg_check_dir(pg_data) <= 0)
1488 3470 : 1 : pg_fatal("could not access directory \"%s\": %m", pg_data);
3471 : :
3505 peter_e@gmx.net 3472 : 3 : fputs(_("syncing data to disk ... "), stdout);
3473 : 3 : fflush(stdout);
406 nathan@postgresql.or 3474 : 3 : sync_pgdata(pg_data, PG_VERSION_NUM, sync_method, sync_data_files);
3505 peter_e@gmx.net 3475 : 3 : check_ok();
4901 bruce@momjian.us 3476 : 3 : return 0;
3477 : : }
3478 : :
4904 3479 [ - + - - ]: 67 : if (pwprompt && pwfilename)
1488 tgl@sss.pgh.pa.us 3480 :UBC 0 : pg_fatal("password prompt and password file cannot be specified together");
3481 : :
2479 peter@eisentraut.org 3482 :CBC 67 : check_authmethod_unspecified(&authmethodlocal);
3483 : 67 : check_authmethod_unspecified(&authmethodhost);
3484 : :
4904 bruce@momjian.us 3485 : 67 : check_authmethod_valid(authmethodlocal, auth_methods_local, "local");
3486 : 67 : check_authmethod_valid(authmethodhost, auth_methods_host, "host");
3487 : :
3488 : 67 : check_need_password(authmethodlocal, authmethodhost);
3489 : :
981 peter@eisentraut.org 3490 [ + - + - : 67 : if (!IsValidWalSegSize(wal_segment_size_mb * 1024 * 1024))
+ - - + ]
980 dgustafsson@postgres 3491 :UBC 0 : pg_fatal("argument of %s must be a power of two between 1 and 1024", "--wal-segsize");
3492 : :
2591 peter@eisentraut.org 3493 :CBC 67 : get_restricted_token();
3494 : :
4904 bruce@momjian.us 3495 : 67 : setup_pgdata();
3496 : :
3497 : 67 : setup_bin_paths(argv[0]);
3498 : :
3499 : 67 : effective_user = get_id();
3170 peter_e@gmx.net 3500 [ + + ]: 67 : if (!username)
4904 bruce@momjian.us 3501 : 63 : username = effective_user;
3502 : :
3649 sfrost@snowman.net 3503 [ + + ]: 67 : if (strncmp(username, "pg_", 3) == 0)
1488 tgl@sss.pgh.pa.us 3504 : 1 : pg_fatal("superuser name \"%s\" is disallowed; role names cannot begin with \"pg_\"", username);
3505 : :
4904 bruce@momjian.us 3506 : 66 : printf(_("The files belonging to this database system will be owned "
3507 : : "by user \"%s\".\n"
3508 : : "This user must also own the server process.\n\n"),
3509 : : effective_user);
3510 : :
3511 : 66 : set_info_version();
3512 : :
3513 : 66 : setup_data_file_paths();
3514 : :
3515 : 66 : setup_locale_encoding();
3516 : :
3517 : 61 : setup_text_search();
3518 : :
4704 peter_e@gmx.net 3519 : 61 : printf("\n");
3520 : :
4792 simon@2ndQuadrant.co 3521 [ + + ]: 61 : if (data_checksums)
3522 : 49 : printf(_("Data page checksums are enabled.\n"));
3523 : : else
3524 : 12 : printf(_("Data page checksums are disabled.\n"));
3525 : :
3535 tgl@sss.pgh.pa.us 3526 [ + - - + ]: 61 : if (pwprompt || pwfilename)
3535 tgl@sss.pgh.pa.us 3527 :UBC 0 : get_su_pwd();
3528 : :
4904 bruce@momjian.us 3529 :CBC 61 : printf("\n");
3530 : :
4901 3531 : 61 : initialize_data_directory();
3532 : :
4900 3533 [ + + ]: 55 : if (do_sync)
3534 : : {
3505 peter_e@gmx.net 3535 : 2 : fputs(_("syncing data to disk ... "), stdout);
3536 : 2 : fflush(stdout);
406 nathan@postgresql.or 3537 : 2 : sync_pgdata(pg_data, PG_VERSION_NUM, sync_method, sync_data_files);
3505 peter_e@gmx.net 3538 : 2 : check_ok();
3539 : : }
3540 : : else
4900 bruce@momjian.us 3541 : 53 : printf(_("\nSync to disk skipped.\nThe data directory might become corrupt if the operating system crashes.\n"));
3542 : :
2479 peter@eisentraut.org 3543 [ + + ]: 55 : if (authwarning)
3544 : : {
3545 : 11 : printf("\n");
3546 : 11 : pg_log_warning("enabling \"trust\" authentication for local connections");
1488 tgl@sss.pgh.pa.us 3547 : 11 : pg_log_warning_hint("You can change this by editing pg_hba.conf or using the option -A, or "
3548 : : "--auth-local and --auth-host, the next time you run initdb.");
3549 : : }
3550 : :
1934 magnus@hagander.net 3551 [ + + ]: 55 : if (!noinstructions)
3552 : : {
3553 : : /*
3554 : : * Build up a shell command to tell the user how to start the server
3555 : : */
3556 : 54 : start_db_cmd = createPQExpBuffer();
3557 : :
3558 : : /* Get directory specification used to start initdb ... */
3559 : 54 : strlcpy(pg_ctl_path, argv[0], sizeof(pg_ctl_path));
3560 : 54 : canonicalize_path(pg_ctl_path);
3561 : 54 : get_parent_directory(pg_ctl_path);
3562 : : /* ... and tag on pg_ctl instead */
3563 : 54 : join_path_components(pg_ctl_path, pg_ctl_path, "pg_ctl");
3564 : :
3565 : : /* Convert the path to use native separators */
1890 alvherre@alvh.no-ip. 3566 : 54 : make_native_path(pg_ctl_path);
3567 : :
3568 : : /* path to pg_ctl, properly quoted */
1934 magnus@hagander.net 3569 : 54 : appendShellString(start_db_cmd, pg_ctl_path);
3570 : :
3571 : : /* add -D switch, with properly quoted data directory */
3572 : 54 : appendPQExpBufferStr(start_db_cmd, " -D ");
3573 : 54 : appendShellString(start_db_cmd, pgdata_native);
3574 : :
3575 : : /* add suggested -l switch and "start" command */
3576 : : /* translator: This is a placeholder in a shell command. */
3577 : 54 : appendPQExpBuffer(start_db_cmd, " -l %s start", _("logfile"));
3578 : :
3579 : 54 : printf(_("\nSuccess. You can now start the database server using:\n\n"
3580 : : " %s\n\n"),
3581 : : start_db_cmd->data);
3582 : :
3583 : 54 : destroyPQExpBuffer(start_db_cmd);
3584 : : }
3585 : :
3586 : :
2684 peter@eisentraut.org 3587 : 55 : success = true;
8212 bruce@momjian.us 3588 : 55 : return 0;
3589 : : }
|