summaryrefslogtreecommitdiffstats
path: root/libcutils
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2017-05-01 21:34:15 -0700
committerElliott Hughes <enh@google.com>2017-05-01 21:34:15 -0700
commitbf0492a9a1e3976dd4aea5596d1b1e10e8cbeea2 (patch)
tree0d636e0d98234ca805c649a6c1f4099552da0dbc /libcutils
parent16214f2955eb8570f120893a02d978ca91f0a5a7 (diff)
downloadcore-bf0492a9a1e3976dd4aea5596d1b1e10e8cbeea2.tar.gz
core-bf0492a9a1e3976dd4aea5596d1b1e10e8cbeea2.tar.bz2
core-bf0492a9a1e3976dd4aea5596d1b1e10e8cbeea2.zip
Preserve errno better in native_handle functions.
So a caller of native_handle_clone can trust errno to be relevant. Bug: http://b/37215366 Test: builds Change-Id: I0992f38ad559db4a02fce07123842dbad8e3f473
Diffstat (limited to 'libcutils')
-rw-r--r--libcutils/native_handle.c42
1 files changed, 17 insertions, 25 deletions
diff --git a/libcutils/native_handle.c b/libcutils/native_handle.c
index 9f4840a59..95bbc41c4 100644
--- a/libcutils/native_handle.c
+++ b/libcutils/native_handle.c
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-#define LOG_TAG "NativeHandle"
+#include <cutils/native_handle.h>
#include <errno.h>
#include <stdint.h>
@@ -22,15 +22,12 @@
#include <string.h>
#include <unistd.h>
-#include <android/log.h>
-#include <cutils/native_handle.h>
-
static const int kMaxNativeFds = 1024;
static const int kMaxNativeInts = 1024;
-native_handle_t* native_handle_init(char* storage, int numFds, int numInts)
-{
+native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
if ((uintptr_t) storage % alignof(native_handle_t)) {
+ errno = EINVAL;
return NULL;
}
@@ -38,13 +35,12 @@ native_handle_t* native_handle_init(char* storage, int numFds, int numInts)
handle->version = sizeof(native_handle_t);
handle->numFds = numFds;
handle->numInts = numInts;
-
return handle;
}
-native_handle_t* native_handle_create(int numFds, int numInts)
-{
+native_handle_t* native_handle_create(int numFds, int numInts) {
if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) {
+ errno = EINVAL;
return NULL;
}
@@ -58,14 +54,13 @@ native_handle_t* native_handle_create(int numFds, int numInts)
return h;
}
-native_handle_t* native_handle_clone(const native_handle_t* handle)
-{
+native_handle_t* native_handle_clone(const native_handle_t* handle) {
native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
- int i;
+ if (clone == NULL) return NULL;
- for (i = 0; i < handle->numFds; i++) {
+ for (int i = 0; i < handle->numFds; i++) {
clone->data[i] = dup(handle->data[i]);
- if (clone->data[i] < 0) {
+ if (clone->data[i] == -1) {
clone->numFds = i;
native_handle_close(clone);
native_handle_delete(clone);
@@ -74,30 +69,27 @@ native_handle_t* native_handle_clone(const native_handle_t* handle)
}
memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds],
- sizeof(int) * handle->numInts);
+ sizeof(int) * handle->numInts);
return clone;
}
-int native_handle_delete(native_handle_t* h)
-{
+int native_handle_delete(native_handle_t* h) {
if (h) {
- if (h->version != sizeof(native_handle_t))
- return -EINVAL;
+ if (h->version != sizeof(native_handle_t)) return -EINVAL;
free(h);
}
return 0;
}
-int native_handle_close(const native_handle_t* h)
-{
- if (h->version != sizeof(native_handle_t))
- return -EINVAL;
+int native_handle_close(const native_handle_t* h) {
+ if (h->version != sizeof(native_handle_t)) return -EINVAL;
+ int saved_errno = errno;
const int numFds = h->numFds;
- int i;
- for (i=0 ; i<numFds ; i++) {
+ for (int i = 0; i < numFds; ++i) {
close(h->data[i]);
}
+ errno = saved_errno;
return 0;
}