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