OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_IPC_CHANNEL_POSIX_H_ | 5 #ifndef IPC_IPC_CHANNEL_POSIX_H_ |
6 #define IPC_IPC_CHANNEL_POSIX_H_ | 6 #define IPC_IPC_CHANNEL_POSIX_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "ipc/ipc_channel.h" | 9 #include "ipc/ipc_channel.h" |
10 | 10 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 bool ProcessIncomingMessages(); | 75 bool ProcessIncomingMessages(); |
76 bool ProcessOutgoingMessages(); | 76 bool ProcessOutgoingMessages(); |
77 | 77 |
78 bool AcceptConnection(); | 78 bool AcceptConnection(); |
79 void ClosePipeOnError(); | 79 void ClosePipeOnError(); |
80 int GetHelloMessageProcId(); | 80 int GetHelloMessageProcId(); |
81 void QueueHelloMessage(); | 81 void QueueHelloMessage(); |
82 bool IsHelloMessage(const Message* m) const; | 82 bool IsHelloMessage(const Message* m) const; |
83 | 83 |
| 84 // Reads data from the "regular" (non FD) pipe into the input buffers. The |
| 85 // two output params will identify the data received. |
| 86 // |
| 87 // On success, returns true. If there is no data waiting, the pointers will |
| 88 // both be set to NULL. Otherwise, they'll indicate the data read. This will |
| 89 // be inside the input_buf_ for short messages, and for long messages will |
| 90 // automatically spill into the input_overflow_buf_. When in non-READWRITE |
| 91 // mode this will also load any handles from the message into input_fds_. |
| 92 // |
| 93 // On failure, returns false. This means there was some kind of pipe error |
| 94 // and we should not continue. |
| 95 bool ReadDataFromPipe(const char** begin, const char** end); |
| 96 |
| 97 #if defined(IPC_USES_READWRITE) |
| 98 // Reads the next message from the fd_pipe_ and appends them to the |
| 99 // input_fds_ queue. Returns false if there was a message receiving error. |
| 100 // True means there was a message and it was processed properly, or there was |
| 101 // no messages. |
| 102 bool ReadFileDescriptorsFromFDPipe(); |
| 103 #endif |
| 104 |
| 105 // Loads the required file desciptors into the given message. Returns true |
| 106 // on success. False means a fatal channel error. |
| 107 // |
| 108 // This will read from the input_fds_ and read more handles from the FD |
| 109 // pipe if necessary. |
| 110 bool PopulateMessageFileDescriptors(Message* msg); |
| 111 |
| 112 // Finds the set of file descriptors in the given message. On success, |
| 113 // appends the descriptors to the input_fds_ member and returns true |
| 114 // |
| 115 // Returns false if the message was truncated. In this case, any handles that |
| 116 // were sent will be closed. |
| 117 bool ExtractFileDescriptorsFromMsghdr(msghdr* msg); |
| 118 |
| 119 // Closes all handles in the input_fds_ list and clears the list. This is |
| 120 // used to clean up handles in error conditions to avoid leaking the handles. |
| 121 void ClearInputFDs(); |
| 122 |
| 123 // Handles the first message sent over the pipe which contains setup info. |
| 124 void HandleHelloMessage(const Message& msg); |
| 125 |
84 // MessageLoopForIO::Watcher implementation. | 126 // MessageLoopForIO::Watcher implementation. |
85 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; | 127 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE; |
86 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; | 128 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE; |
87 | 129 |
88 Mode mode_; | 130 Mode mode_; |
89 | 131 |
90 // After accepting one client connection on our server socket we want to | 132 // After accepting one client connection on our server socket we want to |
91 // stop listening. | 133 // stop listening. |
92 MessageLoopForIO::FileDescriptorWatcher server_listen_connection_watcher_; | 134 MessageLoopForIO::FileDescriptorWatcher server_listen_connection_watcher_; |
93 MessageLoopForIO::FileDescriptorWatcher read_watcher_; | 135 MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
(...skipping 27 matching lines...) Expand all Loading... |
121 | 163 |
122 // The "name" of our pipe. On Windows this is the global identifier for | 164 // The "name" of our pipe. On Windows this is the global identifier for |
123 // the pipe. On POSIX it's used as a key in a local map of file descriptors. | 165 // the pipe. On POSIX it's used as a key in a local map of file descriptors. |
124 std::string pipe_name_; | 166 std::string pipe_name_; |
125 | 167 |
126 Listener* listener_; | 168 Listener* listener_; |
127 | 169 |
128 // Messages to be sent are queued here. | 170 // Messages to be sent are queued here. |
129 std::queue<Message*> output_queue_; | 171 std::queue<Message*> output_queue_; |
130 | 172 |
131 // We read from the pipe into this buffer | 173 // We read from the pipe into this buffer. Managed by ReadDataFromPipe, do |
| 174 // not access directly outside that function. |
132 char input_buf_[Channel::kReadBufferSize]; | 175 char input_buf_[Channel::kReadBufferSize]; |
133 | 176 |
| 177 // Large messages that span multiple pipe buffers, get built-up using |
| 178 // this buffer. |
| 179 std::string input_overflow_buf_; |
| 180 |
134 // We assume a worst case: kReadBufferSize bytes of messages, where each | 181 // We assume a worst case: kReadBufferSize bytes of messages, where each |
135 // message has no payload and a full complement of descriptors. | 182 // message has no payload and a full complement of descriptors. |
136 static const size_t kMaxReadFDs = | 183 static const size_t kMaxReadFDs = |
137 (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) * | 184 (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) * |
138 FileDescriptorSet::kMaxDescriptorsPerMessage; | 185 FileDescriptorSet::kMaxDescriptorsPerMessage; |
139 | 186 |
140 // This is a control message buffer large enough to hold kMaxReadFDs | 187 // Buffer size for file descriptors used for recvmsg. On Mac the CMSG macros |
| 188 // don't seem to be constant so we have to pick a "large enough" value. |
141 #if defined(OS_MACOSX) | 189 #if defined(OS_MACOSX) |
142 // TODO(agl): OSX appears to have non-constant CMSG macros! | 190 static const size_t kMaxReadFDBuffer = 1024; |
143 char input_cmsg_buf_[1024]; | |
144 #else | 191 #else |
145 char input_cmsg_buf_[CMSG_SPACE(sizeof(int) * kMaxReadFDs)]; | 192 static const size_t kMaxReadFDBuffer = CMSG_SPACE(sizeof(int) * kMaxReadFDs); |
146 #endif | 193 #endif |
147 | 194 |
148 // Large messages that span multiple pipe buffers, get built-up using | 195 // Temporary buffer used to receive the file descriptors from recvmsg. |
149 // this buffer. | 196 // Code that writes into this should immediately read them out and save |
150 std::string input_overflow_buf_; | 197 // them to input_fds_, since this buffer will be re-used anytime we call |
151 std::vector<int> input_overflow_fds_; | 198 // recvmsg. |
| 199 char input_cmsg_buf_[kMaxReadFDBuffer]; |
| 200 |
| 201 // File descriptors extracted from messages coming off of the channel. The |
| 202 // handles may span messages and come off different channels from the message |
| 203 // data (in the case of READWRITE), and are processed in FIFO here. |
| 204 std::deque<int> input_fds_; |
152 | 205 |
153 // True if we are responsible for unlinking the unix domain socket file. | 206 // True if we are responsible for unlinking the unix domain socket file. |
154 bool must_unlink_; | 207 bool must_unlink_; |
155 | 208 |
156 #if defined(OS_LINUX) | 209 #if defined(OS_LINUX) |
157 // If non-zero, overrides the process ID sent in the hello message. | 210 // If non-zero, overrides the process ID sent in the hello message. |
158 static int global_pid_; | 211 static int global_pid_; |
159 #endif // OS_LINUX | 212 #endif // OS_LINUX |
160 | 213 |
161 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl); | 214 DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl); |
162 }; | 215 }; |
163 | 216 |
164 // The maximum length of the name of a pipe for MODE_NAMED_SERVER or | 217 // The maximum length of the name of a pipe for MODE_NAMED_SERVER or |
165 // MODE_NAMED_CLIENT if you want to pass in your own socket. | 218 // MODE_NAMED_CLIENT if you want to pass in your own socket. |
166 // The standard size on linux is 108, mac is 104. To maintain consistency | 219 // The standard size on linux is 108, mac is 104. To maintain consistency |
167 // across platforms we standardize on the smaller value. | 220 // across platforms we standardize on the smaller value. |
168 static const size_t kMaxPipeNameLength = 104; | 221 static const size_t kMaxPipeNameLength = 104; |
169 | 222 |
170 } // namespace IPC | 223 } // namespace IPC |
171 | 224 |
172 #endif // IPC_IPC_CHANNEL_POSIX_H_ | 225 #endif // IPC_IPC_CHANNEL_POSIX_H_ |
OLD | NEW |