9
9
#include " FifoFiles.h"
10
10
#include " JSONUtils.h"
11
11
12
- #if !defined(_WIN32)
12
+ #include " llvm/Support/FileSystem.h"
13
+
14
+ #if defined(_WIN32)
15
+ #include < Windows.h>
16
+ #include < fcntl.h>
17
+ #include < io.h>
18
+ #else
13
19
#include < sys/stat.h>
14
20
#include < sys/types.h>
15
21
#include < unistd.h>
@@ -24,26 +30,65 @@ using namespace llvm;
24
30
25
31
namespace lldb_dap {
26
32
27
- FifoFile::FifoFile (StringRef path) : m_path(path) {}
33
+ std::error_code EC;
28
34
35
+ FifoFile::FifoFile (StringRef path)
36
+ : m_path(path), m_file(fopen(path.data(), " r+" )) {
37
+ if (m_file == nullptr ) {
38
+ EC = std::error_code (errno, std::generic_category ());
39
+ llvm::errs () << " Failed to open fifo file: " << path << EC.message ()
40
+ << " \n " ;
41
+ return ;
42
+ }
43
+ if (setvbuf (m_file, NULL , _IONBF, 0 ))
44
+ llvm::errs () << " Error setting unbuffered mode on C FILE\n " ;
45
+ }
46
+ FifoFile::FifoFile (StringRef path, FILE *f) : m_path(path), m_file(f) {}
29
47
FifoFile::~FifoFile () {
48
+ fclose (m_file);
30
49
#if !defined(_WIN32)
31
50
unlink (m_path.c_str ());
32
51
#endif
52
+ // Unreferenced named pipes are deleted automatically on Win32
33
53
}
34
54
35
- Expected<std::shared_ptr<FifoFile>> CreateFifoFile (StringRef path) {
36
- #if defined(_WIN32)
37
- return createStringError (inconvertibleErrorCode (), " Unimplemented" );
55
+ // This probably belongs to llvm::sys::fs as another FSEntity type
56
+ std::error_code createNamedPipe (const Twine &Prefix, StringRef Suffix,
57
+ int &ResultFd,
58
+ SmallVectorImpl<char > &ResultPath) {
59
+ const char *Middle = Suffix.empty () ? " -%%%%%%" : " -%%%%%%." ;
60
+ auto EC = sys::fs::getPotentiallyUniqueFileName (
61
+ #ifdef _WIN32
62
+ " \\\\ .\\ pipe\\ LOCAL\\ "
63
+ #else
64
+ " /tmp/"
65
+ #endif
66
+ + Prefix + Middle + Suffix,
67
+ ResultPath);
68
+ if (EC)
69
+ return EC;
70
+ ResultPath.push_back (0 );
71
+ const char *path = ResultPath.data ();
72
+ #ifdef _WIN32
73
+ HANDLE h = ::CreateNamedPipeA (
74
+ path, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
75
+ PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1 , 1024 , 1024 , 0 , NULL );
76
+ if (h == INVALID_HANDLE_VALUE)
77
+ return std::error_code (::GetLastError (), std::system_category ());
78
+ ResultFd = _open_osfhandle ((intptr_t )h, _O_TEXT | _O_RDWR);
79
+ if (ResultFd == -1 )
80
+ return std::error_code (::GetLastError (), std::system_category ());
38
81
#else
39
- if (int err = mkfifo (path.data (), 0600 ))
40
- return createStringError (std::error_code (err, std::generic_category ()),
41
- " Couldn't create fifo file: %s" , path.data ());
42
- return std::make_shared<FifoFile>(path);
82
+ if (mkfifo (path, 0600 ) == -1 )
83
+ return std::error_code (errno, std::generic_category ());
84
+ EC = openFileForWrite (ResultPath, ResultFd, sys::fs::CD_OpenExisting, sys::fs::OF_None, 0600 );
85
+ if (EC)
86
+ return EC;
43
87
#endif
88
+ return std::error_code ();
44
89
}
45
90
46
- FifoFileIO::FifoFileIO (StringRef fifo_file, StringRef other_endpoint_name)
91
+ FifoFileIO::FifoFileIO (FifoFile fifo_file, StringRef other_endpoint_name)
47
92
: m_fifo_file(fifo_file), m_other_endpoint_name(other_endpoint_name) {}
48
93
49
94
Expected<json::Value> FifoFileIO::ReadJSON (std::chrono::milliseconds timeout) {
@@ -52,13 +97,20 @@ Expected<json::Value> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout) {
52
97
std::optional<std::string> line;
53
98
std::future<void > *future =
54
99
new std::future<void >(std::async (std::launch::async, [&]() {
55
- std::ifstream reader (m_fifo_file, std::ifstream::in);
56
- std::string buffer;
57
- std::getline (reader, buffer);
58
- if (!buffer.empty ())
59
- line = buffer;
100
+ rewind (m_fifo_file.m_file );
101
+ constexpr size_t buffer_size = 2048 ;
102
+ char buffer[buffer_size];
103
+ char *ptr = fgets (buffer, buffer_size, m_fifo_file.m_file );
104
+ if (ptr == nullptr || *ptr == 0 )
105
+ return ;
106
+ size_t len = strlen (buffer);
107
+ if (len <= 1 )
108
+ return ;
109
+ buffer[len - 1 ] = ' \0 ' ; // remove newline
110
+ line = buffer;
60
111
}));
61
- if (future->wait_for (timeout) == std::future_status::timeout || !line)
112
+
113
+ if (future->wait_for (timeout) == std::future_status::timeout)
62
114
// Indeed this is a leak, but it's intentional. "future" obj destructor
63
115
// will block on waiting for the worker thread to join. And the worker
64
116
// thread might be stuck in blocking I/O. Intentionally leaking the obj
@@ -69,6 +121,11 @@ Expected<json::Value> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout) {
69
121
return createStringError (inconvertibleErrorCode (),
70
122
" Timed out trying to get messages from the " +
71
123
m_other_endpoint_name);
124
+ if (!line) {
125
+ return createStringError (inconvertibleErrorCode (),
126
+ " Failed to get messages from the " +
127
+ m_other_endpoint_name);
128
+ }
72
129
delete future;
73
130
return json::parse (*line);
74
131
}
@@ -78,8 +135,12 @@ Error FifoFileIO::SendJSON(const json::Value &json,
78
135
bool done = false ;
79
136
std::future<void > *future =
80
137
new std::future<void >(std::async (std::launch::async, [&]() {
81
- std::ofstream writer (m_fifo_file, std::ofstream::out);
82
- writer << JSONToString (json) << std::endl;
138
+ rewind (m_fifo_file.m_file );
139
+ auto payload = JSONToString (json);
140
+ if (fputs (payload.c_str (), m_fifo_file.m_file ) == EOF ||
141
+ fputc (' \n ' , m_fifo_file.m_file ) == EOF) {
142
+ return ;
143
+ }
83
144
done = true ;
84
145
}));
85
146
if (future->wait_for (timeout) == std::future_status::timeout || !done) {
@@ -98,4 +159,17 @@ Error FifoFileIO::SendJSON(const json::Value &json,
98
159
return Error::success ();
99
160
}
100
161
162
+ Error FifoFileIO::WaitForPeer () {
163
+ #ifdef _WIN32
164
+ if (!::ConnectNamedPipe ((HANDLE)_get_osfhandle (fileno (m_fifo_file.m_file )),
165
+ NULL ) &&
166
+ GetLastError () != ERROR_PIPE_CONNECTED) {
167
+ return createStringError (
168
+ std::error_code (GetLastError (), std::system_category ()),
169
+ " Failed to connect to the " + m_other_endpoint_name);
170
+ }
171
+ #endif
172
+ return Error::success ();
173
+ }
174
+
101
175
} // namespace lldb_dap
0 commit comments