uname.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #include "orconfig.h"
  6. #include "lib/osinfo/uname.h"
  7. #include "lib/string/compat_string.h"
  8. #include "lib/string/printf.h"
  9. #ifdef HAVE_UNAME
  10. #include <sys/utsname.h>
  11. #endif
  12. #ifdef _WIN32
  13. #include <windows.h>
  14. #endif
  15. /** Hold the result of our call to <b>uname</b>. */
  16. static char uname_result[256];
  17. /** True iff uname_result is set. */
  18. static int uname_result_is_set = 0;
  19. /** Return a pointer to a description of our platform.
  20. */
  21. MOCK_IMPL(const char *,
  22. get_uname,(void))
  23. {
  24. #ifdef HAVE_UNAME
  25. struct utsname u;
  26. #endif
  27. if (!uname_result_is_set) {
  28. #ifdef HAVE_UNAME
  29. if (uname(&u) != -1) {
  30. /* (Linux says 0 is success, Solaris says 1 is success) */
  31. strlcpy(uname_result, u.sysname, sizeof(uname_result));
  32. } else
  33. #endif /* defined(HAVE_UNAME) */
  34. {
  35. #ifdef _WIN32
  36. OSVERSIONINFOEX info;
  37. int i;
  38. const char *plat = NULL;
  39. static struct {
  40. unsigned major; unsigned minor; const char *version;
  41. } win_version_table[] = {
  42. { 6, 2, "Windows 8" },
  43. { 6, 1, "Windows 7" },
  44. { 6, 0, "Windows Vista" },
  45. { 5, 2, "Windows Server 2003" },
  46. { 5, 1, "Windows XP" },
  47. { 5, 0, "Windows 2000" },
  48. /* { 4, 0, "Windows NT 4.0" }, */
  49. { 4, 90, "Windows Me" },
  50. { 4, 10, "Windows 98" },
  51. /* { 4, 0, "Windows 95" } */
  52. { 3, 51, "Windows NT 3.51" },
  53. { 0, 0, NULL }
  54. };
  55. memset(&info, 0, sizeof(info));
  56. info.dwOSVersionInfoSize = sizeof(info);
  57. if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
  58. strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
  59. " doesn't work.", sizeof(uname_result));
  60. uname_result_is_set = 1;
  61. return uname_result;
  62. }
  63. if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
  64. if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
  65. plat = "Windows NT 4.0";
  66. else
  67. plat = "Windows 95";
  68. } else {
  69. for (i=0; win_version_table[i].major>0; ++i) {
  70. if (win_version_table[i].major == info.dwMajorVersion &&
  71. win_version_table[i].minor == info.dwMinorVersion) {
  72. plat = win_version_table[i].version;
  73. break;
  74. }
  75. }
  76. }
  77. if (plat) {
  78. strlcpy(uname_result, plat, sizeof(uname_result));
  79. } else {
  80. if (info.dwMajorVersion > 6 ||
  81. (info.dwMajorVersion==6 && info.dwMinorVersion>2))
  82. tor_snprintf(uname_result, sizeof(uname_result),
  83. "Very recent version of Windows [major=%d,minor=%d]",
  84. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  85. else
  86. tor_snprintf(uname_result, sizeof(uname_result),
  87. "Unrecognized version of Windows [major=%d,minor=%d]",
  88. (int)info.dwMajorVersion,(int)info.dwMinorVersion);
  89. }
  90. #ifdef VER_NT_SERVER
  91. if (info.wProductType == VER_NT_SERVER ||
  92. info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
  93. strlcat(uname_result, " [server]", sizeof(uname_result));
  94. }
  95. #endif /* defined(VER_NT_SERVER) */
  96. #else /* !(defined(_WIN32)) */
  97. /* LCOV_EXCL_START -- can't provoke uname failure */
  98. strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
  99. /* LCOV_EXCL_STOP */
  100. #endif /* defined(_WIN32) */
  101. }
  102. uname_result_is_set = 1;
  103. }
  104. return uname_result;
  105. }