LLDB mainline
DomainSocketWindows.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "llvm/ADT/ScopeExit.h"
12#include "llvm/Support/Error.h"
13#include "llvm/Support/FileSystem.h"
14#include "llvm/Support/Path.h"
15
16#include <chrono>
17#include <memory>
18
19using namespace lldb;
20using namespace lldb_private;
21
22llvm::Expected<DomainSocket::Pair> DomainSocketWindows::CreatePair() {
23 // Windows has no socketpair(). Emulate it the same way TCPSocket::CreatePair
24 // does for loopback TCP: bind a listener to a unique temporary path, connect
25 // a client to it, and accept. AF_UNIX SOCK_STREAM connect() completes once
26 // the connection is queued (backlog >= 1), so a single thread can connect
27 // and then accept without deadlocking.
28 llvm::SmallString<128> model;
29 llvm::sys::path::system_temp_directory(/*erasedOnReboot=*/true, model);
30 llvm::sys::path::append(model, "lldb-domain-socketpair-%%%%%%%%.sock");
31 llvm::SmallString<128> path;
32 llvm::sys::fs::createUniquePath(model, path, /*MakeAbsolute=*/false);
33 auto remove_file =
34 llvm::make_scope_exit([&] { llvm::sys::fs::remove(path); });
35
36 auto listen_socket =
37 std::make_unique<DomainSocketWindows>(/*should_close=*/true);
38 if (Status error = listen_socket->Listen(path, /*backlog=*/1); error.Fail())
39 return error.takeError();
40
41 auto connect_socket =
42 std::make_unique<DomainSocketWindows>(/*should_close=*/true);
43 if (Status error = connect_socket->Connect(path); error.Fail())
44 return error.takeError();
45
46 // The connection is already queued, so a short timeout is sufficient.
47 Socket *accept_socket = nullptr;
48 if (Status error =
49 listen_socket->Accept(std::chrono::seconds(1), accept_socket);
50 error.Fail())
51 return error.takeError();
52
53 return Pair(std::move(connect_socket),
54 std::unique_ptr<DomainSocket>(
55 static_cast<DomainSocket *>(accept_socket)));
56}
static llvm::raw_ostream & error(Stream &strm)
static llvm::Expected< Pair > CreatePair()
Create a connected pair of domain sockets.
DomainSocket(NativeSocket socket, bool should_close)
std::pair< std::unique_ptr< DomainSocket >, std::unique_ptr< DomainSocket > > Pair
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.