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