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