The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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> |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 32 | #include <linux/loop.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | |
| 34 | #include "init.h" |
| 35 | #include "keywords.h" |
| 36 | #include "property_service.h" |
| 37 | #include "devices.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 38 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 39 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 40 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
| 42 | #include <private/android_filesystem_config.h> |
| 43 | |
| 44 | void add_environment(const char *name, const char *value); |
| 45 | |
| 46 | extern int init_module(void *, unsigned long, const char *); |
| 47 | |
| 48 | static 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) |
Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 55 | return -errno; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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) { |
Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 65 | return -errno; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | } else { |
| 67 | return 0; |
| 68 | } |
| 69 | } |
| 70 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 71 | static int insmod(const char *filename, char *options) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | { |
| 73 | void *module; |
| 74 | unsigned size; |
| 75 | int ret; |
| 76 | |
| 77 | module = read_file(filename, &size); |
| 78 | if (!module) |
| 79 | return -1; |
| 80 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 81 | ret = init_module(module, size, options); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
| 83 | free(module); |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static 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 | |
| 102 | static 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 | |
| 125 | done: |
| 126 | close(s); |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static void service_start_if_not_disabled(struct service *svc) |
| 131 | { |
| 132 | if (!(svc->flags & SVC_DISABLED)) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 133 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
Jay Freeman (saurik) | e7cb137 | 2008-11-17 06:41:10 +0000 | [diff] [blame] | 137 | int do_chdir(int nargs, char **args) |
| 138 | { |
| 139 | chdir(args[1]); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | int do_chroot(int nargs, char **args) |
| 144 | { |
| 145 | chroot(args[1]); |
| 146 | return 0; |
| 147 | } |
| 148 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 149 | int 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 | |
| 159 | int do_class_stop(int nargs, char **args) |
| 160 | { |
| 161 | service_for_each_class(args[1], service_stop); |
| 162 | return 0; |
| 163 | } |
| 164 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 165 | int do_class_reset(int nargs, char **args) |
| 166 | { |
| 167 | service_for_each_class(args[1], service_reset); |
| 168 | return 0; |
| 169 | } |
| 170 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | int do_domainname(int nargs, char **args) |
| 172 | { |
| 173 | return write_file("/proc/sys/kernel/domainname", args[1]); |
| 174 | } |
| 175 | |
| 176 | int do_exec(int nargs, char **args) |
| 177 | { |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | int do_export(int nargs, char **args) |
| 182 | { |
| 183 | add_environment(args[1], args[2]); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int do_hostname(int nargs, char **args) |
| 188 | { |
| 189 | return write_file("/proc/sys/kernel/hostname", args[1]); |
| 190 | } |
| 191 | |
| 192 | int do_ifup(int nargs, char **args) |
| 193 | { |
| 194 | return __ifupdown(args[1], 1); |
| 195 | } |
| 196 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 197 | |
| 198 | static 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 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | int do_insmod(int nargs, char **args) |
| 216 | { |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 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); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int do_import(int nargs, char **args) |
| 229 | { |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 230 | return init_parse_config_file(args[1]); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | int 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 | |
| 263 | static 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 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 278 | #define DATA_MNT_POINT "/data" |
| 279 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 280 | /* mount <type> <device> <path> <flags ...> <options> */ |
| 281 | int do_mount(int nargs, char **args) |
| 282 | { |
| 283 | char tmp[64]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 284 | char *source, *target, *system; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 285 | char *options = NULL; |
| 286 | unsigned flags = 0; |
| 287 | int n, i; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 288 | int wait = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 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 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 307 | system = args[1]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 308 | source = args[2]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 309 | target = args[3]; |
| 310 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 311 | if (!strncmp(source, "mtd@", 4)) { |
| 312 | n = mtd_name_to_number(source + 4); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 313 | if (n < 0) { |
| 314 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 315 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 316 | |
| 317 | sprintf(tmp, "/dev/block/mtdblock%d", n); |
| 318 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 319 | if (wait) |
| 320 | wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 321 | if (mount(tmp, target, system, flags, options) < 0) { |
| 322 | return -1; |
| 323 | } |
| 324 | |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 325 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 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); |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 356 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | close(loop); |
| 361 | } |
| 362 | |
| 363 | close(fd); |
| 364 | ERROR("out of loopback devices"); |
| 365 | return -1; |
| 366 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 367 | if (wait) |
| 368 | wait_for_file(source, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 369 | if (mount(source, target, system, flags, options) < 0) { |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 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 | */ |
Ken Sumrall | 4e84d3b | 2011-01-14 12:44:09 -0800 | [diff] [blame] | 391 | property_set("ro.crypto.state", "encrypted"); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 392 | property_set("vold.decrypt", "1"); |
| 393 | } else { |
| 394 | return -1; |
| 395 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 398 | if (!strcmp(target, DATA_MNT_POINT)) { |
| 399 | char fs_flags[32]; |
| 400 | |
| 401 | /* Save the original mount options */ |
| 402 | property_set("ro.crypto.fs_type", system); |
| 403 | property_set("ro.crypto.fs_real_blkdev", source); |
| 404 | property_set("ro.crypto.fs_mnt_point", target); |
| 405 | if (options) { |
| 406 | property_set("ro.crypto.fs_options", options); |
| 407 | } |
| 408 | snprintf(fs_flags, sizeof(fs_flags), "0x%8.8x", flags); |
| 409 | property_set("ro.crypto.fs_flags", fs_flags); |
| 410 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 411 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 412 | |
| 413 | exit_success: |
| 414 | /* If not running encrypted, then set the property saying we are |
| 415 | * unencrypted, and also trigger the action for a nonencrypted system. |
| 416 | */ |
| 417 | if (!strcmp(target, DATA_MNT_POINT)) { |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 418 | const char *prop; |
| 419 | |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 420 | prop = property_get("ro.crypto.state"); |
| 421 | if (! prop) { |
| 422 | prop = "notset"; |
| 423 | } |
| 424 | if (strcmp(prop, "encrypted")) { |
| 425 | property_set("ro.crypto.state", "unencrypted"); |
| 426 | action_for_each_trigger("nonencrypted", action_add_queue_tail); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return 0; |
| 431 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | int do_setkey(int nargs, char **args) |
| 435 | { |
| 436 | struct kbentry kbe; |
| 437 | kbe.kb_table = strtoul(args[1], 0, 0); |
| 438 | kbe.kb_index = strtoul(args[2], 0, 0); |
| 439 | kbe.kb_value = strtoul(args[3], 0, 0); |
| 440 | return setkey(&kbe); |
| 441 | } |
| 442 | |
| 443 | int do_setprop(int nargs, char **args) |
| 444 | { |
| 445 | property_set(args[1], args[2]); |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | int do_setrlimit(int nargs, char **args) |
| 450 | { |
| 451 | struct rlimit limit; |
| 452 | int resource; |
| 453 | resource = atoi(args[1]); |
| 454 | limit.rlim_cur = atoi(args[2]); |
| 455 | limit.rlim_max = atoi(args[3]); |
| 456 | return setrlimit(resource, &limit); |
| 457 | } |
| 458 | |
| 459 | int do_start(int nargs, char **args) |
| 460 | { |
| 461 | struct service *svc; |
| 462 | svc = service_find_by_name(args[1]); |
| 463 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 464 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | int do_stop(int nargs, char **args) |
| 470 | { |
| 471 | struct service *svc; |
| 472 | svc = service_find_by_name(args[1]); |
| 473 | if (svc) { |
| 474 | service_stop(svc); |
| 475 | } |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | int do_restart(int nargs, char **args) |
| 480 | { |
| 481 | struct service *svc; |
| 482 | svc = service_find_by_name(args[1]); |
| 483 | if (svc) { |
| 484 | service_stop(svc); |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 485 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 486 | } |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | int do_trigger(int nargs, char **args) |
| 491 | { |
Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 492 | action_for_each_trigger(args[1], action_add_queue_tail); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | int do_symlink(int nargs, char **args) |
| 497 | { |
| 498 | return symlink(args[1], args[2]); |
| 499 | } |
| 500 | |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 501 | int do_rm(int nargs, char **args) |
| 502 | { |
| 503 | return unlink(args[1]); |
| 504 | } |
| 505 | |
| 506 | int do_rmdir(int nargs, char **args) |
| 507 | { |
| 508 | return rmdir(args[1]); |
| 509 | } |
| 510 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 511 | int do_sysclktz(int nargs, char **args) |
| 512 | { |
| 513 | struct timezone tz; |
| 514 | |
| 515 | if (nargs != 2) |
| 516 | return -1; |
| 517 | |
| 518 | memset(&tz, 0, sizeof(tz)); |
| 519 | tz.tz_minuteswest = atoi(args[1]); |
| 520 | if (settimeofday(NULL, &tz)) |
| 521 | return -1; |
| 522 | return 0; |
| 523 | } |
| 524 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 525 | int do_write(int nargs, char **args) |
| 526 | { |
| 527 | return write_file(args[1], args[2]); |
| 528 | } |
| 529 | |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 530 | int do_copy(int nargs, char **args) |
| 531 | { |
| 532 | char *buffer = NULL; |
| 533 | int rc = 0; |
| 534 | int fd1 = -1, fd2 = -1; |
| 535 | struct stat info; |
| 536 | int brtw, brtr; |
| 537 | char *p; |
| 538 | |
| 539 | if (nargs != 3) |
| 540 | return -1; |
| 541 | |
| 542 | if (stat(args[1], &info) < 0) |
| 543 | return -1; |
| 544 | |
| 545 | if ((fd1 = open(args[1], O_RDONLY)) < 0) |
| 546 | goto out_err; |
| 547 | |
Tom Zhu | 4833d9f | 2009-09-28 19:53:12 -0500 | [diff] [blame] | 548 | if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 549 | goto out_err; |
| 550 | |
| 551 | if (!(buffer = malloc(info.st_size))) |
| 552 | goto out_err; |
| 553 | |
| 554 | p = buffer; |
| 555 | brtr = info.st_size; |
| 556 | while(brtr) { |
| 557 | rc = read(fd1, p, brtr); |
| 558 | if (rc < 0) |
| 559 | goto out_err; |
| 560 | if (rc == 0) |
| 561 | break; |
| 562 | p += rc; |
| 563 | brtr -= rc; |
| 564 | } |
| 565 | |
| 566 | p = buffer; |
| 567 | brtw = info.st_size; |
| 568 | while(brtw) { |
| 569 | rc = write(fd2, p, brtw); |
| 570 | if (rc < 0) |
| 571 | goto out_err; |
| 572 | if (rc == 0) |
| 573 | break; |
| 574 | p += rc; |
| 575 | brtw -= rc; |
| 576 | } |
| 577 | |
| 578 | rc = 0; |
| 579 | goto out; |
| 580 | out_err: |
| 581 | rc = -1; |
| 582 | out: |
| 583 | if (buffer) |
| 584 | free(buffer); |
| 585 | if (fd1 >= 0) |
| 586 | close(fd1); |
| 587 | if (fd2 >= 0) |
| 588 | close(fd2); |
| 589 | return rc; |
| 590 | } |
| 591 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 592 | int do_chown(int nargs, char **args) { |
| 593 | /* GID is optional. */ |
| 594 | if (nargs == 3) { |
| 595 | if (chown(args[2], decode_uid(args[1]), -1) < 0) |
| 596 | return -errno; |
| 597 | } else if (nargs == 4) { |
| 598 | if (chown(args[3], decode_uid(args[1]), decode_uid(args[2]))) |
| 599 | return -errno; |
| 600 | } else { |
| 601 | return -1; |
| 602 | } |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | static mode_t get_mode(const char *s) { |
| 607 | mode_t mode = 0; |
| 608 | while (*s) { |
| 609 | if (*s >= '0' && *s <= '7') { |
| 610 | mode = (mode<<3) | (*s-'0'); |
| 611 | } else { |
| 612 | return -1; |
| 613 | } |
| 614 | s++; |
| 615 | } |
| 616 | return mode; |
| 617 | } |
| 618 | |
| 619 | int do_chmod(int nargs, char **args) { |
| 620 | mode_t mode = get_mode(args[1]); |
| 621 | if (chmod(args[2], mode) < 0) { |
| 622 | return -errno; |
| 623 | } |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | int do_loglevel(int nargs, char **args) { |
| 628 | if (nargs == 2) { |
| 629 | log_set_level(atoi(args[1])); |
| 630 | return 0; |
| 631 | } |
| 632 | return -1; |
| 633 | } |
| 634 | |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 635 | int do_load_persist_props(int nargs, char **args) { |
| 636 | if (nargs == 1) { |
| 637 | load_persist_props(); |
| 638 | return 0; |
| 639 | } |
| 640 | return -1; |
| 641 | } |
| 642 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 643 | int do_wait(int nargs, char **args) |
| 644 | { |
| 645 | if (nargs == 2) { |
| 646 | return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); |
| 647 | } |
| 648 | return -1; |
| 649 | } |