Commit 5e0ad88b authored by cmasone's avatar cmasone Committed by Commit bot

Force creation of application listening socket

If a previous mojo_shell run exited uncleanly, there may
be a unix domain socket sitting at the path that
IncomingConnectionListener wants to own and listen at.
This change destroys it, if possible, and creates a new
one.

BUG=407782
TEST=new unit test, run mojo_shell multiple times in a row with --enable-external-applications

Review URL: https://codereview.chromium.org/611833005

Cr-Commit-Position: refs/heads/master@{#297271}
parent 09cf0975
...@@ -47,15 +47,15 @@ void IncomingConnectionListenerPosix::StartListening() { ...@@ -47,15 +47,15 @@ void IncomingConnectionListenerPosix::StartListening() {
DCHECK(listen_thread_checker_.CalledOnValidThread()); DCHECK(listen_thread_checker_.CalledOnValidThread());
int rv = net::OK; int rv = net::OK;
if (base::PathExists(socket_path_)) { if (!base::DirectoryExists(socket_path_.DirName())) {
LOG(ERROR) << "Listening socket file already exists.";
rv = net::ERR_FILE_EXISTS;
} else if (!base::DirectoryExists(socket_path_.DirName())) {
LOG(ERROR) << "Directorty for listening socket does not exist."; LOG(ERROR) << "Directorty for listening socket does not exist.";
rv = net::ERR_FILE_NOT_FOUND; rv = net::ERR_FILE_NOT_FOUND;
} else if (!base::PathIsWritable(socket_path_.DirName())) { } else if (!base::PathIsWritable(socket_path_.DirName())) {
LOG(ERROR) << "Listening socket file path is not writable."; LOG(ERROR) << "Listening socket file path is not writable.";
rv = net::ERR_ACCESS_DENIED; rv = net::ERR_ACCESS_DENIED;
} else if (!base::DeleteFile(socket_path_, false)) {
PLOG(ERROR) << "Listening socket file exists and can't be deleted";
rv = net::ERR_FILE_EXISTS;
} else { } else {
const std::string& socket_address = socket_path_.value(); const std::string& socket_address = socket_path_.value();
rv = listen_socket_.ListenWithAddressAndPort(socket_address, 0, 100); rv = listen_socket_.ListenWithAddressAndPort(socket_address, 0, 100);
......
...@@ -97,15 +97,40 @@ TEST_F(IncomingConnectionListenerTest, ConnectSuccess) { ...@@ -97,15 +97,40 @@ TEST_F(IncomingConnectionListenerTest, ConnectSuccess) {
run_loop_.Run(); run_loop_.Run();
} }
TEST_F(IncomingConnectionListenerTest, ConnectFails_SocketFileExists) { TEST_F(IncomingConnectionListenerTest, ConnectSuccess_SocketFileExists) {
TestDelegate delegate;
IncomingConnectionListenerPosix listener(socket_path_, &delegate);
ASSERT_EQ(1, base::WriteFile(socket_path_, "1", 1));
ASSERT_TRUE(base::PathExists(socket_path_));
listener.StartListening();
ExternalApplicationRegistrarConnection connection(socket_path_);
connection.Connect(base::Bind(&OnConnect, run_loop_.QuitClosure()));
run_loop_.Run();
}
TEST_F(IncomingConnectionListenerTest, ConnectFails_SocketFileUndeletable) {
ListeningFailsDelegate fail_delegate(net::ERR_FILE_EXISTS); ListeningFailsDelegate fail_delegate(net::ERR_FILE_EXISTS);
IncomingConnectionListenerPosix listener(socket_path_, &fail_delegate); IncomingConnectionListenerPosix listener(socket_path_, &fail_delegate);
// Create the socket file.
ASSERT_EQ(1, base::WriteFile(socket_path_, "1", 1)); ASSERT_EQ(1, base::WriteFile(socket_path_, "1", 1));
ASSERT_TRUE(base::PathExists(socket_path_)); ASSERT_TRUE(base::PathExists(socket_path_));
// Render it undeletable, but in a way that the test harness can recover
// later.
int temp_dir_perms = 0;
ASSERT_TRUE(base::GetPosixFilePermissions(temp_dir_.path(), &temp_dir_perms));
ASSERT_TRUE(
base::SetPosixFilePermissions(temp_dir_.path(),
base::FILE_PERMISSION_READ_BY_USER |
base::FILE_PERMISSION_WRITE_BY_USER));
// The listener should fail to start up. // The listener should fail to start up.
listener.StartListening(); listener.StartListening();
ASSERT_TRUE(base::SetPosixFilePermissions(temp_dir_.path(), temp_dir_perms));
} }
TEST_F(IncomingConnectionListenerTest, ConnectFails_SocketDirNonexistent) { TEST_F(IncomingConnectionListenerTest, ConnectFails_SocketDirNonexistent) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment