xref: /openbsd-src/gnu/llvm/lldb/tools/lldb-vscode/FifoFiles.cpp (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1be691f3bSpatrick //===-- FifoFiles.cpp -------------------------------------------*- C++ -*-===//
2be691f3bSpatrick //
3be691f3bSpatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4be691f3bSpatrick // See https://llvm.org/LICENSE.txt for license information.
5be691f3bSpatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6be691f3bSpatrick //
7be691f3bSpatrick //===----------------------------------------------------------------------===//
8be691f3bSpatrick 
9be691f3bSpatrick #include "FifoFiles.h"
10be691f3bSpatrick 
11be691f3bSpatrick #if !defined(_WIN32)
12be691f3bSpatrick #include <sys/stat.h>
13be691f3bSpatrick #include <sys/types.h>
14be691f3bSpatrick #include <unistd.h>
15be691f3bSpatrick #endif
16be691f3bSpatrick 
17be691f3bSpatrick #include <chrono>
18be691f3bSpatrick #include <fstream>
19be691f3bSpatrick #include <future>
20*f6aab3d8Srobert #include <optional>
21be691f3bSpatrick #include <thread>
22be691f3bSpatrick 
23be691f3bSpatrick #include "llvm/Support/FileSystem.h"
24be691f3bSpatrick 
25be691f3bSpatrick #include "lldb/lldb-defines.h"
26be691f3bSpatrick 
27be691f3bSpatrick using namespace llvm;
28be691f3bSpatrick 
29be691f3bSpatrick namespace lldb_vscode {
30be691f3bSpatrick 
FifoFile(StringRef path)31be691f3bSpatrick FifoFile::FifoFile(StringRef path) : m_path(path) {}
32be691f3bSpatrick 
~FifoFile()33be691f3bSpatrick FifoFile::~FifoFile() {
34be691f3bSpatrick #if !defined(_WIN32)
35be691f3bSpatrick   unlink(m_path.c_str());
36be691f3bSpatrick #endif
37be691f3bSpatrick }
38be691f3bSpatrick 
CreateFifoFile(StringRef path)39be691f3bSpatrick Expected<std::shared_ptr<FifoFile>> CreateFifoFile(StringRef path) {
40be691f3bSpatrick #if defined(_WIN32)
41be691f3bSpatrick   return createStringError(inconvertibleErrorCode(), "Unimplemented");
42be691f3bSpatrick #else
43be691f3bSpatrick   if (int err = mkfifo(path.data(), 0600))
44be691f3bSpatrick     return createStringError(std::error_code(err, std::generic_category()),
45be691f3bSpatrick                              "Couldn't create fifo file: %s", path.data());
46be691f3bSpatrick   return std::make_shared<FifoFile>(path);
47be691f3bSpatrick #endif
48be691f3bSpatrick }
49be691f3bSpatrick 
FifoFileIO(StringRef fifo_file,StringRef other_endpoint_name)50be691f3bSpatrick FifoFileIO::FifoFileIO(StringRef fifo_file, StringRef other_endpoint_name)
51be691f3bSpatrick     : m_fifo_file(fifo_file), m_other_endpoint_name(other_endpoint_name) {}
52be691f3bSpatrick 
ReadJSON(std::chrono::milliseconds timeout)53be691f3bSpatrick Expected<json::Value> FifoFileIO::ReadJSON(std::chrono::milliseconds timeout) {
54be691f3bSpatrick   // We use a pointer for this future, because otherwise its normal destructor
55be691f3bSpatrick   // would wait for the getline to end, rendering the timeout useless.
56*f6aab3d8Srobert   std::optional<std::string> line;
57be691f3bSpatrick   std::future<void> *future =
58be691f3bSpatrick       new std::future<void>(std::async(std::launch::async, [&]() {
59be691f3bSpatrick         std::ifstream reader(m_fifo_file, std::ifstream::in);
60be691f3bSpatrick         std::string buffer;
61be691f3bSpatrick         std::getline(reader, buffer);
62be691f3bSpatrick         if (!buffer.empty())
63be691f3bSpatrick           line = buffer;
64be691f3bSpatrick       }));
65*f6aab3d8Srobert   if (future->wait_for(timeout) == std::future_status::timeout || !line)
66*f6aab3d8Srobert     // Indeed this is a leak, but it's intentional. "future" obj destructor
67*f6aab3d8Srobert     //  will block on waiting for the worker thread to join. And the worker
68*f6aab3d8Srobert     //  thread might be stuck in blocking I/O. Intentionally leaking the  obj
69*f6aab3d8Srobert     //  as a hack to avoid blocking main thread, and adding annotation to
70*f6aab3d8Srobert     //  supress static code inspection warnings
71*f6aab3d8Srobert 
72*f6aab3d8Srobert     // coverity[leaked_storage]
73be691f3bSpatrick     return createStringError(inconvertibleErrorCode(),
74be691f3bSpatrick                              "Timed out trying to get messages from the " +
75be691f3bSpatrick                                  m_other_endpoint_name);
76be691f3bSpatrick   delete future;
77be691f3bSpatrick   return json::parse(*line);
78be691f3bSpatrick }
79be691f3bSpatrick 
SendJSON(const json::Value & json,std::chrono::milliseconds timeout)80be691f3bSpatrick Error FifoFileIO::SendJSON(const json::Value &json,
81be691f3bSpatrick                            std::chrono::milliseconds timeout) {
82be691f3bSpatrick   bool done = false;
83be691f3bSpatrick   std::future<void> *future =
84be691f3bSpatrick       new std::future<void>(std::async(std::launch::async, [&]() {
85be691f3bSpatrick         std::ofstream writer(m_fifo_file, std::ofstream::out);
86be691f3bSpatrick         writer << JSONToString(json) << std::endl;
87be691f3bSpatrick         done = true;
88be691f3bSpatrick       }));
89be691f3bSpatrick   if (future->wait_for(timeout) == std::future_status::timeout || !done) {
90*f6aab3d8Srobert     // Indeed this is a leak, but it's intentional. "future" obj destructor will
91*f6aab3d8Srobert     // block on waiting for the worker thread to join. And the worker thread
92*f6aab3d8Srobert     // might be stuck in blocking I/O. Intentionally leaking the  obj as a hack
93*f6aab3d8Srobert     // to avoid blocking main thread, and adding annotation to supress static
94*f6aab3d8Srobert     // code inspection warnings"
95*f6aab3d8Srobert 
96*f6aab3d8Srobert     // coverity[leaked_storage]
97be691f3bSpatrick     return createStringError(inconvertibleErrorCode(),
98be691f3bSpatrick                              "Timed out trying to send messages to the " +
99be691f3bSpatrick                                  m_other_endpoint_name);
100be691f3bSpatrick   }
101be691f3bSpatrick   delete future;
102be691f3bSpatrick   return Error::success();
103be691f3bSpatrick }
104be691f3bSpatrick 
105be691f3bSpatrick } // namespace lldb_vscode
106