michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* Copyright 2012 Mozilla Foundation and Mozilla contributors michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "nsDebug.h" michael@0: #include "base/message_loop.h" michael@0: #include "mozilla/FileUtils.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include "UeventPoller.h" michael@0: michael@0: namespace mozilla { michael@0: namespace hal_impl { michael@0: michael@0: static void ShutdownUevent(); michael@0: michael@0: class NetlinkPoller : public MessageLoopForIO::Watcher michael@0: { michael@0: public: michael@0: NetlinkPoller() : mSocket(-1), michael@0: mIOLoop(MessageLoopForIO::current()) michael@0: { michael@0: } michael@0: michael@0: virtual ~NetlinkPoller() {} michael@0: michael@0: bool OpenSocket(); michael@0: michael@0: virtual void OnFileCanReadWithoutBlocking(int fd); michael@0: michael@0: // no writing to the netlink socket michael@0: virtual void OnFileCanWriteWithoutBlocking(int fd) michael@0: { michael@0: MOZ_CRASH("Must not write to netlink socket"); michael@0: } michael@0: michael@0: MessageLoopForIO *GetIOLoop () const { return mIOLoop; } michael@0: void RegisterObserver(IUeventObserver *aObserver) michael@0: { michael@0: mUeventObserverList.AddObserver(aObserver); michael@0: } michael@0: michael@0: void UnregisterObserver(IUeventObserver *aObserver) michael@0: { michael@0: mUeventObserverList.RemoveObserver(aObserver); michael@0: if (mUeventObserverList.Length() == 0) michael@0: ShutdownUevent(); // this will destroy self michael@0: } michael@0: michael@0: private: michael@0: ScopedClose mSocket; michael@0: MessageLoopForIO* mIOLoop; michael@0: MessageLoopForIO::FileDescriptorWatcher mReadWatcher; michael@0: michael@0: const static int kBuffsize = 64 * 1024; michael@0: uint8_t mBuffer [kBuffsize]; michael@0: michael@0: typedef ObserverList UeventObserverList; michael@0: UeventObserverList mUeventObserverList; michael@0: }; michael@0: michael@0: bool michael@0: NetlinkPoller::OpenSocket() michael@0: { michael@0: mSocket.rwget() = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); michael@0: if (mSocket.get() < 0) michael@0: return false; michael@0: michael@0: int sz = kBuffsize; michael@0: michael@0: if (setsockopt(mSocket.get(), SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)) < 0) michael@0: return false; michael@0: michael@0: // add FD_CLOEXEC flag michael@0: int flags = fcntl(mSocket.get(), F_GETFD); michael@0: if (flags == -1) { michael@0: return false; michael@0: } michael@0: flags |= FD_CLOEXEC; michael@0: if (fcntl(mSocket.get(), F_SETFD, flags) == -1) michael@0: return false; michael@0: michael@0: // set non-blocking michael@0: if (fcntl(mSocket.get(), F_SETFL, O_NONBLOCK) == -1) michael@0: return false; michael@0: michael@0: struct sockaddr_nl saddr; michael@0: bzero(&saddr, sizeof(saddr)); michael@0: saddr.nl_family = AF_NETLINK; michael@0: saddr.nl_groups = 1; michael@0: saddr.nl_pid = gettid(); michael@0: michael@0: do { michael@0: if (bind(mSocket.get(), (struct sockaddr *)&saddr, sizeof(saddr)) == 0) { michael@0: break; michael@0: } michael@0: michael@0: if (errno != EADDRINUSE) { michael@0: return false; michael@0: } michael@0: michael@0: if (saddr.nl_pid == 0) { michael@0: return false; michael@0: } michael@0: michael@0: // Once there was any other place in the same process assigning saddr.nl_pid by michael@0: // gettid(), we can detect it and print warning message. michael@0: printf_stderr("The netlink socket address saddr.nl_pid=%u is in use. Let the kernel re-assign.\n", saddr.nl_pid); michael@0: saddr.nl_pid = 0; michael@0: } while (true); michael@0: michael@0: if (!mIOLoop->WatchFileDescriptor(mSocket.get(), michael@0: true, michael@0: MessageLoopForIO::WATCH_READ, michael@0: &mReadWatcher, michael@0: this)) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static nsAutoPtr sPoller; michael@0: michael@0: class UeventInitTask : public Task michael@0: { michael@0: virtual void Run() michael@0: { michael@0: if (!sPoller) { michael@0: return; michael@0: } michael@0: if (sPoller->OpenSocket()) { michael@0: return; michael@0: } michael@0: sPoller->GetIOLoop()->PostDelayedTask(FROM_HERE, new UeventInitTask(), 1000); michael@0: } michael@0: }; michael@0: michael@0: void michael@0: NetlinkPoller::OnFileCanReadWithoutBlocking(int fd) michael@0: { michael@0: MOZ_ASSERT(fd == mSocket.get()); michael@0: while (true) { michael@0: int ret = read(fd, mBuffer, kBuffsize); michael@0: if (ret == -1) { michael@0: if (errno == EAGAIN || errno == EWOULDBLOCK) { michael@0: return; michael@0: } michael@0: if (errno == EINTR) { michael@0: continue; michael@0: } michael@0: } michael@0: if (ret <= 0) { michael@0: // fatal error on netlink socket which should not happen michael@0: _exit(1); michael@0: } michael@0: NetlinkEvent netlinkEvent; michael@0: netlinkEvent.decode(reinterpret_cast(mBuffer), ret); michael@0: mUeventObserverList.Broadcast(netlinkEvent); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: InitializeUevent() michael@0: { michael@0: MOZ_ASSERT(!sPoller); michael@0: sPoller = new NetlinkPoller(); michael@0: sPoller->GetIOLoop()->PostTask(FROM_HERE, new UeventInitTask()); michael@0: michael@0: } michael@0: michael@0: static void michael@0: ShutdownUevent() michael@0: { michael@0: sPoller = nullptr; michael@0: } michael@0: michael@0: void michael@0: RegisterUeventListener(IUeventObserver *aObserver) michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: michael@0: if (!sPoller) michael@0: InitializeUevent(); michael@0: sPoller->RegisterObserver(aObserver); michael@0: } michael@0: michael@0: void michael@0: UnregisterUeventListener(IUeventObserver *aObserver) michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: michael@0: sPoller->UnregisterObserver(aObserver); michael@0: } michael@0: michael@0: } // hal_impl michael@0: } // mozilla michael@0: