native_handle.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #include <cutils/native_handle.h>
  17. #include <errno.h>
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. native_handle_t* native_handle_init(char* storage, int numFds, int numInts) {
  23. if ((uintptr_t) storage % alignof(native_handle_t)) {
  24. errno = EINVAL;
  25. return NULL;
  26. }
  27. native_handle_t* handle = (native_handle_t*) storage;
  28. handle->version = sizeof(native_handle_t);
  29. handle->numFds = numFds;
  30. handle->numInts = numInts;
  31. return handle;
  32. }
  33. native_handle_t* native_handle_create(int numFds, int numInts) {
  34. if (numFds < 0 || numInts < 0 || numFds > NATIVE_HANDLE_MAX_FDS ||
  35. numInts > NATIVE_HANDLE_MAX_INTS) {
  36. errno = EINVAL;
  37. return NULL;
  38. }
  39. size_t mallocSize = sizeof(native_handle_t) + (sizeof(int) * (numFds + numInts));
  40. native_handle_t* h = static_cast<native_handle_t*>(malloc(mallocSize));
  41. if (h) {
  42. h->version = sizeof(native_handle_t);
  43. h->numFds = numFds;
  44. h->numInts = numInts;
  45. }
  46. return h;
  47. }
  48. native_handle_t* native_handle_clone(const native_handle_t* handle) {
  49. native_handle_t* clone = native_handle_create(handle->numFds, handle->numInts);
  50. if (clone == NULL) return NULL;
  51. for (int i = 0; i < handle->numFds; i++) {
  52. clone->data[i] = dup(handle->data[i]);
  53. if (clone->data[i] == -1) {
  54. clone->numFds = i;
  55. native_handle_close(clone);
  56. native_handle_delete(clone);
  57. return NULL;
  58. }
  59. }
  60. memcpy(&clone->data[handle->numFds], &handle->data[handle->numFds],
  61. sizeof(int) * handle->numInts);
  62. return clone;
  63. }
  64. int native_handle_delete(native_handle_t* h) {
  65. if (h) {
  66. if (h->version != sizeof(native_handle_t)) return -EINVAL;
  67. free(h);
  68. }
  69. return 0;
  70. }
  71. int native_handle_close(const native_handle_t* h) {
  72. if (h->version != sizeof(native_handle_t)) return -EINVAL;
  73. int saved_errno = errno;
  74. const int numFds = h->numFds;
  75. for (int i = 0; i < numFds; ++i) {
  76. close(h->data[i]);
  77. }
  78. errno = saved_errno;
  79. return 0;
  80. }