1 /*-------------------------------------------------------------------------
4 * miscellanious initialization support stuff
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/utils/init/miscinit.c,v 1.51 2000/07/02 15:20:56 petere Exp $
13 *-------------------------------------------------------------------------
17 #include <sys/param.h>
18 #include <sys/types.h>
29 #include "catalog/catname.h"
30 #include "catalog/pg_shadow.h"
31 #include "miscadmin.h"
32 #include "utils/builtins.h"
33 #include "utils/syscache.h"
35 static char *GetPidFname(void);
39 unsigned char RecodeForwTable[128];
40 unsigned char RecodeBackTable[128];
44 ProcessingMode Mode = InitProcessing;
46 /* ----------------------------------------------------------------
47 * ignoring system indexes support stuff
48 * ----------------------------------------------------------------
51 static bool isIgnoringSystemIndexes = false;
54 * IsIgnoringSystemIndexes
55 * True if ignoring system indexes.
58 IsIgnoringSystemIndexes()
60 return isIgnoringSystemIndexes;
65 * Set true or false whether PostgreSQL ignores system indexes.
69 IgnoreSystemIndexes(bool mode)
71 isIgnoringSystemIndexes = mode;
74 /* ----------------------------------------------------------------
75 * database path / name support stuff
76 * ----------------------------------------------------------------
80 SetDatabasePath(const char *path)
83 /* use strdup since this is done before memory contexts are set up */
86 DatabasePath = strdup(path);
87 AssertState(DatabasePath);
92 SetDatabaseName(const char *name)
97 DatabaseName = strdup(name);
98 AssertState(DatabaseName);
103 /* even if MULTIBYTE is not enabled, these functions are necessary
104 * since pg_proc.h has references to them.
108 getdatabaseencoding(PG_FUNCTION_ARGS)
110 PG_RETURN_NAME("SQL_ASCII");
114 PG_encoding_to_char(PG_FUNCTION_ARGS)
116 PG_RETURN_NAME("SQL_ASCII");
120 PG_char_to_encoding(PG_FUNCTION_ARGS)
130 /* Some standard C libraries, including GNU, have an isblank() function.
131 Others, including Solaris, do not. So we have our own.
134 isblank(const char c)
136 return c == ' ' || c == 9 /* tab */ ;
140 next_token(FILE *fp, char *buf, const int bufsz)
142 /*--------------------------------------------------------------------------
143 Grab one token out of fp. Tokens are strings of non-blank
144 characters bounded by blank characters, beginning of line, and end
145 of line. Blank means space or tab. Return the token as *buf.
146 Leave file positioned to character immediately after the token or
147 EOF, whichever comes first. If no more tokens on line, return null
148 string as *buf and position file to beginning of next line or EOF,
149 whichever comes first.
150 --------------------------------------------------------------------------*/
152 char *eb = buf + (bufsz - 1);
154 /* Move over inital token-delimiting blanks */
155 while (isblank(c = getc(fp)));
161 * build a token in buf of next characters up to EOF, eol, or
164 while (c != EOF && c != '\n' && !isblank(c))
171 * Put back the char right after the token (putting back EOF
181 read_through_eol(FILE *file)
187 while (c != '\n' && c != EOF);
200 unsigned char FromChar,
203 for (i = 0; i < 128; i++)
205 RecodeForwTable[i] = i + 128;
206 RecodeBackTable[i] = i + 128;
209 p = getenv("PG_RECODETABLE");
212 map_file = (char *) malloc((strlen(DataDir) +
213 strlen(p) + 2) * sizeof(char));
214 sprintf(map_file, "%s/%s", DataDir, p);
215 file = AllocateFile(map_file, PG_BINARY_R);
228 read_through_eol(file);
231 /* Read the FromChar */
232 next_token(file, buf, sizeof(buf));
235 FromChar = strtoul(buf, 0, 0);
236 /* Read the ToChar */
237 next_token(file, buf, sizeof(buf));
240 ToChar = strtoul(buf, 0, 0);
241 RecodeForwTable[FromChar - 128] = ToChar;
242 RecodeBackTable[ToChar - 128] = FromChar;
244 read_through_eol(file);
255 convertstr(unsigned char *buff, int len, int dest)
260 for (i = 0; i < len; i++, buff++)
264 *buff = RecodeForwTable[*buff - 128];
266 *buff = RecodeBackTable[*buff - 128];
274 * GetPgUserName and SetPgUserName
276 * SetPgUserName must be called before InitPostgres, since the setuid()
279 * Replace GetPgUserName() with a lower-case version
280 * to allow use in new case-insensitive SQL (referenced
281 * in pg_proc.h). Define GetPgUserName() as a macro - tgl 97/04/26
297 if (IsUnderPostmaster)
299 /* use the (possibly) authenticated name that's provided */
300 if (!(p = getenv("PG_USER")))
301 elog(FATAL, "SetPgUserName: PG_USER environment variable is unset");
305 /* setuid() has not yet been done, see above comment */
306 if (!(pw = getpwuid(geteuid())))
307 elog(FATAL, "SetPgUserName: no entry in host passwd file");
312 UserName = malloc(strlen(p) + 1);
314 #endif /* NO_SECURITY */
317 /* ----------------------------------------------------------------
318 * GetUserId and SetUserId
319 * ----------------------------------------------------------------
321 static Oid UserId = InvalidOid;
326 AssertState(OidIsValid(UserId));
336 AssertState(!OidIsValid(UserId)); /* only once */
339 * Don't do scans if we're bootstrapping, none of the system catalogs
340 * exist yet, and they should be owned by postgres anyway.
342 if (IsBootstrapProcessingMode())
348 userName = GetPgUserName();
349 userTup = SearchSysCacheTuple(SHADOWNAME,
350 PointerGetDatum(userName),
352 if (!HeapTupleIsValid(userTup))
353 elog(FATAL, "SetUserId: user '%s' is not in '%s'",
356 UserId = (Oid) ((Form_pg_shadow) GETSTRUCT(userTup))->usesysid;
359 /*-------------------------------------------------------------------------
361 * posmaster pid file stuffs. $DATADIR/postmaster.pid is created when:
363 * (1) postmaster starts. In this case pid > 0.
364 * (2) postgres starts in standalone mode. In this case
367 * to gain an interlock.
369 * SetPidFname(datadir)
370 * Remember the the pid file name. This is neccesary
371 * UnlinkPidFile() is called from proc_exit().
373 * GetPidFname(datadir)
374 * Get the pid file name. SetPidFname() should be called
375 * before GetPidFname() gets called.
378 * This is called from proc_exit() and unlink the pid file.
380 * SetPidFile(pid_t pid)
381 * Create the pid file. On failure, it checks if the process
382 * actually exists or not. SetPidFname() should be called
383 * in prior to calling SetPidFile().
385 *-------------------------------------------------------------------------
389 * Path to pid file. proc_exit() remember it to unlink the file.
391 static char PidFile[MAXPGPATH];
394 * Remove the pid file. This function is called from proc_exit.
403 * Set path to the pid file
406 SetPidFname(char *datadir)
408 snprintf(PidFile, sizeof(PidFile), "%s/%s", datadir, PIDFNAME);
412 * Get path to the pid file
421 * Create the pid file
424 SetPidFile(pid_t pid)
436 pidfile = GetPidFname();
437 fd = open(pidfile, O_RDWR | O_CREAT | O_EXCL, 0600);
442 * Couldn't create the pid file. Probably it already exists. Read
443 * the file to see if the process actually exists
445 fd = open(pidfile, O_RDONLY, 0600);
448 fprintf(stderr, "Can't open pid file: %s\n", pidfile);
449 fprintf(stderr, "Please check the permission and try again.\n");
452 if ((len = read(fd, pidstr, sizeof(pidstr) - 1)) < 0)
454 fprintf(stderr, "Can't read pid file: %s\n", pidfile);
455 fprintf(stderr, "Please check the permission and try again.\n");
462 * Check to see if the process actually exists
465 post_pid = (pid_t) atoi(pidstr);
467 /* if pid < 0, the pid is for postgres, not postmatser */
471 post_pid = -post_pid;
474 if (post_pid == 0 || (post_pid > 0 && kill(post_pid, 0) < 0))
478 * No, the process did not exist. Unlink the file and try to
481 if (unlink(pidfile) < 0)
483 fprintf(stderr, "Can't remove pid file: %s\n", pidfile);
484 fprintf(stderr, "The file seems accidently left, but I couldn't remove it.\n");
485 fprintf(stderr, "Please remove the file by hand and try again.\n");
488 fd = open(pidfile, O_RDWR | O_CREAT | O_EXCL, 0600);
491 fprintf(stderr, "Can't create pid file: %s\n", pidfile);
492 fprintf(stderr, "Please check the permission and try again.\n");
500 * Another postmaster is running
502 fprintf(stderr, "Can't create pid file: %s\n", pidfile);
504 fprintf(stderr, "Is another postgres (pid: %d) running?\n", (int) post_pid);
506 fprintf(stderr, "Is another postmaster (pid: %s) running?\n", pidstr);
511 sprintf(pidstr, "%d", (int) pid);
512 if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr))
514 fprintf(stderr, "Write to pid file failed\n");
515 fprintf(stderr, "Please check the permission and try again.\n");
528 * Determine whether the PG_VERSION file in directory `path' indicates
529 * a data version compatible with the version of this program.
531 * If compatible, return. Otherwise, elog(FATAL).
534 ValidatePgVersion(const char *path)
536 char full_path[MAXPGPATH];
539 long file_major, file_minor;
540 long my_major = 0, my_minor = 0;
542 const char *version_string = PG_VERSION;
544 my_major = strtol(version_string, &endptr, 10);
546 my_minor = strtol(endptr+1, NULL, 10);
548 snprintf(full_path, MAXPGPATH, "%s/PG_VERSION", path);
550 file = AllocateFile(full_path, "r");
554 elog(FATAL, "File %s is missing. This is not a valid data directory.", full_path);
556 elog(FATAL, "cannot open %s: %s", full_path, strerror(errno));
559 ret = fscanf(file, "%ld.%ld", &file_major, &file_minor);
561 elog(FATAL, "cannot read %s: %s", full_path, strerror(errno));
563 elog(FATAL, "`%s' does not have a valid format. You need to initdb.", full_path);
567 if (my_major != file_major || my_minor != file_minor)
568 elog(FATAL, "The data directory was initalized by PostgreSQL version %ld.%ld, "
569 "which is not compatible with this verion %s.",
570 file_major, file_minor, version_string);