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