2012-09-05 20:06:06 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "UnixSocket.h"
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include <fcntl.h>
|
2012-09-05 20:06:06 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "base/eintr_wrapper.h"
|
|
|
|
#include "base/message_loop.h"
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "mozilla/Util.h"
|
|
|
|
#include "mozilla/FileUtils.h"
|
|
|
|
#include "nsString.h"
|
2012-09-05 20:06:06 -07:00
|
|
|
#include "nsThreadUtils.h"
|
2012-09-25 13:13:15 -07:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
2012-09-05 20:06:06 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
class UnixSocketImpl : public MessageLoopForIO::Watcher
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
2012-09-30 23:17:46 -07:00
|
|
|
UnixSocketImpl(UnixSocketConsumer* aConsumer, int aFd)
|
2012-09-25 13:13:15 -07:00
|
|
|
: mConsumer(aConsumer)
|
|
|
|
, mIOLoop(nullptr)
|
2012-09-30 23:17:46 -07:00
|
|
|
, mFd(aFd)
|
2012-09-25 13:13:15 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~UnixSocketImpl()
|
|
|
|
{
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QueueWriteData(UnixSocketRawData* aData)
|
|
|
|
{
|
|
|
|
mOutgoingQ.AppendElement(aData);
|
|
|
|
OnFileCanWriteWithoutBlocking(mFd);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFdValid()
|
|
|
|
{
|
|
|
|
return mFd > 0;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
|
|
|
|
void SetUpIO()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIOLoop);
|
|
|
|
mIOLoop = MessageLoopForIO::current();
|
|
|
|
mIOLoop->WatchFileDescriptor(mFd,
|
|
|
|
true,
|
|
|
|
MessageLoopForIO::WATCH_READ,
|
|
|
|
&mReadWatcher,
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrepareRemoval()
|
|
|
|
{
|
|
|
|
mConsumer.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Consumer pointer. Non-thread safe RefPtr, so should only be manipulated
|
|
|
|
* directly from main thread. All non-main-thread accesses should happen with
|
|
|
|
* mImpl as container.
|
|
|
|
*/
|
|
|
|
RefPtr<UnixSocketConsumer> mConsumer;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* libevent triggered functions that reads data from socket when available and
|
|
|
|
* guarenteed non-blocking. Only to be called on IO thread.
|
|
|
|
*
|
|
|
|
* @param aFd File descriptor to read from
|
|
|
|
*/
|
|
|
|
virtual void OnFileCanReadWithoutBlocking(int aFd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* libevent or developer triggered functions that writes data to socket when
|
|
|
|
* available and guarenteed non-blocking. Only to be called on IO thread.
|
|
|
|
*
|
|
|
|
* @param aFd File descriptor to read from
|
|
|
|
*/
|
|
|
|
virtual void OnFileCanWriteWithoutBlocking(int aFd);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* IO Loop pointer. Must be initalized and called from IO thread only.
|
|
|
|
*/
|
|
|
|
MessageLoopForIO* mIOLoop;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Raw data queue. Must be pushed/popped from IO thread only.
|
|
|
|
*/
|
|
|
|
typedef nsTArray<UnixSocketRawData* > UnixSocketRawDataQueue;
|
|
|
|
UnixSocketRawDataQueue mOutgoingQ;
|
|
|
|
|
2012-09-30 22:54:27 -07:00
|
|
|
/**
|
|
|
|
* File descriptor to read from/write to. Connection happens on user provided
|
|
|
|
* thread. Read/write/close happens on IO thread.
|
|
|
|
*/
|
|
|
|
ScopedClose mFd;
|
|
|
|
|
|
|
|
/**
|
2012-09-30 23:17:46 -07:00
|
|
|
* Incoming packet. Only to be accessed on IO Thread.
|
2012-09-30 22:54:27 -07:00
|
|
|
*/
|
2012-09-30 23:17:46 -07:00
|
|
|
nsAutoPtr<UnixSocketRawData> mIncoming;
|
2012-09-30 22:54:27 -07:00
|
|
|
|
|
|
|
/**
|
2012-09-30 23:17:46 -07:00
|
|
|
* Read watcher for libevent. Only to be accessed on IO Thread.
|
2012-09-30 22:54:27 -07:00
|
|
|
*/
|
2012-09-30 23:17:46 -07:00
|
|
|
MessageLoopForIO::FileDescriptorWatcher mReadWatcher;
|
2012-09-30 22:54:27 -07:00
|
|
|
|
|
|
|
/**
|
2012-09-30 23:17:46 -07:00
|
|
|
* Write watcher for libevent. Only to be accessed on IO Thread.
|
2012-09-30 22:54:27 -07:00
|
|
|
*/
|
2012-09-30 23:17:46 -07:00
|
|
|
MessageLoopForIO::FileDescriptorWatcher mWriteWatcher;
|
2012-09-25 13:13:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
DestroyImpl(UnixSocketImpl* impl)
|
|
|
|
{
|
|
|
|
delete impl;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
class SocketReceiveTask : public nsRunnable
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
public:
|
|
|
|
SocketReceiveTask(UnixSocketImpl* aImpl, UnixSocketRawData* aData) :
|
|
|
|
mImpl(aImpl),
|
|
|
|
mRawData(aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aData);
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if(!mImpl->mConsumer) {
|
|
|
|
NS_WARNING("mConsumer is null, aborting receive!");
|
|
|
|
// Since we've already explicitly closed and the close happened before
|
|
|
|
// this, this isn't really an error. Since we've warned, return OK.
|
|
|
|
return NS_OK;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
mImpl->mConsumer->ReceiveSocketData(mRawData);
|
|
|
|
return NS_OK;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
nsAutoPtr<UnixSocketRawData> mRawData;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
class SocketSendTask : public Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SocketSendTask(UnixSocketConsumer* aConsumer, UnixSocketImpl* aImpl,
|
|
|
|
UnixSocketRawData* aData)
|
|
|
|
: mConsumer(aConsumer),
|
|
|
|
mImpl(aImpl),
|
|
|
|
mData(aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aConsumer);
|
|
|
|
MOZ_ASSERT(aImpl);
|
|
|
|
MOZ_ASSERT(aData);
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
void Run()
|
|
|
|
{
|
|
|
|
mImpl->QueueWriteData(mData);
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<UnixSocketConsumer> mConsumer;
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
UnixSocketRawData* mData;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
class StartImplReadingTask : public Task
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StartImplReadingTask(UnixSocketImpl* aImpl)
|
|
|
|
: mImpl(aImpl)
|
|
|
|
{
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
void Run()
|
|
|
|
{
|
|
|
|
mImpl->SetUpIO();
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
private:
|
|
|
|
UnixSocketImpl* mImpl;
|
|
|
|
};
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-30 23:17:46 -07:00
|
|
|
bool
|
|
|
|
UnixSocketConnector::Connect(int aFd, const char* aAddress)
|
2012-09-30 22:54:27 -07:00
|
|
|
{
|
2012-09-30 23:17:46 -07:00
|
|
|
if (!ConnectInternal(aFd, aAddress))
|
2012-09-30 22:54:27 -07:00
|
|
|
{
|
2012-09-30 23:17:46 -07:00
|
|
|
return false;
|
2012-09-30 22:54:27 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
// Set close-on-exec bit.
|
2012-09-30 23:17:46 -07:00
|
|
|
int flags = fcntl(aFd, F_GETFD);
|
2012-09-25 13:13:15 -07:00
|
|
|
if (-1 == flags) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
flags |= FD_CLOEXEC;
|
2012-09-30 23:17:46 -07:00
|
|
|
if (-1 == fcntl(aFd, F_SETFD, flags)) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
// Select non-blocking IO.
|
2012-09-30 23:17:46 -07:00
|
|
|
if (-1 == fcntl(aFd, F_SETFL, O_NONBLOCK)) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
UnixSocketConsumer::~UnixSocketConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(UnixSocketRawData* aData)
|
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new SocketSendTask(this, mImpl, aData));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnixSocketConsumer::SendSocketData(const nsACString& aStr)
|
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (aStr.Length() > UnixSocketRawData::MAX_DATA_SIZE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCString str(aStr);
|
|
|
|
UnixSocketRawData* d = new UnixSocketRawData(aStr.Length());
|
|
|
|
memcpy(d->mData, str.get(), aStr.Length());
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
new SocketSendTask(this, mImpl, d));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UnixSocketConsumer::CloseSocket()
|
|
|
|
{
|
|
|
|
if (!mImpl) {
|
|
|
|
return;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
2012-09-25 13:13:15 -07:00
|
|
|
// To make sure the owner doesn't die on the IOThread, remove pointer here
|
|
|
|
mImpl->PrepareRemoval();
|
|
|
|
// Line it up to be destructed on the IO Thread
|
|
|
|
// Kill our pointer to it
|
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
|
|
|
NewRunnableFunction(DestroyImpl,
|
|
|
|
mImpl.forget()));
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
void
|
|
|
|
UnixSocketImpl::OnFileCanReadWithoutBlocking(int aFd)
|
|
|
|
{
|
|
|
|
// Keep reading data until either
|
|
|
|
//
|
|
|
|
// - mIncoming is completely read
|
|
|
|
// If so, sConsumer->MessageReceived(mIncoming.forget())
|
|
|
|
//
|
|
|
|
// - mIncoming isn't completely read, but there's no more
|
|
|
|
// data available on the socket
|
|
|
|
// If so, break;
|
|
|
|
while (true) {
|
|
|
|
if (!mIncoming) {
|
|
|
|
mIncoming = new UnixSocketRawData();
|
|
|
|
ssize_t ret = read(aFd, mIncoming->mData, UnixSocketRawData::MAX_DATA_SIZE);
|
|
|
|
if (ret <= 0) {
|
|
|
|
if (ret == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue; // retry system call when interrupted
|
|
|
|
}
|
|
|
|
else if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
mIncoming.forget();
|
|
|
|
return; // no data available: return and re-poll
|
|
|
|
}
|
|
|
|
// else fall through to error handling on other errno's
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoString str;
|
|
|
|
str.AssignLiteral("Cannot read from network, error ");
|
|
|
|
str += (int)ret;
|
|
|
|
NS_WARNING(NS_ConvertUTF16toUTF8(str).get());
|
|
|
|
#endif
|
|
|
|
// At this point, assume that we can't actually access
|
|
|
|
// the socket anymore
|
|
|
|
mIncoming.forget();
|
|
|
|
mReadWatcher.StopWatchingFileDescriptor();
|
|
|
|
mWriteWatcher.StopWatchingFileDescriptor();
|
|
|
|
mConsumer->CloseSocket();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIncoming->mData[ret] = 0;
|
|
|
|
mIncoming->mSize = ret;
|
|
|
|
nsRefPtr<SocketReceiveTask> t =
|
|
|
|
new SocketReceiveTask(this, mIncoming.forget());
|
|
|
|
NS_DispatchToMainThread(t);
|
|
|
|
if (ret < ssize_t(UnixSocketRawData::MAX_DATA_SIZE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
void
|
|
|
|
UnixSocketImpl::OnFileCanWriteWithoutBlocking(int aFd)
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
2012-09-25 13:13:15 -07:00
|
|
|
// Try to write the bytes of mCurrentRilRawData. If all were written, continue.
|
|
|
|
//
|
|
|
|
// Otherwise, save the byte position of the next byte to write
|
|
|
|
// within mCurrentRilRawData, and request another write when the
|
|
|
|
// system won't block.
|
|
|
|
//
|
|
|
|
while (true) {
|
|
|
|
UnixSocketRawData* data;
|
|
|
|
if (mOutgoingQ.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data = mOutgoingQ.ElementAt(0);
|
|
|
|
const uint8_t *toWrite;
|
|
|
|
toWrite = data->mData;
|
|
|
|
|
|
|
|
while (data->mCurrentWriteOffset < data->mSize) {
|
|
|
|
ssize_t write_amount = data->mSize - data->mCurrentWriteOffset;
|
|
|
|
ssize_t written;
|
|
|
|
written = write (aFd, toWrite + data->mCurrentWriteOffset,
|
|
|
|
write_amount);
|
|
|
|
if (written > 0) {
|
|
|
|
data->mCurrentWriteOffset += written;
|
|
|
|
}
|
|
|
|
if (written != write_amount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->mCurrentWriteOffset != data->mSize) {
|
|
|
|
MessageLoopForIO::current()->WatchFileDescriptor(
|
|
|
|
aFd,
|
|
|
|
false,
|
|
|
|
MessageLoopForIO::WATCH_WRITE,
|
|
|
|
&mWriteWatcher,
|
|
|
|
this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mOutgoingQ.RemoveElementAt(0);
|
|
|
|
delete data;
|
|
|
|
}
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
2012-09-25 13:13:15 -07:00
|
|
|
bool
|
2012-09-30 23:17:46 -07:00
|
|
|
UnixSocketConsumer::ConnectSocket(UnixSocketConnector& aConnector,
|
2012-09-25 13:13:15 -07:00
|
|
|
const char* aAddress)
|
2012-09-05 20:06:06 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2012-09-30 23:17:46 -07:00
|
|
|
MOZ_ASSERT(!mImpl);
|
|
|
|
ScopedClose fd(aConnector.Create());
|
|
|
|
if (fd < 0) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-30 23:17:46 -07:00
|
|
|
if (!aConnector.Connect(fd, aAddress)) {
|
2012-09-25 13:13:15 -07:00
|
|
|
return false;
|
|
|
|
}
|
2012-09-30 23:17:46 -07:00
|
|
|
mImpl = new UnixSocketImpl(this, fd.forget());
|
2012-09-25 13:13:15 -07:00
|
|
|
XRE_GetIOMessageLoop()->PostTask(FROM_HERE,
|
2012-09-30 23:17:46 -07:00
|
|
|
new StartImplReadingTask(mImpl));
|
2012-09-25 13:13:15 -07:00
|
|
|
return true;
|
2012-09-05 20:06:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|