1 /*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <cutils/native_handle.h>
17 #include <errno.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 static const int kMaxNativeFds = 1024;
23 static const int kMaxNativeInts = 1024;
native_handle_init(char * storage,int numFds,int numInts)24 native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
25 if ((uintptr_t) storage % alignof(native_handle_t)) {
26 errno = EINVAL;
27 return NULL;
28 }
29 native_handle_t* handle = (native_handle_t*) storage;
30 handle->version = sizeof(native_handle_t);
31 handle->numFds = numFds;
32 handle->numInts = numInts;
33 return handle;
34 }
native_handle_create(int numFds,int numInts)35 native_handle_t* native_handle_create(int numFds, int numInts) {
36 if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) {
37 errno = EINVAL;
38 return NULL;
39 }
40 size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
41 native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize));
42 if (h) {
43 h->version = sizeof(native_handle_t);
44 h->numFds = numFds;
45 h->numInts = numInts;
46 }
47 return h;
48 }
native_handle_clone(const native_handle_t * handle)49 native_handle_t* native_handle_clone(const native_handle_t* handle) {
50 native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
51 if (clone == NULL) return NULL;
52 for (int i = 0; i < handle->numFds; i++) {
53 clone->data[i] = dup(handle->data[i]);
54 if (clone->data[i] == -1) {
55 clone->numFds = i;
56 native_handle_close(clone);
57 native_handle_delete(clone);
58 return NULL;
59 }
60 }
61 memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds],
62 sizeof(int) * handle->numInts);
63 return clone;
64 }
native_handle_delete(native_handle_t * h)65 int native_handle_delete(native_handle_t* h) {
66 if (h) {
67 if (h->version != sizeof(native_handle_t)) return -EINVAL;
68 free(h);
69 }
70 return 0;
71 }
native_handle_close(const native_handle_t * h)72 int native_handle_close(const native_handle_t* h) {
73 if (h->version != sizeof(native_handle_t)) return -EINVAL;
74 int saved_errno = errno;
75 const int numFds = h->numFds;
76 for (int i = 0; i < numFds; ++i) {
77 close(h->data[i]);
78 }
79 errno = saved_errno;
80 return 0;
81 }
82