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