PostgreSQL的 initdb 源代码分析之四

继续分析:

    if (pwprompt && pwfilename)
    {
        fprintf(stderr, _("%s: password prompt and password file cannot be specified together
"), progname);
        exit(1);
    }

    if (authmethod == NULL || !strlen(authmethod))
    {
        authwarning = _("
WARNING: enabling "trust" authentication for local connections
"
                        "You can change this by editing pg_hba.conf or using the -A option the
"
                        "next time you run initdb.
");
        authmethod = "trust";
    }


    if (strcmp(authmethod, "md5") &&
        strcmp(authmethod, "peer") &&
        strcmp(authmethod, "ident") &&
        strcmp(authmethod, "trust") &&

#ifdef USE_PAM
        strcmp(authmethod, "pam") &&
        strncmp(authmethod, "pam ", 4) &&        /* pam with space = param */
#endif

        strcmp(authmethod, "crypt") &&
        strcmp(authmethod, "password")
        )

        /*
         * Kerberos methods not listed because they are not supported over
         * local connections and are rejected in hba.c
         */
    {
        fprintf(stderr, _("%s: unrecognized authentication method "%s"
"),
                progname, authmethod);
        exit(1);
    }

    if ((!strcmp(authmethod, "md5") ||
         !strcmp(authmethod, "crypt") ||
         !strcmp(authmethod, "password")) &&
        !(pwprompt || pwfilename))
    {
        fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication
"), 
        progname, authmethod); exit(
1); } /* * When ident is specified, use peer for local connections. Mirrored, when * peer is specified, use ident for TCP connections. */ if (strcmp(authmethod, "ident") == 0) authmethodlocal = "peer"; else if (strcmp(authmethod, "peer") == 0) { authmethodlocal = "peer"; authmethod = "ident"; } else authmethodlocal = authmethod;

因为我在运行initdb的时候,未指定认证方式,所以缺省的认证方式将被设置为 trust。

接下来,因为我运行initdb是指定了 -D 参数,所以如下一段也被跳过:

    if (strlen(pg_data) == 0)
    {
        pgdenv = getenv("PGDATA");
        if (pgdenv && strlen(pgdenv))
        {
            /* PGDATA found */
            pg_data = xstrdup(pgdenv);
        }
        else
        {
            fprintf(stderr,
                    _("%s: no data directory specified
"
                      "You must identify the directory where the data for this database system
"
                      "will reside.  Do this with either the invocation option -D or the
"
                      "environment variable PGDATA.
"),
                    progname);
            exit(1);
        }
    }

接下来,canonicalize_path 是处理一些多余的/,或windows和unix平台的斜线和反斜线的统一处理。

    pg_data_native = pg_data;

    canonicalize_path(pg_data);

接下来,Win32平台相关代码也忽略:

#ifdef WIN32

    /*
     * Before we execute another program, make sure that we are running with a
     * restricted token. If not, re-execute ourselves with one.
     */

    if ((restrict_env = getenv("PG_RESTRICT_EXEC")) == NULL
        || strcmp(restrict_env, "1") != 0)
    {
        PROCESS_INFORMATION pi;
        char       *cmdline;

        ZeroMemory(&pi, sizeof(pi));

        cmdline = xstrdup(GetCommandLine());

        putenv("PG_RESTRICT_EXEC=1");

        if (!CreateRestrictedProcess(cmdline, &pi))
        {
            fprintf(stderr, "Failed to re-exec with restricted token: %lu.
", GetLastError());
        }
        else
        {
            /*
             * Successfully re-execed. Now wait for child process to capture
             * exitcode.
             */
            DWORD        x;

            CloseHandle(pi.hThread);
            WaitForSingleObject(pi.hProcess, INFINITE);

            if (!GetExitCodeProcess(pi.hProcess, &x))
            {
                fprintf(stderr, "Failed to get exit code from subprocess: %lu
", GetLastError());
                exit(1);
            }
            exit(x);
        }
    }
#endif

接下来,进行存储,也是和windows平台有点关系,加入了双引号:

    /*
     * we have to set PGDATA for postgres rather than pass it on the command
     * line to avoid dumb quoting problems on Windows, and we would especially
     * need quotes otherwise on Windows because paths there are most likely to
     * have embedded spaces.
     */
    pgdenv = pg_malloc(8 + strlen(pg_data));
    sprintf(pgdenv, "PGDATA=%s", pg_data);
    putenv(pgdenv);
原文地址:https://www.cnblogs.com/gaojian/p/3173995.html