Roll src/third_party/WebKit dbf9be3:8d6c3d5 (svn 202308:202312)
[chromium-blink-merge.git] / ipc / ipc_channel_posix.h
blobbe47705db0178f84ccdf7b7e5861c5a813b21c92
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef IPC_IPC_CHANNEL_POSIX_H_
6 #define IPC_IPC_CHANNEL_POSIX_H_
8 #include "ipc/ipc_channel.h"
10 #include <sys/socket.h> // for CMSG macros
12 #include <queue>
13 #include <set>
14 #include <string>
15 #include <vector>
17 #include "base/files/scoped_file.h"
18 #include "base/message_loop/message_loop.h"
19 #include "base/process/process.h"
20 #include "ipc/ipc_channel_reader.h"
21 #include "ipc/ipc_message_attachment_set.h"
23 namespace IPC {
25 class IPC_EXPORT ChannelPosix : public Channel,
26 public internal::ChannelReader,
27 public base::MessageLoopForIO::Watcher {
28 public:
29 // |broker| must outlive the newly created object.
30 ChannelPosix(const IPC::ChannelHandle& channel_handle,
31 Mode mode,
32 Listener* listener);
33 ~ChannelPosix() override;
35 // Channel implementation
36 bool Connect() override;
37 void Close() override;
38 bool Send(Message* message) override;
39 AttachmentBroker* GetAttachmentBroker() override;
40 base::ProcessId GetPeerPID() const override;
41 base::ProcessId GetSelfPID() const override;
42 int GetClientFileDescriptor() const override;
43 base::ScopedFD TakeClientFileDescriptor() override;
45 // Returns true if the channel supports listening for connections.
46 bool AcceptsConnections() const;
48 // Returns true if the channel supports listening for connections and is
49 // currently connected.
50 bool HasAcceptedConnection() const;
52 // Closes any currently connected socket, and returns to a listening state
53 // for more connections.
54 void ResetToAcceptingConnectionState();
56 // Returns true if the peer process' effective user id can be determined, in
57 // which case the supplied peer_euid is updated with it.
58 bool GetPeerEuid(uid_t* peer_euid) const;
60 void CloseClientFileDescriptor();
62 static bool IsNamedServerInitialized(const std::string& channel_id);
63 #if defined(OS_LINUX)
64 static void SetGlobalPid(int pid);
65 #endif // OS_LINUX
67 private:
68 bool CreatePipe(const IPC::ChannelHandle& channel_handle);
70 bool ProcessOutgoingMessages();
72 bool AcceptConnection();
73 void ClosePipeOnError();
74 int GetHelloMessageProcId() const;
75 void QueueHelloMessage();
76 void CloseFileDescriptors(Message* msg);
77 void QueueCloseFDMessage(int fd, int hops);
79 // ChannelReader implementation.
80 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override;
81 bool ShouldDispatchInputMessage(Message* msg) override;
82 bool GetNonBrokeredAttachments(Message* msg) override;
83 bool DidEmptyInputBuffers() override;
84 void HandleInternalMessage(const Message& msg) override;
85 base::ProcessId GetSenderPID() override;
86 bool IsAttachmentBrokerEndpoint() override;
88 // Finds the set of file descriptors in the given message. On success,
89 // appends the descriptors to the input_fds_ member and returns true
91 // Returns false if the message was truncated. In this case, any handles that
92 // were sent will be closed.
93 bool ExtractFileDescriptorsFromMsghdr(msghdr* msg);
95 // Closes all handles in the input_fds_ list and clears the list. This is
96 // used to clean up handles in error conditions to avoid leaking the handles.
97 void ClearInputFDs();
99 // MessageLoopForIO::Watcher implementation.
100 void OnFileCanReadWithoutBlocking(int fd) override;
101 void OnFileCanWriteWithoutBlocking(int fd) override;
103 Mode mode_;
105 base::ProcessId peer_pid_;
107 // After accepting one client connection on our server socket we want to
108 // stop listening.
109 base::MessageLoopForIO::FileDescriptorWatcher
110 server_listen_connection_watcher_;
111 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_;
112 base::MessageLoopForIO::FileDescriptorWatcher write_watcher_;
114 // Indicates whether we're currently blocked waiting for a write to complete.
115 bool is_blocked_on_write_;
116 bool waiting_connect_;
118 // If sending a message blocks then we use this variable
119 // to keep track of where we are.
120 size_t message_send_bytes_written_;
122 // File descriptor we're listening on for new connections if we listen
123 // for connections.
124 base::ScopedFD server_listen_pipe_;
126 // The pipe used for communication.
127 base::ScopedFD pipe_;
129 // For a server, the client end of our socketpair() -- the other end of our
130 // pipe_ that is passed to the client.
131 base::ScopedFD client_pipe_;
132 mutable base::Lock client_pipe_lock_; // Lock that protects |client_pipe_|.
134 // The "name" of our pipe. On Windows this is the global identifier for
135 // the pipe. On POSIX it's used as a key in a local map of file descriptors.
136 std::string pipe_name_;
138 // Messages to be sent are queued here.
139 std::queue<Message*> output_queue_;
141 // We assume a worst case: kReadBufferSize bytes of messages, where each
142 // message has no payload and a full complement of descriptors.
143 static const size_t kMaxReadFDs =
144 (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) *
145 MessageAttachmentSet::kMaxDescriptorsPerMessage;
147 // Buffer size for file descriptors used for recvmsg. On Mac the CMSG macros
148 // are not constant so we have to pick a "large enough" padding for headers.
149 #if defined(OS_MACOSX)
150 static const size_t kMaxReadFDBuffer = 1024 + sizeof(int) * kMaxReadFDs;
151 #else
152 static const size_t kMaxReadFDBuffer = CMSG_SPACE(sizeof(int) * kMaxReadFDs);
153 #endif
154 static_assert(kMaxReadFDBuffer <= 8192,
155 "kMaxReadFDBuffer too big for a stack buffer");
157 // File descriptors extracted from messages coming off of the channel. The
158 // handles may span messages and come off different channels from the message
159 // data (in the case of READWRITE), and are processed in FIFO here.
160 // NOTE: The implementation assumes underlying storage here is contiguous, so
161 // don't change to something like std::deque<> without changing the
162 // implementation!
163 std::vector<int> input_fds_;
166 void ResetSafely(base::ScopedFD* fd);
167 bool in_dtor_;
169 #if defined(OS_MACOSX)
170 // On OSX, sent FDs must not be closed until we get an ack.
171 // Keep track of sent FDs here to make sure the remote is not
172 // trying to bamboozle us.
173 std::set<int> fds_to_close_;
174 #endif
176 // True if we are responsible for unlinking the unix domain socket file.
177 bool must_unlink_;
179 #if defined(OS_LINUX)
180 // If non-zero, overrides the process ID sent in the hello message.
181 static int global_pid_;
182 #endif // OS_LINUX
184 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelPosix);
187 } // namespace IPC
189 #endif // IPC_IPC_CHANNEL_POSIX_H_