summaryrefslogtreecommitdiffstats
path: root/ipc/unixfd/UnixFdWatcher.cpp
blob: 367c1f59756545718b35707687d34c9e4a283287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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 <fcntl.h>
#include "UnixFdWatcher.h"

#ifdef CHROMIUM_LOG
#undef CHROMIUM_LOG
#endif

#include <stdio.h>
#define IODEBUG true
#define CHROMIUM_LOG(args...) if (IODEBUG) printf(args);

namespace mozilla {
namespace ipc {

UnixFdWatcher::~UnixFdWatcher()
{
  NS_WARNING_ASSERTION(!IsOpen(), "mFd should have been closed already");
}

void
UnixFdWatcher::Close()
{
  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);

  if (NS_WARN_IF(!IsOpen())) {
    /* mFd should have been open */
    return;
  }
  OnClose();
  RemoveWatchers(READ_WATCHER|WRITE_WATCHER);
  mFd.dispose();
}

void
UnixFdWatcher::AddWatchers(unsigned long aWatchers, bool aPersistent)
{
  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
  MOZ_ASSERT(IsOpen());

  // Before we add a watcher, we need to remove it! Removing is always
  // safe, but adding the same watcher twice can lead to endless loops
  // inside libevent.
  RemoveWatchers(aWatchers);

  if (aWatchers & READ_WATCHER) {
    MessageLoopForIO::current()->WatchFileDescriptor(
      mFd,
      aPersistent,
      MessageLoopForIO::WATCH_READ,
      &mReadWatcher,
      this);
  }
  if (aWatchers & WRITE_WATCHER) {
    MessageLoopForIO::current()->WatchFileDescriptor(
      mFd,
      aPersistent,
      MessageLoopForIO::WATCH_WRITE,
      &mWriteWatcher,
      this);
  }
}

void
UnixFdWatcher::RemoveWatchers(unsigned long aWatchers)
{
  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
  MOZ_ASSERT(IsOpen());

  if (aWatchers & READ_WATCHER) {
    mReadWatcher.StopWatchingFileDescriptor();
  }
  if (aWatchers & WRITE_WATCHER) {
    mWriteWatcher.StopWatchingFileDescriptor();
  }
}

void
UnixFdWatcher::OnError(const char* aFunction, int aErrno)
{
  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);

  CHROMIUM_LOG("%s failed with error %d (%s)",
               aFunction, aErrno, strerror(aErrno));
}

UnixFdWatcher::UnixFdWatcher(MessageLoop* aIOLoop)
: mIOLoop(aIOLoop)
{
  MOZ_ASSERT(mIOLoop);
}

UnixFdWatcher::UnixFdWatcher(MessageLoop* aIOLoop, int aFd)
: mIOLoop(aIOLoop)
, mFd(aFd)
{
  MOZ_ASSERT(mIOLoop);
}

void
UnixFdWatcher::SetFd(int aFd)
{
  MOZ_ASSERT(MessageLoopForIO::current() == mIOLoop);
  MOZ_ASSERT(!IsOpen());
  MOZ_ASSERT(FdIsNonBlocking(aFd));

  mFd = aFd;
}

bool
UnixFdWatcher::FdIsNonBlocking(int aFd)
{
  int flags = TEMP_FAILURE_RETRY(fcntl(aFd, F_GETFL));
  return (flags > 0) && (flags & O_NONBLOCK);
}

}
}