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