Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/ipc/glue/Transport_posix.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include <unistd.h>
8
9
#include <string>
10
11
#include "base/eintr_wrapper.h"
12
13
#include "mozilla/ipc/Transport.h"
14
#include "mozilla/ipc/FileDescriptor.h"
15
#include "ProtocolUtils.h"
16
17
using namespace std;
18
19
using base::ProcessHandle;
20
21
namespace mozilla {
22
namespace ipc {
23
24
nsresult
25
CreateTransport(base::ProcessId aProcIdOne,
26
                TransportDescriptor* aOne,
27
                TransportDescriptor* aTwo)
28
0
{
29
0
  wstring id = IPC::Channel::GenerateVerifiedChannelID(std::wstring());
30
0
  // Use MODE_SERVER to force creation of the socketpair
31
0
  Transport t(id, Transport::MODE_SERVER, nullptr);
32
0
  int fd1 = t.GetFileDescriptor();
33
0
  int fd2, dontcare;
34
0
  t.GetClientFileDescriptorMapping(&fd2, &dontcare);
35
0
  if (fd1 < 0 || fd2 < 0) {
36
0
    return NS_ERROR_TRANSPORT_INIT;
37
0
  }
38
0
39
0
  // The Transport closes these fds when it goes out of scope, so we
40
0
  // dup them here
41
0
  fd1 = dup(fd1);
42
0
  if (fd1 < 0) {
43
0
    AnnotateCrashReportWithErrno(
44
0
      CrashReporter::Annotation::IpcCreateTransportDupErrno, errno);
45
0
  }
46
0
  fd2 = dup(fd2);
47
0
  if (fd2 < 0) {
48
0
    AnnotateCrashReportWithErrno(
49
0
      CrashReporter::Annotation::IpcCreateTransportDupErrno, errno);
50
0
  }
51
0
52
0
  if (fd1 < 0 || fd2 < 0) {
53
0
    IGNORE_EINTR(close(fd1));
54
0
    IGNORE_EINTR(close(fd2));
55
0
    return NS_ERROR_DUPLICATE_HANDLE;
56
0
  }
57
0
58
0
  aOne->mFd = base::FileDescriptor(fd1, true/*close after sending*/);
59
0
  aTwo->mFd = base::FileDescriptor(fd2, true/*close after sending*/);
60
0
  return NS_OK;
61
0
}
62
63
UniquePtr<Transport>
64
OpenDescriptor(const TransportDescriptor& aTd, Transport::Mode aMode)
65
0
{
66
0
  return MakeUnique<Transport>(aTd.mFd.fd, aMode, nullptr);
67
0
}
68
69
UniquePtr<Transport>
70
OpenDescriptor(const FileDescriptor& aFd, Transport::Mode aMode)
71
0
{
72
0
  auto rawFD = aFd.ClonePlatformHandle();
73
0
  return MakeUnique<Transport>(rawFD.release(), aMode, nullptr);
74
0
}
75
76
TransportDescriptor
77
DuplicateDescriptor(const TransportDescriptor& aTd)
78
0
{
79
0
  TransportDescriptor result = aTd;
80
0
  result.mFd.fd = dup(aTd.mFd.fd);
81
0
  if (result.mFd.fd == -1) {
82
0
    AnnotateSystemError();
83
0
  }
84
0
  MOZ_RELEASE_ASSERT(result.mFd.fd != -1, "DuplicateDescriptor failed");
85
0
  return result;
86
0
}
87
88
void
89
CloseDescriptor(const TransportDescriptor& aTd)
90
0
{
91
0
  close(aTd.mFd.fd);
92
0
}
93
94
} // namespace ipc
95
} // namespace mozilla