blob: 4ac4f5782c57554d9734b32f1cacb093d06c87f9 [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>
Mathieu Chartier1104ae82014-06-03 10:24:21 -070025#include <pthread.h>
26#include <stdbool.h>
Mark Salyzyn12717162014-04-29 15:49:14 -070027#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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080034
35#include <cutils/ashmem.h>
36
Mark Salyzynedc70932014-05-02 12:23:56 -070037#ifndef __unused
Mark Salyzyn12717162014-04-29 15:49:14 -070038#define __unused __attribute__((__unused__))
Mark Salyzynedc70932014-05-02 12:23:56 -070039#endif
Mark Salyzyn12717162014-04-29 15:49:14 -070040
Mathieu Chartier1104ae82014-06-03 10:24:21 -070041static pthread_once_t seed_initialized = PTHREAD_ONCE_INIT;
42static void initialize_random() {
43 srand(time(NULL) + getpid());
44}
45
Mark Salyzyn12717162014-04-29 15:49:14 -070046int ashmem_create_region(const char *ignored __unused, size_t size)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047{
Mathieu Chartier1104ae82014-06-03 10:24:21 -070048 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)))]);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Mathieu Chartier1104ae82014-06-03 10:24:21 -070070 /* 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;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080085}
86
Mark Salyzyn12717162014-04-29 15:49:14 -070087int ashmem_set_prot_region(int fd __unused, int prot __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088{
Mathieu Chartier1104ae82014-06-03 10:24:21 -070089 return 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080090}
91
Mark Salyzyn12717162014-04-29 15:49:14 -070092int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093{
Mathieu Chartier1104ae82014-06-03 10:24:21 -070094 return ASHMEM_NOT_PURGED;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095}
96
Mark Salyzyn12717162014-04-29 15:49:14 -070097int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098{
Mathieu Chartier1104ae82014-06-03 10:24:21 -070099 return ASHMEM_IS_UNPINNED;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100}
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100101
102int ashmem_get_size_region(int fd)
103{
Mathieu Chartier1104ae82014-06-03 10:24:21 -0700104 struct stat buf;
105 int result;
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100106
Mathieu Chartier1104ae82014-06-03 10:24:21 -0700107 result = fstat(fd, &buf);
108 if (result == -1) {
109 return -1;
110 }
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100111
Mathieu Chartier1104ae82014-06-03 10:24:21 -0700112 // 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 }
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100118
Mathieu Chartier1104ae82014-06-03 10:24:21 -0700119 return (int)buf.st_size; // TODO: care about overflow (> 2GB file)?
Bjorn Bringert7be52b12009-06-02 00:41:09 +0100120}