1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/glue/Transport_posix.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=8 et : 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#include <unistd.h> 1.12 + 1.13 +#include <string> 1.14 + 1.15 +#include "chrome/common/child_process_info.h" 1.16 + 1.17 +#include "mozilla/ipc/Transport.h" 1.18 +#include "mozilla/ipc/FileDescriptor.h" 1.19 + 1.20 +using namespace base; 1.21 +using namespace std; 1.22 + 1.23 +namespace mozilla { 1.24 +namespace ipc { 1.25 + 1.26 +bool 1.27 +CreateTransport(ProcessHandle /*unused*/, ProcessHandle /*unused*/, 1.28 + TransportDescriptor* aOne, TransportDescriptor* aTwo) 1.29 +{ 1.30 + // Gecko doesn't care about this random ID, and the argument to this 1.31 + // function isn't really necessary, it can be just any random 1.32 + // pointer value 1.33 + wstring id = ChildProcessInfo::GenerateRandomChannelID(aOne); 1.34 + // Use MODE_SERVER to force creation of the socketpair 1.35 + Transport t(id, Transport::MODE_SERVER, nullptr); 1.36 + int fd1 = t.GetFileDescriptor(); 1.37 + int fd2, dontcare; 1.38 + t.GetClientFileDescriptorMapping(&fd2, &dontcare); 1.39 + if (fd1 < 0 || fd2 < 0) { 1.40 + return false; 1.41 + } 1.42 + 1.43 + // The Transport closes these fds when it goes out of scope, so we 1.44 + // dup them here 1.45 + fd1 = dup(fd1); 1.46 + fd2 = dup(fd2); 1.47 + if (fd1 < 0 || fd2 < 0) { 1.48 + return false; 1.49 + } 1.50 + 1.51 + aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/); 1.52 + aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/); 1.53 + return true; 1.54 +} 1.55 + 1.56 +Transport* 1.57 +OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode) 1.58 +{ 1.59 + return new Transport(aTd.mFd.fd, aMode, nullptr); 1.60 +} 1.61 + 1.62 +Transport* 1.63 +OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode) 1.64 +{ 1.65 + return new Transport(aFd.PlatformHandle(), aMode, nullptr); 1.66 +} 1.67 + 1.68 +void 1.69 +CloseDescriptor(const TransportDescriptor& aTd) 1.70 +{ 1.71 + close(aTd.mFd.fd); 1.72 +} 1.73 + 1.74 +} // namespace ipc 1.75 +} // namespace mozilla