uname.c 3.7 KB

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