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

  1. /*
  2. * now we are starting to do real work, trap signals so we can clean up
  3. */
  4.  
  5. /* some of these are not valid on Windows */
  6. #ifdef SIGHUP
  7. pqsignal(SIGHUP, trapsig);
  8. #endif
  9. #ifdef SIGINT
  10. pqsignal(SIGINT, trapsig);
  11. #endif
  12. #ifdef SIGQUIT
  13. pqsignal(SIGQUIT, trapsig);
  14. #endif
  15. #ifdef SIGTERM
  16. pqsignal(SIGTERM, trapsig);
  17. #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:

接下来:

  1. switch (pg_check_dir(pg_data))
  2. {
  3. case :
  4. /* PGDATA not there, must create it */
  5. printf(_("creating directory %s ... "),
  6. pg_data);
  7. fflush(stdout);
  8.  
  9. if (!mkdatadir(NULL))
  10. exit_nicely();
  11. else
  12. check_ok();
  13.  
  14. made_new_pgdata = true;
  15. break;
  16.  
  17. case :
  18. /* Present but empty, fix permissions and use it */
  19. printf(_("fixing permissions on existing directory %s ... "),
  20. pg_data);
  21. fflush(stdout);
  22.  
  23. if (chmod(pg_data, S_IRWXU) != )
  24. {
  25. fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
  26. progname, pg_data, strerror(errno));
  27. exit_nicely();
  28. }
  29. else
  30. check_ok();
  31.  
  32. found_existing_pgdata = true;
  33. break;
  34.  
  35. case :
  36. /* Present and not empty */
  37. fprintf(stderr,
  38. _("%s: directory \"%s\" exists but is not empty\n"),
  39. progname, pg_data);
  40. fprintf(stderr,
  41. _("If you want to create a new database system, either remove or empty\n"
  42. "the directory \"%s\" or run %s\n"
  43. "with an argument other than \"%s\".\n"),
  44. pg_data, progname, pg_data);
  45. exit(); /* no further message needed */
  46.  
  47. default:
  48. /* Trouble accessing directory */
  49. fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
  50. progname, pg_data, strerror(errno));
  51. exit_nicely();
  52. }

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

  1. /*
  2. * Test to see if a directory exists and is empty or not.
  3. *
  4. * Returns:
  5. * 0 if nonexistent
  6. * 1 if exists and empty
  7. * 2 if exists and not empty
  8. * -1 if trouble accessing directory (errno reflects the error)
  9. */
  10. int
  11. pg_check_dir(const char *dir)
  12. {
  13. int result = ;
  14. DIR *chkdir;
  15. struct dirent *file;
  16.  
  17. errno = ;
  18.  
  19. chkdir = opendir(dir);
  20.  
  21. if (chkdir == NULL)
  22. return (errno == ENOENT) ? : -;
  23.  
  24. while ((file = readdir(chkdir)) != NULL)
  25. {
  26. if (strcmp(".", file->d_name) == ||
  27. strcmp("..", file->d_name) == )
  28. {
  29. /* skip this and parent directory */
  30. continue;
  31. }
  32. else
  33. {
  34. result = ; /* not empty */
  35. break;
  36. }
  37. }
  38.  
  39. #ifdef WIN32
  40.  
  41. /*
  42. * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
  43. * released version
  44. */
  45. if (GetLastError() == ERROR_NO_MORE_FILES)
  46. errno = ;
  47. #endif
  48.  
  49. closedir(chkdir);
  50.  
  51. if (errno != )
  52. result = -; /* some kind of I/O error? */
  53.  
  54. return result;
  55. }

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

PostgreSQL的 initdb 源代码分析之九的更多相关文章

  1. PostgreSQL的 initdb 源代码分析之十九

    继续分析: setup_dictionary(); 展开: 其中: cmd 是:"/home/pgsql/project/bin/postgres" --single -F -O ...

  2. PostgreSQL的initdb 源代码分析之六

    继续分析 下面的是获取运行此程序的用户名称,主要还是为了防止在linux下用root来运行的情形. effective_user = get_id(); ) username = effective_ ...

  3. PostgreSQL的 initdb 源代码分析之二

    继续分析 下面这一段,当 initdb --version 或者  initdb --help 才有意义. ) { ], || strcmp(argv[], ) { usage(progname); ...

  4. PostgreSQL的 initdb 源代码分析之二十四

    继续分析: make_template0(); 展开: 无需再作解释,就是创建template0数据库 /* * copy template1 to template0 */ static void ...

  5. PostgreSQL的 initdb 源代码分析之十五

    继续分析: if (pwprompt || pwfilename) get_set_pwd(); 由于我启动initdb的时候,没有设置口令相关的选项,故此略过. 接下来: setup_depend( ...

  6. PostgreSQL的 initdb 源代码分析之十三

    继续分析: /* Bootstrap template1 */ bootstrap_template1(); 展开: 我这里读入的文件是:/home/pgsql/project/share/postg ...

  7. PostgreSQL的 initdb 源代码分析之十二

    继续分析 /* Now create all the text config files */ setup_config(); 将其展开: 实质就是,确定各种参数,分别写入 postgresql.co ...

  8. PostgreSQL的 initdb 源代码分析之十一

    继续分析: /* Top level PG_VERSION is checked by bootstrapper, so make it first */ write_version_file(NUL ...

  9. PostgreSQL的 initdb 源代码分析之七

    继续分析:由于我使用initdb的时候,没有指定 locale,所以会使用OS的缺省locale,这里是 en_US.UTF-8 printf(_("The files belonging ...

随机推荐

  1. C扩展Python

    基本想法: 先看中文小介绍,再看英文详细文档. 1. 参考 首先参考THIS, IBM的工程师好像出了好多这样的文章啊,而且每次看到时间戳,我都想戳自己- -! 2. ERROR 可能遇到错误: fa ...

  2. Java Socket(2): 异常处理

    1 超时 套接字底层是基于TCP的,所以socket的超时和TCP超时是相同的.下面先讨论套接字读写缓冲区,接着讨论连接建立超时.读写超时以及JAVA套接字编程的嵌套异常捕获和一个超时例子程序的抓包示 ...

  3. wifi详解(四)

    1        IOCTL的调用逻辑 之所以要分析这个,是因为上层wpa_supplicant和WIFI驱动打交道的方式,多半是通过ioctl的方式进行的,所以看看它的调用逻辑(这里只列出其主要的调 ...

  4. ArcGIS AO开发高亮显示某些要素

    参考代码1 ifeaturecursor pcur = ifeatureclass.search(iqueryfilter pfilter); pfilter.whereclause = strAdd ...

  5. ylbtech-SubwayNav(地铁线路导航)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计 DatabaseName:SubwayNav(地铁线路导航) Type:线路导航 1.A, ...

  6. Redis,Memcache,mongoDB的区别

    从以下几个维度,对redis.memcache.mongoDB 做了对比,欢迎拍砖 1.性能 都比较高,性能对我们来说应该都不是瓶颈 总体来讲,TPS方面redis和memcache差不多,要大于mo ...

  7. Apache OFBiz 学习笔记 之 服务引擎 二

    加载服务定义文件   ofbiz-component.xml:所有的服务定义文件在每个组件的ofbi-component.xml文件中   加载服务定义 例:framework/common/ofbi ...

  8. 不知道帐号密码的情况下完全重装Mac Min的OS X10.7系统

    现状: 1.原系统OS X 10.7 2.老账号不知道密码 3.Mac小盒子 目的: 1.删除老账号 2.更新系统到10.9以上 尝试过程1: 1.按住option键 + 开机 2.选择“磁盘工具” ...

  9. ASP.NET MVC之Html.RenderAction

    WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...

  10. JAVA中的异常(异常处理流程、异常处理的缺陷)

    异常处理流程 1)首先由try{...}catch(Exception e){ System.out.println(e); e.printStackTrace(); }finally{...}结构 ...