blob: c78bec7dbecb15532c25d7131f228e3ef1c9c8ed [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>
Tom Cherryccf23532017-03-28 16:40:41 -070046#include <android-base/strings.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070047#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070048#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070049#include <ext4_utils/ext4_crypt.h>
50#include <ext4_utils/ext4_crypt_init_extensions.h>
Tom Cherryccf23532017-03-28 16:40:41 -070051#include <fs_mgr.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070052#include <selinux/android.h>
53#include <selinux/label.h>
54#include <selinux/selinux.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070055
Tom Cherryfa0c21c2015-07-23 17:53:11 -070056#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070057#include "bootchart.h"
Tom Cherrybac32992015-07-31 12:45:25 -070058#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070059#include "init_parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070060#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070061#include "reboot.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080063#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070064#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065
Paul Crowley0b8b2302016-12-19 13:03:47 -080066using namespace std::literals::string_literals;
67
Nick Kralevichbc609542015-01-31 21:39:46 -080068#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
69
Elliott Hughes9605a942016-11-10 17:43:47 -080070static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080071
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080072static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070073 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
74 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070075 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080077 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080078 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070079 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070080 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070081 }
82 close(fd);
83 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084}
85
Tom Cherryb7349902015-08-26 11:43:36 -070086static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087 struct ifreq ifr;
88 int s, ret;
89
90 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
91
92 s = socket(AF_INET, SOCK_DGRAM, 0);
93 if (s < 0)
94 return -1;
95
96 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
97 if (ret < 0) {
98 goto done;
99 }
100
101 if (up)
102 ifr.ifr_flags |= IFF_UP;
103 else
104 ifr.ifr_flags &= ~IFF_UP;
105
106 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800107
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108done:
109 close(s);
110 return ret;
111}
112
Paul Crowley0b8b2302016-12-19 13:03:47 -0800113static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700114 std::string err;
115 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700116 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700117 return -1;
118 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700119 DoReboot(ANDROID_RB_RESTART2, "reboot", "recovery", false);
120 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700121}
122
Tom Cherryb7349902015-08-26 11:43:36 -0700123static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700124 /* Starting a class does not start services
125 * which are explicitly disabled. They must
126 * be started individually.
127 */
Tom Cherrybac32992015-07-31 12:45:25 -0700128 ServiceManager::GetInstance().
129 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700130 return 0;
131}
132
Tom Cherryb7349902015-08-26 11:43:36 -0700133static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700134 ServiceManager::GetInstance().
135 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700136 return 0;
137}
138
Tom Cherryb7349902015-08-26 11:43:36 -0700139static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700140 ServiceManager::GetInstance().
141 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800142 return 0;
143}
144
Steven Moreland2b63d542017-03-10 14:04:37 -0800145static int do_class_restart(const std::vector<std::string>& args) {
146 ServiceManager::GetInstance().
147 ForEachServiceInClass(args[1], [] (Service* s) { s->Restart(); });
148 return 0;
149}
150
Tom Cherryb7349902015-08-26 11:43:36 -0700151static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700152 std::string err;
153 if (!WriteFile("/proc/sys/kernel/domainname", args[1], &err)) {
154 LOG(ERROR) << err;
155 return -1;
156 }
157 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158}
159
Tom Cherryb7349902015-08-26 11:43:36 -0700160static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700161 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
162 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700163 return -1;
164 }
Tom Cherrybac32992015-07-31 12:45:25 -0700165 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700166}
167
Tom Cherryb7349902015-08-26 11:43:36 -0700168static int do_exec(const std::vector<std::string>& args) {
Tom Cherryb27004a2017-03-27 16:27:30 -0700169 return ServiceManager::GetInstance().Exec(args) ? 0 : -1;
170}
171
172static int do_exec_start(const std::vector<std::string>& args) {
173 return ServiceManager::GetInstance().ExecStart(args[1]) ? 0 : -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174}
175
Tom Cherryb7349902015-08-26 11:43:36 -0700176static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700177 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178}
179
Tom Cherryb7349902015-08-26 11:43:36 -0700180static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700181 std::string err;
182 if (!WriteFile("/proc/sys/kernel/hostname", args[1], &err)) {
183 LOG(ERROR) << err;
184 return -1;
185 }
186 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187}
188
Tom Cherryb7349902015-08-26 11:43:36 -0700189static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700190 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191}
192
Tom Cherryb7349902015-08-26 11:43:36 -0700193static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800194 int flags = 0;
195 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800196
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800197 if (!(*it).compare("-f")) {
198 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
199 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800200 }
201
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800202 std::string filename = *it++;
203 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
204 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800205}
206
Tom Cherryb7349902015-08-26 11:43:36 -0700207static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700208 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700209 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210
211 /* mkdir <path> [mode] [owner] [group] */
212
Tom Cherry96f67312015-07-30 13:52:55 -0700213 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700214 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215 }
216
Tom Cherrye7656b72017-05-01 17:10:09 -0700217 ret = make_dir(args[1].c_str(), mode, sehandle);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700218 /* chmod in case the directory already exists */
219 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700220 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700221 }
222 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 return -errno;
224 }
225
Tom Cherry96f67312015-07-30 13:52:55 -0700226 if (args.size() >= 4) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700227 uid_t uid;
228 std::string decode_uid_err;
229 if (!DecodeUid(args[3], &uid, &decode_uid_err)) {
230 LOG(ERROR) << "Unable to find UID for '" << args[3] << "': " << decode_uid_err;
231 return -1;
232 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233 gid_t gid = -1;
234
Tom Cherry96f67312015-07-30 13:52:55 -0700235 if (args.size() == 5) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700236 if (!DecodeUid(args[4], &gid, &decode_uid_err)) {
237 LOG(ERROR) << "Unable to find GID for '" << args[3] << "': " << decode_uid_err;
238 return -1;
239 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240 }
241
Tom Cherry96f67312015-07-30 13:52:55 -0700242 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243 return -errno;
244 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700245
246 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
247 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700248 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700249 if (ret == -1) {
250 return -errno;
251 }
252 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700253 }
254
Paul Crowleyaf8be582016-05-10 08:52:06 -0700255 if (e4crypt_is_native()) {
256 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800257 const std::vector<std::string> options = {
258 "--prompt_and_wipe_data",
259 "--reason=set_policy_failed:"s + args[1]};
260 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700261 return -1;
262 }
263 }
264 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265}
266
Alex Light68ab20f2016-06-23 11:11:39 -0700267/* umount <path> */
268static int do_umount(const std::vector<std::string>& args) {
269 return umount(args[1].c_str());
270}
271
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272static struct {
273 const char *name;
274 unsigned flag;
275} mount_flags[] = {
276 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200277 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700278 { "nosuid", MS_NOSUID },
279 { "nodev", MS_NODEV },
280 { "nodiratime", MS_NODIRATIME },
281 { "ro", MS_RDONLY },
282 { "rw", 0 },
283 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700284 { "bind", MS_BIND },
285 { "rec", MS_REC },
286 { "unbindable", MS_UNBINDABLE },
287 { "private", MS_PRIVATE },
288 { "slave", MS_SLAVE },
289 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290 { "defaults", 0 },
291 { 0, 0 },
292};
293
Ken Sumrall752923c2010-12-03 16:33:31 -0800294#define DATA_MNT_POINT "/data"
295
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700297static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700299 const char *source, *target, *system;
300 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700302 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700304 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305
Tom Cherry96f67312015-07-30 13:52:55 -0700306 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700308 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 flags |= mount_flags[i].flag;
310 break;
311 }
312 }
313
Colin Crosscd0f1732010-04-19 17:10:24 -0700314 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700315 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700316 wait = 1;
317 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700318 else if (na + 1 == args.size())
319 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700320 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 }
322
Tom Cherry96f67312015-07-30 13:52:55 -0700323 system = args[1].c_str();
324 source = args[2].c_str();
325 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000326
Elliott Hughes31951162016-06-24 15:15:03 -0700327 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000328 int mode, loop, fd;
329 struct loop_info info;
330
331 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800332 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000333 if (fd < 0) {
334 return -1;
335 }
336
337 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800338 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800339 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000340 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100341 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000342 return -1;
343 }
344
345 /* if it is a blank loop device */
346 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
347 /* if it becomes our loop device */
348 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
349 close(fd);
350
351 if (mount(tmp, target, system, flags, options) < 0) {
352 ioctl(loop, LOOP_CLR_FD, 0);
353 close(loop);
354 return -1;
355 }
356
357 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800358 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359 }
360 }
361
362 close(loop);
363 }
364
365 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700366 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 return -1;
368 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700369 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800370 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000371 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700372 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373 }
374
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800376
377exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800378 return 0;
379
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380}
381
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800382/* Imports .rc files from the specified paths. Default ones are applied if none is given.
383 *
384 * start_index: index of the first path in the args list
385 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700386static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700387 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700388 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900389 // Fallbacks for partitions on which early mount isn't enabled.
390 if (!parser.is_system_etc_init_loaded()) {
391 parser.ParseConfig("/system/etc/init");
392 parser.set_is_system_etc_init_loaded(true);
393 }
394 if (!parser.is_vendor_etc_init_loaded()) {
395 parser.ParseConfig("/vendor/etc/init");
396 parser.set_is_vendor_etc_init_loaded(true);
397 }
398 if (!parser.is_odm_etc_init_loaded()) {
399 parser.ParseConfig("/odm/etc/init");
400 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800401 }
402 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700403 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800404 parser.ParseConfig(args[i]);
405 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700406 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700407
408 // Turning this on and letting the INFO logging be discarded adds 0.2s to
409 // Nexus 9 boot time, so it's disabled by default.
Tom Cherry30a6f272017-04-19 15:31:58 -0700410 if (false) DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700411}
412
Wei Wangd61a7e22016-08-23 11:58:09 -0700413/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800414 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700415 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700416 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700417static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700418 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700419
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700420 /*
421 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
422 * do the call in the child to provide protection to the main init
423 * process if anything goes wrong (crash or memory leak), and wait for
424 * the child to finish in the parent.
425 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700426 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700427 if (pid > 0) {
428 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700429 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700430 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700431 if (wp_ret == -1) {
432 // Unexpected error code. We will continue anyway.
433 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700434 }
435
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700436 if (WIFEXITED(status)) {
437 ret = WEXITSTATUS(status);
438 } else {
439 ret = -1;
440 }
441 } else if (pid == 0) {
442 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700443
444 // So we can always see what fs_mgr_mount_all() does.
445 // Only needed if someone explicitly changes the default log level in their init.rc.
446 android::base::ScopedLogSeverity info(android::base::INFO);
447
448 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700449 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800450 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700451 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700452 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700453 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700454 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700455 } else {
456 /* fork failed, return an error */
457 return -1;
458 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700459 return ret;
460}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700461
Wei Wangd61a7e22016-08-23 11:58:09 -0700462/* Queue event based on fs_mgr return code.
463 *
464 * code: return code of fs_mgr_mount_all
465 *
466 * This function might request a reboot, in which case it will
467 * not return.
468 *
469 * return code is processed based on input code
470 */
471static int queue_fs_event(int code) {
472 int ret = code;
473 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700474 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700475 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700476 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000477 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700478 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700479 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700480 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800481 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700482 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800483 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700484 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700485 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700486 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700487 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800488 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
489 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700490 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700491 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000492 if (e4crypt_install_keyring()) {
493 return -1;
494 }
495 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000496 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000497
498 // Although encrypted, we have device key, so we do not need to
499 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700500 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
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
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700537 std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800538 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700539 int ret = mount_fstab(fstabfile, mount_mode);
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700540 property_set(prop_name, std::to_string(t.duration_ms()));
Wei Wangd61a7e22016-08-23 11:58:09 -0700541
542 if (import_rc) {
543 /* Paths of .rc files are specified at the 2nd argument and beyond */
544 import_late(args, 2, path_arg_end);
545 }
546
547 if (queue_event) {
548 /* queue_fs_event will queue event based on mount_fstab return code
549 * and return processed return code*/
550 ret = queue_fs_event(ret);
551 }
552
553 return ret;
554}
555
Tom Cherryb7349902015-08-26 11:43:36 -0700556static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700557 struct fstab *fstab;
558 int ret;
559
Tom Cherry96f67312015-07-30 13:52:55 -0700560 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700561 ret = fs_mgr_swapon_all(fstab);
562 fs_mgr_free_fstab(fstab);
563
564 return ret;
565}
566
Tom Cherryb7349902015-08-26 11:43:36 -0700567static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700568 property_set(args[1], args[2]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569 return 0;
570}
571
Tom Cherryb7349902015-08-26 11:43:36 -0700572static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700573 struct rlimit limit;
574 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700575 if (android::base::ParseInt(args[1], &resource) &&
576 android::base::ParseUint(args[2], &limit.rlim_cur) &&
577 android::base::ParseUint(args[3], &limit.rlim_max)) {
578 return setrlimit(resource, &limit);
579 }
580 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
581 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700582}
583
Tom Cherryb7349902015-08-26 11:43:36 -0700584static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700585 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
586 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700587 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700588 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589 }
Tom Cherrybac32992015-07-31 12:45:25 -0700590 if (!svc->Start())
591 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 return 0;
593}
594
Tom Cherryb7349902015-08-26 11:43:36 -0700595static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700596 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
597 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700598 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700599 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 }
Tom Cherrybac32992015-07-31 12:45:25 -0700601 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602 return 0;
603}
604
Tom Cherryb7349902015-08-26 11:43:36 -0700605static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700606 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
607 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700608 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700609 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700610 }
Tom Cherrybac32992015-07-31 12:45:25 -0700611 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700612 return 0;
613}
614
Tom Cherryb7349902015-08-26 11:43:36 -0700615static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700616 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 return 0;
618}
619
Tom Cherryb7349902015-08-26 11:43:36 -0700620static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700621 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700622}
623
Tom Cherryb7349902015-08-26 11:43:36 -0700624static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700625 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800626}
627
Tom Cherryb7349902015-08-26 11:43:36 -0700628static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700629 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800630}
631
Tom Cherryb7349902015-08-26 11:43:36 -0700632static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700633 struct timezone tz = {};
634 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
635 return 0;
636 }
637 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800638}
639
Tom Cherryb7349902015-08-26 11:43:36 -0700640static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700641 int mode = -1;
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800642 bool loaded = fs_mgr_load_verity_state(&mode);
643 if (loaded && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700644 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000645 }
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800646 return loaded ? 0 : 1;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000647}
648
Tom Cherryb7349902015-08-26 11:43:36 -0700649static void verity_update_property(fstab_rec *fstab, const char *mount_point,
650 int mode, int status) {
Tom Cherry1c3a53f2017-06-22 16:50:31 -0700651 property_set("partition."s + mount_point + ".verified", std::to_string(mode));
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000652}
653
Tom Cherryb7349902015-08-26 11:43:36 -0700654static int do_verity_update_state(const std::vector<std::string>& args) {
Bowgo Tsaiaaf70e72017-03-02 00:03:56 +0800655 return fs_mgr_update_verity_state(verity_update_property) ? 0 : 1;
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000656}
657
Tom Cherryb7349902015-08-26 11:43:36 -0700658static int do_write(const std::vector<std::string>& args) {
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700659 std::string err;
660 if (!WriteFile(args[1], args[2], &err)) {
661 LOG(ERROR) << err;
662 return -1;
663 }
664 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700665}
666
Tom Cherryb7349902015-08-26 11:43:36 -0700667static int do_copy(const std::vector<std::string>& args) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800668 std::string data;
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700669 std::string err;
670 if (!ReadFile(args[1], &data, &err)) {
671 LOG(ERROR) << err;
672 return -1;
San Mehat7c44fe52009-08-26 16:39:25 -0700673 }
Tom Cherry2cbbe9f2017-05-04 18:17:33 -0700674 if (!WriteFile(args[2], data, &err)) {
675 LOG(ERROR) << err;
676 return -1;
677 }
678 return 0;
San Mehat7c44fe52009-08-26 16:39:25 -0700679}
680
Tom Cherryb7349902015-08-26 11:43:36 -0700681static int do_chown(const std::vector<std::string>& args) {
Tom Cherry517e1f12017-05-04 17:40:33 -0700682 uid_t uid;
683 std::string decode_uid_err;
684 if (!DecodeUid(args[1], &uid, &decode_uid_err)) {
685 LOG(ERROR) << "Unable to find UID for '" << args[1] << "': " << decode_uid_err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700686 return -1;
687 }
Tom Cherry517e1f12017-05-04 17:40:33 -0700688
689 // GID is optional and pushes the index of path out by one if specified.
690 const std::string& path = (args.size() == 4) ? args[3] : args[2];
691 gid_t gid = -1;
692
693 if (args.size() == 4) {
694 if (!DecodeUid(args[2], &gid, &decode_uid_err)) {
695 LOG(ERROR) << "Unable to find GID for '" << args[2] << "': " << decode_uid_err;
696 return -1;
697 }
698 }
699
700 if (lchown(path.c_str(), uid, gid) == -1) return -errno;
701
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700702 return 0;
703}
704
705static mode_t get_mode(const char *s) {
706 mode_t mode = 0;
707 while (*s) {
708 if (*s >= '0' && *s <= '7') {
709 mode = (mode<<3) | (*s-'0');
710 } else {
711 return -1;
712 }
713 s++;
714 }
715 return mode;
716}
717
Tom Cherryb7349902015-08-26 11:43:36 -0700718static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700719 mode_t mode = get_mode(args[1].c_str());
720 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700721 return -errno;
722 }
723 return 0;
724}
725
Tom Cherryb7349902015-08-26 11:43:36 -0700726static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400727 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500728
Paul Lawrencea8d84342016-11-14 15:40:18 -0800729 struct flag_type {const char* name; int value;};
730 static const flag_type flags[] = {
731 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
732 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
733 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
734 {0, 0}
735 };
736
737 int flag = 0;
738
739 bool in_flags = true;
740 for (size_t i = 1; i < args.size(); ++i) {
741 if (android::base::StartsWith(args[i], "--")) {
742 if (!in_flags) {
743 LOG(ERROR) << "restorecon - flags must precede paths";
744 return -1;
745 }
746 bool found = false;
747 for (size_t j = 0; flags[j].name; ++j) {
748 if (args[i] == flags[j].name) {
749 flag |= flags[j].value;
750 found = true;
751 break;
752 }
753 }
754 if (!found) {
755 LOG(ERROR) << "restorecon - bad flag " << args[i];
756 return -1;
757 }
758 } else {
759 in_flags = false;
Tom Cherry482f36c2017-05-08 13:38:15 -0700760 if (selinux_android_restorecon(args[i].c_str(), flag) < 0) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800761 ret = -errno;
762 }
763 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500764 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400765 return ret;
766}
767
Tom Cherryb7349902015-08-26 11:43:36 -0700768static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800769 std::vector<std::string> non_const_args(args);
770 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
771 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500772}
773
Tom Cherryb7349902015-08-26 11:43:36 -0700774static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700775 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700776 int log_level = -1;
777 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700778 android::base::LogSeverity severity;
779 switch (log_level) {
780 case 7: severity = android::base::DEBUG; break;
781 case 6: severity = android::base::INFO; break;
782 case 5:
783 case 4: severity = android::base::WARNING; break;
784 case 3: severity = android::base::ERROR; break;
785 case 2:
786 case 1:
787 case 0: severity = android::base::FATAL; break;
788 default:
789 LOG(ERROR) << "loglevel: invalid log level " << log_level;
790 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700791 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700792 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700793 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700794}
795
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700796static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700797 load_persist_props();
798 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800799}
800
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700801static int do_load_system_props(const std::vector<std::string>& args) {
802 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700803 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700804}
805
Tom Cherryb7349902015-08-26 11:43:36 -0700806static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700807 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800808 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700809 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700810 int timeout;
811 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800812 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700813 }
814 }
815 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700816}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000817
Wei Wang132ac312017-01-25 16:27:03 -0800818static int do_wait_for_prop(const std::vector<std::string>& args) {
819 const char* name = args[1].c_str();
820 const char* value = args[2].c_str();
821 size_t value_len = strlen(value);
822
823 if (!is_legal_property_name(name)) {
824 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
825 << "\") failed: bad name";
826 return -1;
827 }
828 if (value_len >= PROP_VALUE_MAX) {
829 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
830 << "\") failed: value too long";
831 return -1;
832 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800833 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800834 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
835 << "\") failed: init already in waiting";
836 return -1;
837 }
838 return 0;
839}
840
Paul Lawrence806d10b2015-04-28 22:07:10 +0000841/*
842 * Callback to make a directory from the ext4 code
843 */
Tom Cherryb7349902015-08-26 11:43:36 -0700844static int do_installkeys_ensure_dir_exists(const char* dir) {
Tom Cherrye7656b72017-05-01 17:10:09 -0700845 if (make_dir(dir, 0700, sehandle) && errno != EEXIST) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000846 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000847 }
848
Paul Lawrence806d10b2015-04-28 22:07:10 +0000849 return 0;
850}
851
Paul Crowley749af8c2015-05-28 17:35:06 +0100852static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700853 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100854}
855
Tom Cherryb7349902015-08-26 11:43:36 -0700856static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100857 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000858 return 0;
859 }
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700860 auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder;
861 if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) {
862 PLOG(ERROR) << "Failed to create " << unencrypted_dir;
863 return -1;
864 }
865 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
866 "enablefilecrypto"};
867 return do_exec(exec_args);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000868}
Paul Crowley749af8c2015-05-28 17:35:06 +0100869
Paul Crowley59497452016-02-01 16:37:13 +0000870static int do_init_user0(const std::vector<std::string>& args) {
Tom Cherry5a86cb72017-05-03 13:19:03 -0700871 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
872 "init_user0"};
873 return do_exec(exec_args);
Paul Crowley59497452016-02-01 16:37:13 +0000874}
875
Tom Cherryad54d092017-04-19 16:18:50 -0700876const BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
Tom Cherryb7349902015-08-26 11:43:36 -0700877 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -0700878 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700879 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800880 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700881 {"chmod", {2, 2, do_chmod}},
882 {"chown", {2, 3, do_chown}},
883 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800884 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700885 {"class_start", {1, 1, do_class_start}},
886 {"class_stop", {1, 1, do_class_stop}},
887 {"copy", {2, 2, do_copy}},
888 {"domainname", {1, 1, do_domainname}},
889 {"enable", {1, 1, do_enable}},
890 {"exec", {1, kMax, do_exec}},
Tom Cherryb27004a2017-03-27 16:27:30 -0700891 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700892 {"export", {2, 2, do_export}},
893 {"hostname", {1, 1, do_hostname}},
894 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000895 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700896 {"insmod", {1, kMax, do_insmod}},
897 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700898 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700899 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700900 {"loglevel", {1, 1, do_loglevel}},
901 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800902 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700903 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700904 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700905 {"restart", {1, 1, do_restart}},
906 {"restorecon", {1, kMax, do_restorecon}},
907 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
908 {"rm", {1, 1, do_rm}},
909 {"rmdir", {1, 1, do_rmdir}},
910 {"setprop", {2, 2, do_setprop}},
911 {"setrlimit", {3, 3, do_setrlimit}},
912 {"start", {1, 1, do_start}},
913 {"stop", {1, 1, do_stop}},
914 {"swapon_all", {1, 1, do_swapon_all}},
915 {"symlink", {2, 2, do_symlink}},
916 {"sysclktz", {1, 1, do_sysclktz}},
917 {"trigger", {1, 1, do_trigger}},
918 {"verity_load_state", {0, 0, do_verity_load_state}},
919 {"verity_update_state", {0, 0, do_verity_update_state}},
920 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800921 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700922 {"write", {2, 2, do_write}},
923 };
Tom Cherryb27004a2017-03-27 16:27:30 -0700924 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700925 return builtin_functions;
926}