blob: 5335608bccd999c003caf36a6f869b362f10f2aa [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>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070022#include <linux/loop.h>
23#include <linux/module.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070024#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070025#include <net/if.h>
Mark Salyzynad575e02016-04-05 08:10:25 -070026#include <sched.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070027#include <signal.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070028#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070029#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070030#include <string.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070031#include <sys/mount.h>
32#include <sys/resource.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070033#include <sys/socket.h>
34#include <sys/stat.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070035#include <sys/syscall.h>
Tom Cherryf57c0bf2017-04-07 13:46:21 -070036#include <sys/system_properties.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080037#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070038#include <sys/types.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070039#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070040#include <unistd.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041
Tom Cherryede0d532017-07-06 14:20:11 -070042#include <android-base/chrono_utils.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070043#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070044#include <android-base/logging.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080045#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070046#include <android-base/properties.h>
Tom Cherry70379912017-08-01 14:10:11 -070047#include <android-base/stringprintf.h>
Tom Cherryccf23532017-03-28 16:40:41 -070048#include <android-base/strings.h>
Tom Cherry70379912017-08-01 14:10:11 -070049#include <android-base/unique_fd.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070050#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070051#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070052#include <ext4_utils/ext4_crypt.h>
53#include <ext4_utils/ext4_crypt_init_extensions.h>
Tom Cherryccf23532017-03-28 16:40:41 -070054#include <fs_mgr.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070055#include <selinux/android.h>
56#include <selinux/label.h>
57#include <selinux/selinux.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070058
Tom Cherryfa0c21c2015-07-23 17:53:11 -070059#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070060#include "bootchart.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "init.h"
Tom Cherry67dee622017-07-27 12:54:48 -070062#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070063#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070064#include "reboot.h"
Tom Cherrybac32992015-07-31 12:45:25 -070065#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080066#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070067#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068
Paul Crowley0b8b2302016-12-19 13:03:47 -080069using namespace std::literals::string_literals;
70
Tom Cherry70379912017-08-01 14:10:11 -070071using android::base::unique_fd;
72
Nick Kralevichbc609542015-01-31 21:39:46 -080073#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
74
Tom Cherry81f5d3e2017-06-22 12:53:17 -070075namespace android {
76namespace init {
77
Elliott Hughes9605a942016-11-10 17:43:47 -080078static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080079
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080080static int insmod(const char *filename, const char *options, int flags) {
Tom Cherry70379912017-08-01 14:10:11 -070081 unique_fd fd(TEMP_FAILURE_RETRY(open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
Nick Kralevich124a9c92016-03-27 16:55:59 -070082 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070083 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080085 }
Tom Cherry70379912017-08-01 14:10:11 -070086 int rc = syscall(__NR_finit_module, fd.get(), options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070087 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070088 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070089 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070090 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070091}
92
Tom Cherryb7349902015-08-26 11:43:36 -070093static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094 struct ifreq ifr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095
96 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
97
Tom Cherry70379912017-08-01 14:10:11 -070098 unique_fd s(TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_DGRAM, 0)));
99 if (s < 0) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100
Tom Cherry70379912017-08-01 14:10:11 -0700101 int ret = ioctl(s, SIOCGIFFLAGS, &ifr);
102 if (ret < 0) return ret;
103
104 if (up) {
105 ifr.ifr_flags |= IFF_UP;
106 } else {
107 ifr.ifr_flags &= ~IFF_UP;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108 }
109
Tom Cherry70379912017-08-01 14:10:11 -0700110 return ioctl(s, SIOCSIFFLAGS, &ifr);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111}
112
Paul Crowley0b8b2302016-12-19 13:03:47 -0800113static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700114 std::string err;
115 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700116 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700117 return -1;
118 }
Wei Wangeeab4912017-06-27 22:08:45 -0700119 property_set("sys.powerctl", "reboot,recovery");
Keun-young Park8d01f632017-03-13 11:54:47 -0700120 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700121}
122
Tom Cherry911b9b12017-07-27 16:20:58 -0700123template <typename F>
124static void ForEachServiceInClass(const std::string& classname, F function) {
125 for (const auto& service : ServiceList::GetInstance()) {
126 if (service->classnames().count(classname)) std::invoke(function, service);
127 }
128}
129
Tom Cherryb7349902015-08-26 11:43:36 -0700130static int do_class_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700131 // Starting a class does not start services which are explicitly disabled.
132 // They must be started individually.
133 ForEachServiceInClass(args[1], &Service::StartIfNotDisabled);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700134 return 0;
135}
136
Tom Cherryb7349902015-08-26 11:43:36 -0700137static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700138 ForEachServiceInClass(args[1], &Service::Stop);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 return 0;
140}
141
Tom Cherryb7349902015-08-26 11:43:36 -0700142static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700143 ForEachServiceInClass(args[1], &Service::Reset);
Ken Sumrall752923c2010-12-03 16:33:31 -0800144 return 0;
145}
146
Steven Moreland2b63d542017-03-10 14:04:37 -0800147static int do_class_restart(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700148 ForEachServiceInClass(args[1], &Service::Restart);
Steven Moreland2b63d542017-03-10 14:04:37 -0800149 return 0;
150}
151
Tom Cherryb7349902015-08-26 11:43:36 -0700152static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700153 std::string err;
154 if (!WriteFile("/proc/sys/kernel/domainname", args[1], &err)) {
155 LOG(ERROR) << err;
156 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 Cherry2cbbe9f2017-05-04 18:17:33 -0700201 std::string err;
202 if (!WriteFile("/proc/sys/kernel/hostname", args[1], &err)) {
203 LOG(ERROR) << err;
204 return -1;
205 }
206 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207}
208
Tom Cherryb7349902015-08-26 11:43:36 -0700209static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700210 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700211}
212
Tom Cherryb7349902015-08-26 11:43:36 -0700213static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800214 int flags = 0;
215 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800216
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800217 if (!(*it).compare("-f")) {
218 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
219 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800220 }
221
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800222 std::string filename = *it++;
223 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
224 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800225}
226
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;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700229 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230
231 /* mkdir <path> [mode] [owner] [group] */
232
Tom Cherry96f67312015-07-30 13:52:55 -0700233 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700234 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235 }
236
Tom Cherrye7656b72017-05-01 17:10:09 -0700237 ret = make_dir(args[1].c_str(), mode, sehandle);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700238 /* chmod in case the directory already exists */
239 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700240 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700241 }
242 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243 return -errno;
244 }
245
Tom Cherry96f67312015-07-30 13:52:55 -0700246 if (args.size() >= 4) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700247 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 Project4f6e8d72008-10-21 07:00:00 -0700253 gid_t gid = -1;
254
Tom Cherry96f67312015-07-30 13:52:55 -0700255 if (args.size() == 5) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700256 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 Project4f6e8d72008-10-21 07:00:00 -0700260 }
261
Tom Cherry96f67312015-07-30 13:52:55 -0700262 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263 return -errno;
264 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700265
266 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
267 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700268 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700269 if (ret == -1) {
270 return -errno;
271 }
272 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273 }
274
Paul Crowleyaf8be582016-05-10 08:52:06 -0700275 if (e4crypt_is_native()) {
276 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800277 const std::vector<std::string> options = {
278 "--prompt_and_wipe_data",
279 "--reason=set_policy_failed:"s + args[1]};
280 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700281 return -1;
282 }
283 }
284 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285}
286
Alex Light68ab20f2016-06-23 11:11:39 -0700287/* umount <path> */
288static int do_umount(const std::vector<std::string>& args) {
289 return umount(args[1].c_str());
290}
291
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292static struct {
293 const char *name;
294 unsigned flag;
295} mount_flags[] = {
296 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200297 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298 { "nosuid", MS_NOSUID },
299 { "nodev", MS_NODEV },
300 { "nodiratime", MS_NODIRATIME },
301 { "ro", MS_RDONLY },
302 { "rw", 0 },
303 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700304 { "bind", MS_BIND },
305 { "rec", MS_REC },
306 { "unbindable", MS_UNBINDABLE },
307 { "private", MS_PRIVATE },
308 { "slave", MS_SLAVE },
309 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 { "defaults", 0 },
311 { 0, 0 },
312};
313
Ken Sumrall752923c2010-12-03 16:33:31 -0800314#define DATA_MNT_POINT "/data"
315
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700317static int do_mount(const std::vector<std::string>& args) {
Tom Cherry70379912017-08-01 14:10:11 -0700318 const char* options = nullptr;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319 unsigned flags = 0;
Tom Cherry70379912017-08-01 14:10:11 -0700320 bool wait = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321
Tom Cherry70379912017-08-01 14:10:11 -0700322 for (size_t na = 4; na < args.size(); na++) {
323 size_t i;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700325 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 flags |= mount_flags[i].flag;
327 break;
328 }
329 }
330
Colin Crosscd0f1732010-04-19 17:10:24 -0700331 if (!mount_flags[i].name) {
Tom Cherry70379912017-08-01 14:10:11 -0700332 if (!args[na].compare("wait")) {
333 wait = true;
334 // If our last argument isn't a flag, wolf it up as an option string.
335 } else if (na + 1 == args.size()) {
Tom Cherry96f67312015-07-30 13:52:55 -0700336 options = args[na].c_str();
Tom Cherry70379912017-08-01 14:10:11 -0700337 }
Colin Crosscd0f1732010-04-19 17:10:24 -0700338 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700339 }
340
Tom Cherry70379912017-08-01 14:10:11 -0700341 const char* system = args[1].c_str();
342 const char* source = args[2].c_str();
343 const char* target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000344
Tom Cherry70379912017-08-01 14:10:11 -0700345 if (android::base::StartsWith(source, "loop@")) {
346 int mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
347 unique_fd fd(TEMP_FAILURE_RETRY(open(source + 5, mode | O_CLOEXEC)));
348 if (fd < 0) return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000349
Tom Cherry70379912017-08-01 14:10:11 -0700350 for (size_t n = 0;; n++) {
351 std::string tmp = android::base::StringPrintf("/dev/block/loop%zu", n);
352 unique_fd loop(TEMP_FAILURE_RETRY(open(tmp.c_str(), mode | O_CLOEXEC)));
353 if (loop < 0) return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000354
Tom Cherry70379912017-08-01 14:10:11 -0700355 loop_info info;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000356 /* if it is a blank loop device */
357 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
358 /* if it becomes our loop device */
Tom Cherry70379912017-08-01 14:10:11 -0700359 if (ioctl(loop, LOOP_SET_FD, fd.get()) >= 0) {
360 if (mount(tmp.c_str(), target, system, flags, options) < 0) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000361 ioctl(loop, LOOP_CLR_FD, 0);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 return -1;
363 }
Tom Cherry70379912017-08-01 14:10:11 -0700364 return 0;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000365 }
366 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 }
368
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700369 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000370 return -1;
371 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700372 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800373 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000374 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700375 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000376 }
377
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800379
Ken Sumralldd4d7862011-02-17 18:09:47 -0800380 return 0;
381
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382}
383
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800384/* Imports .rc files from the specified paths. Default ones are applied if none is given.
385 *
386 * start_index: index of the first path in the args list
387 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700388static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherry67dee622017-07-27 12:54:48 -0700389 auto& action_manager = ActionManager::GetInstance();
Tom Cherry911b9b12017-07-27 16:20:58 -0700390 auto& service_list = ServiceList::GetInstance();
391 Parser parser = CreateParser(action_manager, service_list);
Wei Wangd61a7e22016-08-23 11:58:09 -0700392 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900393 // Fallbacks for partitions on which early mount isn't enabled.
Tom Cherry67dee622017-07-27 12:54:48 -0700394 for (const auto& path : late_import_paths) {
395 parser.ParseConfig(path);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900396 }
Tom Cherry67dee622017-07-27 12:54:48 -0700397 late_import_paths.clear();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800398 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700399 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800400 parser.ParseConfig(args[i]);
401 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700402 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700403
404 // Turning this on and letting the INFO logging be discarded adds 0.2s to
405 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700406 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700407}
408
Wei Wangd61a7e22016-08-23 11:58:09 -0700409/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800410 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700411 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700412 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700413static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700414 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700415
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700416 /*
417 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
418 * do the call in the child to provide protection to the main init
419 * process if anything goes wrong (crash or memory leak), and wait for
420 * the child to finish in the parent.
421 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700422 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700423 if (pid > 0) {
424 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700425 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700426 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700427 if (wp_ret == -1) {
428 // Unexpected error code. We will continue anyway.
429 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700430 }
431
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700432 if (WIFEXITED(status)) {
433 ret = WEXITSTATUS(status);
434 } else {
435 ret = -1;
436 }
437 } else if (pid == 0) {
438 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700439
440 // So we can always see what fs_mgr_mount_all() does.
441 // Only needed if someone explicitly changes the default log level in their init.rc.
442 android::base::ScopedLogSeverity info(android::base::INFO);
443
444 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700445 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800446 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700447 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700448 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700449 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700450 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700451 } else {
452 /* fork failed, return an error */
453 return -1;
454 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700455 return ret;
456}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700457
Wei Wangd61a7e22016-08-23 11:58:09 -0700458/* Queue event based on fs_mgr return code.
459 *
460 * code: return code of fs_mgr_mount_all
461 *
462 * This function might request a reboot, in which case it will
463 * not return.
464 *
465 * return code is processed based on input code
466 */
467static int queue_fs_event(int code) {
468 int ret = code;
469 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700470 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700471 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700472 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000473 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700474 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700475 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700476 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800477 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700478 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800479 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700480 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700481 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700482 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700483 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800484 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
485 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700486 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700487 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000488 if (e4crypt_install_keyring()) {
489 return -1;
490 }
491 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000492 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000493
494 // Although encrypted, we have device key, so we do not need to
495 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700496 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700497 } else if (code > 0) {
498 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700499 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700500 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501
502 return ret;
503}
504
Wei Wangd61a7e22016-08-23 11:58:09 -0700505/* mount_all <fstab> [ <path> ]* [--<options>]*
506 *
507 * This function might request a reboot, in which case it will
508 * not return.
509 */
510static int do_mount_all(const std::vector<std::string>& args) {
511 std::size_t na = 0;
512 bool import_rc = true;
513 bool queue_event = true;
514 int mount_mode = MOUNT_MODE_DEFAULT;
515 const char* fstabfile = args[1].c_str();
516 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800517 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700518
519 for (na = args.size() - 1; na > 1; --na) {
520 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800521 path_arg_end = na;
522 queue_event = false;
523 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800524 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700525 } else if (args[na] == "--late") {
526 path_arg_end = na;
527 import_rc = false;
528 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800529 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700530 }
531 }
532
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700533 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Tom Cherryede0d532017-07-06 14:20:11 -0700534 android::base::Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700535 int ret = mount_fstab(fstabfile, mount_mode);
Tom Cherryede0d532017-07-06 14:20:11 -0700536 property_set(prop_name, std::to_string(t.duration().count()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700537
538 if (import_rc) {
539 /* Paths of .rc files are specified at the 2nd argument and beyond */
540 import_late(args, 2, path_arg_end);
541 }
542
543 if (queue_event) {
544 /* queue_fs_event will queue event based on mount_fstab return code
545 * and return processed return code*/
546 ret = queue_fs_event(ret);
547 }
548
549 return ret;
550}
551
Tom Cherryb7349902015-08-26 11:43:36 -0700552static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700553 struct fstab *fstab;
554 int ret;
555
Tom Cherry96f67312015-07-30 13:52:55 -0700556 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700557 ret = fs_mgr_swapon_all(fstab);
558 fs_mgr_free_fstab(fstab);
559
560 return ret;
561}
562
Tom Cherryb7349902015-08-26 11:43:36 -0700563static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700564 property_set(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700565 return 0;
566}
567
Tom Cherryb7349902015-08-26 11:43:36 -0700568static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569 struct rlimit limit;
570 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700571 if (android::base::ParseInt(args[1], &resource) &&
572 android::base::ParseUint(args[2], &limit.rlim_cur) &&
573 android::base::ParseUint(args[3], &limit.rlim_max)) {
574 return setrlimit(resource, &limit);
575 }
576 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
577 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578}
579
Tom Cherryb7349902015-08-26 11:43:36 -0700580static int do_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700581 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700582 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700583 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700584 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585 }
Tom Cherrybac32992015-07-31 12:45:25 -0700586 if (!svc->Start())
587 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588 return 0;
589}
590
Tom Cherryb7349902015-08-26 11:43:36 -0700591static int do_stop(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700592 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700593 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700594 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700595 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700596 }
Tom Cherrybac32992015-07-31 12:45:25 -0700597 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 return 0;
599}
600
Tom Cherryb7349902015-08-26 11:43:36 -0700601static int do_restart(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700602 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700603 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700604 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700605 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 }
Tom Cherrybac32992015-07-31 12:45:25 -0700607 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700608 return 0;
609}
610
Tom Cherryb7349902015-08-26 11:43:36 -0700611static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700612 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700613 return 0;
614}
615
Tom Cherryb7349902015-08-26 11:43:36 -0700616static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700617 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700618}
619
Tom Cherryb7349902015-08-26 11:43:36 -0700620static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700621 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800622}
623
Tom Cherryb7349902015-08-26 11:43:36 -0700624static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700625 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800626}
627
Tom Cherryb7349902015-08-26 11:43:36 -0700628static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700629 struct timezone tz = {};
630 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
631 return 0;
632 }
633 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800634}
635
Tom Cherryb7349902015-08-26 11:43:36 -0700636static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700637 int mode = -1;
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800638 bool loaded = fs_mgr_load_verity_state(&mode);
639 if (loaded && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700640 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000641 }
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800642 return loaded ? 0 : 1;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000643}
644
Tom Cherryb7349902015-08-26 11:43:36 -0700645static void verity_update_property(fstab_rec *fstab, const char *mount_point,
646 int mode, int status) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700647 property_set("partition."s + mount_point + ".verified", std::to_string(mode));
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000648}
649
Tom Cherryb7349902015-08-26 11:43:36 -0700650static int do_verity_update_state(const std::vector<std::string>& args) {
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800651 return fs_mgr_update_verity_state(verity_update_property) ? 0 : 1;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000652}
653
Tom Cherryb7349902015-08-26 11:43:36 -0700654static int do_write(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700655 std::string err;
656 if (!WriteFile(args[1], args[2], &err)) {
657 LOG(ERROR) << err;
658 return -1;
659 }
660 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661}
662
Tom Cherryb7349902015-08-26 11:43:36 -0700663static int do_copy(const std::vector<std::string>& args) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800664 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700665 std::string err;
666 if (!ReadFile(args[1], &data, &err)) {
667 LOG(ERROR) << err;
668 return -1;
San Mehat7c44fe52009-08-26 16:39:25 -0700669 }
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700670 if (!WriteFile(args[2], data, &err)) {
671 LOG(ERROR) << err;
672 return -1;
673 }
674 return 0;
San Mehat7c44fe52009-08-26 16:39:25 -0700675}
676
Tom Cherryb7349902015-08-26 11:43:36 -0700677static int do_chown(const std::vector<std::string>& args) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700678 uid_t uid;
679 std::string decode_uid_err;
680 if (!DecodeUid(args[1], &uid, &decode_uid_err)) {
681 LOG(ERROR) << "Unable to find UID for '" << args[1] << "': " << decode_uid_err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682 return -1;
683 }
Tom Cherry517e1f12017-05-04 17:40:33 -0700684
685 // GID is optional and pushes the index of path out by one if specified.
686 const std::string& path = (args.size() == 4) ? args[3] : args[2];
687 gid_t gid = -1;
688
689 if (args.size() == 4) {
690 if (!DecodeUid(args[2], &gid, &decode_uid_err)) {
691 LOG(ERROR) << "Unable to find GID for '" << args[2] << "': " << decode_uid_err;
692 return -1;
693 }
694 }
695
696 if (lchown(path.c_str(), uid, gid) == -1) return -errno;
697
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700698 return 0;
699}
700
701static mode_t get_mode(const char *s) {
702 mode_t mode = 0;
703 while (*s) {
704 if (*s >= '0' && *s <= '7') {
705 mode = (mode<<3) | (*s-'0');
706 } else {
707 return -1;
708 }
709 s++;
710 }
711 return mode;
712}
713
Tom Cherryb7349902015-08-26 11:43:36 -0700714static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700715 mode_t mode = get_mode(args[1].c_str());
716 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717 return -errno;
718 }
719 return 0;
720}
721
Tom Cherryb7349902015-08-26 11:43:36 -0700722static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400723 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500724
Paul Lawrencea8d84342016-11-14 15:40:18 -0800725 struct flag_type {const char* name; int value;};
726 static const flag_type flags[] = {
727 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
728 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
729 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
730 {0, 0}
731 };
732
733 int flag = 0;
734
735 bool in_flags = true;
736 for (size_t i = 1; i < args.size(); ++i) {
737 if (android::base::StartsWith(args[i], "--")) {
738 if (!in_flags) {
739 LOG(ERROR) << "restorecon - flags must precede paths";
740 return -1;
741 }
742 bool found = false;
743 for (size_t j = 0; flags[j].name; ++j) {
744 if (args[i] == flags[j].name) {
745 flag |= flags[j].value;
746 found = true;
747 break;
748 }
749 }
750 if (!found) {
751 LOG(ERROR) << "restorecon - bad flag " << args[i];
752 return -1;
753 }
754 } else {
755 in_flags = false;
Tom Cherry482f36c2017-05-08 13:38:15 -0700756 if (selinux_android_restorecon(args[i].c_str(), flag) < 0) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800757 ret = -errno;
758 }
759 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500760 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400761 return ret;
762}
763
Tom Cherryb7349902015-08-26 11:43:36 -0700764static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800765 std::vector<std::string> non_const_args(args);
766 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
767 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500768}
769
Tom Cherryb7349902015-08-26 11:43:36 -0700770static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700771 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700772 int log_level = -1;
773 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700774 android::base::LogSeverity severity;
775 switch (log_level) {
776 case 7: severity = android::base::DEBUG; break;
777 case 6: severity = android::base::INFO; break;
778 case 5:
779 case 4: severity = android::base::WARNING; break;
780 case 3: severity = android::base::ERROR; break;
781 case 2:
782 case 1:
783 case 0: severity = android::base::FATAL; break;
784 default:
785 LOG(ERROR) << "loglevel: invalid log level " << log_level;
786 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700787 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700788 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700789 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790}
791
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700792static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700793 load_persist_props();
794 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800795}
796
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700797static int do_load_system_props(const std::vector<std::string>& args) {
798 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700799 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700800}
801
Tom Cherryb7349902015-08-26 11:43:36 -0700802static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700803 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800804 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700805 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700806 int timeout;
807 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800808 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700809 }
810 }
811 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700812}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000813
Wei Wang132ac312017-01-25 16:27:03 -0800814static int do_wait_for_prop(const std::vector<std::string>& args) {
815 const char* name = args[1].c_str();
816 const char* value = args[2].c_str();
817 size_t value_len = strlen(value);
818
819 if (!is_legal_property_name(name)) {
820 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
821 << "\") failed: bad name";
822 return -1;
823 }
824 if (value_len >= PROP_VALUE_MAX) {
825 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
826 << "\") failed: value too long";
827 return -1;
828 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800829 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800830 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
831 << "\") failed: init already in waiting";
832 return -1;
833 }
834 return 0;
835}
836
Paul Lawrence806d10b2015-04-28 22:07:10 +0000837/*
838 * Callback to make a directory from the ext4 code
839 */
Tom Cherryb7349902015-08-26 11:43:36 -0700840static int do_installkeys_ensure_dir_exists(const char* dir) {
Tom Cherrye7656b72017-05-01 17:10:09 -0700841 if (make_dir(dir, 0700, sehandle) && errno != EEXIST) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000842 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000843 }
844
Paul Lawrence806d10b2015-04-28 22:07:10 +0000845 return 0;
846}
847
Paul Crowley749af8c2015-05-28 17:35:06 +0100848static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700849 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100850}
851
Tom Cherryb7349902015-08-26 11:43:36 -0700852static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100853 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000854 return 0;
855 }
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700856 auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder;
857 if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) {
858 PLOG(ERROR) << "Failed to create " << unencrypted_dir;
859 return -1;
860 }
861 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
862 "enablefilecrypto"};
863 return do_exec(exec_args);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000864}
Paul Crowley749af8c2015-05-28 17:35:06 +0100865
Paul Crowley59497452016-02-01 16:37:13 +0000866static int do_init_user0(const std::vector<std::string>& args) {
Tom Cherry5a86cb72017-05-03 13:19:03 -0700867 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
868 "init_user0"};
869 return do_exec(exec_args);
Paul Crowley59497452016-02-01 16:37:13 +0000870}
871
Tom Cherryad54d092017-04-19 16:18:50 -0700872const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
Tom Cherryb7349902015-08-26 11:43:36 -0700873 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -0700874 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700875 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800876 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700877 {"chmod", {2, 2, do_chmod}},
878 {"chown", {2, 3, do_chown}},
879 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800880 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700881 {"class_start", {1, 1, do_class_start}},
882 {"class_stop", {1, 1, do_class_stop}},
883 {"copy", {2, 2, do_copy}},
884 {"domainname", {1, 1, do_domainname}},
885 {"enable", {1, 1, do_enable}},
886 {"exec", {1, kMax, do_exec}},
Tom Cherryb27004a2017-03-27 16:27:30 -0700887 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700888 {"export", {2, 2, do_export}},
889 {"hostname", {1, 1, do_hostname}},
890 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000891 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700892 {"insmod", {1, kMax, do_insmod}},
893 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700894 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700895 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700896 {"loglevel", {1, 1, do_loglevel}},
897 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800898 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700899 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700900 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700901 {"restart", {1, 1, do_restart}},
902 {"restorecon", {1, kMax, do_restorecon}},
903 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
904 {"rm", {1, 1, do_rm}},
905 {"rmdir", {1, 1, do_rmdir}},
906 {"setprop", {2, 2, do_setprop}},
907 {"setrlimit", {3, 3, do_setrlimit}},
908 {"start", {1, 1, do_start}},
909 {"stop", {1, 1, do_stop}},
910 {"swapon_all", {1, 1, do_swapon_all}},
911 {"symlink", {2, 2, do_symlink}},
912 {"sysclktz", {1, 1, do_sysclktz}},
913 {"trigger", {1, 1, do_trigger}},
914 {"verity_load_state", {0, 0, do_verity_load_state}},
915 {"verity_update_state", {0, 0, do_verity_update_state}},
916 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800917 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700918 {"write", {2, 2, do_write}},
919 };
Tom Cherryb27004a2017-03-27 16:27:30 -0700920 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700921 return builtin_functions;
922}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700923
924} // namespace init
925} // namespace android