The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 24 | #include <limits.h> |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 25 | #include <pthread.h> |
| 26 | #include <stdbool.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 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> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 34 | |
| 35 | #include <cutils/ashmem.h> |
| 36 | |
Mark Salyzyn | edc7093 | 2014-05-02 12:23:56 -0700 | [diff] [blame] | 37 | #ifndef __unused |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 38 | #define __unused __attribute__((__unused__)) |
Mark Salyzyn | edc7093 | 2014-05-02 12:23:56 -0700 | [diff] [blame] | 39 | #endif |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 40 | |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 41 | static pthread_once_t seed_initialized = PTHREAD_ONCE_INIT; |
| 42 | static void initialize_random() { |
| 43 | srand(time(NULL) + getpid()); |
| 44 | } |
| 45 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 46 | int ashmem_create_region(const char *ignored __unused, size_t size) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 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)))]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 87 | int ashmem_set_prot_region(int fd __unused, int prot __unused) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 89 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 92 | int ashmem_pin_region(int fd __unused, size_t offset __unused, size_t len __unused) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 94 | return ASHMEM_NOT_PURGED; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 97 | int ashmem_unpin_region(int fd __unused, size_t offset __unused, size_t len __unused) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 99 | return ASHMEM_IS_UNPINNED; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 100 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 101 | |
| 102 | int ashmem_get_size_region(int fd) |
| 103 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 104 | struct stat buf; |
| 105 | int result; |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 106 | |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 107 | result = fstat(fd, &buf); |
| 108 | if (result == -1) { |
| 109 | return -1; |
| 110 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 111 | |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 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 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 118 | |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 119 | return (int)buf.st_size; // TODO: care about overflow (> 2GB file)? |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 120 | } |