blob: 5a67e5e1efb3fbb84fafc5acb6bdc2222a35a0aa [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
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070048#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080049#include <android-base/parseint.h>
Tom Cherryccf23532017-03-28 16:40:41 -070050#include <android-base/properties.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/stringprintf.h>
Tom Cherryccf23532017-03-28 16:40:41 -070052#include <android-base/strings.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>
Tom Cherryccf23532017-03-28 16:40:41 -070057#include <fs_mgr.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070058#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070059
Tom Cherryfa0c21c2015-07-23 17:53:11 -070060#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070061#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070063#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070064#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070065#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070066#include "property_service.h"
Keun-young Park8d01f632017-03-13 11:54:47 -070067#include "reboot.h"
Tom Cherrybac32992015-07-31 12:45:25 -070068#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080069#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070070#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071
Paul Crowley0b8b2302016-12-19 13:03:47 -080072using namespace std::literals::string_literals;
73
Nick Kralevichbc609542015-01-31 21:39:46 -080074#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
75
Elliott Hughes9605a942016-11-10 17:43:47 -080076static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080077
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080078static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070079 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
80 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070081 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080083 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080084 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070085 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070086 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070087 }
88 close(fd);
89 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070090}
91
Tom Cherryb7349902015-08-26 11:43:36 -070092static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070093 struct ifreq ifr;
94 int s, ret;
95
96 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
97
98 s = socket(AF_INET, SOCK_DGRAM, 0);
99 if (s < 0)
100 return -1;
101
102 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
103 if (ret < 0) {
104 goto done;
105 }
106
107 if (up)
108 ifr.ifr_flags |= IFF_UP;
109 else
110 ifr.ifr_flags &= ~IFF_UP;
111
112 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800113
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114done:
115 close(s);
116 return ret;
117}
118
Paul Crowley0b8b2302016-12-19 13:03:47 -0800119static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700120 std::string err;
121 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700122 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700123 return -1;
124 }
Keun-young Park8d01f632017-03-13 11:54:47 -0700125 DoReboot(ANDROID_RB_RESTART2, "reboot", "recovery", false);
126 return 0;
Yusuke Sato0df08272015-07-08 14:57:07 -0700127}
128
Tom Cherryb7349902015-08-26 11:43:36 -0700129static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700130 /* Starting a class does not start services
131 * which are explicitly disabled. They must
132 * be started individually.
133 */
Tom Cherrybac32992015-07-31 12:45:25 -0700134 ServiceManager::GetInstance().
135 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
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_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700140 ServiceManager::GetInstance().
141 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700142 return 0;
143}
144
Tom Cherryb7349902015-08-26 11:43:36 -0700145static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700146 ServiceManager::GetInstance().
147 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800148 return 0;
149}
150
Steven Moreland2b63d542017-03-10 14:04:37 -0800151static int do_class_restart(const std::vector<std::string>& args) {
152 ServiceManager::GetInstance().
153 ForEachServiceInClass(args[1], [] (Service* s) { s->Restart(); });
154 return 0;
155}
156
Tom Cherryb7349902015-08-26 11:43:36 -0700157static int do_domainname(const std::vector<std::string>& args) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500158 return write_file("/proc/sys/kernel/domainname", args[1].c_str()) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159}
160
Tom Cherryb7349902015-08-26 11:43:36 -0700161static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700162 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
163 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700164 return -1;
165 }
Tom Cherrybac32992015-07-31 12:45:25 -0700166 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700167}
168
Tom Cherryb7349902015-08-26 11:43:36 -0700169static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700170 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
171 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800172 return -1;
173 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800174 if (!start_waiting_for_exec()) {
Tom Cherrybac32992015-07-31 12:45:25 -0700175 return -1;
176 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800177 if (!svc->Start()) {
178 stop_waiting_for_exec();
179 ServiceManager::GetInstance().RemoveService(*svc);
180 return -1;
181 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800182 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183}
184
Tom Cherryb7349902015-08-26 11:43:36 -0700185static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700186 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700187}
188
Tom Cherryb7349902015-08-26 11:43:36 -0700189static int do_hostname(const std::vector<std::string>& args) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500190 return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191}
192
Tom Cherryb7349902015-08-26 11:43:36 -0700193static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700194 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195}
196
Tom Cherryb7349902015-08-26 11:43:36 -0700197static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800198 int flags = 0;
199 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800200
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800201 if (!(*it).compare("-f")) {
202 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
203 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800204 }
205
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800206 std::string filename = *it++;
207 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
208 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800209}
210
Tom Cherryb7349902015-08-26 11:43:36 -0700211static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700213 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214
215 /* mkdir <path> [mode] [owner] [group] */
216
Tom Cherry96f67312015-07-30 13:52:55 -0700217 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700218 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 }
220
Tom Cherry96f67312015-07-30 13:52:55 -0700221 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700222 /* chmod in case the directory already exists */
223 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700224 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700225 }
226 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227 return -errno;
228 }
229
Tom Cherry96f67312015-07-30 13:52:55 -0700230 if (args.size() >= 4) {
231 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232 gid_t gid = -1;
233
Tom Cherry96f67312015-07-30 13:52:55 -0700234 if (args.size() == 5) {
235 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236 }
237
Tom Cherry96f67312015-07-30 13:52:55 -0700238 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239 return -errno;
240 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700241
242 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
243 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700244 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700245 if (ret == -1) {
246 return -errno;
247 }
248 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700249 }
250
Paul Crowleyaf8be582016-05-10 08:52:06 -0700251 if (e4crypt_is_native()) {
252 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800253 const std::vector<std::string> options = {
254 "--prompt_and_wipe_data",
255 "--reason=set_policy_failed:"s + args[1]};
256 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700257 return -1;
258 }
259 }
260 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261}
262
Alex Light68ab20f2016-06-23 11:11:39 -0700263/* umount <path> */
264static int do_umount(const std::vector<std::string>& args) {
265 return umount(args[1].c_str());
266}
267
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268static struct {
269 const char *name;
270 unsigned flag;
271} mount_flags[] = {
272 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200273 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274 { "nosuid", MS_NOSUID },
275 { "nodev", MS_NODEV },
276 { "nodiratime", MS_NODIRATIME },
277 { "ro", MS_RDONLY },
278 { "rw", 0 },
279 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700280 { "bind", MS_BIND },
281 { "rec", MS_REC },
282 { "unbindable", MS_UNBINDABLE },
283 { "private", MS_PRIVATE },
284 { "slave", MS_SLAVE },
285 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 { "defaults", 0 },
287 { 0, 0 },
288};
289
Ken Sumrall752923c2010-12-03 16:33:31 -0800290#define DATA_MNT_POINT "/data"
291
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700293static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700295 const char *source, *target, *system;
296 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700297 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700298 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700300 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301
Tom Cherry96f67312015-07-30 13:52:55 -0700302 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700304 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 flags |= mount_flags[i].flag;
306 break;
307 }
308 }
309
Colin Crosscd0f1732010-04-19 17:10:24 -0700310 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700311 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700312 wait = 1;
313 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700314 else if (na + 1 == args.size())
315 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700316 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 }
318
Tom Cherry96f67312015-07-30 13:52:55 -0700319 system = args[1].c_str();
320 source = args[2].c_str();
321 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000322
Elliott Hughes31951162016-06-24 15:15:03 -0700323 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000324 int mode, loop, fd;
325 struct loop_info info;
326
327 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800328 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000329 if (fd < 0) {
330 return -1;
331 }
332
333 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800334 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800335 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000336 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100337 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000338 return -1;
339 }
340
341 /* if it is a blank loop device */
342 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
343 /* if it becomes our loop device */
344 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
345 close(fd);
346
347 if (mount(tmp, target, system, flags, options) < 0) {
348 ioctl(loop, LOOP_CLR_FD, 0);
349 close(loop);
350 return -1;
351 }
352
353 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800354 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000355 }
356 }
357
358 close(loop);
359 }
360
361 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700362 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000363 return -1;
364 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700365 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800366 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700368 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000369 }
370
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800372
373exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800374 return 0;
375
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376}
377
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800378/* Imports .rc files from the specified paths. Default ones are applied if none is given.
379 *
380 * start_index: index of the first path in the args list
381 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700382static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700383 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700384 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900385 // Fallbacks for partitions on which early mount isn't enabled.
386 if (!parser.is_system_etc_init_loaded()) {
387 parser.ParseConfig("/system/etc/init");
388 parser.set_is_system_etc_init_loaded(true);
389 }
390 if (!parser.is_vendor_etc_init_loaded()) {
391 parser.ParseConfig("/vendor/etc/init");
392 parser.set_is_vendor_etc_init_loaded(true);
393 }
394 if (!parser.is_odm_etc_init_loaded()) {
395 parser.ParseConfig("/odm/etc/init");
396 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800397 }
398 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700399 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800400 parser.ParseConfig(args[i]);
401 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700402 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700403
404 // Turning this on and letting the INFO logging be discarded adds 0.2s to
405 // Nexus 9 boot time, so it's disabled by default.
406 if (false) parser.DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700407}
408
Wei Wangd61a7e22016-08-23 11:58:09 -0700409/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800410 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700411 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700412 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700413static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700414 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700415
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700416 /*
417 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
418 * do the call in the child to provide protection to the main init
419 * process if anything goes wrong (crash or memory leak), and wait for
420 * the child to finish in the parent.
421 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700422 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700423 if (pid > 0) {
424 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700425 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700426 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700427 if (wp_ret == -1) {
428 // Unexpected error code. We will continue anyway.
429 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700430 }
431
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700432 if (WIFEXITED(status)) {
433 ret = WEXITSTATUS(status);
434 } else {
435 ret = -1;
436 }
437 } else if (pid == 0) {
438 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700439
440 // So we can always see what fs_mgr_mount_all() does.
441 // Only needed if someone explicitly changes the default log level in their init.rc.
442 android::base::ScopedLogSeverity info(android::base::INFO);
443
444 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700445 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800446 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700447 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700448 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700449 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700450 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700451 } else {
452 /* fork failed, return an error */
453 return -1;
454 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700455 return ret;
456}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700457
Wei Wangd61a7e22016-08-23 11:58:09 -0700458/* Queue event based on fs_mgr return code.
459 *
460 * code: return code of fs_mgr_mount_all
461 *
462 * This function might request a reboot, in which case it will
463 * not return.
464 *
465 * return code is processed based on input code
466 */
467static int queue_fs_event(int code) {
468 int ret = code;
469 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700470 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700471 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700472 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000473 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700474 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700475 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700476 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800477 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700478 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800479 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700480 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700481 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700482 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700483 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800484 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
485 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700486 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700487 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000488 if (e4crypt_install_keyring()) {
489 return -1;
490 }
491 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000492 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000493
494 // Although encrypted, we have device key, so we do not need to
495 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700496 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700497 } else if (code > 0) {
498 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700499 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700500 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501
502 return ret;
503}
504
Wei Wangd61a7e22016-08-23 11:58:09 -0700505/* mount_all <fstab> [ <path> ]* [--<options>]*
506 *
507 * This function might request a reboot, in which case it will
508 * not return.
509 */
510static int do_mount_all(const std::vector<std::string>& args) {
511 std::size_t na = 0;
512 bool import_rc = true;
513 bool queue_event = true;
514 int mount_mode = MOUNT_MODE_DEFAULT;
515 const char* fstabfile = args[1].c_str();
516 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800517 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700518
519 for (na = args.size() - 1; na > 1; --na) {
520 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800521 path_arg_end = na;
522 queue_event = false;
523 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800524 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700525 } else if (args[na] == "--late") {
526 path_arg_end = na;
527 import_rc = false;
528 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800529 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700530 }
531 }
532
Keun-young Park69fae7b2017-03-03 18:57:09 -0800533 std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s",
534 prop_post_fix);
535 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700536 int ret = mount_fstab(fstabfile, mount_mode);
Keun-young Park69fae7b2017-03-03 18:57:09 -0800537 property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str());
Wei Wangd61a7e22016-08-23 11:58:09 -0700538
539 if (import_rc) {
540 /* Paths of .rc files are specified at the 2nd argument and beyond */
541 import_late(args, 2, path_arg_end);
542 }
543
544 if (queue_event) {
545 /* queue_fs_event will queue event based on mount_fstab return code
546 * and return processed return code*/
547 ret = queue_fs_event(ret);
548 }
549
550 return ret;
551}
552
Tom Cherryb7349902015-08-26 11:43:36 -0700553static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700554 struct fstab *fstab;
555 int ret;
556
Tom Cherry96f67312015-07-30 13:52:55 -0700557 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700558 ret = fs_mgr_swapon_all(fstab);
559 fs_mgr_free_fstab(fstab);
560
561 return ret;
562}
563
Tom Cherryb7349902015-08-26 11:43:36 -0700564static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700565 const char* name = args[1].c_str();
566 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700567 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700568 return 0;
569}
570
Tom Cherryb7349902015-08-26 11:43:36 -0700571static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 struct rlimit limit;
573 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700574 if (android::base::ParseInt(args[1], &resource) &&
575 android::base::ParseUint(args[2], &limit.rlim_cur) &&
576 android::base::ParseUint(args[3], &limit.rlim_max)) {
577 return setrlimit(resource, &limit);
578 }
579 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
580 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581}
582
Tom Cherryb7349902015-08-26 11:43:36 -0700583static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700584 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
585 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700586 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700587 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588 }
Tom Cherrybac32992015-07-31 12:45:25 -0700589 if (!svc->Start())
590 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700591 return 0;
592}
593
Tom Cherryb7349902015-08-26 11:43:36 -0700594static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700595 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
596 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700597 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700598 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700599 }
Tom Cherrybac32992015-07-31 12:45:25 -0700600 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700601 return 0;
602}
603
Tom Cherryb7349902015-08-26 11:43:36 -0700604static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700605 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
606 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700607 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700608 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700609 }
Tom Cherrybac32992015-07-31 12:45:25 -0700610 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700611 return 0;
612}
613
Tom Cherryb7349902015-08-26 11:43:36 -0700614static int do_powerctl(const std::vector<std::string>& args) {
Keun-young Park8d01f632017-03-13 11:54:47 -0700615 const std::string& command = args[1];
Yusuke Satof93d4292015-07-21 15:50:59 -0700616 unsigned int cmd = 0;
Keun-young Park8d01f632017-03-13 11:54:47 -0700617 std::vector<std::string> cmd_params = android::base::Split(command, ",");
618 std::string reason_string = cmd_params[0];
619 std::string reboot_target = "";
620 bool runFsck = false;
621 bool commandInvalid = false;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700622
Keun-young Park8d01f632017-03-13 11:54:47 -0700623 if (cmd_params.size() > 2) {
624 commandInvalid = true;
625 } else if (cmd_params[0] == "shutdown") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700626 cmd = ANDROID_RB_POWEROFF;
Keun-young Park8d01f632017-03-13 11:54:47 -0700627 if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") {
628 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
629 // Run fsck once the file system is remounted in read-only mode.
630 runFsck = true;
631 reason_string = cmd_params[1];
632 }
633 } else if (cmd_params[0] == "reboot") {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700634 cmd = ANDROID_RB_RESTART2;
Keun-young Park8d01f632017-03-13 11:54:47 -0700635 if (cmd_params.size() == 2) {
636 reboot_target = cmd_params[1];
637 // When rebooting to the bootloader notify the bootloader writing
638 // also the BCB.
639 if (reboot_target == "bootloader") {
640 std::string err;
641 if (!write_reboot_bootloader(&err)) {
642 LOG(ERROR) << "reboot-bootloader: Error writing "
643 "bootloader_message: "
644 << err;
645 }
646 }
647 }
648 } else if (command == "thermal-shutdown") { // no additional parameter allowed
Todd Poynor16663a22017-01-31 14:15:20 -0800649 cmd = ANDROID_RB_THERMOFF;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700650 } else {
Keun-young Park8d01f632017-03-13 11:54:47 -0700651 commandInvalid = true;
652 }
653 if (commandInvalid) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700654 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700655 return -EINVAL;
656 }
657
Keun-young Park8d01f632017-03-13 11:54:47 -0700658 DoReboot(cmd, reason_string, reboot_target, runFsck);
659 return 0;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700660}
661
Tom Cherryb7349902015-08-26 11:43:36 -0700662static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700663 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664 return 0;
665}
666
Tom Cherryb7349902015-08-26 11:43:36 -0700667static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700668 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700669}
670
Tom Cherryb7349902015-08-26 11:43:36 -0700671static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700672 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800673}
674
Tom Cherryb7349902015-08-26 11:43:36 -0700675static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700676 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800677}
678
Tom Cherryb7349902015-08-26 11:43:36 -0700679static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700680 struct timezone tz = {};
681 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
682 return 0;
683 }
684 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800685}
686
Tom Cherryb7349902015-08-26 11:43:36 -0700687static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700688 int mode = -1;
689 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000690 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700691 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000692 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700693 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000694}
695
Tom Cherryb7349902015-08-26 11:43:36 -0700696static void verity_update_property(fstab_rec *fstab, const char *mount_point,
697 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100698 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
699 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000700}
701
Tom Cherryb7349902015-08-26 11:43:36 -0700702static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700703 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000704}
705
Tom Cherryb7349902015-08-26 11:43:36 -0700706static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700707 const char* path = args[1].c_str();
708 const char* value = args[2].c_str();
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500709 return write_file(path, value) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700710}
711
Tom Cherryb7349902015-08-26 11:43:36 -0700712static int do_copy(const std::vector<std::string>& args) {
Yongqin Liu82bac0d2016-12-28 16:06:19 +0800713 std::string data;
714 if (read_file(args[1].c_str(), &data)) {
715 return write_file(args[2].c_str(), data.data()) ? 0 : 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700716 }
Yongqin Liu82bac0d2016-12-28 16:06:19 +0800717 return 1;
San Mehat7c44fe52009-08-26 16:39:25 -0700718}
719
Tom Cherryb7349902015-08-26 11:43:36 -0700720static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700721 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700722 if (args.size() == 3) {
723 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700724 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700725 } else if (args.size() == 4) {
726 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
727 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700728 return -errno;
729 } else {
730 return -1;
731 }
732 return 0;
733}
734
735static mode_t get_mode(const char *s) {
736 mode_t mode = 0;
737 while (*s) {
738 if (*s >= '0' && *s <= '7') {
739 mode = (mode<<3) | (*s-'0');
740 } else {
741 return -1;
742 }
743 s++;
744 }
745 return mode;
746}
747
Tom Cherryb7349902015-08-26 11:43:36 -0700748static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700749 mode_t mode = get_mode(args[1].c_str());
750 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700751 return -errno;
752 }
753 return 0;
754}
755
Tom Cherryb7349902015-08-26 11:43:36 -0700756static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400757 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500758
Paul Lawrencea8d84342016-11-14 15:40:18 -0800759 struct flag_type {const char* name; int value;};
760 static const flag_type flags[] = {
761 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
762 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
763 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
764 {0, 0}
765 };
766
767 int flag = 0;
768
769 bool in_flags = true;
770 for (size_t i = 1; i < args.size(); ++i) {
771 if (android::base::StartsWith(args[i], "--")) {
772 if (!in_flags) {
773 LOG(ERROR) << "restorecon - flags must precede paths";
774 return -1;
775 }
776 bool found = false;
777 for (size_t j = 0; flags[j].name; ++j) {
778 if (args[i] == flags[j].name) {
779 flag |= flags[j].value;
780 found = true;
781 break;
782 }
783 }
784 if (!found) {
785 LOG(ERROR) << "restorecon - bad flag " << args[i];
786 return -1;
787 }
788 } else {
789 in_flags = false;
790 if (restorecon(args[i].c_str(), flag) < 0) {
791 ret = -errno;
792 }
793 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500794 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400795 return ret;
796}
797
Tom Cherryb7349902015-08-26 11:43:36 -0700798static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800799 std::vector<std::string> non_const_args(args);
800 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
801 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500802}
803
Tom Cherryb7349902015-08-26 11:43:36 -0700804static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700805 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700806 int log_level = -1;
807 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700808 android::base::LogSeverity severity;
809 switch (log_level) {
810 case 7: severity = android::base::DEBUG; break;
811 case 6: severity = android::base::INFO; break;
812 case 5:
813 case 4: severity = android::base::WARNING; break;
814 case 3: severity = android::base::ERROR; break;
815 case 2:
816 case 1:
817 case 0: severity = android::base::FATAL; break;
818 default:
819 LOG(ERROR) << "loglevel: invalid log level " << log_level;
820 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700821 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700822 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700823 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824}
825
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700826static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700827 load_persist_props();
828 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800829}
830
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700831static int do_load_system_props(const std::vector<std::string>& args) {
832 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700833 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700834}
835
Tom Cherryb7349902015-08-26 11:43:36 -0700836static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700837 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800838 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700839 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700840 int timeout;
841 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800842 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700843 }
844 }
845 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700846}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000847
Wei Wang132ac312017-01-25 16:27:03 -0800848static int do_wait_for_prop(const std::vector<std::string>& args) {
849 const char* name = args[1].c_str();
850 const char* value = args[2].c_str();
851 size_t value_len = strlen(value);
852
853 if (!is_legal_property_name(name)) {
854 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
855 << "\") failed: bad name";
856 return -1;
857 }
858 if (value_len >= PROP_VALUE_MAX) {
859 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
860 << "\") failed: value too long";
861 return -1;
862 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800863 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -0800864 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
865 << "\") failed: init already in waiting";
866 return -1;
867 }
868 return 0;
869}
870
Paul Lawrence806d10b2015-04-28 22:07:10 +0000871/*
872 * Callback to make a directory from the ext4 code
873 */
Tom Cherryb7349902015-08-26 11:43:36 -0700874static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000875 if (make_dir(dir, 0700) && errno != EEXIST) {
876 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000877 }
878
Paul Lawrence806d10b2015-04-28 22:07:10 +0000879 return 0;
880}
881
Paul Crowley749af8c2015-05-28 17:35:06 +0100882static bool is_file_crypto() {
Tom Cherryccf23532017-03-28 16:40:41 -0700883 return android::base::GetProperty("ro.crypto.type", "") == "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}