[Lldb-commits] [lldb] r228307 - Fix warning about the use of mktemp and make platform agnostic by adding and using PipeBase::CreateWithUniqueName - on behalf of flackr.

Oleksiy Vyalov ovyalov at google.com
Thu Feb 5 08:29:12 PST 2015


Author: ovyalov
Date: Thu Feb  5 10:29:12 2015
New Revision: 228307

URL: http://llvm.org/viewvc/llvm-project?rev=228307&view=rev
Log:
Fix warning about the use of mktemp and make platform agnostic by adding and using PipeBase::CreateWithUniqueName - on behalf of flackr.

http://reviews.llvm.org/D7348


Modified:
    lldb/trunk/include/lldb/Host/PipeBase.h
    lldb/trunk/include/lldb/Host/posix/PipePosix.h
    lldb/trunk/include/lldb/Host/windows/PipeWindows.h
    lldb/trunk/source/Host/posix/PipePosix.cpp
    lldb/trunk/source/Host/windows/PipeWindows.cpp
    lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp

Modified: lldb/trunk/include/lldb/Host/PipeBase.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/PipeBase.h?rev=228307&r1=228306&r2=228307&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/PipeBase.h (original)
+++ lldb/trunk/include/lldb/Host/PipeBase.h Thu Feb  5 10:29:12 2015
@@ -14,6 +14,7 @@
 #include <string>
 
 #include "lldb/Core/Error.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 
 namespace lldb_private
@@ -25,6 +26,7 @@ class PipeBase
 
     virtual Error CreateNew(bool child_process_inherit) = 0;
     virtual Error CreateNew(llvm::StringRef name, bool child_process_inherit) = 0;
+    virtual Error CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl<char>& name) = 0;
 
     virtual Error OpenAsReader(llvm::StringRef name, bool child_process_inherit) = 0;
 

Modified: lldb/trunk/include/lldb/Host/posix/PipePosix.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/posix/PipePosix.h?rev=228307&r1=228306&r2=228307&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/posix/PipePosix.h (original)
+++ lldb/trunk/include/lldb/Host/posix/PipePosix.h Thu Feb  5 10:29:12 2015
@@ -36,6 +36,8 @@ public:
     Error
     CreateNew(llvm::StringRef name, bool child_process_inherit) override;
     Error
+    CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl<char>& name) override;
+    Error
     OpenAsReader(llvm::StringRef name, bool child_process_inherit) override;
     Error
     OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) override;

Modified: lldb/trunk/include/lldb/Host/windows/PipeWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/windows/PipeWindows.h?rev=228307&r1=228306&r2=228307&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/windows/PipeWindows.h (original)
+++ lldb/trunk/include/lldb/Host/windows/PipeWindows.h Thu Feb  5 10:29:12 2015
@@ -31,6 +31,7 @@ class PipeWindows : public PipeBase
 
     Error CreateNew(bool child_process_inherit) override;
     Error CreateNew(llvm::StringRef name, bool child_process_inherit) override;
+    Error CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl<char>& name) override;
     Error OpenAsReader(llvm::StringRef name, bool child_process_inherit) override;
     Error OpenAsWriterWithTimeout(llvm::StringRef name, bool child_process_inherit, const std::chrono::microseconds &timeout) override;
 

Modified: lldb/trunk/source/Host/posix/PipePosix.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/posix/PipePosix.cpp?rev=228307&r1=228306&r2=228307&view=diff
==============================================================================
--- lldb/trunk/source/Host/posix/PipePosix.cpp (original)
+++ lldb/trunk/source/Host/posix/PipePosix.cpp Thu Feb  5 10:29:12 2015
@@ -9,12 +9,16 @@
 
 #include "lldb/Host/posix/PipePosix.h"
 #include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/FileSystem.h"
 
 #include <functional>
 #include <thread>
 
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -182,6 +186,37 @@ PipePosix::CreateNew(llvm::StringRef nam
     return error;
 }
 
+Error
+PipePosix::CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl<char>& name)
+{
+    llvm::SmallString<PATH_MAX> named_pipe_path;
+    llvm::SmallString<PATH_MAX> pipe_spec((prefix + ".%%%%%%").str());
+    FileSpec tmpdir_file_spec;
+    tmpdir_file_spec.Clear();
+    if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
+    {
+        tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str());
+    }
+    else
+    {
+        tmpdir_file_spec.AppendPathComponent("/tmp");
+        tmpdir_file_spec.AppendPathComponent(pipe_spec.c_str());
+    }
+
+    // It's possible that another process creates the target path after we've
+    // verified it's available but before we create it, in which case we
+    // should try again.
+    Error error;
+    do {
+        llvm::sys::fs::createUniqueFile(tmpdir_file_spec.GetPath().c_str(), named_pipe_path);
+        error = CreateNew(named_pipe_path, child_process_inherit);
+    } while (error.GetError() == EEXIST);
+
+    if (error.Success())
+        name = named_pipe_path;
+    return error;
+}
+
 Error
 PipePosix::OpenAsReader(llvm::StringRef name, bool child_process_inherit)
 {

Modified: lldb/trunk/source/Host/windows/PipeWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/PipeWindows.cpp?rev=228307&r1=228306&r2=228307&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/PipeWindows.cpp (original)
+++ lldb/trunk/source/Host/windows/PipeWindows.cpp Thu Feb  5 10:29:12 2015
@@ -9,6 +9,8 @@
 
 #include "lldb/Host/windows/PipeWindows.h"
 
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/raw_ostream.h"
 
 #include <fcntl.h>
@@ -90,6 +92,24 @@ PipeWindows::CreateNew(llvm::StringRef n
 }
 
 Error
+PipeWindows::CreateWithUniqueName(llvm::StringRef prefix, bool child_process_inherit, llvm::SmallVectorImpl<char>& name)
+{
+    llvm::SmallString<128> pipe_name;
+    Error error;
+    do {
+        pipe_name = prefix;
+        pipe_name += "-";
+        for (unsigned i = 0; i < 6; i++) {
+            pipe_name += "0123456789abcdef"[llvm::sys::Process::GetRandomNumber() & 15];
+        }
+        Error error = CreateNew(pipe_name, child_process_inherit);
+    } while (error.GetError() == ERROR_ALREADY_EXISTS);
+    if (error.Success())
+        name = pipe_name;
+    return error;
+}
+
+Error
 PipeWindows::OpenAsReader(llvm::StringRef name, bool child_process_inherit)
 {
     if (CanRead() || CanWrite())

Modified: lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp?rev=228307&r1=228306&r2=228307&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp (original)
+++ lldb/trunk/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp Thu Feb  5 10:29:12 2015
@@ -30,6 +30,7 @@
 #include "lldb/Host/ThreadLauncher.h"
 #include "lldb/Host/TimeValue.h"
 #include "lldb/Target/Process.h"
+#include "llvm/ADT/SmallString.h"
 
 // Project includes
 #include "ProcessGDBRemoteLog.h"
@@ -764,8 +765,7 @@ GDBRemoteCommunication::StartDebugserver
             debugserver_args.AppendArgument("--setsid");
         }
 
-        char named_pipe_path[PATH_MAX];
-        named_pipe_path[0] = '\0';
+        llvm::SmallString<PATH_MAX> named_pipe_path;
         Pipe port_named_pipe;
 
         bool listen = false;
@@ -779,25 +779,11 @@ GDBRemoteCommunication::StartDebugserver
             {
                 // Binding to port zero, we need to figure out what port it ends up
                 // using using a named pipe...
-                FileSpec tmpdir_file_spec;
-                if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
-                {
-                    tmpdir_file_spec.AppendPathComponent("debugserver-named-pipe.XXXXXX");
-                    strncpy(named_pipe_path, tmpdir_file_spec.GetPath().c_str(), sizeof(named_pipe_path));
-                }
-                else
-                {
-                    strncpy(named_pipe_path, "/tmp/debugserver-named-pipe.XXXXXX", sizeof(named_pipe_path));
-                }
-
-                if (::mktemp (named_pipe_path))
-                {
-                    error = port_named_pipe.CreateNew(named_pipe_path, false);
-                    if (error.Fail())
-                        return error;
-                    debugserver_args.AppendArgument("--named-pipe");
-                    debugserver_args.AppendArgument(named_pipe_path);
-                }
+                error = port_named_pipe.CreateWithUniqueName("debugserver-named-pipe", false, named_pipe_path);
+                if (error.Fail())
+                    return error;
+                debugserver_args.AppendArgument("--named-pipe");
+                debugserver_args.AppendArgument(named_pipe_path.c_str());
             }
             else
             {
@@ -877,7 +863,7 @@ GDBRemoteCommunication::StartDebugserver
         
         if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
         {
-            if (named_pipe_path[0])
+            if (named_pipe_path.size() > 0)
             {
                 error = port_named_pipe.OpenAsReader(named_pipe_path, false);
                 if (error.Success())
@@ -897,7 +883,7 @@ GDBRemoteCommunication::StartDebugserver
                     else
                     {
                         if (log)
-                            log->Printf("GDBRemoteCommunication::%s() failed to read a port value from named pipe %s: %s", __FUNCTION__, named_pipe_path, error.AsCString());
+                            log->Printf("GDBRemoteCommunication::%s() failed to read a port value from named pipe %s: %s", __FUNCTION__, named_pipe_path.c_str(), error.AsCString());
 
                     }
                     port_named_pipe.Close();
@@ -905,13 +891,13 @@ GDBRemoteCommunication::StartDebugserver
                 else
                 {
                     if (log)
-                        log->Printf("GDBRemoteCommunication::%s() failed to open named pipe %s for reading: %s", __FUNCTION__, named_pipe_path, error.AsCString());
+                        log->Printf("GDBRemoteCommunication::%s() failed to open named pipe %s for reading: %s", __FUNCTION__, named_pipe_path.c_str(), error.AsCString());
                 }
                 const auto err = port_named_pipe.Delete(named_pipe_path);
                 if (err.Fail())
                 {
                     if (log)
-                        log->Printf ("GDBRemoteCommunication::%s failed to delete pipe %s: %s", __FUNCTION__, named_pipe_path, err.AsCString());
+                        log->Printf ("GDBRemoteCommunication::%s failed to delete pipe %s: %s", __FUNCTION__, named_pipe_path.c_str(), err.AsCString());
                 }
             }
             else if (listen)





More information about the lldb-commits mailing list