checkDataDir

checkDataDir函数检查数据目录,初始化文件和目录的creae mode和mode maks。

void checkDataDir(void) {
struct stat stat_buf;
Assert(DataDir);
if (stat(DataDir, &stat_buf) != 0) {
if (errno == ENOENT) ereport(FATAL,(errcode_for_file_access(),errmsg("data directory \"%s\" does not exist",DataDir)));
else ereport(FATAL,(errcode_for_file_access(),errmsg("could not read permissions of directory \"%s\": %m",DataDir)));
}
/* eventual chdir would fail anyway, but let's test ... */
if (!S_ISDIR(stat_buf.st_mode)) ereport(FATAL,(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),errmsg("specified data directory \"%s\" is not a directory",DataDir)));
// 检查目录是否属于userid。
/* Check that the directory belongs to my userid; if not, reject.
* This check is an essential part of the interlock that prevents two
* postmasters from starting in the same directory (see CreateLockFile()).
* Do not remove or weaken it.
* XXX can we safely enable this check on Windows? */
#if !defined(WIN32) && !defined(__CYGWIN__)
if (stat_buf.st_uid != geteuid())
ereport(FATAL,(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),errmsg("data directory \"%s\" has wrong ownership",DataDir),errhint("The server must be started by the user that owns the data directory.")));
#endif

// 检查目录权限是否正确 0700 0750
/* Check if the directory has correct permissions. If not, reject.
* Only two possible modes are allowed, 0700 and 0750. The latter mode
* indicates that group read/execute should be allowed on all newly
* created files and directories.
* XXX temporarily suppress check when on Windows, because there may not
* be proper support for Unix-y file permissions. Need to think of a
* reasonable check to apply on Windows. */
#if !defined(WIN32) && !defined(__CYGWIN__)
if (stat_buf.st_mode & PG_MODE_MASK_GROUP)
ereport(FATAL,(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),errmsg("data directory \"%s\" has invalid permissions",DataDir),errdetail("Permissions should be u=rwx (0700) or u=rwx,g=rx (0750).")));
#endif

/* Reset creation modes and mask based on the mode of the data directory.
* The mask was set earlier in startup to disallow group permissions on
* newly created files and directories. However, if group read/execute
* are present on the data directory then modify the create modes and mask
* to allow group read/execute on newly created files and directories and
* set the data_directory_mode GUC.
* Suppress when on Windows, because there may not be proper support for
* Unix-y file permissions. */
#if !defined(WIN32) && !defined(__CYGWIN__)
SetDataDirectoryCreatePerm(stat_buf.st_mode);
umask(pg_mode_mask);
data_directory_mode = pg_dir_create_mode;
#endif

/* Check for PG_VERSION */
ValidatePgVersion(DataDir);
}

checkControlFile

checkControlFile函数pg_control在数据目录下是否存在于正确的路径下。

static void checkControlFile(void) {
char path[MAXPGPATH];
FILE *fp;
snprintf(path, sizeof(path), "%s/global/pg_control", DataDir);
fp = AllocateFile(path, PG_BINARY_R);
if (fp == NULL) {
write_stderr("%s: could not find the database system\n Expected to find it in the directory \"%s\",\nbut could not open file \"%s\": %s\n",progname, DataDir, path, strerror(errno));
ExitPostmaster(2);
}
FreeFile(fp);
}

ChangeToDataDir

将工作目录转换到DataDir

void
ChangeToDataDir(void)
{
AssertState(DataDir);

if (chdir(DataDir) < 0)
ereport(FATAL,
(errcode_for_file_access(),
errmsg("could not change directory to \"%s\": %m",
DataDir)));
}

PG守护进程(Postmaster)——checkDataDir、checkControlFile、ChangeToDataDir_#endif