ipc/glue/FileDescriptor.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "FileDescriptor.h"
     7 #include "mozilla/Assertions.h"
     8 #include "nsDebug.h"
    10 #ifdef XP_WIN
    12 #include <windows.h>
    13 #define INVALID_HANDLE INVALID_HANDLE_VALUE
    15 #else // XP_WIN
    17 #include <unistd.h>
    19 #ifndef OS_POSIX
    20 #define OS_POSIX
    21 #endif
    23 #include "base/eintr_wrapper.h"
    24 #define INVALID_HANDLE -1
    26 #endif // XP_WIN
    28 using mozilla::ipc::FileDescriptor;
    30 FileDescriptor::FileDescriptor()
    31 : mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false),
    32   mHandleCreatedByOtherProcessWasUsed(false)
    33 { }
    35 FileDescriptor::FileDescriptor(PlatformHandleType aHandle)
    36 : mHandle(INVALID_HANDLE), mHandleCreatedByOtherProcess(false),
    37   mHandleCreatedByOtherProcessWasUsed(false)
    38 {
    39   DuplicateInCurrentProcess(aHandle);
    40 }
    42 void
    43 FileDescriptor::DuplicateInCurrentProcess(PlatformHandleType aHandle)
    44 {
    45   MOZ_ASSERT_IF(mHandleCreatedByOtherProcess,
    46                 mHandleCreatedByOtherProcessWasUsed);
    48   if (IsValid(aHandle)) {
    49     PlatformHandleType newHandle;
    50 #ifdef XP_WIN
    51     if (DuplicateHandle(GetCurrentProcess(), aHandle, GetCurrentProcess(),
    52                         &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
    53 #else // XP_WIN
    54     if ((newHandle = dup(aHandle)) != INVALID_HANDLE) {
    55 #endif
    56       mHandle = newHandle;
    57       return;
    58     }
    59     NS_WARNING("Failed to duplicate file handle for current process!");
    60   }
    62   mHandle = INVALID_HANDLE;
    63 }
    65 void
    66 FileDescriptor::CloseCurrentProcessHandle()
    67 {
    68   MOZ_ASSERT_IF(mHandleCreatedByOtherProcess,
    69                 mHandleCreatedByOtherProcessWasUsed);
    71   // Don't actually close handles created by another process.
    72   if (mHandleCreatedByOtherProcess) {
    73     return;
    74   }
    76   if (IsValid()) {
    77 #ifdef XP_WIN
    78     if (!CloseHandle(mHandle)) {
    79       NS_WARNING("Failed to close file handle for current process!");
    80     }
    81 #else // XP_WIN
    82     HANDLE_EINTR(close(mHandle));
    83 #endif
    84     mHandle = INVALID_HANDLE;
    85   }
    86 }
    88 FileDescriptor::PickleType
    89 FileDescriptor::ShareTo(const FileDescriptor::IPDLPrivate&,
    90                         FileDescriptor::ProcessHandle aOtherProcess) const
    91 {
    92   PlatformHandleType newHandle;
    93 #ifdef XP_WIN
    94   if (IsValid()) {
    95     if (DuplicateHandle(GetCurrentProcess(), mHandle, aOtherProcess,
    96                         &newHandle, 0, FALSE, DUPLICATE_SAME_ACCESS)) {
    97       return newHandle;
    98     }
    99     NS_WARNING("Failed to duplicate file handle for other process!");
   100   }
   101   return INVALID_HANDLE;
   102 #else // XP_WIN
   103   if (IsValid()) {
   104     newHandle = dup(mHandle);
   105     if (IsValid(newHandle)) {
   106       return base::FileDescriptor(newHandle, /* auto_close */ true);
   107     }
   108     NS_WARNING("Failed to duplicate file handle for other process!");
   109   }
   110   return base::FileDescriptor();
   111 #endif
   113   MOZ_CRASH("Must not get here!");
   114 }
   116 // static
   117 bool
   118 FileDescriptor::IsValid(PlatformHandleType aHandle)
   119 {
   120   return aHandle != INVALID_HANDLE;
   121 }

mercurial