| 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> |
| 11 #include <sys/stat.h> | 11 #include <sys/stat.h> |
| 12 #include <sys/types.h> | 12 #include <sys/types.h> |
| 13 #include <sys/un.h> | |
| 14 #include <unistd.h> | 13 #include <unistd.h> |
| 15 | 14 |
| 16 #if defined(OS_OPENBSD) | 15 #if defined(OS_OPENBSD) |
| 17 #include <sys/uio.h> | 16 #include <sys/uio.h> |
| 18 #endif | 17 #endif |
| 19 | 18 |
| 19 #if !defined(__native_client_nonsfi__) |
| 20 #include <sys/un.h> |
| 21 #endif |
| 22 |
| 20 #include <map> | 23 #include <map> |
| 21 #include <string> | 24 #include <string> |
| 22 | 25 |
| 23 #include "base/command_line.h" | 26 #include "base/command_line.h" |
| 24 #include "base/files/file_path.h" | 27 #include "base/files/file_path.h" |
| 25 #include "base/files/file_util.h" | 28 #include "base/files/file_util.h" |
| 26 #include "base/location.h" | 29 #include "base/location.h" |
| 27 #include "base/logging.h" | 30 #include "base/logging.h" |
| 28 #include "base/memory/scoped_ptr.h" | 31 #include "base/memory/scoped_ptr.h" |
| 29 #include "base/memory/singleton.h" | 32 #include "base/memory/singleton.h" |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 if (value == -1) { | 251 if (value == -1) { |
| 249 PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; | 252 PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; |
| 250 return false; | 253 return false; |
| 251 } | 254 } |
| 252 if (!(value & O_NONBLOCK)) { | 255 if (!(value & O_NONBLOCK)) { |
| 253 LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; | 256 LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; |
| 254 return false; | 257 return false; |
| 255 } | 258 } |
| 256 #endif // IPC_USES_READWRITE | 259 #endif // IPC_USES_READWRITE |
| 257 } else if (mode_ & MODE_NAMED_FLAG) { | 260 } else if (mode_ & MODE_NAMED_FLAG) { |
| 261 #if defined(__native_client_nonsfi__) |
| 262 LOG(FATAL) |
| 263 << "IPC channels in nacl_helper_nonsfi should not be in NAMED mode."; |
| 264 #else |
| 258 // Case 2 from comment above. | 265 // Case 2 from comment above. |
| 259 int local_pipe_fd = -1; | 266 int local_pipe_fd = -1; |
| 260 | 267 |
| 261 if (mode_ & MODE_SERVER_FLAG) { | 268 if (mode_ & MODE_SERVER_FLAG) { |
| 262 if (!CreateServerUnixDomainSocket(base::FilePath(pipe_name_), | 269 if (!CreateServerUnixDomainSocket(base::FilePath(pipe_name_), |
| 263 &local_pipe_fd)) { | 270 &local_pipe_fd)) { |
| 264 return false; | 271 return false; |
| 265 } | 272 } |
| 266 | 273 |
| 267 must_unlink_ = true; | 274 must_unlink_ = true; |
| 268 } else if (mode_ & MODE_CLIENT_FLAG) { | 275 } else if (mode_ & MODE_CLIENT_FLAG) { |
| 269 if (!CreateClientUnixDomainSocket(base::FilePath(pipe_name_), | 276 if (!CreateClientUnixDomainSocket(base::FilePath(pipe_name_), |
| 270 &local_pipe_fd)) { | 277 &local_pipe_fd)) { |
| 271 return false; | 278 return false; |
| 272 } | 279 } |
| 273 } else { | 280 } else { |
| 274 LOG(ERROR) << "Bad mode: " << mode_; | 281 LOG(ERROR) << "Bad mode: " << mode_; |
| 275 return false; | 282 return false; |
| 276 } | 283 } |
| 277 | 284 |
| 278 local_pipe.reset(local_pipe_fd); | 285 local_pipe.reset(local_pipe_fd); |
| 286 #endif // !defined(__native_client_nonsfi__) |
| 279 } else { | 287 } else { |
| 280 local_pipe.reset(PipeMap::GetInstance()->Lookup(pipe_name_)); | 288 local_pipe.reset(PipeMap::GetInstance()->Lookup(pipe_name_)); |
| 281 if (mode_ & MODE_CLIENT_FLAG) { | 289 if (mode_ & MODE_CLIENT_FLAG) { |
| 282 if (local_pipe.is_valid()) { | 290 if (local_pipe.is_valid()) { |
| 283 // Case 3 from comment above. | 291 // Case 3 from comment above. |
| 284 // We only allow one connection. | 292 // We only allow one connection. |
| 285 local_pipe.reset(HANDLE_EINTR(dup(local_pipe.release()))); | 293 local_pipe.reset(HANDLE_EINTR(dup(local_pipe.release()))); |
| 286 PipeMap::GetInstance()->Remove(pipe_name_); | 294 PipeMap::GetInstance()->Remove(pipe_name_); |
| 287 } else { | 295 } else { |
| 288 // Case 4a from comment above. | 296 // Case 4a from comment above. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 int fd_pipe_fd = 1, remote_fd_pipe_fd = -1; | 337 int fd_pipe_fd = 1, remote_fd_pipe_fd = -1; |
| 330 if (!SocketPair(&fd_pipe_fd, &remote_fd_pipe_fd)) { | 338 if (!SocketPair(&fd_pipe_fd, &remote_fd_pipe_fd)) { |
| 331 return false; | 339 return false; |
| 332 } | 340 } |
| 333 | 341 |
| 334 fd_pipe_.reset(fd_pipe_fd); | 342 fd_pipe_.reset(fd_pipe_fd); |
| 335 remote_fd_pipe_.reset(remote_fd_pipe_fd); | 343 remote_fd_pipe_.reset(remote_fd_pipe_fd); |
| 336 } | 344 } |
| 337 #endif // IPC_USES_READWRITE | 345 #endif // IPC_USES_READWRITE |
| 338 | 346 |
| 339 if ((mode_ & MODE_SERVER_FLAG) && (mode_ & MODE_NAMED_FLAG)) | 347 if ((mode_ & MODE_SERVER_FLAG) && (mode_ & MODE_NAMED_FLAG)) { |
| 348 #if defined(__native_client_nonsfi__) |
| 349 LOG(FATAL) << "IPC channels in nacl_helper_nonsfi " |
| 350 << "should not be in NAMED or SERVER mode."; |
| 351 #else |
| 340 server_listen_pipe_.reset(local_pipe.release()); | 352 server_listen_pipe_.reset(local_pipe.release()); |
| 341 else | 353 #endif |
| 354 } else { |
| 342 pipe_.reset(local_pipe.release()); | 355 pipe_.reset(local_pipe.release()); |
| 356 } |
| 343 return true; | 357 return true; |
| 344 } | 358 } |
| 345 | 359 |
| 346 bool ChannelPosix::Connect() { | 360 bool ChannelPosix::Connect() { |
| 347 if (!server_listen_pipe_.is_valid() && !pipe_.is_valid()) { | 361 if (!server_listen_pipe_.is_valid() && !pipe_.is_valid()) { |
| 348 DLOG(WARNING) << "Channel creation failed: " << pipe_name_; | 362 DLOG(WARNING) << "Channel creation failed: " << pipe_name_; |
| 349 return false; | 363 return false; |
| 350 } | 364 } |
| 351 | 365 |
| 352 bool did_connect = true; | 366 bool did_connect = true; |
| 353 if (server_listen_pipe_.is_valid()) { | 367 if (server_listen_pipe_.is_valid()) { |
| 368 #if defined(__native_client_nonsfi__) |
| 369 LOG(FATAL) << "IPC channels in nacl_helper_nonsfi " |
| 370 << "should always be in client mode."; |
| 371 #else |
| 354 // Watch the pipe for connections, and turn any connections into | 372 // Watch the pipe for connections, and turn any connections into |
| 355 // active sockets. | 373 // active sockets. |
| 356 base::MessageLoopForIO::current()->WatchFileDescriptor( | 374 base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 357 server_listen_pipe_.get(), | 375 server_listen_pipe_.get(), |
| 358 true, | 376 true, |
| 359 base::MessageLoopForIO::WATCH_READ, | 377 base::MessageLoopForIO::WATCH_READ, |
| 360 &server_listen_connection_watcher_, | 378 &server_listen_connection_watcher_, |
| 361 this); | 379 this); |
| 380 #endif |
| 362 } else { | 381 } else { |
| 363 did_connect = AcceptConnection(); | 382 did_connect = AcceptConnection(); |
| 364 } | 383 } |
| 365 return did_connect; | 384 return did_connect; |
| 366 } | 385 } |
| 367 | 386 |
| 368 void ChannelPosix::CloseFileDescriptors(Message* msg) { | 387 void ChannelPosix::CloseFileDescriptors(Message* msg) { |
| 369 #if defined(OS_MACOSX) | 388 #if defined(OS_MACOSX) |
| 370 // There is a bug on OSX which makes it dangerous to close | 389 // There is a bug on OSX which makes it dangerous to close |
| 371 // a file descriptor while it is in transit. So instead we | 390 // a file descriptor while it is in transit. So instead we |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 } | 593 } |
| 575 | 594 |
| 576 bool ChannelPosix::AcceptsConnections() const { | 595 bool ChannelPosix::AcceptsConnections() const { |
| 577 return server_listen_pipe_.is_valid(); | 596 return server_listen_pipe_.is_valid(); |
| 578 } | 597 } |
| 579 | 598 |
| 580 bool ChannelPosix::HasAcceptedConnection() const { | 599 bool ChannelPosix::HasAcceptedConnection() const { |
| 581 return AcceptsConnections() && pipe_.is_valid(); | 600 return AcceptsConnections() && pipe_.is_valid(); |
| 582 } | 601 } |
| 583 | 602 |
| 603 #if !defined(__native_client_nonsfi__) |
| 604 // GetPeerEuid is not supported in nacl_helper_nonsfi. |
| 584 bool ChannelPosix::GetPeerEuid(uid_t* peer_euid) const { | 605 bool ChannelPosix::GetPeerEuid(uid_t* peer_euid) const { |
| 585 DCHECK(!(mode_ & MODE_SERVER) || HasAcceptedConnection()); | 606 DCHECK(!(mode_ & MODE_SERVER) || HasAcceptedConnection()); |
| 586 return IPC::GetPeerEuid(pipe_.get(), peer_euid); | 607 return IPC::GetPeerEuid(pipe_.get(), peer_euid); |
| 587 } | 608 } |
| 609 #endif |
| 588 | 610 |
| 589 void ChannelPosix::ResetToAcceptingConnectionState() { | 611 void ChannelPosix::ResetToAcceptingConnectionState() { |
| 590 // Unregister libevent for the unix domain socket and close it. | 612 // Unregister libevent for the unix domain socket and close it. |
| 591 read_watcher_.StopWatchingFileDescriptor(); | 613 read_watcher_.StopWatchingFileDescriptor(); |
| 592 write_watcher_.StopWatchingFileDescriptor(); | 614 write_watcher_.StopWatchingFileDescriptor(); |
| 593 pipe_.reset(); | 615 pipe_.reset(); |
| 594 #if defined(IPC_USES_READWRITE) | 616 #if defined(IPC_USES_READWRITE) |
| 595 fd_pipe_.reset(); | 617 fd_pipe_.reset(); |
| 596 remote_fd_pipe_.reset(); | 618 remote_fd_pipe_.reset(); |
| 597 #endif // IPC_USES_READWRITE | 619 #endif // IPC_USES_READWRITE |
| (...skipping 28 matching lines...) Expand all Loading... |
| 626 #if defined(OS_LINUX) | 648 #if defined(OS_LINUX) |
| 627 // static | 649 // static |
| 628 void ChannelPosix::SetGlobalPid(int pid) { | 650 void ChannelPosix::SetGlobalPid(int pid) { |
| 629 global_pid_ = pid; | 651 global_pid_ = pid; |
| 630 } | 652 } |
| 631 #endif // OS_LINUX | 653 #endif // OS_LINUX |
| 632 | 654 |
| 633 // Called by libevent when we can read from the pipe without blocking. | 655 // Called by libevent when we can read from the pipe without blocking. |
| 634 void ChannelPosix::OnFileCanReadWithoutBlocking(int fd) { | 656 void ChannelPosix::OnFileCanReadWithoutBlocking(int fd) { |
| 635 if (fd == server_listen_pipe_.get()) { | 657 if (fd == server_listen_pipe_.get()) { |
| 658 #if defined(__native_client_nonsfi__) |
| 659 LOG(FATAL) |
| 660 << "IPC channels in nacl_helper_nonsfi should not be SERVER mode."; |
| 661 #else |
| 636 int new_pipe = 0; | 662 int new_pipe = 0; |
| 637 if (!ServerAcceptConnection(server_listen_pipe_.get(), &new_pipe) || | 663 if (!ServerAcceptConnection(server_listen_pipe_.get(), &new_pipe) || |
| 638 new_pipe < 0) { | 664 new_pipe < 0) { |
| 639 Close(); | 665 Close(); |
| 640 listener()->OnChannelListenError(); | 666 listener()->OnChannelListenError(); |
| 641 } | 667 } |
| 642 | 668 |
| 643 if (pipe_.is_valid()) { | 669 if (pipe_.is_valid()) { |
| 644 // We already have a connection. We only handle one at a time. | 670 // We already have a connection. We only handle one at a time. |
| 645 // close our new descriptor. | 671 // close our new descriptor. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 664 DLOG(WARNING) << "Client euid is not authorised"; | 690 DLOG(WARNING) << "Client euid is not authorised"; |
| 665 ResetToAcceptingConnectionState(); | 691 ResetToAcceptingConnectionState(); |
| 666 return; | 692 return; |
| 667 } | 693 } |
| 668 } | 694 } |
| 669 | 695 |
| 670 if (!AcceptConnection()) { | 696 if (!AcceptConnection()) { |
| 671 NOTREACHED() << "AcceptConnection should not fail on server"; | 697 NOTREACHED() << "AcceptConnection should not fail on server"; |
| 672 } | 698 } |
| 673 waiting_connect_ = false; | 699 waiting_connect_ = false; |
| 700 #endif |
| 674 } else if (fd == pipe_) { | 701 } else if (fd == pipe_) { |
| 675 if (waiting_connect_ && (mode_ & MODE_SERVER_FLAG)) { | 702 if (waiting_connect_ && (mode_ & MODE_SERVER_FLAG)) { |
| 676 waiting_connect_ = false; | 703 waiting_connect_ = false; |
| 677 } | 704 } |
| 678 if (!ProcessIncomingMessages()) { | 705 if (!ProcessIncomingMessages()) { |
| 679 // ClosePipeOnError may delete this object, so we mustn't call | 706 // ClosePipeOnError may delete this object, so we mustn't call |
| 680 // ProcessOutgoingMessages. | 707 // ProcessOutgoingMessages. |
| 681 ClosePipeOnError(); | 708 ClosePipeOnError(); |
| 682 return; | 709 return; |
| 683 } | 710 } |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 916 cmsg = CMSG_NXTHDR(msg, cmsg)) { | 943 cmsg = CMSG_NXTHDR(msg, cmsg)) { |
| 917 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { | 944 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) { |
| 918 unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); | 945 unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
| 919 DCHECK_EQ(0U, payload_len % sizeof(int)); | 946 DCHECK_EQ(0U, payload_len % sizeof(int)); |
| 920 const int* file_descriptors = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 947 const int* file_descriptors = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 921 unsigned num_file_descriptors = payload_len / 4; | 948 unsigned num_file_descriptors = payload_len / 4; |
| 922 input_fds_.insert(input_fds_.end(), | 949 input_fds_.insert(input_fds_.end(), |
| 923 file_descriptors, | 950 file_descriptors, |
| 924 file_descriptors + num_file_descriptors); | 951 file_descriptors + num_file_descriptors); |
| 925 | 952 |
| 953 #if !defined(__native_client_nonsfi__) |
| 954 // The PNaCl toolchain for Non-SFI binary build does not support |
| 955 // MSG_CTRUNC. |
| 926 // Check this after adding the FDs so we don't leak them. | 956 // Check this after adding the FDs so we don't leak them. |
| 927 if (msg->msg_flags & MSG_CTRUNC) { | 957 if (msg->msg_flags & MSG_CTRUNC) { |
| 928 ClearInputFDs(); | 958 ClearInputFDs(); |
| 929 return false; | 959 return false; |
| 930 } | 960 } |
| 961 #endif |
| 931 | 962 |
| 932 return true; | 963 return true; |
| 933 } | 964 } |
| 934 } | 965 } |
| 935 | 966 |
| 936 // No file descriptors found, but that's OK. | 967 // No file descriptors found, but that's OK. |
| 937 return true; | 968 return true; |
| 938 } | 969 } |
| 939 | 970 |
| 940 void ChannelPosix::ClearInputFDs() { | 971 void ChannelPosix::ClearInputFDs() { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1025 // idempotent. | 1056 // idempotent. |
| 1026 | 1057 |
| 1027 ResetToAcceptingConnectionState(); | 1058 ResetToAcceptingConnectionState(); |
| 1028 | 1059 |
| 1029 if (must_unlink_) { | 1060 if (must_unlink_) { |
| 1030 unlink(pipe_name_.c_str()); | 1061 unlink(pipe_name_.c_str()); |
| 1031 must_unlink_ = false; | 1062 must_unlink_ = false; |
| 1032 } | 1063 } |
| 1033 | 1064 |
| 1034 if (server_listen_pipe_.is_valid()) { | 1065 if (server_listen_pipe_.is_valid()) { |
| 1066 #if defined(__native_client_nonsfi__) |
| 1067 LOG(FATAL) |
| 1068 << "IPC channels in nacl_helper_nonsfi should not be SERVER mode."; |
| 1069 #else |
| 1035 server_listen_pipe_.reset(); | 1070 server_listen_pipe_.reset(); |
| 1036 // Unregister libevent for the listening socket and close it. | 1071 // Unregister libevent for the listening socket and close it. |
| 1037 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | 1072 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
| 1073 #endif |
| 1038 } | 1074 } |
| 1039 | 1075 |
| 1040 CloseClientFileDescriptor(); | 1076 CloseClientFileDescriptor(); |
| 1041 } | 1077 } |
| 1042 | 1078 |
| 1043 base::ProcessId ChannelPosix::GetPeerPID() const { | 1079 base::ProcessId ChannelPosix::GetPeerPID() const { |
| 1044 return peer_pid_; | 1080 return peer_pid_; |
| 1045 } | 1081 } |
| 1046 | 1082 |
| 1047 base::ProcessId ChannelPosix::GetSelfPID() const { | 1083 base::ProcessId ChannelPosix::GetSelfPID() const { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1076 } | 1112 } |
| 1077 | 1113 |
| 1078 #if defined(OS_LINUX) | 1114 #if defined(OS_LINUX) |
| 1079 // static | 1115 // static |
| 1080 void Channel::SetGlobalPid(int pid) { | 1116 void Channel::SetGlobalPid(int pid) { |
| 1081 ChannelPosix::SetGlobalPid(pid); | 1117 ChannelPosix::SetGlobalPid(pid); |
| 1082 } | 1118 } |
| 1083 #endif // OS_LINUX | 1119 #endif // OS_LINUX |
| 1084 | 1120 |
| 1085 } // namespace IPC | 1121 } // namespace IPC |
| OLD | NEW |