ipc/glue/Transport_posix.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 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * vim: sw=2 ts=8 et :
     3  */
     4 /* This Source Code Form is subject to the terms of the Mozilla Public
     5  * License, v. 2.0. If a copy of the MPL was not distributed with this
     6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     8 #include <unistd.h>
    10 #include <string>
    12 #include "chrome/common/child_process_info.h"
    14 #include "mozilla/ipc/Transport.h"
    15 #include "mozilla/ipc/FileDescriptor.h"
    17 using namespace base;
    18 using namespace std;
    20 namespace mozilla {
    21 namespace ipc {
    23 bool
    24 CreateTransport(ProcessHandle /*unused*/, ProcessHandle /*unused*/,
    25                 TransportDescriptor* aOne, TransportDescriptor* aTwo)
    26 {
    27   // Gecko doesn't care about this random ID, and the argument to this
    28   // function isn't really necessary, it can be just any random
    29   // pointer value
    30   wstring id = ChildProcessInfo::GenerateRandomChannelID(aOne);
    31   // Use MODE_SERVER to force creation of the socketpair
    32   Transport t(id, Transport::MODE_SERVER, nullptr);
    33   int fd1 = t.GetFileDescriptor();
    34   int fd2, dontcare;
    35   t.GetClientFileDescriptorMapping(&fd2, &dontcare);
    36   if (fd1 < 0 || fd2 < 0) {
    37     return false;
    38   }
    40   // The Transport closes these fds when it goes out of scope, so we
    41   // dup them here
    42   fd1 = dup(fd1);
    43   fd2 = dup(fd2);
    44   if (fd1 < 0 || fd2 < 0) {
    45     return false;
    46   }
    48   aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/);
    49   aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/);
    50   return true;
    51 }
    53 Transport*
    54 OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode)
    55 {
    56   return new Transport(aTd.mFd.fd, aMode, nullptr);
    57 }
    59 Transport*
    60 OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode)
    61 {
    62   return new Transport(aFd.PlatformHandle(), aMode, nullptr);
    63 }
    65 void
    66 CloseDescriptor(const TransportDescriptor& aTd)
    67 {
    68   close(aTd.mFd.fd);
    69 }
    71 } // namespace ipc
    72 } // namespace mozilla

mercurial