builtins.c revision ef3f7fa32b451bd6f180c4c1586a98cee41aa296
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 { "defaults", 0 }, 343 { 0, 0 }, 344}; 345 346#define DATA_MNT_POINT "/data" 347 348/* mount <type> <device> <path> <flags ...> <options> */ 349int do_mount(int nargs, char **args) 350{ 351 char tmp[64]; 352 char *source, *target, *system; 353 char *options = NULL; 354 unsigned flags = 0; 355 int n, i; 356 int wait = 0; 357 358 for (n = 4; n < nargs; n++) { 359 for (i = 0; mount_flags[i].name; i++) { 360 if (!strcmp(args[n], mount_flags[i].name)) { 361 flags |= mount_flags[i].flag; 362 break; 363 } 364 } 365 366 if (!mount_flags[i].name) { 367 if (!strcmp(args[n], "wait")) 368 wait = 1; 369 /* if our last argument isn't a flag, wolf it up as an option string */ 370 else if (n + 1 == nargs) 371 options = args[n]; 372 } 373 } 374 375 system = args[1]; 376 source = args[2]; 377 target = args[3]; 378 379 if (!strncmp(source, "mtd@", 4)) { 380 n = mtd_name_to_number(source + 4); 381 if (n < 0) { 382 return -1; 383 } 384 385 sprintf(tmp, "/dev/block/mtdblock%d", n); 386 387 if (wait) 388 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); 389 if (mount(tmp, target, system, flags, options) < 0) { 390 return -1; 391 } 392 393 goto exit_success; 394 } else if (!strncmp(source, "loop@", 5)) { 395 int mode, loop, fd; 396 struct loop_info info; 397 398 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; 399 fd = open(source + 5, mode); 400 if (fd < 0) { 401 return -1; 402 } 403 404 for (n = 0; ; n++) { 405 sprintf(tmp, "/dev/block/loop%d", n); 406 loop = open(tmp, mode); 407 if (loop < 0) { 408 return -1; 409 } 410 411 /* if it is a blank loop device */ 412 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { 413 /* if it becomes our loop device */ 414 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { 415 close(fd); 416 417 if (mount(tmp, target, system, flags, options) < 0) { 418 ioctl(loop, LOOP_CLR_FD, 0); 419 close(loop); 420 return -1; 421 } 422 423 close(loop); 424 goto exit_success; 425 } 426 } 427 428 close(loop); 429 } 430 431 close(fd); 432 ERROR("out of loopback devices"); 433 return -1; 434 } else { 435 if (wait) 436 wait_for_file(source, COMMAND_RETRY_TIMEOUT); 437 if (mount(source, target, system, flags, options) < 0) { 438 return -1; 439 } 440 441 } 442 443exit_success: 444 return 0; 445 446} 447 448int do_mount_all(int nargs, char **args) 449{ 450 pid_t pid; 451 int ret = -1; 452 int child_ret = -1; 453 int status; 454 const char *prop; 455 456 if (nargs != 2) { 457 return -1; 458 } 459 460 /* 461 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and 462 * do the call in the child to provide protection to the main init 463 * process if anything goes wrong (crash or memory leak), and wait for 464 * the child to finish in the parent. 465 */ 466 pid = fork(); 467 if (pid > 0) { 468 /* Parent. Wait for the child to return */ 469 waitpid(pid, &status, 0); 470 if (WIFEXITED(status)) { 471 ret = WEXITSTATUS(status); 472 } else { 473 ret = -1; 474 } 475 } else if (pid == 0) { 476 /* child, call fs_mgr_mount_all() */ 477 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */ 478 child_ret = fs_mgr_mount_all(args[1]); 479 if (child_ret == -1) { 480 ERROR("fs_mgr_mount_all returned an error\n"); 481 } 482 exit(child_ret); 483 } else { 484 /* fork failed, return an error */ 485 return -1; 486 } 487 488 /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */ 489 if (ret == 1) { 490 property_set("ro.crypto.state", "encrypted"); 491 property_set("vold.decrypt", "1"); 492 } else if (ret == 0) { 493 property_set("ro.crypto.state", "unencrypted"); 494 /* If fs_mgr determined this is an unencrypted device, then trigger 495 * that action. 496 */ 497 action_for_each_trigger("nonencrypted", action_add_queue_tail); 498 } 499 500 return ret; 501} 502 503int do_setcon(int nargs, char **args) { 504#ifdef HAVE_SELINUX 505 if (is_selinux_enabled() <= 0) 506 return 0; 507 if (setcon(args[1]) < 0) { 508 return -errno; 509 } 510#endif 511 return 0; 512} 513 514int do_setenforce(int nargs, char **args) { 515#ifdef HAVE_SELINUX 516 if (is_selinux_enabled() <= 0) 517 return 0; 518 if (security_setenforce(atoi(args[1])) < 0) { 519 return -errno; 520 } 521#endif 522 return 0; 523} 524 525int do_setkey(int nargs, char **args) 526{ 527 struct kbentry kbe; 528 kbe.kb_table = strtoul(args[1], 0, 0); 529 kbe.kb_index = strtoul(args[2], 0, 0); 530 kbe.kb_value = strtoul(args[3], 0, 0); 531 return setkey(&kbe); 532} 533 534int do_setprop(int nargs, char **args) 535{ 536 const char *name = args[1]; 537 const char *value = args[2]; 538 char prop_val[PROP_VALUE_MAX]; 539 int ret; 540 541 ret = expand_props(prop_val, value, sizeof(prop_val)); 542 if (ret) { 543 ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); 544 return -EINVAL; 545 } 546 property_set(name, prop_val); 547 return 0; 548} 549 550int do_setrlimit(int nargs, char **args) 551{ 552 struct rlimit limit; 553 int resource; 554 resource = atoi(args[1]); 555 limit.rlim_cur = atoi(args[2]); 556 limit.rlim_max = atoi(args[3]); 557 return setrlimit(resource, &limit); 558} 559 560int do_start(int nargs, char **args) 561{ 562 struct service *svc; 563 svc = service_find_by_name(args[1]); 564 if (svc) { 565 service_start(svc, NULL); 566 } 567 return 0; 568} 569 570int do_stop(int nargs, char **args) 571{ 572 struct service *svc; 573 svc = service_find_by_name(args[1]); 574 if (svc) { 575 service_stop(svc); 576 } 577 return 0; 578} 579 580int do_restart(int nargs, char **args) 581{ 582 struct service *svc; 583 svc = service_find_by_name(args[1]); 584 if (svc) { 585 service_stop(svc); 586 service_start(svc, NULL); 587 } 588 return 0; 589} 590 591int do_trigger(int nargs, char **args) 592{ 593 action_for_each_trigger(args[1], action_add_queue_tail); 594 return 0; 595} 596 597int do_symlink(int nargs, char **args) 598{ 599 return symlink(args[1], args[2]); 600} 601 602int do_rm(int nargs, char **args) 603{ 604 return unlink(args[1]); 605} 606 607int do_rmdir(int nargs, char **args) 608{ 609 return rmdir(args[1]); 610} 611 612int do_sysclktz(int nargs, char **args) 613{ 614 struct timezone tz; 615 616 if (nargs != 2) 617 return -1; 618 619 memset(&tz, 0, sizeof(tz)); 620 tz.tz_minuteswest = atoi(args[1]); 621 if (settimeofday(NULL, &tz)) 622 return -1; 623 return 0; 624} 625 626int do_write(int nargs, char **args) 627{ 628 const char *path = args[1]; 629 const char *value = args[2]; 630 char prop_val[PROP_VALUE_MAX]; 631 int ret; 632 633 ret = expand_props(prop_val, value, sizeof(prop_val)); 634 if (ret) { 635 ERROR("cannot expand '%s' while writing to '%s'\n", value, path); 636 return -EINVAL; 637 } 638 return write_file(path, prop_val); 639} 640 641int do_copy(int nargs, char **args) 642{ 643 char *buffer = NULL; 644 int rc = 0; 645 int fd1 = -1, fd2 = -1; 646 struct stat info; 647 int brtw, brtr; 648 char *p; 649 650 if (nargs != 3) 651 return -1; 652 653 if (stat(args[1], &info) < 0) 654 return -1; 655 656 if ((fd1 = open(args[1], O_RDONLY)) < 0) 657 goto out_err; 658 659 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) 660 goto out_err; 661 662 if (!(buffer = malloc(info.st_size))) 663 goto out_err; 664 665 p = buffer; 666 brtr = info.st_size; 667 while(brtr) { 668 rc = read(fd1, p, brtr); 669 if (rc < 0) 670 goto out_err; 671 if (rc == 0) 672 break; 673 p += rc; 674 brtr -= rc; 675 } 676 677 p = buffer; 678 brtw = info.st_size; 679 while(brtw) { 680 rc = write(fd2, p, brtw); 681 if (rc < 0) 682 goto out_err; 683 if (rc == 0) 684 break; 685 p += rc; 686 brtw -= rc; 687 } 688 689 rc = 0; 690 goto out; 691out_err: 692 rc = -1; 693out: 694 if (buffer) 695 free(buffer); 696 if (fd1 >= 0) 697 close(fd1); 698 if (fd2 >= 0) 699 close(fd2); 700 return rc; 701} 702 703int do_chown(int nargs, char **args) { 704 /* GID is optional. */ 705 if (nargs == 3) { 706 if (_chown(args[2], decode_uid(args[1]), -1) < 0) 707 return -errno; 708 } else if (nargs == 4) { 709 if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0) 710 return -errno; 711 } else { 712 return -1; 713 } 714 return 0; 715} 716 717static mode_t get_mode(const char *s) { 718 mode_t mode = 0; 719 while (*s) { 720 if (*s >= '0' && *s <= '7') { 721 mode = (mode<<3) | (*s-'0'); 722 } else { 723 return -1; 724 } 725 s++; 726 } 727 return mode; 728} 729 730int do_chmod(int nargs, char **args) { 731 mode_t mode = get_mode(args[1]); 732 if (_chmod(args[2], mode) < 0) { 733 return -errno; 734 } 735 return 0; 736} 737 738int do_restorecon(int nargs, char **args) { 739 int i; 740 741 for (i = 1; i < nargs; i++) { 742 if (restorecon(args[i]) < 0) 743 return -errno; 744 } 745 return 0; 746} 747 748int do_setsebool(int nargs, char **args) { 749#ifdef HAVE_SELINUX 750 SELboolean *b = alloca(nargs * sizeof(SELboolean)); 751 char *v; 752 int i; 753 754 if (is_selinux_enabled() <= 0) 755 return 0; 756 757 for (i = 1; i < nargs; i++) { 758 char *name = args[i]; 759 v = strchr(name, '='); 760 if (!v) { 761 ERROR("setsebool: argument %s had no =\n", name); 762 return -EINVAL; 763 } 764 *v++ = 0; 765 b[i-1].name = name; 766 if (!strcmp(v, "1") || !strcasecmp(v, "true") || !strcasecmp(v, "on")) 767 b[i-1].value = 1; 768 else if (!strcmp(v, "0") || !strcasecmp(v, "false") || !strcasecmp(v, "off")) 769 b[i-1].value = 0; 770 else { 771 ERROR("setsebool: invalid value %s\n", v); 772 return -EINVAL; 773 } 774 } 775 776 if (security_set_boolean_list(nargs - 1, b, 0) < 0) 777 return -errno; 778#endif 779 return 0; 780} 781 782int do_loglevel(int nargs, char **args) { 783 if (nargs == 2) { 784 klog_set_level(atoi(args[1])); 785 return 0; 786 } 787 return -1; 788} 789 790int do_load_persist_props(int nargs, char **args) { 791 if (nargs == 1) { 792 load_persist_props(); 793 return 0; 794 } 795 return -1; 796} 797 798int do_wait(int nargs, char **args) 799{ 800 if (nargs == 2) { 801 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); 802 } 803 return -1; 804} 805