postgres 之 initdb 源码分析 五

2.8 函数check_authmethod_unspecified()

当不指定认证方式时,认为trust,并在initdb时输出warning信息。

static void
check_authmethod_unspecified(const char **authmethod)
{
	if (*authmethod == NULL || strlen(*authmethod) == 0)
	{
		authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n"
						"You can change this by editing pg_hba.conf or using the option -A,or\n"
			"--auth-local and --auth-host,the next time you run initdb.\n");
		*authmethod = "trust";
	}
}

2.9 函数check_authmethod_valid

检查local (SOCK通信)和host(TCP/IP通信)方式下,认证方式是否为指定的几个之内

static const char *auth_methods_local[] = {"trust","reject","md5","password","peer","radius",

static void
check_authmethod_valid(const char *authmethod,const char **valid_methods,const char *conntype)
{
	const char **p;

	for (p = valid_methods; *p; p++)
	{
		if (strcmp(authmethod,*p) == 0)
			return;
		/* with space = param */
		if (strchr(authmethod,' '))
			if (strncmp(authmethod,*p,(authmethod - strchr(authmethod,' '))) == 0)
				return;
	}

	fprintf(stderr,_("%s: invalid authentication method \"%s\" for \"%s\" connections\n"),progname,authmethod,conntype);
	exit(1);
}


2.10 函数check_need_password

在initdb时,如果指定-A 为md5 或password ,如果不指定-W 则报错
[wln@localhost bin]$ ./initdb -A md5 -D ./data
initdb: must specify a password for the superuser to enable md5 authentication
static void
check_need_password(const char *authmethodlocal,const char *authmethodhost)
{
	if ((strcmp(authmethodlocal,"md5") == 0 ||
		 strcmp(authmethodlocal,"password") == 0) &&
		(strcmp(authmethodhost,"md5") == 0 ||
		 strcmp(authmethodhost,"password") == 0) &&
		!(pwprompt || pwfilename))
	{
		fprintf(stderr,_("%s: must specify a password for the superuser to enable %s authentication\n"),(strcmp(authmethodlocal,"md5") == 0 ||
				 strcmp(authmethodlocal,"password") == 0)
				? authmethodlocal
				: authmethodhost);
		exit(1);
	}
}

2.11 函数get_restricted_token(void)

由于针对WIN32 ,忽略

2.12 函数 setup_bin_paths()

查找initdb所需要的postgres可执行文件及其附属文件

void
setup_bin_paths(const char *argv0)
{
	int			ret;

	if ((ret = find_other_exec(argv0,"postgres",PG_BACKEND_VERSIONSTR,backend_exec)) < 0)
	{
		char		full_path[MAXPGPATH];

		if (find_my_exec(argv0,full_path) < 0)
			strlcpy(full_path,sizeof(full_path));

		if (ret == -1)
			fprintf(stderr,_("The program \"postgres\" is needed by %s "
					  "but was not found in the\n"
					  "same directory as \"%s\".\n"
					  "Check your installation.\n"),full_path);
		else
			fprintf(stderr,_("The program \"postgres\" was found by \"%s\"\n"
					  "but was not the same version as %s.\n"
					  "Check your installation.\n"),full_path,progname);
		exit(1);
	}

	/* store binary directory */
	strcpy(bin_path,backend_exec);
	*last_dir_separator(bin_path) = '\0';
	canonicalize_path(bin_path);

	if (!share_path)
	{
		share_path = pg_malloc(MAXPGPATH);
		get_share_path(backend_exec,share_path);
	}
	else if (!is_absolute_path(share_path))
	{
		fprintf(stderr,_("%s: input file location must be an absolute path\n"),progname);
		exit(1);
	}

	canonicalize_path(share_path);
}


2.13 函数 get_id()

得到执行initdb程序的用户名

/*
 * find the current user
 *
 * on unix make sure it isn't really root
 */
static char *
get_id(void)
{
#ifndef WIN32

	struct passwd *pw;

	if (geteuid() == 0)			/* 0 is root's uid */
	{
		fprintf(stderr,_("%s: cannot be run as root\n"
				  "Please log in (using,e.g.,\"su\") as the "
				  "(unprivileged) user that will\n"
				  "own the server process.\n"),progname);
		exit(1);
	}

	pw = getpwuid(geteuid());
	if (!pw)
	{
		fprintf(stderr,_("%s: Could not obtain information about current user: %s\n"),strerror(errno));
		exit(1);
	}
#else							/* the windows code */

	struct passwd_win32
	{
		int			pw_uid;
		char		pw_name[128];
	}			pass_win32;
	struct passwd_win32 *pw = &pass_win32;
	DWORD		pwname_size = sizeof(pass_win32.pw_name) - 1;

	pw->pw_uid = 1;
	if (!GetUserName(pw->pw_name,&pwname_size))
	{
		fprintf(stderr,_("%s: Could not get current user name: %s\n"),strerror(errno));
		exit(1);
	}
#endif

	return pg_strdup(pw->pw_name);
}

2.13.1 此时得到initdb输出的第一行信息

printf(_("The files belonging to this database system will be owned "
"by user \"%s\".\n"
"This user must also own the server process.\n\n"),
effective_user);

[wln@localhost bin]$ ./initdb -D ./data 
The files belonging to this database system will be owned by user "wln".
This user must also own the server process.

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...