ashmem-host.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2008 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. * Implementation of the user-space ashmem API for the simulator, which lacks
  18. * an ashmem-enabled kernel. See ashmem-dev.c for the real ashmem-based version.
  19. */
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <time.h>
  29. #include <limits.h>
  30. #include <cutils/ashmem.h>
  31. int ashmem_create_region(const char *ignored, size_t size)
  32. {
  33. static const char txt[] = "abcdefghijklmnopqrstuvwxyz"
  34. "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  35. char name[64];
  36. unsigned int retries = 0;
  37. pid_t pid = getpid();
  38. int fd;
  39. srand(time(NULL) + pid);
  40. retry:
  41. /* not beautiful, its just wolf-like loop unrolling */
  42. snprintf(name, sizeof(name), "/tmp/android-ashmem-%d-%c%c%c%c%c%c%c%c",
  43. pid,
  44. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  45. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  46. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  47. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  48. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  49. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  50. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
  51. txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))]);
  52. /* open O_EXCL & O_CREAT: we are either the sole owner or we fail */
  53. fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
  54. if (fd == -1) {
  55. /* unlikely, but if we failed because `name' exists, retry */
  56. if (errno == EEXIST && ++retries < 6)
  57. goto retry;
  58. return -1;
  59. }
  60. /* truncate the file to `len' bytes */
  61. if (ftruncate(fd, size) == -1)
  62. goto error;
  63. if (unlink(name) == -1)
  64. goto error;
  65. return fd;
  66. error:
  67. close(fd);
  68. return -1;
  69. }
  70. int ashmem_set_prot_region(int fd, int prot)
  71. {
  72. return 0;
  73. }
  74. int ashmem_pin_region(int fd, size_t offset, size_t len)
  75. {
  76. return ASHMEM_NOT_PURGED;
  77. }
  78. int ashmem_unpin_region(int fd, size_t offset, size_t len)
  79. {
  80. return ASHMEM_IS_UNPINNED;
  81. }
  82. int ashmem_get_size_region(int fd)
  83. {
  84. struct stat buf;
  85. int result;
  86. result = fstat(fd, &buf);
  87. if (result == -1) {
  88. return -1;
  89. }
  90. // Check if this is an "ashmem" region.
  91. // TODO: This is very hacky, and can easily break. We need some reliable indicator.
  92. if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
  93. errno = ENOTTY;
  94. return -1;
  95. }
  96. return (int)buf.st_size; // TODO: care about overflow (> 2GB file)?
  97. }