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