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