fixed lots of not-thread-safe things

Most of the things I fixed here were using libc functions that are
fundamentally not thread-safe.
diff --git a/bbb_cape/src/bbb/bbb.gyp b/bbb_cape/src/bbb/bbb.gyp
index e154cef..e5b19b0 100644
--- a/bbb_cape/src/bbb/bbb.gyp
+++ b/bbb_cape/src/bbb/bbb.gyp
@@ -54,6 +54,7 @@
       'dependencies': [
         '<(AOS)/build/aos.gyp:logging',
         '<(AOS)/common/common.gyp:time',
+        '<(AOS)/common/util/util.gyp:run_command',
       ],
     },
     {
@@ -112,6 +113,7 @@
         'crc',
         'byte_io',
         '<(AOS)/common/util/util.gyp:log_interval',
+        '<(AOS)/common/util/util.gyp:run_command',
       ],
       'export_dependent_settings': [
         '<(AOS)/build/aos.gyp:logging',
diff --git a/bbb_cape/src/bbb/export_uart.cc b/bbb_cape/src/bbb/export_uart.cc
index 3d2b24f..bcb1a7c 100644
--- a/bbb_cape/src/bbb/export_uart.cc
+++ b/bbb_cape/src/bbb/export_uart.cc
@@ -3,9 +3,11 @@
 #include <unistd.h>
 #include <errno.h>
 #include <string.h>
+#include <sys/wait.h>
 
 #include "aos/common/logging/logging.h"
 #include "aos/common/time.h"
+#include "aos/common/util/run_command.h"
 
 namespace bbb {
 namespace {
@@ -27,11 +29,15 @@
 void ExportUart() {
   if (easy_access(device)) {
     LOG(INFO, "unexporting BB-UART1\n");
-    if (system("bash -c 'echo -$(cat /sys/devices/bone_capemgr.*/slots"
-               " | fgrep BB-UART1"
-               " | cut -d : -f 1 | tr -d \" \")"
-               " > /sys/devices/bone_capemgr.*/slots'") == -1) {
-      PLOG(FATAL, "system([disable OMAP UART]) failed");
+    const int result = ::aos::util::RunCommand(
+        "bash -c 'echo -$(cat /sys/devices/bone_capemgr.*/slots"
+        " | fgrep BB-UART1"
+        " | cut -d : -f 1 | tr -d \" \")"
+        " > /sys/devices/bone_capemgr.*/slots'");
+    if (result == -1) {
+      PLOG(FATAL, "RunCommand([disable OMAP UART]) failed");
+    } else if (!WIFEXITED(result) || WEXITSTATUS(result) != 0) {
+      LOG(FATAL, "command to disable OMAP UART failed; result = %x\n", result);
     }
     while (easy_access(device)) {
       LOG(DEBUG, "waiting for BB-UART1 to be unexported\n");
@@ -42,10 +48,12 @@
   LOG(INFO, "exporting BB-UART1\n");
   // 2 strings to work around a VIM bug where the indenter locks up when they're
   // combined as 1...
-  if (system("bash -c 'echo BB-UART1 > /sys/devices/bone_capemgr.*"
-             "/slots'") ==
-      -1) {
-    PLOG(FATAL, "system([enable OMAP UART]) failed");
+  const int result = ::aos::util::RunCommand(
+      "bash -c 'echo BB-UART1 > /sys/devices/bone_capemgr.*" "/slots'");
+  if (result == -1) {
+    PLOG(FATAL, "RunCommand([enable OMAP UART]) failed");
+  } else if (!WIFEXITED(result) || WEXITSTATUS(result) != 0) {
+    LOG(FATAL, "command to enable OMAP UART failed; result = %x\n", result);
   }
   while (!easy_access(device)) {
     LOG(DEBUG, "waiting for BB-UART1 to be exported\n");
diff --git a/bbb_cape/src/bbb/packet_finder.cc b/bbb_cape/src/bbb/packet_finder.cc
index b9f1d2f..f3ca2b5 100644
--- a/bbb_cape/src/bbb/packet_finder.cc
+++ b/bbb_cape/src/bbb/packet_finder.cc
@@ -1,3 +1,8 @@
+// This has to come before anybody drags in <stdlib.h> or else we end up with
+// the wrong version of WIFEXITED etc (for one thing, they don't const-qualify
+// their casts) (sometimes at least).
+#include <sys/wait.h>
+
 #include "bbb/packet_finder.h"
 
 #include <inttypes.h>
@@ -8,6 +13,7 @@
 #include <algorithm>
 
 #include "aos/common/logging/logging.h"
+#include "aos/common/util/run_command.h"
 
 #include "cape/cows.h"
 #include "bbb/crc.h"
@@ -65,14 +71,13 @@
       // Iff we're root.
       if (getuid() == 0) {
         // TODO(brians): Do this cleanly.
-        int chrt_result =
-            system("chrt -o 0 bash -c 'chrt -r -p 55"
-                   " $(pgrep irq/89)'");
+        const int chrt_result = ::aos::util::RunCommand(
+            "chrt -o 0 bash -c 'chrt -r -p 55 $(pgrep irq/89)'");
         if (chrt_result == -1) {
-          LOG(FATAL, "system(chrt -r -p 55 the_irq) failed\n");
+          LOG(FATAL, "RunCommand(chrt -r -p 55 the_irq) failed\n");
         } else if (!WIFEXITED(chrt_result) || WEXITSTATUS(chrt_result) != 0) {
-          LOG(FATAL, "$(chrt -r -p 55 the_irq) failed, return value = %d\n",
-              WEXITSTATUS(chrt_result));
+          LOG(FATAL, "$(chrt -r -p 55 the_irq) failed; result = %x\n",
+              chrt_result);
         }
       } else {
         LOG(WARNING, "not root, so not increasing priority of the IRQ\n");
diff --git a/bbb_cape/src/flasher/flasher.gyp b/bbb_cape/src/flasher/flasher.gyp
index fba1477..09e4ccf 100644
--- a/bbb_cape/src/flasher/flasher.gyp
+++ b/bbb_cape/src/flasher/flasher.gyp
@@ -19,6 +19,7 @@
         '<(DEPTH)/bbb_cape/src/bbb/bbb.gyp:gpios',
         '<(DEPTH)/bbb_cape/src/bbb/bbb.gyp:export_uart',
         '<(AOS)/common/common.gyp:time',
+        '<(AOS)/common/libc/libc.gyp:dirname',
       ],
     },
   ],
diff --git a/bbb_cape/src/flasher/stm32_flasher.cc b/bbb_cape/src/flasher/stm32_flasher.cc
index d9d87fe..639908a 100644
--- a/bbb_cape/src/flasher/stm32_flasher.cc
+++ b/bbb_cape/src/flasher/stm32_flasher.cc
@@ -9,6 +9,7 @@
 #include "aos/common/logging/logging.h"
 #include "aos/common/logging/logging_impl.h"
 #include "aos/common/time.h"
+#include "aos/common/libc/dirname.h"
 
 extern "C" {
 #include "stm32flash/parsers/parser.h"
@@ -51,9 +52,9 @@
 
   serial_baud_t baud_rate = SERIAL_BAUD_57600;
 
-  ::std::string filename =
-      ::std::string(dirname(strdup(argv[0]))) +
-      "/../../../bbb_cape/src/cape/.obj/" + target + ".hex";
+  ::std::string filename = ::aos::libc::Dirname(argv[0]) +
+                           "/../../../bbb_cape/src/cape/.obj/" + target +
+                           ".hex";
 
   int file = open(filename.c_str(), O_RDONLY);
   if (file == -1) {