Home | History | Annotate | Download | only in libcutils
      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 #include <cutils/native_handle.h>
     18 
     19 #include <errno.h>
     20 #include <stdint.h>
     21 #include <stdlib.h>
     22 #include <string.h>
     23 #include <unistd.h>
     24 
     25 static const int kMaxNativeFds = 1024;
     26 static const int kMaxNativeInts = 1024;
     27 
     28 native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
     29     if ((uintptr_t) storage % alignof(native_handle_t)) {
     30         errno = EINVAL;
     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;
     38     return handle;
     39 }
     40 
     41 native_handle_t* native_handle_create(int numFds, int numInts) {
     42     if (numFds < 0 || numInts < 0 || numFds > kMaxNativeFds || numInts > kMaxNativeInts) {
     43         errno = EINVAL;
     44         return NULL;
     45     }
     46 
     47     size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
     48     native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize));
     49     if (h) {
     50         h->version = sizeof(native_handle_t);
     51         h->numFds = numFds;
     52         h->numInts = numInts;
     53     }
     54     return h;
     55 }
     56 
     57 native_handle_t* native_handle_clone(const native_handle_t* handle) {
     58     native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
     59     if (clone == NULL) return NULL;
     60 
     61     for (int i = 0; i < handle->numFds; i++) {
     62         clone->data[i] = dup(handle->data[i]);
     63         if (clone->data[i] == -1) {
     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],
     72            sizeof(int) * handle->numInts);
     73 
     74     return clone;
     75 }
     76 
     77 int native_handle_delete(native_handle_t* h) {
     78     if (h) {
     79         if (h->version != sizeof(native_handle_t)) return -EINVAL;
     80         free(h);
     81     }
     82     return 0;
     83 }
     84 
     85 int native_handle_close(const native_handle_t* h) {
     86     if (h->version != sizeof(native_handle_t)) return -EINVAL;
     87 
     88     int saved_errno = errno;
     89     const int numFds = h->numFds;
     90     for (int i = 0; i < numFds; ++i) {
     91         close(h->data[i]);
     92     }
     93     errno = saved_errno;
     94     return 0;
     95 }
     96