blob: 0351582017961b37631596525af39c197f6681bd [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 Cherryccf23532017-03-28 16:40:41 -070047#include <android-base/strings.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070048#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070049#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070050#include <ext4_utils/ext4_crypt.h>
51#include <ext4_utils/ext4_crypt_init_extensions.h>
Tom Cherryccf23532017-03-28 16:40:41 -070052#include <fs_mgr.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070053#include <selinux/android.h>
54#include <selinux/label.h>
55#include <selinux/selinux.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070056
Tom Cherryfa0c21c2015-07-23 17:53:11 -070057#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070058#include "bootchart.h"
Tom Cherrybac32992015-07-31 12:45:25 -070059#include "init.h"
Tom Cherry67dee622017-07-27 12:54:48 -070060#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070062#include "reboot.h"
Tom Cherrybac32992015-07-31 12:45:25 -070063#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080064#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070065#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066
Paul Crowley0b8b2302016-12-19 13:03:47 -080067using namespace std::literals::string_literals;
68
Nick Kralevichbc609542015-01-31 21:39:46 -080069#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
70
Tom Cherry81f5d3e2017-06-22 12:53:17 -070071namespace android {
72namespace init {
73
Elliott Hughes9605a942016-11-10 17:43:47 -080074static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080075
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080076static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070077 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
78 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070079 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080081 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080082 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070083 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070084 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070085 }
86 close(fd);
87 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088}
89
Tom Cherryb7349902015-08-26 11:43:36 -070090static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070091 struct ifreq ifr;
92 int s, ret;
93
94 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
95
96 s = socket(AF_INET, SOCK_DGRAM, 0);
97 if (s < 0)
98 return -1;
99
100 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
101 if (ret < 0) {
102 goto done;
103 }
104
105 if (up)
106 ifr.ifr_flags |= IFF_UP;
107 else
108 ifr.ifr_flags &= ~IFF_UP;
109
110 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800111
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112done:
113 close(s);
114 return ret;
115}
116
Paul Crowley0b8b2302016-12-19 13:03:47 -0800117static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700118 std::string err;
119 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700120 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700121 return -1;
122 }
Wei Wangeeab4912017-06-27 22:08:45 -0700123 property_set("sys.powerctl", "reboot,recovery");
Keun-young Park8d01f632017-03-13 11:54:47 -0700124 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700125}
126
Tom Cherry911b9b12017-07-27 16:20:58 -0700127template <typename F>
128static void ForEachServiceInClass(const std::string& classname, F function) {
129 for (const auto& service : ServiceList::GetInstance()) {
130 if (service->classnames().count(classname)) std::invoke(function, service);
131 }
132}
133
Tom Cherryb7349902015-08-26 11:43:36 -0700134static int do_class_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700135 // Starting a class does not start services which are explicitly disabled.
136 // They must be started individually.
137 ForEachServiceInClass(args[1], &Service::StartIfNotDisabled);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138 return 0;
139}
140
Tom Cherryb7349902015-08-26 11:43:36 -0700141static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700142 ForEachServiceInClass(args[1], &Service::Stop);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700143 return 0;
144}
145
Tom Cherryb7349902015-08-26 11:43:36 -0700146static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700147 ForEachServiceInClass(args[1], &Service::Reset);
Ken Sumrall752923c2010-12-03 16:33:31 -0800148 return 0;
149}
150
Steven Moreland2b63d542017-03-10 14:04:37 -0800151static int do_class_restart(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700152 ForEachServiceInClass(args[1], &Service::Restart);
Steven Moreland2b63d542017-03-10 14:04:37 -0800153 return 0;
154}
155
Tom Cherryb7349902015-08-26 11:43:36 -0700156static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700157 std::string err;
158 if (!WriteFile("/proc/sys/kernel/domainname", args[1], &err)) {
159 LOG(ERROR) << err;
160 return -1;
161 }
162 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163}
164
Tom Cherryb7349902015-08-26 11:43:36 -0700165static int do_enable(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700166 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700167 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700168 return -1;
169 }
Tom Cherrybac32992015-07-31 12:45:25 -0700170 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700171}
172
Tom Cherryb7349902015-08-26 11:43:36 -0700173static int do_exec(const std::vector<std::string>& args) {
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700174 auto service = Service::MakeTemporaryOneshotService(args);
175 if (!service) {
176 LOG(ERROR) << "Failed to create exec service: " << android::base::Join(args, " ");
177 return -1;
178 }
179 if (!service->ExecStart()) {
180 LOG(ERROR) << "Failed to Start exec service";
181 return -1;
182 }
Tom Cherry911b9b12017-07-27 16:20:58 -0700183 ServiceList::GetInstance().AddService(std::move(service));
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700184 return 0;
Tom Cherryb27004a2017-03-27 16:27:30 -0700185}
186
187static int do_exec_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700188 Service* service = ServiceList::GetInstance().FindService(args[1]);
Tom Cherry3b81f2d2017-07-28 14:48:41 -0700189 if (!service) {
190 LOG(ERROR) << "ExecStart(" << args[1] << "): Service not found";
191 return -1;
192 }
193 if (!service->ExecStart()) {
194 LOG(ERROR) << "ExecStart(" << args[1] << "): Could not start Service";
195 return -1;
196 }
197 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198}
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700201 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700202}
203
Tom Cherryb7349902015-08-26 11:43:36 -0700204static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700205 std::string err;
206 if (!WriteFile("/proc/sys/kernel/hostname", args[1], &err)) {
207 LOG(ERROR) << err;
208 return -1;
209 }
210 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700211}
212
Tom Cherryb7349902015-08-26 11:43:36 -0700213static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700214 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215}
216
Tom Cherryb7349902015-08-26 11:43:36 -0700217static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800218 int flags = 0;
219 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800220
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800221 if (!(*it).compare("-f")) {
222 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
223 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800224 }
225
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800226 std::string filename = *it++;
227 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
228 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800229}
230
Tom Cherryb7349902015-08-26 11:43:36 -0700231static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700233 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234
235 /* mkdir <path> [mode] [owner] [group] */
236
Tom Cherry96f67312015-07-30 13:52:55 -0700237 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700238 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239 }
240
Tom Cherrye7656b72017-05-01 17:10:09 -0700241 ret = make_dir(args[1].c_str(), mode, sehandle);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700242 /* chmod in case the directory already exists */
243 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700244 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700245 }
246 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247 return -errno;
248 }
249
Tom Cherry96f67312015-07-30 13:52:55 -0700250 if (args.size() >= 4) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700251 uid_t uid;
252 std::string decode_uid_err;
253 if (!DecodeUid(args[3], &uid, &decode_uid_err)) {
254 LOG(ERROR) << "Unable to find UID for '" << args[3] << "': " << decode_uid_err;
255 return -1;
256 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257 gid_t gid = -1;
258
Tom Cherry96f67312015-07-30 13:52:55 -0700259 if (args.size() == 5) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700260 if (!DecodeUid(args[4], &gid, &decode_uid_err)) {
261 LOG(ERROR) << "Unable to find GID for '" << args[3] << "': " << decode_uid_err;
262 return -1;
263 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264 }
265
Tom Cherry96f67312015-07-30 13:52:55 -0700266 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700267 return -errno;
268 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700269
270 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
271 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700272 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700273 if (ret == -1) {
274 return -errno;
275 }
276 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277 }
278
Paul Crowleyaf8be582016-05-10 08:52:06 -0700279 if (e4crypt_is_native()) {
280 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800281 const std::vector<std::string> options = {
282 "--prompt_and_wipe_data",
283 "--reason=set_policy_failed:"s + args[1]};
284 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700285 return -1;
286 }
287 }
288 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289}
290
Alex Light68ab20f2016-06-23 11:11:39 -0700291/* umount <path> */
292static int do_umount(const std::vector<std::string>& args) {
293 return umount(args[1].c_str());
294}
295
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296static struct {
297 const char *name;
298 unsigned flag;
299} mount_flags[] = {
300 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200301 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302 { "nosuid", MS_NOSUID },
303 { "nodev", MS_NODEV },
304 { "nodiratime", MS_NODIRATIME },
305 { "ro", MS_RDONLY },
306 { "rw", 0 },
307 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700308 { "bind", MS_BIND },
309 { "rec", MS_REC },
310 { "unbindable", MS_UNBINDABLE },
311 { "private", MS_PRIVATE },
312 { "slave", MS_SLAVE },
313 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 { "defaults", 0 },
315 { 0, 0 },
316};
317
Ken Sumrall752923c2010-12-03 16:33:31 -0800318#define DATA_MNT_POINT "/data"
319
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700321static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700323 const char *source, *target, *system;
324 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700326 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700328 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700329
Tom Cherry96f67312015-07-30 13:52:55 -0700330 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700331 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700332 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333 flags |= mount_flags[i].flag;
334 break;
335 }
336 }
337
Colin Crosscd0f1732010-04-19 17:10:24 -0700338 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700339 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700340 wait = 1;
341 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700342 else if (na + 1 == args.size())
343 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700344 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700345 }
346
Tom Cherry96f67312015-07-30 13:52:55 -0700347 system = args[1].c_str();
348 source = args[2].c_str();
349 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000350
Elliott Hughes31951162016-06-24 15:15:03 -0700351 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000352 int mode, loop, fd;
353 struct loop_info info;
354
355 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800356 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000357 if (fd < 0) {
358 return -1;
359 }
360
361 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800362 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800363 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100365 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366 return -1;
367 }
368
369 /* if it is a blank loop device */
370 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
371 /* if it becomes our loop device */
372 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
373 close(fd);
374
375 if (mount(tmp, target, system, flags, options) < 0) {
376 ioctl(loop, LOOP_CLR_FD, 0);
377 close(loop);
378 return -1;
379 }
380
381 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800382 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000383 }
384 }
385
386 close(loop);
387 }
388
389 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700390 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391 return -1;
392 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700393 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800394 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000395 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700396 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000397 }
398
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800400
401exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800402 return 0;
403
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404}
405
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800406/* Imports .rc files from the specified paths. Default ones are applied if none is given.
407 *
408 * start_index: index of the first path in the args list
409 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700410static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherry67dee622017-07-27 12:54:48 -0700411 auto& action_manager = ActionManager::GetInstance();
Tom Cherry911b9b12017-07-27 16:20:58 -0700412 auto& service_list = ServiceList::GetInstance();
413 Parser parser = CreateParser(action_manager, service_list);
Wei Wangd61a7e22016-08-23 11:58:09 -0700414 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900415 // Fallbacks for partitions on which early mount isn't enabled.
Tom Cherry67dee622017-07-27 12:54:48 -0700416 for (const auto& path : late_import_paths) {
417 parser.ParseConfig(path);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900418 }
Tom Cherry67dee622017-07-27 12:54:48 -0700419 late_import_paths.clear();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800420 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700421 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800422 parser.ParseConfig(args[i]);
423 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700424 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700425
426 // Turning this on and letting the INFO logging be discarded adds 0.2s to
427 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700428 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700429}
430
Wei Wangd61a7e22016-08-23 11:58:09 -0700431/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800432 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700433 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700434 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700435static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700436 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700437
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700438 /*
439 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
440 * do the call in the child to provide protection to the main init
441 * process if anything goes wrong (crash or memory leak), and wait for
442 * the child to finish in the parent.
443 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700444 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700445 if (pid > 0) {
446 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700447 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700448 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700449 if (wp_ret == -1) {
450 // Unexpected error code. We will continue anyway.
451 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700452 }
453
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700454 if (WIFEXITED(status)) {
455 ret = WEXITSTATUS(status);
456 } else {
457 ret = -1;
458 }
459 } else if (pid == 0) {
460 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700461
462 // So we can always see what fs_mgr_mount_all() does.
463 // Only needed if someone explicitly changes the default log level in their init.rc.
464 android::base::ScopedLogSeverity info(android::base::INFO);
465
466 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700467 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800468 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700469 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700470 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700471 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700472 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700473 } else {
474 /* fork failed, return an error */
475 return -1;
476 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700477 return ret;
478}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700479
Wei Wangd61a7e22016-08-23 11:58:09 -0700480/* Queue event based on fs_mgr return code.
481 *
482 * code: return code of fs_mgr_mount_all
483 *
484 * This function might request a reboot, in which case it will
485 * not return.
486 *
487 * return code is processed based on input code
488 */
489static int queue_fs_event(int code) {
490 int ret = code;
491 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700492 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700493 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700494 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000495 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700496 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700497 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700498 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800499 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700500 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800501 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700502 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700503 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700504 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700505 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800506 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
507 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700508 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700509 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000510 if (e4crypt_install_keyring()) {
511 return -1;
512 }
513 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000514 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000515
516 // Although encrypted, we have device key, so we do not need to
517 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700518 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700519 } else if (code > 0) {
520 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700522 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700523
524 return ret;
525}
526
Wei Wangd61a7e22016-08-23 11:58:09 -0700527/* mount_all <fstab> [ <path> ]* [--<options>]*
528 *
529 * This function might request a reboot, in which case it will
530 * not return.
531 */
532static int do_mount_all(const std::vector<std::string>& args) {
533 std::size_t na = 0;
534 bool import_rc = true;
535 bool queue_event = true;
536 int mount_mode = MOUNT_MODE_DEFAULT;
537 const char* fstabfile = args[1].c_str();
538 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800539 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700540
541 for (na = args.size() - 1; na > 1; --na) {
542 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800543 path_arg_end = na;
544 queue_event = false;
545 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800546 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700547 } else if (args[na] == "--late") {
548 path_arg_end = na;
549 import_rc = false;
550 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800551 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700552 }
553 }
554
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700555 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Tom Cherryede0d532017-07-06 14:20:11 -0700556 android::base::Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700557 int ret = mount_fstab(fstabfile, mount_mode);
Tom Cherryede0d532017-07-06 14:20:11 -0700558 property_set(prop_name, std::to_string(t.duration().count()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700559
560 if (import_rc) {
561 /* Paths of .rc files are specified at the 2nd argument and beyond */
562 import_late(args, 2, path_arg_end);
563 }
564
565 if (queue_event) {
566 /* queue_fs_event will queue event based on mount_fstab return code
567 * and return processed return code*/
568 ret = queue_fs_event(ret);
569 }
570
571 return ret;
572}
573
Tom Cherryb7349902015-08-26 11:43:36 -0700574static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700575 struct fstab *fstab;
576 int ret;
577
Tom Cherry96f67312015-07-30 13:52:55 -0700578 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700579 ret = fs_mgr_swapon_all(fstab);
580 fs_mgr_free_fstab(fstab);
581
582 return ret;
583}
584
Tom Cherryb7349902015-08-26 11:43:36 -0700585static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700586 property_set(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587 return 0;
588}
589
Tom Cherryb7349902015-08-26 11:43:36 -0700590static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700591 struct rlimit limit;
592 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700593 if (android::base::ParseInt(args[1], &resource) &&
594 android::base::ParseUint(args[2], &limit.rlim_cur) &&
595 android::base::ParseUint(args[3], &limit.rlim_max)) {
596 return setrlimit(resource, &limit);
597 }
598 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
599 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600}
601
Tom Cherryb7349902015-08-26 11:43:36 -0700602static int do_start(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700603 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700604 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700605 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700606 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700607 }
Tom Cherrybac32992015-07-31 12:45:25 -0700608 if (!svc->Start())
609 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700610 return 0;
611}
612
Tom Cherryb7349902015-08-26 11:43:36 -0700613static int do_stop(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700614 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700615 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700616 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700617 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700618 }
Tom Cherrybac32992015-07-31 12:45:25 -0700619 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620 return 0;
621}
622
Tom Cherryb7349902015-08-26 11:43:36 -0700623static int do_restart(const std::vector<std::string>& args) {
Tom Cherry911b9b12017-07-27 16:20:58 -0700624 Service* svc = ServiceList::GetInstance().FindService(args[1]);
Tom Cherrybac32992015-07-31 12:45:25 -0700625 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700626 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700627 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700628 }
Tom Cherrybac32992015-07-31 12:45:25 -0700629 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700630 return 0;
631}
632
Tom Cherryb7349902015-08-26 11:43:36 -0700633static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700634 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700635 return 0;
636}
637
Tom Cherryb7349902015-08-26 11:43:36 -0700638static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700639 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640}
641
Tom Cherryb7349902015-08-26 11:43:36 -0700642static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700643 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800644}
645
Tom Cherryb7349902015-08-26 11:43:36 -0700646static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700647 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800648}
649
Tom Cherryb7349902015-08-26 11:43:36 -0700650static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700651 struct timezone tz = {};
652 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
653 return 0;
654 }
655 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800656}
657
Tom Cherryb7349902015-08-26 11:43:36 -0700658static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700659 int mode = -1;
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800660 bool loaded = fs_mgr_load_verity_state(&mode);
661 if (loaded && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700662 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000663 }
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800664 return loaded ? 0 : 1;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000665}
666
Tom Cherryb7349902015-08-26 11:43:36 -0700667static void verity_update_property(fstab_rec *fstab, const char *mount_point,
668 int mode, int status) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700669 property_set("partition."s + mount_point + ".verified", std::to_string(mode));
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000670}
671
Tom Cherryb7349902015-08-26 11:43:36 -0700672static int do_verity_update_state(const std::vector<std::string>& args) {
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800673 return fs_mgr_update_verity_state(verity_update_property) ? 0 : 1;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000674}
675
Tom Cherryb7349902015-08-26 11:43:36 -0700676static int do_write(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700677 std::string err;
678 if (!WriteFile(args[1], args[2], &err)) {
679 LOG(ERROR) << err;
680 return -1;
681 }
682 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700683}
684
Tom Cherryb7349902015-08-26 11:43:36 -0700685static int do_copy(const std::vector<std::string>& args) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800686 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700687 std::string err;
688 if (!ReadFile(args[1], &data, &err)) {
689 LOG(ERROR) << err;
690 return -1;
San Mehat7c44fe52009-08-26 16:39:25 -0700691 }
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700692 if (!WriteFile(args[2], data, &err)) {
693 LOG(ERROR) << err;
694 return -1;
695 }
696 return 0;
San Mehat7c44fe52009-08-26 16:39:25 -0700697}
698
Tom Cherryb7349902015-08-26 11:43:36 -0700699static int do_chown(const std::vector<std::string>& args) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700700 uid_t uid;
701 std::string decode_uid_err;
702 if (!DecodeUid(args[1], &uid, &decode_uid_err)) {
703 LOG(ERROR) << "Unable to find UID for '" << args[1] << "': " << decode_uid_err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704 return -1;
705 }
Tom Cherry517e1f12017-05-04 17:40:33 -0700706
707 // GID is optional and pushes the index of path out by one if specified.
708 const std::string& path = (args.size() == 4) ? args[3] : args[2];
709 gid_t gid = -1;
710
711 if (args.size() == 4) {
712 if (!DecodeUid(args[2], &gid, &decode_uid_err)) {
713 LOG(ERROR) << "Unable to find GID for '" << args[2] << "': " << decode_uid_err;
714 return -1;
715 }
716 }
717
718 if (lchown(path.c_str(), uid, gid) == -1) return -errno;
719
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720 return 0;
721}
722
723static mode_t get_mode(const char *s) {
724 mode_t mode = 0;
725 while (*s) {
726 if (*s >= '0' && *s <= '7') {
727 mode = (mode<<3) | (*s-'0');
728 } else {
729 return -1;
730 }
731 s++;
732 }
733 return mode;
734}
735
Tom Cherryb7349902015-08-26 11:43:36 -0700736static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700737 mode_t mode = get_mode(args[1].c_str());
738 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700739 return -errno;
740 }
741 return 0;
742}
743
Tom Cherryb7349902015-08-26 11:43:36 -0700744static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400745 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500746
Paul Lawrencea8d84342016-11-14 15:40:18 -0800747 struct flag_type {const char* name; int value;};
748 static const flag_type flags[] = {
749 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
750 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
751 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
752 {0, 0}
753 };
754
755 int flag = 0;
756
757 bool in_flags = true;
758 for (size_t i = 1; i < args.size(); ++i) {
759 if (android::base::StartsWith(args[i], "--")) {
760 if (!in_flags) {
761 LOG(ERROR) << "restorecon - flags must precede paths";
762 return -1;
763 }
764 bool found = false;
765 for (size_t j = 0; flags[j].name; ++j) {
766 if (args[i] == flags[j].name) {
767 flag |= flags[j].value;
768 found = true;
769 break;
770 }
771 }
772 if (!found) {
773 LOG(ERROR) << "restorecon - bad flag " << args[i];
774 return -1;
775 }
776 } else {
777 in_flags = false;
Tom Cherry482f36c2017-05-08 13:38:15 -0700778 if (selinux_android_restorecon(args[i].c_str(), flag) < 0) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800779 ret = -errno;
780 }
781 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500782 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400783 return ret;
784}
785
Tom Cherryb7349902015-08-26 11:43:36 -0700786static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800787 std::vector<std::string> non_const_args(args);
788 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
789 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500790}
791
Tom Cherryb7349902015-08-26 11:43:36 -0700792static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700793 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700794 int log_level = -1;
795 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700796 android::base::LogSeverity severity;
797 switch (log_level) {
798 case 7: severity = android::base::DEBUG; break;
799 case 6: severity = android::base::INFO; break;
800 case 5:
801 case 4: severity = android::base::WARNING; break;
802 case 3: severity = android::base::ERROR; break;
803 case 2:
804 case 1:
805 case 0: severity = android::base::FATAL; break;
806 default:
807 LOG(ERROR) << "loglevel: invalid log level " << log_level;
808 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700809 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700810 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700811 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700812}
813
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700814static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700815 load_persist_props();
816 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800817}
818
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700819static int do_load_system_props(const std::vector<std::string>& args) {
820 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700821 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700822}
823
Tom Cherryb7349902015-08-26 11:43:36 -0700824static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700825 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800826 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700827 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700828 int timeout;
829 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800830 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700831 }
832 }
833 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700834}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000835
Wei Wang132ac312017-01-25 16:27:03 -0800836static int do_wait_for_prop(const std::vector<std::string>& args) {
837 const char* name = args[1].c_str();
838 const char* value = args[2].c_str();
839 size_t value_len = strlen(value);
840
841 if (!is_legal_property_name(name)) {
842 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
843 << "\") failed: bad name";
844 return -1;
845 }
846 if (value_len >= PROP_VALUE_MAX) {
847 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
848 << "\") failed: value too long";
849 return -1;
850 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800851 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800852 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
853 << "\") failed: init already in waiting";
854 return -1;
855 }
856 return 0;
857}
858
Paul Lawrence806d10b2015-04-28 22:07:10 +0000859/*
860 * Callback to make a directory from the ext4 code
861 */
Tom Cherryb7349902015-08-26 11:43:36 -0700862static int do_installkeys_ensure_dir_exists(const char* dir) {
Tom Cherrye7656b72017-05-01 17:10:09 -0700863 if (make_dir(dir, 0700, sehandle) && errno != EEXIST) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000864 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000865 }
866
Paul Lawrence806d10b2015-04-28 22:07:10 +0000867 return 0;
868}
869
Paul Crowley749af8c2015-05-28 17:35:06 +0100870static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700871 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100872}
873
Tom Cherryb7349902015-08-26 11:43:36 -0700874static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100875 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000876 return 0;
877 }
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700878 auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder;
879 if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) {
880 PLOG(ERROR) << "Failed to create " << unencrypted_dir;
881 return -1;
882 }
883 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
884 "enablefilecrypto"};
885 return do_exec(exec_args);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000886}
Paul Crowley749af8c2015-05-28 17:35:06 +0100887
Paul Crowley59497452016-02-01 16:37:13 +0000888static int do_init_user0(const std::vector<std::string>& args) {
Tom Cherry5a86cb72017-05-03 13:19:03 -0700889 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
890 "init_user0"};
891 return do_exec(exec_args);
Paul Crowley59497452016-02-01 16:37:13 +0000892}
893
Tom Cherryad54d092017-04-19 16:18:50 -0700894const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
Tom Cherryb7349902015-08-26 11:43:36 -0700895 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -0700896 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700897 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800898 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700899 {"chmod", {2, 2, do_chmod}},
900 {"chown", {2, 3, do_chown}},
901 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800902 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700903 {"class_start", {1, 1, do_class_start}},
904 {"class_stop", {1, 1, do_class_stop}},
905 {"copy", {2, 2, do_copy}},
906 {"domainname", {1, 1, do_domainname}},
907 {"enable", {1, 1, do_enable}},
908 {"exec", {1, kMax, do_exec}},
Tom Cherryb27004a2017-03-27 16:27:30 -0700909 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700910 {"export", {2, 2, do_export}},
911 {"hostname", {1, 1, do_hostname}},
912 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000913 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700914 {"insmod", {1, kMax, do_insmod}},
915 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700916 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700917 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700918 {"loglevel", {1, 1, do_loglevel}},
919 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800920 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700921 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700922 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700923 {"restart", {1, 1, do_restart}},
924 {"restorecon", {1, kMax, do_restorecon}},
925 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
926 {"rm", {1, 1, do_rm}},
927 {"rmdir", {1, 1, do_rmdir}},
928 {"setprop", {2, 2, do_setprop}},
929 {"setrlimit", {3, 3, do_setrlimit}},
930 {"start", {1, 1, do_start}},
931 {"stop", {1, 1, do_stop}},
932 {"swapon_all", {1, 1, do_swapon_all}},
933 {"symlink", {2, 2, do_symlink}},
934 {"sysclktz", {1, 1, do_sysclktz}},
935 {"trigger", {1, 1, do_trigger}},
936 {"verity_load_state", {0, 0, do_verity_load_state}},
937 {"verity_update_state", {0, 0, do_verity_update_state}},
938 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800939 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700940 {"write", {2, 2, do_write}},
941 };
Tom Cherryb27004a2017-03-27 16:27:30 -0700942 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700943 return builtin_functions;
944}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700945
946} // namespace init
947} // namespace android