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
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"
25 class IPC_EXPORT ChannelPosix
: public Channel
,
26 public internal::ChannelReader
,
27 public base::MessageLoopForIO::Watcher
{
29 // |broker| must outlive the newly created object.
30 ChannelPosix(const IPC::ChannelHandle
& channel_handle
,
33 AttachmentBroker
* broker
);
34 ~ChannelPosix() override
;
36 // Channel implementation
37 bool Connect() override
;
38 void Close() override
;
39 bool Send(Message
* message
) override
;
40 AttachmentBroker
* GetAttachmentBroker() override
;
41 base::ProcessId
GetPeerPID() const override
;
42 base::ProcessId
GetSelfPID() const override
;
43 int GetClientFileDescriptor() const override
;
44 base::ScopedFD
TakeClientFileDescriptor() override
;
46 // Returns true if the channel supports listening for connections.
47 bool AcceptsConnections() const;
49 // Returns true if the channel supports listening for connections and is
50 // currently connected.
51 bool HasAcceptedConnection() const;
53 // Closes any currently connected socket, and returns to a listening state
54 // for more connections.
55 void ResetToAcceptingConnectionState();
57 // Returns true if the peer process' effective user id can be determined, in
58 // which case the supplied peer_euid is updated with it.
59 bool GetPeerEuid(uid_t
* peer_euid
) const;
61 void CloseClientFileDescriptor();
63 static bool IsNamedServerInitialized(const std::string
& channel_id
);
65 static void SetGlobalPid(int pid
);
69 bool CreatePipe(const IPC::ChannelHandle
& channel_handle
);
71 bool ProcessOutgoingMessages();
73 bool AcceptConnection();
74 void ClosePipeOnError();
75 int GetHelloMessageProcId() const;
76 void QueueHelloMessage();
77 void CloseFileDescriptors(Message
* msg
);
78 void QueueCloseFDMessage(int fd
, int hops
);
80 // ChannelReader implementation.
81 ReadState
ReadData(char* buffer
, int buffer_len
, int* bytes_read
) override
;
82 bool WillDispatchInputMessage(Message
* msg
) override
;
83 bool DidEmptyInputBuffers() override
;
84 void HandleInternalMessage(const Message
& msg
) override
;
86 // Finds the set of file descriptors in the given message. On success,
87 // appends the descriptors to the input_fds_ member and returns true
89 // Returns false if the message was truncated. In this case, any handles that
90 // were sent will be closed.
91 bool ExtractFileDescriptorsFromMsghdr(msghdr
* msg
);
93 // Closes all handles in the input_fds_ list and clears the list. This is
94 // used to clean up handles in error conditions to avoid leaking the handles.
97 // MessageLoopForIO::Watcher implementation.
98 void OnFileCanReadWithoutBlocking(int fd
) override
;
99 void OnFileCanWriteWithoutBlocking(int fd
) override
;
103 base::ProcessId peer_pid_
;
105 // After accepting one client connection on our server socket we want to
107 base::MessageLoopForIO::FileDescriptorWatcher
108 server_listen_connection_watcher_
;
109 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_
;
110 base::MessageLoopForIO::FileDescriptorWatcher write_watcher_
;
112 // Indicates whether we're currently blocked waiting for a write to complete.
113 bool is_blocked_on_write_
;
114 bool waiting_connect_
;
116 // If sending a message blocks then we use this variable
117 // to keep track of where we are.
118 size_t message_send_bytes_written_
;
120 // File descriptor we're listening on for new connections if we listen
122 base::ScopedFD server_listen_pipe_
;
124 // The pipe used for communication.
125 base::ScopedFD pipe_
;
127 // For a server, the client end of our socketpair() -- the other end of our
128 // pipe_ that is passed to the client.
129 base::ScopedFD client_pipe_
;
130 mutable base::Lock client_pipe_lock_
; // Lock that protects |client_pipe_|.
132 // The "name" of our pipe. On Windows this is the global identifier for
133 // the pipe. On POSIX it's used as a key in a local map of file descriptors.
134 std::string pipe_name_
;
136 // Messages to be sent are queued here.
137 std::queue
<Message
*> output_queue_
;
139 // We assume a worst case: kReadBufferSize bytes of messages, where each
140 // message has no payload and a full complement of descriptors.
141 static const size_t kMaxReadFDs
=
142 (Channel::kReadBufferSize
/ sizeof(IPC::Message::Header
)) *
143 MessageAttachmentSet::kMaxDescriptorsPerMessage
;
145 // Buffer size for file descriptors used for recvmsg. On Mac the CMSG macros
146 // are not constant so we have to pick a "large enough" padding for headers.
147 #if defined(OS_MACOSX)
148 static const size_t kMaxReadFDBuffer
= 1024 + sizeof(int) * kMaxReadFDs
;
150 static const size_t kMaxReadFDBuffer
= CMSG_SPACE(sizeof(int) * kMaxReadFDs
);
152 static_assert(kMaxReadFDBuffer
<= 8192,
153 "kMaxReadFDBuffer too big for a stack buffer");
155 // File descriptors extracted from messages coming off of the channel. The
156 // handles may span messages and come off different channels from the message
157 // data (in the case of READWRITE), and are processed in FIFO here.
158 // NOTE: The implementation assumes underlying storage here is contiguous, so
159 // don't change to something like std::deque<> without changing the
161 std::vector
<int> input_fds_
;
164 void ResetSafely(base::ScopedFD
* fd
);
167 #if defined(OS_MACOSX)
168 // On OSX, sent FDs must not be closed until we get an ack.
169 // Keep track of sent FDs here to make sure the remote is not
170 // trying to bamboozle us.
171 std::set
<int> fds_to_close_
;
174 // True if we are responsible for unlinking the unix domain socket file.
177 #if defined(OS_LINUX)
178 // If non-zero, overrides the process ID sent in the hello message.
179 static int global_pid_
;
182 // |broker_| must outlive this instance.
183 AttachmentBroker
* broker_
;
185 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelPosix
);
190 #endif // IPC_IPC_CHANNEL_POSIX_H_