blob: 66f7a3d593018482aa13b9620e15d3cb7fcf216c [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
Elliott Hughesbf0492a2017-05-01 21:34:15 -070017#include <cutils/native_handle.h>
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070018
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
Adam Lesinski07edc3b2015-04-27 12:13:33 -070025static const int kMaxNativeFds = 1024;
26static const int kMaxNativeInts = 1024;
27
Elliott Hughesbf0492a2017-05-01 21:34:15 -070028native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
Chia-I Wub8437912016-09-23 11:17:11 +080029 if ((uintptr_t) storage % alignof(native_handle_t)) {
Elliott Hughesbf0492a2017-05-01 21:34:15 -070030 errno = EINVAL;
Chia-I Wub8437912016-09-23 11:17:11 +080031 return NULL;
32 }
33
34 native_handle_t* handle = (native_handle_t*) storage;
35 handle->version = sizeof(native_handle_t);
36 handle->numFds = numFds;
37 handle->numInts = numInts;
Chia-I Wub8437912016-09-23 11:17:11 +080038 return handle;
39}
40
Elliott Hughesbf0492a2017-05-01 21:34:15 -070041native_handle_t* native_handle_create(int numFds, int numInts) {
Adam Lesinski07edc3b2015-04-27 12:13:33 -070042 if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) {
Elliott Hughesbf0492a2017-05-01 21:34:15 -070043 errno = EINVAL;
Adam Lesinski07edc3b2015-04-27 12:13:33 -070044 return NULL;
45 }
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070046
Adam Lesinski07edc3b2015-04-27 12:13:33 -070047 size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080048 native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize));
Michael Lentine2b8852d2014-10-31 15:22:52 -070049 if (h) {
50 h->version = sizeof(native_handle_t);
51 h->numFds = numFds;
52 h->numInts = numInts;
53 }
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070054 return h;
55}
56
Elliott Hughesbf0492a2017-05-01 21:34:15 -070057native_handle_t* native_handle_clone(const native_handle_t* handle) {
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080058 native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
Elliott Hughesbf0492a2017-05-01 21:34:15 -070059 if (clone == NULL) return NULL;
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080060
Elliott Hughesbf0492a2017-05-01 21:34:15 -070061 for (int i = 0; i < handle->numFds; i++) {
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080062 clone->data[i] = dup(handle->data[i]);
Elliott Hughesbf0492a2017-05-01 21:34:15 -070063 if (clone->data[i] == -1) {
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080064 clone->numFds = i;
65 native_handle_close(clone);
66 native_handle_delete(clone);
67 return NULL;
68 }
69 }
70
71 memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds],
Elliott Hughesbf0492a2017-05-01 21:34:15 -070072 sizeof(int) * handle->numInts);
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080073
74 return clone;
75}
76
Elliott Hughesbf0492a2017-05-01 21:34:15 -070077int native_handle_delete(native_handle_t* h) {
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070078 if (h) {
Elliott Hughesbf0492a2017-05-01 21:34:15 -070079 if (h->version != sizeof(native_handle_t)) return -EINVAL;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070080 free(h);
81 }
82 return 0;
83}
84
Elliott Hughesbf0492a2017-05-01 21:34:15 -070085int native_handle_close(const native_handle_t* h) {
86 if (h->version != sizeof(native_handle_t)) return -EINVAL;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070087
Elliott Hughesbf0492a2017-05-01 21:34:15 -070088 int saved_errno = errno;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070089 const int numFds = h->numFds;
Elliott Hughesbf0492a2017-05-01 21:34:15 -070090 for (int i = 0; i < numFds; ++i) {
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070091 close(h->data[i]);
92 }
Elliott Hughesbf0492a2017-05-01 21:34:15 -070093 errno = saved_errno;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070094 return 0;
95}