且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostgreSQL的 initdb 源代码分析之九

更新时间:2022-09-15 12:11:06

继续:下面的是定义信号处理函数。

PostgreSQL的 initdb 源代码分析之九
    /*
     * now we are starting to do real work, trap signals so we can clean up
     */

    /* some of these are not valid on Windows */
#ifdef SIGHUP
    pqsignal(SIGHUP, trapsig);
#endif
#ifdef SIGINT
    pqsignal(SIGINT, trapsig);
#endif
#ifdef SIGQUIT
    pqsignal(SIGQUIT, trapsig);
#endif
#ifdef SIGTERM
    pqsignal(SIGTERM, trapsig);
#endif

  #ifdef SIGPIPE
      pqsignal(SIGPIPE, SIG_IGN);
  #endif

PostgreSQL的 initdb 源代码分析之九

SIGHUP: 我用  kill -HUP initdb的进程号,trapsig函数会收到 SIGHUP 信号,这是退出时候会收到的信号。

SIGINT:    我用  kill -INT initdb的进程号,trapsig函数会收到 SIGINT 信号,这是ctrl+c时会收到的信号。

SIGQUIT:  ctrl+\ 时会受到的信号。

SIGTERM: 

接下来:

PostgreSQL的 initdb 源代码分析之九
    switch (pg_check_dir(pg_data))
    {
        case 0:
            /* PGDATA not there, must create it */
            printf(_("creating directory %s ... "),
                   pg_data);
            fflush(stdout);

            if (!mkdatadir(NULL))
                exit_nicely();
            else
                check_ok();

            made_new_pgdata = true;
            break;

        case 1:
            /* Present but empty, fix permissions and use it */
            printf(_("fixing permissions on existing directory %s ... "),
                   pg_data);
            fflush(stdout);

            if (chmod(pg_data, S_IRWXU) != 0)
            {
                fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
                        progname, pg_data, strerror(errno));
                exit_nicely();
            }
            else
                check_ok();

            found_existing_pgdata = true;
            break;

        case 2:
            /* Present and not empty */
            fprintf(stderr,
                    _("%s: directory \"%s\" exists but is not empty\n"),
                    progname, pg_data);
            fprintf(stderr,
                    _("If you want to create a new database system, either remove or empty\n"
                      "the directory \"%s\" or run %s\n"
                      "with an argument other than \"%s\".\n"),
                    pg_data, progname, pg_data);
            exit(1);            /* no further message needed */

        default:
            /* Trouble accessing directory */
            fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
                    progname, pg_data, strerror(errno));
            exit_nicely();
    }
PostgreSQL的 initdb 源代码分析之九

此时,要看这个函数的效果:

PostgreSQL的 initdb 源代码分析之九
/*
 * Test to see if a directory exists and is empty or not.
 *
 * Returns:
 *        0 if nonexistent
 *        1 if exists and empty
 *        2 if exists and not empty
 *        -1 if trouble accessing directory (errno reflects the error)
 */
int
pg_check_dir(const char *dir)
{
    int            result = 1;
    DIR           *chkdir;
    struct dirent *file;

    errno = 0;

    chkdir = opendir(dir);

    if (chkdir == NULL)
        return (errno == ENOENT) ? 0 : -1;

    while ((file = readdir(chkdir)) != NULL)
    {
        if (strcmp(".", file->d_name) == 0 ||
            strcmp("..", file->d_name) == 0)
        {
            /* skip this and parent directory */
            continue;
        }
        else
        {
            result = 2;            /* not empty */
            break;
        }
    }

#ifdef WIN32

    /*
     * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
     * released version
     */
    if (GetLastError() == ERROR_NO_MORE_FILES)
        errno = 0;
#endif

    closedir(chkdir);

    if (errno != 0)
        result = -1;            /* some kind of I/O error? */

    return result;
}
PostgreSQL的 initdb 源代码分析之九

按最正常的情况,我的目录存在而且为空,则 check_ok() 得到执行,而  found_existing_pgdata = true...







本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/p/3176559.html,如需转载请自行联系原作者