Call SetShmBase() in init_rs

The ShmEventLoop rust test was using /dev/shm/aos and breaking out of
the sandbox. Fix that.

Change-Id: I14db8b500a03a66fef41399c4cea334ff79b217f
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/testing/BUILD b/aos/testing/BUILD
index 787fdda..02459e4 100644
--- a/aos/testing/BUILD
+++ b/aos/testing/BUILD
@@ -8,6 +8,7 @@
     visibility = ["//visibility:public"],
     deps = [
         "//aos:init",
+        "//aos/testing:tmpdir",
         "@com_github_gflags_gflags//:gflags",
         "@com_github_google_glog//:glog",
         "@com_google_googletest//:gtest",
@@ -100,6 +101,9 @@
     hdrs = ["tmpdir.h"],
     target_compatible_with = ["@platforms//os:linux"],
     visibility = ["//visibility:public"],
+    deps = [
+        "//aos/ipc_lib:shm_base",
+    ],
 )
 
 cc_library(
diff --git a/aos/testing/gtest_main.cc b/aos/testing/gtest_main.cc
index 21f141f..2fb77fe 100644
--- a/aos/testing/gtest_main.cc
+++ b/aos/testing/gtest_main.cc
@@ -7,6 +7,7 @@
 #include "gtest/gtest.h"
 
 #include "aos/init.h"
+#include "aos/testing/tmpdir.h"
 
 DEFINE_bool(print_logs, false,
             "Print the log messages as they are being generated.");
@@ -14,7 +15,6 @@
               "Print all log messages to FILE instead of standard output.");
 
 namespace aos {
-void SetShmBase(const std::string_view base) __attribute__((weak));
 
 namespace testing {
 
@@ -48,12 +48,7 @@
 
   // Point shared memory away from /dev/shm if we are testing.  We don't care
   // about RT in this case, so if it is backed by disk, we are fine.
-  if (::aos::SetShmBase) {
-    const char *tmpdir_c_str = getenv("TEST_TMPDIR");
-    if (tmpdir_c_str != nullptr) {
-      aos::SetShmBase(tmpdir_c_str);
-    }
-  }
+  aos::testing::SetTestShmBase();
 
   return RUN_ALL_TESTS();
 }
diff --git a/aos/testing/test_shm.cc b/aos/testing/test_shm.cc
index 0787e5a..8dc189a 100644
--- a/aos/testing/test_shm.cc
+++ b/aos/testing/test_shm.cc
@@ -2,6 +2,8 @@
 
 #include <sys/mman.h>
 
+#include "gflags/gflags.h"
+
 #include "aos/logging/logging.h"
 #include "aos/testing/test_logging.h"
 
diff --git a/aos/testing/tmpdir.cc b/aos/testing/tmpdir.cc
index 15e3c13..7287603 100644
--- a/aos/testing/tmpdir.cc
+++ b/aos/testing/tmpdir.cc
@@ -3,16 +3,24 @@
 #include <cstdlib>
 #include <string>
 
+#include "aos/ipc_lib/shm_base.h"
+
 namespace aos {
 namespace testing {
 
-std::string TestTmpDir() {
+namespace {
+std::string TestTmpDirOr(std::string fallback) {
   const char *tmp_dir = std::getenv("TEST_TMPDIR");
   if (tmp_dir != nullptr) {
     return tmp_dir;
   }
-  return "/tmp";
+  return fallback;
 }
+}  // namespace
+
+std::string TestTmpDir() { return TestTmpDirOr("/tmp"); }
+
+void SetTestShmBase() { SetShmBase(TestTmpDirOr(FLAGS_shm_base)); }
 
 }  // namespace testing
 }  // namespace aos
diff --git a/aos/testing/tmpdir.h b/aos/testing/tmpdir.h
index 7e64342..8eabf86 100644
--- a/aos/testing/tmpdir.h
+++ b/aos/testing/tmpdir.h
@@ -9,6 +9,10 @@
 // Returns a usable temporary directory.
 std::string TestTmpDir();
 
+// Sets shm_base to a folder inside of TEST_TMPDIR if set, or --shm_base
+// otherwise.
+void SetTestShmBase();
+
 }  // namespace testing
 }  // namespace aos