summaryrefslogtreecommitdiffstats
path: root/ipc/ipc_channel_posix.cc
diff options
context:
space:
mode:
authorfalken@chromium.org <falken@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-13 03:46:35 +0000
committerfalken@chromium.org <falken@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-03-13 03:46:35 +0000
commit09e6752c18cb6961efb5bf55d83576640cd70c8b (patch)
tree1119bbea6af4c7d55252d64ac09ebdd2ba1e8331 /ipc/ipc_channel_posix.cc
parent09929e37e58f8b4a399e9913fcc3b56e3248a3ef (diff)
downloadchromium_src-09e6752c18cb6961efb5bf55d83576640cd70c8b.zip
chromium_src-09e6752c18cb6961efb5bf55d83576640cd70c8b.tar.gz
chromium_src-09e6752c18cb6961efb5bf55d83576640cd70c8b.tar.bz2
Revert 187554 "Implement IPC::ChannelFactory, a class that accep..."
Reverting because it seems to cause failures on ipc_tests (UnixDomainSocketUtil.Connect, UnixDomainSocketUtil.SendReceive) on Android. > Implement IPC::ChannelFactory, a class that accept()s on a UNIX socket. > > IPC::ChannelFactory listens on a UNIX domain socket and notifies its delegate > when a client connects. The delegate is expected to craft an IPC::Channel from > the handle it is given. > > Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=186912 > > Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=187233 > > Review URL: https://chromiumcodereview.appspot.com/12386010 TBR=jeremya@chromium.org Review URL: https://codereview.chromium.org/12431017 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@187760 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ipc/ipc_channel_posix.cc')
-rw-r--r--ipc/ipc_channel_posix.cc183
1 files changed, 170 insertions, 13 deletions
diff --git a/ipc/ipc_channel_posix.cc b/ipc/ipc_channel_posix.cc
index 7f38a48..17b3641 100644
--- a/ipc/ipc_channel_posix.cc
+++ b/ipc/ipc_channel_posix.cc
@@ -40,7 +40,6 @@
#include "ipc/ipc_logging.h"
#include "ipc/ipc_message_utils.h"
#include "ipc/ipc_switches.h"
-#include "ipc/unix_domain_socket_util.h"
namespace IPC {
@@ -138,6 +137,143 @@ class PipeMap {
};
//------------------------------------------------------------------------------
+// Verify that kMaxPipeNameLength is a decent size.
+COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength,
+ BAD_SUN_PATH_LENGTH);
+
+// Creates a unix domain socket bound to the specified name that is listening
+// for connections.
+bool CreateServerUnixDomainSocket(const std::string& pipe_name,
+ int* server_listen_fd) {
+ DCHECK(server_listen_fd);
+
+ if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) {
+ DLOG(ERROR) << "pipe_name.length() == " << pipe_name.length();
+ return false;
+ }
+
+ // Create socket.
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0) {
+ return false;
+ }
+
+ // Make socket non-blocking
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+ PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name;
+ if (HANDLE_EINTR(close(fd)) < 0)
+ PLOG(ERROR) << "close " << pipe_name;
+ return false;
+ }
+
+ // Delete any old FS instances.
+ unlink(pipe_name.c_str());
+
+ // Make sure the path we need exists.
+ base::FilePath path(pipe_name);
+ base::FilePath dir_path = path.DirName();
+ if (!file_util::CreateDirectory(dir_path)) {
+ if (HANDLE_EINTR(close(fd)) < 0)
+ PLOG(ERROR) << "close " << pipe_name;
+ return false;
+ }
+
+ // Create unix_addr structure.
+ struct sockaddr_un unix_addr;
+ memset(&unix_addr, 0, sizeof(unix_addr));
+ unix_addr.sun_family = AF_UNIX;
+ int path_len = snprintf(unix_addr.sun_path, IPC::kMaxPipeNameLength,
+ "%s", pipe_name.c_str());
+ DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len);
+ size_t unix_addr_len = offsetof(struct sockaddr_un,
+ sun_path) + path_len + 1;
+
+ // Bind the socket.
+ if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr),
+ unix_addr_len) != 0) {
+ PLOG(ERROR) << "bind " << pipe_name;
+ if (HANDLE_EINTR(close(fd)) < 0)
+ PLOG(ERROR) << "close " << pipe_name;
+ return false;
+ }
+
+ // Start listening on the socket.
+ const int listen_queue_length = 1;
+ if (listen(fd, listen_queue_length) != 0) {
+ PLOG(ERROR) << "listen " << pipe_name;
+ if (HANDLE_EINTR(close(fd)) < 0)
+ PLOG(ERROR) << "close " << pipe_name;
+ return false;
+ }
+
+ *server_listen_fd = fd;
+ return true;
+}
+
+// Accept a connection on a socket we are listening to.
+bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
+ DCHECK(server_socket);
+
+ int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0));
+ if (accept_fd < 0)
+ return false;
+ if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) {
+ PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd;
+ if (HANDLE_EINTR(close(accept_fd)) < 0)
+ PLOG(ERROR) << "close " << accept_fd;
+ return false;
+ }
+
+ *server_socket = accept_fd;
+ return true;
+}
+
+bool CreateClientUnixDomainSocket(const std::string& pipe_name,
+ int* client_socket) {
+ DCHECK(client_socket);
+ DCHECK_GT(pipe_name.length(), 0u);
+ DCHECK_LT(pipe_name.length(), kMaxPipeNameLength);
+
+ if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) {
+ return false;
+ }
+
+ // Create socket.
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (fd < 0) {
+ PLOG(ERROR) << "socket " << pipe_name;
+ return false;
+ }
+
+ // Make socket non-blocking
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
+ PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name;
+ if (HANDLE_EINTR(close(fd)) < 0)
+ PLOG(ERROR) << "close " << pipe_name;
+ return false;
+ }
+
+ // Create server side of socket.
+ struct sockaddr_un server_unix_addr;
+ memset(&server_unix_addr, 0, sizeof(server_unix_addr));
+ server_unix_addr.sun_family = AF_UNIX;
+ int path_len = snprintf(server_unix_addr.sun_path, IPC::kMaxPipeNameLength,
+ "%s", pipe_name.c_str());
+ DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len);
+ size_t server_unix_addr_len = offsetof(struct sockaddr_un,
+ sun_path) + path_len + 1;
+
+ if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr),
+ server_unix_addr_len)) != 0) {
+ PLOG(ERROR) << "connect " << pipe_name;
+ if (HANDLE_EINTR(close(fd)) < 0)
+ PLOG(ERROR) << "close " << pipe_name;
+ return false;
+ }
+
+ *client_socket = fd;
+ return true;
+}
bool SocketWriteErrorIsRecoverable() {
#if defined(OS_MACOSX)
@@ -252,14 +388,12 @@ bool Channel::ChannelImpl::CreatePipe(
} else if (mode_ & MODE_NAMED_FLAG) {
// Case 2 from comment above.
if (mode_ & MODE_SERVER_FLAG) {
- if (!CreateServerUnixDomainSocket(base::FilePath(pipe_name_),
- &local_pipe)) {
+ if (!CreateServerUnixDomainSocket(pipe_name_, &local_pipe)) {
return false;
}
must_unlink_ = true;
} else if (mode_ & MODE_CLIENT_FLAG) {
- if (!CreateClientUnixDomainSocket(base::FilePath(pipe_name_),
- &local_pipe)) {
+ if (!CreateClientUnixDomainSocket(pipe_name_, &local_pipe)) {
return false;
}
} else {
@@ -540,9 +674,33 @@ bool Channel::ChannelImpl::HasAcceptedConnection() const {
return AcceptsConnections() && pipe_ != -1;
}
-bool Channel::ChannelImpl::GetPeerEuid(uid_t* peer_euid) const {
- DCHECK(!(mode_ & MODE_SERVER) || HasAcceptedConnection());
- return IPC::GetPeerEuid(pipe_, peer_euid);
+bool Channel::ChannelImpl::GetClientEuid(uid_t* client_euid) const {
+ DCHECK(HasAcceptedConnection());
+#if defined(OS_MACOSX) || defined(OS_OPENBSD)
+ uid_t peer_euid;
+ gid_t peer_gid;
+ if (getpeereid(pipe_, &peer_euid, &peer_gid) != 0) {
+ PLOG(ERROR) << "getpeereid " << pipe_;
+ return false;
+ }
+ *client_euid = peer_euid;
+ return true;
+#elif defined(OS_SOLARIS)
+ return false;
+#else
+ struct ucred cred;
+ socklen_t cred_len = sizeof(cred);
+ if (getsockopt(pipe_, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != 0) {
+ PLOG(ERROR) << "getsockopt " << pipe_;
+ return false;
+ }
+ if (static_cast<unsigned>(cred_len) < sizeof(cred)) {
+ NOTREACHED() << "Truncated ucred from SO_PEERCRED?";
+ return false;
+ }
+ *client_euid = cred.uid;
+ return true;
+#endif
}
void Channel::ChannelImpl::ResetToAcceptingConnectionState() {
@@ -595,8 +753,7 @@ void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) {
bool send_server_hello_msg = false;
if (fd == server_listen_pipe_) {
int new_pipe = 0;
- if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe) ||
- new_pipe < 0) {
+ if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe)) {
Close();
listener()->OnChannelListenError();
}
@@ -616,7 +773,7 @@ void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) {
if ((mode_ & MODE_OPEN_ACCESS_FLAG) == 0) {
// Verify that the IPC channel peer is running as the same user.
uid_t client_euid;
- if (!GetPeerEuid(&client_euid)) {
+ if (!GetClientEuid(&client_euid)) {
DLOG(ERROR) << "Unable to query client euid";
ResetToAcceptingConnectionState();
return;
@@ -1000,8 +1157,8 @@ bool Channel::HasAcceptedConnection() const {
return channel_impl_->HasAcceptedConnection();
}
-bool Channel::GetPeerEuid(uid_t* peer_euid) const {
- return channel_impl_->GetPeerEuid(peer_euid);
+bool Channel::GetClientEuid(uid_t* client_euid) const {
+ return channel_impl_->GetClientEuid(client_euid);
}
void Channel::ResetToAcceptingConnectionState() {