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