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> |
Elliott Hughes | 3d74d7a | 2015-01-29 21:31:23 -0800 | [diff] [blame^] | 32 | #include <sys/time.h> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 33 | #include <sys/wait.h> |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 34 | #include <linux/loop.h> |
Ken Sumrall | 7bc6e9e | 2011-05-26 20:01:39 -0700 | [diff] [blame] | 35 | #include <cutils/partition_utils.h> |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 36 | #include <cutils/android_reboot.h> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 37 | #include <fs_mgr.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 39 | #include <selinux/selinux.h> |
| 40 | #include <selinux/label.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 41 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | #include "init.h" |
| 43 | #include "keywords.h" |
| 44 | #include "property_service.h" |
| 45 | #include "devices.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 46 | #include "init_parser.h" |
Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 47 | #include "util.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 48 | #include "log.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
| 50 | #include <private/android_filesystem_config.h> |
| 51 | |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 52 | int add_environment(const char *name, const char *value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
| 54 | extern int init_module(void *, unsigned long, const char *); |
| 55 | |
| 56 | static int write_file(const char *path, const char *value) |
| 57 | { |
| 58 | int fd, ret, len; |
| 59 | |
Nick Kralevich | 5535b05 | 2013-09-17 14:43:12 -0700 | [diff] [blame] | 60 | fd = open(path, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | |
| 62 | if (fd < 0) |
Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 63 | return -errno; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | |
| 65 | len = strlen(value); |
| 66 | |
| 67 | do { |
| 68 | ret = write(fd, value, len); |
| 69 | } while (ret < 0 && errno == EINTR); |
| 70 | |
| 71 | close(fd); |
| 72 | if (ret < 0) { |
Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 73 | return -errno; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | } else { |
| 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | |
Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 79 | static int _open(const char *path) |
| 80 | { |
| 81 | int fd; |
| 82 | |
| 83 | fd = open(path, O_RDONLY | O_NOFOLLOW); |
| 84 | if (fd < 0) |
| 85 | fd = open(path, O_WRONLY | O_NOFOLLOW); |
| 86 | |
| 87 | return fd; |
| 88 | } |
| 89 | |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 90 | static int _chown(const char *path, unsigned int uid, unsigned int gid) |
| 91 | { |
| 92 | int fd; |
| 93 | int ret; |
| 94 | |
Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 95 | fd = _open(path); |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 96 | if (fd < 0) { |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | ret = fchown(fd, uid, gid); |
| 101 | if (ret < 0) { |
| 102 | int errno_copy = errno; |
| 103 | close(fd); |
| 104 | errno = errno_copy; |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | close(fd); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int _chmod(const char *path, mode_t mode) |
| 114 | { |
| 115 | int fd; |
| 116 | int ret; |
| 117 | |
Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 118 | fd = _open(path); |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 119 | if (fd < 0) { |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | ret = fchmod(fd, mode); |
| 124 | if (ret < 0) { |
| 125 | int errno_copy = errno; |
| 126 | close(fd); |
| 127 | errno = errno_copy; |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | close(fd); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 136 | static int insmod(const char *filename, char *options) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 137 | { |
| 138 | void *module; |
| 139 | unsigned size; |
| 140 | int ret; |
| 141 | |
| 142 | module = read_file(filename, &size); |
| 143 | if (!module) |
| 144 | return -1; |
| 145 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 146 | ret = init_module(module, size, options); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 147 | |
| 148 | free(module); |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static int setkey(struct kbentry *kbe) |
| 154 | { |
| 155 | int fd, ret; |
| 156 | |
| 157 | fd = open("/dev/tty0", O_RDWR | O_SYNC); |
| 158 | if (fd < 0) |
| 159 | return -1; |
| 160 | |
| 161 | ret = ioctl(fd, KDSKBENT, kbe); |
| 162 | |
| 163 | close(fd); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int __ifupdown(const char *interface, int up) |
| 168 | { |
| 169 | struct ifreq ifr; |
| 170 | int s, ret; |
| 171 | |
| 172 | strlcpy(ifr.ifr_name, interface, IFNAMSIZ); |
| 173 | |
| 174 | s = socket(AF_INET, SOCK_DGRAM, 0); |
| 175 | if (s < 0) |
| 176 | return -1; |
| 177 | |
| 178 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); |
| 179 | if (ret < 0) { |
| 180 | goto done; |
| 181 | } |
| 182 | |
| 183 | if (up) |
| 184 | ifr.ifr_flags |= IFF_UP; |
| 185 | else |
| 186 | ifr.ifr_flags &= ~IFF_UP; |
| 187 | |
| 188 | ret = ioctl(s, SIOCSIFFLAGS, &ifr); |
| 189 | |
| 190 | done: |
| 191 | close(s); |
| 192 | return ret; |
| 193 | } |
| 194 | |
| 195 | static void service_start_if_not_disabled(struct service *svc) |
| 196 | { |
| 197 | if (!(svc->flags & SVC_DISABLED)) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 198 | service_start(svc, NULL); |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 199 | } else { |
| 200 | svc->flags |= SVC_DISABLED_START; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Jay Freeman (saurik) | e7cb137 | 2008-11-17 06:41:10 +0000 | [diff] [blame] | 204 | int do_chdir(int nargs, char **args) |
| 205 | { |
| 206 | chdir(args[1]); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int do_chroot(int nargs, char **args) |
| 211 | { |
| 212 | chroot(args[1]); |
| 213 | return 0; |
| 214 | } |
| 215 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 216 | int do_class_start(int nargs, char **args) |
| 217 | { |
| 218 | /* Starting a class does not start services |
| 219 | * which are explicitly disabled. They must |
| 220 | * be started individually. |
| 221 | */ |
| 222 | service_for_each_class(args[1], service_start_if_not_disabled); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int do_class_stop(int nargs, char **args) |
| 227 | { |
| 228 | service_for_each_class(args[1], service_stop); |
| 229 | return 0; |
| 230 | } |
| 231 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 232 | int do_class_reset(int nargs, char **args) |
| 233 | { |
| 234 | service_for_each_class(args[1], service_reset); |
| 235 | return 0; |
| 236 | } |
| 237 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | int do_domainname(int nargs, char **args) |
| 239 | { |
| 240 | return write_file("/proc/sys/kernel/domainname", args[1]); |
| 241 | } |
| 242 | |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 243 | int do_enable(int nargs, char **args) |
| 244 | { |
| 245 | struct service *svc; |
| 246 | svc = service_find_by_name(args[1]); |
| 247 | if (svc) { |
| 248 | svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED); |
| 249 | if (svc->flags & SVC_DISABLED_START) { |
| 250 | service_start(svc, NULL); |
| 251 | } |
| 252 | } else { |
| 253 | return -1; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 258 | int do_exec(int nargs, char **args) |
| 259 | { |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | int do_export(int nargs, char **args) |
| 264 | { |
James Morrissey | 381341f | 2014-05-16 11:36:36 +0100 | [diff] [blame] | 265 | return add_environment(args[1], args[2]); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int do_hostname(int nargs, char **args) |
| 269 | { |
| 270 | return write_file("/proc/sys/kernel/hostname", args[1]); |
| 271 | } |
| 272 | |
| 273 | int do_ifup(int nargs, char **args) |
| 274 | { |
| 275 | return __ifupdown(args[1], 1); |
| 276 | } |
| 277 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 278 | |
| 279 | static int do_insmod_inner(int nargs, char **args, int opt_len) |
| 280 | { |
| 281 | char options[opt_len + 1]; |
| 282 | int i; |
| 283 | |
| 284 | options[0] = '\0'; |
| 285 | if (nargs > 2) { |
| 286 | strcpy(options, args[2]); |
| 287 | for (i = 3; i < nargs; ++i) { |
| 288 | strcat(options, " "); |
| 289 | strcat(options, args[i]); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return insmod(args[1], options); |
| 294 | } |
| 295 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | int do_insmod(int nargs, char **args) |
| 297 | { |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 298 | int i; |
| 299 | int size = 0; |
| 300 | |
| 301 | if (nargs > 2) { |
| 302 | for (i = 2; i < nargs; ++i) |
| 303 | size += strlen(args[i]) + 1; |
| 304 | } |
| 305 | |
| 306 | return do_insmod_inner(nargs, args, size); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 307 | } |
| 308 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | int do_mkdir(int nargs, char **args) |
| 310 | { |
| 311 | mode_t mode = 0755; |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 312 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 313 | |
| 314 | /* mkdir <path> [mode] [owner] [group] */ |
| 315 | |
| 316 | if (nargs >= 3) { |
| 317 | mode = strtoul(args[2], 0, 8); |
| 318 | } |
| 319 | |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 320 | ret = make_dir(args[1], mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 321 | /* chmod in case the directory already exists */ |
| 322 | if (ret == -1 && errno == EEXIST) { |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 323 | ret = _chmod(args[1], mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 324 | } |
| 325 | if (ret == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 326 | return -errno; |
| 327 | } |
| 328 | |
| 329 | if (nargs >= 4) { |
| 330 | uid_t uid = decode_uid(args[3]); |
| 331 | gid_t gid = -1; |
| 332 | |
| 333 | if (nargs == 5) { |
| 334 | gid = decode_uid(args[4]); |
| 335 | } |
| 336 | |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 337 | if (_chown(args[1], uid, gid) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 338 | return -errno; |
| 339 | } |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 340 | |
| 341 | /* chown may have cleared S_ISUID and S_ISGID, chmod again */ |
| 342 | if (mode & (S_ISUID | S_ISGID)) { |
| 343 | ret = _chmod(args[1], mode); |
| 344 | if (ret == -1) { |
| 345 | return -errno; |
| 346 | } |
| 347 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static struct { |
| 354 | const char *name; |
| 355 | unsigned flag; |
| 356 | } mount_flags[] = { |
| 357 | { "noatime", MS_NOATIME }, |
Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 358 | { "noexec", MS_NOEXEC }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 | { "nosuid", MS_NOSUID }, |
| 360 | { "nodev", MS_NODEV }, |
| 361 | { "nodiratime", MS_NODIRATIME }, |
| 362 | { "ro", MS_RDONLY }, |
| 363 | { "rw", 0 }, |
| 364 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 365 | { "bind", MS_BIND }, |
| 366 | { "rec", MS_REC }, |
| 367 | { "unbindable", MS_UNBINDABLE }, |
| 368 | { "private", MS_PRIVATE }, |
| 369 | { "slave", MS_SLAVE }, |
| 370 | { "shared", MS_SHARED }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 371 | { "defaults", 0 }, |
| 372 | { 0, 0 }, |
| 373 | }; |
| 374 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 375 | #define DATA_MNT_POINT "/data" |
| 376 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 377 | /* mount <type> <device> <path> <flags ...> <options> */ |
| 378 | int do_mount(int nargs, char **args) |
| 379 | { |
| 380 | char tmp[64]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 381 | char *source, *target, *system; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 382 | char *options = NULL; |
| 383 | unsigned flags = 0; |
| 384 | int n, i; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 385 | int wait = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 386 | |
| 387 | for (n = 4; n < nargs; n++) { |
| 388 | for (i = 0; mount_flags[i].name; i++) { |
| 389 | if (!strcmp(args[n], mount_flags[i].name)) { |
| 390 | flags |= mount_flags[i].flag; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 395 | if (!mount_flags[i].name) { |
| 396 | if (!strcmp(args[n], "wait")) |
| 397 | wait = 1; |
| 398 | /* if our last argument isn't a flag, wolf it up as an option string */ |
| 399 | else if (n + 1 == nargs) |
| 400 | options = args[n]; |
| 401 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 404 | system = args[1]; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 405 | source = args[2]; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 406 | target = args[3]; |
| 407 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 408 | if (!strncmp(source, "mtd@", 4)) { |
| 409 | n = mtd_name_to_number(source + 4); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 410 | if (n < 0) { |
| 411 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 412 | } |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 413 | |
| 414 | sprintf(tmp, "/dev/block/mtdblock%d", n); |
| 415 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 416 | if (wait) |
| 417 | wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 418 | if (mount(tmp, target, system, flags, options) < 0) { |
| 419 | return -1; |
| 420 | } |
| 421 | |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 422 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 423 | } else if (!strncmp(source, "loop@", 5)) { |
| 424 | int mode, loop, fd; |
| 425 | struct loop_info info; |
| 426 | |
| 427 | mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; |
| 428 | fd = open(source + 5, mode); |
| 429 | if (fd < 0) { |
| 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | for (n = 0; ; n++) { |
| 434 | sprintf(tmp, "/dev/block/loop%d", n); |
| 435 | loop = open(tmp, mode); |
| 436 | if (loop < 0) { |
Tomasz Kondel | bfdcc40 | 2014-02-06 08:57:27 +0100 | [diff] [blame] | 437 | close(fd); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 438 | return -1; |
| 439 | } |
| 440 | |
| 441 | /* if it is a blank loop device */ |
| 442 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { |
| 443 | /* if it becomes our loop device */ |
| 444 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { |
| 445 | close(fd); |
| 446 | |
| 447 | if (mount(tmp, target, system, flags, options) < 0) { |
| 448 | ioctl(loop, LOOP_CLR_FD, 0); |
| 449 | close(loop); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | close(loop); |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 454 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
| 458 | close(loop); |
| 459 | } |
| 460 | |
| 461 | close(fd); |
| 462 | ERROR("out of loopback devices"); |
| 463 | return -1; |
| 464 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 465 | if (wait) |
| 466 | wait_for_file(source, COMMAND_RETRY_TIMEOUT); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 467 | if (mount(source, target, system, flags, options) < 0) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 468 | return -1; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 469 | } |
| 470 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 471 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 472 | |
| 473 | exit_success: |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 474 | return 0; |
| 475 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 476 | } |
| 477 | |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 478 | static int wipe_data_via_recovery() |
| 479 | { |
| 480 | mkdir("/cache/recovery", 0700); |
| 481 | int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC, 0600); |
| 482 | if (fd >= 0) { |
Jeff Sharkey | d26135b | 2014-09-24 11:46:36 -0700 | [diff] [blame] | 483 | write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1); |
| 484 | write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 485 | close(fd); |
| 486 | } else { |
| 487 | ERROR("could not open /cache/recovery/command\n"); |
| 488 | return -1; |
| 489 | } |
| 490 | android_reboot(ANDROID_RB_RESTART2, 0, "recovery"); |
| 491 | while (1) { pause(); } // never reached |
| 492 | } |
| 493 | |
| 494 | |
| 495 | /* |
| 496 | * This function might request a reboot, in which case it will |
| 497 | * not return. |
| 498 | */ |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 499 | int do_mount_all(int nargs, char **args) |
| 500 | { |
| 501 | pid_t pid; |
| 502 | int ret = -1; |
| 503 | int child_ret = -1; |
| 504 | int status; |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 505 | struct fstab *fstab; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 506 | |
| 507 | if (nargs != 2) { |
| 508 | return -1; |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and |
| 513 | * do the call in the child to provide protection to the main init |
| 514 | * process if anything goes wrong (crash or memory leak), and wait for |
| 515 | * the child to finish in the parent. |
| 516 | */ |
| 517 | pid = fork(); |
| 518 | if (pid > 0) { |
| 519 | /* Parent. Wait for the child to return */ |
Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 520 | int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 521 | if (wp_ret < 0) { |
| 522 | /* Unexpected error code. We will continue anyway. */ |
| 523 | NOTICE("waitpid failed rc=%d, errno=%d\n", wp_ret, errno); |
| 524 | } |
| 525 | |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 526 | if (WIFEXITED(status)) { |
| 527 | ret = WEXITSTATUS(status); |
| 528 | } else { |
| 529 | ret = -1; |
| 530 | } |
| 531 | } else if (pid == 0) { |
| 532 | /* child, call fs_mgr_mount_all() */ |
| 533 | klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 534 | fstab = fs_mgr_read_fstab(args[1]); |
| 535 | child_ret = fs_mgr_mount_all(fstab); |
| 536 | fs_mgr_free_fstab(fstab); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 537 | if (child_ret == -1) { |
| 538 | ERROR("fs_mgr_mount_all returned an error\n"); |
| 539 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 540 | _exit(child_ret); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 541 | } else { |
| 542 | /* fork failed, return an error */ |
| 543 | return -1; |
| 544 | } |
| 545 | |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 546 | if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 547 | property_set("vold.decrypt", "trigger_encryption"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 548 | } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 549 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 13d5bb4 | 2014-01-30 10:43:52 -0800 | [diff] [blame] | 550 | property_set("vold.decrypt", "trigger_default_encryption"); |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 551 | } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 552 | property_set("ro.crypto.state", "unencrypted"); |
| 553 | /* If fs_mgr determined this is an unencrypted device, then trigger |
| 554 | * that action. |
| 555 | */ |
| 556 | action_for_each_trigger("nonencrypted", action_add_queue_tail); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 557 | } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { |
| 558 | /* Setup a wipe via recovery, and reboot into recovery */ |
| 559 | ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n"); |
| 560 | ret = wipe_data_via_recovery(); |
| 561 | /* If reboot worked, there is no return. */ |
| 562 | } else if (ret > 0) { |
| 563 | ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 564 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 565 | /* else ... < 0: error */ |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 570 | int do_swapon_all(int nargs, char **args) |
| 571 | { |
| 572 | struct fstab *fstab; |
| 573 | int ret; |
| 574 | |
| 575 | fstab = fs_mgr_read_fstab(args[1]); |
| 576 | ret = fs_mgr_swapon_all(fstab); |
| 577 | fs_mgr_free_fstab(fstab); |
| 578 | |
| 579 | return ret; |
| 580 | } |
| 581 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 582 | int do_setcon(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 583 | if (is_selinux_enabled() <= 0) |
| 584 | return 0; |
| 585 | if (setcon(args[1]) < 0) { |
| 586 | return -errno; |
| 587 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | int do_setenforce(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 592 | if (is_selinux_enabled() <= 0) |
| 593 | return 0; |
| 594 | if (security_setenforce(atoi(args[1])) < 0) { |
| 595 | return -errno; |
| 596 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 597 | return 0; |
| 598 | } |
| 599 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 600 | int do_setkey(int nargs, char **args) |
| 601 | { |
| 602 | struct kbentry kbe; |
| 603 | kbe.kb_table = strtoul(args[1], 0, 0); |
| 604 | kbe.kb_index = strtoul(args[2], 0, 0); |
| 605 | kbe.kb_value = strtoul(args[3], 0, 0); |
| 606 | return setkey(&kbe); |
| 607 | } |
| 608 | |
| 609 | int do_setprop(int nargs, char **args) |
| 610 | { |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 611 | const char *name = args[1]; |
| 612 | const char *value = args[2]; |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 613 | char prop_val[PROP_VALUE_MAX]; |
| 614 | int ret; |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 615 | |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 616 | ret = expand_props(prop_val, value, sizeof(prop_val)); |
| 617 | if (ret) { |
| 618 | ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); |
| 619 | return -EINVAL; |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 620 | } |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 621 | property_set(name, prop_val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
| 625 | int do_setrlimit(int nargs, char **args) |
| 626 | { |
| 627 | struct rlimit limit; |
| 628 | int resource; |
| 629 | resource = atoi(args[1]); |
| 630 | limit.rlim_cur = atoi(args[2]); |
| 631 | limit.rlim_max = atoi(args[3]); |
| 632 | return setrlimit(resource, &limit); |
| 633 | } |
| 634 | |
| 635 | int do_start(int nargs, char **args) |
| 636 | { |
| 637 | struct service *svc; |
| 638 | svc = service_find_by_name(args[1]); |
| 639 | if (svc) { |
San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 640 | service_start(svc, NULL); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 641 | } |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | int do_stop(int nargs, char **args) |
| 646 | { |
| 647 | struct service *svc; |
| 648 | svc = service_find_by_name(args[1]); |
| 649 | if (svc) { |
| 650 | service_stop(svc); |
| 651 | } |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | int do_restart(int nargs, char **args) |
| 656 | { |
| 657 | struct service *svc; |
| 658 | svc = service_find_by_name(args[1]); |
| 659 | if (svc) { |
Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 660 | service_restart(svc); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 661 | } |
| 662 | return 0; |
| 663 | } |
| 664 | |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 665 | int do_powerctl(int nargs, char **args) |
| 666 | { |
| 667 | char command[PROP_VALUE_MAX]; |
| 668 | int res; |
| 669 | int len = 0; |
| 670 | int cmd = 0; |
| 671 | char *reboot_target; |
| 672 | |
| 673 | res = expand_props(command, args[1], sizeof(command)); |
| 674 | if (res) { |
| 675 | ERROR("powerctl: cannot expand '%s'\n", args[1]); |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | |
| 679 | if (strncmp(command, "shutdown", 8) == 0) { |
| 680 | cmd = ANDROID_RB_POWEROFF; |
| 681 | len = 8; |
| 682 | } else if (strncmp(command, "reboot", 6) == 0) { |
| 683 | cmd = ANDROID_RB_RESTART2; |
| 684 | len = 6; |
| 685 | } else { |
| 686 | ERROR("powerctl: unrecognized command '%s'\n", command); |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
| 690 | if (command[len] == ',') { |
| 691 | reboot_target = &command[len + 1]; |
| 692 | } else if (command[len] == '\0') { |
| 693 | reboot_target = ""; |
| 694 | } else { |
| 695 | ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]); |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | |
| 699 | return android_reboot(cmd, 0, reboot_target); |
| 700 | } |
| 701 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 702 | int do_trigger(int nargs, char **args) |
| 703 | { |
Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 704 | 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] | 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | int do_symlink(int nargs, char **args) |
| 709 | { |
| 710 | return symlink(args[1], args[2]); |
| 711 | } |
| 712 | |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 713 | int do_rm(int nargs, char **args) |
| 714 | { |
| 715 | return unlink(args[1]); |
| 716 | } |
| 717 | |
| 718 | int do_rmdir(int nargs, char **args) |
| 719 | { |
| 720 | return rmdir(args[1]); |
| 721 | } |
| 722 | |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 723 | int do_sysclktz(int nargs, char **args) |
| 724 | { |
| 725 | struct timezone tz; |
| 726 | |
| 727 | if (nargs != 2) |
| 728 | return -1; |
| 729 | |
| 730 | memset(&tz, 0, sizeof(tz)); |
| 731 | tz.tz_minuteswest = atoi(args[1]); |
| 732 | if (settimeofday(NULL, &tz)) |
| 733 | return -1; |
| 734 | return 0; |
| 735 | } |
| 736 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 737 | int do_write(int nargs, char **args) |
| 738 | { |
Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 739 | const char *path = args[1]; |
| 740 | const char *value = args[2]; |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 741 | char prop_val[PROP_VALUE_MAX]; |
| 742 | int ret; |
Mike Lockwood | 2c4d5dc | 2011-06-07 17:30:11 -0700 | [diff] [blame] | 743 | |
Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 744 | ret = expand_props(prop_val, value, sizeof(prop_val)); |
| 745 | if (ret) { |
| 746 | ERROR("cannot expand '%s' while writing to '%s'\n", value, path); |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | return write_file(path, prop_val); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 750 | } |
| 751 | |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 752 | int do_copy(int nargs, char **args) |
| 753 | { |
| 754 | char *buffer = NULL; |
| 755 | int rc = 0; |
| 756 | int fd1 = -1, fd2 = -1; |
| 757 | struct stat info; |
| 758 | int brtw, brtr; |
| 759 | char *p; |
| 760 | |
| 761 | if (nargs != 3) |
| 762 | return -1; |
| 763 | |
| 764 | if (stat(args[1], &info) < 0) |
| 765 | return -1; |
| 766 | |
| 767 | if ((fd1 = open(args[1], O_RDONLY)) < 0) |
| 768 | goto out_err; |
| 769 | |
Tom Zhu | 4833d9f | 2009-09-28 19:53:12 -0500 | [diff] [blame] | 770 | 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] | 771 | goto out_err; |
| 772 | |
| 773 | if (!(buffer = malloc(info.st_size))) |
| 774 | goto out_err; |
| 775 | |
| 776 | p = buffer; |
| 777 | brtr = info.st_size; |
| 778 | while(brtr) { |
| 779 | rc = read(fd1, p, brtr); |
| 780 | if (rc < 0) |
| 781 | goto out_err; |
| 782 | if (rc == 0) |
| 783 | break; |
| 784 | p += rc; |
| 785 | brtr -= rc; |
| 786 | } |
| 787 | |
| 788 | p = buffer; |
| 789 | brtw = info.st_size; |
| 790 | while(brtw) { |
| 791 | rc = write(fd2, p, brtw); |
| 792 | if (rc < 0) |
| 793 | goto out_err; |
| 794 | if (rc == 0) |
| 795 | break; |
| 796 | p += rc; |
| 797 | brtw -= rc; |
| 798 | } |
| 799 | |
| 800 | rc = 0; |
| 801 | goto out; |
| 802 | out_err: |
| 803 | rc = -1; |
| 804 | out: |
| 805 | if (buffer) |
| 806 | free(buffer); |
| 807 | if (fd1 >= 0) |
| 808 | close(fd1); |
| 809 | if (fd2 >= 0) |
| 810 | close(fd2); |
| 811 | return rc; |
| 812 | } |
| 813 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 814 | int do_chown(int nargs, char **args) { |
| 815 | /* GID is optional. */ |
| 816 | if (nargs == 3) { |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 817 | if (_chown(args[2], decode_uid(args[1]), -1) < 0) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 818 | return -errno; |
| 819 | } else if (nargs == 4) { |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 820 | if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 821 | return -errno; |
| 822 | } else { |
| 823 | return -1; |
| 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static mode_t get_mode(const char *s) { |
| 829 | mode_t mode = 0; |
| 830 | while (*s) { |
| 831 | if (*s >= '0' && *s <= '7') { |
| 832 | mode = (mode<<3) | (*s-'0'); |
| 833 | } else { |
| 834 | return -1; |
| 835 | } |
| 836 | s++; |
| 837 | } |
| 838 | return mode; |
| 839 | } |
| 840 | |
| 841 | int do_chmod(int nargs, char **args) { |
| 842 | mode_t mode = get_mode(args[1]); |
Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 843 | if (_chmod(args[2], mode) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 844 | return -errno; |
| 845 | } |
| 846 | return 0; |
| 847 | } |
| 848 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 849 | int do_restorecon(int nargs, char **args) { |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 850 | int i; |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 851 | int ret = 0; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 852 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 853 | for (i = 1; i < nargs; i++) { |
Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 854 | if (restorecon(args[i]) < 0) |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 855 | ret = -errno; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 856 | } |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 857 | return ret; |
| 858 | } |
| 859 | |
| 860 | int do_restorecon_recursive(int nargs, char **args) { |
| 861 | int i; |
| 862 | int ret = 0; |
| 863 | |
| 864 | for (i = 1; i < nargs; i++) { |
| 865 | if (restorecon_recursive(args[i]) < 0) |
| 866 | ret = -errno; |
| 867 | } |
| 868 | return ret; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | int do_setsebool(int nargs, char **args) { |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 872 | const char *name = args[1]; |
| 873 | const char *value = args[2]; |
| 874 | SELboolean b; |
| 875 | int ret; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 876 | |
| 877 | if (is_selinux_enabled() <= 0) |
| 878 | return 0; |
| 879 | |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 880 | b.name = name; |
| 881 | if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on")) |
| 882 | b.value = 1; |
| 883 | else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off")) |
| 884 | b.value = 0; |
| 885 | else { |
| 886 | ERROR("setsebool: invalid value %s\n", value); |
| 887 | return -EINVAL; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 888 | } |
| 889 | |
Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 890 | if (security_set_boolean_list(1, &b, 0) < 0) { |
| 891 | ret = -errno; |
| 892 | ERROR("setsebool: could not set %s to %s\n", name, value); |
| 893 | return ret; |
| 894 | } |
Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 895 | |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 899 | int do_loglevel(int nargs, char **args) { |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 900 | int log_level; |
| 901 | char log_level_str[PROP_VALUE_MAX] = ""; |
| 902 | if (nargs != 2) { |
| 903 | ERROR("loglevel: missing argument\n"); |
| 904 | return -EINVAL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 905 | } |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 906 | |
| 907 | if (expand_props(log_level_str, args[1], sizeof(log_level_str))) { |
| 908 | ERROR("loglevel: cannot expand '%s'\n", args[1]); |
| 909 | return -EINVAL; |
| 910 | } |
| 911 | log_level = atoi(log_level_str); |
| 912 | if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) { |
| 913 | ERROR("loglevel: invalid log level'%d'\n", log_level); |
| 914 | return -EINVAL; |
| 915 | } |
| 916 | klog_set_level(log_level); |
| 917 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 920 | int do_load_persist_props(int nargs, char **args) { |
| 921 | if (nargs == 1) { |
| 922 | load_persist_props(); |
| 923 | return 0; |
| 924 | } |
| 925 | return -1; |
| 926 | } |
| 927 | |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 928 | int do_load_all_props(int nargs, char **args) { |
| 929 | if (nargs == 1) { |
| 930 | load_all_props(); |
| 931 | return 0; |
| 932 | } |
| 933 | return -1; |
| 934 | } |
| 935 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 936 | int do_wait(int nargs, char **args) |
| 937 | { |
| 938 | if (nargs == 2) { |
| 939 | return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); |
Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 940 | } else if (nargs == 3) { |
| 941 | return wait_for_file(args[1], atoi(args[2])); |
| 942 | } else |
| 943 | return -1; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 944 | } |