Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(139)

Side by Side Diff: ipc/ipc_channel_posix.cc

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

Powered by Google App Engine
This is Rietveld 408576698