blob: ec55b03d5454a37af698cf22fc7ec191b0988ff2 [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 Cherrybac32992015-07-31 12:45:25 -0700169 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
170 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800171 return -1;
172 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800173 if (!start_waiting_for_exec()) {
Tom Cherrybac32992015-07-31 12:45:25 -0700174 return -1;
175 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800176 if (!svc->Start()) {
177 stop_waiting_for_exec();
178 ServiceManager::GetInstance().RemoveService(*svc);
179 return -1;
180 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800181 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182}
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700185 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186}
187
Tom Cherryb7349902015-08-26 11:43:36 -0700188static int do_hostname(const std::vector<std::string>& args) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500189 return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190}
191
Tom Cherryb7349902015-08-26 11:43:36 -0700192static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700193 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194}
195
Tom Cherryb7349902015-08-26 11:43:36 -0700196static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800197 int flags = 0;
198 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800199
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800200 if (!(*it).compare("-f")) {
201 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
202 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800203 }
204
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800205 std::string filename = *it++;
206 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
207 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800208}
209
Tom Cherryb7349902015-08-26 11:43:36 -0700210static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700211 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700212 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213
214 /* mkdir <path> [mode] [owner] [group] */
215
Tom Cherry96f67312015-07-30 13:52:55 -0700216 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700217 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218 }
219
Tom Cherry96f67312015-07-30 13:52:55 -0700220 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700221 /* chmod in case the directory already exists */
222 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700223 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700224 }
225 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 return -errno;
227 }
228
Tom Cherry96f67312015-07-30 13:52:55 -0700229 if (args.size() >= 4) {
230 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231 gid_t gid = -1;
232
Tom Cherry96f67312015-07-30 13:52:55 -0700233 if (args.size() == 5) {
234 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235 }
236
Tom Cherry96f67312015-07-30 13:52:55 -0700237 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700238 return -errno;
239 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700240
241 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
242 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700243 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700244 if (ret == -1) {
245 return -errno;
246 }
247 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248 }
249
Paul Crowleyaf8be582016-05-10 08:52:06 -0700250 if (e4crypt_is_native()) {
251 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800252 const std::vector<std::string> options = {
253 "--prompt_and_wipe_data",
254 "--reason=set_policy_failed:"s + args[1]};
255 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700256 return -1;
257 }
258 }
259 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260}
261
Alex Light68ab20f2016-06-23 11:11:39 -0700262/* umount <path> */
263static int do_umount(const std::vector<std::string>& args) {
264 return umount(args[1].c_str());
265}
266
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700267static struct {
268 const char *name;
269 unsigned flag;
270} mount_flags[] = {
271 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200272 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273 { "nosuid", MS_NOSUID },
274 { "nodev", MS_NODEV },
275 { "nodiratime", MS_NODIRATIME },
276 { "ro", MS_RDONLY },
277 { "rw", 0 },
278 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700279 { "bind", MS_BIND },
280 { "rec", MS_REC },
281 { "unbindable", MS_UNBINDABLE },
282 { "private", MS_PRIVATE },
283 { "slave", MS_SLAVE },
284 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 { "defaults", 0 },
286 { 0, 0 },
287};
288
Ken Sumrall752923c2010-12-03 16:33:31 -0800289#define DATA_MNT_POINT "/data"
290
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700291/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700292static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700294 const char *source, *target, *system;
295 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700297 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700299 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300
Tom Cherry96f67312015-07-30 13:52:55 -0700301 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700303 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304 flags |= mount_flags[i].flag;
305 break;
306 }
307 }
308
Colin Crosscd0f1732010-04-19 17:10:24 -0700309 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700310 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700311 wait = 1;
312 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700313 else if (na + 1 == args.size())
314 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700315 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 }
317
Tom Cherry96f67312015-07-30 13:52:55 -0700318 system = args[1].c_str();
319 source = args[2].c_str();
320 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000321
Elliott Hughes31951162016-06-24 15:15:03 -0700322 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000323 int mode, loop, fd;
324 struct loop_info info;
325
326 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800327 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000328 if (fd < 0) {
329 return -1;
330 }
331
332 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800333 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800334 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000335 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100336 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000337 return -1;
338 }
339
340 /* if it is a blank loop device */
341 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
342 /* if it becomes our loop device */
343 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
344 close(fd);
345
346 if (mount(tmp, target, system, flags, options) < 0) {
347 ioctl(loop, LOOP_CLR_FD, 0);
348 close(loop);
349 return -1;
350 }
351
352 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800353 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000354 }
355 }
356
357 close(loop);
358 }
359
360 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700361 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 return -1;
363 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700364 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800365 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700367 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000368 }
369
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700370 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800371
372exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800373 return 0;
374
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375}
376
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800377/* Imports .rc files from the specified paths. Default ones are applied if none is given.
378 *
379 * start_index: index of the first path in the args list
380 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700381static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700382 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700383 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900384 // Fallbacks for partitions on which early mount isn't enabled.
385 if (!parser.is_system_etc_init_loaded()) {
386 parser.ParseConfig("/system/etc/init");
387 parser.set_is_system_etc_init_loaded(true);
388 }
389 if (!parser.is_vendor_etc_init_loaded()) {
390 parser.ParseConfig("/vendor/etc/init");
391 parser.set_is_vendor_etc_init_loaded(true);
392 }
393 if (!parser.is_odm_etc_init_loaded()) {
394 parser.ParseConfig("/odm/etc/init");
395 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800396 }
397 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700398 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800399 parser.ParseConfig(args[i]);
400 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700401 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700402
403 // Turning this on and letting the INFO logging be discarded adds 0.2s to
404 // Nexus 9 boot time, so it's disabled by default.
405 if (false) parser.DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700406}
407
Wei Wangd61a7e22016-08-23 11:58:09 -0700408/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800409 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700410 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700411 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700412static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700413 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700414
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700415 /*
416 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
417 * do the call in the child to provide protection to the main init
418 * process if anything goes wrong (crash or memory leak), and wait for
419 * the child to finish in the parent.
420 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700421 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700422 if (pid > 0) {
423 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700424 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700425 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700426 if (wp_ret == -1) {
427 // Unexpected error code. We will continue anyway.
428 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700429 }
430
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700431 if (WIFEXITED(status)) {
432 ret = WEXITSTATUS(status);
433 } else {
434 ret = -1;
435 }
436 } else if (pid == 0) {
437 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700438
439 // So we can always see what fs_mgr_mount_all() does.
440 // Only needed if someone explicitly changes the default log level in their init.rc.
441 android::base::ScopedLogSeverity info(android::base::INFO);
442
443 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700444 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800445 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700446 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700447 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700448 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700449 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700450 } else {
451 /* fork failed, return an error */
452 return -1;
453 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700454 return ret;
455}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700456
Wei Wangd61a7e22016-08-23 11:58:09 -0700457/* Queue event based on fs_mgr return code.
458 *
459 * code: return code of fs_mgr_mount_all
460 *
461 * This function might request a reboot, in which case it will
462 * not return.
463 *
464 * return code is processed based on input code
465 */
466static int queue_fs_event(int code) {
467 int ret = code;
468 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700469 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700470 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700471 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000472 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700473 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700474 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700475 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800476 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700477 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800478 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700479 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700480 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700481 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700482 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800483 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
484 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700485 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700486 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000487 if (e4crypt_install_keyring()) {
488 return -1;
489 }
490 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000491 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000492
493 // Although encrypted, we have device key, so we do not need to
494 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700495 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700496 } else if (code > 0) {
497 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700498 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700499 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700500
501 return ret;
502}
503
Wei Wangd61a7e22016-08-23 11:58:09 -0700504/* mount_all <fstab> [ <path> ]* [--<options>]*
505 *
506 * This function might request a reboot, in which case it will
507 * not return.
508 */
509static int do_mount_all(const std::vector<std::string>& args) {
510 std::size_t na = 0;
511 bool import_rc = true;
512 bool queue_event = true;
513 int mount_mode = MOUNT_MODE_DEFAULT;
514 const char* fstabfile = args[1].c_str();
515 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800516 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700517
518 for (na = args.size() - 1; na > 1; --na) {
519 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800520 path_arg_end = na;
521 queue_event = false;
522 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800523 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700524 } else if (args[na] == "--late") {
525 path_arg_end = na;
526 import_rc = false;
527 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800528 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700529 }
530 }
531
Keun-young Park69fae7b2017-03-03 18:57:09 -0800532 std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s",
533 prop_post_fix);
534 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700535 int ret = mount_fstab(fstabfile, mount_mode);
Keun-young Park69fae7b2017-03-03 18:57:09 -0800536 property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str());
Wei Wangd61a7e22016-08-23 11:58:09 -0700537
538 if (import_rc) {
539 /* Paths of .rc files are specified at the 2nd argument and beyond */
540 import_late(args, 2, path_arg_end);
541 }
542
543 if (queue_event) {
544 /* queue_fs_event will queue event based on mount_fstab return code
545 * and return processed return code*/
546 ret = queue_fs_event(ret);
547 }
548
549 return ret;
550}
551
Tom Cherryb7349902015-08-26 11:43:36 -0700552static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700553 struct fstab *fstab;
554 int ret;
555
Tom Cherry96f67312015-07-30 13:52:55 -0700556 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700557 ret = fs_mgr_swapon_all(fstab);
558 fs_mgr_free_fstab(fstab);
559
560 return ret;
561}
562
Tom Cherryb7349902015-08-26 11:43:36 -0700563static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700564 const char* name = args[1].c_str();
565 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700566 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700567 return 0;
568}
569
Tom Cherryb7349902015-08-26 11:43:36 -0700570static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571 struct rlimit limit;
572 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700573 if (android::base::ParseInt(args[1], &resource) &&
574 android::base::ParseUint(args[2], &limit.rlim_cur) &&
575 android::base::ParseUint(args[3], &limit.rlim_max)) {
576 return setrlimit(resource, &limit);
577 }
578 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
579 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700580}
581
Tom Cherryb7349902015-08-26 11:43:36 -0700582static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700583 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
584 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700585 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700586 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587 }
Tom Cherrybac32992015-07-31 12:45:25 -0700588 if (!svc->Start())
589 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700590 return 0;
591}
592
Tom Cherryb7349902015-08-26 11:43:36 -0700593static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700594 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
595 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700596 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700597 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 }
Tom Cherrybac32992015-07-31 12:45:25 -0700599 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 return 0;
601}
602
Tom Cherryb7349902015-08-26 11:43:36 -0700603static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700604 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
605 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700606 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700607 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700608 }
Tom Cherrybac32992015-07-31 12:45:25 -0700609 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700610 return 0;
611}
612
Tom Cherryb7349902015-08-26 11:43:36 -0700613static int do_powerctl(const std::vector<std::string>& args) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700614 const std::string& command = args[1];
Yusuke Satof93d4292015-07-21 15:50:59 -0700615 unsigned int cmd = 0;
Keun-young Park8d01f632017-03-13 11:54:47 -0700616 std::vector<std::string> cmd_params = android::base::Split(command, ",");
617 std::string reason_string = cmd_params[0];
618 std::string reboot_target = "";
619 bool runFsck = false;
620 bool commandInvalid = false;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700621
Keun-young Park8d01f632017-03-13 11:54:47 -0700622 if (cmd_params.size() > 2) {
623 commandInvalid = true;
624 } else if (cmd_params[0] == "shutdown") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700625 cmd = ANDROID_RB_POWEROFF;
Keun-young Park8d01f632017-03-13 11:54:47 -0700626 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
627 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
628 // Run fsck once the file system is remounted in read-only mode.
629 runFsck = true;
630 reason_string = cmd_params[1];
631 }
632 } else if (cmd_params[0] == "reboot") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700633 cmd = ANDROID_RB_RESTART2;
Keun-young Park8d01f632017-03-13 11:54:47 -0700634 if (cmd_params.size() == 2) {
635 reboot_target = cmd_params[1];
636 // When rebooting to the bootloader notify the bootloader writing
637 // also the BCB.
638 if (reboot_target == "bootloader") {
639 std::string err;
640 if (!write_reboot_bootloader(&err)) {
641 LOG(ERROR) << "reboot-bootloader: Error writing "
642 "bootloader_message: "
643 << err;
644 }
645 }
646 }
647 } else if (command == "thermal-shutdown") { // no additional parameter allowed
Todd Poynor16663a22017-01-31 14:15:20 -0800648 cmd = ANDROID_RB_THERMOFF;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700649 } else {
Keun-young Park8d01f632017-03-13 11:54:47 -0700650 commandInvalid = true;
651 }
652 if (commandInvalid) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700653 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700654 return -EINVAL;
655 }
656
Keun-young Park8d01f632017-03-13 11:54:47 -0700657 DoReboot(cmd, reason_string, reboot_target, runFsck);
658 return 0;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700659}
660
Tom Cherryb7349902015-08-26 11:43:36 -0700661static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700662 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700663 return 0;
664}
665
Tom Cherryb7349902015-08-26 11:43:36 -0700666static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700667 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700668}
669
Tom Cherryb7349902015-08-26 11:43:36 -0700670static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700671 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800672}
673
Tom Cherryb7349902015-08-26 11:43:36 -0700674static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700675 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800676}
677
Tom Cherryb7349902015-08-26 11:43:36 -0700678static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700679 struct timezone tz = {};
680 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
681 return 0;
682 }
683 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800684}
685
Tom Cherryb7349902015-08-26 11:43:36 -0700686static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700687 int mode = -1;
688 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000689 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700690 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000691 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700692 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000693}
694
Tom Cherryb7349902015-08-26 11:43:36 -0700695static void verity_update_property(fstab_rec *fstab, const char *mount_point,
696 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100697 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
698 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000699}
700
Tom Cherryb7349902015-08-26 11:43:36 -0700701static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700702 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000703}
704
Tom Cherryb7349902015-08-26 11:43:36 -0700705static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700706 const char* path = args[1].c_str();
707 const char* value = args[2].c_str();
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500708 return write_file(path, value) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709}
710
Tom Cherryb7349902015-08-26 11:43:36 -0700711static int do_copy(const std::vector<std::string>& args) {
Yongqin Liu82bac0d2016-12-28 16:06:19 +0800712 std::string data;
713 if (read_file(args[1].c_str(), &data)) {
714 return write_file(args[2].c_str(), data.data()) ? 0 : 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700715 }
Yongqin Liu82bac0d2016-12-28 16:06:19 +0800716 return 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700717}
718
Tom Cherryb7349902015-08-26 11:43:36 -0700719static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700721 if (args.size() == 3) {
722 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700723 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700724 } else if (args.size() == 4) {
725 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
726 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727 return -errno;
728 } else {
729 return -1;
730 }
731 return 0;
732}
733
734static mode_t get_mode(const char *s) {
735 mode_t mode = 0;
736 while (*s) {
737 if (*s >= '0' && *s <= '7') {
738 mode = (mode<<3) | (*s-'0');
739 } else {
740 return -1;
741 }
742 s++;
743 }
744 return mode;
745}
746
Tom Cherryb7349902015-08-26 11:43:36 -0700747static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700748 mode_t mode = get_mode(args[1].c_str());
749 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750 return -errno;
751 }
752 return 0;
753}
754
Tom Cherryb7349902015-08-26 11:43:36 -0700755static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400756 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500757
Paul Lawrencea8d84342016-11-14 15:40:18 -0800758 struct flag_type {const char* name; int value;};
759 static const flag_type flags[] = {
760 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
761 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
762 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
763 {0, 0}
764 };
765
766 int flag = 0;
767
768 bool in_flags = true;
769 for (size_t i = 1; i < args.size(); ++i) {
770 if (android::base::StartsWith(args[i], "--")) {
771 if (!in_flags) {
772 LOG(ERROR) << "restorecon - flags must precede paths";
773 return -1;
774 }
775 bool found = false;
776 for (size_t j = 0; flags[j].name; ++j) {
777 if (args[i] == flags[j].name) {
778 flag |= flags[j].value;
779 found = true;
780 break;
781 }
782 }
783 if (!found) {
784 LOG(ERROR) << "restorecon - bad flag " << args[i];
785 return -1;
786 }
787 } else {
788 in_flags = false;
789 if (restorecon(args[i].c_str(), flag) < 0) {
790 ret = -errno;
791 }
792 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500793 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400794 return ret;
795}
796
Tom Cherryb7349902015-08-26 11:43:36 -0700797static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800798 std::vector<std::string> non_const_args(args);
799 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
800 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500801}
802
Tom Cherryb7349902015-08-26 11:43:36 -0700803static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700804 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700805 int log_level = -1;
806 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700807 android::base::LogSeverity severity;
808 switch (log_level) {
809 case 7: severity = android::base::DEBUG; break;
810 case 6: severity = android::base::INFO; break;
811 case 5:
812 case 4: severity = android::base::WARNING; break;
813 case 3: severity = android::base::ERROR; break;
814 case 2:
815 case 1:
816 case 0: severity = android::base::FATAL; break;
817 default:
818 LOG(ERROR) << "loglevel: invalid log level " << log_level;
819 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700820 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700821 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700822 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823}
824
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700825static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700826 load_persist_props();
827 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800828}
829
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700830static int do_load_system_props(const std::vector<std::string>& args) {
831 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700832 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700833}
834
Tom Cherryb7349902015-08-26 11:43:36 -0700835static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700836 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800837 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700838 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700839 int timeout;
840 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800841 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700842 }
843 }
844 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700845}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000846
Wei Wang132ac312017-01-25 16:27:03 -0800847static int do_wait_for_prop(const std::vector<std::string>& args) {
848 const char* name = args[1].c_str();
849 const char* value = args[2].c_str();
850 size_t value_len = strlen(value);
851
852 if (!is_legal_property_name(name)) {
853 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
854 << "\") failed: bad name";
855 return -1;
856 }
857 if (value_len >= PROP_VALUE_MAX) {
858 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
859 << "\") failed: value too long";
860 return -1;
861 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800862 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800863 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
864 << "\") failed: init already in waiting";
865 return -1;
866 }
867 return 0;
868}
869
Paul Lawrence806d10b2015-04-28 22:07:10 +0000870/*
871 * Callback to make a directory from the ext4 code
872 */
Tom Cherryb7349902015-08-26 11:43:36 -0700873static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000874 if (make_dir(dir, 0700) && errno != EEXIST) {
875 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000876 }
877
Paul Lawrence806d10b2015-04-28 22:07:10 +0000878 return 0;
879}
880
Paul Crowley749af8c2015-05-28 17:35:06 +0100881static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700882 std::string value = property_get("ro.crypto.type");
883 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100884}
885
Tom Cherryb7349902015-08-26 11:43:36 -0700886static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100887 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000888 return 0;
889 }
Tom Cherry96f67312015-07-30 13:52:55 -0700890 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000891 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000892}
Paul Crowley749af8c2015-05-28 17:35:06 +0100893
Paul Crowley59497452016-02-01 16:37:13 +0000894static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000895 return e4crypt_do_init_user0();
896}
897
Tom Cherryb7349902015-08-26 11:43:36 -0700898BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
899 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
900 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -0800901 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700902 {"chmod", {2, 2, do_chmod}},
903 {"chown", {2, 3, do_chown}},
904 {"class_reset", {1, 1, do_class_reset}},
Steven Moreland2b63d542017-03-10 14:04:37 -0800905 {"class_restart", {1, 1, do_class_restart}},
Tom Cherryb7349902015-08-26 11:43:36 -0700906 {"class_start", {1, 1, do_class_start}},
907 {"class_stop", {1, 1, do_class_stop}},
908 {"copy", {2, 2, do_copy}},
909 {"domainname", {1, 1, do_domainname}},
910 {"enable", {1, 1, do_enable}},
911 {"exec", {1, kMax, do_exec}},
912 {"export", {2, 2, do_export}},
913 {"hostname", {1, 1, do_hostname}},
914 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000915 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700916 {"insmod", {1, kMax, do_insmod}},
917 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700918 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700919 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700920 {"loglevel", {1, 1, do_loglevel}},
921 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800922 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700923 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700924 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700925 {"powerctl", {1, 1, do_powerctl}},
926 {"restart", {1, 1, do_restart}},
927 {"restorecon", {1, kMax, do_restorecon}},
928 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
929 {"rm", {1, 1, do_rm}},
930 {"rmdir", {1, 1, do_rmdir}},
931 {"setprop", {2, 2, do_setprop}},
932 {"setrlimit", {3, 3, do_setrlimit}},
933 {"start", {1, 1, do_start}},
934 {"stop", {1, 1, do_stop}},
935 {"swapon_all", {1, 1, do_swapon_all}},
936 {"symlink", {2, 2, do_symlink}},
937 {"sysclktz", {1, 1, do_sysclktz}},
938 {"trigger", {1, 1, do_trigger}},
939 {"verity_load_state", {0, 0, do_verity_load_state}},
940 {"verity_update_state", {0, 0, do_verity_update_state}},
941 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -0800942 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -0700943 {"write", {2, 2, do_write}},
944 };
945 return builtin_functions;
946}