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