that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <string.h> |
| 21 | #include <ctype.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <dirent.h> |
| 25 | #include <limits.h> |
| 26 | #include <errno.h> |
| 27 | |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 28 | #include "../../bionic/libc/private/bionic_futex.h" |
| 29 | |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 30 | #include <cutils/properties.h> |
| 31 | |
| 32 | #include "legacy_properties.h" |
| 33 | |
| 34 | #include <sys/mman.h> |
Ethan Yonker | 6029e93 | 2014-11-06 09:31:48 -0600 | [diff] [blame] | 35 | // Not available in 5.0 |
| 36 | //#include <sys/atomics.h> |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 37 | #include "legacy_property_service.h" |
| 38 | |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 39 | #ifndef INT32_MAX |
| 40 | #define INT32_MAX (2147483647) |
| 41 | #endif |
| 42 | |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 43 | static int persistent_properties_loaded = 0; |
| 44 | static int property_area_inited = 0; |
| 45 | |
| 46 | static int property_set_fd = -1; |
| 47 | |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 48 | typedef struct { |
| 49 | void *data; |
| 50 | size_t size; |
| 51 | int fd; |
| 52 | } workspace; |
| 53 | |
| 54 | static int init_workspace(workspace *w, size_t size) |
| 55 | { |
| 56 | void *data; |
| 57 | int fd; |
| 58 | |
| 59 | /* dev is a tmpfs that we can use to carve a shared workspace |
| 60 | * out of, so let's do that... |
| 61 | */ |
| 62 | fd = open("/dev/__legacy_properties__", O_RDWR | O_CREAT, 0600); |
| 63 | if (fd < 0) |
| 64 | return -1; |
| 65 | |
| 66 | if (ftruncate(fd, size) < 0) |
| 67 | goto out; |
| 68 | |
| 69 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 70 | if(data == MAP_FAILED) |
| 71 | goto out; |
| 72 | |
| 73 | close(fd); |
| 74 | |
| 75 | fd = open("/dev/__legacy_properties__", O_RDONLY); |
| 76 | if (fd < 0) |
| 77 | return -1; |
| 78 | |
| 79 | unlink("/dev/__legacy_properties__"); |
| 80 | |
| 81 | w->data = data; |
| 82 | w->size = size; |
| 83 | w->fd = fd; |
| 84 | return 0; |
| 85 | |
| 86 | out: |
| 87 | close(fd); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | /* (8 header words + 247 toc words) = 1020 bytes */ |
| 92 | /* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */ |
| 93 | |
| 94 | #define PA_COUNT_MAX 247 |
| 95 | #define PA_INFO_START 1024 |
| 96 | #define PA_SIZE 32768 |
| 97 | |
| 98 | static workspace pa_workspace; |
| 99 | static prop_info *pa_info_array; |
| 100 | |
| 101 | prop_area *__legacy_property_area__; |
| 102 | |
| 103 | static int init_property_area(void) |
| 104 | { |
| 105 | prop_area *pa; |
| 106 | |
| 107 | if(pa_info_array) |
| 108 | return -1; |
| 109 | |
| 110 | if(init_workspace(&pa_workspace, PA_SIZE)) |
| 111 | return -1; |
| 112 | |
| 113 | fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC); |
| 114 | |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 115 | pa_info_array = (prop_info*) (((char*) pa_workspace.data) + PA_INFO_START); |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 116 | |
Ethan Yonker | f117962 | 2016-08-25 15:32:21 -0500 | [diff] [blame] | 117 | pa = (prop_area*)(pa_workspace.data); |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 118 | memset(pa, 0, PA_SIZE); |
| 119 | pa->magic = PROP_AREA_MAGIC; |
| 120 | pa->version = PROP_AREA_VERSION; |
| 121 | |
| 122 | /* plug into the lib property services */ |
| 123 | __legacy_property_area__ = pa; |
| 124 | property_area_inited = 1; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static void update_prop_info(prop_info *pi, const char *value, unsigned len) |
| 129 | { |
| 130 | pi->serial = pi->serial | 1; |
| 131 | memcpy(pi->value, value, len + 1); |
| 132 | pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); |
| 133 | __futex_wake(&pi->serial, INT32_MAX); |
| 134 | } |
| 135 | |
| 136 | static const prop_info *__legacy_property_find(const char *name) |
| 137 | { |
| 138 | prop_area *pa = __legacy_property_area__; |
| 139 | unsigned count = pa->count; |
| 140 | unsigned *toc = pa->toc; |
| 141 | unsigned len = strlen(name); |
| 142 | prop_info *pi; |
| 143 | |
| 144 | while(count--) { |
| 145 | unsigned entry = *toc++; |
| 146 | if(TOC_NAME_LEN(entry) != len) continue; |
| 147 | |
| 148 | pi = TOC_TO_INFO(pa, entry); |
| 149 | if(memcmp(name, pi->name, len)) continue; |
| 150 | |
| 151 | return pi; |
| 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int legacy_property_set(const char *name, const char *value) |
| 158 | { |
| 159 | prop_area *pa; |
| 160 | prop_info *pi; |
| 161 | |
| 162 | int namelen = strlen(name); |
| 163 | int valuelen = strlen(value); |
| 164 | |
| 165 | if(namelen >= PROP_NAME_MAX) return -1; |
| 166 | if(valuelen >= PROP_VALUE_MAX) return -1; |
| 167 | if(namelen < 1) return -1; |
| 168 | |
| 169 | pi = (prop_info*) __legacy_property_find(name); |
| 170 | |
| 171 | |
| 172 | if(pi != 0) { |
| 173 | /* ro.* properties may NEVER be modified once set */ |
| 174 | if(!strncmp(name, "ro.", 3)) return -1; |
| 175 | |
| 176 | pa = __legacy_property_area__; |
| 177 | update_prop_info(pi, value, valuelen); |
| 178 | pa->serial++; |
| 179 | __futex_wake(&pa->serial, INT32_MAX); |
| 180 | } else { |
| 181 | pa = __legacy_property_area__; |
| 182 | if(pa->count == PA_COUNT_MAX) return -1; |
| 183 | |
| 184 | pi = pa_info_array + pa->count; |
| 185 | pi->serial = (valuelen << 24); |
| 186 | memcpy(pi->name, name, namelen + 1); |
| 187 | memcpy(pi->value, value, valuelen + 1); |
| 188 | |
| 189 | pa->toc[pa->count] = |
Ethan Yonker | bcc502c | 2014-11-10 11:22:10 -0600 | [diff] [blame] | 190 | (namelen << 24) | (((unsigned long) pi) - ((unsigned long) pa)); |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 191 | |
| 192 | pa->count++; |
| 193 | pa->serial++; |
| 194 | __futex_wake(&pa->serial, INT32_MAX); |
| 195 | } |
| 196 | |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | void legacy_get_property_workspace(int *fd, int *sz) |
| 201 | { |
| 202 | *fd = pa_workspace.fd; |
| 203 | *sz = pa_workspace.size; |
| 204 | } |
| 205 | |
| 206 | static void copy_property_to_legacy(const char *key, const char *value, void *cookie) |
| 207 | { |
| 208 | legacy_property_set(key, value); |
| 209 | } |
| 210 | |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 211 | int legacy_properties_init() |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 212 | { |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 213 | if(init_property_area() != 0) |
| 214 | return -1; |
that | 7e303cf | 2014-03-06 07:57:43 +0100 | [diff] [blame] | 215 | |
Matt Mower | cdd3b33 | 2014-03-27 14:38:48 -0500 | [diff] [blame] | 216 | if(property_list(copy_property_to_legacy, 0) != 0) |
| 217 | return -1; |
| 218 | |
| 219 | return 0; |
| 220 | } |