blob: b409e5b9d896faa7d7d6a093087c405d98658fa5 [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
Elliott Hughesbf0492a2017-05-01 21:34:15 -070025native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
Chia-I Wub8437912016-09-23 11:17:11 +080026 if ((uintptr_t) storage % alignof(native_handle_t)) {
Elliott Hughesbf0492a2017-05-01 21:34:15 -070027 errno = EINVAL;
Chia-I Wub8437912016-09-23 11:17:11 +080028 return NULL;
29 }
30
31 native_handle_t* handle = (native_handle_t*) storage;
32 handle->version = sizeof(native_handle_t);
33 handle->numFds = numFds;
34 handle->numInts = numInts;
Chia-I Wub8437912016-09-23 11:17:11 +080035 return handle;
36}
37
Elliott Hughesbf0492a2017-05-01 21:34:15 -070038native_handle_t* native_handle_create(int numFds, int numInts) {
Martijn Coenen6b628222018-12-05 09:42:25 +010039 if (numFds < 0 || numInts < 0 || numFds > NATIVE_HANDLE_MAX_FDS ||
40 numInts > NATIVE_HANDLE_MAX_INTS) {
Elliott Hughesbf0492a2017-05-01 21:34:15 -070041 errno = EINVAL;
Adam Lesinski07edc3b2015-04-27 12:13:33 -070042 return NULL;
43 }
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070044
Adam Lesinski07edc3b2015-04-27 12:13:33 -070045 size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
Elliott Hughes8e9aeb92017-11-10 10:22:07 -080046 native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize));
Michael Lentine2b8852d2014-10-31 15:22:52 -070047 if (h) {
48 h->version = sizeof(native_handle_t);
49 h->numFds = numFds;
50 h->numInts = numInts;
51 }
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070052 return h;
53}
54
Elliott Hughesbf0492a2017-05-01 21:34:15 -070055native_handle_t* native_handle_clone(const native_handle_t* handle) {
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080056 native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
Elliott Hughesbf0492a2017-05-01 21:34:15 -070057 if (clone == NULL) return NULL;
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080058
Elliott Hughesbf0492a2017-05-01 21:34:15 -070059 for (int i = 0; i < handle->numFds; i++) {
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080060 clone->data[i] = dup(handle->data[i]);
Elliott Hughesbf0492a2017-05-01 21:34:15 -070061 if (clone->data[i] == -1) {
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080062 clone->numFds = i;
63 native_handle_close(clone);
64 native_handle_delete(clone);
65 return NULL;
66 }
67 }
68
69 memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds],
Elliott Hughesbf0492a2017-05-01 21:34:15 -070070 sizeof(int) * handle->numInts);
Chia-I Wufd3ea3d2016-09-23 11:13:52 +080071
72 return clone;
73}
74
Elliott Hughesbf0492a2017-05-01 21:34:15 -070075int native_handle_delete(native_handle_t* h) {
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070076 if (h) {
Elliott Hughesbf0492a2017-05-01 21:34:15 -070077 if (h->version != sizeof(native_handle_t)) return -EINVAL;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070078 free(h);
79 }
80 return 0;
81}
82
Elliott Hughesbf0492a2017-05-01 21:34:15 -070083int native_handle_close(const native_handle_t* h) {
84 if (h->version != sizeof(native_handle_t)) return -EINVAL;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070085
Elliott Hughesbf0492a2017-05-01 21:34:15 -070086 int saved_errno = errno;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070087 const int numFds = h->numFds;
Elliott Hughesbf0492a2017-05-01 21:34:15 -070088 for (int i = 0; i < numFds; ++i) {
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070089 close(h->data[i]);
90 }
Elliott Hughesbf0492a2017-05-01 21:34:15 -070091 errno = saved_errno;
Mathias Agopian3fc51ba2009-05-20 14:16:34 -070092 return 0;
93}