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 <stdbool.h> |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
| 34 | #include <cutils/ashmem.h> |
Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 35 | #include <utils/Compat.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
| 41 | 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] | 42 | { |
Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 43 | char template[PATH_MAX]; |
| 44 | snprintf(template, sizeof(template), "/tmp/android-ashmem-%d-XXXXXXXXX", getpid()); |
| 45 | int fd = mkstemp(template); |
| 46 | if (fd != -1 && TEMP_FAILURE_RETRY(ftruncate(fd, size)) != -1 && unlink(template) != -1) { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 47 | return fd; |
| 48 | } |
| 49 | close(fd); |
| 50 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 53 | 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] | 54 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 55 | return 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 58 | 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] | 59 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 60 | return ASHMEM_NOT_PURGED; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Mark Salyzyn | 1271716 | 2014-04-29 15:49:14 -0700 | [diff] [blame] | 63 | 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] | 64 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 65 | return ASHMEM_IS_UNPINNED; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 66 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 67 | |
| 68 | int ashmem_get_size_region(int fd) |
| 69 | { |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 70 | struct stat buf; |
Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 71 | int result = fstat(fd, &buf); |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 72 | if (result == -1) { |
| 73 | return -1; |
| 74 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 75 | |
Mathieu Chartier | 1104ae8 | 2014-06-03 10:24:21 -0700 | [diff] [blame] | 76 | // Check if this is an "ashmem" region. |
| 77 | // TODO: This is very hacky, and can easily break. We need some reliable indicator. |
| 78 | if (!(buf.st_nlink == 0 && S_ISREG(buf.st_mode))) { |
| 79 | errno = ENOTTY; |
| 80 | return -1; |
| 81 | } |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 82 | |
Elliott Hughes | 2004a86 | 2015-02-09 11:41:07 -0800 | [diff] [blame] | 83 | return buf.st_size; |
Bjorn Bringert | 7be52b1 | 2009-06-02 00:41:09 +0100 | [diff] [blame] | 84 | } |