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 | |
Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 25 | static const int kMaxNativeFds = 1024; |
| 26 | static const int kMaxNativeInts = 1024; |
| 27 | |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 28 | 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] | 29 | if ((uintptr_t) storage % alignof(native_handle_t)) { |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 30 | errno = EINVAL; |
Chia-I Wu | b843791 | 2016-09-23 11:17:11 +0800 | [diff] [blame] | 31 | 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 Wu | b843791 | 2016-09-23 11:17:11 +0800 | [diff] [blame] | 38 | return handle; |
| 39 | } |
| 40 | |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 41 | native_handle_t* native_handle_create(int numFds, int numInts) { |
Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 42 | if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) { |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 43 | errno = EINVAL; |
Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 46 | |
Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 47 | size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts)); |
Elliott Hughes | 8e9aeb9 | 2017-11-10 10:22:07 -0800 | [diff] [blame] | 48 | native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize)); |
Michael Lentine | 2b8852d | 2014-10-31 15:22:52 -0700 | [diff] [blame] | 49 | if (h) { |
| 50 | h->version = sizeof(native_handle_t); |
| 51 | h->numFds = numFds; |
| 52 | h->numInts = numInts; |
| 53 | } |
Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 54 | return h; |
| 55 | } |
| 56 | |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 57 | native_handle_t* native_handle_clone(const native_handle_t* handle) { |
Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 58 | native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts); |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 59 | if (clone == NULL) return NULL; |
Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 60 | |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 61 | for (int i = 0; i < handle->numFds; i++) { |
Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 62 | clone->data[i] = dup(handle->data[i]); |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 63 | if (clone->data[i] == -1) { |
Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 64 | 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 Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 72 | sizeof(int) * handle->numInts); |
Chia-I Wu | fd3ea3d | 2016-09-23 11:13:52 +0800 | [diff] [blame] | 73 | |
| 74 | return clone; |
| 75 | } |
| 76 | |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 77 | int native_handle_delete(native_handle_t* h) { |
Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 78 | if (h) { |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 79 | if (h->version != sizeof(native_handle_t)) return -EINVAL; |
Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 80 | free(h); |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
Elliott Hughes | bf0492a | 2017-05-01 21:34:15 -0700 | [diff] [blame] | 85 | int native_handle_close(const native_handle_t* h) { |
| 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 | } |