uname.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. const char *plat = NULL;
  44. static struct {
  45. unsigned major; unsigned minor; const char *version;
  46. } win_version_table[] = {
  47. { 6, 2, "Windows 8" },
  48. { 6, 1, "Windows 7" },
  49. { 6, 0, "Windows Vista" },
  50. { 5, 2, "Windows Server 2003" },
  51. { 5, 1, "Windows XP" },
  52. { 5, 0, "Windows 2000" },
  53. /* { 4, 0, "Windows NT 4.0" }, */
  54. { 4, 90, "Windows Me" },
  55. { 4, 10, "Windows 98" },
  56. /* { 4, 0, "Windows 95" } */
  57. { 3, 51, "Windows NT 3.51" },
  58. { 0, 0, NULL }
  59. };
  60. memset(&info, 0, sizeof(info));
  61. info.dwOSVersionInfoSize = sizeof(info);
  62. if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
  63. strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
  64. " doesn't work.", sizeof(uname_result));
  65. uname_result_is_set = 1;
  66. return uname_result;
  67. }
  68. if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
  69. if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
  70. plat = "Windows NT 4.0";
  71. else
  72. plat = "Windows 95";
  73. } else {
  74. for (i=0; win_version_table[i].major>0; ++i) {
  75. if (win_version_table[i].major == info.dwMajorVersion &&
  76. win_version_table[i].minor == info.dwMinorVersion) {
  77. plat = win_version_table[i].version;
  78. break;
  79. }
  80. }
  81. }
  82. if (plat) {
  83. strlcpy(uname_result, plat, sizeof(uname_result));
  84. } else {
  85. if (info.dwMajorVersion > 6 ||
  86. (info.dwMajorVersion==6 && info.dwMinorVersion>2))
  87. tor_snprintf(uname_result, sizeof(uname_result),
  88. "Very recent version of Windows [major=%d,minor=%d]",
  89. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  90. else
  91. tor_snprintf(uname_result, sizeof(uname_result),
  92. "Unrecognized version of Windows [major=%d,minor=%d]",
  93. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  94. }
  95. #ifdef VER_NT_SERVER
  96. if (info.wProductType == VER_NT_SERVER ||
  97. info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
  98. strlcat(uname_result, " [server]", sizeof(uname_result));
  99. }
  100. #endif /* defined(VER_NT_SERVER) */
  101. #else /* !(defined(_WIN32)) */
  102. /* LCOV_EXCL_START -- can't provoke uname failure */
  103. strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
  104. /* LCOV_EXCL_STOP */
  105. #endif /* defined(_WIN32) */
  106. }
  107. uname_result_is_set = 1;
  108. }
  109. return uname_result;
  110. }