8
8
9
9
#include " FifoFiles.h"
10
10
#include " JSONUtils.h"
11
+ #include " llvm/Support/FileSystem.h"
12
+ #include " llvm/Support/WindowsError.h"
11
13
12
- #if !defined(_WIN32)
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,41 +30,95 @@ using namespace llvm;
24
30
25
31
namespace lldb_dap {
26
32
27
- FifoFile::FifoFile (StringRef path) : m_path(path) {}
28
-
33
+ FifoFile::FifoFile (StringRef path)
34
+ : m_path(path), m_file(fopen(path.data(), " r+" )) {
35
+ std::error_code EC;
36
+ if (m_file == nullptr ) {
37
+ EC = std::error_code (errno, std::generic_category ());
38
+ llvm::errs () << " Failed to open fifo file " << path << " : " << EC.message ()
39
+ << " \n " ;
40
+ std::terminate ();
41
+ }
42
+ if (setvbuf (m_file, NULL , _IONBF, 0 ))
43
+ llvm::errs () << " Error setting unbuffered mode on C FILE\n " ;
44
+ }
45
+ FifoFile::FifoFile (StringRef path, FILE *f) : m_path(path), m_file(f) {}
46
+ FifoFile::FifoFile (FifoFile &&other)
47
+ : m_path(other.m_path), m_file(other.m_file) {
48
+ other.m_path .clear ();
49
+ other.m_file = nullptr ;
50
+ }
29
51
FifoFile::~FifoFile () {
52
+ if (m_file)
53
+ fclose (m_file);
30
54
#if !defined(_WIN32)
55
+ // Unreferenced named pipes are deleted automatically on Win32
31
56
unlink (m_path.c_str ());
32
57
#endif
33
58
}
34
59
35
- Expected<std::shared_ptr<FifoFile>> CreateFifoFile (StringRef path) {
36
- #if defined(_WIN32)
37
- return createStringError (inconvertibleErrorCode (), " Unimplemented" );
60
+ // This probably belongs to llvm::sys::fs as another FSEntity type
61
+ std::error_code createUniqueNamedPipe (const Twine &prefix, StringRef suffix,
62
+ int &result_fd,
63
+ SmallVectorImpl<char > &result_path) {
64
+ const char *middle = suffix.empty () ? " -%%%%%%" : " -%%%%%%." ;
65
+ auto EC = sys::fs::getPotentiallyUniqueFileName (
66
+ #ifdef _WIN32
67
+ " \\\\ .\\ pipe\\ LOCAL\\ "
68
+ #else
69
+ " /tmp/"
70
+ #endif
71
+ + prefix + middle + suffix,
72
+ result_path);
73
+ if (EC)
74
+ return EC;
75
+ result_path.push_back (0 );
76
+ const char *path = result_path.data ();
77
+ #ifdef _WIN32
78
+ HANDLE h = ::CreateNamedPipeA (
79
+ path, PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
80
+ PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT, 1 , 1024 , 1024 , 0 , NULL );
81
+ if (h == INVALID_HANDLE_VALUE)
82
+ return llvm::mapLastWindowsError ();
83
+ result_fd = _open_osfhandle ((intptr_t )h, _O_TEXT | _O_RDWR);
84
+ if (result_fd == -1 )
85
+ return llvm::mapLastWindowsError ();
38
86
#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);
87
+ if (mkfifo (path, 0600 ) == -1 )
88
+ return std::error_code (errno, std::generic_category ());
89
+ EC = openFileForWrite (result_path, result_fd, sys::fs::CD_OpenExisting,
90
+ sys::fs::OF_None, 0600 );
91
+ if (EC)
92
+ return EC;
43
93
#endif
94
+ result_path.pop_back ();
95
+ return std::error_code ();
44
96
}
45
97
46
- FifoFileIO::FifoFileIO (StringRef fifo_file, StringRef other_endpoint_name)
47
- : m_fifo_file(fifo_file), m_other_endpoint_name(other_endpoint_name) {}
98
+ FifoFileIO::FifoFileIO (FifoFile &&fifo_file, StringRef other_endpoint_name)
99
+ : m_fifo_file(std::move(fifo_file)),
100
+ m_other_endpoint_name (other_endpoint_name) {}
48
101
49
102
Expected<json::Value> FifoFileIO::ReadJSON (std::chrono::milliseconds timeout) {
50
103
// We use a pointer for this future, because otherwise its normal destructor
51
104
// would wait for the getline to end, rendering the timeout useless.
52
105
std::optional<std::string> line;
53
106
std::future<void > *future =
54
107
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;
108
+ rewind (m_fifo_file.m_file );
109
+ constexpr size_t buffer_size = 2048 ;
110
+ char buffer[buffer_size];
111
+ char *ptr = fgets (buffer, buffer_size, m_fifo_file.m_file );
112
+ if (ptr == nullptr || *ptr == 0 )
113
+ return ;
114
+ size_t len = strlen (buffer);
115
+ if (len <= 1 )
116
+ return ;
117
+ buffer[len - 1 ] = ' \0 ' ; // remove newline
118
+ line = buffer;
60
119
}));
61
- if (future->wait_for (timeout) == std::future_status::timeout || !line)
120
+
121
+ if (future->wait_for (timeout) == std::future_status::timeout)
62
122
// Indeed this is a leak, but it's intentional. "future" obj destructor
63
123
// will block on waiting for the worker thread to join. And the worker
64
124
// thread might be stuck in blocking I/O. Intentionally leaking the obj
@@ -69,6 +129,11 @@ Expected<json::Value> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout) {
69
129
return createStringError (inconvertibleErrorCode (),
70
130
" Timed out trying to get messages from the " +
71
131
m_other_endpoint_name);
132
+ if (!line) {
133
+ return createStringError (inconvertibleErrorCode (),
134
+ " Failed to get messages from the " +
135
+ m_other_endpoint_name);
136
+ }
72
137
delete future;
73
138
return json::parse (*line);
74
139
}
@@ -78,8 +143,12 @@ Error FifoFileIO::SendJSON(const json::Value &json,
78
143
bool done = false ;
79
144
std::future<void > *future =
80
145
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;
146
+ rewind (m_fifo_file.m_file );
147
+ auto payload = JSONToString (json);
148
+ if (fputs (payload.c_str (), m_fifo_file.m_file ) == EOF ||
149
+ fputc (' \n ' , m_fifo_file.m_file ) == EOF) {
150
+ return ;
151
+ }
83
152
done = true ;
84
153
}));
85
154
if (future->wait_for (timeout) == std::future_status::timeout || !done) {
@@ -98,4 +167,17 @@ Error FifoFileIO::SendJSON(const json::Value &json,
98
167
return Error::success ();
99
168
}
100
169
170
+ Error FifoFileIO::WaitForPeer () {
171
+ #ifdef _WIN32
172
+ if (!::ConnectNamedPipe ((HANDLE)_get_osfhandle (fileno (m_fifo_file.m_file )),
173
+ NULL ) &&
174
+ GetLastError () != ERROR_PIPE_CONNECTED) {
175
+ return createStringError (llvm::mapLastWindowsError (),
176
+ " Failed to connect to the " +
177
+ m_other_endpoint_name);
178
+ }
179
+ #endif
180
+ return Error::success ();
181
+ }
182
+
101
183
} // namespace lldb_dap
0 commit comments