blob: 0b2e761e786485c20570b59083fc85d6373cb2ea [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/partition_utils.h>
55#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070056#include <ext4_utils/ext4_crypt.h>
57#include <ext4_utils/ext4_crypt_init_extensions.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"
67#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
Yusuke Sato0df08272015-07-08 14:57:07 -070074#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080075
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
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700119// Turn off backlight while we are performing power down cleanup activities.
120static void turnOffBacklight() {
121 static const char off[] = "0";
122
123 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
124
125 static const char backlightDir[] = "/sys/class/backlight";
126 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
127 if (!dir) {
128 return;
129 }
130
131 struct dirent *dp;
132 while ((dp = readdir(dir.get())) != NULL) {
133 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
134 (dp->d_name[0] == '.')) {
135 continue;
136 }
137
138 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
139 backlightDir,
140 dp->d_name);
141 android::base::WriteStringToFile(off, fileName);
142 }
143}
144
Paul Crowley0b8b2302016-12-19 13:03:47 -0800145static int reboot_into_recovery(const std::vector<std::string>& options) {
Paul Crowleyaf8be582016-05-10 08:52:06 -0700146 std::string err;
147 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700148 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700149 return -1;
150 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000151 reboot("recovery");
Paul Crowleyaf8be582016-05-10 08:52:06 -0700152}
153
Tom Cherryb7349902015-08-26 11:43:36 -0700154static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700155 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
156 return;
157
158 /* First, lazily unmount the directory. This unmount request finishes when
159 * all processes that open a file or directory in |entry->mnt_dir| exit.
160 */
161 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
162
163 /* Next, kill all processes except init, kthreadd, and kthreadd's
164 * children to finish the lazy unmount. Killing all processes here is okay
165 * because this callback function is only called right before reboot().
166 * It might be cleaner to selectively kill processes that actually use
167 * |entry->mnt_dir| rather than killing all, probably by reusing a function
168 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
169 * not allow init to scan /proc/<pid> files which the utility function
170 * heavily relies on. The policy does not allow the process to execute
171 * killall/pkill binaries either. Note that some processes might
172 * automatically restart after kill(), but that is not really a problem
173 * because |entry->mnt_dir| is no longer visible to such new processes.
174 */
Tom Cherrybac32992015-07-31 12:45:25 -0700175 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700176 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
177
Mark Salyzynad575e02016-04-05 08:10:25 -0700178 // Restart Watchdogd to allow us to complete umounting and fsck
179 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
180 if (svc) {
181 do {
182 sched_yield(); // do not be so eager, let cleanup have priority
183 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
184 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
185 svc->Start();
186 }
187
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700188 turnOffBacklight();
189
Yusuke Sato0df08272015-07-08 14:57:07 -0700190 int count = 0;
191 while (count++ < UNMOUNT_CHECK_TIMES) {
192 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
193 if (fd >= 0) {
194 /* |entry->mnt_dir| has sucessfully been unmounted. */
195 close(fd);
196 break;
197 } else if (errno == EBUSY) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800198 // Some processes using |entry->mnt_dir| are still alive. Wait for a
199 // while then retry.
200 std::this_thread::sleep_for(5000ms / UNMOUNT_CHECK_TIMES);
Yusuke Sato0df08272015-07-08 14:57:07 -0700201 continue;
202 } else {
203 /* Cannot open the device. Give up. */
204 return;
205 }
206 }
207
Mark Salyzynad575e02016-04-05 08:10:25 -0700208 // NB: With watchdog still running, there is no cap on the time it takes
209 // to complete the fsck, from the users perspective the device graphics
210 // and responses are locked-up and they may choose to hold the power
211 // button in frustration if it drags out.
212
Yusuke Sato0df08272015-07-08 14:57:07 -0700213 int st;
214 if (!strcmp(entry->mnt_type, "f2fs")) {
215 const char *f2fs_argv[] = {
216 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
217 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700218 android_fork_execvp_ext(arraysize(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700219 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700220 } else if (!strcmp(entry->mnt_type, "ext4")) {
221 const char *ext4_argv[] = {
222 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
223 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700224 android_fork_execvp_ext(arraysize(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700225 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700226 }
227}
228
Tom Cherryb7349902015-08-26 11:43:36 -0700229static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230 /* Starting a class does not start services
231 * which are explicitly disabled. They must
232 * be started individually.
233 */
Tom Cherrybac32992015-07-31 12:45:25 -0700234 ServiceManager::GetInstance().
235 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236 return 0;
237}
238
Tom Cherryb7349902015-08-26 11:43:36 -0700239static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700240 ServiceManager::GetInstance().
241 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242 return 0;
243}
244
Tom Cherryb7349902015-08-26 11:43:36 -0700245static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700246 ServiceManager::GetInstance().
247 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800248 return 0;
249}
250
Tom Cherryb7349902015-08-26 11:43:36 -0700251static int do_domainname(const std::vector<std::string>& args) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500252 return write_file("/proc/sys/kernel/domainname", args[1].c_str()) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700253}
254
Tom Cherryb7349902015-08-26 11:43:36 -0700255static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700256 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
257 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700258 return -1;
259 }
Tom Cherrybac32992015-07-31 12:45:25 -0700260 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700261}
262
Tom Cherryb7349902015-08-26 11:43:36 -0700263static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700264 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
265 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800266 return -1;
267 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800268 if (!start_waiting_for_exec()) {
Tom Cherrybac32992015-07-31 12:45:25 -0700269 return -1;
270 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -0800271 if (!svc->Start()) {
272 stop_waiting_for_exec();
273 ServiceManager::GetInstance().RemoveService(*svc);
274 return -1;
275 }
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800276 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277}
278
Tom Cherryb7349902015-08-26 11:43:36 -0700279static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700280 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281}
282
Tom Cherryb7349902015-08-26 11:43:36 -0700283static int do_hostname(const std::vector<std::string>& args) {
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500284 return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285}
286
Tom Cherryb7349902015-08-26 11:43:36 -0700287static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700288 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289}
290
Tom Cherryb7349902015-08-26 11:43:36 -0700291static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800292 int flags = 0;
293 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800294
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800295 if (!(*it).compare("-f")) {
296 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
297 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800298 }
299
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800300 std::string filename = *it++;
301 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
302 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800303}
304
Tom Cherryb7349902015-08-26 11:43:36 -0700305static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700307 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308
309 /* mkdir <path> [mode] [owner] [group] */
310
Tom Cherry96f67312015-07-30 13:52:55 -0700311 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700312 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313 }
314
Tom Cherry96f67312015-07-30 13:52:55 -0700315 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700316 /* chmod in case the directory already exists */
317 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700318 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700319 }
320 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 return -errno;
322 }
323
Tom Cherry96f67312015-07-30 13:52:55 -0700324 if (args.size() >= 4) {
325 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 gid_t gid = -1;
327
Tom Cherry96f67312015-07-30 13:52:55 -0700328 if (args.size() == 5) {
329 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330 }
331
Tom Cherry96f67312015-07-30 13:52:55 -0700332 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333 return -errno;
334 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700335
336 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
337 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700338 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700339 if (ret == -1) {
340 return -errno;
341 }
342 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700343 }
344
Paul Crowleyaf8be582016-05-10 08:52:06 -0700345 if (e4crypt_is_native()) {
346 if (e4crypt_set_directory_policy(args[1].c_str())) {
Paul Crowley0b8b2302016-12-19 13:03:47 -0800347 const std::vector<std::string> options = {
348 "--prompt_and_wipe_data",
349 "--reason=set_policy_failed:"s + args[1]};
350 reboot_into_recovery(options);
Paul Crowleyaf8be582016-05-10 08:52:06 -0700351 return -1;
352 }
353 }
354 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355}
356
Alex Light68ab20f2016-06-23 11:11:39 -0700357/* umount <path> */
358static int do_umount(const std::vector<std::string>& args) {
359 return umount(args[1].c_str());
360}
361
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362static struct {
363 const char *name;
364 unsigned flag;
365} mount_flags[] = {
366 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200367 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700368 { "nosuid", MS_NOSUID },
369 { "nodev", MS_NODEV },
370 { "nodiratime", MS_NODIRATIME },
371 { "ro", MS_RDONLY },
372 { "rw", 0 },
373 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700374 { "bind", MS_BIND },
375 { "rec", MS_REC },
376 { "unbindable", MS_UNBINDABLE },
377 { "private", MS_PRIVATE },
378 { "slave", MS_SLAVE },
379 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380 { "defaults", 0 },
381 { 0, 0 },
382};
383
Ken Sumrall752923c2010-12-03 16:33:31 -0800384#define DATA_MNT_POINT "/data"
385
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700387static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700389 const char *source, *target, *system;
390 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700391 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700392 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700393 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700394 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395
Tom Cherry96f67312015-07-30 13:52:55 -0700396 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700398 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 flags |= mount_flags[i].flag;
400 break;
401 }
402 }
403
Colin Crosscd0f1732010-04-19 17:10:24 -0700404 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700405 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700406 wait = 1;
407 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700408 else if (na + 1 == args.size())
409 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700410 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411 }
412
Tom Cherry96f67312015-07-30 13:52:55 -0700413 system = args[1].c_str();
414 source = args[2].c_str();
415 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000416
Elliott Hughes31951162016-06-24 15:15:03 -0700417 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 int mode, loop, fd;
419 struct loop_info info;
420
421 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800422 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000423 if (fd < 0) {
424 return -1;
425 }
426
427 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800428 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800429 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000430 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100431 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000432 return -1;
433 }
434
435 /* if it is a blank loop device */
436 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
437 /* if it becomes our loop device */
438 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
439 close(fd);
440
441 if (mount(tmp, target, system, flags, options) < 0) {
442 ioctl(loop, LOOP_CLR_FD, 0);
443 close(loop);
444 return -1;
445 }
446
447 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800448 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000449 }
450 }
451
452 close(loop);
453 }
454
455 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700456 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000457 return -1;
458 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700459 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800460 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000461 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700462 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000463 }
464
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800466
467exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800468 return 0;
469
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470}
471
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800472/* Imports .rc files from the specified paths. Default ones are applied if none is given.
473 *
474 * start_index: index of the first path in the args list
475 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700476static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700477 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700478 if (end_index <= start_index) {
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +0900479 // Fallbacks for partitions on which early mount isn't enabled.
480 if (!parser.is_system_etc_init_loaded()) {
481 parser.ParseConfig("/system/etc/init");
482 parser.set_is_system_etc_init_loaded(true);
483 }
484 if (!parser.is_vendor_etc_init_loaded()) {
485 parser.ParseConfig("/vendor/etc/init");
486 parser.set_is_vendor_etc_init_loaded(true);
487 }
488 if (!parser.is_odm_etc_init_loaded()) {
489 parser.ParseConfig("/odm/etc/init");
490 parser.set_is_odm_etc_init_loaded(true);
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800491 }
492 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700493 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800494 parser.ParseConfig(args[i]);
495 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700496 }
Tom Cherryd8a72572017-03-13 12:24:49 -0700497
498 // Turning this on and letting the INFO logging be discarded adds 0.2s to
499 // Nexus 9 boot time, so it's disabled by default.
500 if (false) parser.DumpState();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700501}
502
Wei Wangd61a7e22016-08-23 11:58:09 -0700503/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800504 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700505 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700506 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700507static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700508 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700509
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700510 /*
511 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
512 * do the call in the child to provide protection to the main init
513 * process if anything goes wrong (crash or memory leak), and wait for
514 * the child to finish in the parent.
515 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700516 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700517 if (pid > 0) {
518 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700519 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700520 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700521 if (wp_ret == -1) {
522 // Unexpected error code. We will continue anyway.
523 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700524 }
525
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700526 if (WIFEXITED(status)) {
527 ret = WEXITSTATUS(status);
528 } else {
529 ret = -1;
530 }
531 } else if (pid == 0) {
532 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700533
534 // So we can always see what fs_mgr_mount_all() does.
535 // Only needed if someone explicitly changes the default log level in their init.rc.
536 android::base::ScopedLogSeverity info(android::base::INFO);
537
538 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700539 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800540 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700541 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700542 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700543 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700544 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700545 } else {
546 /* fork failed, return an error */
547 return -1;
548 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700549 return ret;
550}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700551
Wei Wangd61a7e22016-08-23 11:58:09 -0700552/* Queue event based on fs_mgr return code.
553 *
554 * code: return code of fs_mgr_mount_all
555 *
556 * This function might request a reboot, in which case it will
557 * not return.
558 *
559 * return code is processed based on input code
560 */
561static int queue_fs_event(int code) {
562 int ret = code;
563 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700564 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700565 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700566 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000567 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700568 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700569 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700570 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800571 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700572 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800573 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700574 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700575 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700576 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700577 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowley0b8b2302016-12-19 13:03:47 -0800578 const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" };
579 ret = reboot_into_recovery(options);
JP Abgrallcee20682014-07-02 14:26:54 -0700580 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700581 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000582 if (e4crypt_install_keyring()) {
583 return -1;
584 }
585 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000586 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000587
588 // Although encrypted, we have device key, so we do not need to
589 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700590 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700591 } else if (code > 0) {
592 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700593 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700594 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700595
596 return ret;
597}
598
Wei Wangd61a7e22016-08-23 11:58:09 -0700599/* mount_all <fstab> [ <path> ]* [--<options>]*
600 *
601 * This function might request a reboot, in which case it will
602 * not return.
603 */
604static int do_mount_all(const std::vector<std::string>& args) {
605 std::size_t na = 0;
606 bool import_rc = true;
607 bool queue_event = true;
608 int mount_mode = MOUNT_MODE_DEFAULT;
609 const char* fstabfile = args[1].c_str();
610 std::size_t path_arg_end = args.size();
Keun-young Park69fae7b2017-03-03 18:57:09 -0800611 const char* prop_post_fix = "default";
Wei Wangd61a7e22016-08-23 11:58:09 -0700612
613 for (na = args.size() - 1; na > 1; --na) {
614 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800615 path_arg_end = na;
616 queue_event = false;
617 mount_mode = MOUNT_MODE_EARLY;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800618 prop_post_fix = "early";
Wei Wangd61a7e22016-08-23 11:58:09 -0700619 } else if (args[na] == "--late") {
620 path_arg_end = na;
621 import_rc = false;
622 mount_mode = MOUNT_MODE_LATE;
Keun-young Park69fae7b2017-03-03 18:57:09 -0800623 prop_post_fix = "late";
Wei Wangd61a7e22016-08-23 11:58:09 -0700624 }
625 }
626
Keun-young Park69fae7b2017-03-03 18:57:09 -0800627 std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s",
628 prop_post_fix);
629 Timer t;
Wei Wangd61a7e22016-08-23 11:58:09 -0700630 int ret = mount_fstab(fstabfile, mount_mode);
Keun-young Park69fae7b2017-03-03 18:57:09 -0800631 property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str());
Wei Wangd61a7e22016-08-23 11:58:09 -0700632
633 if (import_rc) {
634 /* Paths of .rc files are specified at the 2nd argument and beyond */
635 import_late(args, 2, path_arg_end);
636 }
637
638 if (queue_event) {
639 /* queue_fs_event will queue event based on mount_fstab return code
640 * and return processed return code*/
641 ret = queue_fs_event(ret);
642 }
643
644 return ret;
645}
646
Tom Cherryb7349902015-08-26 11:43:36 -0700647static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700648 struct fstab *fstab;
649 int ret;
650
Tom Cherry96f67312015-07-30 13:52:55 -0700651 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700652 ret = fs_mgr_swapon_all(fstab);
653 fs_mgr_free_fstab(fstab);
654
655 return ret;
656}
657
Tom Cherryb7349902015-08-26 11:43:36 -0700658static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700659 const char* name = args[1].c_str();
660 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700661 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700662 return 0;
663}
664
Tom Cherryb7349902015-08-26 11:43:36 -0700665static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700666 struct rlimit limit;
667 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700668 if (android::base::ParseInt(args[1], &resource) &&
669 android::base::ParseUint(args[2], &limit.rlim_cur) &&
670 android::base::ParseUint(args[3], &limit.rlim_max)) {
671 return setrlimit(resource, &limit);
672 }
673 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
674 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700675}
676
Tom Cherryb7349902015-08-26 11:43:36 -0700677static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700678 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
679 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700680 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700681 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682 }
Tom Cherrybac32992015-07-31 12:45:25 -0700683 if (!svc->Start())
684 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700685 return 0;
686}
687
Tom Cherryb7349902015-08-26 11:43:36 -0700688static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700689 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
690 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700691 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700692 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700693 }
Tom Cherrybac32992015-07-31 12:45:25 -0700694 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700695 return 0;
696}
697
Tom Cherryb7349902015-08-26 11:43:36 -0700698static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700699 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
700 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700701 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700702 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700703 }
Tom Cherrybac32992015-07-31 12:45:25 -0700704 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705 return 0;
706}
707
Tom Cherryb7349902015-08-26 11:43:36 -0700708static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700709 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700710 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700711 unsigned int cmd = 0;
712 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700713 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700714
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700715 if (strncmp(command, "shutdown", 8) == 0) {
716 cmd = ANDROID_RB_POWEROFF;
717 len = 8;
718 } else if (strncmp(command, "reboot", 6) == 0) {
719 cmd = ANDROID_RB_RESTART2;
720 len = 6;
721 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700722 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700723 return -EINVAL;
724 }
725
726 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700727 if (cmd == ANDROID_RB_POWEROFF &&
728 !strcmp(&command[len + 1], "userrequested")) {
729 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
730 // Run fsck once the file system is remounted in read-only mode.
731 callback_on_ro_remount = unmount_and_fsck;
732 } else if (cmd == ANDROID_RB_RESTART2) {
733 reboot_target = &command[len + 1];
Vineela Tummalapalli039734c2016-10-28 19:30:07 -0700734 // When rebooting to the bootloader notify the bootloader writing
735 // also the BCB.
736 if (strcmp(reboot_target, "bootloader") == 0) {
737 std::string err;
738 if (!write_reboot_bootloader(&err)) {
739 LOG(ERROR) << "reboot-bootloader: Error writing "
740 "bootloader_message: " << err;
741 }
742 }
Yusuke Satof93d4292015-07-21 15:50:59 -0700743 }
744 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700745 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700746 return -EINVAL;
747 }
748
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800749 std::string timeout = property_get("ro.build.shutdown_timeout");
750 unsigned int delay = 0;
751
Elliott Hughesda46b392016-10-11 17:09:00 -0700752 if (android::base::ParseUint(timeout, &delay) && delay > 0) {
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800753 Timer t;
754 // Ask all services to terminate.
755 ServiceManager::GetInstance().ForEachService(
756 [] (Service* s) { s->Terminate(); });
757
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000758 while (t.duration_s() < delay) {
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800759 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
760
761 int service_count = 0;
762 ServiceManager::GetInstance().ForEachService(
763 [&service_count] (Service* s) {
764 // Count the number of services running.
765 // Exclude the console as it will ignore the SIGTERM signal
766 // and not exit.
767 // Note: SVC_CONSOLE actually means "requires console" but
768 // it is only used by the shell.
769 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
770 service_count++;
771 }
772 });
773
774 if (service_count == 0) {
775 // All terminable services terminated. We can exit early.
776 break;
777 }
778
779 // Wait a bit before recounting the number or running services.
Elliott Hughes290a2282016-11-14 17:08:47 -0800780 std::this_thread::sleep_for(50ms);
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800781 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000782 LOG(VERBOSE) << "Terminating running services took " << t;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800783 }
784
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000785 return android_reboot_with_callback(cmd, 0, reboot_target, callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700786}
787
Tom Cherryb7349902015-08-26 11:43:36 -0700788static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700789 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700790 return 0;
791}
792
Tom Cherryb7349902015-08-26 11:43:36 -0700793static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700794 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795}
796
Tom Cherryb7349902015-08-26 11:43:36 -0700797static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700798 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800799}
800
Tom Cherryb7349902015-08-26 11:43:36 -0700801static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700802 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800803}
804
Tom Cherryb7349902015-08-26 11:43:36 -0700805static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700806 struct timezone tz = {};
807 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
808 return 0;
809 }
810 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800811}
812
Tom Cherryb7349902015-08-26 11:43:36 -0700813static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700814 int mode = -1;
815 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000816 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700817 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000818 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700819 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000820}
821
Tom Cherryb7349902015-08-26 11:43:36 -0700822static void verity_update_property(fstab_rec *fstab, const char *mount_point,
823 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100824 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
825 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000826}
827
Tom Cherryb7349902015-08-26 11:43:36 -0700828static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700829 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000830}
831
Tom Cherryb7349902015-08-26 11:43:36 -0700832static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700833 const char* path = args[1].c_str();
834 const char* value = args[2].c_str();
Jorge Lucangeli Obes77f0e9f2016-12-28 14:07:02 -0500835 return write_file(path, value) ? 0 : 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836}
837
Tom Cherryb7349902015-08-26 11:43:36 -0700838static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700839 char *buffer = NULL;
840 int rc = 0;
841 int fd1 = -1, fd2 = -1;
842 struct stat info;
843 int brtw, brtr;
844 char *p;
845
Tom Cherry96f67312015-07-30 13:52:55 -0700846 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700847 return -1;
848
Tom Cherry96f67312015-07-30 13:52:55 -0700849 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700850 goto out_err;
851
Tom Cherry96f67312015-07-30 13:52:55 -0700852 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700853 goto out_err;
854
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800855 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700856 goto out_err;
857
858 p = buffer;
859 brtr = info.st_size;
860 while(brtr) {
861 rc = read(fd1, p, brtr);
862 if (rc < 0)
863 goto out_err;
864 if (rc == 0)
865 break;
866 p += rc;
867 brtr -= rc;
868 }
869
870 p = buffer;
871 brtw = info.st_size;
872 while(brtw) {
873 rc = write(fd2, p, brtw);
874 if (rc < 0)
875 goto out_err;
876 if (rc == 0)
877 break;
878 p += rc;
879 brtw -= rc;
880 }
881
882 rc = 0;
883 goto out;
884out_err:
885 rc = -1;
886out:
887 if (buffer)
888 free(buffer);
889 if (fd1 >= 0)
890 close(fd1);
891 if (fd2 >= 0)
892 close(fd2);
893 return rc;
894}
895
Tom Cherryb7349902015-08-26 11:43:36 -0700896static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700897 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700898 if (args.size() == 3) {
899 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700901 } else if (args.size() == 4) {
902 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
903 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700904 return -errno;
905 } else {
906 return -1;
907 }
908 return 0;
909}
910
911static mode_t get_mode(const char *s) {
912 mode_t mode = 0;
913 while (*s) {
914 if (*s >= '0' && *s <= '7') {
915 mode = (mode<<3) | (*s-'0');
916 } else {
917 return -1;
918 }
919 s++;
920 }
921 return mode;
922}
923
Tom Cherryb7349902015-08-26 11:43:36 -0700924static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700925 mode_t mode = get_mode(args[1].c_str());
926 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700927 return -errno;
928 }
929 return 0;
930}
931
Tom Cherryb7349902015-08-26 11:43:36 -0700932static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400933 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500934
Paul Lawrencea8d84342016-11-14 15:40:18 -0800935 struct flag_type {const char* name; int value;};
936 static const flag_type flags[] = {
937 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
938 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
939 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
940 {0, 0}
941 };
942
943 int flag = 0;
944
945 bool in_flags = true;
946 for (size_t i = 1; i < args.size(); ++i) {
947 if (android::base::StartsWith(args[i], "--")) {
948 if (!in_flags) {
949 LOG(ERROR) << "restorecon - flags must precede paths";
950 return -1;
951 }
952 bool found = false;
953 for (size_t j = 0; flags[j].name; ++j) {
954 if (args[i] == flags[j].name) {
955 flag |= flags[j].value;
956 found = true;
957 break;
958 }
959 }
960 if (!found) {
961 LOG(ERROR) << "restorecon - bad flag " << args[i];
962 return -1;
963 }
964 } else {
965 in_flags = false;
966 if (restorecon(args[i].c_str(), flag) < 0) {
967 ret = -errno;
968 }
969 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500970 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400971 return ret;
972}
973
Tom Cherryb7349902015-08-26 11:43:36 -0700974static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800975 std::vector<std::string> non_const_args(args);
976 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
977 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500978}
979
Tom Cherryb7349902015-08-26 11:43:36 -0700980static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700981 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700982 int log_level = -1;
983 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700984 android::base::LogSeverity severity;
985 switch (log_level) {
986 case 7: severity = android::base::DEBUG; break;
987 case 6: severity = android::base::INFO; break;
988 case 5:
989 case 4: severity = android::base::WARNING; break;
990 case 3: severity = android::base::ERROR; break;
991 case 2:
992 case 1:
993 case 0: severity = android::base::FATAL; break;
994 default:
995 LOG(ERROR) << "loglevel: invalid log level " << log_level;
996 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700997 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700998 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700999 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001000}
1001
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001002static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -07001003 load_persist_props();
1004 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -08001005}
1006
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001007static int do_load_system_props(const std::vector<std::string>& args) {
1008 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -07001009 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -07001010}
1011
Tom Cherryb7349902015-08-26 11:43:36 -07001012static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -07001013 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -08001014 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -07001015 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -07001016 int timeout;
1017 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -08001018 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -07001019 }
1020 }
1021 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -07001022}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001023
Wei Wang132ac312017-01-25 16:27:03 -08001024static int do_wait_for_prop(const std::vector<std::string>& args) {
1025 const char* name = args[1].c_str();
1026 const char* value = args[2].c_str();
1027 size_t value_len = strlen(value);
1028
1029 if (!is_legal_property_name(name)) {
1030 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
1031 << "\") failed: bad name";
1032 return -1;
1033 }
1034 if (value_len >= PROP_VALUE_MAX) {
1035 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
1036 << "\") failed: value too long";
1037 return -1;
1038 }
Wei Wang2d0fdaa2017-02-02 10:52:39 -08001039 if (!start_waiting_for_property(name, value)) {
Wei Wang132ac312017-01-25 16:27:03 -08001040 LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value
1041 << "\") failed: init already in waiting";
1042 return -1;
1043 }
1044 return 0;
1045}
1046
Paul Lawrence806d10b2015-04-28 22:07:10 +00001047/*
1048 * Callback to make a directory from the ext4 code
1049 */
Tom Cherryb7349902015-08-26 11:43:36 -07001050static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +00001051 if (make_dir(dir, 0700) && errno != EEXIST) {
1052 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001053 }
1054
Paul Lawrence806d10b2015-04-28 22:07:10 +00001055 return 0;
1056}
1057
Paul Crowley749af8c2015-05-28 17:35:06 +01001058static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -07001059 std::string value = property_get("ro.crypto.type");
1060 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +01001061}
1062
Tom Cherryb7349902015-08-26 11:43:36 -07001063static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +01001064 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +00001065 return 0;
1066 }
Tom Cherry96f67312015-07-30 13:52:55 -07001067 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +00001068 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001069}
Paul Crowley749af8c2015-05-28 17:35:06 +01001070
Paul Crowley59497452016-02-01 16:37:13 +00001071static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +00001072 return e4crypt_do_init_user0();
1073}
1074
Tom Cherryb7349902015-08-26 11:43:36 -07001075BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
1076 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
1077 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -08001078 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -07001079 {"chmod", {2, 2, do_chmod}},
1080 {"chown", {2, 3, do_chown}},
1081 {"class_reset", {1, 1, do_class_reset}},
1082 {"class_start", {1, 1, do_class_start}},
1083 {"class_stop", {1, 1, do_class_stop}},
1084 {"copy", {2, 2, do_copy}},
1085 {"domainname", {1, 1, do_domainname}},
1086 {"enable", {1, 1, do_enable}},
1087 {"exec", {1, kMax, do_exec}},
1088 {"export", {2, 2, do_export}},
1089 {"hostname", {1, 1, do_hostname}},
1090 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +00001091 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -07001092 {"insmod", {1, kMax, do_insmod}},
1093 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -07001094 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001095 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -07001096 {"loglevel", {1, 1, do_loglevel}},
1097 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +08001098 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -07001099 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -07001100 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -07001101 {"powerctl", {1, 1, do_powerctl}},
1102 {"restart", {1, 1, do_restart}},
1103 {"restorecon", {1, kMax, do_restorecon}},
1104 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
1105 {"rm", {1, 1, do_rm}},
1106 {"rmdir", {1, 1, do_rmdir}},
1107 {"setprop", {2, 2, do_setprop}},
1108 {"setrlimit", {3, 3, do_setrlimit}},
1109 {"start", {1, 1, do_start}},
1110 {"stop", {1, 1, do_stop}},
1111 {"swapon_all", {1, 1, do_swapon_all}},
1112 {"symlink", {2, 2, do_symlink}},
1113 {"sysclktz", {1, 1, do_sysclktz}},
1114 {"trigger", {1, 1, do_trigger}},
1115 {"verity_load_state", {0, 0, do_verity_load_state}},
1116 {"verity_update_state", {0, 0, do_verity_update_state}},
1117 {"wait", {1, 2, do_wait}},
Wei Wang132ac312017-01-25 16:27:03 -08001118 {"wait_for_prop", {2, 2, do_wait_for_prop}},
Tom Cherryb7349902015-08-26 11:43:36 -07001119 {"write", {2, 2, do_write}},
1120 };
1121 return builtin_functions;
1122}