| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 17 | #include <cutils/native_handle.h> | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 18 |  | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 19 | #include <errno.h> | 
| Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 20 | #include <stdint.h> | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 21 | #include <stdlib.h> | 
| Mark Salyzyn | 23ed4c2 | 2016-09-28 13:33:27 -0700 | [diff] [blame] | 22 | #include <string.h> | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 23 | #include <unistd.h> | 
|  | 24 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 25 | native_handle_t* native_handle_init(char* storage, int numFds, int numInts) { | 
| Chia-I Wu | b843791 | 2016-09-23 11:17:11 +0800 | [diff] [blame] | 26 | if ((uintptr_t) storage % alignof(native_handle_t)) { | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 27 | errno = EINVAL; | 
| Chia-I Wu | b843791 | 2016-09-23 11:17:11 +0800 | [diff] [blame] | 28 | 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 Wu | b843791 | 2016-09-23 11:17:11 +0800 | [diff] [blame] | 35 | return handle; | 
|  | 36 | } | 
|  | 37 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 38 | native_handle_t* native_handle_create(int numFds, int numInts) { | 
| Martijn Coenen | 02debfa | 2018-12-05 09:42:25 +0100 | [diff] [blame] | 39 | if (numFds < 0 || numInts < 0 || numFds > NATIVE_HANDLE_MAX_FDS || | 
|  | 40 | numInts > NATIVE_HANDLE_MAX_INTS) { | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 41 | errno = EINVAL; | 
| Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 42 | return NULL; | 
|  | 43 | } | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 44 |  | 
| Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 45 | size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts)); | 
| Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 46 | native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize)); | 
| Michael Lentine | 2b8852d | 2014-10-31 15:22:52 -0700 | [diff] [blame] | 47 | if (h) { | 
|  | 48 | h->version = sizeof(native_handle_t); | 
|  | 49 | h->numFds = numFds; | 
|  | 50 | h->numInts = numInts; | 
|  | 51 | } | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 52 | return h; | 
|  | 53 | } | 
|  | 54 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 55 | native_handle_t* native_handle_clone(const native_handle_t* handle) { | 
| Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 56 | native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts); | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 57 | if (clone == NULL) return NULL; | 
| Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 58 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 59 | for (int i = 0; i < handle->numFds; i++) { | 
| Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 60 | clone->data[i] = dup(handle->data[i]); | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 61 | if (clone->data[i] == -1) { | 
| Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 62 | 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 Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 70 | sizeof(int) * handle->numInts); | 
| Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 71 |  | 
|  | 72 | return clone; | 
|  | 73 | } | 
|  | 74 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 75 | int native_handle_delete(native_handle_t* h) { | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 76 | if (h) { | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 77 | if (h->version != sizeof(native_handle_t)) return -EINVAL; | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 78 | free(h); | 
|  | 79 | } | 
|  | 80 | return 0; | 
|  | 81 | } | 
|  | 82 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 83 | int native_handle_close(const native_handle_t* h) { | 
| Elliott Hughes | 7e42484 | 2019-11-12 20:20:42 -0800 | [diff] [blame] | 84 | if (!h) return 0; | 
|  | 85 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 86 | if (h->version != sizeof(native_handle_t)) return -EINVAL; | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 87 |  | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 88 | int saved_errno = errno; | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 89 | const int numFds = h->numFds; | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 90 | for (int i = 0; i < numFds; ++i) { | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 91 | close(h->data[i]); | 
|  | 92 | } | 
| Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 93 | errno = saved_errno; | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 94 | return 0; | 
|  | 95 | } |