builtins.c revision 752923c168009d03e9e00e590155fbd0a2880ccb
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 <linux/loop.h> 33 34#include "init.h" 35#include "keywords.h" 36#include "property_service.h" 37#include "devices.h" 38#include "init_parser.h" 39#include "util.h" 40#include "log.h" 41 42#include <private/android_filesystem_config.h> 43 44void add_environment(const char *name, const char *value); 45 46extern int init_module(void *, unsigned long, const char *); 47 48static int write_file(const char *path, const char *value) 49{ 50 int fd, ret, len; 51 52 fd = open(path, O_WRONLY|O_CREAT, 0622); 53 54 if (fd < 0) 55 return -errno; 56 57 len = strlen(value); 58 59 do { 60 ret = write(fd, value, len); 61 } while (ret < 0 && errno == EINTR); 62 63 close(fd); 64 if (ret < 0) { 65 return -errno; 66 } else { 67 return 0; 68 } 69} 70 71static int insmod(const char *filename, char *options) 72{ 73 void *module; 74 unsigned size; 75 int ret; 76 77 module = read_file(filename, &size); 78 if (!module) 79 return -1; 80 81 ret = init_module(module, size, options); 82 83 free(module); 84 85 return ret; 86} 87 88static int setkey(struct kbentry *kbe) 89{ 90 int fd, ret; 91 92 fd = open("/dev/tty0", O_RDWR | O_SYNC); 93 if (fd < 0) 94 return -1; 95 96 ret = ioctl(fd, KDSKBENT, kbe); 97 98 close(fd); 99 return ret; 100} 101 102static int __ifupdown(const char *interface, int up) 103{ 104 struct ifreq ifr; 105 int s, ret; 106 107 strlcpy(ifr.ifr_name, interface, IFNAMSIZ); 108 109 s = socket(AF_INET, SOCK_DGRAM, 0); 110 if (s < 0) 111 return -1; 112 113 ret = ioctl(s, SIOCGIFFLAGS, &ifr); 114 if (ret < 0) { 115 goto done; 116 } 117 118 if (up) 119 ifr.ifr_flags |= IFF_UP; 120 else 121 ifr.ifr_flags &= ~IFF_UP; 122 123 ret = ioctl(s, SIOCSIFFLAGS, &ifr); 124 125done: 126 close(s); 127 return ret; 128} 129 130static void service_start_if_not_disabled(struct service *svc) 131{ 132 if (!(svc->flags & SVC_DISABLED)) { 133 service_start(svc, NULL); 134 } 135} 136 137int do_chdir(int nargs, char **args) 138{ 139 chdir(args[1]); 140 return 0; 141} 142 143int do_chroot(int nargs, char **args) 144{ 145 chroot(args[1]); 146 return 0; 147} 148 149int do_class_start(int nargs, char **args) 150{ 151 /* Starting a class does not start services 152 * which are explicitly disabled. They must 153 * be started individually. 154 */ 155 service_for_each_class(args[1], service_start_if_not_disabled); 156 return 0; 157} 158 159int do_class_stop(int nargs, char **args) 160{ 161 service_for_each_class(args[1], service_stop); 162 return 0; 163} 164 165int do_class_reset(int nargs, char **args) 166{ 167 service_for_each_class(args[1], service_reset); 168 return 0; 169} 170 171int do_domainname(int nargs, char **args) 172{ 173 return write_file("/proc/sys/kernel/domainname", args[1]); 174} 175 176int do_exec(int nargs, char **args) 177{ 178 return -1; 179} 180 181int do_export(int nargs, char **args) 182{ 183 add_environment(args[1], args[2]); 184 return 0; 185} 186 187int do_hostname(int nargs, char **args) 188{ 189 return write_file("/proc/sys/kernel/hostname", args[1]); 190} 191 192int do_ifup(int nargs, char **args) 193{ 194 return __ifupdown(args[1], 1); 195} 196 197 198static int do_insmod_inner(int nargs, char **args, int opt_len) 199{ 200 char options[opt_len + 1]; 201 int i; 202 203 options[0] = '\0'; 204 if (nargs > 2) { 205 strcpy(options, args[2]); 206 for (i = 3; i < nargs; ++i) { 207 strcat(options, " "); 208 strcat(options, args[i]); 209 } 210 } 211 212 return insmod(args[1], options); 213} 214 215int do_insmod(int nargs, char **args) 216{ 217 int i; 218 int size = 0; 219 220 if (nargs > 2) { 221 for (i = 2; i < nargs; ++i) 222 size += strlen(args[i]) + 1; 223 } 224 225 return do_insmod_inner(nargs, args, size); 226} 227 228int do_import(int nargs, char **args) 229{ 230 return init_parse_config_file(args[1]); 231} 232 233int do_mkdir(int nargs, char **args) 234{ 235 mode_t mode = 0755; 236 237 /* mkdir <path> [mode] [owner] [group] */ 238 239 if (nargs >= 3) { 240 mode = strtoul(args[2], 0, 8); 241 } 242 243 if (mkdir(args[1], mode)) { 244 return -errno; 245 } 246 247 if (nargs >= 4) { 248 uid_t uid = decode_uid(args[3]); 249 gid_t gid = -1; 250 251 if (nargs == 5) { 252 gid = decode_uid(args[4]); 253 } 254 255 if (chown(args[1], uid, gid)) { 256 return -errno; 257 } 258 } 259 260 return 0; 261} 262 263static struct { 264 const char *name; 265 unsigned flag; 266} mount_flags[] = { 267 { "noatime", MS_NOATIME }, 268 { "nosuid", MS_NOSUID }, 269 { "nodev", MS_NODEV }, 270 { "nodiratime", MS_NODIRATIME }, 271 { "ro", MS_RDONLY }, 272 { "rw", 0 }, 273 { "remount", MS_REMOUNT }, 274 { "defaults", 0 }, 275 { 0, 0 }, 276}; 277 278#define DATA_MNT_POINT "/data" 279 280/* mount <type> <device> <path> <flags ...> <options> */ 281int do_mount(int nargs, char **args) 282{ 283 char tmp[64]; 284 char *source, *target, *system; 285 char *options = NULL; 286 unsigned flags = 0; 287 int n, i; 288 int wait = 0; 289 290 for (n = 4; n < nargs; n++) { 291 for (i = 0; mount_flags[i].name; i++) { 292 if (!strcmp(args[n], mount_flags[i].name)) { 293 flags |= mount_flags[i].flag; 294 break; 295 } 296 } 297 298 if (!mount_flags[i].name) { 299 if (!strcmp(args[n], "wait")) 300 wait = 1; 301 /* if our last argument isn't a flag, wolf it up as an option string */ 302 else if (n + 1 == nargs) 303 options = args[n]; 304 } 305 } 306 307 system = args[1]; 308 source = args[2]; 309 target = args[3]; 310 311 if (!strncmp(source, "mtd@", 4)) { 312 n = mtd_name_to_number(source + 4); 313 if (n < 0) { 314 return -1; 315 } 316 317 sprintf(tmp, "/dev/block/mtdblock%d", n); 318 319 if (wait) 320 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); 321 if (mount(tmp, target, system, flags, options) < 0) { 322 return -1; 323 } 324 325 return 0; 326 } else if (!strncmp(source, "loop@", 5)) { 327 int mode, loop, fd; 328 struct loop_info info; 329 330 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; 331 fd = open(source + 5, mode); 332 if (fd < 0) { 333 return -1; 334 } 335 336 for (n = 0; ; n++) { 337 sprintf(tmp, "/dev/block/loop%d", n); 338 loop = open(tmp, mode); 339 if (loop < 0) { 340 return -1; 341 } 342 343 /* if it is a blank loop device */ 344 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { 345 /* if it becomes our loop device */ 346 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { 347 close(fd); 348 349 if (mount(tmp, target, system, flags, options) < 0) { 350 ioctl(loop, LOOP_CLR_FD, 0); 351 close(loop); 352 return -1; 353 } 354 355 close(loop); 356 return 0; 357 } 358 } 359 360 close(loop); 361 } 362 363 close(fd); 364 ERROR("out of loopback devices"); 365 return -1; 366 } else { 367 if (wait) 368 wait_for_file(source, COMMAND_RETRY_TIMEOUT); 369 if (mount(source, target, system, flags, options) < 0) { 370 /* If this fails, it may be an encrypted filesystem. 371 * We only support encrypting /data. Check 372 * if we're trying to mount it, and if so, 373 * assume it's encrypted, mount a tmpfs instead. 374 * Then save the orig mount parms in properties 375 * for vold to query when it mounts the real 376 * encrypted /data. 377 */ 378 if (!strcmp(target, DATA_MNT_POINT)) { 379 const char *tmpfs_options; 380 381 tmpfs_options = property_get("ro.crypto.tmpfs_options"); 382 383 if (mount("tmpfs", target, "tmpfs", MS_NOATIME | MS_NOSUID | MS_NODEV, 384 tmpfs_options) < 0) { 385 return -1; 386 } 387 388 /* Set the property that triggers the framework to do a minimal 389 * startup and ask the user for a password 390 */ 391 property_set("vold.decrypt", "1"); 392 } else { 393 return -1; 394 } 395 } else { 396 if (!strcmp(target, DATA_MNT_POINT)) { 397 /* We succeeded in mounting /data, so it's not encrypted */ 398 action_for_each_trigger("nonencrypted", action_add_queue_tail); 399 } 400 } 401 402 if (!strcmp(target, DATA_MNT_POINT)) { 403 char fs_flags[32]; 404 405 /* Save the original mount options */ 406 property_set("ro.crypto.fs_type", system); 407 property_set("ro.crypto.fs_real_blkdev", source); 408 property_set("ro.crypto.fs_mnt_point", target); 409 if (options) { 410 property_set("ro.crypto.fs_options", options); 411 } 412 snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags); 413 property_set("ro.crypto.fs_flags", fs_flags); 414 } 415 return 0; 416 } 417} 418 419int do_setkey(int nargs, char **args) 420{ 421 struct kbentry kbe; 422 kbe.kb_table = strtoul(args[1], 0, 0); 423 kbe.kb_index = strtoul(args[2], 0, 0); 424 kbe.kb_value = strtoul(args[3], 0, 0); 425 return setkey(&kbe); 426} 427 428int do_setprop(int nargs, char **args) 429{ 430 property_set(args[1], args[2]); 431 return 0; 432} 433 434int do_setrlimit(int nargs, char **args) 435{ 436 struct rlimit limit; 437 int resource; 438 resource = atoi(args[1]); 439 limit.rlim_cur = atoi(args[2]); 440 limit.rlim_max = atoi(args[3]); 441 return setrlimit(resource, &limit); 442} 443 444int do_start(int nargs, char **args) 445{ 446 struct service *svc; 447 svc = service_find_by_name(args[1]); 448 if (svc) { 449 service_start(svc, NULL); 450 } 451 return 0; 452} 453 454int do_stop(int nargs, char **args) 455{ 456 struct service *svc; 457 svc = service_find_by_name(args[1]); 458 if (svc) { 459 service_stop(svc); 460 } 461 return 0; 462} 463 464int do_restart(int nargs, char **args) 465{ 466 struct service *svc; 467 svc = service_find_by_name(args[1]); 468 if (svc) { 469 service_stop(svc); 470 service_start(svc, NULL); 471 } 472 return 0; 473} 474 475int do_trigger(int nargs, char **args) 476{ 477 action_for_each_trigger(args[1], action_add_queue_tail); 478 return 0; 479} 480 481int do_symlink(int nargs, char **args) 482{ 483 return symlink(args[1], args[2]); 484} 485 486int do_sysclktz(int nargs, char **args) 487{ 488 struct timezone tz; 489 490 if (nargs != 2) 491 return -1; 492 493 memset(&tz, 0, sizeof(tz)); 494 tz.tz_minuteswest = atoi(args[1]); 495 if (settimeofday(NULL, &tz)) 496 return -1; 497 return 0; 498} 499 500int do_write(int nargs, char **args) 501{ 502 return write_file(args[1], args[2]); 503} 504 505int do_copy(int nargs, char **args) 506{ 507 char *buffer = NULL; 508 int rc = 0; 509 int fd1 = -1, fd2 = -1; 510 struct stat info; 511 int brtw, brtr; 512 char *p; 513 514 if (nargs != 3) 515 return -1; 516 517 if (stat(args[1], &info) < 0) 518 return -1; 519 520 if ((fd1 = open(args[1], O_RDONLY)) < 0) 521 goto out_err; 522 523 if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) 524 goto out_err; 525 526 if (!(buffer = malloc(info.st_size))) 527 goto out_err; 528 529 p = buffer; 530 brtr = info.st_size; 531 while(brtr) { 532 rc = read(fd1, p, brtr); 533 if (rc < 0) 534 goto out_err; 535 if (rc == 0) 536 break; 537 p += rc; 538 brtr -= rc; 539 } 540 541 p = buffer; 542 brtw = info.st_size; 543 while(brtw) { 544 rc = write(fd2, p, brtw); 545 if (rc < 0) 546 goto out_err; 547 if (rc == 0) 548 break; 549 p += rc; 550 brtw -= rc; 551 } 552 553 rc = 0; 554 goto out; 555out_err: 556 rc = -1; 557out: 558 if (buffer) 559 free(buffer); 560 if (fd1 >= 0) 561 close(fd1); 562 if (fd2 >= 0) 563 close(fd2); 564 return rc; 565} 566 567int do_chown(int nargs, char **args) { 568 /* GID is optional. */ 569 if (nargs == 3) { 570 if (chown(args[2], decode_uid(args[1]), -1) < 0) 571 return -errno; 572 } else if (nargs == 4) { 573 if (chown(args[3], decode_uid(args[1]), decode_uid(args[2]))) 574 return -errno; 575 } else { 576 return -1; 577 } 578 return 0; 579} 580 581static mode_t get_mode(const char *s) { 582 mode_t mode = 0; 583 while (*s) { 584 if (*s >= '0' && *s <= '7') { 585 mode = (mode<<3) | (*s-'0'); 586 } else { 587 return -1; 588 } 589 s++; 590 } 591 return mode; 592} 593 594int do_chmod(int nargs, char **args) { 595 mode_t mode = get_mode(args[1]); 596 if (chmod(args[2], mode) < 0) { 597 return -errno; 598 } 599 return 0; 600} 601 602int do_loglevel(int nargs, char **args) { 603 if (nargs == 2) { 604 log_set_level(atoi(args[1])); 605 return 0; 606 } 607 return -1; 608} 609 610int do_wait(int nargs, char **args) 611{ 612 if (nargs == 2) { 613 return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); 614 } 615 return -1; 616} 617