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

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

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

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

SIGTERM: 

接下来:

    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
"),
                        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
"),
                    progname, pg_data);
            fprintf(stderr,
                    _("If you want to create a new database system, either remove or empty
"
                      "the directory "%s" or run %s
"
                      "with an argument other than "%s".
"),
                    pg_data, progname, pg_data);
            exit(1);            /* no further message needed */

        default:
            /* Trouble accessing directory */
            fprintf(stderr, _("%s: could not access directory "%s": %s
"),
                    progname, pg_data, strerror(errno));
            exit_nicely();
    }

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

/*
 * 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;
}

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

原文地址:https://www.cnblogs.com/gaojian/p/3176559.html