[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Extract get_uname to lib/osinfo.



commit e0957022bd108696a941bdb284420fc55cbcf187
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Fri Jun 29 10:37:35 2018 -0400

    Extract get_uname to lib/osinfo.
---
 .gitignore                  |   2 +
 Makefile.am                 |   2 +
 src/common/compat.c         |  93 -------------------------------------
 src/common/compat.h         |   1 -
 src/include.am              |   1 +
 src/lib/osinfo/.may_include |   5 ++
 src/lib/osinfo/include.am   |  17 +++++++
 src/lib/osinfo/uname.c      | 110 ++++++++++++++++++++++++++++++++++++++++++++
 src/lib/osinfo/uname.h      |   9 ++++
 src/or/config.c             |   1 +
 src/or/main.c               |   1 +
 src/or/router.c             |   3 +-
 src/test/test_dir.c         |   2 +-
 src/test/test_options.c     |   2 +-
 14 files changed, 152 insertions(+), 97 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2e37c3436..b21b03efe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -195,6 +195,8 @@ uptime-*.json
 /src/lib/libtor-meminfo-testing.a
 /src/lib/libtor-net.a
 /src/lib/libtor-net-testing.a
+/src/lib/libtor-osinfo.a
+/src/lib/libtor-osinfo-testing.a
 /src/lib/libtor-process.a
 /src/lib/libtor-process-testing.a
 /src/lib/libtor-sandbox.a
diff --git a/Makefile.am b/Makefile.am
index f44f64732..68cc3b5f1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -51,6 +51,7 @@ TOR_UTIL_LIBS = \
 	src/lib/libtor-memarea.a \
 	src/lib/libtor-math.a \
         src/lib/libtor-meminfo.a \
+	src/lib/libtor-osinfo.a \
 	src/lib/libtor-log.a \
 	src/lib/libtor-lock.a \
 	src/lib/libtor-fdio.a \
@@ -77,6 +78,7 @@ TOR_UTIL_TESTING_LIBS = \
 	src/lib/libtor-memarea-testing.a \
 	src/lib/libtor-math-testing.a \
         src/lib/libtor-meminfo-testing.a \
+	src/lib/libtor-osinfo-testing.a \
 	src/lib/libtor-log-testing.a \
 	src/lib/libtor-lock-testing.a \
 	src/lib/libtor-fdio-testing.a \
diff --git a/src/common/compat.c b/src/common/compat.c
index 6eb4ee77b..2f9db70a4 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -128,99 +128,6 @@ SecureZeroMemory(PVOID ptr, SIZE_T cnt)
 #include "lib/net/address.h"
 #include "lib/sandbox/sandbox.h"
 
-/** Hold the result of our call to <b>uname</b>. */
-static char uname_result[256];
-/** True iff uname_result is set. */
-static int uname_result_is_set = 0;
-
-/** Return a pointer to a description of our platform.
- */
-MOCK_IMPL(const char *,
-get_uname,(void))
-{
-#ifdef HAVE_UNAME
-  struct utsname u;
-#endif
-  if (!uname_result_is_set) {
-#ifdef HAVE_UNAME
-    if (uname(&u) != -1) {
-      /* (Linux says 0 is success, Solaris says 1 is success) */
-      strlcpy(uname_result, u.sysname, sizeof(uname_result));
-    } else
-#endif /* defined(HAVE_UNAME) */
-      {
-#ifdef _WIN32
-        OSVERSIONINFOEX info;
-        int i;
-        const char *plat = NULL;
-        static struct {
-          unsigned major; unsigned minor; const char *version;
-        } win_version_table[] = {
-          { 6, 2, "Windows 8" },
-          { 6, 1, "Windows 7" },
-          { 6, 0, "Windows Vista" },
-          { 5, 2, "Windows Server 2003" },
-          { 5, 1, "Windows XP" },
-          { 5, 0, "Windows 2000" },
-          /* { 4, 0, "Windows NT 4.0" }, */
-          { 4, 90, "Windows Me" },
-          { 4, 10, "Windows 98" },
-          /* { 4, 0, "Windows 95" } */
-          { 3, 51, "Windows NT 3.51" },
-          { 0, 0, NULL }
-        };
-        memset(&info, 0, sizeof(info));
-        info.dwOSVersionInfoSize = sizeof(info);
-        if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
-          strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
-                  " doesn't work.", sizeof(uname_result));
-          uname_result_is_set = 1;
-          return uname_result;
-        }
-        if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
-          if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
-            plat = "Windows NT 4.0";
-          else
-            plat = "Windows 95";
-        } else {
-          for (i=0; win_version_table[i].major>0; ++i) {
-            if (win_version_table[i].major == info.dwMajorVersion &&
-                win_version_table[i].minor == info.dwMinorVersion) {
-              plat = win_version_table[i].version;
-              break;
-            }
-          }
-        }
-        if (plat) {
-          strlcpy(uname_result, plat, sizeof(uname_result));
-        } else {
-          if (info.dwMajorVersion > 6 ||
-              (info.dwMajorVersion==6 && info.dwMinorVersion>2))
-            tor_snprintf(uname_result, sizeof(uname_result),
-                         "Very recent version of Windows [major=%d,minor=%d]",
-                         (int)info.dwMajorVersion,(int)info.dwMinorVersion);
-          else
-            tor_snprintf(uname_result, sizeof(uname_result),
-                         "Unrecognized version of Windows [major=%d,minor=%d]",
-                         (int)info.dwMajorVersion,(int)info.dwMinorVersion);
-        }
-#ifdef VER_NT_SERVER
-      if (info.wProductType == VER_NT_SERVER ||
-          info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
-        strlcat(uname_result, " [server]", sizeof(uname_result));
-      }
-#endif /* defined(VER_NT_SERVER) */
-#else /* !(defined(_WIN32)) */
-        /* LCOV_EXCL_START -- can't provoke uname failure */
-        strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
-        /* LCOV_EXCL_STOP */
-#endif /* defined(_WIN32) */
-      }
-    uname_result_is_set = 1;
-  }
-  return uname_result;
-}
-
 /*
  *   Process control
  */
diff --git a/src/common/compat.h b/src/common/compat.h
index 85e760d04..7e7a81496 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -85,7 +85,6 @@ typedef enum {
 } socks5_reply_status_t;
 
 /* ===== OS compatibility */
-MOCK_DECL(const char *, get_uname, (void));
 
 ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
 
diff --git a/src/include.am b/src/include.am
index 703aab17e..69a2dc5ae 100644
--- a/src/include.am
+++ b/src/include.am
@@ -19,6 +19,7 @@ include src/lib/memarea/include.am
 include src/lib/meminfo/include.am
 include src/lib/malloc/include.am
 include src/lib/net/include.am
+include src/lib/osinfo/include.am
 include src/lib/process/include.am
 include src/lib/sandbox/include.am
 include src/lib/string/include.am
diff --git a/src/lib/osinfo/.may_include b/src/lib/osinfo/.may_include
new file mode 100644
index 000000000..058f6eb89
--- /dev/null
+++ b/src/lib/osinfo/.may_include
@@ -0,0 +1,5 @@
+orconfig.h
+
+lib/osinfo/*.h
+lib/string/*.h
+lib/testsupport/*.h
diff --git a/src/lib/osinfo/include.am b/src/lib/osinfo/include.am
new file mode 100644
index 000000000..16c581260
--- /dev/null
+++ b/src/lib/osinfo/include.am
@@ -0,0 +1,17 @@
+
+noinst_LIBRARIES += src/lib/libtor-osinfo.a
+
+if UNITTESTS_ENABLED
+noinst_LIBRARIES += src/lib/libtor-osinfo-testing.a
+endif
+
+src_lib_libtor_osinfo_a_SOURCES =			\
+	src/lib/osinfo/uname.c
+
+src_lib_libtor_osinfo_testing_a_SOURCES = \
+	$(src_lib_libtor_osinfo_a_SOURCES)
+src_lib_libtor_osinfo_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS)
+src_lib_libtor_osinfo_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
+
+noinst_HEADERS +=					\
+	src/lib/osinfo/uname.h
diff --git a/src/lib/osinfo/uname.c b/src/lib/osinfo/uname.c
new file mode 100644
index 000000000..fb5cc7c20
--- /dev/null
+++ b/src/lib/osinfo/uname.c
@@ -0,0 +1,110 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "lib/osinfo/uname.h"
+
+#include "lib/string/compat_string.h"
+#include "lib/string/printf.h"
+
+#ifdef HAVE_UNAME
+#include <sys/utsname.h>
+#endif
+#ifdef _WIN32
+#include <windows.h>
+#endif
+
+/** Hold the result of our call to <b>uname</b>. */
+static char uname_result[256];
+/** True iff uname_result is set. */
+static int uname_result_is_set = 0;
+
+/** Return a pointer to a description of our platform.
+ */
+MOCK_IMPL(const char *,
+get_uname,(void))
+{
+#ifdef HAVE_UNAME
+  struct utsname u;
+#endif
+  if (!uname_result_is_set) {
+#ifdef HAVE_UNAME
+    if (uname(&u) != -1) {
+      /* (Linux says 0 is success, Solaris says 1 is success) */
+      strlcpy(uname_result, u.sysname, sizeof(uname_result));
+    } else
+#endif /* defined(HAVE_UNAME) */
+      {
+#ifdef _WIN32
+        OSVERSIONINFOEX info;
+        int i;
+        const char *plat = NULL;
+        static struct {
+          unsigned major; unsigned minor; const char *version;
+        } win_version_table[] = {
+          { 6, 2, "Windows 8" },
+          { 6, 1, "Windows 7" },
+          { 6, 0, "Windows Vista" },
+          { 5, 2, "Windows Server 2003" },
+          { 5, 1, "Windows XP" },
+          { 5, 0, "Windows 2000" },
+          /* { 4, 0, "Windows NT 4.0" }, */
+          { 4, 90, "Windows Me" },
+          { 4, 10, "Windows 98" },
+          /* { 4, 0, "Windows 95" } */
+          { 3, 51, "Windows NT 3.51" },
+          { 0, 0, NULL }
+        };
+        memset(&info, 0, sizeof(info));
+        info.dwOSVersionInfoSize = sizeof(info);
+        if (! GetVersionEx((LPOSVERSIONINFO)&info)) {
+          strlcpy(uname_result, "Bizarre version of Windows where GetVersionEx"
+                  " doesn't work.", sizeof(uname_result));
+          uname_result_is_set = 1;
+          return uname_result;
+        }
+        if (info.dwMajorVersion == 4 && info.dwMinorVersion == 0) {
+          if (info.dwPlatformId == VER_PLATFORM_WIN32_NT)
+            plat = "Windows NT 4.0";
+          else
+            plat = "Windows 95";
+        } else {
+          for (i=0; win_version_table[i].major>0; ++i) {
+            if (win_version_table[i].major == info.dwMajorVersion &&
+                win_version_table[i].minor == info.dwMinorVersion) {
+              plat = win_version_table[i].version;
+              break;
+            }
+          }
+        }
+        if (plat) {
+          strlcpy(uname_result, plat, sizeof(uname_result));
+        } else {
+          if (info.dwMajorVersion > 6 ||
+              (info.dwMajorVersion==6 && info.dwMinorVersion>2))
+            tor_snprintf(uname_result, sizeof(uname_result),
+                         "Very recent version of Windows [major=%d,minor=%d]",
+                         (int)info.dwMajorVersion,(int)info.dwMinorVersion);
+          else
+            tor_snprintf(uname_result, sizeof(uname_result),
+                         "Unrecognized version of Windows [major=%d,minor=%d]",
+                         (int)info.dwMajorVersion,(int)info.dwMinorVersion);
+        }
+#ifdef VER_NT_SERVER
+      if (info.wProductType == VER_NT_SERVER ||
+          info.wProductType == VER_NT_DOMAIN_CONTROLLER) {
+        strlcat(uname_result, " [server]", sizeof(uname_result));
+      }
+#endif /* defined(VER_NT_SERVER) */
+#else /* !(defined(_WIN32)) */
+        /* LCOV_EXCL_START -- can't provoke uname failure */
+        strlcpy(uname_result, "Unknown platform", sizeof(uname_result));
+        /* LCOV_EXCL_STOP */
+#endif /* defined(_WIN32) */
+      }
+    uname_result_is_set = 1;
+  }
+  return uname_result;
+}
diff --git a/src/lib/osinfo/uname.h b/src/lib/osinfo/uname.h
new file mode 100644
index 000000000..1f0b78385
--- /dev/null
+++ b/src/lib/osinfo/uname.h
@@ -0,0 +1,9 @@
+
+#ifndef HAVE_TOR_UNAME_H
+#define HAVE_TOR_UNAME_H
+
+#include "lib/testsupport/testsupport.h"
+
+MOCK_DECL(const char *, get_uname,(void));
+
+#endif
diff --git a/src/or/config.c b/src/or/config.c
index b98f124a8..458b6bae2 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -112,6 +112,7 @@
 #endif
 
 #include "lib/meminfo/meminfo.h"
+#include "lib/osinfo/uname.h"
 #include "lib/process/daemon.h"
 #include "lib/process/pidfile.h"
 #include "lib/process/restrict.h"
diff --git a/src/or/main.c b/src/or/main.c
index 53be1b103..81a8cfbee 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -112,6 +112,7 @@
 #include "or/ext_orport.h"
 #include "lib/memarea/memarea.h"
 #include "lib/meminfo/meminfo.h"
+#include "lib/osinfo/uname.h"
 #include "lib/sandbox/sandbox.h"
 #include "lib/fs/lockfile.h"
 #include "lib/net/buffers_net.h"
diff --git a/src/or/router.c b/src/or/router.c
index f13788d66..c7fed1f02 100644
--- a/src/or/router.c
+++ b/src/or/router.c
@@ -50,6 +50,8 @@
 #include "or/port_cfg_st.h"
 #include "or/routerinfo_st.h"
 
+#include "lib/osinfo/uname.h"
+
 /**
  * \file router.c
  * \brief Miscellaneous relay functionality, including RSA key maintenance,
@@ -3824,4 +3826,3 @@ router_get_all_orports(const routerinfo_t *ri)
   fake_node.ri = (routerinfo_t *)ri;
   return node_get_all_orports(&fake_node);
 }
-
diff --git a/src/test/test_dir.c b/src/test/test_dir.c
index 2643161fc..498ecf942 100644
--- a/src/test/test_dir.c
+++ b/src/test/test_dir.c
@@ -31,6 +31,7 @@
 #include "or/fp_pair.h"
 #include "or/hibernate.h"
 #include "lib/memarea/memarea.h"
+#include "lib/osinfo/uname.h"
 #include "or/networkstatus.h"
 #include "or/router.h"
 #include "or/routerkeys.h"
@@ -6042,4 +6043,3 @@ struct testcase_t dir_tests[] = {
   DIR(networkstatus_consensus_has_ipv6, TT_FORK),
   END_OF_TESTCASES
 };
-
diff --git a/src/test/test_options.c b/src/test/test_options.c
index 5860bef7a..ab8727c3d 100644
--- a/src/test/test_options.c
+++ b/src/test/test_options.c
@@ -17,6 +17,7 @@
 
 #include "lib/sandbox/sandbox.h"
 #include "lib/memarea/memarea.h"
+#include "lib/osinfo/uname.h"
 #include "or/policies.h"
 #include "test/test_helpers.h"
 
@@ -4237,4 +4238,3 @@ struct testcase_t options_tests[] = {
   LOCAL_VALIDATE_TEST(accel),
   END_OF_TESTCASES              /*  */
 };
-



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits