2012-08-16 08:02:32 +04:00
|
|
|
/* 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 "FileDescriptor.h"
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2013-01-10 22:15:57 +04:00
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#define INVALID_HANDLE INVALID_HANDLE_VALUE
|
2013-01-10 22:15:57 +04:00
|
|
|
|
|
|
|
#else // XP_WIN
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
#include <unistd.h>
|
2013-01-10 22:15:57 +04:00
|
|
|
|
|
|
|
#ifndef OS_POSIX
|
|
|
|
#define OS_POSIX
|
2013-01-10 23:15:25 +04:00
|
|
|
#endif
|
2013-01-10 22:15:57 +04:00
|
|
|
|
2013-01-10 22:15:57 +04:00
|
|
|
#include "base/eintr_wrapper.h"
|
|
|
|
#define INVALID_HANDLE -1
|
|
|
|
|
|
|
|
#endif // XP_WIN
|
|
|
|
|
2012-08-16 08:02:32 +04:00
|
|
|
using mozilla::ipc::FileDescriptor;
|
|
|
|
|
|
|
|
FileDescriptor::FileDescriptor()
|
2013-01-10 22:15:57 +04:00
|
|
|
: mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false),
|
|
|
|
mHandleCreatedByOtherProcessWasUsed(false)
|
2012-08-16 08:02:32 +04:00
|
|
|
{ }
|
|
|
|
|
2013-01-10 22:15:57 +04:00
|
|
|
FileDescriptor::FileDescriptor(PlatformHandleType aHandle)
|
|
|
|
: mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false),
|
|
|
|
mHandleCreatedByOtherProcessWasUsed(false)
|
|
|
|
{
|
|
|
|
DuplicateInCurrentProcess(aHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileDescriptor::DuplicateInCurrentProcess(PlatformHandleType aHandle)
|
2013-01-10 16:00:17 +04:00
|
|
|
{
|
2013-02-12 02:38:41 +04:00
|
|
|
MOZ_ASSERT(!mHandleCreatedByOtherProcess);
|
2013-01-10 22:15:57 +04:00
|
|
|
|
|
|
|
if (IsValid(aHandle)) {
|
|
|
|
PlatformHandleType newHandle;
|
2013-01-10 16:00:17 +04:00
|
|
|
#ifdef XP_WIN
|
2013-01-10 22:15:57 +04:00
|
|
|
if (DuplicateHandle(GetCurrentProcess(), aHandle, GetCurrentProcess(),
|
|
|
|
&newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
|
|
|
|
#else // XP_WIN
|
|
|
|
if ((newHandle = dup(aHandle)) != INVALID_HANDLE) {
|
|
|
|
#endif
|
|
|
|
mHandle = newHandle;
|
|
|
|
return;
|
|
|
|
}
|
2013-01-25 05:10:39 +04:00
|
|
|
NS_WARNING("Failed to duplicate file handle for current process!");
|
2013-01-10 16:54:27 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 22:15:57 +04:00
|
|
|
mHandle = INVALID_HANDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileDescriptor::CloseCurrentProcessHandle()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT_IF(mHandleCreatedByOtherProcess,
|
|
|
|
mHandleCreatedByOtherProcessWasUsed);
|
|
|
|
|
|
|
|
// Don't actually close handles created by another process.
|
|
|
|
if (mHandleCreatedByOtherProcess) {
|
|
|
|
return;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
2013-01-10 16:54:27 +04:00
|
|
|
|
2013-01-10 22:15:57 +04:00
|
|
|
if (IsValid()) {
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (!CloseHandle(mHandle)) {
|
2013-01-25 05:10:39 +04:00
|
|
|
NS_WARNING("Failed to close file handle for current process!");
|
2013-01-10 22:15:57 +04:00
|
|
|
}
|
2012-08-16 08:02:32 +04:00
|
|
|
#else // XP_WIN
|
2013-01-10 22:15:57 +04:00
|
|
|
HANDLE_EINTR(close(mHandle));
|
|
|
|
#endif
|
|
|
|
mHandle = INVALID_HANDLE;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|
2013-01-10 22:15:57 +04:00
|
|
|
}
|
2013-01-10 16:54:27 +04:00
|
|
|
|
2013-01-10 22:15:57 +04:00
|
|
|
FileDescriptor::PickleType
|
|
|
|
FileDescriptor::ShareTo(const FileDescriptor::IPDLPrivate&,
|
|
|
|
FileDescriptor::ProcessHandle aOtherProcess) const
|
|
|
|
{
|
|
|
|
PlatformHandleType newHandle;
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (IsValid()) {
|
|
|
|
if (DuplicateHandle(GetCurrentProcess(), mHandle, aOtherProcess,
|
|
|
|
&newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
|
|
|
|
return newHandle;
|
|
|
|
}
|
2013-01-25 05:10:39 +04:00
|
|
|
NS_WARNING("Failed to duplicate file handle for other process!");
|
2013-01-10 22:15:57 +04:00
|
|
|
}
|
2013-01-10 22:15:57 +04:00
|
|
|
return INVALID_HANDLE;
|
|
|
|
#else // XP_WIN
|
|
|
|
if (IsValid()) {
|
|
|
|
newHandle = dup(mHandle);
|
2013-01-25 05:10:39 +04:00
|
|
|
if (IsValid(newHandle)) {
|
|
|
|
return base::FileDescriptor(newHandle, /* auto_close */ true);
|
|
|
|
}
|
|
|
|
NS_WARNING("Failed to duplicate file handle for other process!");
|
2013-01-10 22:15:57 +04:00
|
|
|
}
|
|
|
|
return base::FileDescriptor();
|
2012-08-16 08:02:32 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
MOZ_NOT_REACHED("Must not get here!");
|
|
|
|
return PickleType();
|
|
|
|
}
|
|
|
|
|
2013-01-10 22:15:57 +04:00
|
|
|
// static
|
2012-08-16 08:02:32 +04:00
|
|
|
bool
|
2013-01-10 22:15:57 +04:00
|
|
|
FileDescriptor::IsValid(PlatformHandleType aHandle)
|
2012-08-16 08:02:32 +04:00
|
|
|
{
|
2013-01-10 22:15:57 +04:00
|
|
|
return aHandle != INVALID_HANDLE;
|
2012-08-16 08:02:32 +04:00
|
|
|
}
|