uname.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (c) 2003-2004, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file uname.c
  7. * \brief Look up a description of the operating system.
  8. **/
  9. #include "orconfig.h"
  10. #include "lib/osinfo/uname.h"
  11. #include "lib/string/compat_string.h"
  12. #include "lib/string/printf.h"
  13. #ifdef HAVE_UNAME
  14. #include <sys/utsname.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <windows.h>
  18. #endif
  19. #include <string.h>
  20. /** Hold the result of our call to <b>uname</b>. */
  21. static char uname_result[256];
  22. /** True iff uname_result is set. */
  23. static int uname_result_is_set = 0;
  24. /** Return a pointer to a description of our platform.
  25. */
  26. MOCK_IMPL(const char *,
  27. get_uname,(void))
  28. {
  29. #ifdef HAVE_UNAME
  30. struct utsname u;
  31. #endif
  32. if (!uname_result_is_set) {
  33. #ifdef HAVE_UNAME
  34. if (uname(&u) != -1) {
  35. /* (Linux says 0 is success, Solaris says 1 is success) */
  36. strlcpy(uname_result, u.sysname, sizeof(uname_result));
  37. } else
  38. #endif /* defined(HAVE_UNAME) */
  39. {
  40. #ifdef _WIN32
  41. OSVERSIONINFOEX info;
  42. int i;
  43. int is_client = 0;
  44. int is_server = 0;
  45. const char *plat = NULL;
  46. static struct {
  47. unsigned major; unsigned minor;
  48. const char *client_version; const char *server_version;
  49. } win_version_table[] = {
  50. /* This table must be sorted in descending order.
  51. * Sources:
  52. * https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
  53. * https://docs.microsoft.com/en-us/windows/desktop/api/winnt/
  54. * ns-winnt-_osversioninfoexa#remarks
  55. */
  56. /* Windows Server 2019 is indistinguishable from Windows Server 2016
  57. * using GetVersionEx().
  58. { 10, 0, NULL, "Windows Server 2019" }, */
  59. { 10, 0, "Windows 10", "Windows Server 2016" },
  60. { 6, 3, "Windows 8.1", "Windows Server 2012 R2" },
  61. { 6, 2, "Windows 8", "Windows Server 2012" },
  62. { 6, 1, "Windows 7", "Windows Server 2008 R2" },
  63. { 6, 0, "Windows Vista", "Windows Server 2008" },
  64. { 5, 2, "Windows XP Professional", "Windows Server 2003" },
  65. /* Windows XP did not have a server version, but we need something here */
  66. { 5, 1, "Windows XP", "Windows XP Server" },
  67. { 5, 0, "Windows 2000 Professional", "Windows 2000 Server" },
  68. /* Earlier versions are not supported by GetVersionEx(). */
  69. { 0, 0, NULL, NULL }
  70. };
  71. memset(&info, 0, sizeof(info));
  72. info.dwOSVersionInfoSize = sizeof(info);
  73. if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
  74. strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
  75. " doesn't work.", sizeof(uname_result));
  76. uname_result_is_set = 1;
  77. return uname_result;
  78. }
  79. #ifdef VER_NT_SERVER
  80. if (info.wProductType == VER_NT_SERVER ||
  81. info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
  82. is_server = 1;
  83. } else {
  84. is_client = 1;
  85. }
  86. #endif /* defined(VER_NT_SERVER) */
  87. /* Search the version table for a matching version */
  88. for (i=0; win_version_table[i].major>0; ++i) {
  89. if (win_version_table[i].major == info.dwMajorVersion &&
  90. win_version_table[i].minor == info.dwMinorVersion) {
  91. if (is_server) {
  92. plat = win_version_table[i].server_version;
  93. } else {
  94. /* Use client versions for clients, and when we don't know if it
  95. * is a client or a server. */
  96. plat = win_version_table[i].client_version;
  97. }
  98. break;
  99. }
  100. }
  101. if (plat) {
  102. strlcpy(uname_result, plat, sizeof(uname_result));
  103. } else {
  104. if (info.dwMajorVersion > win_version_table[0].major ||
  105. (info.dwMajorVersion == win_version_table[0].major &&
  106. info.dwMinorVersion > win_version_table[0].minor))
  107. tor_snprintf(uname_result, sizeof(uname_result),
  108. "Very recent version of Windows [major=%d,minor=%d]",
  109. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  110. else
  111. tor_snprintf(uname_result, sizeof(uname_result),
  112. "Unrecognized version of Windows [major=%d,minor=%d]",
  113. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  114. }
  115. /* Now append extra information to the name.
  116. *
  117. * Microsoft's API documentation says that on Windows 8.1 and later,
  118. * GetVersionEx returns Windows 8 (6.2) for applications without an
  119. * app compatibility manifest (including tor's default build).
  120. *
  121. * But in our testing, we have seen the actual Windows version on
  122. * Windows Server 2012 R2, even without a manifest. */
  123. if (info.dwMajorVersion > 6 ||
  124. (info.dwMajorVersion == 6 && info.dwMinorVersion >= 2)) {
  125. /* When GetVersionEx() returns Windows 8, the actual OS may be any
  126. * later version. */
  127. strlcat(uname_result, " [or later]", sizeof(uname_result));
  128. }
  129. /* When we don't know if the OS is a client or server version, we use
  130. * the client version, and this qualifier. */
  131. if (!is_server && !is_client) {
  132. strlcat(uname_result, " [client or server]", sizeof(uname_result));
  133. }
  134. #else /* !(defined(_WIN32)) */
  135. /* LCOV_EXCL_START -- can't provoke uname failure */
  136. strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
  137. /* LCOV_EXCL_STOP */
  138. #endif /* defined(_WIN32) */
  139. }
  140. uname_result_is_set = 1;
  141. }
  142. return uname_result;
  143. }