| 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 |  | 
 | 17 | #define LOG_TAG "NativeHandle" | 
 | 18 |  | 
 | 19 | #include <stdint.h> | 
 | 20 | #include <errno.h> | 
 | 21 | #include <string.h> | 
 | 22 | #include <stdlib.h> | 
 | 23 | #include <unistd.h> | 
 | 24 |  | 
 | 25 | #include <cutils/log.h> | 
 | 26 | #include <cutils/native_handle.h> | 
 | 27 |  | 
| Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 28 | static const int kMaxNativeFds = 1024; | 
 | 29 | static const int kMaxNativeInts = 1024; | 
 | 30 |  | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 31 | native_handle_t* native_handle_create(int numFds, int numInts) | 
 | 32 | { | 
| Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 33 |     if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) { | 
 | 34 |         return NULL; | 
 | 35 |     } | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 36 |  | 
| Adam Lesinski | 07edc3b | 2015-04-27 12:13:33 -0700 | [diff] [blame] | 37 |     size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts)); | 
 | 38 |     native_handle_t* h = malloc(mallocSize); | 
| Michael Lentine | 2b8852d | 2014-10-31 15:22:52 -0700 | [diff] [blame] | 39 |     if (h) { | 
 | 40 |         h->version = sizeof(native_handle_t); | 
 | 41 |         h->numFds = numFds; | 
 | 42 |         h->numInts = numInts; | 
 | 43 |     } | 
| Mathias Agopian | 3fc51ba | 2009-05-20 14:16:34 -0700 | [diff] [blame] | 44 |     return h; | 
 | 45 | } | 
 | 46 |  | 
 | 47 | int native_handle_delete(native_handle_t* h) | 
 | 48 | { | 
 | 49 |     if (h) { | 
 | 50 |         if (h->version != sizeof(native_handle_t)) | 
 | 51 |             return -EINVAL; | 
 | 52 |         free(h); | 
 | 53 |     } | 
 | 54 |     return 0; | 
 | 55 | } | 
 | 56 |  | 
 | 57 | int native_handle_close(const native_handle_t* h) | 
 | 58 | { | 
 | 59 |     if (h->version != sizeof(native_handle_t)) | 
 | 60 |         return -EINVAL; | 
 | 61 |  | 
 | 62 |     const int numFds = h->numFds; | 
 | 63 |     int i; | 
 | 64 |     for (i=0 ; i<numFds ; i++) { | 
 | 65 |         close(h->data[i]); | 
 | 66 |     } | 
 | 67 |     return 0; | 
 | 68 | } |