blob: e0a98f50a2e0c2db7bbf1f998ffc906633aa6c2d [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>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080036#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070037#include <sys/types.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070038#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070039#include <unistd.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070041#include <android-base/file.h>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070042#include <android-base/logging.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080043#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070044#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080045#include <android-base/stringprintf.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 Cherry53089aa2017-03-31 15:47:33 -0700152 return write_file("/proc/sys/kernel/domainname", args[1]) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153}
154
Tom Cherryb7349902015-08-26 11:43:36 -0700155static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700156 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
157 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700158 return -1;
159 }
Tom Cherrybac32992015-07-31 12:45:25 -0700160 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700161}
162
Tom Cherryb7349902015-08-26 11:43:36 -0700163static int do_exec(const std::vector<std::string>& args) {
Tom Cherryb27004a2017-03-27 16:27:30 -0700164 return ServiceManager::GetInstance().Exec(args) ? 0 : -1;
165}
166
167static int do_exec_start(const std::vector<std::string>& args) {
168 return ServiceManager::GetInstance().ExecStart(args[1]) ? 0 : -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169}
170
Tom Cherryb7349902015-08-26 11:43:36 -0700171static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700172 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173}
174
Tom Cherryb7349902015-08-26 11:43:36 -0700175static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry53089aa2017-03-31 15:47:33 -0700176 return write_file("/proc/sys/kernel/hostname", args[1]) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700177}
178
Tom Cherryb7349902015-08-26 11:43:36 -0700179static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700180 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181}
182
Tom Cherryb7349902015-08-26 11:43:36 -0700183static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800184 int flags = 0;
185 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800186
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800187 if (!(*it).compare("-f")) {
188 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
189 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800190 }
191
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800192 std::string filename = *it++;
193 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
194 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800195}
196
Tom Cherryb7349902015-08-26 11:43:36 -0700197static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700199 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700200
201 /* mkdir <path> [mode] [owner] [group] */
202
Tom Cherry96f67312015-07-30 13:52:55 -0700203 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700204 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205 }
206
Tom Cherry96f67312015-07-30 13:52:55 -0700207 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700208 /* chmod in case the directory already exists */
209 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700210 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700211 }
212 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 return -errno;
214 }
215
Tom Cherry96f67312015-07-30 13:52:55 -0700216 if (args.size() >= 4) {
217 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 gid_t gid = -1;
219
Tom Cherry96f67312015-07-30 13:52:55 -0700220 if (args.size() == 5) {
221 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222 }
223
Tom Cherry96f67312015-07-30 13:52:55 -0700224 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 return -errno;
226 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700227
228 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
229 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700230 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700231 if (ret == -1) {
232 return -errno;
233 }
234 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235 }
236
Paul Crowleyaf8be582016-05-10 08:52:06 -0700237 if (e4crypt_is_native()) {
238 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800239 const std::vector<std::string> options = {
240 "--prompt_and_wipe_data",
241 "--reason=set_policy_failed:"s + args[1]};
242 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700243 return -1;
244 }
245 }
246 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700247}
248
Alex Light68ab20f2016-06-23 11:11:39 -0700249/* umount <path> */
250static int do_umount(const std::vector<std::string>& args) {
251 return umount(args[1].c_str());
252}
253
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254static struct {
255 const char *name;
256 unsigned flag;
257} mount_flags[] = {
258 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200259 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260 { "nosuid", MS_NOSUID },
261 { "nodev", MS_NODEV },
262 { "nodiratime", MS_NODIRATIME },
263 { "ro", MS_RDONLY },
264 { "rw", 0 },
265 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700266 { "bind", MS_BIND },
267 { "rec", MS_REC },
268 { "unbindable", MS_UNBINDABLE },
269 { "private", MS_PRIVATE },
270 { "slave", MS_SLAVE },
271 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272 { "defaults", 0 },
273 { 0, 0 },
274};
275
Ken Sumrall752923c2010-12-03 16:33:31 -0800276#define DATA_MNT_POINT "/data"
277
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700278/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700279static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700281 const char *source, *target, *system;
282 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700284 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700286 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287
Tom Cherry96f67312015-07-30 13:52:55 -0700288 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700290 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700291 flags |= mount_flags[i].flag;
292 break;
293 }
294 }
295
Colin Crosscd0f1732010-04-19 17:10:24 -0700296 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700297 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700298 wait = 1;
299 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700300 else if (na + 1 == args.size())
301 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700302 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 }
304
Tom Cherry96f67312015-07-30 13:52:55 -0700305 system = args[1].c_str();
306 source = args[2].c_str();
307 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000308
Elliott Hughes31951162016-06-24 15:15:03 -0700309 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000310 int mode, loop, fd;
311 struct loop_info info;
312
313 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800314 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000315 if (fd < 0) {
316 return -1;
317 }
318
319 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800320 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800321 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000322 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100323 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000324 return -1;
325 }
326
327 /* if it is a blank loop device */
328 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
329 /* if it becomes our loop device */
330 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
331 close(fd);
332
333 if (mount(tmp, target, system, flags, options) < 0) {
334 ioctl(loop, LOOP_CLR_FD, 0);
335 close(loop);
336 return -1;
337 }
338
339 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800340 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000341 }
342 }
343
344 close(loop);
345 }
346
347 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700348 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000349 return -1;
350 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700351 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800352 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000353 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700354 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000355 }
356
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800358
359exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800360 return 0;
361
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362}
363
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800364/* Imports .rc files from the specified paths. Default ones are applied if none is given.
365 *
366 * start_index: index of the first path in the args list
367 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700368static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700369 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700370 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900371 // Fallbacks for partitions on which early mount isn't enabled.
372 if (!parser.is_system_etc_init_loaded()) {
373 parser.ParseConfig("/system/etc/init");
374 parser.set_is_system_etc_init_loaded(true);
375 }
376 if (!parser.is_vendor_etc_init_loaded()) {
377 parser.ParseConfig("/vendor/etc/init");
378 parser.set_is_vendor_etc_init_loaded(true);
379 }
380 if (!parser.is_odm_etc_init_loaded()) {
381 parser.ParseConfig("/odm/etc/init");
382 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800383 }
384 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700385 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800386 parser.ParseConfig(args[i]);
387 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700388 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700389
390 // Turning this on and letting the INFO logging be discarded adds 0.2s to
391 // Nexus 9 boot time, so it's disabled by default.
392 if (false) parser.DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700393}
394
Wei Wangd61a7e22016-08-23 11:58:09 -0700395/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800396 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700397 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700398 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700399static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700400 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700401
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700402 /*
403 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
404 * do the call in the child to provide protection to the main init
405 * process if anything goes wrong (crash or memory leak), and wait for
406 * the child to finish in the parent.
407 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700408 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700409 if (pid > 0) {
410 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700411 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700412 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700413 if (wp_ret == -1) {
414 // Unexpected error code. We will continue anyway.
415 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700416 }
417
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700418 if (WIFEXITED(status)) {
419 ret = WEXITSTATUS(status);
420 } else {
421 ret = -1;
422 }
423 } else if (pid == 0) {
424 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700425
426 // So we can always see what fs_mgr_mount_all() does.
427 // Only needed if someone explicitly changes the default log level in their init.rc.
428 android::base::ScopedLogSeverity info(android::base::INFO);
429
430 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700431 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800432 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700433 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700434 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700435 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700436 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700437 } else {
438 /* fork failed, return an error */
439 return -1;
440 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700441 return ret;
442}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700443
Wei Wangd61a7e22016-08-23 11:58:09 -0700444/* Queue event based on fs_mgr return code.
445 *
446 * code: return code of fs_mgr_mount_all
447 *
448 * This function might request a reboot, in which case it will
449 * not return.
450 *
451 * return code is processed based on input code
452 */
453static int queue_fs_event(int code) {
454 int ret = code;
455 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700456 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700457 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700458 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000459 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700460 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700461 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700462 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800463 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700464 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800465 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700466 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700467 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700468 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700469 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800470 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
471 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700472 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700473 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000474 if (e4crypt_install_keyring()) {
475 return -1;
476 }
477 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000478 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000479
480 // Although encrypted, we have device key, so we do not need to
481 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700482 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700483 } else if (code > 0) {
484 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700485 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700486 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700487
488 return ret;
489}
490
Wei Wangd61a7e22016-08-23 11:58:09 -0700491/* mount_all <fstab> [ <path> ]* [--<options>]*
492 *
493 * This function might request a reboot, in which case it will
494 * not return.
495 */
496static int do_mount_all(const std::vector<std::string>& args) {
497 std::size_t na = 0;
498 bool import_rc = true;
499 bool queue_event = true;
500 int mount_mode = MOUNT_MODE_DEFAULT;
501 const char* fstabfile = args[1].c_str();
502 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800503 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700504
505 for (na = args.size() - 1; na > 1; --na) {
506 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800507 path_arg_end = na;
508 queue_event = false;
509 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800510 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700511 } else if (args[na] == "--late") {
512 path_arg_end = na;
513 import_rc = false;
514 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800515 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700516 }
517 }
518
Keun-young Park69fae7b2017-03-03 18:57:09 -0800519 std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s",
520 prop_post_fix);
521 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700522 int ret = mount_fstab(fstabfile, mount_mode);
Keun-young Park69fae7b2017-03-03 18:57:09 -0800523 property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str());
Wei Wangd61a7e22016-08-23 11:58:09 -0700524
525 if (import_rc) {
526 /* Paths of .rc files are specified at the 2nd argument and beyond */
527 import_late(args, 2, path_arg_end);
528 }
529
530 if (queue_event) {
531 /* queue_fs_event will queue event based on mount_fstab return code
532 * and return processed return code*/
533 ret = queue_fs_event(ret);
534 }
535
536 return ret;
537}
538
Tom Cherryb7349902015-08-26 11:43:36 -0700539static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700540 struct fstab *fstab;
541 int ret;
542
Tom Cherry96f67312015-07-30 13:52:55 -0700543 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700544 ret = fs_mgr_swapon_all(fstab);
545 fs_mgr_free_fstab(fstab);
546
547 return ret;
548}
549
Tom Cherryb7349902015-08-26 11:43:36 -0700550static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700551 const char* name = args[1].c_str();
552 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700553 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554 return 0;
555}
556
Tom Cherryb7349902015-08-26 11:43:36 -0700557static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700558 struct rlimit limit;
559 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700560 if (android::base::ParseInt(args[1], &resource) &&
561 android::base::ParseUint(args[2], &limit.rlim_cur) &&
562 android::base::ParseUint(args[3], &limit.rlim_max)) {
563 return setrlimit(resource, &limit);
564 }
565 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
566 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700567}
568
Tom Cherryb7349902015-08-26 11:43:36 -0700569static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700570 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
571 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700572 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700573 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574 }
Tom Cherrybac32992015-07-31 12:45:25 -0700575 if (!svc->Start())
576 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577 return 0;
578}
579
Tom Cherryb7349902015-08-26 11:43:36 -0700580static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700581 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
582 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700583 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700584 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585 }
Tom Cherrybac32992015-07-31 12:45:25 -0700586 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587 return 0;
588}
589
Tom Cherryb7349902015-08-26 11:43:36 -0700590static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700591 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
592 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700593 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700594 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595 }
Tom Cherrybac32992015-07-31 12:45:25 -0700596 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 return 0;
598}
599
Tom Cherryb7349902015-08-26 11:43:36 -0700600static int do_powerctl(const std::vector<std::string>& args) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700601 const std::string& command = args[1];
Yusuke Satof93d4292015-07-21 15:50:59 -0700602 unsigned int cmd = 0;
Keun-young Park8d01f632017-03-13 11:54:47 -0700603 std::vector<std::string> cmd_params = android::base::Split(command, ",");
604 std::string reason_string = cmd_params[0];
605 std::string reboot_target = "";
606 bool runFsck = false;
607 bool commandInvalid = false;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700608
Keun-young Park8d01f632017-03-13 11:54:47 -0700609 if (cmd_params.size() > 2) {
610 commandInvalid = true;
611 } else if (cmd_params[0] == "shutdown") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700612 cmd = ANDROID_RB_POWEROFF;
Keun-young Park8d01f632017-03-13 11:54:47 -0700613 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
614 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
615 // Run fsck once the file system is remounted in read-only mode.
616 runFsck = true;
617 reason_string = cmd_params[1];
618 }
619 } else if (cmd_params[0] == "reboot") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700620 cmd = ANDROID_RB_RESTART2;
Keun-young Park8d01f632017-03-13 11:54:47 -0700621 if (cmd_params.size() == 2) {
622 reboot_target = cmd_params[1];
623 // When rebooting to the bootloader notify the bootloader writing
624 // also the BCB.
625 if (reboot_target == "bootloader") {
626 std::string err;
627 if (!write_reboot_bootloader(&err)) {
628 LOG(ERROR) << "reboot-bootloader: Error writing "
629 "bootloader_message: "
630 << err;
631 }
632 }
633 }
634 } else if (command == "thermal-shutdown") { // no additional parameter allowed
Todd Poynor16663a22017-01-31 14:15:20 -0800635 cmd = ANDROID_RB_THERMOFF;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700636 } else {
Keun-young Park8d01f632017-03-13 11:54:47 -0700637 commandInvalid = true;
638 }
639 if (commandInvalid) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700640 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700641 return -EINVAL;
642 }
643
Keun-young Park8d01f632017-03-13 11:54:47 -0700644 DoReboot(cmd, reason_string, reboot_target, runFsck);
645 return 0;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646}
647
Tom Cherryb7349902015-08-26 11:43:36 -0700648static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700649 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700650 return 0;
651}
652
Tom Cherryb7349902015-08-26 11:43:36 -0700653static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700654 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700655}
656
Tom Cherryb7349902015-08-26 11:43:36 -0700657static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700658 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800659}
660
Tom Cherryb7349902015-08-26 11:43:36 -0700661static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700662 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800663}
664
Tom Cherryb7349902015-08-26 11:43:36 -0700665static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700666 struct timezone tz = {};
667 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
668 return 0;
669 }
670 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800671}
672
Tom Cherryb7349902015-08-26 11:43:36 -0700673static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700674 int mode = -1;
675 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000676 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700677 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000678 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700679 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000680}
681
Tom Cherryb7349902015-08-26 11:43:36 -0700682static void verity_update_property(fstab_rec *fstab, const char *mount_point,
683 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100684 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
685 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000686}
687
Tom Cherryb7349902015-08-26 11:43:36 -0700688static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700689 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000690}
691
Tom Cherryb7349902015-08-26 11:43:36 -0700692static int do_write(const std::vector<std::string>& args) {
Tom Cherry53089aa2017-03-31 15:47:33 -0700693 return write_file(args[1], args[2]) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700694}
695
Tom Cherryb7349902015-08-26 11:43:36 -0700696static int do_copy(const std::vector<std::string>& args) {
Yongqin Liudbe88e72016-12-28 16:06:19 +0800697 std::string data;
698 if (read_file(args[1], &data)) {
699 return write_file(args[2], data) ? 0 : 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700700 }
Yongqin Liudbe88e72016-12-28 16:06:19 +0800701 return 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700702}
703
Tom Cherryb7349902015-08-26 11:43:36 -0700704static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700706 if (args.size() == 3) {
707 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700708 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700709 } else if (args.size() == 4) {
710 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
711 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700712 return -errno;
713 } else {
714 return -1;
715 }
716 return 0;
717}
718
719static mode_t get_mode(const char *s) {
720 mode_t mode = 0;
721 while (*s) {
722 if (*s >= '0' && *s <= '7') {
723 mode = (mode<<3) | (*s-'0');
724 } else {
725 return -1;
726 }
727 s++;
728 }
729 return mode;
730}
731
Tom Cherryb7349902015-08-26 11:43:36 -0700732static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700733 mode_t mode = get_mode(args[1].c_str());
734 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700735 return -errno;
736 }
737 return 0;
738}
739
Tom Cherryb7349902015-08-26 11:43:36 -0700740static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400741 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500742
Paul Lawrencea8d84342016-11-14 15:40:18 -0800743 struct flag_type {const char* name; int value;};
744 static const flag_type flags[] = {
745 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
746 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
747 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
748 {0, 0}
749 };
750
751 int flag = 0;
752
753 bool in_flags = true;
754 for (size_t i = 1; i < args.size(); ++i) {
755 if (android::base::StartsWith(args[i], "--")) {
756 if (!in_flags) {
757 LOG(ERROR) << "restorecon - flags must precede paths";
758 return -1;
759 }
760 bool found = false;
761 for (size_t j = 0; flags[j].name; ++j) {
762 if (args[i] == flags[j].name) {
763 flag |= flags[j].value;
764 found = true;
765 break;
766 }
767 }
768 if (!found) {
769 LOG(ERROR) << "restorecon - bad flag " << args[i];
770 return -1;
771 }
772 } else {
773 in_flags = false;
774 if (restorecon(args[i].c_str(), flag) < 0) {
775 ret = -errno;
776 }
777 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500778 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400779 return ret;
780}
781
Tom Cherryb7349902015-08-26 11:43:36 -0700782static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800783 std::vector<std::string> non_const_args(args);
784 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
785 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500786}
787
Tom Cherryb7349902015-08-26 11:43:36 -0700788static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700789 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700790 int log_level = -1;
791 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700792 android::base::LogSeverity severity;
793 switch (log_level) {
794 case 7: severity = android::base::DEBUG; break;
795 case 6: severity = android::base::INFO; break;
796 case 5:
797 case 4: severity = android::base::WARNING; break;
798 case 3: severity = android::base::ERROR; break;
799 case 2:
800 case 1:
801 case 0: severity = android::base::FATAL; break;
802 default:
803 LOG(ERROR) << "loglevel: invalid log level " << log_level;
804 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700805 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700806 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700807 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808}
809
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700810static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700811 load_persist_props();
812 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800813}
814
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700815static int do_load_system_props(const std::vector<std::string>& args) {
816 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700817 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700818}
819
Tom Cherryb7349902015-08-26 11:43:36 -0700820static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700821 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800822 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700823 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700824 int timeout;
825 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800826 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700827 }
828 }
829 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700830}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000831
Wei Wang132ac312017-01-25 16:27:03 -0800832static int do_wait_for_prop(const std::vector<std::string>& args) {
833 const char* name = args[1].c_str();
834 const char* value = args[2].c_str();
835 size_t value_len = strlen(value);
836
837 if (!is_legal_property_name(name)) {
838 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
839 << "\") failed: bad name";
840 return -1;
841 }
842 if (value_len >= PROP_VALUE_MAX) {
843 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
844 << "\") failed: value too long";
845 return -1;
846 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800847 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800848 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
849 << "\") failed: init already in waiting";
850 return -1;
851 }
852 return 0;
853}
854
Paul Lawrence806d10b2015-04-28 22:07:10 +0000855/*
856 * Callback to make a directory from the ext4 code
857 */
Tom Cherryb7349902015-08-26 11:43:36 -0700858static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000859 if (make_dir(dir, 0700) && errno != EEXIST) {
860 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000861 }
862
Paul Lawrence806d10b2015-04-28 22:07:10 +0000863 return 0;
864}
865
Paul Crowley749af8c2015-05-28 17:35:06 +0100866static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700867 return android::base::GetProperty("ro.crypto.type", "") == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100868}
869
Tom Cherryb7349902015-08-26 11:43:36 -0700870static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100871 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000872 return 0;
873 }
Janis Danisevskis9cc51722017-03-29 14:50:01 -0700874 auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder;
875 if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) {
876 PLOG(ERROR) << "Failed to create " << unencrypted_dir;
877 return -1;
878 }
879 std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs",
880 "enablefilecrypto"};
881 return do_exec(exec_args);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000882}
Paul Crowley749af8c2015-05-28 17:35:06 +0100883
Paul Crowley59497452016-02-01 16:37:13 +0000884static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000885 return e4crypt_do_init_user0();
886}
887
Tom Cherryb7349902015-08-26 11:43:36 -0700888BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
889 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
Tom Cherryb27004a2017-03-27 16:27:30 -0700890 // clang-format off
Tom Cherryb7349902015-08-26 11:43:36 -0700891 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800892 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700893 {"chmod", {2, 2, do_chmod}},
894 {"chown", {2, 3, do_chown}},
895 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800896 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700897 {"class_start", {1, 1, do_class_start}},
898 {"class_stop", {1, 1, do_class_stop}},
899 {"copy", {2, 2, do_copy}},
900 {"domainname", {1, 1, do_domainname}},
901 {"enable", {1, 1, do_enable}},
902 {"exec", {1, kMax, do_exec}},
Tom Cherryb27004a2017-03-27 16:27:30 -0700903 {"exec_start", {1, 1, do_exec_start}},
Tom Cherryb7349902015-08-26 11:43:36 -0700904 {"export", {2, 2, do_export}},
905 {"hostname", {1, 1, do_hostname}},
906 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000907 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700908 {"insmod", {1, kMax, do_insmod}},
909 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700910 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700911 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700912 {"loglevel", {1, 1, do_loglevel}},
913 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800914 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700915 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700916 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700917 {"powerctl", {1, 1, do_powerctl}},
918 {"restart", {1, 1, do_restart}},
919 {"restorecon", {1, kMax, do_restorecon}},
920 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
921 {"rm", {1, 1, do_rm}},
922 {"rmdir", {1, 1, do_rmdir}},
923 {"setprop", {2, 2, do_setprop}},
924 {"setrlimit", {3, 3, do_setrlimit}},
925 {"start", {1, 1, do_start}},
926 {"stop", {1, 1, do_stop}},
927 {"swapon_all", {1, 1, do_swapon_all}},
928 {"symlink", {2, 2, do_symlink}},
929 {"sysclktz", {1, 1, do_sysclktz}},
930 {"trigger", {1, 1, do_trigger}},
931 {"verity_load_state", {0, 0, do_verity_load_state}},
932 {"verity_update_state", {0, 0, do_verity_update_state}},
933 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800934 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700935 {"write", {2, 2, do_write}},
936 };
Tom Cherryb27004a2017-03-27 16:27:30 -0700937 // clang-format on
Tom Cherryb7349902015-08-26 11:43:36 -0700938 return builtin_functions;
939}