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