Crash if too many open files

If there are too many open files, and we can't dup a socket because we
received EMFILE or ENFILE, let's crash immediately to make this problem
obvious.
Otherwise, we are simply propagating null pointers through the system
and witness null pointer dereference later (in the good cases), or
undefined behaviour.

Bug: 141111452
Test: none
Change-Id: I012bc050e5246456e00c22f0c17a5752b2708875
diff --git a/libs/input/InputTransport.cpp b/libs/input/InputTransport.cpp
index 7835651..8a2fc2a 100644
--- a/libs/input/InputTransport.cpp
+++ b/libs/input/InputTransport.cpp
@@ -362,6 +362,13 @@
     if (!newFd.ok()) {
         ALOGE("Could not duplicate fd %i for channel %s: %s", getFd(), mName.c_str(),
               strerror(errno));
+        const bool hitFdLimit = errno == EMFILE || errno == ENFILE;
+        // If this process is out of file descriptors, then throwing that might end up exploding
+        // on the other side of a binder call, which isn't really helpful.
+        // Better to just crash here and hope that the FD leak is slow.
+        // Other failures could be client errors, so we still propagate those back to the caller.
+        LOG_ALWAYS_FATAL_IF(hitFdLimit, "Too many open files, could not duplicate input channel %s",
+                            getName().c_str());
         return nullptr;
     }
     return InputChannel::create(mName, std::move(newFd));