src/os/unix/ngx_posix_init.c - nginx source code

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <nginx.h>


  8. ngx_int_t   ngx_ncpu;
  9. ngx_int_t   ngx_max_sockets;
  10. ngx_uint_t  ngx_inherited_nonblocking;
  11. ngx_uint_t  ngx_tcp_nodelay_and_tcp_nopush;


  12. struct rlimit  rlmt;


  13. ngx_os_io_t ngx_os_io = {
  14.     ngx_unix_recv,
  15.     ngx_readv_chain,
  16.     ngx_udp_unix_recv,
  17.     ngx_unix_send,
  18.     ngx_udp_unix_send,
  19.     ngx_udp_unix_sendmsg_chain,
  20.     ngx_writev_chain,
  21.     0
  22. };


  23. ngx_int_t
  24. ngx_os_init(ngx_log_t *log)
  25. {
  26.     ngx_time_t  *tp;
  27.     ngx_uint_t   n;
  28. #if (NGX_HAVE_LEVEL1_DCACHE_LINESIZE)
  29.     long         size;
  30. #endif

  31. #if (NGX_HAVE_OS_SPECIFIC_INIT)
  32.     if (ngx_os_specific_init(log) != NGX_OK) {
  33.         return NGX_ERROR;
  34.     }
  35. #endif

  36.     if (ngx_init_setproctitle(log) != NGX_OK) {
  37.         return NGX_ERROR;
  38.     }

  39.     ngx_pagesize = getpagesize();

  40.     if (ngx_cacheline_size == 0) {
  41.         ngx_cacheline_size = NGX_CPU_CACHE_LINE;
  42.     }

  43.     for (n = ngx_pagesize; n >>= 1; ngx_pagesize_shift++) { /* void */ }

  44. #if (NGX_HAVE_SC_NPROCESSORS_ONLN)
  45.     if (ngx_ncpu == 0) {
  46.         ngx_ncpu = sysconf(_SC_NPROCESSORS_ONLN);
  47.     }
  48. #endif

  49.     if (ngx_ncpu < 1) {
  50.         ngx_ncpu = 1;
  51.     }

  52. #if (NGX_HAVE_LEVEL1_DCACHE_LINESIZE)
  53.     size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
  54.     if (size > 0) {
  55.         ngx_cacheline_size = size;
  56.     }
  57. #endif

  58.     ngx_cpuinfo();

  59.     if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) {
  60.         ngx_log_error(NGX_LOG_ALERT, log, errno,
  61.                       "getrlimit(RLIMIT_NOFILE) failed");
  62.         return NGX_ERROR;
  63.     }

  64.     ngx_max_sockets = (ngx_int_t) rlmt.rlim_cur;

  65. #if (NGX_HAVE_INHERITED_NONBLOCK || NGX_HAVE_ACCEPT4)
  66.     ngx_inherited_nonblocking = 1;
  67. #else
  68.     ngx_inherited_nonblocking = 0;
  69. #endif

  70.     tp = ngx_timeofday();
  71.     srandom(((unsigned) ngx_pid << 16) ^ tp->sec ^ tp->msec);

  72.     return NGX_OK;
  73. }


  74. void
  75. ngx_os_status(ngx_log_t *log)
  76. {
  77.     ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER_BUILD);

  78. #ifdef NGX_COMPILER
  79.     ngx_log_error(NGX_LOG_NOTICE, log, 0, "built by " NGX_COMPILER);
  80. #endif

  81. #if (NGX_HAVE_OS_SPECIFIC_INIT)
  82.     ngx_os_specific_status(log);
  83. #endif

  84.     ngx_log_error(NGX_LOG_NOTICE, log, 0,
  85.                   "getrlimit(RLIMIT_NOFILE): %r:%r",
  86.                   rlmt.rlim_cur, rlmt.rlim_max);
  87. }


  88. #if 0

  89. ngx_int_t
  90. ngx_posix_post_conf_init(ngx_log_t *log)
  91. {
  92.     ngx_fd_t  pp[2];

  93.     if (pipe(pp) == -1) {
  94.         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed");
  95.         return NGX_ERROR;
  96.     }

  97.     if (dup2(pp[1], STDERR_FILENO) == -1) {
  98.         ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDERR) failed");
  99.         return NGX_ERROR;
  100.     }

  101.     if (pp[1] > STDERR_FILENO) {
  102.         if (close(pp[1]) == -1) {
  103.             ngx_log_error(NGX_LOG_EMERG, log, errno, "close() failed");
  104.             return NGX_ERROR;
  105.         }
  106.     }

  107.     return NGX_OK;
  108. }

  109. #endif