Android-cuttlefish cvd tool
unix_sockets.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <sys/socket.h>
19
20#include <cstdint>
21#include <vector>
22
25
26namespace cuttlefish {
27
29 public:
30 static ControlMessage FromRaw(const cmsghdr*);
32 const std::vector<SharedFD>&);
33#ifdef __linux__
34 static ControlMessage FromCredentials(const ucred&);
35#endif
41
42 const cmsghdr* Raw() const;
43
44#ifdef __linux__
45 bool IsCredentials() const;
46 Result<ucred> AsCredentials() const;
47#endif
48
49 bool IsFileDescriptors() const;
51
52 private:
53 friend class UnixMessageSocket;
54 ControlMessage() = default;
55 cmsghdr* Raw();
56
57 std::vector<char> data_;
58 std::vector<int> fds_;
59};
60
62 std::vector<char> data;
63 std::vector<ControlMessage> control;
64
65 bool HasFileDescriptors();
67#ifdef __linux__
68 bool HasCredentials();
69 Result<ucred> Credentials();
70#endif
71};
72
74 public:
76 [[nodiscard]] Result<void> WriteMessage(const UnixSocketMessage&);
78
79#ifdef __linux__
80 [[nodiscard]] Result<void> EnableCredentials(bool);
81#endif
82
83 private:
85 std::uint32_t max_message_size_;
86};
87
88} // namespace cuttlefish
Definition: expected.h:86
Definition: shared_fd.h:129
Definition: unix_sockets.h:73
Result< void > WriteMessage(const UnixSocketMessage &)
Definition: unix_sockets.cpp:223
UnixMessageSocket(SharedFD)
Definition: unix_sockets.cpp:205
Result< UnixSocketMessage > ReadMessage()
Definition: unix_sockets.cpp:258
std::uint32_t max_message_size_
Definition: unix_sockets.h:85
SharedFD socket_
Definition: unix_sockets.h:84
Definition: alloc_utils.cpp:23
Definition: unix_sockets.h:28
ControlMessage(const ControlMessage &)=delete
Result< std::vector< SharedFD > > AsSharedFDs() const
Definition: unix_sockets.cpp:143
std::vector< int > fds_
Definition: unix_sockets.h:58
ControlMessage & operator=(const ControlMessage &)=delete
static Result< ControlMessage > FromFileDescriptors(const std::vector< SharedFD > &)
Definition: unix_sockets.cpp:55
bool IsFileDescriptors() const
Definition: unix_sockets.cpp:137
static ControlMessage FromRaw(const cmsghdr *)
Definition: unix_sockets.cpp:38
const cmsghdr * Raw() const
Definition: unix_sockets.cpp:117
~ControlMessage()
Definition: unix_sockets.cpp:104
std::vector< char > data_
Definition: unix_sockets.h:57
Definition: unix_sockets.h:61
std::vector< char > data
Definition: unix_sockets.h:62
Result< std::vector< SharedFD > > FileDescriptors()
Definition: unix_sockets.cpp:167
bool HasFileDescriptors()
Definition: unix_sockets.cpp:159
std::vector< ControlMessage > control
Definition: unix_sockets.h:63