| 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 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 17 | #include "builtins.h" | 
|  | 18 |  | 
| Mark Salyzyn | a98cc9c | 2016-04-05 13:43:40 -0700 | [diff] [blame] | 19 | #include <dirent.h> | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | #include <errno.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 21 | #include <fcntl.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 22 | #include <linux/loop.h> | 
|  | 23 | #include <linux/module.h> | 
| Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 24 | #include <mntent.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 25 | #include <net/if.h> | 
| Mark Salyzyn | ad575e0 | 2016-04-05 08:10:25 -0700 | [diff] [blame] | 26 | #include <sched.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 27 | #include <signal.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 28 | #include <stdio.h> | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | #include <stdlib.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 30 | #include <string.h> | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | #include <sys/mount.h> | 
|  | 32 | #include <sys/resource.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 33 | #include <sys/socket.h> | 
|  | 34 | #include <sys/stat.h> | 
| Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 35 | #include <sys/syscall.h> | 
| Tom Cherry | f57c0bf | 2017-04-07 13:46:21 -0700 | [diff] [blame] | 36 | #include <sys/system_properties.h> | 
| Elliott Hughes | 3d74d7a | 2015-01-29 21:31:23 -0800 | [diff] [blame] | 37 | #include <sys/time.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 38 | #include <sys/types.h> | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 39 | #include <sys/wait.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 40 | #include <unistd.h> | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 41 |  | 
| Mark Salyzyn | a98cc9c | 2016-04-05 13:43:40 -0700 | [diff] [blame] | 42 | #include <android-base/file.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 43 | #include <android-base/logging.h> | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 44 | #include <android-base/parseint.h> | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 45 | #include <android-base/properties.h> | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 46 | #include <android-base/strings.h> | 
| Yabin Cui | 0b1252c | 2016-06-24 18:28:03 -0700 | [diff] [blame] | 47 | #include <bootloader_message/bootloader_message.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 48 | #include <cutils/android_reboot.h> | 
| Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 49 | #include <ext4_utils/ext4_crypt.h> | 
|  | 50 | #include <ext4_utils/ext4_crypt_init_extensions.h> | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 51 | #include <fs_mgr.h> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 52 | #include <selinux/android.h> | 
|  | 53 | #include <selinux/label.h> | 
|  | 54 | #include <selinux/selinux.h> | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 55 |  | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 56 | #include "action.h" | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 57 | #include "bootchart.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 58 | #include "init.h" | 
| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 59 | #include "init_parser.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 60 | #include "property_service.h" | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 61 | #include "reboot.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 62 | #include "service.h" | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 63 | #include "signal_handler.h" | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 64 | #include "util.h" | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 65 |  | 
| Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 66 | using namespace std::literals::string_literals; | 
|  | 67 |  | 
| Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 68 | #define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW | 
|  | 69 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 70 | namespace android { | 
|  | 71 | namespace init { | 
|  | 72 |  | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 73 | static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s; | 
| Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 74 |  | 
| Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 75 | static int insmod(const char *filename, const char *options, int flags) { | 
| Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 76 | int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); | 
|  | 77 | if (fd == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 78 | PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed"; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | return -1; | 
| Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 80 | } | 
| Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 81 | int rc = syscall(__NR_finit_module, fd, options, flags); | 
| Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 82 | if (rc == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 83 | PLOG(ERROR) << "finit_module for \"" << filename << "\" failed"; | 
| Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 84 | } | 
|  | 85 | close(fd); | 
|  | 86 | return rc; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 89 | static int __ifupdown(const char *interface, int up) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | struct ifreq ifr; | 
|  | 91 | int s, ret; | 
|  | 92 |  | 
|  | 93 | strlcpy(ifr.ifr_name, interface, IFNAMSIZ); | 
|  | 94 |  | 
|  | 95 | s = socket(AF_INET, SOCK_DGRAM, 0); | 
|  | 96 | if (s < 0) | 
|  | 97 | return -1; | 
|  | 98 |  | 
|  | 99 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); | 
|  | 100 | if (ret < 0) { | 
|  | 101 | goto done; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | if (up) | 
|  | 105 | ifr.ifr_flags |= IFF_UP; | 
|  | 106 | else | 
|  | 107 | ifr.ifr_flags &= ~IFF_UP; | 
|  | 108 |  | 
|  | 109 | ret = ioctl(s, SIOCSIFFLAGS, &ifr); | 
| Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 110 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | done: | 
|  | 112 | close(s); | 
|  | 113 | return ret; | 
|  | 114 | } | 
|  | 115 |  | 
| Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 116 | static int reboot_into_recovery(const std::vector<std::string>& options) { | 
| Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 117 | std::string err; | 
|  | 118 | if (!write_bootloader_message(options, &err)) { | 
| Elliott Hughes | 7f5b29f | 2016-06-27 09:54:25 -0700 | [diff] [blame] | 119 | LOG(ERROR) << "failed to set bootloader message: " << err; | 
| Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 120 | return -1; | 
|  | 121 | } | 
| Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 122 | DoReboot(ANDROID_RB_RESTART2, "reboot", "recovery", false); | 
|  | 123 | return 0; | 
| Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 124 | } | 
|  | 125 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 126 | static int do_class_start(const std::vector<std::string>& args) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | /* Starting a class does not start services | 
|  | 128 | * which are explicitly disabled.  They must | 
|  | 129 | * be started individually. | 
|  | 130 | */ | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 131 | ServiceManager::GetInstance(). | 
|  | 132 | ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); }); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | return 0; | 
|  | 134 | } | 
|  | 135 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 136 | static int do_class_stop(const std::vector<std::string>& args) { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 137 | ServiceManager::GetInstance(). | 
|  | 138 | ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); }); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | return 0; | 
|  | 140 | } | 
|  | 141 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 142 | static int do_class_reset(const std::vector<std::string>& args) { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 143 | ServiceManager::GetInstance(). | 
|  | 144 | ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); }); | 
| Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 145 | return 0; | 
|  | 146 | } | 
|  | 147 |  | 
| Steven Moreland | 2b63d54 | 2017-03-10 14:04:37 -0800 | [diff] [blame] | 148 | static int do_class_restart(const std::vector<std::string>& args) { | 
|  | 149 | ServiceManager::GetInstance(). | 
|  | 150 | ForEachServiceInClass(args[1], [] (Service* s) { s->Restart(); }); | 
|  | 151 | return 0; | 
|  | 152 | } | 
|  | 153 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 154 | static int do_domainname(const std::vector<std::string>& args) { | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 155 | std::string err; | 
|  | 156 | if (!WriteFile("/proc/sys/kernel/domainname", args[1], &err)) { | 
|  | 157 | LOG(ERROR) << err; | 
|  | 158 | return -1; | 
|  | 159 | } | 
|  | 160 | return 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 163 | static int do_enable(const std::vector<std::string>& args) { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 164 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); | 
|  | 165 | if (!svc) { | 
| JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 166 | return -1; | 
|  | 167 | } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 168 | return svc->Enable(); | 
| JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 169 | } | 
|  | 170 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 171 | static int do_exec(const std::vector<std::string>& args) { | 
| Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 172 | return ServiceManager::GetInstance().Exec(args) ? 0 : -1; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | static int do_exec_start(const std::vector<std::string>& args) { | 
|  | 176 | return ServiceManager::GetInstance().ExecStart(args[1]) ? 0 : -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 179 | static int do_export(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 180 | return add_environment(args[1].c_str(), args[2].c_str()); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 183 | static int do_hostname(const std::vector<std::string>& args) { | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 184 | std::string err; | 
|  | 185 | if (!WriteFile("/proc/sys/kernel/hostname", args[1], &err)) { | 
|  | 186 | LOG(ERROR) << err; | 
|  | 187 | return -1; | 
|  | 188 | } | 
|  | 189 | return 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 192 | static int do_ifup(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 193 | return __ifupdown(args[1].c_str(), 1); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 196 | static int do_insmod(const std::vector<std::string>& args) { | 
| Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 197 | int flags = 0; | 
|  | 198 | auto it = args.begin() + 1; | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 199 |  | 
| Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 200 | if (!(*it).compare("-f")) { | 
|  | 201 | flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS; | 
|  | 202 | it++; | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 205 | std::string filename = *it++; | 
|  | 206 | std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' '); | 
|  | 207 | return insmod(filename.c_str(), options.c_str(), flags); | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 210 | static int do_mkdir(const std::vector<std::string>& args) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | mode_t mode = 0755; | 
| Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 212 | int ret; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 |  | 
|  | 214 | /* mkdir <path> [mode] [owner] [group] */ | 
|  | 215 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 216 | if (args.size() >= 3) { | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 217 | mode = std::strtoul(args[2].c_str(), 0, 8); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Tom Cherry | e7656b7 | 2017-05-01 17:10:09 -0700 | [diff] [blame] | 220 | ret = make_dir(args[1].c_str(), mode, sehandle); | 
| Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 221 | /* chmod in case the directory already exists */ | 
|  | 222 | if (ret == -1 && errno == EEXIST) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 223 | ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW); | 
| Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 224 | } | 
|  | 225 | if (ret == -1) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | return -errno; | 
|  | 227 | } | 
|  | 228 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 229 | if (args.size() >= 4) { | 
| Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 230 | uid_t uid; | 
|  | 231 | std::string decode_uid_err; | 
|  | 232 | if (!DecodeUid(args[3], &uid, &decode_uid_err)) { | 
|  | 233 | LOG(ERROR) << "Unable to find UID for '" << args[3] << "': " << decode_uid_err; | 
|  | 234 | return -1; | 
|  | 235 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 236 | gid_t gid = -1; | 
|  | 237 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 238 | if (args.size() == 5) { | 
| Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 239 | if (!DecodeUid(args[4], &gid, &decode_uid_err)) { | 
|  | 240 | LOG(ERROR) << "Unable to find GID for '" << args[3] << "': " << decode_uid_err; | 
|  | 241 | return -1; | 
|  | 242 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 245 | if (lchown(args[1].c_str(), uid, gid) == -1) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 246 | return -errno; | 
|  | 247 | } | 
| Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 248 |  | 
|  | 249 | /* chown may have cleared S_ISUID and S_ISGID, chmod again */ | 
|  | 250 | if (mode & (S_ISUID | S_ISGID)) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 251 | ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW); | 
| Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 252 | if (ret == -1) { | 
|  | 253 | return -errno; | 
|  | 254 | } | 
|  | 255 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
| Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 258 | if (e4crypt_is_native()) { | 
|  | 259 | if (e4crypt_set_directory_policy(args[1].c_str())) { | 
| Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 260 | const std::vector<std::string> options = { | 
|  | 261 | "--prompt_and_wipe_data", | 
|  | 262 | "--reason=set_policy_failed:"s + args[1]}; | 
|  | 263 | reboot_into_recovery(options); | 
| Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 264 | return -1; | 
|  | 265 | } | 
|  | 266 | } | 
|  | 267 | return 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 270 | /* umount <path> */ | 
|  | 271 | static int do_umount(const std::vector<std::string>& args) { | 
|  | 272 | return umount(args[1].c_str()); | 
|  | 273 | } | 
|  | 274 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 275 | static struct { | 
|  | 276 | const char *name; | 
|  | 277 | unsigned flag; | 
|  | 278 | } mount_flags[] = { | 
|  | 279 | { "noatime",    MS_NOATIME }, | 
| Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 280 | { "noexec",     MS_NOEXEC }, | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 281 | { "nosuid",     MS_NOSUID }, | 
|  | 282 | { "nodev",      MS_NODEV }, | 
|  | 283 | { "nodiratime", MS_NODIRATIME }, | 
|  | 284 | { "ro",         MS_RDONLY }, | 
|  | 285 | { "rw",         0 }, | 
|  | 286 | { "remount",    MS_REMOUNT }, | 
| Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 287 | { "bind",       MS_BIND }, | 
|  | 288 | { "rec",        MS_REC }, | 
|  | 289 | { "unbindable", MS_UNBINDABLE }, | 
|  | 290 | { "private",    MS_PRIVATE }, | 
|  | 291 | { "slave",      MS_SLAVE }, | 
|  | 292 | { "shared",     MS_SHARED }, | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 293 | { "defaults",   0 }, | 
|  | 294 | { 0,            0 }, | 
|  | 295 | }; | 
|  | 296 |  | 
| Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 297 | #define DATA_MNT_POINT "/data" | 
|  | 298 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 299 | /* mount <type> <device> <path> <flags ...> <options> */ | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 300 | static int do_mount(const std::vector<std::string>& args) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 301 | char tmp[64]; | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 302 | const char *source, *target, *system; | 
|  | 303 | const char *options = NULL; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 304 | unsigned flags = 0; | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 305 | std::size_t na = 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 306 | int n, i; | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 307 | int wait = 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 308 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 309 | for (na = 4; na < args.size(); na++) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | for (i = 0; mount_flags[i].name; i++) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 311 | if (!args[na].compare(mount_flags[i].name)) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 312 | flags |= mount_flags[i].flag; | 
|  | 313 | break; | 
|  | 314 | } | 
|  | 315 | } | 
|  | 316 |  | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 317 | if (!mount_flags[i].name) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 318 | if (!args[na].compare("wait")) | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 319 | wait = 1; | 
|  | 320 | /* if our last argument isn't a flag, wolf it up as an option string */ | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 321 | else if (na + 1 == args.size()) | 
|  | 322 | options = args[na].c_str(); | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 323 | } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 326 | system = args[1].c_str(); | 
|  | 327 | source = args[2].c_str(); | 
|  | 328 | target = args[3].c_str(); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 329 |  | 
| Elliott Hughes | 3195116 | 2016-06-24 15:15:03 -0700 | [diff] [blame] | 330 | if (!strncmp(source, "loop@", 5)) { | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 331 | int mode, loop, fd; | 
|  | 332 | struct loop_info info; | 
|  | 333 |  | 
|  | 334 | mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; | 
| Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 335 | fd = open(source + 5, mode | O_CLOEXEC); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 336 | if (fd < 0) { | 
|  | 337 | return -1; | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | for (n = 0; ; n++) { | 
| Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 341 | snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n); | 
| Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 342 | loop = open(tmp, mode | O_CLOEXEC); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 343 | if (loop < 0) { | 
| Tomasz Kondel | bfdcc40 | 2014-02-06 08:57:27 +0100 | [diff] [blame] | 344 | close(fd); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 345 | return -1; | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | /* if it is a blank loop device */ | 
|  | 349 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { | 
|  | 350 | /* if it becomes our loop device */ | 
|  | 351 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { | 
|  | 352 | close(fd); | 
|  | 353 |  | 
|  | 354 | if (mount(tmp, target, system, flags, options) < 0) { | 
|  | 355 | ioctl(loop, LOOP_CLR_FD, 0); | 
|  | 356 | close(loop); | 
|  | 357 | return -1; | 
|  | 358 | } | 
|  | 359 |  | 
|  | 360 | close(loop); | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 361 | goto exit_success; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 362 | } | 
|  | 363 | } | 
|  | 364 |  | 
|  | 365 | close(loop); | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | close(fd); | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 369 | LOG(ERROR) << "out of loopback devices"; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 370 | return -1; | 
|  | 371 | } else { | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 372 | if (wait) | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 373 | wait_for_file(source, kCommandRetryTimeout); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 374 | if (mount(source, target, system, flags, options) < 0) { | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 375 | return -1; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 378 | } | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 379 |  | 
|  | 380 | exit_success: | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 381 | return 0; | 
|  | 382 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 383 | } | 
|  | 384 |  | 
| Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 385 | /* Imports .rc files from the specified paths. Default ones are applied if none is given. | 
|  | 386 | * | 
|  | 387 | * start_index: index of the first path in the args list | 
|  | 388 | */ | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 389 | static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 390 | Parser& parser = Parser::GetInstance(); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 391 | if (end_index <= start_index) { | 
| Jaekyun Seok | 4ec72cc | 2017-02-22 20:37:57 +0900 | [diff] [blame] | 392 | // Fallbacks for partitions on which early mount isn't enabled. | 
|  | 393 | if (!parser.is_system_etc_init_loaded()) { | 
|  | 394 | parser.ParseConfig("/system/etc/init"); | 
|  | 395 | parser.set_is_system_etc_init_loaded(true); | 
|  | 396 | } | 
|  | 397 | if (!parser.is_vendor_etc_init_loaded()) { | 
|  | 398 | parser.ParseConfig("/vendor/etc/init"); | 
|  | 399 | parser.set_is_vendor_etc_init_loaded(true); | 
|  | 400 | } | 
|  | 401 | if (!parser.is_odm_etc_init_loaded()) { | 
|  | 402 | parser.ParseConfig("/odm/etc/init"); | 
|  | 403 | parser.set_is_odm_etc_init_loaded(true); | 
| Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 404 | } | 
|  | 405 | } else { | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 406 | for (size_t i = start_index; i < end_index; ++i) { | 
| Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 407 | parser.ParseConfig(args[i]); | 
|  | 408 | } | 
| Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 409 | } | 
| Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 410 |  | 
|  | 411 | // Turning this on and letting the INFO logging be discarded adds 0.2s to | 
|  | 412 | // Nexus 9 boot time, so it's disabled by default. | 
| Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 413 | if (false) DumpState(); | 
| Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 414 | } | 
|  | 415 |  | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 416 | /* mount_fstab | 
| Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 417 | * | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 418 | *  Call fs_mgr_mount_all() to mount the given fstab | 
| JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 419 | */ | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 420 | static int mount_fstab(const char* fstabfile, int mount_mode) { | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 421 | int ret = -1; | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 422 |  | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 423 | /* | 
|  | 424 | * Call fs_mgr_mount_all() to mount all filesystems.  We fork(2) and | 
|  | 425 | * do the call in the child to provide protection to the main init | 
|  | 426 | * process if anything goes wrong (crash or memory leak), and wait for | 
|  | 427 | * the child to finish in the parent. | 
|  | 428 | */ | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 429 | pid_t pid = fork(); | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 430 | if (pid > 0) { | 
|  | 431 | /* Parent.  Wait for the child to return */ | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 432 | int status; | 
| Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 433 | int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 434 | if (wp_ret == -1) { | 
|  | 435 | // Unexpected error code. We will continue anyway. | 
|  | 436 | PLOG(WARNING) << "waitpid failed"; | 
| Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 437 | } | 
|  | 438 |  | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 439 | if (WIFEXITED(status)) { | 
|  | 440 | ret = WEXITSTATUS(status); | 
|  | 441 | } else { | 
|  | 442 | ret = -1; | 
|  | 443 | } | 
|  | 444 | } else if (pid == 0) { | 
|  | 445 | /* child, call fs_mgr_mount_all() */ | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 446 |  | 
|  | 447 | // So we can always see what fs_mgr_mount_all() does. | 
|  | 448 | // Only needed if someone explicitly changes the default log level in their init.rc. | 
|  | 449 | android::base::ScopedLogSeverity info(android::base::INFO); | 
|  | 450 |  | 
|  | 451 | struct fstab* fstab = fs_mgr_read_fstab(fstabfile); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 452 | int child_ret = fs_mgr_mount_all(fstab, mount_mode); | 
| Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 453 | fs_mgr_free_fstab(fstab); | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 454 | if (child_ret == -1) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 455 | PLOG(ERROR) << "fs_mgr_mount_all returned an error"; | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 456 | } | 
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 457 | _exit(child_ret); | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 458 | } else { | 
|  | 459 | /* fork failed, return an error */ | 
|  | 460 | return -1; | 
|  | 461 | } | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 462 | return ret; | 
|  | 463 | } | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 464 |  | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 465 | /* Queue event based on fs_mgr return code. | 
|  | 466 | * | 
|  | 467 | * code: return code of fs_mgr_mount_all | 
|  | 468 | * | 
|  | 469 | * This function might request a reboot, in which case it will | 
|  | 470 | * not return. | 
|  | 471 | * | 
|  | 472 | * return code is processed based on input code | 
|  | 473 | */ | 
|  | 474 | static int queue_fs_event(int code) { | 
|  | 475 | int ret = code; | 
|  | 476 | if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { | 
| Paul Lawrence | 1f99218 | 2016-04-18 15:37:31 -0700 | [diff] [blame] | 477 | ActionManager::GetInstance().QueueEventTrigger("encrypt"); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 478 | } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 479 | property_set("ro.crypto.state", "encrypted"); | 
| Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 480 | property_set("ro.crypto.type", "block"); | 
| Paul Lawrence | 1f99218 | 2016-04-18 15:37:31 -0700 | [diff] [blame] | 481 | ActionManager::GetInstance().QueueEventTrigger("defaultcrypto"); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 482 | } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 483 | property_set("ro.crypto.state", "unencrypted"); | 
| Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 484 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 485 | } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) { | 
| Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 486 | property_set("ro.crypto.state", "unsupported"); | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 487 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 488 | } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { | 
| JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 489 | /* Setup a wipe via recovery, and reboot into recovery */ | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 490 | PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery."; | 
| Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 491 | const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" }; | 
|  | 492 | ret = reboot_into_recovery(options); | 
| JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 493 | /* If reboot worked, there is no return. */ | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 494 | } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) { | 
| Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 495 | if (e4crypt_install_keyring()) { | 
|  | 496 | return -1; | 
|  | 497 | } | 
|  | 498 | property_set("ro.crypto.state", "encrypted"); | 
| Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 499 | property_set("ro.crypto.type", "file"); | 
| Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 500 |  | 
|  | 501 | // Although encrypted, we have device key, so we do not need to | 
|  | 502 | // do anything different from the nonencrypted case. | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 503 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 504 | } else if (code > 0) { | 
|  | 505 | PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code; | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 506 | } | 
| JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 507 | /* else ... < 0: error */ | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 508 |  | 
|  | 509 | return ret; | 
|  | 510 | } | 
|  | 511 |  | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 512 | /* mount_all <fstab> [ <path> ]* [--<options>]* | 
|  | 513 | * | 
|  | 514 | * This function might request a reboot, in which case it will | 
|  | 515 | * not return. | 
|  | 516 | */ | 
|  | 517 | static int do_mount_all(const std::vector<std::string>& args) { | 
|  | 518 | std::size_t na = 0; | 
|  | 519 | bool import_rc = true; | 
|  | 520 | bool queue_event = true; | 
|  | 521 | int mount_mode = MOUNT_MODE_DEFAULT; | 
|  | 522 | const char* fstabfile = args[1].c_str(); | 
|  | 523 | std::size_t path_arg_end = args.size(); | 
| Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 524 | const char* prop_post_fix = "default"; | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 525 |  | 
|  | 526 | for (na = args.size() - 1; na > 1; --na) { | 
|  | 527 | if (args[na] == "--early") { | 
| Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 528 | path_arg_end = na; | 
|  | 529 | queue_event = false; | 
|  | 530 | mount_mode = MOUNT_MODE_EARLY; | 
| Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 531 | prop_post_fix = "early"; | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 532 | } else if (args[na] == "--late") { | 
|  | 533 | path_arg_end = na; | 
|  | 534 | import_rc = false; | 
|  | 535 | mount_mode = MOUNT_MODE_LATE; | 
| Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 536 | prop_post_fix = "late"; | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 537 | } | 
|  | 538 | } | 
|  | 539 |  | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 540 | std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; | 
| Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 541 | Timer t; | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 542 | int ret =  mount_fstab(fstabfile, mount_mode); | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 543 | property_set(prop_name, std::to_string(t.duration_ms())); | 
| Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 544 |  | 
|  | 545 | if (import_rc) { | 
|  | 546 | /* Paths of .rc files are specified at the 2nd argument and beyond */ | 
|  | 547 | import_late(args, 2, path_arg_end); | 
|  | 548 | } | 
|  | 549 |  | 
|  | 550 | if (queue_event) { | 
|  | 551 | /* queue_fs_event will queue event based on mount_fstab return code | 
|  | 552 | * and return processed return code*/ | 
|  | 553 | ret = queue_fs_event(ret); | 
|  | 554 | } | 
|  | 555 |  | 
|  | 556 | return ret; | 
|  | 557 | } | 
|  | 558 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 559 | static int do_swapon_all(const std::vector<std::string>& args) { | 
| Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 560 | struct fstab *fstab; | 
|  | 561 | int ret; | 
|  | 562 |  | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 563 | fstab = fs_mgr_read_fstab(args[1].c_str()); | 
| Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 564 | ret = fs_mgr_swapon_all(fstab); | 
|  | 565 | fs_mgr_free_fstab(fstab); | 
|  | 566 |  | 
|  | 567 | return ret; | 
|  | 568 | } | 
|  | 569 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 570 | static int do_setprop(const std::vector<std::string>& args) { | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 571 | property_set(args[1], args[2]); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 572 | return 0; | 
|  | 573 | } | 
|  | 574 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 575 | static int do_setrlimit(const std::vector<std::string>& args) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 576 | struct rlimit limit; | 
|  | 577 | int resource; | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 578 | if (android::base::ParseInt(args[1], &resource) && | 
|  | 579 | android::base::ParseUint(args[2], &limit.rlim_cur) && | 
|  | 580 | android::base::ParseUint(args[3], &limit.rlim_max)) { | 
|  | 581 | return setrlimit(resource, &limit); | 
|  | 582 | } | 
|  | 583 | LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3]; | 
|  | 584 | return -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 585 | } | 
|  | 586 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 587 | static int do_start(const std::vector<std::string>& args) { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 588 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); | 
|  | 589 | if (!svc) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 590 | LOG(ERROR) << "do_start: Service " << args[1] << " not found"; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 591 | return -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 592 | } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 593 | if (!svc->Start()) | 
|  | 594 | return -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 595 | return 0; | 
|  | 596 | } | 
|  | 597 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 598 | static int do_stop(const std::vector<std::string>& args) { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 599 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); | 
|  | 600 | if (!svc) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 601 | LOG(ERROR) << "do_stop: Service " << args[1] << " not found"; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 602 | return -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 603 | } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 604 | svc->Stop(); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 605 | return 0; | 
|  | 606 | } | 
|  | 607 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 608 | static int do_restart(const std::vector<std::string>& args) { | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 609 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); | 
|  | 610 | if (!svc) { | 
| Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 611 | LOG(ERROR) << "do_restart: Service " << args[1] << " not found"; | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 612 | return -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 613 | } | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 614 | svc->Restart(); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 615 | return 0; | 
|  | 616 | } | 
|  | 617 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 618 | static int do_trigger(const std::vector<std::string>& args) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 619 | ActionManager::GetInstance().QueueEventTrigger(args[1]); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 620 | return 0; | 
|  | 621 | } | 
|  | 622 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 623 | static int do_symlink(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 624 | return symlink(args[1].c_str(), args[2].c_str()); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 625 | } | 
|  | 626 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 627 | static int do_rm(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 628 | return unlink(args[1].c_str()); | 
| Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 629 | } | 
|  | 630 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 631 | static int do_rmdir(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 632 | return rmdir(args[1].c_str()); | 
| Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 633 | } | 
|  | 634 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 635 | static int do_sysclktz(const std::vector<std::string>& args) { | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 636 | struct timezone tz = {}; | 
|  | 637 | if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) { | 
|  | 638 | return 0; | 
|  | 639 | } | 
|  | 640 | return -1; | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 641 | } | 
|  | 642 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 643 | static int do_verity_load_state(const std::vector<std::string>& args) { | 
| Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 644 | int mode = -1; | 
| Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 645 | bool loaded = fs_mgr_load_verity_state(&mode); | 
|  | 646 | if (loaded && mode != VERITY_MODE_DEFAULT) { | 
| Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 647 | ActionManager::GetInstance().QueueEventTrigger("verity-logging"); | 
| Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 648 | } | 
| Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 649 | return loaded ? 0 : 1; | 
| Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 650 | } | 
|  | 651 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 652 | static void verity_update_property(fstab_rec *fstab, const char *mount_point, | 
|  | 653 | int mode, int status) { | 
| Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 654 | property_set("partition."s + mount_point + ".verified", std::to_string(mode)); | 
| Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 655 | } | 
|  | 656 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 657 | static int do_verity_update_state(const std::vector<std::string>& args) { | 
| Bowgo Tsai | aaf70e7 | 2017-03-02 00:03:56 +0800 | [diff] [blame] | 658 | return fs_mgr_update_verity_state(verity_update_property) ? 0 : 1; | 
| Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 659 | } | 
|  | 660 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 661 | static int do_write(const std::vector<std::string>& args) { | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 662 | std::string err; | 
|  | 663 | if (!WriteFile(args[1], args[2], &err)) { | 
|  | 664 | LOG(ERROR) << err; | 
|  | 665 | return -1; | 
|  | 666 | } | 
|  | 667 | return 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 668 | } | 
|  | 669 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 670 | static int do_copy(const std::vector<std::string>& args) { | 
| Yongqin Liu | dbe88e7 | 2016-12-28 16:06:19 +0800 | [diff] [blame] | 671 | std::string data; | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 672 | std::string err; | 
|  | 673 | if (!ReadFile(args[1], &data, &err)) { | 
|  | 674 | LOG(ERROR) << err; | 
|  | 675 | return -1; | 
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 676 | } | 
| Tom Cherry | 2cbbe9f | 2017-05-04 18:17:33 -0700 | [diff] [blame] | 677 | if (!WriteFile(args[2], data, &err)) { | 
|  | 678 | LOG(ERROR) << err; | 
|  | 679 | return -1; | 
|  | 680 | } | 
|  | 681 | return 0; | 
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 682 | } | 
|  | 683 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 684 | static int do_chown(const std::vector<std::string>& args) { | 
| Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 685 | uid_t uid; | 
|  | 686 | std::string decode_uid_err; | 
|  | 687 | if (!DecodeUid(args[1], &uid, &decode_uid_err)) { | 
|  | 688 | LOG(ERROR) << "Unable to find UID for '" << args[1] << "': " << decode_uid_err; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 689 | return -1; | 
|  | 690 | } | 
| Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 691 |  | 
|  | 692 | // GID is optional and pushes the index of path out by one if specified. | 
|  | 693 | const std::string& path = (args.size() == 4) ? args[3] : args[2]; | 
|  | 694 | gid_t gid = -1; | 
|  | 695 |  | 
|  | 696 | if (args.size() == 4) { | 
|  | 697 | if (!DecodeUid(args[2], &gid, &decode_uid_err)) { | 
|  | 698 | LOG(ERROR) << "Unable to find GID for '" << args[2] << "': " << decode_uid_err; | 
|  | 699 | return -1; | 
|  | 700 | } | 
|  | 701 | } | 
|  | 702 |  | 
|  | 703 | if (lchown(path.c_str(), uid, gid) == -1) return -errno; | 
|  | 704 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 705 | return 0; | 
|  | 706 | } | 
|  | 707 |  | 
|  | 708 | static mode_t get_mode(const char *s) { | 
|  | 709 | mode_t mode = 0; | 
|  | 710 | while (*s) { | 
|  | 711 | if (*s >= '0' && *s <= '7') { | 
|  | 712 | mode = (mode<<3) | (*s-'0'); | 
|  | 713 | } else { | 
|  | 714 | return -1; | 
|  | 715 | } | 
|  | 716 | s++; | 
|  | 717 | } | 
|  | 718 | return mode; | 
|  | 719 | } | 
|  | 720 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 721 | static int do_chmod(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 722 | mode_t mode = get_mode(args[1].c_str()); | 
|  | 723 | if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 724 | return -errno; | 
|  | 725 | } | 
|  | 726 | return 0; | 
|  | 727 | } | 
|  | 728 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 729 | static int do_restorecon(const std::vector<std::string>& args) { | 
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 730 | int ret = 0; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 731 |  | 
| Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 732 | struct flag_type {const char* name; int value;}; | 
|  | 733 | static const flag_type flags[] = { | 
|  | 734 | {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE}, | 
|  | 735 | {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE}, | 
|  | 736 | {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS}, | 
|  | 737 | {0, 0} | 
|  | 738 | }; | 
|  | 739 |  | 
|  | 740 | int flag = 0; | 
|  | 741 |  | 
|  | 742 | bool in_flags = true; | 
|  | 743 | for (size_t i = 1; i < args.size(); ++i) { | 
|  | 744 | if (android::base::StartsWith(args[i], "--")) { | 
|  | 745 | if (!in_flags) { | 
|  | 746 | LOG(ERROR) << "restorecon - flags must precede paths"; | 
|  | 747 | return -1; | 
|  | 748 | } | 
|  | 749 | bool found = false; | 
|  | 750 | for (size_t j = 0; flags[j].name; ++j) { | 
|  | 751 | if (args[i] == flags[j].name) { | 
|  | 752 | flag |= flags[j].value; | 
|  | 753 | found = true; | 
|  | 754 | break; | 
|  | 755 | } | 
|  | 756 | } | 
|  | 757 | if (!found) { | 
|  | 758 | LOG(ERROR) << "restorecon - bad flag " << args[i]; | 
|  | 759 | return -1; | 
|  | 760 | } | 
|  | 761 | } else { | 
|  | 762 | in_flags = false; | 
| Tom Cherry | 482f36c | 2017-05-08 13:38:15 -0700 | [diff] [blame] | 763 | if (selinux_android_restorecon(args[i].c_str(), flag) < 0) { | 
| Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 764 | ret = -errno; | 
|  | 765 | } | 
|  | 766 | } | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 767 | } | 
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 768 | return ret; | 
|  | 769 | } | 
|  | 770 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 771 | static int do_restorecon_recursive(const std::vector<std::string>& args) { | 
| Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 772 | std::vector<std::string> non_const_args(args); | 
|  | 773 | non_const_args.insert(std::next(non_const_args.begin()), "--recursive"); | 
|  | 774 | return do_restorecon(non_const_args); | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 775 | } | 
|  | 776 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 777 | static int do_loglevel(const std::vector<std::string>& args) { | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 778 | // TODO: support names instead/as well? | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 779 | int log_level = -1; | 
|  | 780 | android::base::ParseInt(args[1], &log_level); | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 781 | android::base::LogSeverity severity; | 
|  | 782 | switch (log_level) { | 
|  | 783 | case 7: severity = android::base::DEBUG; break; | 
|  | 784 | case 6: severity = android::base::INFO; break; | 
|  | 785 | case 5: | 
|  | 786 | case 4: severity = android::base::WARNING; break; | 
|  | 787 | case 3: severity = android::base::ERROR; break; | 
|  | 788 | case 2: | 
|  | 789 | case 1: | 
|  | 790 | case 0: severity = android::base::FATAL; break; | 
|  | 791 | default: | 
|  | 792 | LOG(ERROR) << "loglevel: invalid log level " << log_level; | 
|  | 793 | return -EINVAL; | 
| Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 794 | } | 
| Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 795 | android::base::SetMinimumLogSeverity(severity); | 
| Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 796 | return 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 797 | } | 
|  | 798 |  | 
| Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 799 | static int do_load_persist_props(const std::vector<std::string>& args) { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 800 | load_persist_props(); | 
|  | 801 | return 0; | 
| Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 802 | } | 
|  | 803 |  | 
| Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 804 | static int do_load_system_props(const std::vector<std::string>& args) { | 
|  | 805 | load_system_props(); | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 806 | return 0; | 
| Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 807 | } | 
|  | 808 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 809 | static int do_wait(const std::vector<std::string>& args) { | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 810 | if (args.size() == 2) { | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 811 | return wait_for_file(args[1].c_str(), kCommandRetryTimeout); | 
| Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 812 | } else if (args.size() == 3) { | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 813 | int timeout; | 
|  | 814 | if (android::base::ParseInt(args[2], &timeout)) { | 
| Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 815 | return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout)); | 
| Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 816 | } | 
|  | 817 | } | 
|  | 818 | return -1; | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 819 | } | 
| Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 820 |  | 
| Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 821 | static int do_wait_for_prop(const std::vector<std::string>& args) { | 
|  | 822 | const char* name = args[1].c_str(); | 
|  | 823 | const char* value = args[2].c_str(); | 
|  | 824 | size_t value_len = strlen(value); | 
|  | 825 |  | 
|  | 826 | if (!is_legal_property_name(name)) { | 
|  | 827 | LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value | 
|  | 828 | << "\") failed: bad name"; | 
|  | 829 | return -1; | 
|  | 830 | } | 
|  | 831 | if (value_len >= PROP_VALUE_MAX) { | 
|  | 832 | LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value | 
|  | 833 | << "\") failed: value too long"; | 
|  | 834 | return -1; | 
|  | 835 | } | 
| Wei Wang | 2d0fdaa | 2017-02-02 10:52:39 -0800 | [diff] [blame] | 836 | if (!start_waiting_for_property(name, value)) { | 
| Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 837 | LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value | 
|  | 838 | << "\") failed: init already in waiting"; | 
|  | 839 | return -1; | 
|  | 840 | } | 
|  | 841 | return 0; | 
|  | 842 | } | 
|  | 843 |  | 
| Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 844 | /* | 
|  | 845 | * Callback to make a directory from the ext4 code | 
|  | 846 | */ | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 847 | static int do_installkeys_ensure_dir_exists(const char* dir) { | 
| Tom Cherry | e7656b7 | 2017-05-01 17:10:09 -0700 | [diff] [blame] | 848 | if (make_dir(dir, 0700, sehandle) && errno != EEXIST) { | 
| Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 849 | return -1; | 
| Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 850 | } | 
|  | 851 |  | 
| Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 852 | return 0; | 
|  | 853 | } | 
|  | 854 |  | 
| Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 855 | static bool is_file_crypto() { | 
| Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 856 | return android::base::GetProperty("ro.crypto.type", "") == "file"; | 
| Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 857 | } | 
|  | 858 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 859 | static int do_installkey(const std::vector<std::string>& args) { | 
| Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 860 | if (!is_file_crypto()) { | 
| Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 861 | return 0; | 
|  | 862 | } | 
| Janis Danisevskis | 9cc5172 | 2017-03-29 14:50:01 -0700 | [diff] [blame] | 863 | auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder; | 
|  | 864 | if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) { | 
|  | 865 | PLOG(ERROR) << "Failed to create " << unencrypted_dir; | 
|  | 866 | return -1; | 
|  | 867 | } | 
|  | 868 | std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", | 
|  | 869 | "enablefilecrypto"}; | 
|  | 870 | return do_exec(exec_args); | 
| Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 871 | } | 
| Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 872 |  | 
| Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 873 | static int do_init_user0(const std::vector<std::string>& args) { | 
| Tom Cherry | 5a86cb7 | 2017-05-03 13:19:03 -0700 | [diff] [blame] | 874 | std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", | 
|  | 875 | "init_user0"}; | 
|  | 876 | return do_exec(exec_args); | 
| Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 877 | } | 
|  | 878 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 879 | const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const { | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 880 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); | 
| Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 881 | // clang-format off | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 882 | static const Map builtin_functions = { | 
| Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 883 | {"bootchart",               {1,     1,    do_bootchart}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 884 | {"chmod",                   {2,     2,    do_chmod}}, | 
|  | 885 | {"chown",                   {2,     3,    do_chown}}, | 
|  | 886 | {"class_reset",             {1,     1,    do_class_reset}}, | 
| Steven Moreland | 2b63d54 | 2017-03-10 14:04:37 -0800 | [diff] [blame] | 887 | {"class_restart",           {1,     1,    do_class_restart}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 888 | {"class_start",             {1,     1,    do_class_start}}, | 
|  | 889 | {"class_stop",              {1,     1,    do_class_stop}}, | 
|  | 890 | {"copy",                    {2,     2,    do_copy}}, | 
|  | 891 | {"domainname",              {1,     1,    do_domainname}}, | 
|  | 892 | {"enable",                  {1,     1,    do_enable}}, | 
|  | 893 | {"exec",                    {1,     kMax, do_exec}}, | 
| Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 894 | {"exec_start",              {1,     1,    do_exec_start}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 895 | {"export",                  {2,     2,    do_export}}, | 
|  | 896 | {"hostname",                {1,     1,    do_hostname}}, | 
|  | 897 | {"ifup",                    {1,     1,    do_ifup}}, | 
| Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 898 | {"init_user0",              {0,     0,    do_init_user0}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 899 | {"insmod",                  {1,     kMax, do_insmod}}, | 
|  | 900 | {"installkey",              {1,     1,    do_installkey}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 901 | {"load_persist_props",      {0,     0,    do_load_persist_props}}, | 
| Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 902 | {"load_system_props",       {0,     0,    do_load_system_props}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 903 | {"loglevel",                {1,     1,    do_loglevel}}, | 
|  | 904 | {"mkdir",                   {1,     4,    do_mkdir}}, | 
| Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 905 | {"mount_all",               {1,     kMax, do_mount_all}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 906 | {"mount",                   {3,     kMax, do_mount}}, | 
| Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 907 | {"umount",                  {1,     1,    do_umount}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 908 | {"restart",                 {1,     1,    do_restart}}, | 
|  | 909 | {"restorecon",              {1,     kMax, do_restorecon}}, | 
|  | 910 | {"restorecon_recursive",    {1,     kMax, do_restorecon_recursive}}, | 
|  | 911 | {"rm",                      {1,     1,    do_rm}}, | 
|  | 912 | {"rmdir",                   {1,     1,    do_rmdir}}, | 
|  | 913 | {"setprop",                 {2,     2,    do_setprop}}, | 
|  | 914 | {"setrlimit",               {3,     3,    do_setrlimit}}, | 
|  | 915 | {"start",                   {1,     1,    do_start}}, | 
|  | 916 | {"stop",                    {1,     1,    do_stop}}, | 
|  | 917 | {"swapon_all",              {1,     1,    do_swapon_all}}, | 
|  | 918 | {"symlink",                 {2,     2,    do_symlink}}, | 
|  | 919 | {"sysclktz",                {1,     1,    do_sysclktz}}, | 
|  | 920 | {"trigger",                 {1,     1,    do_trigger}}, | 
|  | 921 | {"verity_load_state",       {0,     0,    do_verity_load_state}}, | 
|  | 922 | {"verity_update_state",     {0,     0,    do_verity_update_state}}, | 
|  | 923 | {"wait",                    {1,     2,    do_wait}}, | 
| Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 924 | {"wait_for_prop",           {2,     2,    do_wait_for_prop}}, | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 925 | {"write",                   {2,     2,    do_write}}, | 
|  | 926 | }; | 
| Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 927 | // clang-format on | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 928 | return builtin_functions; | 
|  | 929 | } | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 930 |  | 
|  | 931 | }  // namespace init | 
|  | 932 | }  // namespace android |