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