blob: a71d2cdc6659231762b1af0044cf6bcdaa1530e7 [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>
Yusuke Sato0df08272015-07-08 14:57:07 -070022#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070023#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070024#include <signal.h>
Mark Salyzynad575e02016-04-05 08:10:25 -070025#include <sched.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070028#include <string.h>
29#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/mount.h>
31#include <sys/resource.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070032#include <sys/syscall.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080033#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070034#include <sys/types.h>
35#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070036#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070037#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000038#include <linux/loop.h>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080039#include <linux/module.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Paul Crowley0b8b2302016-12-19 13:03:47 -080041#include <string>
Elliott Hughes290a2282016-11-14 17:08:47 -080042#include <thread>
43
Paul Lawrencea8d84342016-11-14 15:40:18 -080044#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050045#include <selinux/selinux.h>
46#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050047
Elliott Hughesdb3f2672015-03-20 09:45:18 -070048#include <fs_mgr.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070049#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080050#include <android-base/parseint.h>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080051#include <android-base/strings.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080052#include <android-base/stringprintf.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070053#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070054#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070055#include <ext4_utils/ext4_crypt.h>
56#include <ext4_utils/ext4_crypt_init_extensions.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070057#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070058
Tom Cherryfa0c21c2015-07-23 17:53:11 -070059#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070060#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070063#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070064#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070065#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070066#include "reboot.h"
Tom Cherrybac32992015-07-31 12:45:25 -070067#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080068#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070069#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070
Paul Crowley0b8b2302016-12-19 13:03:47 -080071using namespace std::literals::string_literals;
72
Nick Kralevichbc609542015-01-31 21:39:46 -080073#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
74
Elliott Hughes9605a942016-11-10 17:43:47 -080075static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080076
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080077static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070078 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
79 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070080 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080082 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080083 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070084 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070085 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070086 }
87 close(fd);
88 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089}
90
Tom Cherryb7349902015-08-26 11:43:36 -070091static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070092 struct ifreq ifr;
93 int s, ret;
94
95 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
96
97 s = socket(AF_INET, SOCK_DGRAM, 0);
98 if (s < 0)
99 return -1;
100
101 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
102 if (ret < 0) {
103 goto done;
104 }
105
106 if (up)
107 ifr.ifr_flags |= IFF_UP;
108 else
109 ifr.ifr_flags &= ~IFF_UP;
110
111 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800112
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113done:
114 close(s);
115 return ret;
116}
117
Paul Crowley0b8b2302016-12-19 13:03:47 -0800118static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700119 std::string err;
120 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700121 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700122 return -1;
123 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700124 DoReboot(ANDROID_RB_RESTART2, "reboot", "recovery", false);
125 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700126}
127
Tom Cherryb7349902015-08-26 11:43:36 -0700128static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700129 /* Starting a class does not start services
130 * which are explicitly disabled. They must
131 * be started individually.
132 */
Tom Cherrybac32992015-07-31 12:45:25 -0700133 ServiceManager::GetInstance().
134 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135 return 0;
136}
137
Tom Cherryb7349902015-08-26 11:43:36 -0700138static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700139 ServiceManager::GetInstance().
140 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141 return 0;
142}
143
Tom Cherryb7349902015-08-26 11:43:36 -0700144static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700145 ServiceManager::GetInstance().
146 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800147 return 0;
148}
149
Steven Moreland2b63d542017-03-10 14:04:37 -0800150static int do_class_restart(const std::vector<std::string>& args) {
151 ServiceManager::GetInstance().
152 ForEachServiceInClass(args[1], [] (Service* s) { s->Restart(); });
153 return 0;
154}
155
Tom Cherryb7349902015-08-26 11:43:36 -0700156static int do_domainname(const std::vector<std::string>& args) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500157 return write_file("/proc/sys/kernel/domainname", args[1].c_str()) ? 0 : 1;
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 Cherry2d804672017-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) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500181 return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182}
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700185 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186}
187
Tom Cherryb7349902015-08-26 11:43:36 -0700188static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800189 int flags = 0;
190 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800191
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800192 if (!(*it).compare("-f")) {
193 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
194 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800195 }
196
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800197 std::string filename = *it++;
198 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
199 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800200}
201
Tom Cherryb7349902015-08-26 11:43:36 -0700202static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700203 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700204 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
206 /* mkdir <path> [mode] [owner] [group] */
207
Tom Cherry96f67312015-07-30 13:52:55 -0700208 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700209 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210 }
211
Tom Cherry96f67312015-07-30 13:52:55 -0700212 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700213 /* chmod in case the directory already exists */
214 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700215 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700216 }
217 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 return -errno;
219 }
220
Tom Cherry96f67312015-07-30 13:52:55 -0700221 if (args.size() >= 4) {
222 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700223 gid_t gid = -1;
224
Tom Cherry96f67312015-07-30 13:52:55 -0700225 if (args.size() == 5) {
226 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227 }
228
Tom Cherry96f67312015-07-30 13:52:55 -0700229 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230 return -errno;
231 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700232
233 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
234 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700235 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700236 if (ret == -1) {
237 return -errno;
238 }
239 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240 }
241
Paul Crowleyaf8be582016-05-10 08:52:06 -0700242 if (e4crypt_is_native()) {
243 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800244 const std::vector<std::string> options = {
245 "--prompt_and_wipe_data",
246 "--reason=set_policy_failed:"s + args[1]};
247 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700248 return -1;
249 }
250 }
251 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252}
253
Alex Light68ab20f2016-06-23 11:11:39 -0700254/* umount <path> */
255static int do_umount(const std::vector<std::string>& args) {
256 return umount(args[1].c_str());
257}
258
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259static struct {
260 const char *name;
261 unsigned flag;
262} mount_flags[] = {
263 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200264 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265 { "nosuid", MS_NOSUID },
266 { "nodev", MS_NODEV },
267 { "nodiratime", MS_NODIRATIME },
268 { "ro", MS_RDONLY },
269 { "rw", 0 },
270 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700271 { "bind", MS_BIND },
272 { "rec", MS_REC },
273 { "unbindable", MS_UNBINDABLE },
274 { "private", MS_PRIVATE },
275 { "slave", MS_SLAVE },
276 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277 { "defaults", 0 },
278 { 0, 0 },
279};
280
Ken Sumrall752923c2010-12-03 16:33:31 -0800281#define DATA_MNT_POINT "/data"
282
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700284static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700286 const char *source, *target, *system;
287 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700289 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700291 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292
Tom Cherry96f67312015-07-30 13:52:55 -0700293 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700295 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296 flags |= mount_flags[i].flag;
297 break;
298 }
299 }
300
Colin Crosscd0f1732010-04-19 17:10:24 -0700301 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700302 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700303 wait = 1;
304 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700305 else if (na + 1 == args.size())
306 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700307 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 }
309
Tom Cherry96f67312015-07-30 13:52:55 -0700310 system = args[1].c_str();
311 source = args[2].c_str();
312 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000313
Elliott Hughes31951162016-06-24 15:15:03 -0700314 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000315 int mode, loop, fd;
316 struct loop_info info;
317
318 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800319 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000320 if (fd < 0) {
321 return -1;
322 }
323
324 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800325 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800326 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000327 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100328 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000329 return -1;
330 }
331
332 /* if it is a blank loop device */
333 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
334 /* if it becomes our loop device */
335 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
336 close(fd);
337
338 if (mount(tmp, target, system, flags, options) < 0) {
339 ioctl(loop, LOOP_CLR_FD, 0);
340 close(loop);
341 return -1;
342 }
343
344 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800345 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000346 }
347 }
348
349 close(loop);
350 }
351
352 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700353 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000354 return -1;
355 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700356 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800357 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000358 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700359 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000360 }
361
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800363
364exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800365 return 0;
366
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367}
368
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800369/* Imports .rc files from the specified paths. Default ones are applied if none is given.
370 *
371 * start_index: index of the first path in the args list
372 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700373static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700374 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700375 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900376 // Fallbacks for partitions on which early mount isn't enabled.
377 if (!parser.is_system_etc_init_loaded()) {
378 parser.ParseConfig("/system/etc/init");
379 parser.set_is_system_etc_init_loaded(true);
380 }
381 if (!parser.is_vendor_etc_init_loaded()) {
382 parser.ParseConfig("/vendor/etc/init");
383 parser.set_is_vendor_etc_init_loaded(true);
384 }
385 if (!parser.is_odm_etc_init_loaded()) {
386 parser.ParseConfig("/odm/etc/init");
387 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800388 }
389 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700390 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800391 parser.ParseConfig(args[i]);
392 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700393 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700394
395 // Turning this on and letting the INFO logging be discarded adds 0.2s to
396 // Nexus 9 boot time, so it's disabled by default.
397 if (false) parser.DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700398}
399
Wei Wangd61a7e22016-08-23 11:58:09 -0700400/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800401 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700402 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700403 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700404static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700405 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700406
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700407 /*
408 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
409 * do the call in the child to provide protection to the main init
410 * process if anything goes wrong (crash or memory leak), and wait for
411 * the child to finish in the parent.
412 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700413 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700414 if (pid > 0) {
415 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700416 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700417 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700418 if (wp_ret == -1) {
419 // Unexpected error code. We will continue anyway.
420 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700421 }
422
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700423 if (WIFEXITED(status)) {
424 ret = WEXITSTATUS(status);
425 } else {
426 ret = -1;
427 }
428 } else if (pid == 0) {
429 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700430
431 // So we can always see what fs_mgr_mount_all() does.
432 // Only needed if someone explicitly changes the default log level in their init.rc.
433 android::base::ScopedLogSeverity info(android::base::INFO);
434
435 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700436 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800437 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700438 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700439 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700440 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700441 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700442 } else {
443 /* fork failed, return an error */
444 return -1;
445 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700446 return ret;
447}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700448
Wei Wangd61a7e22016-08-23 11:58:09 -0700449/* Queue event based on fs_mgr return code.
450 *
451 * code: return code of fs_mgr_mount_all
452 *
453 * This function might request a reboot, in which case it will
454 * not return.
455 *
456 * return code is processed based on input code
457 */
458static int queue_fs_event(int code) {
459 int ret = code;
460 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700461 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700462 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700463 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000464 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700465 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700466 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700467 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800468 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700469 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800470 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700471 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700472 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700473 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700474 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800475 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
476 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700477 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700478 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000479 if (e4crypt_install_keyring()) {
480 return -1;
481 }
482 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000483 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000484
485 // Although encrypted, we have device key, so we do not need to
486 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700487 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700488 } else if (code > 0) {
489 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700490 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700491 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700492
493 return ret;
494}
495
Wei Wangd61a7e22016-08-23 11:58:09 -0700496/* mount_all <fstab> [ <path> ]* [--<options>]*
497 *
498 * This function might request a reboot, in which case it will
499 * not return.
500 */
501static int do_mount_all(const std::vector<std::string>& args) {
502 std::size_t na = 0;
503 bool import_rc = true;
504 bool queue_event = true;
505 int mount_mode = MOUNT_MODE_DEFAULT;
506 const char* fstabfile = args[1].c_str();
507 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800508 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700509
510 for (na = args.size() - 1; na > 1; --na) {
511 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800512 path_arg_end = na;
513 queue_event = false;
514 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800515 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700516 } else if (args[na] == "--late") {
517 path_arg_end = na;
518 import_rc = false;
519 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800520 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700521 }
522 }
523
Keun-young Park69fae7b2017-03-03 18:57:09 -0800524 std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s",
525 prop_post_fix);
526 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700527 int ret = mount_fstab(fstabfile, mount_mode);
Keun-young Park69fae7b2017-03-03 18:57:09 -0800528 property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str());
Wei Wangd61a7e22016-08-23 11:58:09 -0700529
530 if (import_rc) {
531 /* Paths of .rc files are specified at the 2nd argument and beyond */
532 import_late(args, 2, path_arg_end);
533 }
534
535 if (queue_event) {
536 /* queue_fs_event will queue event based on mount_fstab return code
537 * and return processed return code*/
538 ret = queue_fs_event(ret);
539 }
540
541 return ret;
542}
543
Tom Cherryb7349902015-08-26 11:43:36 -0700544static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700545 struct fstab *fstab;
546 int ret;
547
Tom Cherry96f67312015-07-30 13:52:55 -0700548 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700549 ret = fs_mgr_swapon_all(fstab);
550 fs_mgr_free_fstab(fstab);
551
552 return ret;
553}
554
Tom Cherryb7349902015-08-26 11:43:36 -0700555static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700556 const char* name = args[1].c_str();
557 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700558 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 return 0;
560}
561
Tom Cherryb7349902015-08-26 11:43:36 -0700562static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700563 struct rlimit limit;
564 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700565 if (android::base::ParseInt(args[1], &resource) &&
566 android::base::ParseUint(args[2], &limit.rlim_cur) &&
567 android::base::ParseUint(args[3], &limit.rlim_max)) {
568 return setrlimit(resource, &limit);
569 }
570 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
571 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572}
573
Tom Cherryb7349902015-08-26 11:43:36 -0700574static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700575 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
576 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700577 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700578 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579 }
Tom Cherrybac32992015-07-31 12:45:25 -0700580 if (!svc->Start())
581 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700582 return 0;
583}
584
Tom Cherryb7349902015-08-26 11:43:36 -0700585static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700586 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
587 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700588 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700589 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700590 }
Tom Cherrybac32992015-07-31 12:45:25 -0700591 svc->Stop();
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_restart(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_restart: 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->Restart();
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_powerctl(const std::vector<std::string>& args) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700606 const std::string& command = args[1];
Yusuke Satof93d4292015-07-21 15:50:59 -0700607 unsigned int cmd = 0;
Keun-young Park8d01f632017-03-13 11:54:47 -0700608 std::vector<std::string> cmd_params = android::base::Split(command, ",");
609 std::string reason_string = cmd_params[0];
610 std::string reboot_target = "";
611 bool runFsck = false;
612 bool commandInvalid = false;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700613
Keun-young Park8d01f632017-03-13 11:54:47 -0700614 if (cmd_params.size() > 2) {
615 commandInvalid = true;
616 } else if (cmd_params[0] == "shutdown") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700617 cmd = ANDROID_RB_POWEROFF;
Keun-young Park8d01f632017-03-13 11:54:47 -0700618 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
619 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
620 // Run fsck once the file system is remounted in read-only mode.
621 runFsck = true;
622 reason_string = cmd_params[1];
623 }
624 } else if (cmd_params[0] == "reboot") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700625 cmd = ANDROID_RB_RESTART2;
Keun-young Park8d01f632017-03-13 11:54:47 -0700626 if (cmd_params.size() == 2) {
627 reboot_target = cmd_params[1];
628 // When rebooting to the bootloader notify the bootloader writing
629 // also the BCB.
630 if (reboot_target == "bootloader") {
631 std::string err;
632 if (!write_reboot_bootloader(&err)) {
633 LOG(ERROR) << "reboot-bootloader: Error writing "
634 "bootloader_message: "
635 << err;
636 }
637 }
638 }
639 } else if (command == "thermal-shutdown") { // no additional parameter allowed
Todd Poynor16663a22017-01-31 14:15:20 -0800640 cmd = ANDROID_RB_THERMOFF;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700641 } else {
Keun-young Park8d01f632017-03-13 11:54:47 -0700642 commandInvalid = true;
643 }
644 if (commandInvalid) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700645 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646 return -EINVAL;
647 }
648
Keun-young Park8d01f632017-03-13 11:54:47 -0700649 DoReboot(cmd, reason_string, reboot_target, runFsck);
650 return 0;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700651}
652
Tom Cherryb7349902015-08-26 11:43:36 -0700653static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700654 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700655 return 0;
656}
657
Tom Cherryb7349902015-08-26 11:43:36 -0700658static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700659 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700660}
661
Tom Cherryb7349902015-08-26 11:43:36 -0700662static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700663 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800664}
665
Tom Cherryb7349902015-08-26 11:43:36 -0700666static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700667 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800668}
669
Tom Cherryb7349902015-08-26 11:43:36 -0700670static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700671 struct timezone tz = {};
672 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
673 return 0;
674 }
675 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800676}
677
Tom Cherryb7349902015-08-26 11:43:36 -0700678static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700679 int mode = -1;
680 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000681 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700682 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000683 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700684 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000685}
686
Tom Cherryb7349902015-08-26 11:43:36 -0700687static void verity_update_property(fstab_rec *fstab, const char *mount_point,
688 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100689 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
690 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000691}
692
Tom Cherryb7349902015-08-26 11:43:36 -0700693static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700694 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000695}
696
Tom Cherryb7349902015-08-26 11:43:36 -0700697static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700698 const char* path = args[1].c_str();
699 const char* value = args[2].c_str();
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500700 return write_file(path, value) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700701}
702
Tom Cherryb7349902015-08-26 11:43:36 -0700703static int do_copy(const std::vector<std::string>& args) {
Yongqin Liu82bac0d2016-12-28 16:06:19 +0800704 std::string data;
705 if (read_file(args[1].c_str(), &data)) {
706 return write_file(args[2].c_str(), data.data()) ? 0 : 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700707 }
Yongqin Liu82bac0d2016-12-28 16:06:19 +0800708 return 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700709}
710
Tom Cherryb7349902015-08-26 11:43:36 -0700711static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700712 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700713 if (args.size() == 3) {
714 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700715 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700716 } else if (args.size() == 4) {
717 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
718 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719 return -errno;
720 } else {
721 return -1;
722 }
723 return 0;
724}
725
726static mode_t get_mode(const char *s) {
727 mode_t mode = 0;
728 while (*s) {
729 if (*s >= '0' && *s <= '7') {
730 mode = (mode<<3) | (*s-'0');
731 } else {
732 return -1;
733 }
734 s++;
735 }
736 return mode;
737}
738
Tom Cherryb7349902015-08-26 11:43:36 -0700739static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700740 mode_t mode = get_mode(args[1].c_str());
741 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700742 return -errno;
743 }
744 return 0;
745}
746
Tom Cherryb7349902015-08-26 11:43:36 -0700747static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400748 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500749
Paul Lawrencea8d84342016-11-14 15:40:18 -0800750 struct flag_type {const char* name; int value;};
751 static const flag_type flags[] = {
752 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
753 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
754 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
755 {0, 0}
756 };
757
758 int flag = 0;
759
760 bool in_flags = true;
761 for (size_t i = 1; i < args.size(); ++i) {
762 if (android::base::StartsWith(args[i], "--")) {
763 if (!in_flags) {
764 LOG(ERROR) << "restorecon - flags must precede paths";
765 return -1;
766 }
767 bool found = false;
768 for (size_t j = 0; flags[j].name; ++j) {
769 if (args[i] == flags[j].name) {
770 flag |= flags[j].value;
771 found = true;
772 break;
773 }
774 }
775 if (!found) {
776 LOG(ERROR) << "restorecon - bad flag " << args[i];
777 return -1;
778 }
779 } else {
780 in_flags = false;
781 if (restorecon(args[i].c_str(), flag) < 0) {
782 ret = -errno;
783 }
784 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500785 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400786 return ret;
787}
788
Tom Cherryb7349902015-08-26 11:43:36 -0700789static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800790 std::vector<std::string> non_const_args(args);
791 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
792 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500793}
794
Tom Cherryb7349902015-08-26 11:43:36 -0700795static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700796 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700797 int log_level = -1;
798 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700799 android::base::LogSeverity severity;
800 switch (log_level) {
801 case 7: severity = android::base::DEBUG; break;
802 case 6: severity = android::base::INFO; break;
803 case 5:
804 case 4: severity = android::base::WARNING; break;
805 case 3: severity = android::base::ERROR; break;
806 case 2:
807 case 1:
808 case 0: severity = android::base::FATAL; break;
809 default:
810 LOG(ERROR) << "loglevel: invalid log level " << log_level;
811 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700812 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700813 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700814 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815}
816
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700817static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700818 load_persist_props();
819 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800820}
821
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700822static int do_load_system_props(const std::vector<std::string>& args) {
823 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700824 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700825}
826
Tom Cherryb7349902015-08-26 11:43:36 -0700827static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700828 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800829 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700830 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700831 int timeout;
832 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800833 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700834 }
835 }
836 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700837}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000838
Wei Wang132ac312017-01-25 16:27:03 -0800839static int do_wait_for_prop(const std::vector<std::string>& args) {
840 const char* name = args[1].c_str();
841 const char* value = args[2].c_str();
842 size_t value_len = strlen(value);
843
844 if (!is_legal_property_name(name)) {
845 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
846 << "\") failed: bad name";
847 return -1;
848 }
849 if (value_len >= PROP_VALUE_MAX) {
850 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
851 << "\") failed: value too long";
852 return -1;
853 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800854 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800855 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
856 << "\") failed: init already in waiting";
857 return -1;
858 }
859 return 0;
860}
861
Paul Lawrence806d10b2015-04-28 22:07:10 +0000862/*
863 * Callback to make a directory from the ext4 code
864 */
Tom Cherryb7349902015-08-26 11:43:36 -0700865static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000866 if (make_dir(dir, 0700) && errno != EEXIST) {
867 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000868 }
869
Paul Lawrence806d10b2015-04-28 22:07:10 +0000870 return 0;
871}
872
Paul Crowley749af8c2015-05-28 17:35:06 +0100873static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700874 std::string value = property_get("ro.crypto.type");
875 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100876}
877
Tom Cherryb7349902015-08-26 11:43:36 -0700878static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100879 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000880 return 0;
881 }
Tom Cherry96f67312015-07-30 13:52:55 -0700882 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000883 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000884}
Paul Crowley749af8c2015-05-28 17:35:06 +0100885
Paul Crowley59497452016-02-01 16:37:13 +0000886static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000887 return e4crypt_do_init_user0();
888}
889
Tom Cherryb7349902015-08-26 11:43:36 -0700890BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
891 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherry2d804672017-03-27 16:27:30 -0700892 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700893 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800894 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700895 {"chmod", {2, 2, do_chmod}},
896 {"chown", {2, 3, do_chown}},
897 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800898 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700899 {"class_start", {1, 1, do_class_start}},
900 {"class_stop", {1, 1, do_class_stop}},
901 {"copy", {2, 2, do_copy}},
902 {"domainname", {1, 1, do_domainname}},
903 {"enable", {1, 1, do_enable}},
904 {"exec", {1, kMax, do_exec}},
Tom Cherry2d804672017-03-27 16:27:30 -0700905 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700906 {"export", {2, 2, do_export}},
907 {"hostname", {1, 1, do_hostname}},
908 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000909 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700910 {"insmod", {1, kMax, do_insmod}},
911 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700912 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700913 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700914 {"loglevel", {1, 1, do_loglevel}},
915 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800916 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700917 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700918 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700919 {"powerctl", {1, 1, do_powerctl}},
920 {"restart", {1, 1, do_restart}},
921 {"restorecon", {1, kMax, do_restorecon}},
922 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
923 {"rm", {1, 1, do_rm}},
924 {"rmdir", {1, 1, do_rmdir}},
925 {"setprop", {2, 2, do_setprop}},
926 {"setrlimit", {3, 3, do_setrlimit}},
927 {"start", {1, 1, do_start}},
928 {"stop", {1, 1, do_stop}},
929 {"swapon_all", {1, 1, do_swapon_all}},
930 {"symlink", {2, 2, do_symlink}},
931 {"sysclktz", {1, 1, do_sysclktz}},
932 {"trigger", {1, 1, do_trigger}},
933 {"verity_load_state", {0, 0, do_verity_load_state}},
934 {"verity_update_state", {0, 0, do_verity_update_state}},
935 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800936 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700937 {"write", {2, 2, do_write}},
938 };
Tom Cherry2d804672017-03-27 16:27:30 -0700939 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700940 return builtin_functions;
941}