builtins.c revision e50ac5f7771872331df70251d23d6bd8155da4a7
1/* 2 * Copyright (C) 2008 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 <sys/types.h> 18#include <sys/stat.h> 19#include <fcntl.h> 20#include <unistd.h> 21#include <string.h> 22#include <stdio.h> 23#include <linux/kd.h> 24#include <errno.h> 25#include <sys/socket.h> 26#include <netinet/in.h> 27#include <linux/if.h> 28#include <arpa/inet.h> 29#include <stdlib.h> 30#include <sys/mount.h> 31#include <sys/resource.h> 32#include <sys/wait.h> 33#include <linux/loop.h> 34#include <cutils/partition_utils.h> 35#include <sys/system_properties.h> 36#include <fs_mgr.h> 37 38#ifdef HAVE_SELINUX 39#include <selinux/selinux.h> 40#include <selinux/label.h> 41#endif 42 43#include "init.h" 44#include "keywords.h" 45#include "property_service.h" 46#include "devices.h" 47#include "init_parser.h" 48#include "util.h" 49#include "log.h" 50 51#include <private/android_filesystem_config.h> 52 53void add_environment(const char *name, const char *value); 54 55extern int init_module(void *, unsigned long, const char *); 56 57static int write_file(const char *path, const char *value) 58{ 59 int fd, ret, len; 60 61 fd = open(path, O_WRONLY|O_CREAT, 0622); 62 63 if (fd < 0) 64 return -errno; 65 66 len = strlen(value); 67 68 do { 69 ret = write(fd, value, len); 70 } while (ret < 0 && errno == EINTR); 71 72 close(fd); 73 if (ret < 0) { 74 return -errno; 75 } else { 76 return 0; 77 } 78} 79 80static int _open(const char *path) 81{ 82 int fd; 83 84 fd = open(path, O_RDONLY | O_NOFOLLOW); 85 if (fd < 0) 86 fd = open(path, O_WRONLY | O_NOFOLLOW); 87 88 return fd; 89} 90 91static int _chown(const char *path, unsigned int uid, unsigned int gid) 92{ 93 int fd; 94 int ret; 95 96 fd = _open(path); 97 if (fd < 0) { 98 return -1; 99 } 100 101 ret = fchown(fd, uid, gid); 102 if (ret < 0) { 103 int errno_copy = errno; 104 close(fd); 105 errno = errno_copy; 106 return -1; 107 } 108 109 close(fd); 110 111 return 0; 112} 113 114static int _chmod(const char *path, mode_t mode) 115{ 116 int fd; 117 int ret; 118 119 fd = _open(path); 120 if (fd < 0) { 121 return -1; 122 } 123 124 ret = fchmod(fd, mode); 125 if (ret < 0) { 126 int errno_copy = errno; 127 close(fd); 128 errno = errno_copy; 129 return -1; 130 } 131 132 close(fd); 133 134 return 0; 135} 136 137static int insmod(const char *filename, char *options) 138{ 139 void *module; 140 unsigned size; 141 int ret; 142 143 module = read_file(filename, &size); 144 if (!module) 145 return -1; 146 147 ret = init_module(module, size, options); 148 149 free(module); 150 151 return ret; 152} 153 154static int setkey(struct kbentry *kbe) 155{ 156 int fd, ret; 157 158 fd = open("/dev/tty0", O_RDWR | O_SYNC); 159 if (fd < 0) 160 return -1; 161 162 ret = ioctl(fd, KDSKBENT, kbe); 163 164 close(fd); 165 return ret; 166} 167 168static int __ifupdown(const char *interface, int up) 169{ 170 struct ifreq ifr; 171 int s, ret; 172 173 strlcpy(ifr.ifr_name, interface, IFNAMSIZ); 174 175 s = socket(AF_INET, SOCK_DGRAM, 0); 176 if (s < 0) 177 return -1; 178 179 ret = ioctl(s, SIOCGIFFLAGS, &ifr); 180 if (ret < 0) { 181 goto done; 182 } 183 184 if (up) 185 ifr.ifr_flags |= IFF_UP; 186 else 187 ifr.ifr_flags &= ~IFF_UP; 188 189 ret = ioctl(s, SIOCSIFFLAGS, &ifr); 190 191done: 192 close(s); 193 return ret; 194} 195 196static void service_start_if_not_disabled(struct service *svc) 197{ 198 if (!(svc->flags & SVC_DISABLED)) { 199 service_start(svc, NULL); 200 } 201} 202 203int do_chdir(int nargs, char **args) 204{ 205 chdir(args[1]); 206 return 0; 207} 208 209int do_chroot(int nargs, char **args) 210{ 211 chroot(args[1]); 212 return 0; 213} 214 215int do_class_start(int nargs, char **args) 216{ 217 /* Starting a class does not start services 218 * which are explicitly disabled. They must 219 * be started individually. 220 */ 221 service_for_each_class(args[1], service_start_if_not_disabled); 222 return 0; 223} 224 225int do_class_stop(int nargs, char **args) 226{ 227 service_for_each_class(args[1], service_stop); 228 return 0; 229} 230 231int do_class_reset(int nargs, char **args) 232{ 233 service_for_each_class(args[1], service_reset); 234 return 0; 235} 236 237int do_domainname(int nargs, char **args) 238{ 239 return write_file("/proc/sys/kernel/domainname", args[1]); 240} 241 242int do_exec(int nargs, char **args) 243{ 244 return -1; 245} 246 247int do_export(int nargs, char **args) 248{ 249 add_environment(args[1], args[2]); 250 return 0; 251} 252 253int do_hostname(int nargs, char **args) 254{ 255 return write_file("/proc/sys/kernel/hostname", args[1]); 256} 257 258int do_ifup(int nargs, char **args) 259{ 260 return __ifupdown(args[1], 1); 261} 262 263 264static int do_insmod_inner(int nargs, char **args, int opt_len) 265{ 266 char options[opt_len + 1]; 267 int i; 268 269 options[0] = '\0'; 270 if (nargs > 2) { 271 strcpy(options, args[2]); 272 for (i = 3; i < nargs; ++i) { 273 strcat(options, " "); 274 strcat(options, args[i]); 275 } 276 } 277 278 return insmod(args[1], options); 279} 280 281int do_insmod(int nargs, char **args) 282{ 283 int i; 284 int size = 0; 285 286 if (nargs > 2) { 287 for (i = 2; i < nargs; ++i) 288 size += strlen(args[i]) + 1; 289 } 290 291 return do_insmod_inner(nargs, args, size); 292} 293 294int do_mkdir(int nargs, char **args) 295{ 296 mode_t mode = 0755; 297 int ret; 298 299 /* mkdir <path> [mode] [owner] [group] */ 300 301 if (nargs >= 3) { 302 mode = strtoul(args[2], 0, 8); 303 } 304 305 ret = make_dir(args[1], mode); 306 /* chmod in case the directory already exists */ 307 if (ret == -1 && errno == EEXIST) { 308 ret = _chmod(args[1], mode); 309 } 310 if (ret == -1) { 311 return -errno; 312 } 313 314 if (nargs >= 4) { 315 uid_t uid = decode_uid(args[3]); 316 gid_t gid = -1; 317 318 if (nargs == 5) { 319 gid = decode_uid(args[4]); 320 } 321 322 if (_chown(args[1], uid, gid) < 0) { 323 return -errno; 324 } 325 } 326 327 return 0; 328} 329 330static struct { 331 const char *name; 332 unsigned flag; 333} mount_flags[] = { 334 { "noatime", MS_NOATIME }, 335 { "noexec", MS_NOEXEC }, 336 { "nosuid", MS_NOSUID }, 337 { "nodev", MS_NODEV }, 338 { "nodiratime", MS_NODIRATIME }, 339 { "ro", MS_RDONLY }, 340 { "rw", 0 }, 341 { "remount", MS_REMOUNT }, 342 { "bind", MS_BIND }, 343 { "rec", MS_REC }, 344 { "unbindable", MS_UNBINDABLE }, 345 { "private", MS_PRIVATE }, 346 { "slave", MS_SLAVE }, 347 { "shared", MS_SHARED }, 348 { "defaults", 0 }, 349 { 0, 0 }, 350}; 351 352#define DATA_MNT_POINT "/data" 353 354/* mount <type> <device> <path> <flags ...> <options> */ 355int do_mount(int nargs, char **args) 356{ 357 char tmp[64]; 358 char *source, *target, *system; 359 char *options = NULL; 360 unsigned flags = 0; 361 int n, i; 362 int wait = 0; 363 364 for (n = 4; n < nargs; n++) { 365 for (i = 0; mount_flags[i].name; i++) { 366 if (!strcmp(args[n], mount_flags[i].name)) { 367 flags |= mount_flags[i].flag; 368 break; 369 } 370 } 371 372 if (!mount_flags[i].name) { 373 if (!strcmp(args[n], "wait")) 374 wait = 1; 375 /* if our last argument isn't a flag, wolf it up as an option string */ 376 else if (n + 1 == nargs) 377 options = args[n]; 378 } 379 } 380 381 system = args[1]; 382 source = args[2]; 383 target = args[3]; 384 385 if (!strncmp(source, "mtd@", 4)) { 386 n = mtd_name_to_number(source + 4); 387 if (n < 0) { 388 return -1; 389 } 390 391 sprintf(tmp, "/dev/block/mtdblock%d", n); 392 393 if (wait) 394 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); 395 if (mount(tmp, target, system, flags, options) < 0) { 396 return -1; 397 } 398 399 goto exit_success; 400 } else if (!strncmp(source, "loop@", 5)) { 401 int mode, loop, fd; 402 struct loop_info info; 403 404 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; 405 fd = open(source + 5, mode); 406 if (fd < 0) { 407 return -1; 408 } 409 410 for (n = 0; ; n++) { 411 sprintf(tmp, "/dev/block/loop%d", n); 412 loop = open(tmp, mode); 413 if (loop < 0) { 414 return -1; 415 } 416 417 /* if it is a blank loop device */ 418 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { 419 /* if it becomes our loop device */ 420 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { 421 close(fd); 422 423 if (mount(tmp, target, system, flags, options) < 0) { 424 ioctl(loop, LOOP_CLR_FD, 0); 425 close(loop); 426 return -1; 427 } 428 429 close(loop); 430 goto exit_success; 431 } 432 } 433 434 close(loop); 435 } 436 437 close(fd); 438 ERROR("out of loopback devices"); 439 return -1; 440 } else { 441 if (wait) 442 wait_for_file(source, COMMAND_RETRY_TIMEOUT); 443 if (mount(source, target, system, flags, options) < 0) { 444 return -1; 445 } 446 447 } 448 449exit_success: 450 return 0; 451 452} 453 454int do_mount_all(int nargs, char **args) 455{ 456 pid_t pid; 457 int ret = -1; 458 int child_ret = -1; 459 int status; 460 const char *prop; 461 462 if (nargs != 2) { 463 return -1; 464 } 465 466 /* 467 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and 468 * do the call in the child to provide protection to the main init 469 * process if anything goes wrong (crash or memory leak), and wait for 470 * the child to finish in the parent. 471 */ 472 pid = fork(); 473 if (pid > 0) { 474 /* Parent. Wait for the child to return */ 475 waitpid(pid, &status, 0); 476 if (WIFEXITED(status)) { 477 ret = WEXITSTATUS(status); 478 } else { 479 ret = -1; 480 } 481 } else if (pid == 0) { 482 /* child, call fs_mgr_mount_all() */ 483 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */ 484 child_ret = fs_mgr_mount_all(args[1]); 485 if (child_ret == -1) { 486 ERROR("fs_mgr_mount_all returned an error\n"); 487 } 488 exit(child_ret); 489 } else { 490 /* fork failed, return an error */ 491 return -1; 492 } 493 494 /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */ 495 if (ret == 1) { 496 property_set("ro.crypto.state", "encrypted"); 497 property_set("vold.decrypt", "1"); 498 } else if (ret == 0) { 499 property_set("ro.crypto.state", "unencrypted"); 500 /* If fs_mgr determined this is an unencrypted device, then trigger 501 * that action. 502 */ 503 action_for_each_trigger("nonencrypted", action_add_queue_tail); 504 } 505 506 return ret; 507} 508 509int do_setcon(int nargs, char **args) { 510#ifdef HAVE_SELINUX 511 if (is_selinux_enabled() <= 0) 512 return 0; 513 if (setcon(args[1]) < 0) { 514 return -errno; 515 } 516#endif 517 return 0; 518} 519 520int do_setenforce(int nargs, char **args) { 521#ifdef HAVE_SELINUX 522 if (is_selinux_enabled() <= 0) 523 return 0; 524 if (security_setenforce(atoi(args[1])) < 0) { 525 return -errno; 526 } 527#endif 528 return 0; 529} 530 531int do_setkey(int nargs, char **args) 532{ 533 struct kbentry kbe; 534 kbe.kb_table = strtoul(args[1], 0, 0); 535 kbe.kb_index = strtoul(args[2], 0, 0); 536 kbe.kb_value = strtoul(args[3], 0, 0); 537 return setkey(&kbe); 538} 539 540int do_setprop(int nargs, char **args) 541{ 542 const char *name = args[1]; 543 const char *value = args[2]; 544 char prop_val[PROP_VALUE_MAX]; 545 int ret; 546 547 ret = expand_props(prop_val, value, sizeof(prop_val)); 548 if (ret) { 549 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); 550 return -EINVAL; 551 } 552 property_set(name, prop_val); 553 return 0; 554} 555 556int do_setrlimit(int nargs, char **args) 557{ 558 struct rlimit limit; 559 int resource; 560 resource = atoi(args[1]); 561 limit.rlim_cur = atoi(args[2]); 562 limit.rlim_max = atoi(args[3]); 563 return setrlimit(resource, &limit); 564} 565 566int do_start(int nargs, char **args) 567{ 568 struct service *svc; 569 svc = service_find_by_name(args[1]); 570 if (svc) { 571 service_start(svc, NULL); 572 } 573 return 0; 574} 575 576int do_stop(int nargs, char **args) 577{ 578 struct service *svc; 579 svc = service_find_by_name(args[1]); 580 if (svc) { 581 service_stop(svc); 582 } 583 return 0; 584} 585 586int do_restart(int nargs, char **args) 587{ 588 struct service *svc; 589 svc = service_find_by_name(args[1]); 590 if (svc) { 591 service_stop(svc); 592 service_start(svc, NULL); 593 } 594 return 0; 595} 596 597int do_trigger(int nargs, char **args) 598{ 599 action_for_each_trigger(args[1], action_add_queue_tail); 600 return 0; 601} 602 603int do_symlink(int nargs, char **args) 604{ 605 return symlink(args[1], args[2]); 606} 607 608int do_rm(int nargs, char **args) 609{ 610 return unlink(args[1]); 611} 612 613int do_rmdir(int nargs, char **args) 614{ 615 return rmdir(args[1]); 616} 617 618int do_sysclktz(int nargs, char **args) 619{ 620 struct timezone tz; 621 622 if (nargs != 2) 623 return -1; 624 625 memset(&tz, 0, sizeof(tz)); 626 tz.tz_minuteswest = atoi(args[1]); 627 if (settimeofday(NULL, &tz)) 628 return -1; 629 return 0; 630} 631 632int do_write(int nargs, char **args) 633{ 634 const char *path = args[1]; 635 const char *value = args[2]; 636 char prop_val[PROP_VALUE_MAX]; 637 int ret; 638 639 ret = expand_props(prop_val, value, sizeof(prop_val)); 640 if (ret) { 641 ERROR("cannot expand '%s' while writing to '%s'\n", value, path); 642 return -EINVAL; 643 } 644 return write_file(path, prop_val); 645} 646 647int do_copy(int nargs, char **args) 648{ 649 char *buffer = NULL; 650 int rc = 0; 651 int fd1 = -1, fd2 = -1; 652 struct stat info; 653 int brtw, brtr; 654 char *p; 655 656 if (nargs != 3) 657 return -1; 658 659 if (stat(args[1], &info) < 0) 660 return -1; 661 662 if ((fd1 = open(args[1], O_RDONLY)) < 0) 663 goto out_err; 664 665 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) 666 goto out_err; 667 668 if (!(buffer = malloc(info.st_size))) 669 goto out_err; 670 671 p = buffer; 672 brtr = info.st_size; 673 while(brtr) { 674 rc = read(fd1, p, brtr); 675 if (rc < 0) 676 goto out_err; 677 if (rc == 0) 678 break; 679 p += rc; 680 brtr -= rc; 681 } 682 683 p = buffer; 684 brtw = info.st_size; 685 while(brtw) { 686 rc = write(fd2, p, brtw); 687 if (rc < 0) 688 goto out_err; 689 if (rc == 0) 690 break; 691 p += rc; 692 brtw -= rc; 693 } 694 695 rc = 0; 696 goto out; 697out_err: 698 rc = -1; 699out: 700 if (buffer) 701 free(buffer); 702 if (fd1 >= 0) 703 close(fd1); 704 if (fd2 >= 0) 705 close(fd2); 706 return rc; 707} 708 709int do_chown(int nargs, char **args) { 710 /* GID is optional. */ 711 if (nargs == 3) { 712 if (_chown(args[2], decode_uid(args[1]), -1) < 0) 713 return -errno; 714 } else if (nargs == 4) { 715 if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0) 716 return -errno; 717 } else { 718 return -1; 719 } 720 return 0; 721} 722 723static mode_t get_mode(const char *s) { 724 mode_t mode = 0; 725 while (*s) { 726 if (*s >= '0' && *s <= '7') { 727 mode = (mode<<3) | (*s-'0'); 728 } else { 729 return -1; 730 } 731 s++; 732 } 733 return mode; 734} 735 736int do_chmod(int nargs, char **args) { 737 mode_t mode = get_mode(args[1]); 738 if (_chmod(args[2], mode) < 0) { 739 return -errno; 740 } 741 return 0; 742} 743 744int do_restorecon(int nargs, char **args) { 745 int i; 746 747 for (i = 1; i < nargs; i++) { 748 if (restorecon(args[i]) < 0) 749 return -errno; 750 } 751 return 0; 752} 753 754int do_setsebool(int nargs, char **args) { 755#ifdef HAVE_SELINUX 756 SELboolean *b = alloca(nargs * sizeof(SELboolean)); 757 char *v; 758 int i; 759 760 if (is_selinux_enabled() <= 0) 761 return 0; 762 763 for (i = 1; i < nargs; i++) { 764 char *name = args[i]; 765 v = strchr(name, '='); 766 if (!v) { 767 ERROR("setsebool: argument %s had no =\n", name); 768 return -EINVAL; 769 } 770 *v++ = 0; 771 b[i-1].name = name; 772 if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on")) 773 b[i-1].value = 1; 774 else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off")) 775 b[i-1].value = 0; 776 else { 777 ERROR("setsebool: invalid value %s\n", v); 778 return -EINVAL; 779 } 780 } 781 782 if (security_set_boolean_list(nargs - 1, b, 0) < 0) 783 return -errno; 784#endif 785 return 0; 786} 787 788int do_loglevel(int nargs, char **args) { 789 if (nargs == 2) { 790 klog_set_level(atoi(args[1])); 791 return 0; 792 } 793 return -1; 794} 795 796int do_load_persist_props(int nargs, char **args) { 797 if (nargs == 1) { 798 load_persist_props(); 799 return 0; 800 } 801 return -1; 802} 803 804int do_wait(int nargs, char **args) 805{ 806 if (nargs == 2) { 807 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); 808 } 809 return -1; 810} 811