blob: 8760135209ee7a0a27dbb098a2b6c6bd7cc49a31 [file] [log] [blame]
brettw@chromium.org293988a2012-03-01 07:48:14 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef IPC_IPC_CHANNEL_POSIX_H_
6#define IPC_IPC_CHANNEL_POSIX_H_
7
8#include "ipc/ipc_channel.h"
9
10#include <sys/socket.h> // for CMSG macros
11
12#include <queue>
hubbe@chromium.org683920d2013-10-15 09:07:00 +090013#include <set>
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090014#include <string>
15#include <vector>
16
morrita39722a32014-09-30 07:25:54 +090017#include "base/files/scoped_file.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090018#include "base/message_loop/message_loop.h"
rsesek@chromium.org19319712013-07-24 14:15:24 +090019#include "base/process/process.h"
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090020#include "ipc/ipc_channel_reader.h"
morrita33a35902015-01-15 06:17:06 +090021#include "ipc/ipc_message_attachment_set.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090022
23namespace IPC {
24
morrita@chromium.org844f1c32014-06-07 15:15:53 +090025class IPC_EXPORT ChannelPosix : public Channel,
26 public internal::ChannelReader,
27 public base::MessageLoopForIO::Watcher {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090028 public:
erikchen99b3dc02015-06-17 06:21:04 +090029 // |broker| must outlive the newly created object.
30 ChannelPosix(const IPC::ChannelHandle& channel_handle,
31 Mode mode,
32 Listener* listener,
33 AttachmentBroker* broker);
dchengef7721a2014-10-22 11:29:52 +090034 ~ChannelPosix() override;
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090035
36 // Channel implementation
dchengef7721a2014-10-22 11:29:52 +090037 bool Connect() override;
38 void Close() override;
39 bool Send(Message* message) override;
erikchen99b3dc02015-06-17 06:21:04 +090040 AttachmentBroker* GetAttachmentBroker() override;
dchengef7721a2014-10-22 11:29:52 +090041 base::ProcessId GetPeerPID() const override;
42 base::ProcessId GetSelfPID() const override;
43 int GetClientFileDescriptor() const override;
44 base::ScopedFD TakeClientFileDescriptor() override;
morrita@chromium.org844f1c32014-06-07 15:15:53 +090045
46 // Returns true if the channel supports listening for connections.
47 bool AcceptsConnections() const;
48
49 // Returns true if the channel supports listening for connections and is
50 // currently connected.
51 bool HasAcceptedConnection() const;
52
53 // Closes any currently connected socket, and returns to a listening state
54 // for more connections.
55 void ResetToAcceptingConnectionState();
56
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;
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090060
phajdan.jr@chromium.orgaf9455b2011-09-20 02:08:12 +090061 void CloseClientFileDescriptor();
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090062
kkania@chromium.orgf37b4e52011-08-09 15:46:06 +090063 static bool IsNamedServerInitialized(const std::string& channel_id);
perkj596cc352014-12-12 02:27:58 +090064#if defined(OS_LINUX)
65 static void SetGlobalPid(int pid);
66#endif // OS_LINUX
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090067
68 private:
dmaclach@chromium.orgf146c292011-02-04 05:35:09 +090069 bool CreatePipe(const IPC::ChannelHandle& channel_handle);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090070
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090071 bool ProcessOutgoingMessages();
72
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090073 bool AcceptConnection();
74 void ClosePipeOnError();
morrita@chromium.org15996aa2014-08-05 08:44:17 +090075 int GetHelloMessageProcId() const;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090076 void QueueHelloMessage();
hubbe@chromium.org683920d2013-10-15 09:07:00 +090077 void CloseFileDescriptors(Message* msg);
78 void QueueCloseFDMessage(int fd, int hops);
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +090079
brettw@chromium.org0e9d0a12012-03-08 21:30:28 +090080 // ChannelReader implementation.
dchengef7721a2014-10-22 11:29:52 +090081 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read) override;
erikchenf295bbc2015-07-28 03:26:14 +090082 bool ShouldDispatchInputMessage(Message* msg) override;
83 bool GetNonBrokeredAttachments(Message* msg) override;
dchengef7721a2014-10-22 11:29:52 +090084 bool DidEmptyInputBuffers() override;
85 void HandleInternalMessage(const Message& msg) override;
erikchencc6ccfc2015-07-29 08:16:48 +090086 base::ProcessId GetSenderPID() override;
erikchenca630d72015-07-31 07:26:08 +090087 bool IsAttachmentBrokerEndpoint() override;
brettw@chromium.org293988a2012-03-01 07:48:14 +090088
brettw@chromium.org293988a2012-03-01 07:48:14 +090089 // Finds the set of file descriptors in the given message. On success,
90 // appends the descriptors to the input_fds_ member and returns true
91 //
92 // Returns false if the message was truncated. In this case, any handles that
93 // were sent will be closed.
94 bool ExtractFileDescriptorsFromMsghdr(msghdr* msg);
95
96 // Closes all handles in the input_fds_ list and clears the list. This is
97 // used to clean up handles in error conditions to avoid leaking the handles.
98 void ClearInputFDs();
99
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900100 // MessageLoopForIO::Watcher implementation.
dchengef7721a2014-10-22 11:29:52 +0900101 void OnFileCanReadWithoutBlocking(int fd) override;
102 void OnFileCanWriteWithoutBlocking(int fd) override;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900103
104 Mode mode_;
105
jschuh@chromium.orga5cd0762012-04-05 11:38:34 +0900106 base::ProcessId peer_pid_;
107
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900108 // After accepting one client connection on our server socket we want to
109 // stop listening.
xhwang@chromium.org0b2c2a52013-05-01 05:55:03 +0900110 base::MessageLoopForIO::FileDescriptorWatcher
111 server_listen_connection_watcher_;
112 base::MessageLoopForIO::FileDescriptorWatcher read_watcher_;
113 base::MessageLoopForIO::FileDescriptorWatcher write_watcher_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900114
115 // Indicates whether we're currently blocked waiting for a write to complete.
116 bool is_blocked_on_write_;
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900117 bool waiting_connect_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900118
119 // If sending a message blocks then we use this variable
120 // to keep track of where we are.
121 size_t message_send_bytes_written_;
122
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900123 // File descriptor we're listening on for new connections if we listen
124 // for connections.
morrita39722a32014-09-30 07:25:54 +0900125 base::ScopedFD server_listen_pipe_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900126
127 // The pipe used for communication.
morrita39722a32014-09-30 07:25:54 +0900128 base::ScopedFD pipe_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900129
130 // For a server, the client end of our socketpair() -- the other end of our
131 // pipe_ that is passed to the client.
morrita39722a32014-09-30 07:25:54 +0900132 base::ScopedFD client_pipe_;
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +0900133 mutable base::Lock client_pipe_lock_; // Lock that protects |client_pipe_|.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900134
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900135 // The "name" of our pipe. On Windows this is the global identifier for
136 // the pipe. On POSIX it's used as a key in a local map of file descriptors.
137 std::string pipe_name_;
138
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900139 // Messages to be sent are queued here.
140 std::queue<Message*> output_queue_;
141
pkasting@chromium.org9687a8f2011-09-01 09:50:13 +0900142 // We assume a worst case: kReadBufferSize bytes of messages, where each
143 // message has no payload and a full complement of descriptors.
144 static const size_t kMaxReadFDs =
145 (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) *
morrita33a35902015-01-15 06:17:06 +0900146 MessageAttachmentSet::kMaxDescriptorsPerMessage;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900147
brettw@chromium.org293988a2012-03-01 07:48:14 +0900148 // Buffer size for file descriptors used for recvmsg. On Mac the CMSG macros
bratell88511de2015-05-29 22:19:01 +0900149 // are not constant so we have to pick a "large enough" padding for headers.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900150#if defined(OS_MACOSX)
bratell88511de2015-05-29 22:19:01 +0900151 static const size_t kMaxReadFDBuffer = 1024 + sizeof(int) * kMaxReadFDs;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900152#else
brettw@chromium.org293988a2012-03-01 07:48:14 +0900153 static const size_t kMaxReadFDBuffer = CMSG_SPACE(sizeof(int) * kMaxReadFDs);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900154#endif
bratell88511de2015-05-29 22:19:01 +0900155 static_assert(kMaxReadFDBuffer <= 8192,
156 "kMaxReadFDBuffer too big for a stack buffer");
brettw@chromium.org293988a2012-03-01 07:48:14 +0900157
158 // File descriptors extracted from messages coming off of the channel. The
159 // handles may span messages and come off different channels from the message
160 // data (in the case of READWRITE), and are processed in FIFO here.
fischman@chromium.org8b60dfa2012-04-10 06:40:44 +0900161 // NOTE: The implementation assumes underlying storage here is contiguous, so
162 // don't change to something like std::deque<> without changing the
163 // implementation!
164 std::vector<int> input_fds_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900165
morritae491a7b2015-01-28 05:05:53 +0900166
167 void ResetSafely(base::ScopedFD* fd);
168 bool in_dtor_;
169
hubbe@chromium.org683920d2013-10-15 09:07:00 +0900170#if defined(OS_MACOSX)
171 // On OSX, sent FDs must not be closed until we get an ack.
172 // Keep track of sent FDs here to make sure the remote is not
173 // trying to bamboozle us.
174 std::set<int> fds_to_close_;
175#endif
176
dmaclach@chromium.orgc1d3d422010-12-20 15:59:23 +0900177 // True if we are responsible for unlinking the unix domain socket file.
178 bool must_unlink_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900179
perkj596cc352014-12-12 02:27:58 +0900180#if defined(OS_LINUX)
181 // If non-zero, overrides the process ID sent in the hello message.
182 static int global_pid_;
183#endif // OS_LINUX
184
erikchen99b3dc02015-06-17 06:21:04 +0900185 // |broker_| must outlive this instance.
186 AttachmentBroker* broker_;
187
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +0900188 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelPosix);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900189};
190
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900191} // namespace IPC
192
193#endif // IPC_IPC_CHANNEL_POSIX_H_