blob: 9f4840a594b6ae836d916d64cf98f3c65a080840 [file] [log] [blame]
Mathias Agopian3fc51ba2009-05-20 14:16:34 -07001/*
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
17#define LOG_TAG "NativeHandle"
18
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070019#include <errno.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070020#include <stdint.h>
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070021#include <stdlib.h>
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070022#include <string.h>
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070023#include <unistd.h>
24
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070025#include <android/log.h>
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070026#include <cutils/native_handle.h>
27
Adam Lesinski07edc3b2015-04-27 12:13:33 -070028static const int kMaxNativeFds = 1024;
29static const int kMaxNativeInts = 1024;
30
Chia-I Wub8437912016-09-23 11:17:11 +080031native_handle_t* native_handle_init(char* storage, int numFds, int numInts)
32{
33 if ((uintptr_t) storage % alignof(native_handle_t)) {
34 return NULL;
35 }
36
37 native_handle_t* handle = (native_handle_t*) storage;
38 handle->version = sizeof(native_handle_t);
39 handle->numFds = numFds;
40 handle->numInts = numInts;
41
42 return handle;
43}
44
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070045native_handle_t* native_handle_create(int numFds, int numInts)
46{
Adam Lesinski07edc3b2015-04-27 12:13:33 -070047 if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) {
48 return NULL;
49 }
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070050
Adam Lesinski07edc3b2015-04-27 12:13:33 -070051 size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
52 native_handle_t* h = malloc(mallocSize);
Michael Lentine2b8852d2014-10-31 15:22:52 -070053 if (h) {
54 h->version = sizeof(native_handle_t);
55 h->numFds = numFds;
56 h->numInts = numInts;
57 }
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070058 return h;
59}
60
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080061native_handle_t* native_handle_clone(const native_handle_t* handle)
62{
63 native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
64 int i;
65
66 for (i = 0; i < handle->numFds; i++) {
67 clone->data[i] = dup(handle->data[i]);
68 if (clone->data[i] < 0) {
69 clone->numFds = i;
70 native_handle_close(clone);
71 native_handle_delete(clone);
72 return NULL;
73 }
74 }
75
76 memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds],
77 sizeof(int) * handle->numInts);
78
79 return clone;
80}
81
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070082int native_handle_delete(native_handle_t* h)
83{
84 if (h) {
85 if (h->version != sizeof(native_handle_t))
86 return -EINVAL;
87 free(h);
88 }
89 return 0;
90}
91
92int native_handle_close(const native_handle_t* h)
93{
94 if (h->version != sizeof(native_handle_t))
95 return -EINVAL;
96
97 const int numFds = h->numFds;
98 int i;
99 for (i=0 ; i<numFds ; i++) {
100 close(h->data[i]);
101 }
102 return 0;
103}