devices.c revision 4a6f2321e76685b9489d020980dffb1d9ac73fc0
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 <errno.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include <sys/stat.h> 21#include <sys/types.h> 22 23#include <fcntl.h> 24#include <dirent.h> 25#include <unistd.h> 26#include <string.h> 27 28#include <sys/socket.h> 29#include <sys/un.h> 30#include <linux/netlink.h> 31#include <private/android_filesystem_config.h> 32#include <sys/time.h> 33#include <asm/page.h> 34 35#include "init.h" 36#include "devices.h" 37 38#define CMDLINE_PREFIX "/dev" 39#define SYSFS_PREFIX "/sys" 40#define FIRMWARE_DIR "/etc/firmware" 41#define MAX_QEMU_PERM 6 42 43struct uevent { 44 const char *action; 45 const char *path; 46 const char *subsystem; 47 const char *firmware; 48 int major; 49 int minor; 50}; 51 52static int open_uevent_socket(void) 53{ 54 struct sockaddr_nl addr; 55 int sz = 64*1024; // XXX larger? udev uses 16MB! 56 int s; 57 58 memset(&addr, 0, sizeof(addr)); 59 addr.nl_family = AF_NETLINK; 60 addr.nl_pid = getpid(); 61 addr.nl_groups = 0xffffffff; 62 63 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT); 64 if(s < 0) 65 return -1; 66 67 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz)); 68 69 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { 70 close(s); 71 return -1; 72 } 73 74 return s; 75} 76 77struct perms_ { 78 char *name; 79 mode_t perm; 80 unsigned int uid; 81 unsigned int gid; 82 unsigned short prefix; 83}; 84static struct perms_ devperms[] = { 85 { "/dev/null", 0666, AID_ROOT, AID_ROOT, 0 }, 86 { "/dev/zero", 0666, AID_ROOT, AID_ROOT, 0 }, 87 { "/dev/full", 0666, AID_ROOT, AID_ROOT, 0 }, 88 { "/dev/ptmx", 0666, AID_ROOT, AID_ROOT, 0 }, 89 { "/dev/tty", 0666, AID_ROOT, AID_ROOT, 0 }, 90 { "/dev/random", 0666, AID_ROOT, AID_ROOT, 0 }, 91 { "/dev/urandom", 0666, AID_ROOT, AID_ROOT, 0 }, 92 { "/dev/ashmem", 0666, AID_ROOT, AID_ROOT, 0 }, 93 { "/dev/binder", 0666, AID_ROOT, AID_ROOT, 0 }, 94 95 /* logger should be world writable (for logging) but not readable */ 96 { "/dev/log/", 0662, AID_ROOT, AID_LOG, 1 }, 97 98 /* these should not be world writable */ 99 { "/dev/android_adb", 0660, AID_ADB, AID_ADB, 0 }, 100 { "/dev/android_adb_enable", 0660, AID_ADB, AID_ADB, 0 }, 101 { "/dev/ttyMSM0", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 }, 102 { "/dev/ttyHS0", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 }, 103 { "/dev/uinput", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 }, 104 { "/dev/alarm", 0664, AID_SYSTEM, AID_RADIO, 0 }, 105 { "/dev/tty0", 0660, AID_ROOT, AID_SYSTEM, 0 }, 106 { "/dev/graphics/", 0660, AID_ROOT, AID_GRAPHICS, 1 }, 107 { "/dev/hw3d", 0660, AID_SYSTEM, AID_GRAPHICS, 0 }, 108 { "/dev/input/", 0660, AID_ROOT, AID_INPUT, 1 }, 109 { "/dev/eac", 0660, AID_ROOT, AID_AUDIO, 0 }, 110 { "/dev/cam", 0660, AID_ROOT, AID_CAMERA, 0 }, 111 { "/dev/pmem", 0660, AID_SYSTEM, AID_GRAPHICS, 0 }, 112 { "/dev/pmem_gpu", 0660, AID_SYSTEM, AID_GRAPHICS, 1 }, 113 { "/dev/pmem_adsp", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 114 { "/dev/pmem_camera", 0660, AID_SYSTEM, AID_CAMERA, 1 }, 115 { "/dev/oncrpc/", 0660, AID_ROOT, AID_SYSTEM, 1 }, 116 { "/dev/adsp/", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 117 { "/dev/mt9t013", 0660, AID_SYSTEM, AID_SYSTEM, 0 }, 118 { "/dev/akm8976_daemon",0640, AID_COMPASS, AID_SYSTEM, 0 }, 119 { "/dev/akm8976_aot", 0640, AID_COMPASS, AID_SYSTEM, 0 }, 120 { "/dev/akm8976_pffd", 0640, AID_COMPASS, AID_SYSTEM, 0 }, 121 { "/dev/msm_pcm_out", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 122 { "/dev/msm_pcm_in", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 123 { "/dev/msm_pcm_ctl", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 124 { "/dev/msm_snd", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 125 { "/dev/msm_mp3", 0660, AID_SYSTEM, AID_AUDIO, 1 }, 126 { "/dev/msm_audpre", 0660, AID_SYSTEM, AID_AUDIO, 0 }, 127 { "/dev/htc-acoustic", 0660, AID_SYSTEM, AID_AUDIO, 0 }, 128 { "/dev/smd0", 0640, AID_RADIO, AID_RADIO, 0 }, 129 { "/dev/qemu_trace", 0666, AID_SYSTEM, AID_SYSTEM, 0 }, 130 { "/dev/qmi", 0640, AID_RADIO, AID_RADIO, 0 }, 131 { "/dev/qmi0", 0640, AID_RADIO, AID_RADIO, 0 }, 132 { "/dev/qmi1", 0640, AID_RADIO, AID_RADIO, 0 }, 133 { "/dev/qmi2", 0640, AID_RADIO, AID_RADIO, 0 }, 134 { "/dev/tun", 0640, AID_VPN , AID_VPN, 0 }, 135 { NULL, 0, 0, 0, 0 }, 136}; 137 138/* devperms_partners list and perm_node are for hardware specific /dev entries */ 139struct perm_node { 140 struct perms_ dp; 141 struct listnode plist; 142}; 143list_declare(devperms_partners); 144 145/* 146 * Permission override when in emulator mode, must be parsed before 147 * system properties is initalized. 148 */ 149static int qemu_perm_count; 150static struct perms_ qemu_perms[MAX_QEMU_PERM + 1]; 151 152int add_devperms_partners(const char *name, mode_t perm, unsigned int uid, 153 unsigned int gid, unsigned short prefix) { 154 int size; 155 struct perm_node *node = malloc(sizeof (struct perm_node)); 156 if (!node) 157 return -ENOMEM; 158 159 size = strlen(name) + 1; 160 if ((node->dp.name = malloc(size)) == NULL) 161 return -ENOMEM; 162 163 memcpy(node->dp.name, name, size); 164 node->dp.perm = perm; 165 node->dp.uid = uid; 166 node->dp.gid = gid; 167 node->dp.prefix = prefix; 168 169 list_add_tail(&devperms_partners, &node->plist); 170 return 0; 171} 172 173void qemu_init(void) { 174 qemu_perm_count = 0; 175 memset(&qemu_perms, 0, sizeof(qemu_perms)); 176} 177 178static int qemu_perm(const char* name, mode_t perm, unsigned int uid, 179 unsigned int gid, unsigned short prefix) 180{ 181 char *buf; 182 if (qemu_perm_count == MAX_QEMU_PERM) 183 return -ENOSPC; 184 185 buf = malloc(strlen(name) + 1); 186 if (!buf) 187 return -errno; 188 189 strlcpy(buf, name, strlen(name) + 1); 190 qemu_perms[qemu_perm_count].name = buf; 191 qemu_perms[qemu_perm_count].perm = perm; 192 qemu_perms[qemu_perm_count].uid = uid; 193 qemu_perms[qemu_perm_count].gid = gid; 194 qemu_perms[qemu_perm_count].prefix = prefix; 195 196 qemu_perm_count++; 197 return 0; 198} 199 200/* Permission overrides for emulator that are parsed from /proc/cmdline. */ 201void qemu_cmdline(const char* name, const char *value) 202{ 203 char *buf; 204 if (!strcmp(name, "android.ril")) { 205 /* cmd line params currently assume /dev/ prefix */ 206 if (asprintf(&buf, CMDLINE_PREFIX"/%s", value) == -1) { 207 return; 208 } 209 INFO("nani- buf:: %s\n", buf); 210 qemu_perm(buf, 0660, AID_RADIO, AID_ROOT, 0); 211 } 212} 213 214static int get_device_perm_inner(struct perms_ *perms, const char *path, 215 unsigned *uid, unsigned *gid, mode_t *perm) 216{ 217 int i; 218 for(i = 0; perms[i].name; i++) { 219 220 if(perms[i].prefix) { 221 if(strncmp(path, perms[i].name, strlen(perms[i].name))) 222 continue; 223 } else { 224 if(strcmp(path, perms[i].name)) 225 continue; 226 } 227 *uid = perms[i].uid; 228 *gid = perms[i].gid; 229 *perm = perms[i].perm; 230 return 0; 231 } 232 return -1; 233} 234 235/* First checks for emulator specific permissions specified in /proc/cmdline. */ 236static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid) 237{ 238 mode_t perm; 239 240 if (get_device_perm_inner(qemu_perms, path, uid, gid, &perm) == 0) { 241 return perm; 242 } else if (get_device_perm_inner(devperms, path, uid, gid, &perm) == 0) { 243 return perm; 244 } else { 245 struct listnode *node; 246 struct perm_node *perm_node; 247 struct perms_ *dp; 248 249 /* Check partners list. */ 250 list_for_each(node, &devperms_partners) { 251 perm_node = node_to_item(node, struct perm_node, plist); 252 dp = &perm_node->dp; 253 254 if (dp->prefix) { 255 if (strncmp(path, dp->name, strlen(dp->name))) 256 continue; 257 } else { 258 if (strcmp(path, dp->name)) 259 continue; 260 } 261 /* Found perm in partner list. */ 262 *uid = dp->uid; 263 *gid = dp->gid; 264 return dp->perm; 265 } 266 /* Default if nothing found. */ 267 *uid = 0; 268 *gid = 0; 269 return 0600; 270 } 271} 272 273static void make_device(const char *path, int block, int major, int minor) 274{ 275 unsigned uid; 276 unsigned gid; 277 mode_t mode; 278 dev_t dev; 279 280 if(major > 255 || minor > 255) 281 return; 282 283 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR); 284 dev = (major << 8) | minor; 285 mknod(path, mode, dev); 286 chown(path, uid, gid); 287} 288 289#ifdef LOG_UEVENTS 290 291static inline suseconds_t get_usecs(void) 292{ 293 struct timeval tv; 294 gettimeofday(&tv, 0); 295 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec; 296} 297 298#define log_event_print(x...) INFO(x) 299 300#else 301 302#define log_event_print(fmt, args...) do { } while (0) 303#define get_usecs() 0 304 305#endif 306 307static void parse_event(const char *msg, struct uevent *uevent) 308{ 309 uevent->action = ""; 310 uevent->path = ""; 311 uevent->subsystem = ""; 312 uevent->firmware = ""; 313 uevent->major = -1; 314 uevent->minor = -1; 315 316 /* currently ignoring SEQNUM */ 317 while(*msg) { 318 if(!strncmp(msg, "ACTION=", 7)) { 319 msg += 7; 320 uevent->action = msg; 321 } else if(!strncmp(msg, "DEVPATH=", 8)) { 322 msg += 8; 323 uevent->path = msg; 324 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) { 325 msg += 10; 326 uevent->subsystem = msg; 327 } else if(!strncmp(msg, "FIRMWARE=", 9)) { 328 msg += 9; 329 uevent->firmware = msg; 330 } else if(!strncmp(msg, "MAJOR=", 6)) { 331 msg += 6; 332 uevent->major = atoi(msg); 333 } else if(!strncmp(msg, "MINOR=", 6)) { 334 msg += 6; 335 uevent->minor = atoi(msg); 336 } 337 338 /* advance to after the next \0 */ 339 while(*msg++) 340 ; 341 } 342 343 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n", 344 uevent->action, uevent->path, uevent->subsystem, 345 uevent->firmware, uevent->major, uevent->minor); 346} 347 348static void handle_device_event(struct uevent *uevent) 349{ 350 char devpath[96]; 351 char *base, *name; 352 int block; 353 354 /* if it's not a /dev device, nothing to do */ 355 if((uevent->major < 0) || (uevent->minor < 0)) 356 return; 357 358 /* do we have a name? */ 359 name = strrchr(uevent->path, '/'); 360 if(!name) 361 return; 362 name++; 363 364 /* too-long names would overrun our buffer */ 365 if(strlen(name) > 64) 366 return; 367 368 /* are we block or char? where should we live? */ 369 if(!strncmp(uevent->subsystem, "block", 5)) { 370 block = 1; 371 base = "/dev/block/"; 372 mkdir(base, 0755); 373 } else { 374 block = 0; 375 /* this should probably be configurable somehow */ 376 if(!strncmp(uevent->subsystem, "graphics", 8)) { 377 base = "/dev/graphics/"; 378 mkdir(base, 0755); 379 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) { 380 base = "/dev/oncrpc/"; 381 mkdir(base, 0755); 382 } else if (!strncmp(uevent->subsystem, "adsp", 4)) { 383 base = "/dev/adsp/"; 384 mkdir(base, 0755); 385 } else if(!strncmp(uevent->subsystem, "input", 5)) { 386 base = "/dev/input/"; 387 mkdir(base, 0755); 388 } else if(!strncmp(uevent->subsystem, "mtd", 3)) { 389 base = "/dev/mtd/"; 390 mkdir(base, 0755); 391 } else if(!strncmp(uevent->subsystem, "misc", 4) && 392 !strncmp(name, "log_", 4)) { 393 base = "/dev/log/"; 394 mkdir(base, 0755); 395 name += 4; 396 } else 397 base = "/dev/"; 398 } 399 400 snprintf(devpath, sizeof(devpath), "%s%s", base, name); 401 402 if(!strcmp(uevent->action, "add")) { 403 make_device(devpath, block, uevent->major, uevent->minor); 404 return; 405 } 406 407 if(!strcmp(uevent->action, "remove")) { 408 unlink(devpath); 409 return; 410 } 411} 412 413static int load_firmware(int fw_fd, int loading_fd, int data_fd) 414{ 415 struct stat st; 416 long len_to_copy; 417 int ret = 0; 418 419 if(fstat(fw_fd, &st) < 0) 420 return -1; 421 len_to_copy = st.st_size; 422 423 write(loading_fd, "1", 1); /* start transfer */ 424 425 while (len_to_copy > 0) { 426 char buf[PAGE_SIZE]; 427 ssize_t nr; 428 429 nr = read(fw_fd, buf, sizeof(buf)); 430 if(!nr) 431 break; 432 if(nr < 0) { 433 ret = -1; 434 break; 435 } 436 437 len_to_copy -= nr; 438 while (nr > 0) { 439 ssize_t nw = 0; 440 441 nw = write(data_fd, buf + nw, nr); 442 if(nw <= 0) { 443 ret = -1; 444 goto out; 445 } 446 nr -= nw; 447 } 448 } 449 450out: 451 if(!ret) 452 write(loading_fd, "0", 1); /* successful end of transfer */ 453 else 454 write(loading_fd, "-1", 2); /* abort transfer */ 455 456 return ret; 457} 458 459static void process_firmware_event(struct uevent *uevent) 460{ 461 char *root, *loading, *data, *file; 462 int l, loading_fd, data_fd, fw_fd; 463 464 log_event_print("firmware event { '%s', '%s' }\n", 465 uevent->path, uevent->firmware); 466 467 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path); 468 if (l == -1) 469 return; 470 471 l = asprintf(&loading, "%sloading", root); 472 if (l == -1) 473 goto root_free_out; 474 475 l = asprintf(&data, "%sdata", root); 476 if (l == -1) 477 goto loading_free_out; 478 479 l = asprintf(&file, FIRMWARE_DIR"/%s", uevent->firmware); 480 if (l == -1) 481 goto data_free_out; 482 483 loading_fd = open(loading, O_WRONLY); 484 if(loading_fd < 0) 485 goto file_free_out; 486 487 data_fd = open(data, O_WRONLY); 488 if(data_fd < 0) 489 goto loading_close_out; 490 491 fw_fd = open(file, O_RDONLY); 492 if(fw_fd < 0) 493 goto data_close_out; 494 495 if(!load_firmware(fw_fd, loading_fd, data_fd)) 496 log_event_print("firmware copy success { '%s', '%s' }\n", root, file); 497 else 498 log_event_print("firmware copy failure { '%s', '%s' }\n", root, file); 499 500 close(fw_fd); 501data_close_out: 502 close(data_fd); 503loading_close_out: 504 close(loading_fd); 505file_free_out: 506 free(file); 507data_free_out: 508 free(data); 509loading_free_out: 510 free(loading); 511root_free_out: 512 free(root); 513} 514 515static void handle_firmware_event(struct uevent *uevent) 516{ 517 pid_t pid; 518 519 if(strcmp(uevent->subsystem, "firmware")) 520 return; 521 522 if(strcmp(uevent->action, "add")) 523 return; 524 525 /* we fork, to avoid making large memory allocations in init proper */ 526 pid = fork(); 527 if (!pid) { 528 process_firmware_event(uevent); 529 exit(EXIT_SUCCESS); 530 } 531} 532 533#define UEVENT_MSG_LEN 1024 534void handle_device_fd(int fd) 535{ 536 char msg[UEVENT_MSG_LEN+2]; 537 int n; 538 539 while((n = recv(fd, msg, UEVENT_MSG_LEN, 0)) > 0) { 540 struct uevent uevent; 541 542 if(n == UEVENT_MSG_LEN) /* overflow -- discard */ 543 continue; 544 545 msg[n] = '\0'; 546 msg[n+1] = '\0'; 547 548 parse_event(msg, &uevent); 549 550 handle_device_event(&uevent); 551 handle_firmware_event(&uevent); 552 } 553} 554 555/* Coldboot walks parts of the /sys tree and pokes the uevent files 556** to cause the kernel to regenerate device add events that happened 557** before init's device manager was started 558** 559** We drain any pending events from the netlink socket every time 560** we poke another uevent file to make sure we don't overrun the 561** socket's buffer. 562*/ 563 564static void do_coldboot(int event_fd, DIR *d) 565{ 566 struct dirent *de; 567 int dfd, fd; 568 569 dfd = dirfd(d); 570 571 fd = openat(dfd, "uevent", O_WRONLY); 572 if(fd >= 0) { 573 write(fd, "add\n", 4); 574 close(fd); 575 handle_device_fd(event_fd); 576 } 577 578 while((de = readdir(d))) { 579 DIR *d2; 580 581 if(de->d_type != DT_DIR || de->d_name[0] == '.') 582 continue; 583 584 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY); 585 if(fd < 0) 586 continue; 587 588 d2 = fdopendir(fd); 589 if(d2 == 0) 590 close(fd); 591 else { 592 do_coldboot(event_fd, d2); 593 closedir(d2); 594 } 595 } 596} 597 598static void coldboot(int event_fd, const char *path) 599{ 600 DIR *d = opendir(path); 601 if(d) { 602 do_coldboot(event_fd, d); 603 closedir(d); 604 } 605} 606 607int device_init(void) 608{ 609 suseconds_t t0, t1; 610 int fd; 611 612 fd = open_uevent_socket(); 613 if(fd < 0) 614 return -1; 615 616 fcntl(fd, F_SETFD, FD_CLOEXEC); 617 fcntl(fd, F_SETFL, O_NONBLOCK); 618 619 t0 = get_usecs(); 620 coldboot(fd, "/sys/class"); 621 coldboot(fd, "/sys/block"); 622 coldboot(fd, "/sys/devices"); 623 t1 = get_usecs(); 624 625 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0))); 626 627 return fd; 628} 629