It is possible to use this object from multiple threads
and there could be a race between a failure in one thread
and another method call from a different thread which would
result in system calls being called with an invalid fd.
(cherry picked from commit 560ca6e8feb4f52c9aade34fa7cfcfed55288083)
Change-Id: Iefb94ba1ee7177ab20600bee4374daf8e818bdb9
Several ioctl calls were returning -1 instead of -errno,
which may confuse upper layers, especially when the error
is -ENOMEM. Also move some existing "err = -errno" calls
to the top of if statements so that errno cannot change
by libc calls in the error handling code.
(cherry picked from commit a8eda532452651eb1fbae419319455de2a078ef0)
Change-Id: I181f98d5a261e8e3e1b3f6ecd3ba288e7b4b5607