blob: f687b6ca33635933dcfae4f5cba5c93edec99c88 [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
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070042#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070043#include <android-base/logging.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080044#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070045#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080046#include <android-base/stringprintf.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"
Colin Cross6310a822010-04-20 14:29:05 -070060#include "init_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
Elliott Hughes9605a942016-11-10 17:43:47 -080071static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080072
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080073static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070074 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
75 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070076 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080078 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080079 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070080 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070081 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070082 }
83 close(fd);
84 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085}
86
Tom Cherryb7349902015-08-26 11:43:36 -070087static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088 struct ifreq ifr;
89 int s, ret;
90
91 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
92
93 s = socket(AF_INET, SOCK_DGRAM, 0);
94 if (s < 0)
95 return -1;
96
97 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
98 if (ret < 0) {
99 goto done;
100 }
101
102 if (up)
103 ifr.ifr_flags |= IFF_UP;
104 else
105 ifr.ifr_flags &= ~IFF_UP;
106
107 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800108
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109done:
110 close(s);
111 return ret;
112}
113
Paul Crowley0b8b2302016-12-19 13:03:47 -0800114static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700115 std::string err;
116 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700117 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700118 return -1;
119 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700120 DoReboot(ANDROID_RB_RESTART2, "reboot", "recovery", false);
121 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700122}
123
Tom Cherryb7349902015-08-26 11:43:36 -0700124static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125 /* Starting a class does not start services
126 * which are explicitly disabled. They must
127 * be started individually.
128 */
Tom Cherrybac32992015-07-31 12:45:25 -0700129 ServiceManager::GetInstance().
130 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700131 return 0;
132}
133
Tom Cherryb7349902015-08-26 11:43:36 -0700134static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700135 ServiceManager::GetInstance().
136 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700137 return 0;
138}
139
Tom Cherryb7349902015-08-26 11:43:36 -0700140static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700141 ServiceManager::GetInstance().
142 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800143 return 0;
144}
145
Steven Moreland2b63d542017-03-10 14:04:37 -0800146static int do_class_restart(const std::vector<std::string>& args) {
147 ServiceManager::GetInstance().
148 ForEachServiceInClass(args[1], [] (Service* s) { s->Restart(); });
149 return 0;
150}
151
Tom Cherryb7349902015-08-26 11:43:36 -0700152static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry53089aa2017-03-31 15:47:33 -0700153 return write_file("/proc/sys/kernel/domainname", args[1]) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700154}
155
Tom Cherryb7349902015-08-26 11:43:36 -0700156static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700157 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
158 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700159 return -1;
160 }
Tom Cherrybac32992015-07-31 12:45:25 -0700161 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700162}
163
Tom Cherryb7349902015-08-26 11:43:36 -0700164static int do_exec(const std::vector<std::string>& args) {
Tom Cherryb27004a2017-03-27 16:27:30 -0700165 return ServiceManager::GetInstance().Exec(args) ? 0 : -1;
166}
167
168static int do_exec_start(const std::vector<std::string>& args) {
169 return ServiceManager::GetInstance().ExecStart(args[1]) ? 0 : -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700170}
171
Tom Cherryb7349902015-08-26 11:43:36 -0700172static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700173 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174}
175
Tom Cherryb7349902015-08-26 11:43:36 -0700176static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry53089aa2017-03-31 15:47:33 -0700177 return write_file("/proc/sys/kernel/hostname", args[1]) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178}
179
Tom Cherryb7349902015-08-26 11:43:36 -0700180static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700181 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182}
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800185 int flags = 0;
186 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800187
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800188 if (!(*it).compare("-f")) {
189 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
190 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800191 }
192
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800193 std::string filename = *it++;
194 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
195 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800196}
197
Tom Cherryb7349902015-08-26 11:43:36 -0700198static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700199 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700200 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700201
202 /* mkdir <path> [mode] [owner] [group] */
203
Tom Cherry96f67312015-07-30 13:52:55 -0700204 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700205 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206 }
207
Tom Cherry96f67312015-07-30 13:52:55 -0700208 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700209 /* chmod in case the directory already exists */
210 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700211 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700212 }
213 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 return -errno;
215 }
216
Tom Cherry96f67312015-07-30 13:52:55 -0700217 if (args.size() >= 4) {
218 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 gid_t gid = -1;
220
Tom Cherry96f67312015-07-30 13:52:55 -0700221 if (args.size() == 5) {
222 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 }
224
Tom Cherry96f67312015-07-30 13:52:55 -0700225 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 return -errno;
227 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700228
229 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
230 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700231 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700232 if (ret == -1) {
233 return -errno;
234 }
235 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236 }
237
Paul Crowleyaf8be582016-05-10 08:52:06 -0700238 if (e4crypt_is_native()) {
239 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800240 const std::vector<std::string> options = {
241 "--prompt_and_wipe_data",
242 "--reason=set_policy_failed:"s + args[1]};
243 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700244 return -1;
245 }
246 }
247 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248}
249
Alex Light68ab20f2016-06-23 11:11:39 -0700250/* umount <path> */
251static int do_umount(const std::vector<std::string>& args) {
252 return umount(args[1].c_str());
253}
254
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255static struct {
256 const char *name;
257 unsigned flag;
258} mount_flags[] = {
259 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200260 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261 { "nosuid", MS_NOSUID },
262 { "nodev", MS_NODEV },
263 { "nodiratime", MS_NODIRATIME },
264 { "ro", MS_RDONLY },
265 { "rw", 0 },
266 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700267 { "bind", MS_BIND },
268 { "rec", MS_REC },
269 { "unbindable", MS_UNBINDABLE },
270 { "private", MS_PRIVATE },
271 { "slave", MS_SLAVE },
272 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273 { "defaults", 0 },
274 { 0, 0 },
275};
276
Ken Sumrall752923c2010-12-03 16:33:31 -0800277#define DATA_MNT_POINT "/data"
278
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700280static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700282 const char *source, *target, *system;
283 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700285 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700287 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288
Tom Cherry96f67312015-07-30 13:52:55 -0700289 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700291 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292 flags |= mount_flags[i].flag;
293 break;
294 }
295 }
296
Colin Crosscd0f1732010-04-19 17:10:24 -0700297 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700298 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700299 wait = 1;
300 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700301 else if (na + 1 == args.size())
302 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700303 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 }
305
Tom Cherry96f67312015-07-30 13:52:55 -0700306 system = args[1].c_str();
307 source = args[2].c_str();
308 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000309
Elliott Hughes31951162016-06-24 15:15:03 -0700310 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000311 int mode, loop, fd;
312 struct loop_info info;
313
314 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800315 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000316 if (fd < 0) {
317 return -1;
318 }
319
320 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800321 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800322 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000323 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100324 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000325 return -1;
326 }
327
328 /* if it is a blank loop device */
329 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
330 /* if it becomes our loop device */
331 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
332 close(fd);
333
334 if (mount(tmp, target, system, flags, options) < 0) {
335 ioctl(loop, LOOP_CLR_FD, 0);
336 close(loop);
337 return -1;
338 }
339
340 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800341 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000342 }
343 }
344
345 close(loop);
346 }
347
348 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700349 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000350 return -1;
351 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700352 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800353 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000354 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700355 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000356 }
357
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800359
360exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800361 return 0;
362
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363}
364
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800365/* Imports .rc files from the specified paths. Default ones are applied if none is given.
366 *
367 * start_index: index of the first path in the args list
368 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700369static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700370 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700371 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900372 // Fallbacks for partitions on which early mount isn't enabled.
373 if (!parser.is_system_etc_init_loaded()) {
374 parser.ParseConfig("/system/etc/init");
375 parser.set_is_system_etc_init_loaded(true);
376 }
377 if (!parser.is_vendor_etc_init_loaded()) {
378 parser.ParseConfig("/vendor/etc/init");
379 parser.set_is_vendor_etc_init_loaded(true);
380 }
381 if (!parser.is_odm_etc_init_loaded()) {
382 parser.ParseConfig("/odm/etc/init");
383 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800384 }
385 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700386 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800387 parser.ParseConfig(args[i]);
388 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700389 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700390
391 // Turning this on and letting the INFO logging be discarded adds 0.2s to
392 // Nexus 9 boot time, so it's disabled by default.
393 if (false) parser.DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700394}
395
Wei Wangd61a7e22016-08-23 11:58:09 -0700396/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800397 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700398 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700399 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700400static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700401 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700402
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700403 /*
404 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
405 * do the call in the child to provide protection to the main init
406 * process if anything goes wrong (crash or memory leak), and wait for
407 * the child to finish in the parent.
408 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700409 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700410 if (pid > 0) {
411 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700412 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700413 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700414 if (wp_ret == -1) {
415 // Unexpected error code. We will continue anyway.
416 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700417 }
418
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700419 if (WIFEXITED(status)) {
420 ret = WEXITSTATUS(status);
421 } else {
422 ret = -1;
423 }
424 } else if (pid == 0) {
425 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700426
427 // So we can always see what fs_mgr_mount_all() does.
428 // Only needed if someone explicitly changes the default log level in their init.rc.
429 android::base::ScopedLogSeverity info(android::base::INFO);
430
431 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700432 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800433 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700434 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700435 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700436 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700437 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700438 } else {
439 /* fork failed, return an error */
440 return -1;
441 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700442 return ret;
443}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700444
Wei Wangd61a7e22016-08-23 11:58:09 -0700445/* Queue event based on fs_mgr return code.
446 *
447 * code: return code of fs_mgr_mount_all
448 *
449 * This function might request a reboot, in which case it will
450 * not return.
451 *
452 * return code is processed based on input code
453 */
454static int queue_fs_event(int code) {
455 int ret = code;
456 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700457 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700458 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700459 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000460 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700461 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700462 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700463 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800464 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700465 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800466 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700467 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700468 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700469 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700470 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800471 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
472 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700473 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700474 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000475 if (e4crypt_install_keyring()) {
476 return -1;
477 }
478 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000479 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000480
481 // Although encrypted, we have device key, so we do not need to
482 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700483 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrence9dbe97b2017-04-21 12:41:48 -0700484 } else if (code == FS_MGR_MNTALL_DEV_IS_METADATA_ENCRYPTED) {
485 if (e4crypt_install_keyring()) {
486 return -1;
487 }
488 property_set("ro.crypto.state", "encrypted");
489 property_set("ro.crypto.type", "file");
490
491 // defaultcrypto detects file/block encryption. init flow is same for each.
492 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
493 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_METADATA_ENCRYPTION) {
494 if (e4crypt_install_keyring()) {
495 return -1;
496 }
497 property_set("ro.crypto.type", "file");
498
499 // encrypt detects file/block encryption. init flow is same for each.
500 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700501 } else if (code > 0) {
502 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700503 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700504 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700505
506 return ret;
507}
508
Wei Wangd61a7e22016-08-23 11:58:09 -0700509/* mount_all <fstab> [ <path> ]* [--<options>]*
510 *
511 * This function might request a reboot, in which case it will
512 * not return.
513 */
514static int do_mount_all(const std::vector<std::string>& args) {
515 std::size_t na = 0;
516 bool import_rc = true;
517 bool queue_event = true;
518 int mount_mode = MOUNT_MODE_DEFAULT;
519 const char* fstabfile = args[1].c_str();
520 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800521 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700522
523 for (na = args.size() - 1; na > 1; --na) {
524 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800525 path_arg_end = na;
526 queue_event = false;
527 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800528 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700529 } else if (args[na] == "--late") {
530 path_arg_end = na;
531 import_rc = false;
532 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800533 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700534 }
535 }
536
Keun-young Park69fae7b2017-03-03 18:57:09 -0800537 std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s",
538 prop_post_fix);
539 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700540 int ret = mount_fstab(fstabfile, mount_mode);
Keun-young Park69fae7b2017-03-03 18:57:09 -0800541 property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str());
Wei Wangd61a7e22016-08-23 11:58:09 -0700542
543 if (import_rc) {
544 /* Paths of .rc files are specified at the 2nd argument and beyond */
545 import_late(args, 2, path_arg_end);
546 }
547
548 if (queue_event) {
549 /* queue_fs_event will queue event based on mount_fstab return code
550 * and return processed return code*/
551 ret = queue_fs_event(ret);
552 }
553
554 return ret;
555}
556
Tom Cherryb7349902015-08-26 11:43:36 -0700557static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700558 struct fstab *fstab;
559 int ret;
560
Tom Cherry96f67312015-07-30 13:52:55 -0700561 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700562 ret = fs_mgr_swapon_all(fstab);
563 fs_mgr_free_fstab(fstab);
564
565 return ret;
566}
567
Tom Cherryb7349902015-08-26 11:43:36 -0700568static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700569 const char* name = args[1].c_str();
570 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700571 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 return 0;
573}
574
Tom Cherryb7349902015-08-26 11:43:36 -0700575static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700576 struct rlimit limit;
577 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700578 if (android::base::ParseInt(args[1], &resource) &&
579 android::base::ParseUint(args[2], &limit.rlim_cur) &&
580 android::base::ParseUint(args[3], &limit.rlim_max)) {
581 return setrlimit(resource, &limit);
582 }
583 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
584 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585}
586
Tom Cherryb7349902015-08-26 11:43:36 -0700587static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700588 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
589 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700590 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700591 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 }
Tom Cherrybac32992015-07-31 12:45:25 -0700593 if (!svc->Start())
594 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595 return 0;
596}
597
Tom Cherryb7349902015-08-26 11:43:36 -0700598static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700599 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
600 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700601 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700602 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700603 }
Tom Cherrybac32992015-07-31 12:45:25 -0700604 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700605 return 0;
606}
607
Tom Cherryb7349902015-08-26 11:43:36 -0700608static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700609 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
610 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700611 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700612 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700613 }
Tom Cherrybac32992015-07-31 12:45:25 -0700614 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700615 return 0;
616}
617
Tom Cherryb7349902015-08-26 11:43:36 -0700618static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700619 ActionManager::GetInstance().QueueEventTrigger(args[1]);
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_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700624 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700625}
626
Tom Cherryb7349902015-08-26 11:43:36 -0700627static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700628 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800629}
630
Tom Cherryb7349902015-08-26 11:43:36 -0700631static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700632 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800633}
634
Tom Cherryb7349902015-08-26 11:43:36 -0700635static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700636 struct timezone tz = {};
637 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
638 return 0;
639 }
640 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800641}
642
Tom Cherryb7349902015-08-26 11:43:36 -0700643static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700644 int mode = -1;
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800645 bool loaded = fs_mgr_load_verity_state(&mode);
646 if (loaded && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700647 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000648 }
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800649 return loaded ? 0 : 1;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000650}
651
Tom Cherryb7349902015-08-26 11:43:36 -0700652static void verity_update_property(fstab_rec *fstab, const char *mount_point,
653 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100654 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
655 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000656}
657
Tom Cherryb7349902015-08-26 11:43:36 -0700658static int do_verity_update_state(const std::vector<std::string>& args) {
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800659 return fs_mgr_update_verity_state(verity_update_property) ? 0 : 1;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000660}
661
Tom Cherryb7349902015-08-26 11:43:36 -0700662static int do_write(const std::vector<std::string>& args) {
Tom Cherry53089aa2017-03-31 15:47:33 -0700663 return write_file(args[1], args[2]) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664}
665
Tom Cherryb7349902015-08-26 11:43:36 -0700666static int do_copy(const std::vector<std::string>& args) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800667 std::string data;
668 if (read_file(args[1], &data)) {
669 return write_file(args[2], data) ? 0 : 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700670 }
Yongqin Liudbe88e72016-12-28 16:06:19 +0800671 return 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700672}
673
Tom Cherryb7349902015-08-26 11:43:36 -0700674static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700675 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700676 if (args.size() == 3) {
677 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700678 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700679 } else if (args.size() == 4) {
680 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
681 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682 return -errno;
683 } else {
684 return -1;
685 }
686 return 0;
687}
688
689static mode_t get_mode(const char *s) {
690 mode_t mode = 0;
691 while (*s) {
692 if (*s >= '0' && *s <= '7') {
693 mode = (mode<<3) | (*s-'0');
694 } else {
695 return -1;
696 }
697 s++;
698 }
699 return mode;
700}
701
Tom Cherryb7349902015-08-26 11:43:36 -0700702static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700703 mode_t mode = get_mode(args[1].c_str());
704 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705 return -errno;
706 }
707 return 0;
708}
709
Tom Cherryb7349902015-08-26 11:43:36 -0700710static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400711 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500712
Paul Lawrencea8d84342016-11-14 15:40:18 -0800713 struct flag_type {const char* name; int value;};
714 static const flag_type flags[] = {
715 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
716 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
717 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
718 {0, 0}
719 };
720
721 int flag = 0;
722
723 bool in_flags = true;
724 for (size_t i = 1; i < args.size(); ++i) {
725 if (android::base::StartsWith(args[i], "--")) {
726 if (!in_flags) {
727 LOG(ERROR) << "restorecon - flags must precede paths";
728 return -1;
729 }
730 bool found = false;
731 for (size_t j = 0; flags[j].name; ++j) {
732 if (args[i] == flags[j].name) {
733 flag |= flags[j].value;
734 found = true;
735 break;
736 }
737 }
738 if (!found) {
739 LOG(ERROR) << "restorecon - bad flag " << args[i];
740 return -1;
741 }
742 } else {
743 in_flags = false;
744 if (restorecon(args[i].c_str(), flag) < 0) {
745 ret = -errno;
746 }
747 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500748 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400749 return ret;
750}
751
Tom Cherryb7349902015-08-26 11:43:36 -0700752static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800753 std::vector<std::string> non_const_args(args);
754 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
755 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500756}
757
Tom Cherryb7349902015-08-26 11:43:36 -0700758static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700759 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700760 int log_level = -1;
761 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700762 android::base::LogSeverity severity;
763 switch (log_level) {
764 case 7: severity = android::base::DEBUG; break;
765 case 6: severity = android::base::INFO; break;
766 case 5:
767 case 4: severity = android::base::WARNING; break;
768 case 3: severity = android::base::ERROR; break;
769 case 2:
770 case 1:
771 case 0: severity = android::base::FATAL; break;
772 default:
773 LOG(ERROR) << "loglevel: invalid log level " << log_level;
774 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700775 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700776 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700777 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700778}
779
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700780static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700781 load_persist_props();
782 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800783}
784
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700785static int do_load_system_props(const std::vector<std::string>& args) {
786 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700787 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700788}
789
Tom Cherryb7349902015-08-26 11:43:36 -0700790static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700791 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800792 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700793 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700794 int timeout;
795 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800796 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700797 }
798 }
799 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700800}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000801
Wei Wang132ac312017-01-25 16:27:03 -0800802static int do_wait_for_prop(const std::vector<std::string>& args) {
803 const char* name = args[1].c_str();
804 const char* value = args[2].c_str();
805 size_t value_len = strlen(value);
806
807 if (!is_legal_property_name(name)) {
808 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
809 << "\") failed: bad name";
810 return -1;
811 }
812 if (value_len >= PROP_VALUE_MAX) {
813 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
814 << "\") failed: value too long";
815 return -1;
816 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800817 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800818 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
819 << "\") failed: init already in waiting";
820 return -1;
821 }
822 return 0;
823}
824
Paul Lawrence806d10b2015-04-28 22:07:10 +0000825/*
826 * Callback to make a directory from the ext4 code
827 */
Tom Cherryb7349902015-08-26 11:43:36 -0700828static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000829 if (make_dir(dir, 0700) && errno != EEXIST) {
830 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000831 }
832
Paul Lawrence806d10b2015-04-28 22:07:10 +0000833 return 0;
834}
835
Paul Crowley749af8c2015-05-28 17:35:06 +0100836static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700837 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100838}
839
Tom Cherryb7349902015-08-26 11:43:36 -0700840static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100841 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000842 return 0;
843 }
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700844 auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder;
845 if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) {
846 PLOG(ERROR) << "Failed to create " << unencrypted_dir;
847 return -1;
848 }
849 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
850 "enablefilecrypto"};
851 return do_exec(exec_args);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000852}
Paul Crowley749af8c2015-05-28 17:35:06 +0100853
Paul Crowley59497452016-02-01 16:37:13 +0000854static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000855 return e4crypt_do_init_user0();
856}
857
Tom Cherryb7349902015-08-26 11:43:36 -0700858BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
859 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -0700860 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700861 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800862 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700863 {"chmod", {2, 2, do_chmod}},
864 {"chown", {2, 3, do_chown}},
865 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800866 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700867 {"class_start", {1, 1, do_class_start}},
868 {"class_stop", {1, 1, do_class_stop}},
869 {"copy", {2, 2, do_copy}},
870 {"domainname", {1, 1, do_domainname}},
871 {"enable", {1, 1, do_enable}},
872 {"exec", {1, kMax, do_exec}},
Tom Cherryb27004a2017-03-27 16:27:30 -0700873 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700874 {"export", {2, 2, do_export}},
875 {"hostname", {1, 1, do_hostname}},
876 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000877 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700878 {"insmod", {1, kMax, do_insmod}},
879 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700880 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700881 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700882 {"loglevel", {1, 1, do_loglevel}},
883 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800884 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700885 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700886 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700887 {"restart", {1, 1, do_restart}},
888 {"restorecon", {1, kMax, do_restorecon}},
889 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
890 {"rm", {1, 1, do_rm}},
891 {"rmdir", {1, 1, do_rmdir}},
892 {"setprop", {2, 2, do_setprop}},
893 {"setrlimit", {3, 3, do_setrlimit}},
894 {"start", {1, 1, do_start}},
895 {"stop", {1, 1, do_stop}},
896 {"swapon_all", {1, 1, do_swapon_all}},
897 {"symlink", {2, 2, do_symlink}},
898 {"sysclktz", {1, 1, do_sysclktz}},
899 {"trigger", {1, 1, do_trigger}},
900 {"verity_load_state", {0, 0, do_verity_load_state}},
901 {"verity_update_state", {0, 0, do_verity_update_state}},
902 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800903 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700904 {"write", {2, 2, do_write}},
905 };
Tom Cherryb27004a2017-03-27 16:27:30 -0700906 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700907 return builtin_functions;
908}