blob: cfb5d66f02b9f1319412bf822574bbe8254415ed [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
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
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <errno.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070023#include <fcntl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <limits.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080032
33#include <cutils/ashmem.h>
34
Mark Salyzyn12717162014-04-29 15:49:14 -070035#define __unused __attribute__((__unused__))
36
37int ashmem_create_region(const char *ignored __unused, size_t size)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038{
39 static const char txt[] = "abcdefghijklmnopqrstuvwxyz"
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
41 char name[64];
42 unsigned int retries = 0;
43 pid_t pid = getpid();
44 int fd;
45
46 srand(time(NULL) + pid);
47
48retry:
49 /* not beautiful, its just wolf-like loop unrolling */
50 snprintf(name, sizeof(name), "/tmp/android-ashmem-%d-%c%c%c%c%c%c%c%c",
51 pid,
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 txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
59 txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))]);
60
61 /* open O_EXCL & O_CREAT: we are either the sole owner or we fail */
62 fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
63 if (fd == -1) {
64 /* unlikely, but if we failed because `name' exists, retry */
65 if (errno == EEXIST && ++retries < 6)
66 goto retry;
67 return -1;
68 }
69
70 /* truncate the file to `len' bytes */
71 if (ftruncate(fd, size) == -1)
72 goto error;
73
74 if (unlink(name) == -1)
75 goto error;
76
77 return fd;
78error:
79 close(fd);
80 return -1;
81}
82
Mark Salyzyn12717162014-04-29 15:49:14 -070083int ashmem_set_prot_region(int fd __unused, int prot __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084{
85 return 0;
86}
87
Mark Salyzyn12717162014-04-29 15:49:14 -070088int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089{
90 return ASHMEM_NOT_PURGED;
91}
92
Mark Salyzyn12717162014-04-29 15:49:14 -070093int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094{
95 return ASHMEM_IS_UNPINNED;
96}
Bjorn Bringert7be52b12009-06-02 00:41:09 +010097
98int ashmem_get_size_region(int fd)
99{
100 struct stat buf;
101 int result;
102
103 result = fstat(fd, &buf);
104 if (result == -1) {
105 return -1;
106 }
107
108 // Check if this is an "ashmem" region.
109 // TODO: This is very hacky, and can easily break. We need some reliable indicator.
110 if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
111 errno = ENOTTY;
112 return -1;
113 }
114
115 return (int)buf.st_size; // TODO: care about overflow (> 2GB file)?
116}