OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/socket.h> | 10 #include <sys/socket.h> |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 #endif // OS_LINUX | 178 #endif // OS_LINUX |
179 | 179 |
180 ChannelPosix::ChannelPosix(const IPC::ChannelHandle& channel_handle, | 180 ChannelPosix::ChannelPosix(const IPC::ChannelHandle& channel_handle, |
181 Mode mode, Listener* listener) | 181 Mode mode, Listener* listener) |
182 : ChannelReader(listener), | 182 : ChannelReader(listener), |
183 mode_(mode), | 183 mode_(mode), |
184 peer_pid_(base::kNullProcessId), | 184 peer_pid_(base::kNullProcessId), |
185 is_blocked_on_write_(false), | 185 is_blocked_on_write_(false), |
186 waiting_connect_(true), | 186 waiting_connect_(true), |
187 message_send_bytes_written_(0), | 187 message_send_bytes_written_(0), |
188 server_listen_pipe_(-1), | |
189 pipe_(-1), | |
190 client_pipe_(-1), | |
191 #if defined(IPC_USES_READWRITE) | |
192 fd_pipe_(-1), | |
193 remote_fd_pipe_(-1), | |
194 #endif // IPC_USES_READWRITE | |
195 pipe_name_(channel_handle.name), | 188 pipe_name_(channel_handle.name), |
196 must_unlink_(false) { | 189 must_unlink_(false) { |
197 memset(input_cmsg_buf_, 0, sizeof(input_cmsg_buf_)); | 190 memset(input_cmsg_buf_, 0, sizeof(input_cmsg_buf_)); |
198 if (!CreatePipe(channel_handle)) { | 191 if (!CreatePipe(channel_handle)) { |
199 // The pipe may have been closed already. | 192 // The pipe may have been closed already. |
200 const char *modestr = (mode_ & MODE_SERVER_FLAG) ? "server" : "client"; | 193 const char *modestr = (mode_ & MODE_SERVER_FLAG) ? "server" : "client"; |
201 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name | 194 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name |
202 << "\" in " << modestr << " mode"; | 195 << "\" in " << modestr << " mode"; |
203 } | 196 } |
204 } | 197 } |
(...skipping 21 matching lines...) Expand all Loading... |
226 } | 219 } |
227 | 220 |
228 *fd1 = pipe_fds[0]; | 221 *fd1 = pipe_fds[0]; |
229 *fd2 = pipe_fds[1]; | 222 *fd2 = pipe_fds[1]; |
230 | 223 |
231 return true; | 224 return true; |
232 } | 225 } |
233 | 226 |
234 bool ChannelPosix::CreatePipe( | 227 bool ChannelPosix::CreatePipe( |
235 const IPC::ChannelHandle& channel_handle) { | 228 const IPC::ChannelHandle& channel_handle) { |
236 DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); | 229 DCHECK(!server_listen_pipe_.is_valid() && !pipe_.is_valid()); |
237 | 230 |
238 // Four possible cases: | 231 // Four possible cases: |
239 // 1) It's a channel wrapping a pipe that is given to us. | 232 // 1) It's a channel wrapping a pipe that is given to us. |
240 // 2) It's for a named channel, so we create it. | 233 // 2) It's for a named channel, so we create it. |
241 // 3) It's for a client that we implement ourself. This is used | 234 // 3) It's for a client that we implement ourself. This is used |
242 // in single-process unittesting. | 235 // in single-process unittesting. |
243 // 4) It's the initial IPC channel: | 236 // 4) It's the initial IPC channel: |
244 // 4a) Client side: Pull the pipe out of the GlobalDescriptors set. | 237 // 4a) Client side: Pull the pipe out of the GlobalDescriptors set. |
245 // 4b) Server side: create the pipe. | 238 // 4b) Server side: create the pipe. |
246 | 239 |
247 int local_pipe = -1; | 240 base::ScopedFD local_pipe; |
248 if (channel_handle.socket.fd != -1) { | 241 if (channel_handle.socket.fd != -1) { |
249 // Case 1 from comment above. | 242 // Case 1 from comment above. |
250 local_pipe = channel_handle.socket.fd; | 243 local_pipe.reset(channel_handle.socket.fd); |
251 #if defined(IPC_USES_READWRITE) | 244 #if defined(IPC_USES_READWRITE) |
252 // Test the socket passed into us to make sure it is nonblocking. | 245 // Test the socket passed into us to make sure it is nonblocking. |
253 // We don't want to call read/write on a blocking socket. | 246 // We don't want to call read/write on a blocking socket. |
254 int value = fcntl(local_pipe, F_GETFL); | 247 int value = fcntl(local_pipe.get(), F_GETFL); |
255 if (value == -1) { | 248 if (value == -1) { |
256 PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; | 249 PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; |
257 return false; | 250 return false; |
258 } | 251 } |
259 if (!(value & O_NONBLOCK)) { | 252 if (!(value & O_NONBLOCK)) { |
260 LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; | 253 LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; |
261 return false; | 254 return false; |
262 } | 255 } |
263 #endif // IPC_USES_READWRITE | 256 #endif // IPC_USES_READWRITE |
264 } else if (mode_ & MODE_NAMED_FLAG) { | 257 } else if (mode_ & MODE_NAMED_FLAG) { |
265 // Case 2 from comment above. | 258 // Case 2 from comment above. |
| 259 int local_pipe_fd = -1; |
| 260 |
266 if (mode_ & MODE_SERVER_FLAG) { | 261 if (mode_ & MODE_SERVER_FLAG) { |
267 if (!CreateServerUnixDomainSocket(base::FilePath(pipe_name_), | 262 if (!CreateServerUnixDomainSocket(base::FilePath(pipe_name_), |
268 &local_pipe)) { | 263 &local_pipe_fd)) { |
269 return false; | 264 return false; |
270 } | 265 } |
| 266 |
271 must_unlink_ = true; | 267 must_unlink_ = true; |
272 } else if (mode_ & MODE_CLIENT_FLAG) { | 268 } else if (mode_ & MODE_CLIENT_FLAG) { |
273 if (!CreateClientUnixDomainSocket(base::FilePath(pipe_name_), | 269 if (!CreateClientUnixDomainSocket(base::FilePath(pipe_name_), |
274 &local_pipe)) { | 270 &local_pipe_fd)) { |
275 return false; | 271 return false; |
276 } | 272 } |
277 } else { | 273 } else { |
278 LOG(ERROR) << "Bad mode: " << mode_; | 274 LOG(ERROR) << "Bad mode: " << mode_; |
279 return false; | 275 return false; |
280 } | 276 } |
| 277 |
| 278 local_pipe.reset(local_pipe_fd); |
281 } else { | 279 } else { |
282 local_pipe = PipeMap::GetInstance()->Lookup(pipe_name_); | 280 local_pipe.reset(PipeMap::GetInstance()->Lookup(pipe_name_)); |
283 if (mode_ & MODE_CLIENT_FLAG) { | 281 if (mode_ & MODE_CLIENT_FLAG) { |
284 if (local_pipe != -1) { | 282 if (local_pipe.is_valid()) { |
285 // Case 3 from comment above. | 283 // Case 3 from comment above. |
286 // We only allow one connection. | 284 // We only allow one connection. |
287 local_pipe = HANDLE_EINTR(dup(local_pipe)); | 285 local_pipe.reset(HANDLE_EINTR(dup(local_pipe.release()))); |
288 PipeMap::GetInstance()->Remove(pipe_name_); | 286 PipeMap::GetInstance()->Remove(pipe_name_); |
289 } else { | 287 } else { |
290 // Case 4a from comment above. | 288 // Case 4a from comment above. |
291 // Guard against inappropriate reuse of the initial IPC channel. If | 289 // Guard against inappropriate reuse of the initial IPC channel. If |
292 // an IPC channel closes and someone attempts to reuse it by name, the | 290 // an IPC channel closes and someone attempts to reuse it by name, the |
293 // initial channel must not be recycled here. http://crbug.com/26754. | 291 // initial channel must not be recycled here. http://crbug.com/26754. |
294 static bool used_initial_channel = false; | 292 static bool used_initial_channel = false; |
295 if (used_initial_channel) { | 293 if (used_initial_channel) { |
296 LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " | 294 LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " |
297 << pipe_name_; | 295 << pipe_name_; |
298 return false; | 296 return false; |
299 } | 297 } |
300 used_initial_channel = true; | 298 used_initial_channel = true; |
301 | 299 |
302 local_pipe = | 300 local_pipe.reset( |
303 base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); | 301 base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel)); |
304 } | 302 } |
305 } else if (mode_ & MODE_SERVER_FLAG) { | 303 } else if (mode_ & MODE_SERVER_FLAG) { |
306 // Case 4b from comment above. | 304 // Case 4b from comment above. |
307 if (local_pipe != -1) { | 305 if (local_pipe.is_valid()) { |
308 LOG(ERROR) << "Server already exists for " << pipe_name_; | 306 LOG(ERROR) << "Server already exists for " << pipe_name_; |
| 307 // This is a client side pipe registered by other server and |
| 308 // shouldn't be closed. |
| 309 ignore_result(local_pipe.release()); |
309 return false; | 310 return false; |
310 } | 311 } |
311 base::AutoLock lock(client_pipe_lock_); | 312 base::AutoLock lock(client_pipe_lock_); |
312 if (!SocketPair(&local_pipe, &client_pipe_)) | 313 int local_pipe_fd = -1, client_pipe_fd = -1; |
| 314 if (!SocketPair(&local_pipe_fd, &client_pipe_fd)) |
313 return false; | 315 return false; |
314 PipeMap::GetInstance()->Insert(pipe_name_, client_pipe_); | 316 local_pipe.reset(local_pipe_fd); |
| 317 client_pipe_.reset(client_pipe_fd); |
| 318 PipeMap::GetInstance()->Insert(pipe_name_, client_pipe_fd); |
315 } else { | 319 } else { |
316 LOG(ERROR) << "Bad mode: " << mode_; | 320 LOG(ERROR) << "Bad mode: " << mode_; |
317 return false; | 321 return false; |
318 } | 322 } |
319 } | 323 } |
320 | 324 |
321 #if defined(IPC_USES_READWRITE) | 325 #if defined(IPC_USES_READWRITE) |
322 // Create a dedicated socketpair() for exchanging file descriptors. | 326 // Create a dedicated socketpair() for exchanging file descriptors. |
323 // See comments for IPC_USES_READWRITE for details. | 327 // See comments for IPC_USES_READWRITE for details. |
324 if (mode_ & MODE_CLIENT_FLAG) { | 328 if (mode_ & MODE_CLIENT_FLAG) { |
325 if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { | 329 int fd_pipe_fd = 1, remote_fd_pipe_fd = -1; |
| 330 if (!SocketPair(&fd_pipe_fd, &remote_fd_pipe_fd)) { |
326 return false; | 331 return false; |
327 } | 332 } |
| 333 |
| 334 fd_pipe_.reset(fd_pipe_fd); |
| 335 remote_fd_pipe_.reset(remote_fd_pipe_fd); |
328 } | 336 } |
329 #endif // IPC_USES_READWRITE | 337 #endif // IPC_USES_READWRITE |
330 | 338 |
331 if ((mode_ & MODE_SERVER_FLAG) && (mode_ & MODE_NAMED_FLAG)) { | 339 if ((mode_ & MODE_SERVER_FLAG) && (mode_ & MODE_NAMED_FLAG)) |
332 server_listen_pipe_ = local_pipe; | 340 server_listen_pipe_.reset(local_pipe.release()); |
333 local_pipe = -1; | 341 else |
334 } | 342 pipe_.reset(local_pipe.release()); |
335 | |
336 pipe_ = local_pipe; | |
337 return true; | 343 return true; |
338 } | 344 } |
339 | 345 |
340 bool ChannelPosix::Connect() { | 346 bool ChannelPosix::Connect() { |
341 if (server_listen_pipe_ == -1 && pipe_ == -1) { | 347 if (!server_listen_pipe_.is_valid() && !pipe_.is_valid()) { |
342 DLOG(WARNING) << "Channel creation failed: " << pipe_name_; | 348 DLOG(WARNING) << "Channel creation failed: " << pipe_name_; |
343 return false; | 349 return false; |
344 } | 350 } |
345 | 351 |
346 bool did_connect = true; | 352 bool did_connect = true; |
347 if (server_listen_pipe_ != -1) { | 353 if (server_listen_pipe_.is_valid()) { |
348 // Watch the pipe for connections, and turn any connections into | 354 // Watch the pipe for connections, and turn any connections into |
349 // active sockets. | 355 // active sockets. |
350 base::MessageLoopForIO::current()->WatchFileDescriptor( | 356 base::MessageLoopForIO::current()->WatchFileDescriptor( |
351 server_listen_pipe_, | 357 server_listen_pipe_.get(), |
352 true, | 358 true, |
353 base::MessageLoopForIO::WATCH_READ, | 359 base::MessageLoopForIO::WATCH_READ, |
354 &server_listen_connection_watcher_, | 360 &server_listen_connection_watcher_, |
355 this); | 361 this); |
356 } else { | 362 } else { |
357 did_connect = AcceptConnection(); | 363 did_connect = AcceptConnection(); |
358 } | 364 } |
359 return did_connect; | 365 return did_connect; |
360 } | 366 } |
361 | 367 |
(...skipping 17 matching lines...) Expand all Loading... |
379 msg->file_descriptor_set()->CommitAll(); | 385 msg->file_descriptor_set()->CommitAll(); |
380 #endif | 386 #endif |
381 } | 387 } |
382 | 388 |
383 bool ChannelPosix::ProcessOutgoingMessages() { | 389 bool ChannelPosix::ProcessOutgoingMessages() { |
384 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's | 390 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
385 // no connection? | 391 // no connection? |
386 if (output_queue_.empty()) | 392 if (output_queue_.empty()) |
387 return true; | 393 return true; |
388 | 394 |
389 if (pipe_ == -1) | 395 if (!pipe_.is_valid()) |
390 return false; | 396 return false; |
391 | 397 |
392 // Write out all the messages we can till the write blocks or there are no | 398 // Write out all the messages we can till the write blocks or there are no |
393 // more outgoing messages. | 399 // more outgoing messages. |
394 while (!output_queue_.empty()) { | 400 while (!output_queue_.empty()) { |
395 Message* msg = output_queue_.front(); | 401 Message* msg = output_queue_.front(); |
396 | 402 |
397 size_t amt_to_write = msg->size() - message_send_bytes_written_; | 403 size_t amt_to_write = msg->size() - message_send_bytes_written_; |
398 DCHECK_NE(0U, amt_to_write); | 404 DCHECK_NE(0U, amt_to_write); |
399 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + | 405 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 // num_fds < kMaxDescriptorsPerMessage so no danger of overflow. | 446 // num_fds < kMaxDescriptorsPerMessage so no danger of overflow. |
441 msg->header()->num_fds = static_cast<uint16>(num_fds); | 447 msg->header()->num_fds = static_cast<uint16>(num_fds); |
442 | 448 |
443 #if defined(IPC_USES_READWRITE) | 449 #if defined(IPC_USES_READWRITE) |
444 if (!IsHelloMessage(*msg)) { | 450 if (!IsHelloMessage(*msg)) { |
445 // Only the Hello message sends the file descriptor with the message. | 451 // Only the Hello message sends the file descriptor with the message. |
446 // Subsequently, we can send file descriptors on the dedicated | 452 // Subsequently, we can send file descriptors on the dedicated |
447 // fd_pipe_ which makes Seccomp sandbox operation more efficient. | 453 // fd_pipe_ which makes Seccomp sandbox operation more efficient. |
448 struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; | 454 struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; |
449 msgh.msg_iov = &fd_pipe_iov; | 455 msgh.msg_iov = &fd_pipe_iov; |
450 fd_written = fd_pipe_; | 456 fd_written = fd_pipe_.get(); |
451 bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); | 457 bytes_written = |
| 458 HANDLE_EINTR(sendmsg(fd_pipe_.get(), &msgh, MSG_DONTWAIT)); |
452 msgh.msg_iov = &iov; | 459 msgh.msg_iov = &iov; |
453 msgh.msg_controllen = 0; | 460 msgh.msg_controllen = 0; |
454 if (bytes_written > 0) { | 461 if (bytes_written > 0) { |
455 CloseFileDescriptors(msg); | 462 CloseFileDescriptors(msg); |
456 } | 463 } |
457 } | 464 } |
458 #endif // IPC_USES_READWRITE | 465 #endif // IPC_USES_READWRITE |
459 } | 466 } |
460 | 467 |
461 if (bytes_written == 1) { | 468 if (bytes_written == 1) { |
462 fd_written = pipe_; | 469 fd_written = pipe_.get(); |
463 #if defined(IPC_USES_READWRITE) | 470 #if defined(IPC_USES_READWRITE) |
464 if ((mode_ & MODE_CLIENT_FLAG) && IsHelloMessage(*msg)) { | 471 if ((mode_ & MODE_CLIENT_FLAG) && IsHelloMessage(*msg)) { |
465 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | 472 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
466 } | 473 } |
467 if (!msgh.msg_controllen) { | 474 if (!msgh.msg_controllen) { |
468 bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); | 475 bytes_written = |
| 476 HANDLE_EINTR(write(pipe_.get(), out_bytes, amt_to_write)); |
469 } else | 477 } else |
470 #endif // IPC_USES_READWRITE | 478 #endif // IPC_USES_READWRITE |
471 { | 479 { |
472 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); | 480 bytes_written = HANDLE_EINTR(sendmsg(pipe_.get(), &msgh, MSG_DONTWAIT)); |
473 } | 481 } |
474 } | 482 } |
475 if (bytes_written > 0) | 483 if (bytes_written > 0) |
476 CloseFileDescriptors(msg); | 484 CloseFileDescriptors(msg); |
477 | 485 |
478 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { | 486 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { |
479 // We can't close the pipe here, because calling OnChannelError | 487 // We can't close the pipe here, because calling OnChannelError |
480 // may destroy this object, and that would be bad if we are | 488 // may destroy this object, and that would be bad if we are |
481 // called from Send(). Instead, we return false and hope the | 489 // called from Send(). Instead, we return false and hope the |
482 // caller will close the pipe. If they do not, the pipe will | 490 // caller will close the pipe. If they do not, the pipe will |
(...skipping 17 matching lines...) Expand all Loading... |
500 | 508 |
501 if (static_cast<size_t>(bytes_written) != amt_to_write) { | 509 if (static_cast<size_t>(bytes_written) != amt_to_write) { |
502 if (bytes_written > 0) { | 510 if (bytes_written > 0) { |
503 // If write() fails with EAGAIN then bytes_written will be -1. | 511 // If write() fails with EAGAIN then bytes_written will be -1. |
504 message_send_bytes_written_ += bytes_written; | 512 message_send_bytes_written_ += bytes_written; |
505 } | 513 } |
506 | 514 |
507 // Tell libevent to call us back once things are unblocked. | 515 // Tell libevent to call us back once things are unblocked. |
508 is_blocked_on_write_ = true; | 516 is_blocked_on_write_ = true; |
509 base::MessageLoopForIO::current()->WatchFileDescriptor( | 517 base::MessageLoopForIO::current()->WatchFileDescriptor( |
510 pipe_, | 518 pipe_.get(), |
511 false, // One shot | 519 false, // One shot |
512 base::MessageLoopForIO::WATCH_WRITE, | 520 base::MessageLoopForIO::WATCH_WRITE, |
513 &write_watcher_, | 521 &write_watcher_, |
514 this); | 522 this); |
515 return true; | 523 return true; |
516 } else { | 524 } else { |
517 message_send_bytes_written_ = 0; | 525 message_send_bytes_written_ = 0; |
518 | 526 |
519 // Message sent OK! | 527 // Message sent OK! |
520 DVLOG(2) << "sent message @" << msg << " on channel @" << this | 528 DVLOG(2) << "sent message @" << msg << " on channel @" << this |
521 << " with type " << msg->type() << " on fd " << pipe_; | 529 << " with type " << msg->type() << " on fd " << pipe_.get(); |
522 delete output_queue_.front(); | 530 delete output_queue_.front(); |
523 output_queue_.pop(); | 531 output_queue_.pop(); |
524 } | 532 } |
525 } | 533 } |
526 return true; | 534 return true; |
527 } | 535 } |
528 | 536 |
529 bool ChannelPosix::Send(Message* message) { | 537 bool ChannelPosix::Send(Message* message) { |
530 DVLOG(2) << "sending message @" << message << " on channel @" << this | 538 DVLOG(2) << "sending message @" << message << " on channel @" << this |
531 << " with type " << message->type() | 539 << " with type " << message->type() |
532 << " (" << output_queue_.size() << " in queue)"; | 540 << " (" << output_queue_.size() << " in queue)"; |
533 | 541 |
534 #ifdef IPC_MESSAGE_LOG_ENABLED | 542 #ifdef IPC_MESSAGE_LOG_ENABLED |
535 Logging::GetInstance()->OnSendMessage(message, ""); | 543 Logging::GetInstance()->OnSendMessage(message, ""); |
536 #endif // IPC_MESSAGE_LOG_ENABLED | 544 #endif // IPC_MESSAGE_LOG_ENABLED |
537 | 545 |
538 message->TraceMessageBegin(); | 546 message->TraceMessageBegin(); |
539 output_queue_.push(message); | 547 output_queue_.push(message); |
540 if (!is_blocked_on_write_ && !waiting_connect_) { | 548 if (!is_blocked_on_write_ && !waiting_connect_) { |
541 return ProcessOutgoingMessages(); | 549 return ProcessOutgoingMessages(); |
542 } | 550 } |
543 | 551 |
544 return true; | 552 return true; |
545 } | 553 } |
546 | 554 |
547 int ChannelPosix::GetClientFileDescriptor() const { | 555 int ChannelPosix::GetClientFileDescriptor() const { |
548 base::AutoLock lock(client_pipe_lock_); | 556 base::AutoLock lock(client_pipe_lock_); |
549 return client_pipe_; | 557 return client_pipe_.get(); |
550 } | 558 } |
551 | 559 |
552 int ChannelPosix::TakeClientFileDescriptor() { | 560 int ChannelPosix::TakeClientFileDescriptor() { |
553 base::AutoLock lock(client_pipe_lock_); | 561 base::AutoLock lock(client_pipe_lock_); |
554 int fd = client_pipe_; | 562 if (!client_pipe_.is_valid()) |
555 if (client_pipe_ != -1) { | 563 return -1; |
556 PipeMap::GetInstance()->Remove(pipe_name_); | 564 PipeMap::GetInstance()->Remove(pipe_name_); |
557 client_pipe_ = -1; | 565 return client_pipe_.release(); |
558 } | |
559 return fd; | |
560 } | 566 } |
561 | 567 |
562 void ChannelPosix::CloseClientFileDescriptor() { | 568 void ChannelPosix::CloseClientFileDescriptor() { |
563 base::AutoLock lock(client_pipe_lock_); | 569 base::AutoLock lock(client_pipe_lock_); |
564 if (client_pipe_ != -1) { | 570 if (!client_pipe_.is_valid()) |
565 PipeMap::GetInstance()->Remove(pipe_name_); | 571 return; |
566 if (IGNORE_EINTR(close(client_pipe_)) < 0) | 572 PipeMap::GetInstance()->Remove(pipe_name_); |
567 PLOG(ERROR) << "close " << pipe_name_; | 573 client_pipe_.reset(); |
568 client_pipe_ = -1; | |
569 } | |
570 } | 574 } |
571 | 575 |
572 bool ChannelPosix::AcceptsConnections() const { | 576 bool ChannelPosix::AcceptsConnections() const { |
573 return server_listen_pipe_ != -1; | 577 return server_listen_pipe_.is_valid(); |
574 } | 578 } |
575 | 579 |
576 bool ChannelPosix::HasAcceptedConnection() const { | 580 bool ChannelPosix::HasAcceptedConnection() const { |
577 return AcceptsConnections() && pipe_ != -1; | 581 return AcceptsConnections() && pipe_.is_valid(); |
578 } | 582 } |
579 | 583 |
580 bool ChannelPosix::GetPeerEuid(uid_t* peer_euid) const { | 584 bool ChannelPosix::GetPeerEuid(uid_t* peer_euid) const { |
581 DCHECK(!(mode_ & MODE_SERVER) || HasAcceptedConnection()); | 585 DCHECK(!(mode_ & MODE_SERVER) || HasAcceptedConnection()); |
582 return IPC::GetPeerEuid(pipe_, peer_euid); | 586 return IPC::GetPeerEuid(pipe_.get(), peer_euid); |
583 } | 587 } |
584 | 588 |
585 void ChannelPosix::ResetToAcceptingConnectionState() { | 589 void ChannelPosix::ResetToAcceptingConnectionState() { |
586 // Unregister libevent for the unix domain socket and close it. | 590 // Unregister libevent for the unix domain socket and close it. |
587 read_watcher_.StopWatchingFileDescriptor(); | 591 read_watcher_.StopWatchingFileDescriptor(); |
588 write_watcher_.StopWatchingFileDescriptor(); | 592 write_watcher_.StopWatchingFileDescriptor(); |
589 if (pipe_ != -1) { | 593 pipe_.reset(); |
590 if (IGNORE_EINTR(close(pipe_)) < 0) | |
591 PLOG(ERROR) << "close pipe_ " << pipe_name_; | |
592 pipe_ = -1; | |
593 } | |
594 #if defined(IPC_USES_READWRITE) | 594 #if defined(IPC_USES_READWRITE) |
595 if (fd_pipe_ != -1) { | 595 fd_pipe_.reset(); |
596 if (IGNORE_EINTR(close(fd_pipe_)) < 0) | 596 remote_fd_pipe_.reset(); |
597 PLOG(ERROR) << "close fd_pipe_ " << pipe_name_; | |
598 fd_pipe_ = -1; | |
599 } | |
600 if (remote_fd_pipe_ != -1) { | |
601 if (IGNORE_EINTR(close(remote_fd_pipe_)) < 0) | |
602 PLOG(ERROR) << "close remote_fd_pipe_ " << pipe_name_; | |
603 remote_fd_pipe_ = -1; | |
604 } | |
605 #endif // IPC_USES_READWRITE | 597 #endif // IPC_USES_READWRITE |
606 | 598 |
607 while (!output_queue_.empty()) { | 599 while (!output_queue_.empty()) { |
608 Message* m = output_queue_.front(); | 600 Message* m = output_queue_.front(); |
609 output_queue_.pop(); | 601 output_queue_.pop(); |
610 delete m; | 602 delete m; |
611 } | 603 } |
612 | 604 |
613 // Close any outstanding, received file descriptors. | 605 // Close any outstanding, received file descriptors. |
614 ClearInputFDs(); | 606 ClearInputFDs(); |
(...skipping 18 matching lines...) Expand all Loading... |
633 | 625 |
634 #if defined(OS_LINUX) | 626 #if defined(OS_LINUX) |
635 // static | 627 // static |
636 void ChannelPosix::SetGlobalPid(int pid) { | 628 void ChannelPosix::SetGlobalPid(int pid) { |
637 global_pid_ = pid; | 629 global_pid_ = pid; |
638 } | 630 } |
639 #endif // OS_LINUX | 631 #endif // OS_LINUX |
640 | 632 |
641 // Called by libevent when we can read from the pipe without blocking. | 633 // Called by libevent when we can read from the pipe without blocking. |
642 void ChannelPosix::OnFileCanReadWithoutBlocking(int fd) { | 634 void ChannelPosix::OnFileCanReadWithoutBlocking(int fd) { |
643 if (fd == server_listen_pipe_) { | 635 if (fd == server_listen_pipe_.get()) { |
644 int new_pipe = 0; | 636 int new_pipe = 0; |
645 if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe) || | 637 if (!ServerAcceptConnection(server_listen_pipe_.get(), &new_pipe) || |
646 new_pipe < 0) { | 638 new_pipe < 0) { |
647 Close(); | 639 Close(); |
648 listener()->OnChannelListenError(); | 640 listener()->OnChannelListenError(); |
649 } | 641 } |
650 | 642 |
651 if (pipe_ != -1) { | 643 if (pipe_.is_valid()) { |
652 // We already have a connection. We only handle one at a time. | 644 // We already have a connection. We only handle one at a time. |
653 // close our new descriptor. | 645 // close our new descriptor. |
654 if (HANDLE_EINTR(shutdown(new_pipe, SHUT_RDWR)) < 0) | 646 if (HANDLE_EINTR(shutdown(new_pipe, SHUT_RDWR)) < 0) |
655 DPLOG(ERROR) << "shutdown " << pipe_name_; | 647 DPLOG(ERROR) << "shutdown " << pipe_name_; |
656 if (IGNORE_EINTR(close(new_pipe)) < 0) | 648 if (IGNORE_EINTR(close(new_pipe)) < 0) |
657 DPLOG(ERROR) << "close " << pipe_name_; | 649 DPLOG(ERROR) << "close " << pipe_name_; |
658 listener()->OnChannelDenied(); | 650 listener()->OnChannelDenied(); |
659 return; | 651 return; |
660 } | 652 } |
661 pipe_ = new_pipe; | 653 pipe_.reset(new_pipe); |
662 | 654 |
663 if ((mode_ & MODE_OPEN_ACCESS_FLAG) == 0) { | 655 if ((mode_ & MODE_OPEN_ACCESS_FLAG) == 0) { |
664 // Verify that the IPC channel peer is running as the same user. | 656 // Verify that the IPC channel peer is running as the same user. |
665 uid_t client_euid; | 657 uid_t client_euid; |
666 if (!GetPeerEuid(&client_euid)) { | 658 if (!GetPeerEuid(&client_euid)) { |
667 DLOG(ERROR) << "Unable to query client euid"; | 659 DLOG(ERROR) << "Unable to query client euid"; |
668 ResetToAcceptingConnectionState(); | 660 ResetToAcceptingConnectionState(); |
669 return; | 661 return; |
670 } | 662 } |
671 if (client_euid != geteuid()) { | 663 if (client_euid != geteuid()) { |
(...skipping 27 matching lines...) Expand all Loading... |
699 // is invalid. This also flushes any closefd messages. | 691 // is invalid. This also flushes any closefd messages. |
700 if (!is_blocked_on_write_) { | 692 if (!is_blocked_on_write_) { |
701 if (!ProcessOutgoingMessages()) { | 693 if (!ProcessOutgoingMessages()) { |
702 ClosePipeOnError(); | 694 ClosePipeOnError(); |
703 } | 695 } |
704 } | 696 } |
705 } | 697 } |
706 | 698 |
707 // Called by libevent when we can write to the pipe without blocking. | 699 // Called by libevent when we can write to the pipe without blocking. |
708 void ChannelPosix::OnFileCanWriteWithoutBlocking(int fd) { | 700 void ChannelPosix::OnFileCanWriteWithoutBlocking(int fd) { |
709 DCHECK_EQ(pipe_, fd); | 701 DCHECK_EQ(pipe_.get(), fd); |
710 is_blocked_on_write_ = false; | 702 is_blocked_on_write_ = false; |
711 if (!ProcessOutgoingMessages()) { | 703 if (!ProcessOutgoingMessages()) { |
712 ClosePipeOnError(); | 704 ClosePipeOnError(); |
713 } | 705 } |
714 } | 706 } |
715 | 707 |
716 bool ChannelPosix::AcceptConnection() { | 708 bool ChannelPosix::AcceptConnection() { |
717 base::MessageLoopForIO::current()->WatchFileDescriptor( | 709 base::MessageLoopForIO::current()->WatchFileDescriptor( |
718 pipe_, true, base::MessageLoopForIO::WATCH_READ, &read_watcher_, this); | 710 pipe_.get(), |
| 711 true, |
| 712 base::MessageLoopForIO::WATCH_READ, |
| 713 &read_watcher_, |
| 714 this); |
719 QueueHelloMessage(); | 715 QueueHelloMessage(); |
720 | 716 |
721 if (mode_ & MODE_CLIENT_FLAG) { | 717 if (mode_ & MODE_CLIENT_FLAG) { |
722 // If we are a client we want to send a hello message out immediately. | 718 // If we are a client we want to send a hello message out immediately. |
723 // In server mode we will send a hello message when we receive one from a | 719 // In server mode we will send a hello message when we receive one from a |
724 // client. | 720 // client. |
725 waiting_connect_ = false; | 721 waiting_connect_ = false; |
726 return ProcessOutgoingMessages(); | 722 return ProcessOutgoingMessages(); |
727 } else if (mode_ & MODE_SERVER_FLAG) { | 723 } else if (mode_ & MODE_SERVER_FLAG) { |
728 waiting_connect_ = true; | 724 waiting_connect_ = true; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 void ChannelPosix::QueueHelloMessage() { | 757 void ChannelPosix::QueueHelloMessage() { |
762 // Create the Hello message | 758 // Create the Hello message |
763 scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, | 759 scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, |
764 HELLO_MESSAGE_TYPE, | 760 HELLO_MESSAGE_TYPE, |
765 IPC::Message::PRIORITY_NORMAL)); | 761 IPC::Message::PRIORITY_NORMAL)); |
766 if (!msg->WriteInt(GetHelloMessageProcId())) { | 762 if (!msg->WriteInt(GetHelloMessageProcId())) { |
767 NOTREACHED() << "Unable to pickle hello message proc id"; | 763 NOTREACHED() << "Unable to pickle hello message proc id"; |
768 } | 764 } |
769 #if defined(IPC_USES_READWRITE) | 765 #if defined(IPC_USES_READWRITE) |
770 scoped_ptr<Message> hello; | 766 scoped_ptr<Message> hello; |
771 if (remote_fd_pipe_ != -1) { | 767 if (remote_fd_pipe_.is_valid()) { |
772 if (!msg->WriteBorrowingFile(remote_fd_pipe_)) { | 768 if (!msg->WriteBorrowingFile(remote_fd_pipe_.get())) { |
773 NOTREACHED() << "Unable to pickle hello message file descriptors"; | 769 NOTREACHED() << "Unable to pickle hello message file descriptors"; |
774 } | 770 } |
775 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | 771 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
776 } | 772 } |
777 #endif // IPC_USES_READWRITE | 773 #endif // IPC_USES_READWRITE |
778 output_queue_.push(msg.release()); | 774 output_queue_.push(msg.release()); |
779 } | 775 } |
780 | 776 |
781 ChannelPosix::ReadState ChannelPosix::ReadData( | 777 ChannelPosix::ReadState ChannelPosix::ReadData( |
782 char* buffer, | 778 char* buffer, |
783 int buffer_len, | 779 int buffer_len, |
784 int* bytes_read) { | 780 int* bytes_read) { |
785 if (pipe_ == -1) | 781 if (!pipe_.is_valid()) |
786 return READ_FAILED; | 782 return READ_FAILED; |
787 | 783 |
788 struct msghdr msg = {0}; | 784 struct msghdr msg = {0}; |
789 | 785 |
790 struct iovec iov = {buffer, static_cast<size_t>(buffer_len)}; | 786 struct iovec iov = {buffer, static_cast<size_t>(buffer_len)}; |
791 msg.msg_iov = &iov; | 787 msg.msg_iov = &iov; |
792 msg.msg_iovlen = 1; | 788 msg.msg_iovlen = 1; |
793 | 789 |
794 msg.msg_control = input_cmsg_buf_; | 790 msg.msg_control = input_cmsg_buf_; |
795 | 791 |
796 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data | 792 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data |
797 // is waiting on the pipe. | 793 // is waiting on the pipe. |
798 #if defined(IPC_USES_READWRITE) | 794 #if defined(IPC_USES_READWRITE) |
799 if (fd_pipe_ >= 0) { | 795 if (fd_pipe_.is_valid()) { |
800 *bytes_read = HANDLE_EINTR(read(pipe_, buffer, buffer_len)); | 796 *bytes_read = HANDLE_EINTR(read(pipe_.get(), buffer, buffer_len)); |
801 msg.msg_controllen = 0; | 797 msg.msg_controllen = 0; |
802 } else | 798 } else |
803 #endif // IPC_USES_READWRITE | 799 #endif // IPC_USES_READWRITE |
804 { | 800 { |
805 msg.msg_controllen = sizeof(input_cmsg_buf_); | 801 msg.msg_controllen = sizeof(input_cmsg_buf_); |
806 *bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); | 802 *bytes_read = HANDLE_EINTR(recvmsg(pipe_.get(), &msg, MSG_DONTWAIT)); |
807 } | 803 } |
808 if (*bytes_read < 0) { | 804 if (*bytes_read < 0) { |
809 if (errno == EAGAIN) { | 805 if (errno == EAGAIN) { |
810 return READ_PENDING; | 806 return READ_PENDING; |
811 #if defined(OS_MACOSX) | 807 #if defined(OS_MACOSX) |
812 } else if (errno == EPERM) { | 808 } else if (errno == EPERM) { |
813 // On OSX, reading from a pipe with no listener returns EPERM | 809 // On OSX, reading from a pipe with no listener returns EPERM |
814 // treat this as a special case to prevent spurious error messages | 810 // treat this as a special case to prevent spurious error messages |
815 // to the console. | 811 // to the console. |
816 return READ_FAILED; | 812 return READ_FAILED; |
817 #endif // OS_MACOSX | 813 #endif // OS_MACOSX |
818 } else if (errno == ECONNRESET || errno == EPIPE) { | 814 } else if (errno == ECONNRESET || errno == EPIPE) { |
819 return READ_FAILED; | 815 return READ_FAILED; |
820 } else { | 816 } else { |
821 PLOG(ERROR) << "pipe error (" << pipe_ << ")"; | 817 PLOG(ERROR) << "pipe error (" << pipe_.get() << ")"; |
822 return READ_FAILED; | 818 return READ_FAILED; |
823 } | 819 } |
824 } else if (*bytes_read == 0) { | 820 } else if (*bytes_read == 0) { |
825 // The pipe has closed... | 821 // The pipe has closed... |
826 return READ_FAILED; | 822 return READ_FAILED; |
827 } | 823 } |
828 DCHECK(*bytes_read); | 824 DCHECK(*bytes_read); |
829 | 825 |
830 CloseClientFileDescriptor(); | 826 CloseClientFileDescriptor(); |
831 | 827 |
832 // Read any file descriptors from the message. | 828 // Read any file descriptors from the message. |
833 if (!ExtractFileDescriptorsFromMsghdr(&msg)) | 829 if (!ExtractFileDescriptorsFromMsghdr(&msg)) |
834 return READ_FAILED; | 830 return READ_FAILED; |
835 return READ_SUCCEEDED; | 831 return READ_SUCCEEDED; |
836 } | 832 } |
837 | 833 |
838 #if defined(IPC_USES_READWRITE) | 834 #if defined(IPC_USES_READWRITE) |
839 bool ChannelPosix::ReadFileDescriptorsFromFDPipe() { | 835 bool ChannelPosix::ReadFileDescriptorsFromFDPipe() { |
840 char dummy; | 836 char dummy; |
841 struct iovec fd_pipe_iov = { &dummy, 1 }; | 837 struct iovec fd_pipe_iov = { &dummy, 1 }; |
842 | 838 |
843 struct msghdr msg = { 0 }; | 839 struct msghdr msg = { 0 }; |
844 msg.msg_iov = &fd_pipe_iov; | 840 msg.msg_iov = &fd_pipe_iov; |
845 msg.msg_iovlen = 1; | 841 msg.msg_iovlen = 1; |
846 msg.msg_control = input_cmsg_buf_; | 842 msg.msg_control = input_cmsg_buf_; |
847 msg.msg_controllen = sizeof(input_cmsg_buf_); | 843 msg.msg_controllen = sizeof(input_cmsg_buf_); |
848 ssize_t bytes_received = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); | 844 ssize_t bytes_received = |
| 845 HANDLE_EINTR(recvmsg(fd_pipe_.get(), &msg, MSG_DONTWAIT)); |
849 | 846 |
850 if (bytes_received != 1) | 847 if (bytes_received != 1) |
851 return true; // No message waiting. | 848 return true; // No message waiting. |
852 | 849 |
853 if (!ExtractFileDescriptorsFromMsghdr(&msg)) | 850 if (!ExtractFileDescriptorsFromMsghdr(&msg)) |
854 return false; | 851 return false; |
855 return true; | 852 return true; |
856 } | 853 } |
857 #endif | 854 #endif |
858 | 855 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
987 #if defined(IPC_USES_READWRITE) | 984 #if defined(IPC_USES_READWRITE) |
988 if (mode_ & MODE_SERVER_FLAG) { | 985 if (mode_ & MODE_SERVER_FLAG) { |
989 // With IPC_USES_READWRITE, the Hello message from the client to the | 986 // With IPC_USES_READWRITE, the Hello message from the client to the |
990 // server also contains the fd_pipe_, which will be used for all | 987 // server also contains the fd_pipe_, which will be used for all |
991 // subsequent file descriptor passing. | 988 // subsequent file descriptor passing. |
992 DCHECK_EQ(msg.file_descriptor_set()->size(), 1U); | 989 DCHECK_EQ(msg.file_descriptor_set()->size(), 1U); |
993 base::ScopedFD descriptor; | 990 base::ScopedFD descriptor; |
994 if (!msg.ReadFile(&iter, &descriptor)) { | 991 if (!msg.ReadFile(&iter, &descriptor)) { |
995 NOTREACHED(); | 992 NOTREACHED(); |
996 } | 993 } |
997 fd_pipe_ = descriptor.release(); | 994 fd_pipe_.reset(descriptor.release()); |
998 } | 995 } |
999 #endif // IPC_USES_READWRITE | 996 #endif // IPC_USES_READWRITE |
1000 peer_pid_ = pid; | 997 peer_pid_ = pid; |
1001 listener()->OnChannelConnected(pid); | 998 listener()->OnChannelConnected(pid); |
1002 break; | 999 break; |
1003 | 1000 |
1004 #if defined(OS_MACOSX) | 1001 #if defined(OS_MACOSX) |
1005 case Channel::CLOSE_FD_MESSAGE_TYPE: | 1002 case Channel::CLOSE_FD_MESSAGE_TYPE: |
1006 int fd, hops; | 1003 int fd, hops; |
1007 if (!msg.ReadInt(&iter, &hops)) | 1004 if (!msg.ReadInt(&iter, &hops)) |
(...skipping 18 matching lines...) Expand all Loading... |
1026 void ChannelPosix::Close() { | 1023 void ChannelPosix::Close() { |
1027 // Close can be called multiple time, so we need to make sure we're | 1024 // Close can be called multiple time, so we need to make sure we're |
1028 // idempotent. | 1025 // idempotent. |
1029 | 1026 |
1030 ResetToAcceptingConnectionState(); | 1027 ResetToAcceptingConnectionState(); |
1031 | 1028 |
1032 if (must_unlink_) { | 1029 if (must_unlink_) { |
1033 unlink(pipe_name_.c_str()); | 1030 unlink(pipe_name_.c_str()); |
1034 must_unlink_ = false; | 1031 must_unlink_ = false; |
1035 } | 1032 } |
1036 if (server_listen_pipe_ != -1) { | 1033 |
1037 if (IGNORE_EINTR(close(server_listen_pipe_)) < 0) | 1034 if (server_listen_pipe_.is_valid()) { |
1038 DPLOG(ERROR) << "close " << server_listen_pipe_; | 1035 server_listen_pipe_.reset(); |
1039 server_listen_pipe_ = -1; | |
1040 // Unregister libevent for the listening socket and close it. | 1036 // Unregister libevent for the listening socket and close it. |
1041 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | 1037 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
1042 } | 1038 } |
1043 | 1039 |
1044 CloseClientFileDescriptor(); | 1040 CloseClientFileDescriptor(); |
1045 } | 1041 } |
1046 | 1042 |
1047 base::ProcessId ChannelPosix::GetPeerPID() const { | 1043 base::ProcessId ChannelPosix::GetPeerPID() const { |
1048 return peer_pid_; | 1044 return peer_pid_; |
1049 } | 1045 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1081 } | 1077 } |
1082 | 1078 |
1083 #if defined(OS_LINUX) | 1079 #if defined(OS_LINUX) |
1084 // static | 1080 // static |
1085 void Channel::SetGlobalPid(int pid) { | 1081 void Channel::SetGlobalPid(int pid) { |
1086 ChannelPosix::SetGlobalPid(pid); | 1082 ChannelPosix::SetGlobalPid(pid); |
1087 } | 1083 } |
1088 #endif // OS_LINUX | 1084 #endif // OS_LINUX |
1089 | 1085 |
1090 } // namespace IPC | 1086 } // namespace IPC |
OLD | NEW |