The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [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 | |
| 28 | #include <cutils/misc.h> |
| 29 | #include <cutils/sockets.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | |
| 31 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 32 | #include <sys/_system_properties.h> |
| 33 | |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/un.h> |
| 36 | #include <sys/select.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <netinet/in.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/atomics.h> |
| 41 | #include <private/android_filesystem_config.h> |
| 42 | |
| 43 | #include "property_service.h" |
| 44 | #include "init.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 45 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 46 | #include "log.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | |
| 48 | #define PERSISTENT_PROPERTY_DIR "/data/property" |
| 49 | |
| 50 | static int persistent_properties_loaded = 0; |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 51 | static int property_area_inited = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 52 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 53 | static int property_set_fd = -1; |
| 54 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | /* White list of permissions for setting property services. */ |
| 56 | struct { |
| 57 | const char *prefix; |
| 58 | unsigned int uid; |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 59 | unsigned int gid; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | } property_perms[] = { |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 61 | { "net.rmnet0.", AID_RADIO, 0 }, |
| 62 | { "net.gprs.", AID_RADIO, 0 }, |
| 63 | { "net.ppp", AID_RADIO, 0 }, |
Amol Bhatkar | cf01597 | 2011-02-18 17:54:17 -0600 | [diff] [blame] | 64 | { "net.qmi", AID_RADIO, 0 }, |
Benoit Goby | 7100f64 | 2011-07-25 18:02:06 -0700 | [diff] [blame] | 65 | { "net.lte", AID_RADIO, 0 }, |
Benoit Goby | 22bfc4c | 2011-07-28 18:09:55 -0700 | [diff] [blame] | 66 | { "net.cdma", AID_RADIO, 0 }, |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 67 | { "ril.", AID_RADIO, 0 }, |
| 68 | { "gsm.", AID_RADIO, 0 }, |
| 69 | { "persist.radio", AID_RADIO, 0 }, |
| 70 | { "net.dns", AID_RADIO, 0 }, |
Benoit Goby | 5da9358 | 2011-08-22 12:12:37 -0700 | [diff] [blame] | 71 | { "sys.usb.config", AID_RADIO, 0 }, |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 72 | { "net.", AID_SYSTEM, 0 }, |
| 73 | { "dev.", AID_SYSTEM, 0 }, |
| 74 | { "runtime.", AID_SYSTEM, 0 }, |
| 75 | { "hw.", AID_SYSTEM, 0 }, |
| 76 | { "sys.", AID_SYSTEM, 0 }, |
| 77 | { "service.", AID_SYSTEM, 0 }, |
| 78 | { "wlan.", AID_SYSTEM, 0 }, |
| 79 | { "dhcp.", AID_SYSTEM, 0 }, |
| 80 | { "dhcp.", AID_DHCP, 0 }, |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 81 | { "debug.", AID_SHELL, 0 }, |
| 82 | { "log.", AID_SHELL, 0 }, |
| 83 | { "service.adb.root", AID_SHELL, 0 }, |
Mike Lockwood | 58aa5b0 | 2010-12-10 09:48:41 -0800 | [diff] [blame] | 84 | { "service.adb.tcp.port", AID_SHELL, 0 }, |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 85 | { "persist.sys.", AID_SYSTEM, 0 }, |
| 86 | { "persist.service.", AID_SYSTEM, 0 }, |
Oscar Montemayor | ef4e215 | 2009-11-12 12:02:24 -0800 | [diff] [blame] | 87 | { "persist.security.", AID_SYSTEM, 0 }, |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 88 | { NULL, 0, 0 } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * White list of UID that are allowed to start/stop services. |
| 93 | * Currently there are no user apps that require. |
| 94 | */ |
| 95 | struct { |
| 96 | const char *service; |
| 97 | unsigned int uid; |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 98 | unsigned int gid; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 99 | } control_perms[] = { |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 100 | { "dumpstate",AID_SHELL, AID_LOG }, |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 101 | { "ril-daemon",AID_RADIO, AID_RADIO }, |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 102 | {NULL, 0, 0 } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | typedef struct { |
| 106 | void *data; |
| 107 | size_t size; |
| 108 | int fd; |
| 109 | } workspace; |
| 110 | |
| 111 | static int init_workspace(workspace *w, size_t size) |
| 112 | { |
| 113 | void *data; |
| 114 | int fd; |
| 115 | |
Brian Swetland | 25b15be | 2010-07-13 16:43:56 -0700 | [diff] [blame] | 116 | /* dev is a tmpfs that we can use to carve a shared workspace |
| 117 | * out of, so let's do that... |
| 118 | */ |
| 119 | fd = open("/dev/__properties__", O_RDWR | O_CREAT, 0600); |
| 120 | if (fd < 0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | return -1; |
| 122 | |
Brian Swetland | 25b15be | 2010-07-13 16:43:56 -0700 | [diff] [blame] | 123 | if (ftruncate(fd, size) < 0) |
| 124 | goto out; |
| 125 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 127 | if(data == MAP_FAILED) |
| 128 | goto out; |
| 129 | |
Brian Swetland | 25b15be | 2010-07-13 16:43:56 -0700 | [diff] [blame] | 130 | close(fd); |
| 131 | |
| 132 | fd = open("/dev/__properties__", O_RDONLY); |
| 133 | if (fd < 0) |
| 134 | return -1; |
| 135 | |
| 136 | unlink("/dev/__properties__"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 137 | |
| 138 | w->data = data; |
| 139 | w->size = size; |
| 140 | w->fd = fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | return 0; |
| 142 | |
| 143 | out: |
| 144 | close(fd); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | /* (8 header words + 247 toc words) = 1020 bytes */ |
| 149 | /* 1024 bytes header and toc + 247 prop_infos @ 128 bytes = 32640 bytes */ |
| 150 | |
| 151 | #define PA_COUNT_MAX 247 |
| 152 | #define PA_INFO_START 1024 |
| 153 | #define PA_SIZE 32768 |
| 154 | |
| 155 | static workspace pa_workspace; |
| 156 | static prop_info *pa_info_array; |
| 157 | |
| 158 | extern prop_area *__system_property_area__; |
| 159 | |
| 160 | static int init_property_area(void) |
| 161 | { |
| 162 | prop_area *pa; |
| 163 | |
| 164 | if(pa_info_array) |
| 165 | return -1; |
| 166 | |
| 167 | if(init_workspace(&pa_workspace, PA_SIZE)) |
| 168 | return -1; |
| 169 | |
| 170 | fcntl(pa_workspace.fd, F_SETFD, FD_CLOEXEC); |
| 171 | |
| 172 | pa_info_array = (void*) (((char*) pa_workspace.data) + PA_INFO_START); |
| 173 | |
| 174 | pa = pa_workspace.data; |
| 175 | memset(pa, 0, PA_SIZE); |
| 176 | pa->magic = PROP_AREA_MAGIC; |
| 177 | pa->version = PROP_AREA_VERSION; |
| 178 | |
| 179 | /* plug into the lib property services */ |
| 180 | __system_property_area__ = pa; |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 181 | property_area_inited = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static void update_prop_info(prop_info *pi, const char *value, unsigned len) |
| 186 | { |
| 187 | pi->serial = pi->serial | 1; |
| 188 | memcpy(pi->value, value, len + 1); |
| 189 | pi->serial = (len << 24) | ((pi->serial + 1) & 0xffffff); |
| 190 | __futex_wake(&pi->serial, INT32_MAX); |
| 191 | } |
| 192 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 193 | /* |
| 194 | * Checks permissions for starting/stoping system services. |
| 195 | * AID_SYSTEM and AID_ROOT are always allowed. |
| 196 | * |
| 197 | * Returns 1 if uid allowed, 0 otherwise. |
| 198 | */ |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 199 | static int check_control_perms(const char *name, unsigned int uid, unsigned int gid) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 200 | int i; |
| 201 | if (uid == AID_SYSTEM || uid == AID_ROOT) |
| 202 | return 1; |
| 203 | |
| 204 | /* Search the ACL */ |
| 205 | for (i = 0; control_perms[i].service; i++) { |
| 206 | if (strcmp(control_perms[i].service, name) == 0) { |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 207 | if ((uid && control_perms[i].uid == uid) || |
| 208 | (gid && control_perms[i].gid == gid)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 209 | return 1; |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 210 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Checks permissions for setting system properties. |
| 218 | * Returns 1 if uid allowed, 0 otherwise. |
| 219 | */ |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 220 | static int check_perms(const char *name, unsigned int uid, unsigned int gid) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | { |
| 222 | int i; |
| 223 | if (uid == 0) |
| 224 | return 1; |
| 225 | |
| 226 | if(!strncmp(name, "ro.", 3)) |
| 227 | name +=3; |
| 228 | |
| 229 | for (i = 0; property_perms[i].prefix; i++) { |
| 230 | int tmp; |
| 231 | if (strncmp(property_perms[i].prefix, name, |
| 232 | strlen(property_perms[i].prefix)) == 0) { |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 233 | if ((uid && property_perms[i].uid == uid) || |
| 234 | (gid && property_perms[i].gid == gid)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | return 1; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | const char* property_get(const char *name) |
| 244 | { |
| 245 | prop_info *pi; |
| 246 | |
| 247 | if(strlen(name) >= PROP_NAME_MAX) return 0; |
| 248 | |
| 249 | pi = (prop_info*) __system_property_find(name); |
| 250 | |
| 251 | if(pi != 0) { |
| 252 | return pi->value; |
| 253 | } else { |
| 254 | return 0; |
| 255 | } |
| 256 | } |
| 257 | |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 258 | static void write_persistent_property(const char *name, const char *value) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | { |
| 260 | const char *tempPath = PERSISTENT_PROPERTY_DIR "/.temp"; |
| 261 | char path[PATH_MAX]; |
| 262 | int fd, length; |
| 263 | |
| 264 | snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, name); |
| 265 | |
| 266 | fd = open(tempPath, O_WRONLY|O_CREAT|O_TRUNC, 0600); |
| 267 | if (fd < 0) { |
| 268 | ERROR("Unable to write persistent property to temp file %s errno: %d\n", tempPath, errno); |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 269 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 270 | } |
| 271 | write(fd, value, strlen(value)); |
| 272 | close(fd); |
| 273 | |
| 274 | if (rename(tempPath, path)) { |
| 275 | unlink(tempPath); |
| 276 | ERROR("Unable to rename persistent property file %s to %s\n", tempPath, path); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | int property_set(const char *name, const char *value) |
| 281 | { |
| 282 | prop_area *pa; |
| 283 | prop_info *pi; |
| 284 | |
| 285 | int namelen = strlen(name); |
| 286 | int valuelen = strlen(value); |
| 287 | |
| 288 | if(namelen >= PROP_NAME_MAX) return -1; |
| 289 | if(valuelen >= PROP_VALUE_MAX) return -1; |
| 290 | if(namelen < 1) return -1; |
| 291 | |
| 292 | pi = (prop_info*) __system_property_find(name); |
| 293 | |
| 294 | if(pi != 0) { |
| 295 | /* ro.* properties may NEVER be modified once set */ |
| 296 | if(!strncmp(name, "ro.", 3)) return -1; |
| 297 | |
| 298 | pa = __system_property_area__; |
| 299 | update_prop_info(pi, value, valuelen); |
| 300 | pa->serial++; |
| 301 | __futex_wake(&pa->serial, INT32_MAX); |
| 302 | } else { |
| 303 | pa = __system_property_area__; |
| 304 | if(pa->count == PA_COUNT_MAX) return -1; |
| 305 | |
| 306 | pi = pa_info_array + pa->count; |
| 307 | pi->serial = (valuelen << 24); |
| 308 | memcpy(pi->name, name, namelen + 1); |
| 309 | memcpy(pi->value, value, valuelen + 1); |
| 310 | |
| 311 | pa->toc[pa->count] = |
| 312 | (namelen << 24) | (((unsigned) pi) - ((unsigned) pa)); |
| 313 | |
| 314 | pa->count++; |
| 315 | pa->serial++; |
| 316 | __futex_wake(&pa->serial, INT32_MAX); |
| 317 | } |
| 318 | /* If name starts with "net." treat as a DNS property. */ |
Mike Lockwood | b377955 | 2009-05-08 14:27:42 -0400 | [diff] [blame] | 319 | if (strncmp("net.", name, strlen("net.")) == 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 320 | if (strcmp("net.change", name) == 0) { |
| 321 | return 0; |
| 322 | } |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 323 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 324 | * The 'net.change' property is a special property used track when any |
| 325 | * 'net.*' property name is updated. It is _ONLY_ updated here. Its value |
| 326 | * contains the last updated 'net.*' property. |
| 327 | */ |
| 328 | property_set("net.change", name); |
| 329 | } else if (persistent_properties_loaded && |
Mike Lockwood | b377955 | 2009-05-08 14:27:42 -0400 | [diff] [blame] | 330 | strncmp("persist.", name, strlen("persist.")) == 0) { |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 331 | /* |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | * Don't write properties to disk until after we have read all default properties |
| 333 | * to prevent them from being overwritten by default values. |
| 334 | */ |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 335 | write_persistent_property(name, value); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 336 | } |
| 337 | property_changed(name, value); |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int property_list(void (*propfn)(const char *key, const char *value, void *cookie), |
| 342 | void *cookie) |
| 343 | { |
| 344 | char name[PROP_NAME_MAX]; |
| 345 | char value[PROP_VALUE_MAX]; |
| 346 | const prop_info *pi; |
| 347 | unsigned n; |
| 348 | |
| 349 | for(n = 0; (pi = __system_property_find_nth(n)); n++) { |
| 350 | __system_property_read(pi, name, value); |
| 351 | propfn(name, value, cookie); |
| 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 356 | void handle_property_set_fd() |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | { |
| 358 | prop_msg msg; |
| 359 | int s; |
| 360 | int r; |
| 361 | int res; |
| 362 | struct ucred cr; |
| 363 | struct sockaddr_un addr; |
| 364 | socklen_t addr_size = sizeof(addr); |
| 365 | socklen_t cr_size = sizeof(cr); |
| 366 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 367 | if ((s = accept(property_set_fd, (struct sockaddr *) &addr, &addr_size)) < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 368 | return; |
| 369 | } |
| 370 | |
| 371 | /* Check socket options here */ |
| 372 | if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cr, &cr_size) < 0) { |
| 373 | close(s); |
| 374 | ERROR("Unable to recieve socket options\n"); |
| 375 | return; |
| 376 | } |
| 377 | |
Mike Lockwood | d18678f | 2011-07-01 14:48:50 -0400 | [diff] [blame] | 378 | r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | if(r != sizeof(prop_msg)) { |
Mike Lockwood | d18678f | 2011-07-01 14:48:50 -0400 | [diff] [blame] | 380 | ERROR("sys_prop: mis-match msg size recieved: %d expected: %d errno: %d\n", |
| 381 | r, sizeof(prop_msg), errno); |
Brad Fitzpatrick | 9f1e0e3 | 2011-03-30 12:39:56 -0700 | [diff] [blame] | 382 | close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | switch(msg.cmd) { |
| 387 | case PROP_MSG_SETPROP: |
| 388 | msg.name[PROP_NAME_MAX-1] = 0; |
| 389 | msg.value[PROP_VALUE_MAX-1] = 0; |
| 390 | |
| 391 | if(memcmp(msg.name,"ctl.",4) == 0) { |
Brad Fitzpatrick | 71ead18 | 2011-04-01 08:24:13 -0700 | [diff] [blame] | 392 | // Keep the old close-socket-early behavior when handling |
| 393 | // ctl.* properties. |
| 394 | close(s); |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 395 | if (check_control_perms(msg.value, cr.uid, cr.gid)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 396 | handle_control_message((char*) msg.name + 4, (char*) msg.value); |
| 397 | } else { |
Wink Saville | cfa0d84 | 2010-10-03 13:30:11 -0700 | [diff] [blame] | 398 | ERROR("sys_prop: Unable to %s service ctl [%s] uid:%d gid:%d pid:%d\n", |
| 399 | msg.name + 4, msg.value, cr.uid, cr.gid, cr.pid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | } |
| 401 | } else { |
Mike Lockwood | c5e7ef2 | 2009-09-02 18:08:52 -0400 | [diff] [blame] | 402 | if (check_perms(msg.name, cr.uid, cr.gid)) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | property_set((char*) msg.name, (char*) msg.value); |
| 404 | } else { |
| 405 | ERROR("sys_prop: permission denied uid:%d name:%s\n", |
| 406 | cr.uid, msg.name); |
| 407 | } |
Brad Fitzpatrick | 71ead18 | 2011-04-01 08:24:13 -0700 | [diff] [blame] | 408 | |
| 409 | // Note: bionic's property client code assumes that the |
| 410 | // property server will not close the socket until *AFTER* |
| 411 | // the property is written to memory. |
| 412 | close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 413 | } |
| 414 | break; |
| 415 | |
| 416 | default: |
Brad Fitzpatrick | 71ead18 | 2011-04-01 08:24:13 -0700 | [diff] [blame] | 417 | close(s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void get_property_workspace(int *fd, int *sz) |
| 423 | { |
| 424 | *fd = pa_workspace.fd; |
| 425 | *sz = pa_workspace.size; |
| 426 | } |
| 427 | |
| 428 | static void load_properties(char *data) |
| 429 | { |
| 430 | char *key, *value, *eol, *sol, *tmp; |
| 431 | |
| 432 | sol = data; |
| 433 | while((eol = strchr(sol, '\n'))) { |
| 434 | key = sol; |
| 435 | *eol++ = 0; |
| 436 | sol = eol; |
| 437 | |
| 438 | value = strchr(key, '='); |
| 439 | if(value == 0) continue; |
| 440 | *value++ = 0; |
| 441 | |
| 442 | while(isspace(*key)) key++; |
| 443 | if(*key == '#') continue; |
| 444 | tmp = value - 2; |
| 445 | while((tmp > key) && isspace(*tmp)) *tmp-- = 0; |
| 446 | |
| 447 | while(isspace(*value)) value++; |
| 448 | tmp = eol - 2; |
| 449 | while((tmp > value) && isspace(*tmp)) *tmp-- = 0; |
| 450 | |
| 451 | property_set(key, value); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | static void load_properties_from_file(const char *fn) |
| 456 | { |
| 457 | char *data; |
| 458 | unsigned sz; |
| 459 | |
| 460 | data = read_file(fn, &sz); |
| 461 | |
| 462 | if(data != 0) { |
| 463 | load_properties(data); |
| 464 | free(data); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | static void load_persistent_properties() |
| 469 | { |
| 470 | DIR* dir = opendir(PERSISTENT_PROPERTY_DIR); |
| 471 | struct dirent* entry; |
| 472 | char path[PATH_MAX]; |
| 473 | char value[PROP_VALUE_MAX]; |
| 474 | int fd, length; |
| 475 | |
| 476 | if (dir) { |
| 477 | while ((entry = readdir(dir)) != NULL) { |
Mike Lockwood | b377955 | 2009-05-08 14:27:42 -0400 | [diff] [blame] | 478 | if (strncmp("persist.", entry->d_name, strlen("persist."))) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 479 | continue; |
| 480 | #if HAVE_DIRENT_D_TYPE |
| 481 | if (entry->d_type != DT_REG) |
| 482 | continue; |
| 483 | #endif |
| 484 | /* open the file and read the property value */ |
| 485 | snprintf(path, sizeof(path), "%s/%s", PERSISTENT_PROPERTY_DIR, entry->d_name); |
| 486 | fd = open(path, O_RDONLY); |
| 487 | if (fd >= 0) { |
| 488 | length = read(fd, value, sizeof(value) - 1); |
| 489 | if (length >= 0) { |
| 490 | value[length] = 0; |
| 491 | property_set(entry->d_name, value); |
| 492 | } else { |
| 493 | ERROR("Unable to read persistent property file %s errno: %d\n", path, errno); |
| 494 | } |
| 495 | close(fd); |
| 496 | } else { |
| 497 | ERROR("Unable to open persistent property file %s errno: %d\n", path, errno); |
| 498 | } |
| 499 | } |
| 500 | closedir(dir); |
| 501 | } else { |
| 502 | ERROR("Unable to open persistent property directory %s errno: %d\n", PERSISTENT_PROPERTY_DIR, errno); |
| 503 | } |
Tammo Spalink | 3dfe6c6 | 2009-08-26 10:13:20 +0800 | [diff] [blame] | 504 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 | persistent_properties_loaded = 1; |
| 506 | } |
| 507 | |
Dima Zavin | ebf4658 | 2011-09-06 10:12:21 -0700 | [diff] [blame] | 508 | void property_init(bool load_defaults) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 509 | { |
| 510 | init_property_area(); |
Dima Zavin | ebf4658 | 2011-09-06 10:12:21 -0700 | [diff] [blame] | 511 | if (load_defaults) |
| 512 | load_properties_from_file(PROP_PATH_RAMDISK_DEFAULT); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 513 | } |
| 514 | |
Colin Cross | 3294bbb | 2010-04-19 17:11:33 -0700 | [diff] [blame] | 515 | int properties_inited(void) |
| 516 | { |
| 517 | return property_area_inited; |
| 518 | } |
| 519 | |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 520 | /* When booting an encrypted system, /data is not mounted when the |
| 521 | * property service is started, so any properties stored there are |
| 522 | * not loaded. Vold triggers init to load these properties once it |
| 523 | * has mounted /data. |
| 524 | */ |
| 525 | void load_persist_props(void) |
| 526 | { |
| 527 | load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE); |
| 528 | /* Read persistent properties after all default values have been loaded. */ |
| 529 | load_persistent_properties(); |
| 530 | } |
| 531 | |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 532 | void start_property_service(void) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 533 | { |
| 534 | int fd; |
| 535 | |
| 536 | load_properties_from_file(PROP_PATH_SYSTEM_BUILD); |
| 537 | load_properties_from_file(PROP_PATH_SYSTEM_DEFAULT); |
| 538 | load_properties_from_file(PROP_PATH_LOCAL_OVERRIDE); |
| 539 | /* Read persistent properties after all default values have been loaded. */ |
| 540 | load_persistent_properties(); |
| 541 | |
| 542 | fd = create_socket(PROP_SERVICE_NAME, SOCK_STREAM, 0666, 0, 0); |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 543 | if(fd < 0) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 544 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 545 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 546 | |
| 547 | listen(fd, 8); |
Colin Cross | d11beb2 | 2010-04-13 19:33:37 -0700 | [diff] [blame] | 548 | property_set_fd = fd; |
| 549 | } |
| 550 | |
| 551 | int get_property_set_fd() |
| 552 | { |
| 553 | return property_set_fd; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 554 | } |