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.46 2000/05/02 08:13:08 ishii Exp $
13 *-------------------------------------------------------------------------
17 #include <sys/param.h>
18 #include <sys/types.h>
28 #include "catalog/catname.h"
29 #include "catalog/pg_shadow.h"
30 #include "miscadmin.h"
31 #include "utils/syscache.h"
35 unsigned char RecodeForwTable[128];
36 unsigned char RecodeBackTable[128];
40 ProcessingMode Mode = InitProcessing;
42 /* ----------------------------------------------------------------
43 * ignoring system indexes support stuff
44 * ----------------------------------------------------------------
47 static bool isIgnoringSystemIndexes = false;
50 * IsIgnoringSystemIndexes
51 * True if ignoring system indexes.
54 IsIgnoringSystemIndexes()
56 return isIgnoringSystemIndexes;
61 * Set true or false whether PostgreSQL ignores system indexes.
65 IgnoreSystemIndexes(bool mode)
67 isIgnoringSystemIndexes = mode;
70 /* ----------------------------------------------------------------
71 * database path / name support stuff
72 * ----------------------------------------------------------------
76 SetDatabasePath(const char *path)
79 /* use strdup since this is done before memory contexts are set up */
82 DatabasePath = strdup(path);
83 AssertState(DatabasePath);
88 SetDatabaseName(const char *name)
93 DatabaseName = strdup(name);
94 AssertState(DatabaseName);
99 /* even if MULTIBYTE is not enabled, this function is neccesary
100 * since pg_proc.h has entries for them.
103 getdatabaseencoding()
105 return ("SQL_ASCII");
109 pg_encoding_to_char(int encoding)
111 return ("SQL_ASCII");
115 pg_char_to_encoding(const char *encoding_string)
125 /* Some standard C libraries, including GNU, have an isblank() function.
126 Others, including Solaris, do not. So we have our own.
129 isblank(const char c)
131 return c == ' ' || c == 9 /* tab */ ;
135 next_token(FILE *fp, char *buf, const int bufsz)
137 /*--------------------------------------------------------------------------
138 Grab one token out of fp. Tokens are strings of non-blank
139 characters bounded by blank characters, beginning of line, and end
140 of line. Blank means space or tab. Return the token as *buf.
141 Leave file positioned to character immediately after the token or
142 EOF, whichever comes first. If no more tokens on line, return null
143 string as *buf and position file to beginning of next line or EOF,
144 whichever comes first.
145 --------------------------------------------------------------------------*/
147 char *eb = buf + (bufsz - 1);
149 /* Move over inital token-delimiting blanks */
150 while (isblank(c = getc(fp)));
156 * build a token in buf of next characters up to EOF, eol, or
159 while (c != EOF && c != '\n' && !isblank(c))
166 * Put back the char right after the token (putting back EOF
176 read_through_eol(FILE *file)
182 while (c != '\n' && c != EOF);
195 unsigned char FromChar,
198 for (i = 0; i < 128; i++)
200 RecodeForwTable[i] = i + 128;
201 RecodeBackTable[i] = i + 128;
204 p = getenv("PG_RECODETABLE");
207 map_file = (char *) malloc((strlen(DataDir) +
208 strlen(p) + 2) * sizeof(char));
209 sprintf(map_file, "%s/%s", DataDir, p);
211 file = AllocateFile(map_file, "r");
213 file = AllocateFile(map_file, "rb");
227 read_through_eol(file);
230 /* Read the FromChar */
231 next_token(file, buf, sizeof(buf));
234 FromChar = strtoul(buf, 0, 0);
235 /* Read the ToChar */
236 next_token(file, buf, sizeof(buf));
239 ToChar = strtoul(buf, 0, 0);
240 RecodeForwTable[FromChar - 128] = ToChar;
241 RecodeBackTable[ToChar - 128] = FromChar;
243 read_through_eol(file);
254 convertstr(unsigned char *buff, int len, int dest)
259 for (i = 0; i < len; i++, buff++)
263 *buff = RecodeForwTable[*buff - 128];
265 *buff = RecodeBackTable[*buff - 128];
273 * GetPgUserName and SetPgUserName
275 * SetPgUserName must be called before InitPostgres, since the setuid()
278 * Replace GetPgUserName() with a lower-case version
279 * to allow use in new case-insensitive SQL (referenced
280 * in pg_proc.h). Define GetPgUserName() as a macro - tgl 97/04/26
296 if (IsUnderPostmaster)
298 /* use the (possibly) authenticated name that's provided */
299 if (!(p = getenv("PG_USER")))
300 elog(FATAL, "SetPgUserName: PG_USER environment variable is unset");
304 /* setuid() has not yet been done, see above comment */
305 if (!(pw = getpwuid(geteuid())))
306 elog(FATAL, "SetPgUserName: no entry in host passwd file");
311 UserName = malloc(strlen(p) + 1);
313 #endif /* NO_SECURITY */
316 /* ----------------------------------------------------------------
317 * GetUserId and SetUserId
318 * ----------------------------------------------------------------
320 static Oid UserId = InvalidOid;
325 AssertState(OidIsValid(UserId));
335 AssertState(!OidIsValid(UserId)); /* only once */
338 * Don't do scans if we're bootstrapping, none of the system catalogs
339 * exist yet, and they should be owned by postgres anyway.
341 if (IsBootstrapProcessingMode())
347 userName = GetPgUserName();
348 userTup = SearchSysCacheTuple(SHADOWNAME,
349 PointerGetDatum(userName),
351 if (!HeapTupleIsValid(userTup))
352 elog(FATAL, "SetUserId: user '%s' is not in '%s'",
355 UserId = (Oid) ((Form_pg_shadow) GETSTRUCT(userTup))->usesysid;
358 /*-------------------------------------------------------------------------
360 * posmaster pid file stuffs. $DATADIR/postmaster.pid is created when:
362 * (1) postmaster starts. In this case pid > 0.
363 * (2) postgres starts in standalone mode. In this case
366 * to gain an interlock.
368 * SetPidFname(datadir)
369 * Remember the the pid file name. This is neccesary
370 * UnlinkPidFile() is called from proc_exit().
372 * GetPidFname(datadir)
373 * Get the pid file name. SetPidFname() should be called
374 * before GetPidFname() gets called.
377 * This is called from proc_exit() and unlink the pid file.
379 * SetPidFile(pid_t pid)
380 * Create the pid file. On failure, it checks if the process
381 * actually exists or not. SetPidFname() should be called
382 * in prior to calling SetPidFile().
384 *-------------------------------------------------------------------------
388 * Path to pid file. proc_exit() remember it to unlink the file.
390 static char PidFile[MAXPGPATH];
393 * Remove the pid file. This function is called from proc_exit.
402 * Set path to the pid file
405 SetPidFname(char *datadir)
407 snprintf(PidFile, sizeof(PidFile), "%s/%s", datadir, PIDFNAME);
411 * Get path to the pid file
420 * Create the pid file
423 SetPidFile(pid_t pid)
435 pidfile = GetPidFname();
436 fd = open(pidfile, O_RDWR | O_CREAT | O_EXCL, 0600);
441 * Couldn't create the pid file. Probably it already exists. Read
442 * the file to see if the process actually exists
444 fd = open(pidfile, O_RDONLY, 0600);
447 fprintf(stderr, "Can't open pid file: %s\n", pidfile);
448 fprintf(stderr, "Please check the permission and try again.\n");
451 if ((len = read(fd, pidstr, sizeof(pidstr) - 1)) < 0)
453 fprintf(stderr, "Can't read pid file: %s\n", pidfile);
454 fprintf(stderr, "Please check the permission and try again.\n");
461 * Check to see if the process actually exists
464 post_pid = (pid_t) atoi(pidstr);
466 /* if pid < 0, the pid is for postgres, not postmatser */
470 post_pid = -post_pid;
473 if (post_pid == 0 || (post_pid > 0 && kill(post_pid, 0) < 0))
477 * No, the process did not exist. Unlink the file and try to
480 if (unlink(pidfile) < 0)
482 fprintf(stderr, "Can't remove pid file: %s\n", pidfile);
483 fprintf(stderr, "The file seems accidently left, but I couldn't remove it.\n");
484 fprintf(stderr, "Please remove the file by hand and try again.\n");
487 fd = open(pidfile, O_RDWR | O_CREAT | O_EXCL, 0600);
490 fprintf(stderr, "Can't create pid file: %s\n", pidfile);
491 fprintf(stderr, "Please check the permission and try again.\n");
499 * Another postmaster is running
501 fprintf(stderr, "Can't create pid file: %s\n", pidfile);
503 fprintf(stderr, "Is another postgres (pid: %d) running?\n", post_pid);
505 fprintf(stderr, "Is another postmaster (pid: %s) running?\n", pidstr);
510 sprintf(pidstr, "%d", pid);
511 if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr))
513 fprintf(stderr, "Write to pid file failed\n");
514 fprintf(stderr, "Please check the permission and try again.\n");