postgres 之 initdb 源码分析 五

前端之家收集整理的这篇文章主要介绍了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.
原文链接:https://www.f2er.com/postgresql/195711.html

猜你在找的Postgre SQL相关文章