blob: 2d4d78bc79b8bb2dcf38e628a6dbd4eb96ef1f80 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
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 Cherryb7349902015-08-26 11:43:36 -070017#include "builtins.h"
18
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070019#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070021#include <fcntl.h>
Wei Wang542aae42017-08-04 13:22:36 -070022#include <fts.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070023#include <linux/loop.h>
24#include <linux/module.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070025#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <net/if.h>
Mark Salyzynad575e02016-04-05 08:10:25 -070027#include <sched.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070028#include <signal.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070029#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070031#include <string.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070032#include <sys/mount.h>
33#include <sys/resource.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070034#include <sys/socket.h>
35#include <sys/stat.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070036#include <sys/syscall.h>
Tom Cherryf57c0bf2017-04-07 13:46:21 -070037#include <sys/system_properties.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080038#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070039#include <sys/types.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070040#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070041#include <unistd.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042
Tom Cherryede0d532017-07-06 14:20:11 -070043#include <android-base/chrono_utils.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070044#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070045#include <android-base/logging.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080046#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070047#include <android-base/properties.h>
Tom Cherry70379912017-08-01 14:10:11 -070048#include <android-base/stringprintf.h>
Tom Cherryccf23532017-03-28 16:40:41 -070049#include <android-base/strings.h>
Tom Cherry70379912017-08-01 14:10:11 -070050#include <android-base/unique_fd.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070051#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070052#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070053#include <ext4_utils/ext4_crypt.h>
54#include <ext4_utils/ext4_crypt_init_extensions.h>
Tom Cherryccf23532017-03-28 16:40:41 -070055#include <fs_mgr.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070056#include <selinux/android.h>
57#include <selinux/label.h>
58#include <selinux/selinux.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070059
Tom Cherryfa0c21c2015-07-23 17:53:11 -070060#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070061#include "bootchart.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "init.h"
Tom Cherry67dee622017-07-27 12:54:48 -070063#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070064#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070065#include "reboot.h"
Tom Cherrybac32992015-07-31 12:45:25 -070066#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080067#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070068#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069
Paul Crowley0b8b2302016-12-19 13:03:47 -080070using namespace std::literals::string_literals;
71
Tom Cherry70379912017-08-01 14:10:11 -070072using android::base::unique_fd;
73
Nick Kralevichbc609542015-01-31 21:39:46 -080074#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
75
Tom Cherry81f5d3e2017-06-22 12:53:17 -070076namespace android {
77namespace init {
78
Elliott Hughes9605a942016-11-10 17:43:47 -080079static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080080
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080081static int insmod(const char *filename, const char *options, int flags) {
Tom Cherry70379912017-08-01 14:10:11 -070082 unique_fd fd(TEMP_FAILURE_RETRY(open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
Nick Kralevich124a9c92016-03-27 16:55:59 -070083 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070084 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080086 }
Tom Cherry70379912017-08-01 14:10:11 -070087 int rc = syscall(__NR_finit_module, fd.get(), options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070088 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070089 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070090 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070091 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092}
93
Tom Cherryb7349902015-08-26 11:43:36 -070094static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095 struct ifreq ifr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070096
97 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
98
Tom Cherry70379912017-08-01 14:10:11 -070099 unique_fd s(TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_DGRAM, 0)));
100 if (s < 0) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101
Tom Cherry70379912017-08-01 14:10:11 -0700102 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 Project4f6e8d72008-10-21 07:00:00 -0700109 }
110
Tom Cherry70379912017-08-01 14:10:11 -0700111 return ioctl(s, SIOCSIFFLAGS, &ifr);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112}
113
Paul Crowley0b8b2302016-12-19 13:03:47 -0800114static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700115 std::string err;
116 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700117 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700118 return -1;
119 }
Wei Wangeeab4912017-06-27 22:08:45 -0700120 property_set("sys.powerctl", "reboot,recovery");
Keun-young Park8d01f632017-03-13 11:54:47 -0700121 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700122}
123
Tom Cherry911b9b12017-07-27 16:20:58 -0700124template <typename F>
125static 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 Cherryb7349902015-08-26 11:43:36 -0700131static int do_class_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700132 // 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 Project4f6e8d72008-10-21 07:00:00 -0700135 return 0;
136}
137
Tom Cherryb7349902015-08-26 11:43:36 -0700138static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700139 ForEachServiceInClass(args[1], &Service::Stop);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140 return 0;
141}
142
Tom Cherryb7349902015-08-26 11:43:36 -0700143static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700144 ForEachServiceInClass(args[1], &Service::Reset);
Ken Sumrall752923c2010-12-03 16:33:31 -0800145 return 0;
146}
147
Steven Moreland2b63d542017-03-10 14:04:37 -0800148static int do_class_restart(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700149 ForEachServiceInClass(args[1], &Service::Restart);
Steven Moreland2b63d542017-03-10 14:04:37 -0800150 return 0;
151}
152
Tom Cherryb7349902015-08-26 11:43:36 -0700153static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700154 if (auto result = WriteFile("/proc/sys/kernel/domainname", args[1]); !result) {
155 LOG(ERROR) << "Unable to write to /proc/sys/kernel/domainname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700156 return -1;
157 }
158 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159}
160
Tom Cherryb7349902015-08-26 11:43:36 -0700161static int do_enable(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700162 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700163 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700164 return -1;
165 }
Tom Cherrybac32992015-07-31 12:45:25 -0700166 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700167}
168
Tom Cherryb7349902015-08-26 11:43:36 -0700169static int do_exec(const std::vector<std::string>& args) {
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700170 auto service = Service::MakeTemporaryOneshotService(args);
171 if (!service) {
172 LOG(ERROR) << "Failed to create exec service: " << android::base::Join(args, " ");
173 return -1;
174 }
175 if (!service->ExecStart()) {
176 LOG(ERROR) << "Failed to Start exec service";
177 return -1;
178 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700179 ServiceList::GetInstance().AddService(std::move(service));
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700180 return 0;
Tom Cherryb27004a2017-03-27 16:27:30 -0700181}
182
183static int do_exec_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700184 Service* service = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700185 if (!service) {
186 LOG(ERROR) << "ExecStart(" << args[1] << "): Service not found";
187 return -1;
188 }
189 if (!service->ExecStart()) {
190 LOG(ERROR) << "ExecStart(" << args[1] << "): Could not start Service";
191 return -1;
192 }
193 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194}
195
Tom Cherryb7349902015-08-26 11:43:36 -0700196static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700197 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198}
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700201 if (auto result = WriteFile("/proc/sys/kernel/hostname", args[1]); !result) {
202 LOG(ERROR) << "Unable to write to /proc/sys/kernel/hostname: " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700203 return -1;
204 }
205 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206}
207
Tom Cherryb7349902015-08-26 11:43:36 -0700208static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700209 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210}
211
Tom Cherryb7349902015-08-26 11:43:36 -0700212static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800213 int flags = 0;
214 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800215
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800216 if (!(*it).compare("-f")) {
217 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
218 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800219 }
220
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800221 std::string filename = *it++;
222 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
223 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800224}
225
Tom Cherryc3692b32017-08-10 12:22:44 -0700226// mkdir <path> [mode] [owner] [group]
Tom Cherryb7349902015-08-26 11:43:36 -0700227static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228 mode_t mode = 0755;
Tom Cherry96f67312015-07-30 13:52:55 -0700229 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700230 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231 }
232
Tom Cherryc3692b32017-08-10 12:22:44 -0700233 if (!make_dir(args[1], mode)) {
234 /* chmod in case the directory already exists */
235 if (errno == EEXIST) {
236 if (fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW) == -1) {
237 return -errno;
238 }
239 } else {
240 return -errno;
241 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242 }
243
Tom Cherry96f67312015-07-30 13:52:55 -0700244 if (args.size() >= 4) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700245 auto uid = DecodeUid(args[3]);
246 if (!uid) {
247 LOG(ERROR) << "Unable to decode UID for '" << args[3] << "': " << uid.error();
Tom Cherry517e1f12017-05-04 17:40:33 -0700248 return -1;
249 }
Tom Cherry62ca6632017-08-03 12:54:07 -0700250 Result<gid_t> gid = -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251
Tom Cherry96f67312015-07-30 13:52:55 -0700252 if (args.size() == 5) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700253 gid = DecodeUid(args[4]);
254 if (!gid) {
255 LOG(ERROR) << "Unable to decode GID for '" << args[3] << "': " << gid.error();
Tom Cherry517e1f12017-05-04 17:40:33 -0700256 return -1;
257 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258 }
259
Tom Cherry62ca6632017-08-03 12:54:07 -0700260 if (lchown(args[1].c_str(), *uid, *gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261 return -errno;
262 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700263
264 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
265 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherryc3692b32017-08-10 12:22:44 -0700266 if (fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW) == -1) {
Benoit Goby5c8574b2012-08-14 15:43:46 -0700267 return -errno;
268 }
269 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270 }
271
Paul Crowleyaf8be582016-05-10 08:52:06 -0700272 if (e4crypt_is_native()) {
273 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800274 const std::vector<std::string> options = {
275 "--prompt_and_wipe_data",
276 "--reason=set_policy_failed:"s + args[1]};
277 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700278 return -1;
279 }
280 }
281 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282}
283
Alex Light68ab20f2016-06-23 11:11:39 -0700284/* umount <path> */
285static int do_umount(const std::vector<std::string>& args) {
286 return umount(args[1].c_str());
287}
288
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289static struct {
290 const char *name;
291 unsigned flag;
292} mount_flags[] = {
293 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200294 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 { "nosuid", MS_NOSUID },
296 { "nodev", MS_NODEV },
297 { "nodiratime", MS_NODIRATIME },
298 { "ro", MS_RDONLY },
299 { "rw", 0 },
300 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700301 { "bind", MS_BIND },
302 { "rec", MS_REC },
303 { "unbindable", MS_UNBINDABLE },
304 { "private", MS_PRIVATE },
305 { "slave", MS_SLAVE },
306 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 { "defaults", 0 },
308 { 0, 0 },
309};
310
Ken Sumrall752923c2010-12-03 16:33:31 -0800311#define DATA_MNT_POINT "/data"
312
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700314static int do_mount(const std::vector<std::string>& args) {
Tom Cherry70379912017-08-01 14:10:11 -0700315 const char* options = nullptr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 unsigned flags = 0;
Tom Cherry70379912017-08-01 14:10:11 -0700317 bool wait = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318
Tom Cherry70379912017-08-01 14:10:11 -0700319 for (size_t na = 4; na < args.size(); na++) {
320 size_t i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700322 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 flags |= mount_flags[i].flag;
324 break;
325 }
326 }
327
Colin Crosscd0f1732010-04-19 17:10:24 -0700328 if (!mount_flags[i].name) {
Tom Cherry70379912017-08-01 14:10:11 -0700329 if (!args[na].compare("wait")) {
330 wait = true;
331 // If our last argument isn't a flag, wolf it up as an option string.
332 } else if (na + 1 == args.size()) {
Tom Cherry96f67312015-07-30 13:52:55 -0700333 options = args[na].c_str();
Tom Cherry70379912017-08-01 14:10:11 -0700334 }
Colin Crosscd0f1732010-04-19 17:10:24 -0700335 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 }
337
Tom Cherry70379912017-08-01 14:10:11 -0700338 const char* system = args[1].c_str();
339 const char* source = args[2].c_str();
340 const char* target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000341
Tom Cherry70379912017-08-01 14:10:11 -0700342 if (android::base::StartsWith(source, "loop@")) {
343 int mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
344 unique_fd fd(TEMP_FAILURE_RETRY(open(source + 5, mode | O_CLOEXEC)));
345 if (fd < 0) return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000346
Tom Cherry70379912017-08-01 14:10:11 -0700347 for (size_t n = 0;; n++) {
348 std::string tmp = android::base::StringPrintf("/dev/block/loop%zu", n);
349 unique_fd loop(TEMP_FAILURE_RETRY(open(tmp.c_str(), mode | O_CLOEXEC)));
350 if (loop < 0) return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000351
Tom Cherry70379912017-08-01 14:10:11 -0700352 loop_info info;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000353 /* if it is a blank loop device */
354 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
355 /* if it becomes our loop device */
Tom Cherry70379912017-08-01 14:10:11 -0700356 if (ioctl(loop, LOOP_SET_FD, fd.get()) >= 0) {
357 if (mount(tmp.c_str(), target, system, flags, options) < 0) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000358 ioctl(loop, LOOP_CLR_FD, 0);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359 return -1;
360 }
Tom Cherry70379912017-08-01 14:10:11 -0700361 return 0;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 }
363 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 }
365
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700366 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 return -1;
368 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700369 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800370 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000371 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700372 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373 }
374
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800376
Ken Sumralldd4d7862011-02-17 18:09:47 -0800377 return 0;
378
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379}
380
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800381/* Imports .rc files from the specified paths. Default ones are applied if none is given.
382 *
383 * start_index: index of the first path in the args list
384 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700385static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherry67dee622017-07-27 12:54:48 -0700386 auto& action_manager = ActionManager::GetInstance();
Tom Cherry911b9b12017-07-27 16:20:58 -0700387 auto& service_list = ServiceList::GetInstance();
388 Parser parser = CreateParser(action_manager, service_list);
Wei Wangd61a7e22016-08-23 11:58:09 -0700389 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900390 // Fallbacks for partitions on which early mount isn't enabled.
Tom Cherry67dee622017-07-27 12:54:48 -0700391 for (const auto& path : late_import_paths) {
392 parser.ParseConfig(path);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900393 }
Tom Cherry67dee622017-07-27 12:54:48 -0700394 late_import_paths.clear();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800395 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700396 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800397 parser.ParseConfig(args[i]);
398 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700399 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700400
401 // Turning this on and letting the INFO logging be discarded adds 0.2s to
402 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700403 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700404}
405
Wei Wangd61a7e22016-08-23 11:58:09 -0700406/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800407 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700408 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700409 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700410static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700411 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700412
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700413 /*
414 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
415 * do the call in the child to provide protection to the main init
416 * process if anything goes wrong (crash or memory leak), and wait for
417 * the child to finish in the parent.
418 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700419 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700420 if (pid > 0) {
421 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700422 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700423 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700424 if (wp_ret == -1) {
425 // Unexpected error code. We will continue anyway.
426 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700427 }
428
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700429 if (WIFEXITED(status)) {
430 ret = WEXITSTATUS(status);
431 } else {
432 ret = -1;
433 }
434 } else if (pid == 0) {
435 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700436
437 // So we can always see what fs_mgr_mount_all() does.
438 // Only needed if someone explicitly changes the default log level in their init.rc.
439 android::base::ScopedLogSeverity info(android::base::INFO);
440
441 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700442 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800443 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700444 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700445 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700446 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700447 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700448 } else {
449 /* fork failed, return an error */
450 return -1;
451 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700452 return ret;
453}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700454
Wei Wangd61a7e22016-08-23 11:58:09 -0700455/* Queue event based on fs_mgr return code.
456 *
457 * code: return code of fs_mgr_mount_all
458 *
459 * This function might request a reboot, in which case it will
460 * not return.
461 *
462 * return code is processed based on input code
463 */
464static int queue_fs_event(int code) {
465 int ret = code;
466 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700467 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700468 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700469 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000470 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700471 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700472 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700473 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800474 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700475 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800476 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700477 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700478 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700479 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700480 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800481 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
482 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700483 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700484 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000485 if (e4crypt_install_keyring()) {
486 return -1;
487 }
488 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000489 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000490
491 // Although encrypted, we have device key, so we do not need to
492 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700493 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700494 } else if (code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED) {
495 if (e4crypt_install_keyring()) {
496 return -1;
497 }
498 property_set("ro.crypto.state", "encrypted");
499 property_set("ro.crypto.type", "file");
500
501 // defaultcrypto detects file/block encryption. init flow is same for each.
502 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
503 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
504 if (e4crypt_install_keyring()) {
505 return -1;
506 }
507 property_set("ro.crypto.type", "file");
508
509 // encrypt detects file/block encryption. init flow is same for each.
510 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700511 } else if (code > 0) {
512 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700513 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700514 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700515
516 return ret;
517}
518
Wei Wangd61a7e22016-08-23 11:58:09 -0700519/* mount_all <fstab> [ <path> ]* [--<options>]*
520 *
521 * This function might request a reboot, in which case it will
522 * not return.
523 */
524static int do_mount_all(const std::vector<std::string>& args) {
525 std::size_t na = 0;
526 bool import_rc = true;
527 bool queue_event = true;
528 int mount_mode = MOUNT_MODE_DEFAULT;
529 const char* fstabfile = args[1].c_str();
530 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800531 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700532
533 for (na = args.size() - 1; na > 1; --na) {
534 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800535 path_arg_end = na;
536 queue_event = false;
537 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800538 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700539 } else if (args[na] == "--late") {
540 path_arg_end = na;
541 import_rc = false;
542 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800543 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700544 }
545 }
546
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700547 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Tom Cherryede0d532017-07-06 14:20:11 -0700548 android::base::Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700549 int ret = mount_fstab(fstabfile, mount_mode);
Tom Cherryede0d532017-07-06 14:20:11 -0700550 property_set(prop_name, std::to_string(t.duration().count()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700551
552 if (import_rc) {
553 /* Paths of .rc files are specified at the 2nd argument and beyond */
554 import_late(args, 2, path_arg_end);
555 }
556
557 if (queue_event) {
558 /* queue_fs_event will queue event based on mount_fstab return code
559 * and return processed return code*/
560 ret = queue_fs_event(ret);
561 }
562
563 return ret;
564}
565
Tom Cherryb7349902015-08-26 11:43:36 -0700566static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700567 struct fstab *fstab;
568 int ret;
569
Tom Cherry96f67312015-07-30 13:52:55 -0700570 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700571 ret = fs_mgr_swapon_all(fstab);
572 fs_mgr_free_fstab(fstab);
573
574 return ret;
575}
576
Tom Cherryb7349902015-08-26 11:43:36 -0700577static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700578 property_set(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579 return 0;
580}
581
Tom Cherryb7349902015-08-26 11:43:36 -0700582static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700583 struct rlimit limit;
584 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700585 if (android::base::ParseInt(args[1], &resource) &&
586 android::base::ParseUint(args[2], &limit.rlim_cur) &&
587 android::base::ParseUint(args[3], &limit.rlim_max)) {
588 return setrlimit(resource, &limit);
589 }
590 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
591 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592}
593
Tom Cherryb7349902015-08-26 11:43:36 -0700594static int do_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700595 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700596 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700597 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700598 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700599 }
Tom Cherrybac32992015-07-31 12:45:25 -0700600 if (!svc->Start())
601 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602 return 0;
603}
604
Tom Cherryb7349902015-08-26 11:43:36 -0700605static int do_stop(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700606 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700607 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700608 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700609 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700610 }
Tom Cherrybac32992015-07-31 12:45:25 -0700611 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700612 return 0;
613}
614
Tom Cherryb7349902015-08-26 11:43:36 -0700615static int do_restart(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700616 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700617 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700618 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700619 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620 }
Tom Cherrybac32992015-07-31 12:45:25 -0700621 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700622 return 0;
623}
624
Tom Cherryb7349902015-08-26 11:43:36 -0700625static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700626 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 return 0;
628}
629
Tom Cherryb7349902015-08-26 11:43:36 -0700630static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700631 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700632}
633
Tom Cherryb7349902015-08-26 11:43:36 -0700634static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700635 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800636}
637
Tom Cherryb7349902015-08-26 11:43:36 -0700638static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700639 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800640}
641
Tom Cherryb7349902015-08-26 11:43:36 -0700642static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700643 struct timezone tz = {};
Wei Wang542aae42017-08-04 13:22:36 -0700644 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(nullptr, &tz) != -1) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700645 return 0;
646 }
647 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800648}
649
Tom Cherryb7349902015-08-26 11:43:36 -0700650static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700651 int mode = -1;
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800652 bool loaded = fs_mgr_load_verity_state(&mode);
653 if (loaded && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700654 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000655 }
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800656 return loaded ? 0 : 1;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000657}
658
Tom Cherryb7349902015-08-26 11:43:36 -0700659static void verity_update_property(fstab_rec *fstab, const char *mount_point,
660 int mode, int status) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700661 property_set("partition."s + mount_point + ".verified", std::to_string(mode));
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000662}
663
Tom Cherryb7349902015-08-26 11:43:36 -0700664static int do_verity_update_state(const std::vector<std::string>& args) {
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800665 return fs_mgr_update_verity_state(verity_update_property) ? 0 : 1;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000666}
667
Tom Cherryb7349902015-08-26 11:43:36 -0700668static int do_write(const std::vector<std::string>& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700669 if (auto result = WriteFile(args[1], args[2]); !result) {
670 LOG(ERROR) << "Unable to write to file '" << args[1] << "': " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700671 return -1;
672 }
673 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700674}
675
Wei Wang542aae42017-08-04 13:22:36 -0700676static int do_readahead(const std::vector<std::string>& args) {
677 struct stat sb;
678
679 if (stat(args[1].c_str(), &sb)) {
680 PLOG(ERROR) << "Error opening " << args[1];
681 return -1;
682 }
683
684 // We will do readahead in a forked process in order not to block init
685 // since it may block while it reads the
686 // filesystem metadata needed to locate the requested blocks. This
687 // occurs frequently with ext[234] on large files using indirect blocks
688 // instead of extents, giving the appearance that the call blocks until
689 // the requested data has been read.
690 pid_t pid = fork();
691 if (pid == 0) {
692 android::base::Timer t;
693 if (S_ISREG(sb.st_mode)) {
694 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(args[1].c_str(), O_RDONLY)));
695 if (fd == -1) {
696 PLOG(ERROR) << "Error opening file: " << args[1];
697 _exit(EXIT_FAILURE);
698 }
699 if (readahead(fd, 0, std::numeric_limits<size_t>::max())) {
700 PLOG(ERROR) << "Error readahead file: " << args[1];
701 _exit(EXIT_FAILURE);
702 }
703 } else if (S_ISDIR(sb.st_mode)) {
704 char* paths[] = {const_cast<char*>(args[1].data()), nullptr};
705 std::unique_ptr<FTS, decltype(&fts_close)> fts(
706 fts_open(paths, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr), fts_close);
707 if (!fts) {
708 PLOG(ERROR) << "Error opening directory: " << args[1];
709 _exit(EXIT_FAILURE);
710 }
711 // Traverse the entire hierarchy and do readahead
712 for (FTSENT* ftsent = fts_read(fts.get()); ftsent != nullptr;
713 ftsent = fts_read(fts.get())) {
714 if (ftsent->fts_info & FTS_F) {
715 android::base::unique_fd fd(
716 TEMP_FAILURE_RETRY(open(ftsent->fts_accpath, O_RDONLY)));
717 if (fd == -1) {
718 PLOG(ERROR) << "Error opening file: " << args[1];
719 continue;
720 }
721 if (readahead(fd, 0, std::numeric_limits<size_t>::max())) {
722 PLOG(ERROR) << "Unable to readahead on file: " << ftsent->fts_accpath;
723 }
724 }
725 }
726 }
727 LOG(INFO) << "Readahead " << args[1] << " took " << t;
728 _exit(0);
729 } else if (pid < 0) {
730 PLOG(ERROR) << "Fork failed";
731 return -1;
732 }
733 return 0;
734}
735
Tom Cherryb7349902015-08-26 11:43:36 -0700736static int do_copy(const std::vector<std::string>& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700737 auto file_contents = ReadFile(args[1]);
738 if (!file_contents) {
739 LOG(ERROR) << "Could not read input file '" << args[1] << "': " << file_contents.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700740 return -1;
San Mehat7c44fe52009-08-26 16:39:25 -0700741 }
Tom Cherry62ca6632017-08-03 12:54:07 -0700742 if (auto result = WriteFile(args[2], *file_contents); !result) {
743 LOG(ERROR) << "Could not write to output file '" << args[2] << "': " << result.error();
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700744 return -1;
745 }
746 return 0;
San Mehat7c44fe52009-08-26 16:39:25 -0700747}
748
Tom Cherryb7349902015-08-26 11:43:36 -0700749static int do_chown(const std::vector<std::string>& args) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700750 auto uid = DecodeUid(args[1]);
751 if (!uid) {
752 LOG(ERROR) << "Unable to decode UID for '" << args[1] << "': " << uid.error();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753 return -1;
754 }
Tom Cherry517e1f12017-05-04 17:40:33 -0700755
756 // GID is optional and pushes the index of path out by one if specified.
757 const std::string& path = (args.size() == 4) ? args[3] : args[2];
Tom Cherry62ca6632017-08-03 12:54:07 -0700758 Result<gid_t> gid = -1;
Tom Cherry517e1f12017-05-04 17:40:33 -0700759
760 if (args.size() == 4) {
Tom Cherry62ca6632017-08-03 12:54:07 -0700761 gid = DecodeUid(args[2]);
762 if (!gid) {
763 LOG(ERROR) << "Unable to decode GID for '" << args[2] << "': " << gid.error();
Tom Cherry517e1f12017-05-04 17:40:33 -0700764 return -1;
765 }
766 }
767
Tom Cherry62ca6632017-08-03 12:54:07 -0700768 if (lchown(path.c_str(), *uid, *gid) == -1) return -errno;
Tom Cherry517e1f12017-05-04 17:40:33 -0700769
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700770 return 0;
771}
772
773static mode_t get_mode(const char *s) {
774 mode_t mode = 0;
775 while (*s) {
776 if (*s >= '0' && *s <= '7') {
777 mode = (mode<<3) | (*s-'0');
778 } else {
779 return -1;
780 }
781 s++;
782 }
783 return mode;
784}
785
Tom Cherryb7349902015-08-26 11:43:36 -0700786static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700787 mode_t mode = get_mode(args[1].c_str());
788 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 return -errno;
790 }
791 return 0;
792}
793
Tom Cherryb7349902015-08-26 11:43:36 -0700794static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400795 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500796
Paul Lawrencea8d84342016-11-14 15:40:18 -0800797 struct flag_type {const char* name; int value;};
798 static const flag_type flags[] = {
799 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
800 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
801 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
802 {0, 0}
803 };
804
805 int flag = 0;
806
807 bool in_flags = true;
808 for (size_t i = 1; i < args.size(); ++i) {
809 if (android::base::StartsWith(args[i], "--")) {
810 if (!in_flags) {
811 LOG(ERROR) << "restorecon - flags must precede paths";
812 return -1;
813 }
814 bool found = false;
815 for (size_t j = 0; flags[j].name; ++j) {
816 if (args[i] == flags[j].name) {
817 flag |= flags[j].value;
818 found = true;
819 break;
820 }
821 }
822 if (!found) {
823 LOG(ERROR) << "restorecon - bad flag " << args[i];
824 return -1;
825 }
826 } else {
827 in_flags = false;
Tom Cherryc2ef2f02017-05-08 13:38:15 -0700828 if (selinux_android_restorecon(args[i].c_str(), flag) < 0) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800829 ret = -errno;
830 }
831 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500832 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400833 return ret;
834}
835
Tom Cherryb7349902015-08-26 11:43:36 -0700836static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800837 std::vector<std::string> non_const_args(args);
838 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
839 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500840}
841
Tom Cherryb7349902015-08-26 11:43:36 -0700842static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700843 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700844 int log_level = -1;
845 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700846 android::base::LogSeverity severity;
847 switch (log_level) {
848 case 7: severity = android::base::DEBUG; break;
849 case 6: severity = android::base::INFO; break;
850 case 5:
851 case 4: severity = android::base::WARNING; break;
852 case 3: severity = android::base::ERROR; break;
853 case 2:
854 case 1:
855 case 0: severity = android::base::FATAL; break;
856 default:
857 LOG(ERROR) << "loglevel: invalid log level " << log_level;
858 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700859 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700860 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700861 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700862}
863
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700864static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700865 load_persist_props();
866 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800867}
868
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700869static int do_load_system_props(const std::vector<std::string>& args) {
870 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700871 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700872}
873
Tom Cherryb7349902015-08-26 11:43:36 -0700874static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700875 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800876 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700877 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700878 int timeout;
879 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800880 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700881 }
882 }
883 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700884}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000885
Wei Wang132ac312017-01-25 16:27:03 -0800886static int do_wait_for_prop(const std::vector<std::string>& args) {
887 const char* name = args[1].c_str();
888 const char* value = args[2].c_str();
889 size_t value_len = strlen(value);
890
891 if (!is_legal_property_name(name)) {
892 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
893 << "\") failed: bad name";
894 return -1;
895 }
896 if (value_len >= PROP_VALUE_MAX) {
897 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
898 << "\") failed: value too long";
899 return -1;
900 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800901 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800902 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
903 << "\") failed: init already in waiting";
904 return -1;
905 }
906 return 0;
907}
908
Paul Crowley749af8c2015-05-28 17:35:06 +0100909static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700910 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100911}
912
Tom Cherryb7349902015-08-26 11:43:36 -0700913static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100914 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000915 return 0;
916 }
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700917 auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder;
Tom Cherryc3692b32017-08-10 12:22:44 -0700918 if (!make_dir(unencrypted_dir, 0700) && errno != EEXIST) {
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700919 PLOG(ERROR) << "Failed to create " << unencrypted_dir;
920 return -1;
921 }
922 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
923 "enablefilecrypto"};
924 return do_exec(exec_args);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000925}
Paul Crowley749af8c2015-05-28 17:35:06 +0100926
Paul Crowley59497452016-02-01 16:37:13 +0000927static int do_init_user0(const std::vector<std::string>& args) {
Tom Cherry5a86cb72017-05-03 13:19:03 -0700928 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
929 "init_user0"};
930 return do_exec(exec_args);
Paul Crowley59497452016-02-01 16:37:13 +0000931}
932
Tom Cherryad54d092017-04-19 16:18:50 -0700933const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
Tom Cherryb7349902015-08-26 11:43:36 -0700934 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -0700935 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700936 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800937 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700938 {"chmod", {2, 2, do_chmod}},
939 {"chown", {2, 3, do_chown}},
940 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800941 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700942 {"class_start", {1, 1, do_class_start}},
943 {"class_stop", {1, 1, do_class_stop}},
944 {"copy", {2, 2, do_copy}},
945 {"domainname", {1, 1, do_domainname}},
946 {"enable", {1, 1, do_enable}},
947 {"exec", {1, kMax, do_exec}},
Tom Cherryb27004a2017-03-27 16:27:30 -0700948 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700949 {"export", {2, 2, do_export}},
950 {"hostname", {1, 1, do_hostname}},
951 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000952 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700953 {"insmod", {1, kMax, do_insmod}},
954 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700955 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700956 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700957 {"loglevel", {1, 1, do_loglevel}},
958 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800959 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700960 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700961 {"umount", {1, 1, do_umount}},
Wei Wang542aae42017-08-04 13:22:36 -0700962 {"readahead", {1, 1, do_readahead}},
Tom Cherryb7349902015-08-26 11:43:36 -0700963 {"restart", {1, 1, do_restart}},
964 {"restorecon", {1, kMax, do_restorecon}},
965 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
966 {"rm", {1, 1, do_rm}},
967 {"rmdir", {1, 1, do_rmdir}},
968 {"setprop", {2, 2, do_setprop}},
969 {"setrlimit", {3, 3, do_setrlimit}},
970 {"start", {1, 1, do_start}},
971 {"stop", {1, 1, do_stop}},
972 {"swapon_all", {1, 1, do_swapon_all}},
973 {"symlink", {2, 2, do_symlink}},
974 {"sysclktz", {1, 1, do_sysclktz}},
975 {"trigger", {1, 1, do_trigger}},
976 {"verity_load_state", {0, 0, do_verity_load_state}},
977 {"verity_update_state", {0, 0, do_verity_update_state}},
978 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800979 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700980 {"write", {2, 2, do_write}},
981 };
Tom Cherryb27004a2017-03-27 16:27:30 -0700982 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700983 return builtin_functions;
984}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700985
986} // namespace init
987} // namespace android