Skip to content

Commit

Permalink
Bug 1534780 - Move ipc::FileDescriptor's UniquePtr instance into MFBT…
Browse files Browse the repository at this point in the history
… as UniqueFileHandle. r=froydnj

MozReview-Commit-ID: 7bbGVIjTTaJ

Differential Revision: https://phabricator.services.mozilla.com/D26737
  • Loading branch information
jld committed Jun 28, 2019
1 parent d75cd71 commit ae8c2c2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 140 deletions.
133 changes: 32 additions & 101 deletions ipc/glue/FileDescriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,104 +7,71 @@
#include "FileDescriptor.h"

#include "mozilla/Assertions.h"
#include "mozilla/Move.h"
#include "mozilla/TypeTraits.h"
#include "nsDebug.h"

#ifdef XP_WIN

# include <windows.h>
# include "ProtocolUtils.h"
# define INVALID_HANDLE INVALID_HANDLE_VALUE

#else // XP_WIN

# include <unistd.h>

# ifndef OS_POSIX
# define OS_POSIX
# endif

# include "base/eintr_wrapper.h"
# define INVALID_HANDLE -1

#endif // XP_WIN

namespace mozilla {
namespace ipc {

FileDescriptor::FileDescriptor() : mHandle(INVALID_HANDLE) {}
FileDescriptor::FileDescriptor() = default;

FileDescriptor::FileDescriptor(const FileDescriptor& aOther)
: mHandle(INVALID_HANDLE) {
Assign(aOther);
}
: mHandle(Clone(aOther.mHandle.get())) {}

FileDescriptor::FileDescriptor(FileDescriptor&& aOther)
: mHandle(INVALID_HANDLE) {
*this = std::move(aOther);
}
: mHandle(std::move(aOther.mHandle)) {}

FileDescriptor::FileDescriptor(PlatformHandleType aHandle)
: mHandle(INVALID_HANDLE) {
mHandle = Clone(aHandle);
}
: mHandle(Clone(aHandle)) {}

FileDescriptor::FileDescriptor(const IPDLPrivate&, const PickleType& aPickle)
: mHandle(INVALID_HANDLE) {
FileDescriptor::FileDescriptor(const IPDLPrivate&, const PickleType& aPickle) {
#ifdef XP_WIN
mHandle = aPickle;
mHandle.reset(aPickle);
#else
mHandle = aPickle.fd;
mHandle.reset(aPickle.fd);
#endif
}

FileDescriptor::~FileDescriptor() { Close(); }
FileDescriptor::~FileDescriptor() = default;

FileDescriptor& FileDescriptor::operator=(const FileDescriptor& aOther) {
if (this != &aOther) {
Assign(aOther);
mHandle = Clone(aOther.mHandle.get());
}
return *this;
}

FileDescriptor& FileDescriptor::operator=(FileDescriptor&& aOther) {
if (this != &aOther) {
Close();
mHandle = aOther.mHandle;
aOther.mHandle = INVALID_HANDLE;
mHandle = std::move(aOther.mHandle);
}
return *this;
}

bool FileDescriptor::IsValid() const { return IsValid(mHandle); }

void FileDescriptor::Assign(const FileDescriptor& aOther) {
Close();
mHandle = Clone(aOther.mHandle);
}

void FileDescriptor::Close() {
Close(mHandle);
mHandle = INVALID_HANDLE;
}

FileDescriptor::PickleType FileDescriptor::ShareTo(
const FileDescriptor::IPDLPrivate&,
FileDescriptor::ProcessId aTargetPid) const {
PlatformHandleType newHandle;
#ifdef XP_WIN
if (IsValid()) {
if (mozilla::ipc::DuplicateHandle(mHandle, aTargetPid, &newHandle, 0,
if (mozilla::ipc::DuplicateHandle(mHandle.get(), aTargetPid, &newHandle, 0,
DUPLICATE_SAME_ACCESS)) {
return newHandle;
}
NS_WARNING("Failed to duplicate file handle for other process!");
}
return INVALID_HANDLE;
return INVALID_HANDLE_VALUE;
#else // XP_WIN
if (IsValid()) {
newHandle = dup(mHandle);
if (IsValid(newHandle)) {
newHandle = dup(mHandle.get());
if (newHandle >= 0) {
return base::FileDescriptor(newHandle, /* auto_close */ true);
}
NS_WARNING("Failed to duplicate file handle for other process!");
Expand All @@ -115,77 +82,41 @@ FileDescriptor::PickleType FileDescriptor::ShareTo(
MOZ_CRASH("Must not get here!");
}

bool FileDescriptor::IsValid() const { return mHandle != nullptr; }

FileDescriptor::UniquePlatformHandle FileDescriptor::ClonePlatformHandle()
const {
return UniquePlatformHandle(Clone(mHandle));
return Clone(mHandle.get());
}

bool FileDescriptor::operator==(const FileDescriptor& aOther) const {
return mHandle == aOther.mHandle;
}

// static
bool FileDescriptor::IsValid(PlatformHandleType aHandle) {
return aHandle != INVALID_HANDLE;
}

// static
FileDescriptor::PlatformHandleType FileDescriptor::Clone(
FileDescriptor::UniquePlatformHandle FileDescriptor::Clone(
PlatformHandleType aHandle) {
if (!IsValid(aHandle)) {
return INVALID_HANDLE;
}
FileDescriptor::PlatformHandleType newHandle;

#ifdef XP_WIN
if (aHandle == INVALID_HANDLE_VALUE) {
return UniqueFileHandle();
}
if (::DuplicateHandle(GetCurrentProcess(), aHandle, GetCurrentProcess(),
&newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
#else // XP_WIN
if ((newHandle = dup(aHandle)) != INVALID_HANDLE) {
#endif
return newHandle;
return UniqueFileHandle(newHandle);
}
NS_WARNING("Failed to duplicate file handle for current process!");
return INVALID_HANDLE;
}

// static
void FileDescriptor::Close(PlatformHandleType aHandle) {
if (IsValid(aHandle)) {
#ifdef XP_WIN
if (!CloseHandle(aHandle)) {
NS_WARNING("Failed to close file handle for current process!");
}
#else // XP_WIN
IGNORE_EINTR(close(aHandle));
#endif
if (aHandle < 0) {
return UniqueFileHandle();
}
newHandle = dup(aHandle);
if (newHandle >= 0) {
return UniqueFileHandle(newHandle);
}
}

FileDescriptor::PlatformHandleHelper::PlatformHandleHelper(
FileDescriptor::PlatformHandleType aHandle)
: mHandle(aHandle) {}

FileDescriptor::PlatformHandleHelper::PlatformHandleHelper(std::nullptr_t)
: mHandle(INVALID_HANDLE) {}

bool FileDescriptor::PlatformHandleHelper::operator!=(std::nullptr_t) const {
return mHandle != INVALID_HANDLE;
}

FileDescriptor::PlatformHandleHelper::
operator FileDescriptor::PlatformHandleType() const {
return mHandle;
}

#ifdef XP_WIN
FileDescriptor::PlatformHandleHelper::operator std::intptr_t() const {
return reinterpret_cast<std::intptr_t>(mHandle);
}
#endif

void FileDescriptor::PlatformHandleDeleter::operator()(
FileDescriptor::PlatformHandleHelper aHelper) {
FileDescriptor::Close(aHelper);
NS_WARNING("Failed to duplicate file handle for current process!");
return UniqueFileHandle();
}

void IPDLParamTraits<FileDescriptor>::Write(IPC::Message* aMsg,
Expand Down
47 changes: 8 additions & 39 deletions ipc/glue/FileDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@

#include "base/basictypes.h"
#include "base/process.h"
#include "mozilla/UniquePtr.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/ipc/IPDLParamTraits.h"

#ifdef XP_WIN
// Need the HANDLE typedef.
# include <winnt.h>
# include <cstdint>
#else
#ifdef XP_UNIX
# include "base/file_descriptor_posix.h"
#endif

Expand All @@ -40,32 +36,15 @@ class FileDescriptor {
public:
typedef base::ProcessId ProcessId;

using UniquePlatformHandle = mozilla::UniqueFileHandle;
using PlatformHandleType = UniquePlatformHandle::ElementType;

#ifdef XP_WIN
typedef HANDLE PlatformHandleType;
typedef HANDLE PickleType;
typedef PlatformHandleType PickleType;
#else
typedef int PlatformHandleType;
typedef base::FileDescriptor PickleType;
#endif

struct PlatformHandleHelper {
MOZ_IMPLICIT PlatformHandleHelper(PlatformHandleType aHandle);
MOZ_IMPLICIT PlatformHandleHelper(std::nullptr_t);
bool operator!=(std::nullptr_t) const;
operator PlatformHandleType() const;
#ifdef XP_WIN
operator std::intptr_t() const;
#endif
private:
PlatformHandleType mHandle;
};
struct PlatformHandleDeleter {
typedef PlatformHandleHelper pointer;
void operator()(PlatformHandleHelper aHelper);
};
typedef UniquePtr<PlatformHandleType, PlatformHandleDeleter>
UniquePlatformHandle;

// This should only ever be created by IPDL.
struct IPDLPrivate {};

Expand Down Expand Up @@ -108,19 +87,9 @@ class FileDescriptor {
bool operator==(const FileDescriptor& aOther) const;

private:
friend struct PlatformHandleTrait;

void Assign(const FileDescriptor& aOther);

void Close();

static bool IsValid(PlatformHandleType aHandle);

static PlatformHandleType Clone(PlatformHandleType aHandle);

static void Close(PlatformHandleType aHandle);
static UniqueFileHandle Clone(PlatformHandleType aHandle);

PlatformHandleType mHandle;
UniquePlatformHandle mHandle;
};

template <>
Expand Down
35 changes: 35 additions & 0 deletions mfbt/UniquePtrExtensions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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/. */

#include "UniquePtrExtensions.h"

#include "mozilla/Assertions.h"
#include "mozilla/DebugOnly.h"

#ifdef XP_WIN
# include <windows.h>
#else
# include <errno.h>
# include <unistd.h>
#endif

namespace mozilla {
namespace detail {

void FileHandleDeleter::operator()(FileHandleHelper aHelper) {
if (aHelper != nullptr) {
DebugOnly<bool> ok;
#ifdef XP_WIN
ok = CloseHandle(aHelper);
#else
ok = close(aHelper) == 0 || errno == EINTR;
#endif
MOZ_ASSERT(ok, "failed to close file handle");
}
}

} // namespace detail
} // namespace mozilla
Loading

0 comments on commit ae8c2c2

Please sign in to comment.