OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "ipc/ipc_channel_posix.h" | 5 #include "ipc/ipc_channel_posix.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "base/process_util.h" | 23 #include "base/process_util.h" |
24 #include "base/scoped_ptr.h" | 24 #include "base/scoped_ptr.h" |
25 #include "base/singleton.h" | 25 #include "base/singleton.h" |
26 #include "base/string_util.h" | 26 #include "base/string_util.h" |
27 #include "ipc/ipc_descriptors.h" | 27 #include "ipc/ipc_descriptors.h" |
28 #include "ipc/ipc_switches.h" | 28 #include "ipc/ipc_switches.h" |
29 #include "ipc/file_descriptor_set_posix.h" | 29 #include "ipc/file_descriptor_set_posix.h" |
30 #include "ipc/ipc_logging.h" | 30 #include "ipc/ipc_logging.h" |
31 #include "ipc/ipc_message_utils.h" | 31 #include "ipc/ipc_message_utils.h" |
32 | 32 |
33 #if defined (OS_MACOSX) | |
34 #include <poll.h> | |
35 #endif // OS_MACOSX | |
36 | |
33 namespace IPC { | 37 namespace IPC { |
34 | 38 |
35 // IPC channels on Windows use named pipes (CreateNamedPipe()) with | 39 // IPC channels on Windows use named pipes (CreateNamedPipe()) with |
36 // channel ids as the pipe names. Channels on POSIX use anonymous | 40 // channel ids as the pipe names. Channels on POSIX use sockets as |
37 // Unix domain sockets created via socketpair() as pipes. These don't | 41 // pipes These don't quite line up. |
38 // quite line up. | |
39 // | 42 // |
40 // When creating a child subprocess, the parent side of the fork | 43 // When creating a child subprocess we use a socket pair and the parent side of |
41 // arranges it such that the initial control channel ends up on the | 44 // the fork arranges it such that the initial control channel ends up on the |
42 // magic file descriptor kPrimaryIPCChannel in the child. Future | 45 // magic file descriptor kPrimaryIPCChannel in the child. Future |
43 // connections (file descriptors) can then be passed via that | 46 // connections (file descriptors) can then be passed via that |
44 // connection via sendmsg(). | 47 // connection via sendmsg(). |
48 // | |
49 // A POSIX IPC channel can also be set up as a server for a bound UNIX domain | |
50 // socket, and will handle multiple connect and disconnect sequences. Currently | |
51 // it is limited to one connection at a time. | |
45 | 52 |
46 //------------------------------------------------------------------------------ | 53 //------------------------------------------------------------------------------ |
47 namespace { | 54 namespace { |
48 | 55 |
49 // The PipeMap class works around this quirk related to unit tests: | 56 // The PipeMap class works around this quirk related to unit tests: |
50 // | 57 // |
51 // When running as a server, we install the client socket in a | 58 // When running as a server, we install the client socket in a |
52 // specific file descriptor number (@kPrimaryIPCChannel). However, we | 59 // specific file descriptor number (@kPrimaryIPCChannel). However, we |
53 // also have to support the case where we are running unittests in the | 60 // also have to support the case where we are running unittests in the |
54 // same process. (We do not support forking without execing.) | 61 // same process. (We do not support forking without execing.) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 } | 128 } |
122 | 129 |
123 private: | 130 private: |
124 Lock lock_; | 131 Lock lock_; |
125 typedef std::map<std::string, int> ChannelToFDMap; | 132 typedef std::map<std::string, int> ChannelToFDMap; |
126 ChannelToFDMap map_; | 133 ChannelToFDMap map_; |
127 | 134 |
128 friend struct DefaultSingletonTraits<PipeMap>; | 135 friend struct DefaultSingletonTraits<PipeMap>; |
129 }; | 136 }; |
130 | 137 |
131 // Used to map a channel name to the equivalent FD # in the current process. | |
132 // Returns -1 if the channel is unknown. | |
133 int ChannelNameToFD(const std::string& channel_id) { | |
134 // See the large block comment above PipeMap for the reasoning here. | |
135 const int fd = PipeMap::GetInstance()->Lookup(channel_id); | |
136 | |
137 if (fd != -1) { | |
138 int dup_fd = dup(fd); | |
139 if (dup_fd < 0) | |
140 PLOG(FATAL) << "dup(" << fd << ") " << channel_id; | |
141 return dup_fd; | |
142 } | |
143 | |
144 return fd; | |
145 } | |
146 | |
147 //------------------------------------------------------------------------------ | 138 //------------------------------------------------------------------------------ |
148 // The standard size on linux is 108, mac is 104. To maintain consistency | 139 // The standard size on linux is 108, mac is 104. To maintain consistency |
149 // across platforms we standardize on the smaller value. | 140 // across platforms we standardize on the smaller value. |
150 const size_t kMaxPipeNameLength = 104; | 141 const size_t kMaxPipeNameLength = 104; |
151 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, | 142 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, |
152 BAD_SUN_PATH_LENGTH); | 143 BAD_SUN_PATH_LENGTH); |
153 | 144 |
154 // Creates a Fifo with the specified name ready to listen on. | 145 // Creates a unix domain socket bound to the specified name that is listening |
155 bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) { | 146 // for connections. |
147 bool CreateServerUnixDomainSocket(const std::string& pipe_name, | |
148 int* server_listen_fd) { | |
156 DCHECK(server_listen_fd); | 149 DCHECK(server_listen_fd); |
157 DCHECK_GT(pipe_name.length(), 0u); | 150 DCHECK_GT(pipe_name.length(), 0u); |
158 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); | 151 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
159 | 152 |
160 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { | 153 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { |
161 return false; | 154 return false; |
162 } | 155 } |
163 | 156 |
164 // Create socket. | 157 // Create socket. |
165 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | 158 int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
166 if (fd < 0) { | 159 if (fd < 0) { |
167 return false; | 160 return false; |
168 } | 161 } |
169 | 162 |
170 // Make socket non-blocking | 163 // Make socket non-blocking |
171 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { | 164 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
172 PLOG(ERROR) << "fcntl " << pipe_name; | 165 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; |
173 if (HANDLE_EINTR(close(fd)) < 0) | 166 if (HANDLE_EINTR(close(fd)) < 0) |
174 PLOG(ERROR) << "close " << pipe_name; | 167 PLOG(ERROR) << "close " << pipe_name; |
175 return false; | 168 return false; |
176 } | 169 } |
177 | 170 |
178 // Delete any old FS instances. | 171 // Delete any old FS instances. |
179 unlink(pipe_name.c_str()); | 172 unlink(pipe_name.c_str()); |
180 | 173 |
181 // Create unix_addr structure | 174 // Create unix_addr structure |
182 struct sockaddr_un unix_addr; | 175 struct sockaddr_un unix_addr; |
(...skipping 18 matching lines...) Expand all Loading... | |
201 PLOG(ERROR) << "listen " << pipe_name; | 194 PLOG(ERROR) << "listen " << pipe_name; |
202 if (HANDLE_EINTR(close(fd)) < 0) | 195 if (HANDLE_EINTR(close(fd)) < 0) |
203 PLOG(ERROR) << "close " << pipe_name; | 196 PLOG(ERROR) << "close " << pipe_name; |
204 return false; | 197 return false; |
205 } | 198 } |
206 | 199 |
207 *server_listen_fd = fd; | 200 *server_listen_fd = fd; |
208 return true; | 201 return true; |
209 } | 202 } |
210 | 203 |
211 // Accept a connection on a fifo. | 204 // Accept a connection on a socket we are listening to. |
212 bool ServerAcceptFifoConnection(int server_listen_fd, int* server_socket) { | 205 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { |
213 DCHECK(server_socket); | 206 DCHECK(server_socket); |
214 | 207 |
215 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); | 208 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); |
216 if (accept_fd < 0) | 209 if (accept_fd < 0) |
217 return false; | 210 return false; |
218 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { | 211 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { |
219 PLOG(ERROR) << "fcntl " << accept_fd; | 212 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; |
220 if (HANDLE_EINTR(close(accept_fd)) < 0) | 213 if (HANDLE_EINTR(close(accept_fd)) < 0) |
221 PLOG(ERROR) << "close " << accept_fd; | 214 PLOG(ERROR) << "close " << accept_fd; |
222 return false; | 215 return false; |
223 } | 216 } |
224 | 217 |
225 *server_socket = accept_fd; | 218 *server_socket = accept_fd; |
226 return true; | 219 return true; |
227 } | 220 } |
228 | 221 |
229 bool ClientConnectToFifo(const std::string &pipe_name, int* client_socket) { | 222 bool CreateClientUnixDomainSocket(const std::string& pipe_name, |
223 int* client_socket) { | |
230 DCHECK(client_socket); | 224 DCHECK(client_socket); |
231 DCHECK_GT(pipe_name.length(), 0u); | 225 DCHECK_GT(pipe_name.length(), 0u); |
232 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); | 226 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
233 | 227 |
234 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { | 228 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { |
235 return false; | 229 return false; |
236 } | 230 } |
237 | 231 |
238 // Create socket. | 232 // Create socket. |
239 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | 233 int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
240 if (fd < 0) { | 234 if (fd < 0) { |
241 PLOG(ERROR) << "socket " << pipe_name; | 235 PLOG(ERROR) << "socket " << pipe_name; |
242 return false; | 236 return false; |
243 } | 237 } |
244 | 238 |
245 // Make socket non-blocking | 239 // Make socket non-blocking |
246 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { | 240 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
247 PLOG(ERROR) << "fcntl " << pipe_name; | 241 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; |
248 if (HANDLE_EINTR(close(fd)) < 0) | 242 if (HANDLE_EINTR(close(fd)) < 0) |
249 PLOG(ERROR) << "close " << pipe_name; | 243 PLOG(ERROR) << "close " << pipe_name; |
250 return false; | 244 return false; |
251 } | 245 } |
252 | 246 |
253 // Create server side of socket. | 247 // Create server side of socket. |
254 struct sockaddr_un server_unix_addr; | 248 struct sockaddr_un server_unix_addr; |
255 memset(&server_unix_addr, 0, sizeof(server_unix_addr)); | 249 memset(&server_unix_addr, 0, sizeof(server_unix_addr)); |
256 server_unix_addr.sun_family = AF_UNIX; | 250 server_unix_addr.sun_family = AF_UNIX; |
257 snprintf(server_unix_addr.sun_path, kMaxPipeNameLength, "%s", | 251 snprintf(server_unix_addr.sun_path, kMaxPipeNameLength, "%s", |
(...skipping 22 matching lines...) Expand all Loading... | |
280 // EMSGSIZE presents a problem since the system APIs can only call us when | 274 // EMSGSIZE presents a problem since the system APIs can only call us when |
281 // there's room in the socket buffer and not when there is "enough" room. | 275 // there's room in the socket buffer and not when there is "enough" room. |
282 // | 276 // |
283 // The current behavior is to return to the event loop when EMSGSIZE is | 277 // The current behavior is to return to the event loop when EMSGSIZE is |
284 // received and hopefull service another FD. This is however still | 278 // received and hopefull service another FD. This is however still |
285 // technically a busy wait since the event loop will call us right back until | 279 // technically a busy wait since the event loop will call us right back until |
286 // the receiver has read enough data to allow passing the FD over atomically. | 280 // the receiver has read enough data to allow passing the FD over atomically. |
287 return errno == EAGAIN || errno == EMSGSIZE; | 281 return errno == EAGAIN || errno == EMSGSIZE; |
288 #else | 282 #else |
289 return errno == EAGAIN; | 283 return errno == EAGAIN; |
290 #endif | 284 #endif // OS_MACOSX |
291 } | 285 } |
292 | 286 |
293 } // namespace | 287 } // namespace |
294 //------------------------------------------------------------------------------ | 288 //------------------------------------------------------------------------------ |
295 | 289 |
296 Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, | 290 Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, |
297 Mode mode, Listener* listener) | 291 Mode mode, Listener* listener) |
298 : mode_(mode), | 292 : mode_(mode), |
299 is_blocked_on_write_(false), | 293 is_blocked_on_write_(false), |
294 waiting_connect_(true), | |
300 message_send_bytes_written_(0), | 295 message_send_bytes_written_(0), |
301 uses_fifo_(CommandLine::ForCurrentProcess()->HasSwitch( | |
302 switches::kIPCUseFIFO)), | |
303 server_listen_pipe_(-1), | 296 server_listen_pipe_(-1), |
304 pipe_(-1), | 297 pipe_(-1), |
305 client_pipe_(-1), | 298 client_pipe_(-1), |
306 #if defined(IPC_USES_READWRITE) | 299 #if defined(IPC_USES_READWRITE) |
307 fd_pipe_(-1), | 300 fd_pipe_(-1), |
308 remote_fd_pipe_(-1), | 301 remote_fd_pipe_(-1), |
309 #endif | 302 #endif // IPC_USES_READWRITE |
303 pipe_name_(channel_handle.name), | |
Evan Martin
2010/12/14 20:23:19
Up until now, the channel name was just a process-
dmac
2010/12/14 21:01:16
It only needs to be a path in the MODE_NAMED_SERVE
| |
310 listener_(listener), | 304 listener_(listener), |
311 waiting_connect_(true), | |
312 factory_(this) { | 305 factory_(this) { |
313 if (!CreatePipe(channel_handle, mode_)) { | 306 // Check to see if we want to implement using domain sockets. |
307 bool uses_domain_socket = false; | |
308 if (mode_ == MODE_NAMED_SERVER) { | |
309 uses_domain_socket = true; | |
310 mode_ = MODE_SERVER; | |
311 } else if (mode_ == MODE_NAMED_CLIENT) { | |
312 uses_domain_socket = true; | |
313 mode_ = MODE_CLIENT; | |
314 } | |
315 if (!CreatePipe(channel_handle, uses_domain_socket)) { | |
316 // The pipe may have been closed already. | |
317 const char *modestr = (mode_ == MODE_SERVER | |
318 || mode_ == MODE_NAMED_SERVER) ? "server" : "client"; | |
314 // The pipe may have been closed already. | 319 // The pipe may have been closed already. |
315 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name | 320 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name |
316 << "\" in " << (mode_ == MODE_SERVER ? "server" : "client") | 321 << "\" in " << modestr << " mode"; |
317 << " mode"; | |
318 } | 322 } |
319 } | 323 } |
320 | 324 |
321 Channel::ChannelImpl::~ChannelImpl() { | 325 Channel::ChannelImpl::~ChannelImpl() { |
322 Close(); | 326 Close(); |
323 } | 327 } |
324 | 328 |
325 // static | |
326 void AddChannelSocket(const std::string& name, int socket) { | |
327 PipeMap::GetInstance()->Insert(name, socket); | |
328 } | |
329 | |
330 // static | |
331 void RemoveAndCloseChannelSocket(const std::string& name) { | |
332 PipeMap::GetInstance()->RemoveAndClose(name); | |
333 } | |
334 | |
335 // static | |
336 bool ChannelSocketExists(const std::string& name) { | |
337 return PipeMap::GetInstance()->Lookup(name) != -1; | |
338 } | |
339 | |
340 // static | |
341 bool SocketPair(int* fd1, int* fd2) { | 329 bool SocketPair(int* fd1, int* fd2) { |
342 int pipe_fds[2]; | 330 int pipe_fds[2]; |
343 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { | 331 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
344 PLOG(ERROR) << "socketpair()"; | 332 PLOG(ERROR) << "socketpair()"; |
345 return false; | 333 return false; |
346 } | 334 } |
347 | 335 |
348 // Set both ends to be non-blocking. | 336 // Set both ends to be non-blocking. |
349 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || | 337 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
350 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { | 338 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
351 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; | 339 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
352 if (HANDLE_EINTR(close(pipe_fds[0])) < 0) | 340 if (HANDLE_EINTR(close(pipe_fds[0])) < 0) |
353 PLOG(ERROR) << "close"; | 341 PLOG(ERROR) << "close"; |
354 if (HANDLE_EINTR(close(pipe_fds[1])) < 0) | 342 if (HANDLE_EINTR(close(pipe_fds[1])) < 0) |
355 PLOG(ERROR) << "close"; | 343 PLOG(ERROR) << "close"; |
356 return false; | 344 return false; |
357 } | 345 } |
358 | 346 |
359 *fd1 = pipe_fds[0]; | 347 *fd1 = pipe_fds[0]; |
360 *fd2 = pipe_fds[1]; | 348 *fd2 = pipe_fds[1]; |
361 | 349 |
362 return true; | 350 return true; |
363 } | 351 } |
364 | 352 |
365 bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle, | 353 bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle, |
366 Mode mode) { | 354 bool uses_domain_sockets) { |
367 DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); | 355 DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); |
368 pipe_name_ = channel_handle.name; | 356 |
369 pipe_ = channel_handle.socket.fd; | 357 // Four possible cases: |
370 if (uses_fifo_) { | 358 // 1) It's a channel wrapping a pipe that is given to us. |
371 // This only happens in unit tests; see the comment above PipeMap. | 359 // 2) It's for a named channel, so we create it. |
372 // TODO(playmobil): We shouldn't need to create fifos on disk. | 360 // 3) It's for a client that we implement ourself. This is used |
373 // TODO(playmobil): If we do, they should be in the user data directory. | 361 // in unittesting. |
374 // TODO(playmobil): Cleanup any stale fifos. | 362 // 4) It's the initial IPC channel: |
375 if (mode == MODE_SERVER) { | 363 // 4a) Client side: Pull the pipe out of the GlobalDescriptors set. |
376 if (!CreateServerFifo(pipe_name_, &server_listen_pipe_)) { | 364 // 4b) Server side: create the pipe. |
365 | |
366 if (channel_handle.socket.fd != -1) { | |
367 // Case 1 from comment above. | |
368 pipe_ = channel_handle.socket.fd; | |
369 #if defined(IPC_USES_READWRITE) | |
370 // Test the socket passed into us to make sure it is nonblocking. | |
371 // We don't want to call read/write on a blocking socket. | |
372 int value = fcntl(pipe_, F_GETFL); | |
373 if (value == -1) { | |
374 PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; | |
375 return false; | |
376 } | |
377 if (!(value & O_NONBLOCK)) { | |
378 LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; | |
379 return false; | |
380 } | |
381 #endif // IPC_USES_READWRITE | |
382 } else if (uses_domain_sockets) { | |
383 // Case 2 from comment above. | |
384 must_unlink_ = true; | |
385 if (mode_ == MODE_SERVER) { | |
386 if (!CreateServerUnixDomainSocket(pipe_name_, &pipe_)) { | |
377 return false; | 387 return false; |
378 } | 388 } |
379 } else { | 389 } else if (mode_ == MODE_CLIENT) { |
380 if (!ClientConnectToFifo(pipe_name_, &pipe_)) { | 390 if (!CreateClientUnixDomainSocket(pipe_name_, &pipe_)) { |
381 return false; | 391 return false; |
382 } | 392 } |
383 waiting_connect_ = false; | |
384 } | 393 } |
385 } else { | 394 } else { |
386 // This is the normal (non-unit-test) case, where we're using sockets. | 395 pipe_ = PipeMap::GetInstance()->Lookup(pipe_name_); |
387 // Three possible cases: | 396 if (mode_ == MODE_CLIENT) { |
388 // 1) It's for a channel we already have a pipe for; reuse it. | 397 if (pipe_ != -1) { |
389 // 2) It's the initial IPC channel: | 398 // Case 3 from comment above. |
390 // 2a) Server side: create the pipe. | 399 // We only allow one connection. |
391 // 2b) Client side: Pull the pipe out of the GlobalDescriptors set. | 400 pipe_ = HANDLE_EINTR(dup(pipe_)); |
392 if (pipe_ < 0) { | 401 PipeMap::GetInstance()->RemoveAndClose(pipe_name_); |
393 pipe_ = ChannelNameToFD(pipe_name_); | |
394 } | |
395 if (pipe_ < 0) { | |
396 // Initial IPC channel. | |
397 if (mode == MODE_SERVER) { | |
398 if (!SocketPair(&pipe_, &client_pipe_)) | |
399 return false; | |
400 AddChannelSocket(pipe_name_, client_pipe_); | |
401 } else { | 402 } else { |
403 // Case 4a from comment above. | |
402 // Guard against inappropriate reuse of the initial IPC channel. If | 404 // Guard against inappropriate reuse of the initial IPC channel. If |
403 // an IPC channel closes and someone attempts to reuse it by name, the | 405 // an IPC channel closes and someone attempts to reuse it by name, the |
404 // initial channel must not be recycled here. http://crbug.com/26754. | 406 // initial channel must not be recycled here. http://crbug.com/26754. |
405 static bool used_initial_channel = false; | 407 static bool used_initial_channel = false; |
406 if (used_initial_channel) { | 408 if (used_initial_channel) { |
407 LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " | 409 LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " |
408 << pipe_name_; | 410 << pipe_name_; |
409 return false; | 411 return false; |
410 } | 412 } |
411 used_initial_channel = true; | 413 used_initial_channel = true; |
412 | 414 |
413 pipe_ = base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); | 415 pipe_ = base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); |
414 } | 416 } |
417 } else if (mode_ == MODE_SERVER) { | |
418 // Case 4b from comment above. | |
419 if (pipe_ != -1) { | |
420 LOG(ERROR) << "Server already exists for " << pipe_name_; | |
421 return false; | |
422 } | |
423 if (!SocketPair(&pipe_, &client_pipe_)) | |
424 return false; | |
425 PipeMap::GetInstance()->Insert(pipe_name_, client_pipe_); | |
415 } else { | 426 } else { |
416 waiting_connect_ = mode == MODE_SERVER; | 427 LOG(FATAL) << "Unknown mode " << mode_; |
428 return false; | |
417 } | 429 } |
418 } | 430 } |
419 | 431 |
420 // Create the Hello message to be sent when Connect is called | 432 if (mode_ == MODE_SERVER) { |
421 scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, | 433 int listening = 0; |
422 HELLO_MESSAGE_TYPE, | 434 // We support sockets that we are connected to and listening to. |
agl
2010/12/14 20:31:36
This line is unclear. A socket cannot be both conn
dmac
2010/12/14 21:01:16
Removed.
| |
423 IPC::Message::PRIORITY_NORMAL)); | 435 // Currently we only support one connection per socket that we are |
agl
2010/12/14 20:31:36
"one connection per listening socket"
dmac
2010/12/14 21:01:16
Removed.
| |
424 #if defined(IPC_USES_READWRITE) | 436 // listening to. |
425 if (!uses_fifo_) { | 437 // Check to see if the socket is one that we are connected to |
426 // Create a dedicated socketpair() for exchanging file descriptors. | 438 // or one that we are listening to for connections. |
427 // See comments for IPC_USES_READWRITE for details. | 439 #if !defined(OS_MACOSX) |
agl
2010/12/14 20:31:36
This code is terrible! Is there a case where you g
dmac
2010/12/14 21:01:16
Good call. Sorry. This was a left over piece of co
| |
428 if (mode == MODE_SERVER) { | 440 socklen_t len = sizeof(listening); |
429 fd_pipe_ = -1; | 441 if (getsockopt(pipe_, SOL_SOCKET, SO_ACCEPTCONN, |
430 } else if (remote_fd_pipe_ == -1) { | 442 &listening, &len) < 0) { |
431 if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { | 443 PLOG(ERROR) << "Unable to get socket options for " << pipe_name_; |
432 return false; | 444 return false; |
433 } | 445 } |
446 #else | |
447 // Darwin <= Mac OS X 10.6.4 does not support SO_ACCEPTCONN. | |
448 // The poor man's version is to call poll and check to see if the socket | |
449 // in question supports writing. If it doesn't support writing, it is | |
450 // assumed that it is listening for connections. | |
451 struct pollfd pfd = { pipe_, POLLOUT, 0}; | |
452 int count = poll(&pfd, 1, 0); | |
453 if (count < 0) { | |
454 PLOG(ERROR) << "Unable to poll " << pipe_name_; | |
455 return false; | |
456 } | |
457 listening = (count == 0); | |
458 #endif // OS_MACOSX | |
459 if (listening) { | |
460 server_listen_pipe_ = pipe_; | |
461 pipe_ = -1; | |
434 } | 462 } |
435 } | 463 } |
436 #endif | 464 |
437 if (!msg->WriteInt(base::GetCurrentProcId())) { | 465 #if defined(IPC_USES_READWRITE) |
438 Close(); | 466 // Create a dedicated socketpair() for exchanging file descriptors. |
439 return false; | 467 // See comments for IPC_USES_READWRITE for details. |
468 if (mode_ == MODE_CLIENT) { | |
469 if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { | |
470 return false; | |
471 } | |
440 } | 472 } |
473 #endif // IPC_USES_READWRITE | |
441 | 474 |
442 output_queue_.push(msg.release()); | |
443 return true; | 475 return true; |
444 } | 476 } |
445 | 477 |
446 bool Channel::ChannelImpl::Connect() { | 478 bool Channel::ChannelImpl::Connect() { |
447 if (mode_ == MODE_SERVER && uses_fifo_) { | 479 if (server_listen_pipe_ == -1 && pipe_ == -1) { |
448 if (server_listen_pipe_ == -1) { | 480 NOTREACHED() << "Must call create on a channel before calling connect"; |
449 return false; | 481 return false; |
450 } | 482 } |
483 | |
484 bool did_connect = true; | |
485 if (server_listen_pipe_ != -1) { | |
486 // Watch the pipe for connections, and turn any connections into | |
487 // active sockets. | |
451 MessageLoopForIO::current()->WatchFileDescriptor( | 488 MessageLoopForIO::current()->WatchFileDescriptor( |
452 server_listen_pipe_, | 489 server_listen_pipe_, |
453 true, | 490 true, |
454 MessageLoopForIO::WATCH_READ, | 491 MessageLoopForIO::WATCH_READ, |
455 &server_listen_connection_watcher_, | 492 &server_listen_connection_watcher_, |
456 this); | 493 this); |
457 } else { | 494 } else { |
458 if (pipe_ == -1) { | 495 did_connect = AcceptConnection(); |
459 return false; | |
460 } | |
461 MessageLoopForIO::current()->WatchFileDescriptor( | |
462 pipe_, | |
463 true, | |
464 MessageLoopForIO::WATCH_READ, | |
465 &read_watcher_, | |
466 this); | |
467 waiting_connect_ = mode_ == MODE_SERVER; | |
468 } | 496 } |
469 | 497 return did_connect; |
470 if (!waiting_connect_) | |
471 return ProcessOutgoingMessages(); | |
472 return true; | |
473 } | 498 } |
474 | 499 |
475 bool Channel::ChannelImpl::ProcessIncomingMessages() { | 500 bool Channel::ChannelImpl::ProcessIncomingMessages() { |
476 ssize_t bytes_read = 0; | 501 ssize_t bytes_read = 0; |
477 | 502 |
478 struct msghdr msg = {0}; | 503 struct msghdr msg = {0}; |
479 struct iovec iov = {input_buf_, Channel::kReadBufferSize}; | 504 struct iovec iov = {input_buf_, Channel::kReadBufferSize}; |
480 | 505 |
481 msg.msg_iovlen = 1; | 506 msg.msg_iovlen = 1; |
482 msg.msg_control = input_cmsg_buf_; | 507 msg.msg_control = input_cmsg_buf_; |
483 | 508 |
484 for (;;) { | 509 for (;;) { |
485 msg.msg_iov = &iov; | 510 msg.msg_iov = &iov; |
486 | 511 |
487 if (bytes_read == 0) { | 512 if (bytes_read == 0) { |
488 if (pipe_ == -1) | 513 if (pipe_ == -1) |
489 return false; | 514 return false; |
490 | 515 |
491 // Read from pipe. | 516 // Read from pipe. |
492 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data | 517 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data |
493 // is waiting on the pipe. | 518 // is waiting on the pipe. |
494 #if defined(IPC_USES_READWRITE) | 519 #if defined(IPC_USES_READWRITE) |
495 if (fd_pipe_ >= 0) { | 520 if (fd_pipe_ >= 0) { |
496 bytes_read = HANDLE_EINTR(read(pipe_, input_buf_, | 521 bytes_read = HANDLE_EINTR(read(pipe_, input_buf_, |
497 Channel::kReadBufferSize)); | 522 Channel::kReadBufferSize)); |
498 msg.msg_controllen = 0; | 523 msg.msg_controllen = 0; |
499 } else | 524 } else |
500 #endif | 525 #endif // IPC_USES_READWRITE |
501 { | 526 { |
502 msg.msg_controllen = sizeof(input_cmsg_buf_); | 527 msg.msg_controllen = sizeof(input_cmsg_buf_); |
503 bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); | 528 bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); |
504 } | 529 } |
505 if (bytes_read < 0) { | 530 if (bytes_read < 0) { |
506 if (errno == EAGAIN) { | 531 if (errno == EAGAIN) { |
507 return true; | 532 return true; |
508 #if defined(OS_MACOSX) | 533 #if defined(OS_MACOSX) |
509 } else if (errno == EPERM) { | 534 } else if (errno == EPERM) { |
510 // On OSX, reading from a pipe with no listener returns EPERM | 535 // On OSX, reading from a pipe with no listener returns EPERM |
511 // treat this as a special case to prevent spurious error messages | 536 // treat this as a special case to prevent spurious error messages |
512 // to the console. | 537 // to the console. |
513 return false; | 538 return false; |
514 #endif // defined(OS_MACOSX) | 539 #endif // OS_MACOSX |
515 } else if (errno == ECONNRESET || errno == EPIPE) { | 540 } else if (errno == ECONNRESET || errno == EPIPE) { |
516 return false; | 541 return false; |
517 } else { | 542 } else { |
518 PLOG(ERROR) << "pipe error (" << pipe_ << ")"; | 543 PLOG(ERROR) << "pipe error (" << pipe_ << ")"; |
519 return false; | 544 return false; |
520 } | 545 } |
521 } else if (bytes_read == 0) { | 546 } else if (bytes_read == 0) { |
522 // The pipe has closed... | 547 // The pipe has closed... |
523 return false; | 548 return false; |
524 } | 549 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
559 DCHECK(payload_len % sizeof(int) == 0); | 584 DCHECK(payload_len % sizeof(int) == 0); |
560 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 585 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
561 num_wire_fds = payload_len / 4; | 586 num_wire_fds = payload_len / 4; |
562 | 587 |
563 if (msg.msg_flags & MSG_CTRUNC) { | 588 if (msg.msg_flags & MSG_CTRUNC) { |
564 LOG(ERROR) << "SCM_RIGHTS message was truncated" | 589 LOG(ERROR) << "SCM_RIGHTS message was truncated" |
565 << " cmsg_len:" << cmsg->cmsg_len | 590 << " cmsg_len:" << cmsg->cmsg_len |
566 << " fd:" << pipe_; | 591 << " fd:" << pipe_; |
567 for (unsigned i = 0; i < num_wire_fds; ++i) | 592 for (unsigned i = 0; i < num_wire_fds; ++i) |
568 if (HANDLE_EINTR(close(wire_fds[i])) < 0) | 593 if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
569 PLOG(ERROR) << "close" << i; | 594 PLOG(ERROR) << "close " << i; |
570 return false; | 595 return false; |
571 } | 596 } |
572 break; | 597 break; |
573 } | 598 } |
574 } | 599 } |
575 } | 600 } |
576 | 601 |
577 // Process messages from input buffer. | 602 // Process messages from input buffer. |
578 const char *p; | 603 const char *p; |
579 const char *end; | 604 const char *end; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
618 int len = static_cast<int>(message_tail - p); | 643 int len = static_cast<int>(message_tail - p); |
619 Message m(p, len); | 644 Message m(p, len); |
620 const uint16 header_fds = m.header()->num_fds; | 645 const uint16 header_fds = m.header()->num_fds; |
621 if (header_fds) { | 646 if (header_fds) { |
622 // the message has file descriptors | 647 // the message has file descriptors |
623 const char* error = NULL; | 648 const char* error = NULL; |
624 if (header_fds > num_fds - fds_i) { | 649 if (header_fds > num_fds - fds_i) { |
625 // the message has been completely received, but we didn't get | 650 // the message has been completely received, but we didn't get |
626 // enough file descriptors. | 651 // enough file descriptors. |
627 #if defined(IPC_USES_READWRITE) | 652 #if defined(IPC_USES_READWRITE) |
628 if (!uses_fifo_) { | 653 char dummy; |
629 char dummy; | 654 struct iovec fd_pipe_iov = { &dummy, 1 }; |
630 struct iovec fd_pipe_iov = { &dummy, 1 }; | 655 msg.msg_iov = &fd_pipe_iov; |
631 msg.msg_iov = &fd_pipe_iov; | 656 msg.msg_controllen = sizeof(input_cmsg_buf_); |
632 msg.msg_controllen = sizeof(input_cmsg_buf_); | 657 ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); |
633 ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); | 658 if (n == 1 && msg.msg_controllen > 0) { |
634 if (n == 1 && msg.msg_controllen > 0) { | 659 for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
635 for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; | 660 cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
636 cmsg = CMSG_NXTHDR(&msg, cmsg)) { | 661 if (cmsg->cmsg_level == SOL_SOCKET && |
637 if (cmsg->cmsg_level == SOL_SOCKET && | 662 cmsg->cmsg_type == SCM_RIGHTS) { |
638 cmsg->cmsg_type == SCM_RIGHTS) { | 663 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
639 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); | 664 DCHECK(payload_len % sizeof(int) == 0); |
640 DCHECK(payload_len % sizeof(int) == 0); | 665 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
641 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 666 num_wire_fds = payload_len / 4; |
642 num_wire_fds = payload_len / 4; | |
643 | 667 |
644 if (msg.msg_flags & MSG_CTRUNC) { | 668 if (msg.msg_flags & MSG_CTRUNC) { |
645 LOG(ERROR) << "SCM_RIGHTS message was truncated" | 669 LOG(ERROR) << "SCM_RIGHTS message was truncated" |
646 << " cmsg_len:" << cmsg->cmsg_len | 670 << " cmsg_len:" << cmsg->cmsg_len |
647 << " fd:" << pipe_; | 671 << " fd:" << pipe_; |
648 for (unsigned i = 0; i < num_wire_fds; ++i) | 672 for (unsigned i = 0; i < num_wire_fds; ++i) |
649 if (HANDLE_EINTR(close(wire_fds[i])) < 0) | 673 if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
650 PLOG(ERROR) << "close" << i; | 674 PLOG(ERROR) << "close " << i; |
651 return false; | 675 return false; |
652 } | |
653 break; | |
654 } | 676 } |
677 break; | |
655 } | 678 } |
656 if (input_overflow_fds_.empty()) { | 679 } |
657 fds = wire_fds; | 680 if (input_overflow_fds_.empty()) { |
658 num_fds = num_wire_fds; | 681 fds = wire_fds; |
659 } else { | 682 num_fds = num_wire_fds; |
660 if (num_wire_fds > 0) { | 683 } else { |
661 const size_t prev_size = input_overflow_fds_.size(); | 684 if (num_wire_fds > 0) { |
662 input_overflow_fds_.resize(prev_size + num_wire_fds); | 685 const size_t prev_size = input_overflow_fds_.size(); |
663 memcpy(&input_overflow_fds_[prev_size], wire_fds, | 686 input_overflow_fds_.resize(prev_size + num_wire_fds); |
664 num_wire_fds * sizeof(int)); | 687 memcpy(&input_overflow_fds_[prev_size], wire_fds, |
665 } | 688 num_wire_fds * sizeof(int)); |
666 fds = &input_overflow_fds_[0]; | |
667 num_fds = input_overflow_fds_.size(); | |
668 } | 689 } |
690 fds = &input_overflow_fds_[0]; | |
691 num_fds = input_overflow_fds_.size(); | |
669 } | 692 } |
670 } | 693 } |
671 if (header_fds > num_fds - fds_i) | 694 if (header_fds > num_fds - fds_i) |
672 #endif | 695 #endif // IPC_USES_READWRITE |
673 error = "Message needs unreceived descriptors"; | 696 error = "Message needs unreceived descriptors"; |
674 } | 697 } |
675 | 698 |
676 if (header_fds > | 699 if (header_fds > |
677 FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE) { | 700 FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE) { |
678 // There are too many descriptors in this message | 701 // There are too many descriptors in this message |
679 error = "Message requires an excessive number of descriptors"; | 702 error = "Message requires an excessive number of descriptors"; |
680 } | 703 } |
681 | 704 |
682 if (error) { | 705 if (error) { |
683 LOG(WARNING) << error | 706 LOG(WARNING) << error |
684 << " channel:" << this | 707 << " channel:" << this |
685 << " message-type:" << m.type() | 708 << " message-type:" << m.type() |
686 << " header()->num_fds:" << header_fds | 709 << " header()->num_fds:" << header_fds |
687 << " num_fds:" << num_fds | 710 << " num_fds:" << num_fds |
688 << " fds_i:" << fds_i; | 711 << " fds_i:" << fds_i; |
689 #if defined(CHROMIUM_SELINUX) | 712 #if defined(CHROMIUM_SELINUX) |
690 LOG(WARNING) << "In the case of SELinux this can be caused when " | 713 LOG(WARNING) << "In the case of SELinux this can be caused when " |
691 "using a --user-data-dir to which the default " | 714 "using a --user-data-dir to which the default " |
692 "policy doesn't give the renderer access to. "; | 715 "policy doesn't give the renderer access to. "; |
693 #endif | 716 #endif // CHROMIUM_SELINUX |
694 // close the existing file descriptors so that we don't leak them | 717 // close the existing file descriptors so that we don't leak them |
695 for (unsigned i = fds_i; i < num_fds; ++i) | 718 for (unsigned i = fds_i; i < num_fds; ++i) |
696 if (HANDLE_EINTR(close(fds[i])) < 0) | 719 if (HANDLE_EINTR(close(fds[i])) < 0) |
697 PLOG(ERROR) << "close" << i; | 720 PLOG(ERROR) << "close " << i; |
698 input_overflow_fds_.clear(); | 721 input_overflow_fds_.clear(); |
699 // abort the connection | 722 // abort the connection |
700 return false; | 723 return false; |
701 } | 724 } |
702 | 725 |
703 m.file_descriptor_set()->SetDescriptors( | 726 m.file_descriptor_set()->SetDescriptors( |
704 &fds[fds_i], header_fds); | 727 &fds[fds_i], header_fds); |
705 fds_i += header_fds; | 728 fds_i += header_fds; |
706 } | 729 } |
707 DVLOG(2) << "received message on channel @" << this | 730 DVLOG(2) << "received message on channel @" << this |
708 << " with type " << m.type() << " on fd " << pipe_; | 731 << " with type " << m.type() << " on fd " << pipe_; |
709 if (m.routing_id() == MSG_ROUTING_NONE && | 732 if (IsHelloMessage(&m)) { |
710 m.type() == HELLO_MESSAGE_TYPE) { | |
711 // The Hello message contains only the process id. | 733 // The Hello message contains only the process id. |
712 void *iter = NULL; | 734 void *iter = NULL; |
713 int pid; | 735 int pid; |
714 if (!m.ReadInt(&iter, &pid)) { | 736 if (!m.ReadInt(&iter, &pid)) { |
715 NOTREACHED(); | 737 NOTREACHED(); |
716 } | 738 } |
717 #if defined(IPC_USES_READWRITE) | 739 #if defined(IPC_USES_READWRITE) |
718 if (mode_ == MODE_SERVER && !uses_fifo_) { | 740 if (mode_ == MODE_SERVER) { |
719 // On non-Mac, the Hello message from the client to the server | 741 // With IPC_USES_READWRITE, the Hello message from the client to the |
720 // also contains the fd_pipe_, which will be used for all | 742 // server also contains the fd_pipe_, which will be used for all |
721 // subsequent file descriptor passing. | 743 // subsequent file descriptor passing. |
722 DCHECK_EQ(m.file_descriptor_set()->size(), 1U); | 744 DCHECK_EQ(m.file_descriptor_set()->size(), 1U); |
723 base::FileDescriptor descriptor; | 745 base::FileDescriptor descriptor; |
724 if (!m.ReadFileDescriptor(&iter, &descriptor)) { | 746 if (!m.ReadFileDescriptor(&iter, &descriptor)) { |
725 NOTREACHED(); | 747 NOTREACHED(); |
726 } | 748 } |
727 fd_pipe_ = descriptor.fd; | 749 fd_pipe_ = descriptor.fd; |
728 CHECK(descriptor.auto_close); | 750 CHECK(descriptor.auto_close); |
729 } | 751 } |
730 #endif | 752 #endif // IPC_USES_READWRITE |
731 listener_->OnChannelConnected(pid); | 753 listener_->OnChannelConnected(pid); |
732 } else { | 754 } else { |
733 listener_->OnMessageReceived(m); | 755 listener_->OnMessageReceived(m); |
734 } | 756 } |
735 p = message_tail; | 757 p = message_tail; |
736 } else { | 758 } else { |
737 // Last message is partial. | 759 // Last message is partial. |
738 break; | 760 break; |
739 } | 761 } |
740 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); | 762 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
741 fds_i = 0; | 763 fds_i = 0; |
742 fds = &input_overflow_fds_[0]; | 764 fds = &input_overflow_fds_[0]; |
743 num_fds = input_overflow_fds_.size(); | 765 num_fds = input_overflow_fds_.size(); |
744 } | 766 } |
745 input_overflow_buf_.assign(p, end - p); | 767 input_overflow_buf_.assign(p, end - p); |
746 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); | 768 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
747 | 769 |
748 // When the input data buffer is empty, the overflow fds should be too. If | 770 // When the input data buffer is empty, the overflow fds should be too. If |
749 // this is not the case, we probably have a rogue renderer which is trying | 771 // this is not the case, we probably have a rogue renderer which is trying |
750 // to fill our descriptor table. | 772 // to fill our descriptor table. |
751 if (input_overflow_buf_.empty() && !input_overflow_fds_.empty()) { | 773 if (input_overflow_buf_.empty() && !input_overflow_fds_.empty()) { |
752 // We close these descriptors in Close() | 774 // We close these descriptors in Close() |
753 return false; | 775 return false; |
754 } | 776 } |
755 | 777 |
756 bytes_read = 0; // Get more data. | 778 bytes_read = 0; // Get more data. |
757 } | 779 } |
758 | |
759 return true; | |
760 } | 780 } |
761 | 781 |
762 bool Channel::ChannelImpl::ProcessOutgoingMessages() { | 782 bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
763 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's | 783 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
764 // no connection? | 784 // no connection? |
765 is_blocked_on_write_ = false; | 785 if (output_queue_.empty()) |
786 return true; | |
766 | 787 |
767 if (output_queue_.empty()) { | 788 if (pipe_ == -1) |
768 return true; | |
769 } | |
770 | |
771 if (pipe_ == -1) { | |
772 return false; | 789 return false; |
773 } | |
774 | 790 |
775 // Write out all the messages we can till the write blocks or there are no | 791 // Write out all the messages we can till the write blocks or there are no |
776 // more outgoing messages. | 792 // more outgoing messages. |
777 while (!output_queue_.empty()) { | 793 while (!output_queue_.empty()) { |
778 Message* msg = output_queue_.front(); | 794 Message* msg = output_queue_.front(); |
779 | 795 |
780 #if defined(IPC_USES_READWRITE) | |
781 scoped_ptr<Message> hello; | |
782 if (remote_fd_pipe_ != -1 && | |
783 msg->routing_id() == MSG_ROUTING_NONE && | |
784 msg->type() == HELLO_MESSAGE_TYPE) { | |
785 hello.reset(new Message(MSG_ROUTING_NONE, | |
786 HELLO_MESSAGE_TYPE, | |
787 IPC::Message::PRIORITY_NORMAL)); | |
788 void* iter = NULL; | |
789 int pid; | |
790 if (!msg->ReadInt(&iter, &pid) || | |
791 !hello->WriteInt(pid)) { | |
792 NOTREACHED(); | |
793 } | |
794 DCHECK_EQ(hello->size(), msg->size()); | |
795 if (!hello->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, | |
796 false))) { | |
797 NOTREACHED(); | |
798 } | |
799 msg = hello.get(); | |
800 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | |
801 } | |
802 #endif | |
803 | |
804 size_t amt_to_write = msg->size() - message_send_bytes_written_; | 796 size_t amt_to_write = msg->size() - message_send_bytes_written_; |
805 DCHECK(amt_to_write != 0); | 797 DCHECK(amt_to_write != 0); |
806 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + | 798 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + |
807 message_send_bytes_written_; | 799 message_send_bytes_written_; |
808 | 800 |
809 struct msghdr msgh = {0}; | 801 struct msghdr msgh = {0}; |
810 struct iovec iov = {const_cast<char*>(out_bytes), amt_to_write}; | 802 struct iovec iov = {const_cast<char*>(out_bytes), amt_to_write}; |
811 msgh.msg_iov = &iov; | 803 msgh.msg_iov = &iov; |
812 msgh.msg_iovlen = 1; | 804 msgh.msg_iovlen = 1; |
813 char buf[CMSG_SPACE( | 805 char buf[CMSG_SPACE( |
(...skipping 27 matching lines...) Expand all Loading... | |
841 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds); | 833 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds); |
842 msg->file_descriptor_set()->GetDescriptors( | 834 msg->file_descriptor_set()->GetDescriptors( |
843 reinterpret_cast<int*>(CMSG_DATA(cmsg))); | 835 reinterpret_cast<int*>(CMSG_DATA(cmsg))); |
844 msgh.msg_controllen = cmsg->cmsg_len; | 836 msgh.msg_controllen = cmsg->cmsg_len; |
845 | 837 |
846 // DCHECK_LE above already checks that | 838 // DCHECK_LE above already checks that |
847 // num_fds < MAX_DESCRIPTORS_PER_MESSAGE so no danger of overflow. | 839 // num_fds < MAX_DESCRIPTORS_PER_MESSAGE so no danger of overflow. |
848 msg->header()->num_fds = static_cast<uint16>(num_fds); | 840 msg->header()->num_fds = static_cast<uint16>(num_fds); |
849 | 841 |
850 #if defined(IPC_USES_READWRITE) | 842 #if defined(IPC_USES_READWRITE) |
851 if (!uses_fifo_ && | 843 if (!IsHelloMessage(msg)) { |
852 (msg->routing_id() != MSG_ROUTING_NONE || | |
853 msg->type() != HELLO_MESSAGE_TYPE)) { | |
854 // Only the Hello message sends the file descriptor with the message. | 844 // Only the Hello message sends the file descriptor with the message. |
855 // Subsequently, we can send file descriptors on the dedicated | 845 // Subsequently, we can send file descriptors on the dedicated |
856 // fd_pipe_ which makes Seccomp sandbox operation more efficient. | 846 // fd_pipe_ which makes Seccomp sandbox operation more efficient. |
857 struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; | 847 struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; |
858 msgh.msg_iov = &fd_pipe_iov; | 848 msgh.msg_iov = &fd_pipe_iov; |
859 fd_written = fd_pipe_; | 849 fd_written = fd_pipe_; |
860 bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); | 850 bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); |
861 msgh.msg_iov = &iov; | 851 msgh.msg_iov = &iov; |
862 msgh.msg_controllen = 0; | 852 msgh.msg_controllen = 0; |
863 if (bytes_written > 0) { | 853 if (bytes_written > 0) { |
864 msg->file_descriptor_set()->CommitAll(); | 854 msg->file_descriptor_set()->CommitAll(); |
865 } | 855 } |
866 } | 856 } |
867 #endif | 857 #endif // IPC_USES_READWRITE |
868 } | 858 } |
869 | 859 |
870 if (bytes_written == 1) { | 860 if (bytes_written == 1) { |
871 fd_written = pipe_; | 861 fd_written = pipe_; |
872 #if defined(IPC_USES_READWRITE) | 862 #if defined(IPC_USES_READWRITE) |
873 if (mode_ != MODE_SERVER && !uses_fifo_ && | 863 if (mode_ != MODE_SERVER && IsHelloMessage(msg)) { |
874 msg->routing_id() == MSG_ROUTING_NONE && | |
875 msg->type() == HELLO_MESSAGE_TYPE) { | |
876 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | 864 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
877 } | 865 } |
878 if (!uses_fifo_ && !msgh.msg_controllen) { | 866 if (!msgh.msg_controllen) { |
879 bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); | 867 bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); |
880 } else | 868 } else |
881 #endif | 869 #endif // IPC_USES_READWRITE |
882 { | 870 { |
883 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); | 871 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); |
884 } | 872 } |
885 } | 873 } |
886 if (bytes_written > 0) | 874 if (bytes_written > 0) |
887 msg->file_descriptor_set()->CommitAll(); | 875 msg->file_descriptor_set()->CommitAll(); |
888 | 876 |
889 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { | 877 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { |
890 #if defined(OS_MACOSX) | 878 #if defined(OS_MACOSX) |
891 // On OSX writing to a pipe with no listener returns EPERM. | 879 // On OSX writing to a pipe with no listener returns EPERM. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
933 return true; | 921 return true; |
934 } | 922 } |
935 | 923 |
936 bool Channel::ChannelImpl::Send(Message* message) { | 924 bool Channel::ChannelImpl::Send(Message* message) { |
937 DVLOG(2) << "sending message @" << message << " on channel @" << this | 925 DVLOG(2) << "sending message @" << message << " on channel @" << this |
938 << " with type " << message->type() | 926 << " with type " << message->type() |
939 << " (" << output_queue_.size() << " in queue)"; | 927 << " (" << output_queue_.size() << " in queue)"; |
940 | 928 |
941 #ifdef IPC_MESSAGE_LOG_ENABLED | 929 #ifdef IPC_MESSAGE_LOG_ENABLED |
942 Logging::GetInstance()->OnSendMessage(message, ""); | 930 Logging::GetInstance()->OnSendMessage(message, ""); |
943 #endif | 931 #endif // IPC_MESSAGE_LOG_ENABLED |
944 | 932 |
945 output_queue_.push(message); | 933 output_queue_.push(message); |
946 if (!waiting_connect_) { | 934 if (!is_blocked_on_write_ && !waiting_connect_) { |
947 if (!is_blocked_on_write_) { | 935 return ProcessOutgoingMessages(); |
948 if (!ProcessOutgoingMessages()) | |
949 return false; | |
950 } | |
951 } | 936 } |
952 | 937 |
953 return true; | 938 return true; |
954 } | 939 } |
955 | 940 |
956 int Channel::ChannelImpl::GetClientFileDescriptor() const { | 941 int Channel::ChannelImpl::GetClientFileDescriptor() const { |
957 return client_pipe_; | 942 return client_pipe_; |
958 } | 943 } |
959 | 944 |
960 // Called by libevent when we can read from th pipe without blocking. | 945 bool Channel::ChannelImpl::AcceptsConnections() const { |
946 return server_listen_pipe_ != -1; | |
947 } | |
948 | |
949 bool Channel::ChannelImpl::HasAcceptedConnection() const { | |
950 return AcceptsConnections() && pipe_ != -1; | |
951 } | |
952 | |
953 void Channel::ChannelImpl::ResetToAcceptingConnectionState() { | |
954 if (pipe_ == -1) | |
955 return; | |
956 | |
957 // Unregister libevent for the unix domain socket and close it. | |
958 read_watcher_.StopWatchingFileDescriptor(); | |
959 write_watcher_.StopWatchingFileDescriptor(); | |
960 if (HANDLE_EINTR(close(pipe_)) < 0) | |
961 PLOG(ERROR) << "close pipe_ " << pipe_name_; | |
962 pipe_ = -1; | |
963 #if defined(IPC_USES_READWRITE) | |
964 if (fd_pipe_ != -1) { | |
965 if (HANDLE_EINTR(close(fd_pipe_)) < 0) | |
966 PLOG(ERROR) << "close fd_pipe_ " << pipe_name_; | |
967 fd_pipe_ = -1; | |
968 } | |
969 if (remote_fd_pipe_ != -1) { | |
970 if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) | |
971 PLOG(ERROR) << "close remote_fd_pipe_ " << pipe_name_; | |
972 remote_fd_pipe_ = -1; | |
973 } | |
974 #endif // IPC_USES_READWRITE | |
975 | |
976 while (!output_queue_.empty()) { | |
977 Message* m = output_queue_.front(); | |
978 output_queue_.pop(); | |
979 delete m; | |
980 } | |
981 | |
982 // Close any outstanding, received file descriptors. | |
983 for (std::vector<int>::iterator | |
984 i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { | |
985 if (HANDLE_EINTR(close(*i)) < 0) | |
986 PLOG(ERROR) << "close"; | |
987 } | |
988 input_overflow_fds_.clear(); | |
989 } | |
990 | |
991 // Called by libevent when we can read from the pipe without blocking. | |
961 void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { | 992 void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
962 bool send_server_hello_msg = false; | 993 bool send_server_hello_msg = false; |
963 if (waiting_connect_ && mode_ == MODE_SERVER) { | 994 if (fd == server_listen_pipe_) { |
964 if (uses_fifo_) { | 995 int new_pipe = 0; |
965 if (!ServerAcceptFifoConnection(server_listen_pipe_, &pipe_)) { | 996 if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe)) { |
966 Close(); | 997 Close(); |
967 } | 998 listener_->OnChannelListenError(); |
999 } | |
968 | 1000 |
969 // No need to watch the listening socket any longer since only one client | 1001 if (pipe_ != -1) { |
970 // can connect. So unregister with libevent. | 1002 // We already have a connection. We only handle one at a time. |
971 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | 1003 // close our new descriptor. |
1004 if (HANDLE_EINTR(shutdown(new_pipe, SHUT_RDWR)) < 0) | |
1005 PLOG(ERROR) << "shutdown " << pipe_name_; | |
1006 if (HANDLE_EINTR(close(new_pipe)) < 0) | |
1007 PLOG(ERROR) << "close " << pipe_name_; | |
1008 listener_->OnChannelDenied(); | |
1009 return; | |
1010 } | |
1011 pipe_ = new_pipe; | |
972 | 1012 |
973 // Start watching our end of the socket. | 1013 if (!AcceptConnection()) { |
974 MessageLoopForIO::current()->WatchFileDescriptor( | 1014 NOTREACHED() << "AcceptConnection should not fail on server"; |
975 pipe_, | 1015 } |
976 true, | 1016 send_server_hello_msg = true; |
977 MessageLoopForIO::WATCH_READ, | 1017 waiting_connect_ = false; |
978 &read_watcher_, | 1018 } else if (fd == pipe_) { |
979 this); | 1019 if (waiting_connect_ && mode_ == MODE_SERVER) { |
980 | 1020 send_server_hello_msg = true; |
981 waiting_connect_ = false; | |
982 } else { | |
983 // In the case of a socketpair() the server starts listening on its end | |
984 // of the pipe in Connect(). | |
985 waiting_connect_ = false; | 1021 waiting_connect_ = false; |
986 } | 1022 } |
987 send_server_hello_msg = true; | |
988 } | |
989 | |
990 if (!waiting_connect_ && fd == pipe_) { | |
991 if (!ProcessIncomingMessages()) { | 1023 if (!ProcessIncomingMessages()) { |
992 Close(); | 1024 ClosePipeOnError(); |
993 listener_->OnChannelError(); | |
994 // The OnChannelError() call may delete this, so we need to exit now. | |
995 return; | |
996 } | 1025 } |
1026 } else { | |
1027 NOTREACHED() << "Unknown pipe " << fd; | |
997 } | 1028 } |
998 | 1029 |
999 // If we're a server and handshaking, then we want to make sure that we | 1030 // If we're a server and handshaking, then we want to make sure that we |
1000 // only send our handshake message after we've processed the client's. | 1031 // only send our handshake message after we've processed the client's. |
1001 // This gives us a chance to kill the client if the incoming handshake | 1032 // This gives us a chance to kill the client if the incoming handshake |
1002 // is invalid. | 1033 // is invalid. |
1003 if (send_server_hello_msg) { | 1034 if (send_server_hello_msg) { |
1004 ProcessOutgoingMessages(); | 1035 ProcessOutgoingMessages(); |
1005 } | 1036 } |
1006 } | 1037 } |
1007 | 1038 |
1008 // Called by libevent when we can write to the pipe without blocking. | 1039 // Called by libevent when we can write to the pipe without blocking. |
1009 void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { | 1040 void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { |
1041 DCHECK(fd == pipe_); | |
1042 is_blocked_on_write_ = false; | |
1010 if (!ProcessOutgoingMessages()) { | 1043 if (!ProcessOutgoingMessages()) { |
1044 ClosePipeOnError(); | |
1045 } | |
1046 } | |
1047 | |
1048 bool Channel::ChannelImpl::AcceptConnection() { | |
1049 MessageLoopForIO::current()->WatchFileDescriptor(pipe_, | |
1050 true, | |
1051 MessageLoopForIO::WATCH_READ, | |
1052 &read_watcher_, | |
1053 this); | |
1054 QueueHelloMessage(); | |
1055 | |
1056 if (mode_ == MODE_CLIENT) { | |
1057 // If we are a client we want to send a hello message out immediately. | |
1058 // In server mode we will send a hello message when we receive one from a | |
1059 // client. | |
1060 waiting_connect_ = false; | |
1061 return ProcessOutgoingMessages(); | |
1062 } else { | |
1063 waiting_connect_ = true; | |
1064 return true; | |
1065 } | |
1066 } | |
1067 | |
1068 void Channel::ChannelImpl::ClosePipeOnError() { | |
1069 if (HasAcceptedConnection()) { | |
1070 ResetToAcceptingConnectionState(); | |
1071 listener_->OnChannelError(); | |
1072 } else { | |
1011 Close(); | 1073 Close(); |
1012 listener_->OnChannelError(); | 1074 if (AcceptsConnections()) { |
1075 listener_->OnChannelListenError(); | |
1076 } else { | |
1077 listener_->OnChannelError(); | |
1078 } | |
1013 } | 1079 } |
1014 } | 1080 } |
1015 | 1081 |
1082 void Channel::ChannelImpl::QueueHelloMessage() { | |
1083 // Create the Hello message | |
1084 scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, | |
1085 HELLO_MESSAGE_TYPE, | |
1086 IPC::Message::PRIORITY_NORMAL)); | |
1087 | |
1088 if (!msg->WriteInt(base::GetCurrentProcId())) { | |
1089 NOTREACHED() << "Unable to pickle hello message proc id"; | |
1090 } | |
1091 #if defined(IPC_USES_READWRITE) | |
1092 scoped_ptr<Message> hello; | |
1093 if (remote_fd_pipe_ != -1) { | |
1094 if (!msg->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, | |
1095 false))) { | |
1096 NOTREACHED() << "Unable to pickle hello message file descriptors"; | |
1097 } | |
1098 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | |
1099 } | |
1100 #endif // IPC_USES_READWRITE | |
1101 output_queue_.push(msg.release()); | |
1102 } | |
1103 | |
1104 bool Channel::ChannelImpl::IsHelloMessage(const Message* m) const { | |
1105 return m->routing_id() == MSG_ROUTING_NONE && m->type() == HELLO_MESSAGE_TYPE; | |
1106 } | |
1107 | |
1016 void Channel::ChannelImpl::Close() { | 1108 void Channel::ChannelImpl::Close() { |
1017 // Close can be called multiple time, so we need to make sure we're | 1109 // Close can be called multiple time, so we need to make sure we're |
1018 // idempotent. | 1110 // idempotent. |
1019 | 1111 |
1020 // Unregister libevent for the listening socket and close it. | 1112 ResetToAcceptingConnectionState(); |
1021 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | |
1022 | 1113 |
1114 if (must_unlink_) { | |
1115 unlink(pipe_name_.c_str()); | |
1116 must_unlink_ = false; | |
1117 } | |
1023 if (server_listen_pipe_ != -1) { | 1118 if (server_listen_pipe_ != -1) { |
1024 if (HANDLE_EINTR(close(server_listen_pipe_)) < 0) | 1119 if (HANDLE_EINTR(close(server_listen_pipe_)) < 0) |
1025 PLOG(ERROR) << "close " << server_listen_pipe_; | 1120 PLOG(ERROR) << "close " << server_listen_pipe_; |
1026 server_listen_pipe_ = -1; | 1121 server_listen_pipe_ = -1; |
1122 // Unregister libevent for the listening socket and close it. | |
1123 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | |
1027 } | 1124 } |
1028 | 1125 |
1029 // Unregister libevent for the FIFO and close it. | |
1030 read_watcher_.StopWatchingFileDescriptor(); | |
1031 write_watcher_.StopWatchingFileDescriptor(); | |
1032 if (pipe_ != -1) { | |
1033 if (HANDLE_EINTR(close(pipe_)) < 0) | |
1034 PLOG(ERROR) << "close " << pipe_; | |
1035 pipe_ = -1; | |
1036 } | |
1037 if (client_pipe_ != -1) { | 1126 if (client_pipe_ != -1) { |
1038 PipeMap::GetInstance()->RemoveAndClose(pipe_name_); | 1127 PipeMap::GetInstance()->RemoveAndClose(pipe_name_); |
1039 client_pipe_ = -1; | 1128 client_pipe_ = -1; |
1040 } | 1129 } |
1041 #if defined(IPC_USES_READWRITE) | |
1042 if (fd_pipe_ != -1) { | |
1043 if (HANDLE_EINTR(close(fd_pipe_)) < 0) | |
1044 PLOG(ERROR) << "close " << fd_pipe_; | |
1045 fd_pipe_ = -1; | |
1046 } | |
1047 if (remote_fd_pipe_ != -1) { | |
1048 if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) | |
1049 PLOG(ERROR) << "close " << remote_fd_pipe_; | |
1050 remote_fd_pipe_ = -1; | |
1051 } | |
1052 #endif | |
1053 | |
1054 if (uses_fifo_) { | |
1055 // Unlink the FIFO | |
1056 unlink(pipe_name_.c_str()); | |
1057 } | |
1058 | |
1059 while (!output_queue_.empty()) { | |
1060 Message* m = output_queue_.front(); | |
1061 output_queue_.pop(); | |
1062 delete m; | |
1063 } | |
1064 | |
1065 // Close any outstanding, received file descriptors | |
1066 for (std::vector<int>::iterator | |
1067 i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { | |
1068 if (HANDLE_EINTR(close(*i)) < 0) | |
1069 PLOG(ERROR) << "close " << *i; | |
1070 } | |
1071 input_overflow_fds_.clear(); | |
1072 } | 1130 } |
1073 | 1131 |
1074 //------------------------------------------------------------------------------ | 1132 //------------------------------------------------------------------------------ |
1075 // Channel's methods simply call through to ChannelImpl. | 1133 // Channel's methods simply call through to ChannelImpl. |
1076 Channel::Channel(const IPC::ChannelHandle& channel_handle, Mode mode, | 1134 Channel::Channel(const IPC::ChannelHandle& channel_handle, Mode mode, |
1077 Listener* listener) | 1135 Listener* listener) |
1078 : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { | 1136 : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { |
1079 } | 1137 } |
1080 | 1138 |
1081 Channel::~Channel() { | 1139 Channel::~Channel() { |
(...skipping 13 matching lines...) Expand all Loading... | |
1095 } | 1153 } |
1096 | 1154 |
1097 bool Channel::Send(Message* message) { | 1155 bool Channel::Send(Message* message) { |
1098 return channel_impl_->Send(message); | 1156 return channel_impl_->Send(message); |
1099 } | 1157 } |
1100 | 1158 |
1101 int Channel::GetClientFileDescriptor() const { | 1159 int Channel::GetClientFileDescriptor() const { |
1102 return channel_impl_->GetClientFileDescriptor(); | 1160 return channel_impl_->GetClientFileDescriptor(); |
1103 } | 1161 } |
1104 | 1162 |
1163 bool Channel::AcceptsConnections() const { | |
1164 return channel_impl_->AcceptsConnections(); | |
1165 } | |
1166 | |
1167 bool Channel::HasAcceptedConnection() const { | |
1168 return channel_impl_->HasAcceptedConnection(); | |
1169 } | |
1170 | |
1171 void Channel::ResetToAcceptingConnectionState() { | |
1172 channel_impl_->ResetToAcceptingConnectionState(); | |
1173 } | |
1174 | |
1105 } // namespace IPC | 1175 } // namespace IPC |
OLD | NEW |