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