blob: 787396435db616b5cf2748243b9a609c36bbcb16 [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 Salyzynedc70932014-05-02 12:23:56 -070035#ifndef __unused
Mark Salyzyn12717162014-04-29 15:49:14 -070036#define __unused __attribute__((__unused__))
Mark Salyzynedc70932014-05-02 12:23:56 -070037#endif
Mark Salyzyn12717162014-04-29 15:49:14 -070038
39int ashmem_create_region(const char *ignored __unused, size_t size)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040{
41 static const char txt[] = "abcdefghijklmnopqrstuvwxyz"
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
43 char name[64];
44 unsigned int retries = 0;
45 pid_t pid = getpid();
46 int fd;
47
48 srand(time(NULL) + pid);
49
50retry:
51 /* not beautiful, its just wolf-like loop unrolling */
52 snprintf(name, sizeof(name), "/tmp/android-ashmem-%d-%c%c%c%c%c%c%c%c",
53 pid,
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 txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))],
61 txt[(int) ((sizeof(txt) - 1) * (rand() / (RAND_MAX + 1.0)))]);
62
63 /* open O_EXCL & O_CREAT: we are either the sole owner or we fail */
64 fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0600);
65 if (fd == -1) {
66 /* unlikely, but if we failed because `name' exists, retry */
67 if (errno == EEXIST && ++retries < 6)
68 goto retry;
69 return -1;
70 }
71
72 /* truncate the file to `len' bytes */
73 if (ftruncate(fd, size) == -1)
74 goto error;
75
76 if (unlink(name) == -1)
77 goto error;
78
79 return fd;
80error:
81 close(fd);
82 return -1;
83}
84
Mark Salyzyn12717162014-04-29 15:49:14 -070085int ashmem_set_prot_region(int fd __unused, int prot __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086{
87 return 0;
88}
89
Mark Salyzyn12717162014-04-29 15:49:14 -070090int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091{
92 return ASHMEM_NOT_PURGED;
93}
94
Mark Salyzyn12717162014-04-29 15:49:14 -070095int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096{
97 return ASHMEM_IS_UNPINNED;
98}
Bjorn Bringert7be52b12009-06-02 00:41:09 +010099
100int ashmem_get_size_region(int fd)
101{
102 struct stat buf;
103 int result;
104
105 result = fstat(fd, &buf);
106 if (result == -1) {
107 return -1;
108 }
109
110 // Check if this is an "ashmem" region.
111 // TODO: This is very hacky, and can easily break. We need some reliable indicator.
112 if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) {
113 errno = ENOTTY;
114 return -1;
115 }
116
117 return (int)buf.st_size; // TODO: care about overflow (> 2GB file)?
118}