cmake: compat
[m6w6/libmemcached] / libtest / cmdline.cc
index 0553fddebb2172f229e4d5e96fb0066d69a0688e..375b00a156a837cf3ae95cff393dcfa024b0613e 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Data Differential YATL (i.e. libtest)  library
  *
- *  Copyright (C) 2012 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2012-2013 Data Differential, http://datadifferential.com/
  *
  *  Redistribution and use in source and binary forms, with or without
  *  modification, are permitted provided that the following conditions are
@@ -59,6 +59,7 @@ using namespace libtest;
 #include <unistd.h>
 
 #include <algorithm>
+#include <stdexcept>
 
 #ifndef __USE_GNU
 static char **environ= NULL;
@@ -78,7 +79,10 @@ namespace {
          iter != built_argv.end();
          ++iter)
     {
-      arg_buffer << *iter << " ";
+      if (*iter)
+      {
+        arg_buffer << *iter << " ";
+      }
     }
 
     return arg_buffer.str();
@@ -138,7 +142,7 @@ Application::Application(const std::string& arg, const bool _use_libtool_arg) :
     {
       if (libtool() == NULL)
       {
-        FATAL("libtool requested, but know libtool was found");
+        FATAL("libtool requested, but no libtool was found");
       }
     }
 
@@ -262,7 +266,7 @@ Application::error_t Application::run(const char *args[])
   }
   else
   {
-    spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], NULL);
+    spawn_ret= posix_spawn(&_pid, built_argv[0], &file_actions, &spawnattr, &built_argv[0], environ);
   }
 
   posix_spawn_file_actions_destroy(&file_actions);
@@ -276,7 +280,16 @@ Application::error_t Application::run(const char *args[])
   {
     if (_will_fail == false)
     {
-      Error << strerror(spawn_ret) << "(" << spawn_ret << ")";
+      std::string sb;
+      char buf[1024];
+
+      std::for_each(built_argv.begin(), built_argv.end()-1, [&sb](const char *a) {
+        if (sb.size()) {
+          sb.append(" ");
+        }
+        sb.append(a);
+      });
+      Error << strerror(spawn_ret) << "(" << spawn_ret << "): " << sb << " cwd:" << getcwd(buf, sizeof(buf)-1);
     }
     _pid= -1;
     return Application::INVALID_POSIX_SPAWN;
@@ -358,7 +371,7 @@ bool Application::slurp()
     int error;
     switch ((error= errno))
     {
-#ifdef TARGET_OS_LINUX
+#ifdef __linux
     case ERESTART:
 #endif
     case EINTR:
@@ -406,9 +419,25 @@ bool Application::slurp()
   return data_was_read;
 }
 
+std::pair<std::string, std::string> Application::output()
+{
+  slurp();
+  return {
+      std::string {
+          stdout_result().data(),
+          stdout_result().size()
+      },
+      std::string {
+          stderr_result().data(),
+          stderr_result().size()
+      }
+  };
+
+}
+
 Application::error_t Application::join()
 {
-  pid_t waited_pid= waitpid(_pid, &_status, 0);
+  pid_t waited_pid= waitpid(_pid, &_status, WUNTRACED);
   slurp();
   if (waited_pid == _pid and WIFEXITED(_status) == false)
   {
@@ -481,8 +510,20 @@ Application::error_t Application::join()
   }
   else if (waited_pid == -1)
   {
+    std::string error_string;
+    if (stdout_result_length())
+    {
+      error_string+= " stdout: ";
+      error_string+= stdout_c_str();
+    }
+
+    if (stderr_result_length())
+    {
+      error_string+= " stderr: ";
+      error_string+= stderr_c_str();
+    }
+    Error << "waitpid() returned errno:" << strerror(errno) << " " << error_string;
     _app_exit_state= Application::UNKNOWN;
-    Error << "waitpid() returned errno:" << strerror(errno);
   }
   else
   {
@@ -771,7 +812,7 @@ void Application::create_argv(const char *args[])
       vchar::append(built_argv, *ptr);
     }
   }
-  built_argv.push_back(NULL);
+  built_argv.push_back(nullptr);
 }
 
 std::string Application::print()
@@ -810,22 +851,18 @@ int exec_cmdline(const std::string& command, const char *args[], bool use_libtoo
 
   Application::error_t ret= app.run(args);
 
+  if (Application::SUCCESS == ret) {
+    ret = app.join();
+  }
+
   if (ret != Application::SUCCESS)
   {
-    return int(ret);
+    auto out = app.output();
+    Error << command << " stdout: " << out.first;
+    Error << command << " stderr: " << out.second;
   }
 
-  return int(app.join());
-}
-
-const char *gearmand_binary() 
-{
-  return GEARMAND_BINARY;
-}
-
-const char *drizzled_binary() 
-{
-  return DRIZZLED_BINARY;
+  return int(ret);
 }
 
 } // namespace exec_cmdline