src/os/win32/ngx_win32_init.c - nginx-1.31.9 nginx/ @ 2a3a19c6f

Global variables defined

Data types 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_uint_t  ngx_win32_version;
  9. ‌ngx_uint_t  ngx_ncpu;
  10. ‌ngx_uint_t  ngx_max_wsabufs;
  11. ‌ngx_int_t   ngx_max_sockets;
  12. ‌ngx_uint_t  ngx_inherited_nonblocking = 1;
  13. ‌ngx_uint_t  ngx_tcp_nodelay_and_tcp_nopush;

  14. ‌char        ngx_unique[NGX_INT32_LEN + 1];


  15. ‌ngx_os_io_t ngx_os_io = {
  16.     ngx_wsarecv,
  17.     ngx_wsarecv_chain,
  18.     ngx_udp_wsarecv,
  19.     ngx_wsasend,
  20.     NULL,
  21.     NULL,
  22.     ngx_wsasend_chain,
  23.     0
  24. };


  25. ‌typedef struct {
  26.     WORD  wServicePackMinor;
  27.     WORD  wSuiteMask;
  28.     BYTE  wProductType;
  29. ‌} ngx_osviex_stub_t;


  30. ‌static u_int               osviex;
  31. ‌static OSVERSIONINFOEX     osvi;


  32. #if (NGX_LOAD_WSAPOLL)
  33. ‌ngx_wsapoll_pt             WSAPoll;
  34. ‌ngx_uint_t                 ngx_have_wsapoll;
  35. #endif


  36. ngx_int_t
  37. ‌ngx_os_init(ngx_log_t *log)
  38. {
  39.     WSADATA       wsd;
  40.     ngx_err_t     err;
  41.     ngx_time_t   *tp;
  42.     ngx_uint_t    n;
  43.     SYSTEM_INFO   si;

  44.     /* get Windows version */

  45.     ngx_memzero(&osvi, sizeof(OSVERSIONINFOEX));
  46.     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

  47. #ifdef _MSC_VER
  48. #pragma warning(disable:4996)
  49. #endif

  50.     osviex = GetVersionEx((OSVERSIONINFO *) &osvi);

  51.     if (osviex == 0) {
  52.         osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  53.         if (GetVersionEx((OSVERSIONINFO *) &osvi) == 0) {
  54.             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
  55.                           "GetVersionEx() failed");
  56.             return NGX_ERROR;
  57.         }
  58.     }

  59. #ifdef _MSC_VER
  60. #pragma warning(default:4996)
  61. #endif

  62.     /*
  63.      *  Windows 3.1 Win32s   0xxxxx
  64.      *
  65.      *  Windows 95           140000
  66.      *  Windows 98           141000
  67.      *  Windows ME           149000
  68.      *  Windows NT 3.51      235100
  69.      *  Windows NT 4.0       240000
  70.      *  Windows NT 4.0 SP5   240050
  71.      *  Windows 2000         250000
  72.      *  Windows XP           250100
  73.      *  Windows 2003         250200
  74.      *  Windows Vista/2008   260000
  75.      *
  76.      *  Windows CE x.x       3xxxxx
  77.      */

  78.     ngx_win32_version = osvi.dwPlatformId * 100000
  79.                         + osvi.dwMajorVersion * 10000
  80.                         + osvi.dwMinorVersion * 100;

  81.     if (osviex) {
  82.         ngx_win32_version += osvi.wServicePackMajor * 10
  83.                              + osvi.wServicePackMinor;
  84.     }

  85.     GetSystemInfo(&si);
  86.     ngx_pagesize = si.dwPageSize;
  87.     ngx_allocation_granularity = si.dwAllocationGranularity;
  88.     ngx_ncpu = si.dwNumberOfProcessors;
  89.     ngx_cacheline_size = NGX_CPU_CACHE_LINE;

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

  91.     /* delete default "C" locale for _wcsicmp() */
  92.     setlocale(LC_ALL, "");


  93.     /* init Winsock */

  94.     if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
  95.         ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
  96.                       "WSAStartup() failed");
  97.         return NGX_ERROR;
  98.     }

  99.     if (ngx_win32_version < NGX_WIN_NT) {
  100.         ngx_max_wsabufs = 16;
  101.         return NGX_OK;
  102.     }

  103.     /* STUB: ngx_uint_t max */
  104.     ngx_max_wsabufs = 1024 * 1024;

  105. #if (NGX_LOAD_WSAPOLL)
  106.     {
  107.     HMODULE  hmod;

  108.     hmod = GetModuleHandle("ws2_32.dll");
  109.     if (hmod == NULL) {
  110.         ngx_log_error(NGX_LOG_NOTICE, log, ngx_errno,
  111.                       "GetModuleHandle(\"ws2_32.dll\") failed");
  112.         goto nopoll;
  113.     }

  114.     WSAPoll = (ngx_wsapoll_pt) (void *) GetProcAddress(hmod, "WSAPoll");
  115.     if (WSAPoll == NULL) {
  116.         ngx_log_error(NGX_LOG_NOTICE, log, ngx_errno,
  117.                       "GetProcAddress(\"WSAPoll\") failed");
  118.         goto nopoll;
  119.     }

  120.     ngx_have_wsapoll = 1;

  121.     }

  122. nopoll:

  123. #endif

  124.     if (GetEnvironmentVariable("ngx_unique", ngx_unique, NGX_INT32_LEN + 1)
  125.         != 0)
  126.     {
  127.         ngx_process = NGX_PROCESS_WORKER;

  128.     } else {
  129.         err = ngx_errno;

  130.         if (err != ERROR_ENVVAR_NOT_FOUND) {
  131.             ngx_log_error(NGX_LOG_EMERG, log, err,
  132.                           "GetEnvironmentVariable(\"ngx_unique\") failed");
  133.             return NGX_ERROR;
  134.         }

  135.         ngx_sprintf((u_char *) ngx_unique, "%P%Z", ngx_pid);
  136.     }

  137.     tp = ngx_timeofday();
  138.     srand((ngx_pid << 16) ^ (unsigned) tp->sec ^ tp->msec);

  139.     return NGX_OK;
  140. }


  141. void
  142. ‌ngx_os_status(ngx_log_t *log)
  143. {
  144.     ngx_osviex_stub_t  *osviex_stub;

  145.     ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER_BUILD);

  146.     if (osviex) {

  147.         /*
  148.          * the MSVC 6.0 SP2 defines wSuiteMask and wProductType
  149.          * as WORD wReserved[2]
  150.          */
  151.         osviex_stub = (ngx_osviex_stub_t *) &osvi.wServicePackMinor;

  152.         ngx_log_error(NGX_LOG_INFO, log, 0,
  153.                       "OS: %ui build:%ud, \"%s\", suite:%Xd, type:%ud",
  154.                       ngx_win32_version, osvi.dwBuildNumber, osvi.szCSDVersion,
  155.                       osviex_stub->wSuiteMask, osviex_stub->wProductType);

  156.     } else {
  157.         if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {

  158.             /* Win9x build */

  159.             ngx_log_error(NGX_LOG_INFO, log, 0,
  160.                           "OS: %ui build:%ud.%ud.%ud, \"%s\"",
  161.                           ngx_win32_version,
  162.                           osvi.dwBuildNumber >> 24,
  163.                           (osvi.dwBuildNumber >> 16) & 0xff,
  164.                           osvi.dwBuildNumber & 0xffff,
  165.                           osvi.szCSDVersion);

  166.         } else {

  167.             /*
  168.              * VER_PLATFORM_WIN32_NT
  169.              *
  170.              * we do not currently support VER_PLATFORM_WIN32_CE
  171.              * and we do not support VER_PLATFORM_WIN32s at all
  172.              */

  173.             ngx_log_error(NGX_LOG_INFO, log, 0, "OS: %ui build:%ud, \"%s\"",
  174.                           ngx_win32_version, osvi.dwBuildNumber,
  175.                           osvi.szCSDVersion);
  176.         }
  177.     }
  178. }