actually support running non-realtime on not-robots

Change-Id: I2d21bc3a43de39d685de550402769f55426b275b
diff --git a/aos/common/network/team_number.cc b/aos/common/network/team_number.cc
index 5136c87..134113a 100644
--- a/aos/common/network/team_number.cc
+++ b/aos/common/network/team_number.cc
@@ -3,6 +3,7 @@
 #include <netinet/in.h>
 #include <inttypes.h>
 #include <unistd.h>
+#include <stdlib.h>
 
 #include <string>
 
@@ -47,12 +48,23 @@
 
 uint16_t *DoGetTeamNumber() {
   if (override_team != 0) return &override_team;
+
   static uint16_t r;
-  int error = ParseTeamNumber(GetHostname(), &r);
-  if (error) {
-    LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str());
+
+  const char *override_number = getenv("AOS_TEAM_NUMBER");
+  if (override_number != nullptr) {
+    if (!::aos::util::StringToNumber(override_number, &r)) {
+      LOG(FATAL, "error parsing AOS_TEAM_NUMBER '%s'\n", override_number);
+    }
+    LOG(WARNING, "team number overridden by AOS_TEAM_NUMBER to %" PRIu16 "\n",
+        r);
+  } else {
+    int error = ParseTeamNumber(GetHostname(), &r);
+    if (error) {
+      LOG(FATAL, "Invalid hostname %s\n", GetHostname().c_str());
+    }
+    LOG(INFO, "team number is %" PRIu16 "\n", r);
   }
-  LOG(INFO, "team number is %" PRIu16 "\n", r);
   return &r;
 }
 
diff --git a/aos/linux_code/init.cc b/aos/linux_code/init.cc
index c38bfc0..eb3658c 100644
--- a/aos/linux_code/init.cc
+++ b/aos/linux_code/init.cc
@@ -59,12 +59,13 @@
 // non-realtime initialization sequences. May be called twice.
 void InitStart() {
   ::aos::logging::Init();
-  // Allow locking as much as we want into RAM.
-  SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
   WriteCoreDumps();
 }
 
 void LockAllMemory() {
+  // Allow locking as much as we want into RAM.
+  SetSoftRLimit(RLIMIT_MEMLOCK, RLIM_INFINITY, false);
+
   InitStart();
   if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
     PDie("%s-init: mlockall failed", program_invocation_short_name);
@@ -92,22 +93,25 @@
   free(heap_data);
 }
 
-// Do the initialization code that is necessary for both realtime and
-// non-realtime processes.
-void DoInitNRT(aos_core_create create) {
-  InitStart();
-  aos_core_create_shared_mem(create);
-  logging::linux_code::Register();
-}
-
 const char *const kNoRealtimeEnvironmentVariable = "AOS_NO_REALTIME";
 
 }  // namespace
 
-void InitNRT() { DoInitNRT(aos_core_create::reference); }
-void InitCreate() { DoInitNRT(aos_core_create::create); }
+void InitNRT() {
+  InitStart();
+  aos_core_create_shared_mem(false, false);
+  logging::linux_code::Register();
+}
+
+void InitCreate() {
+  InitStart();
+  aos_core_create_shared_mem(true, false);
+  logging::linux_code::Register();
+}
+
 void Init(int relative_priority) {
-  if (getenv(kNoRealtimeEnvironmentVariable) == NULL) {  // if nobody set it
+  bool realtime = getenv(kNoRealtimeEnvironmentVariable) == nullptr;
+  if (realtime) {
     LockAllMemory();
 
     // Only let rt processes run for 3 seconds straight.
@@ -129,7 +133,9 @@
     printf("no realtime for %s. see stderr\n", program_invocation_short_name);
   }
 
-  InitNRT();
+  InitStart();
+  aos_core_create_shared_mem(false, realtime);
+  logging::linux_code::Register();
 }
 
 void Cleanup() {
diff --git a/aos/linux_code/ipc_lib/shared_mem.c b/aos/linux_code/ipc_lib/shared_mem.c
index bc2cc62..79b747a 100644
--- a/aos/linux_code/ipc_lib/shared_mem.c
+++ b/aos/linux_code/ipc_lib/shared_mem.c
@@ -37,7 +37,7 @@
 struct aos_core *global_core = NULL;
 
 // TODO(brians): madvise(2) it to put this shm in core dumps.
-void aos_core_create_shared_mem(enum aos_core_create to_create) {
+void aos_core_create_shared_mem(int create, int lock) {
   assert(global_core == NULL);
   static struct aos_core global_core_data;
   global_core = &global_core_data;
@@ -53,7 +53,7 @@
   }
 
   int shm;
-  if (to_create == create) {
+  if (create) {
     while (1) {
       printf("shared_mem: creating %s\n", global_core->shm_name);
       shm = shm_open(global_core->shm_name, O_RDWR | O_CREAT | O_EXCL, 0666);
@@ -82,12 +82,13 @@
       PLOG(FATAL, "fruncate(%d, 0x%zx) failed", shm, (size_t)SIZEOFSHMSEG);
     }
   }
-  void *shm_address = mmap(
-      (void *)SHM_START, SIZEOFSHMSEG, PROT_READ | PROT_WRITE,
-      MAP_SHARED | MAP_FIXED | MAP_LOCKED | MAP_POPULATE, shm, 0);
+  int flags = MAP_SHARED | MAP_FIXED;
+  if (lock) flags |= MAP_LOCKED | MAP_POPULATE;
+  void *shm_address = mmap((void *)SHM_START, SIZEOFSHMSEG,
+                           PROT_READ | PROT_WRITE, flags, shm, 0);
   if (shm_address == MAP_FAILED) {
-    PLOG(FATAL, "shared_mem: mmap(%p, 0x%zx, stuff, stuff, %d, 0) failed",
-         (void *)SHM_START, (size_t)SIZEOFSHMSEG, shm);
+    PLOG(FATAL, "shared_mem: mmap(%p, 0x%zx, stuff, %x, %d, 0) failed",
+         (void *)SHM_START, (size_t)SIZEOFSHMSEG, flags, shm);
   }
   printf("shared_mem: shm at: %p\n", shm_address);
   if (close(shm) == -1) {
diff --git a/aos/linux_code/ipc_lib/shared_mem.h b/aos/linux_code/ipc_lib/shared_mem.h
index f1eb489..a33290c 100644
--- a/aos/linux_code/ipc_lib/shared_mem.h
+++ b/aos/linux_code/ipc_lib/shared_mem.h
@@ -44,10 +44,6 @@
   aos_global_pointer queue_types;
 } aos_shm_core;
 
-enum aos_core_create {
-  create,
-  reference
-};
 struct aos_core {
   // Non-0 if we "own" shared_mem and should shm_unlink(3) it when we're done.
   int owner;
@@ -71,7 +67,10 @@
 // before passing the memory to this function.
 void aos_core_use_address_as_shared_mem(void *address, size_t size);
 
-void aos_core_create_shared_mem(enum aos_core_create to_create);
+// create is true to remove any existing shm to create a fresh one or false to
+// fail if it does not already exist.
+// lock is true to lock shared memory into RAM or false to not.
+void aos_core_create_shared_mem(int create, int lock);
 void aos_core_free_shared_mem(void);
 
 // Returns whether or not the shared memory system is active.