blob: d10f7198e803c608de9d0aac45448340963f9f5c [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
Elliott Hughes290a2282016-11-14 17:08:47 -080041#include <thread>
42
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043#include <selinux/selinux.h>
44#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050045
Elliott Hughesdb3f2672015-03-20 09:45:18 -070046#include <fs_mgr.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070047#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080048#include <android-base/parseint.h>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080049#include <android-base/strings.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/stringprintf.h>
Yabin Cui1051e102016-06-24 18:28:03 -070051#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070052#include <cutils/partition_utils.h>
53#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070054#include <ext4_utils/ext4_crypt.h>
55#include <ext4_utils/ext4_crypt_init_extensions.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070056#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070057
Tom Cherryfa0c21c2015-07-23 17:53:11 -070058#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070059#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070062#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070063#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070064#include "property_service.h"
65#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080066#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070067#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068
Nick Kralevichbc609542015-01-31 21:39:46 -080069#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070070#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080071
Elliott Hughes9605a942016-11-10 17:43:47 -080072static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080073
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080074static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070075 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
76 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070077 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080079 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080080 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070081 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070082 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070083 }
84 close(fd);
85 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086}
87
Tom Cherryb7349902015-08-26 11:43:36 -070088static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070089 struct ifreq ifr;
90 int s, ret;
91
92 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
93
94 s = socket(AF_INET, SOCK_DGRAM, 0);
95 if (s < 0)
96 return -1;
97
98 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
99 if (ret < 0) {
100 goto done;
101 }
102
103 if (up)
104 ifr.ifr_flags |= IFF_UP;
105 else
106 ifr.ifr_flags &= ~IFF_UP;
107
108 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800109
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110done:
111 close(s);
112 return ret;
113}
114
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700115// Turn off backlight while we are performing power down cleanup activities.
116static void turnOffBacklight() {
117 static const char off[] = "0";
118
119 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
120
121 static const char backlightDir[] = "/sys/class/backlight";
122 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
123 if (!dir) {
124 return;
125 }
126
127 struct dirent *dp;
128 while ((dp = readdir(dir.get())) != NULL) {
129 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
130 (dp->d_name[0] == '.')) {
131 continue;
132 }
133
134 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
135 backlightDir,
136 dp->d_name);
137 android::base::WriteStringToFile(off, fileName);
138 }
139}
140
Paul Crowleyaf8be582016-05-10 08:52:06 -0700141static int wipe_data_via_recovery(const std::string& reason) {
142 const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
143 std::string err;
144 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700145 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700146 return -1;
147 }
148 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
149 while (1) { pause(); } // never reached
150}
151
Tom Cherryb7349902015-08-26 11:43:36 -0700152static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700153 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
154 return;
155
156 /* First, lazily unmount the directory. This unmount request finishes when
157 * all processes that open a file or directory in |entry->mnt_dir| exit.
158 */
159 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
160
161 /* Next, kill all processes except init, kthreadd, and kthreadd's
162 * children to finish the lazy unmount. Killing all processes here is okay
163 * because this callback function is only called right before reboot().
164 * It might be cleaner to selectively kill processes that actually use
165 * |entry->mnt_dir| rather than killing all, probably by reusing a function
166 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
167 * not allow init to scan /proc/<pid> files which the utility function
168 * heavily relies on. The policy does not allow the process to execute
169 * killall/pkill binaries either. Note that some processes might
170 * automatically restart after kill(), but that is not really a problem
171 * because |entry->mnt_dir| is no longer visible to such new processes.
172 */
Tom Cherrybac32992015-07-31 12:45:25 -0700173 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700174 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
175
Mark Salyzynad575e02016-04-05 08:10:25 -0700176 // Restart Watchdogd to allow us to complete umounting and fsck
177 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
178 if (svc) {
179 do {
180 sched_yield(); // do not be so eager, let cleanup have priority
181 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
182 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
183 svc->Start();
184 }
185
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700186 turnOffBacklight();
187
Yusuke Sato0df08272015-07-08 14:57:07 -0700188 int count = 0;
189 while (count++ < UNMOUNT_CHECK_TIMES) {
190 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
191 if (fd >= 0) {
192 /* |entry->mnt_dir| has sucessfully been unmounted. */
193 close(fd);
194 break;
195 } else if (errno == EBUSY) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800196 // Some processes using |entry->mnt_dir| are still alive. Wait for a
197 // while then retry.
198 std::this_thread::sleep_for(5000ms / UNMOUNT_CHECK_TIMES);
Yusuke Sato0df08272015-07-08 14:57:07 -0700199 continue;
200 } else {
201 /* Cannot open the device. Give up. */
202 return;
203 }
204 }
205
Mark Salyzynad575e02016-04-05 08:10:25 -0700206 // NB: With watchdog still running, there is no cap on the time it takes
207 // to complete the fsck, from the users perspective the device graphics
208 // and responses are locked-up and they may choose to hold the power
209 // button in frustration if it drags out.
210
Yusuke Sato0df08272015-07-08 14:57:07 -0700211 int st;
212 if (!strcmp(entry->mnt_type, "f2fs")) {
213 const char *f2fs_argv[] = {
214 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
215 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700216 android_fork_execvp_ext(arraysize(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700217 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700218 } else if (!strcmp(entry->mnt_type, "ext4")) {
219 const char *ext4_argv[] = {
220 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
221 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700222 android_fork_execvp_ext(arraysize(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700223 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700224 }
225}
226
Tom Cherryb7349902015-08-26 11:43:36 -0700227static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228 /* Starting a class does not start services
229 * which are explicitly disabled. They must
230 * be started individually.
231 */
Tom Cherrybac32992015-07-31 12:45:25 -0700232 ServiceManager::GetInstance().
233 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234 return 0;
235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700238 ServiceManager::GetInstance().
239 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240 return 0;
241}
242
Tom Cherryb7349902015-08-26 11:43:36 -0700243static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700244 ServiceManager::GetInstance().
245 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800246 return 0;
247}
248
Tom Cherryb7349902015-08-26 11:43:36 -0700249static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700250 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251}
252
Tom Cherryb7349902015-08-26 11:43:36 -0700253static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700254 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
255 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700256 return -1;
257 }
Tom Cherrybac32992015-07-31 12:45:25 -0700258 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700262 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
263 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800264 return -1;
265 }
Tom Cherrybac32992015-07-31 12:45:25 -0700266 if (!svc->Start()) {
267 return -1;
268 }
269 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800270 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271}
272
Tom Cherryb7349902015-08-26 11:43:36 -0700273static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700274 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275}
276
Tom Cherryb7349902015-08-26 11:43:36 -0700277static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700278 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279}
280
Tom Cherryb7349902015-08-26 11:43:36 -0700281static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700282 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283}
284
Tom Cherryb7349902015-08-26 11:43:36 -0700285static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800286 int flags = 0;
287 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800288
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800289 if (!(*it).compare("-f")) {
290 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
291 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800292 }
293
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800294 std::string filename = *it++;
295 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
296 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800297}
298
Tom Cherryb7349902015-08-26 11:43:36 -0700299static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700301 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
303 /* mkdir <path> [mode] [owner] [group] */
304
Tom Cherry96f67312015-07-30 13:52:55 -0700305 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700306 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 }
308
Tom Cherry96f67312015-07-30 13:52:55 -0700309 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700310 /* chmod in case the directory already exists */
311 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700312 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700313 }
314 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 return -errno;
316 }
317
Tom Cherry96f67312015-07-30 13:52:55 -0700318 if (args.size() >= 4) {
319 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 gid_t gid = -1;
321
Tom Cherry96f67312015-07-30 13:52:55 -0700322 if (args.size() == 5) {
323 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
325
Tom Cherry96f67312015-07-30 13:52:55 -0700326 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 return -errno;
328 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700329
330 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
331 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700332 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700333 if (ret == -1) {
334 return -errno;
335 }
336 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 }
338
Paul Crowleyaf8be582016-05-10 08:52:06 -0700339 if (e4crypt_is_native()) {
340 if (e4crypt_set_directory_policy(args[1].c_str())) {
341 wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
342 return -1;
343 }
344 }
345 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346}
347
348static struct {
349 const char *name;
350 unsigned flag;
351} mount_flags[] = {
352 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200353 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 { "nosuid", MS_NOSUID },
355 { "nodev", MS_NODEV },
356 { "nodiratime", MS_NODIRATIME },
357 { "ro", MS_RDONLY },
358 { "rw", 0 },
359 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700360 { "bind", MS_BIND },
361 { "rec", MS_REC },
362 { "unbindable", MS_UNBINDABLE },
363 { "private", MS_PRIVATE },
364 { "slave", MS_SLAVE },
365 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 { "defaults", 0 },
367 { 0, 0 },
368};
369
Ken Sumrall752923c2010-12-03 16:33:31 -0800370#define DATA_MNT_POINT "/data"
371
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700373static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700375 const char *source, *target, *system;
376 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700378 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700380 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381
Tom Cherry96f67312015-07-30 13:52:55 -0700382 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700384 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 flags |= mount_flags[i].flag;
386 break;
387 }
388 }
389
Colin Crosscd0f1732010-04-19 17:10:24 -0700390 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700391 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700392 wait = 1;
393 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700394 else if (na + 1 == args.size())
395 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700396 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 }
398
Tom Cherry96f67312015-07-30 13:52:55 -0700399 system = args[1].c_str();
400 source = args[2].c_str();
401 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000402
Elliott Hughes31951162016-06-24 15:15:03 -0700403 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000404 int mode, loop, fd;
405 struct loop_info info;
406
407 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800408 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000409 if (fd < 0) {
410 return -1;
411 }
412
413 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800414 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800415 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000416 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100417 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 return -1;
419 }
420
421 /* if it is a blank loop device */
422 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
423 /* if it becomes our loop device */
424 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
425 close(fd);
426
427 if (mount(tmp, target, system, flags, options) < 0) {
428 ioctl(loop, LOOP_CLR_FD, 0);
429 close(loop);
430 return -1;
431 }
432
433 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800434 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000435 }
436 }
437
438 close(loop);
439 }
440
441 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700442 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000443 return -1;
444 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700445 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800446 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000447 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700448 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000449 }
450
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700451 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800452
453exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800454 return 0;
455
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456}
457
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800458/* Imports .rc files from the specified paths. Default ones are applied if none is given.
459 *
460 * start_index: index of the first path in the args list
461 */
Wei Wang254f4432016-08-23 11:58:09 -0700462static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700463 Parser& parser = Parser::GetInstance();
Wei Wang254f4432016-08-23 11:58:09 -0700464 if (end_index <= start_index) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800465 // Use the default set if no path is given
466 static const std::vector<std::string> init_directories = {
467 "/system/etc/init",
468 "/vendor/etc/init",
469 "/odm/etc/init"
470 };
471
472 for (const auto& dir : init_directories) {
473 parser.ParseConfig(dir);
474 }
475 } else {
Wei Wang254f4432016-08-23 11:58:09 -0700476 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800477 parser.ParseConfig(args[i]);
478 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700479 }
480}
481
Wei Wang254f4432016-08-23 11:58:09 -0700482/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800483 *
Wei Wang254f4432016-08-23 11:58:09 -0700484 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700485 */
Wei Wang254f4432016-08-23 11:58:09 -0700486static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700487 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700488
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700489 /*
490 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
491 * do the call in the child to provide protection to the main init
492 * process if anything goes wrong (crash or memory leak), and wait for
493 * the child to finish in the parent.
494 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700495 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700496 if (pid > 0) {
497 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700498 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700499 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700500 if (wp_ret == -1) {
501 // Unexpected error code. We will continue anyway.
502 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700503 }
504
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700505 if (WIFEXITED(status)) {
506 ret = WEXITSTATUS(status);
507 } else {
508 ret = -1;
509 }
510 } else if (pid == 0) {
511 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700512
513 // So we can always see what fs_mgr_mount_all() does.
514 // Only needed if someone explicitly changes the default log level in their init.rc.
515 android::base::ScopedLogSeverity info(android::base::INFO);
516
517 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wang254f4432016-08-23 11:58:09 -0700518 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800519 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700520 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700521 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700522 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700523 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700524 } else {
525 /* fork failed, return an error */
526 return -1;
527 }
Wei Wang254f4432016-08-23 11:58:09 -0700528 return ret;
529}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700530
Wei Wang254f4432016-08-23 11:58:09 -0700531/* Queue event based on fs_mgr return code.
532 *
533 * code: return code of fs_mgr_mount_all
534 *
535 * This function might request a reboot, in which case it will
536 * not return.
537 *
538 * return code is processed based on input code
539 */
540static int queue_fs_event(int code) {
541 int ret = code;
542 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700543 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wang254f4432016-08-23 11:58:09 -0700544 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700545 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000546 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700547 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wang254f4432016-08-23 11:58:09 -0700548 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700549 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800550 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang254f4432016-08-23 11:58:09 -0700551 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800552 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700553 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang254f4432016-08-23 11:58:09 -0700554 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700555 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700556 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowleyaf8be582016-05-10 08:52:06 -0700557 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700558 /* If reboot worked, there is no return. */
Wei Wang254f4432016-08-23 11:58:09 -0700559 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000560 if (e4crypt_install_keyring()) {
561 return -1;
562 }
563 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000564 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000565
566 // Although encrypted, we have device key, so we do not need to
567 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700568 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang254f4432016-08-23 11:58:09 -0700569 } else if (code > 0) {
570 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700571 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700572 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700573
574 return ret;
575}
576
Wei Wang254f4432016-08-23 11:58:09 -0700577/* mount_all <fstab> [ <path> ]* [--<options>]*
578 *
579 * This function might request a reboot, in which case it will
580 * not return.
581 */
582static int do_mount_all(const std::vector<std::string>& args) {
583 std::size_t na = 0;
584 bool import_rc = true;
585 bool queue_event = true;
586 int mount_mode = MOUNT_MODE_DEFAULT;
587 const char* fstabfile = args[1].c_str();
588 std::size_t path_arg_end = args.size();
589
590 for (na = args.size() - 1; na > 1; --na) {
591 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800592 path_arg_end = na;
593 queue_event = false;
594 mount_mode = MOUNT_MODE_EARLY;
Wei Wang254f4432016-08-23 11:58:09 -0700595 } else if (args[na] == "--late") {
596 path_arg_end = na;
597 import_rc = false;
598 mount_mode = MOUNT_MODE_LATE;
599 }
600 }
601
602 int ret = mount_fstab(fstabfile, mount_mode);
603
604 if (import_rc) {
605 /* Paths of .rc files are specified at the 2nd argument and beyond */
606 import_late(args, 2, path_arg_end);
607 }
608
609 if (queue_event) {
610 /* queue_fs_event will queue event based on mount_fstab return code
611 * and return processed return code*/
612 ret = queue_fs_event(ret);
613 }
614
615 return ret;
616}
617
Tom Cherryb7349902015-08-26 11:43:36 -0700618static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700619 struct fstab *fstab;
620 int ret;
621
Tom Cherry96f67312015-07-30 13:52:55 -0700622 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700623 ret = fs_mgr_swapon_all(fstab);
624 fs_mgr_free_fstab(fstab);
625
626 return ret;
627}
628
Tom Cherryb7349902015-08-26 11:43:36 -0700629static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700630 const char* name = args[1].c_str();
631 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700632 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700633 return 0;
634}
635
Tom Cherryb7349902015-08-26 11:43:36 -0700636static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700637 struct rlimit limit;
638 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700639 if (android::base::ParseInt(args[1], &resource) &&
640 android::base::ParseUint(args[2], &limit.rlim_cur) &&
641 android::base::ParseUint(args[3], &limit.rlim_max)) {
642 return setrlimit(resource, &limit);
643 }
644 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
645 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700646}
647
Tom Cherryb7349902015-08-26 11:43:36 -0700648static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700649 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
650 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700651 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700652 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700653 }
Tom Cherrybac32992015-07-31 12:45:25 -0700654 if (!svc->Start())
655 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700656 return 0;
657}
658
Tom Cherryb7349902015-08-26 11:43:36 -0700659static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700660 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
661 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700662 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700663 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664 }
Tom Cherrybac32992015-07-31 12:45:25 -0700665 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700666 return 0;
667}
668
Tom Cherryb7349902015-08-26 11:43:36 -0700669static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700670 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
671 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700672 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700673 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700674 }
Tom Cherrybac32992015-07-31 12:45:25 -0700675 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700676 return 0;
677}
678
Tom Cherryb7349902015-08-26 11:43:36 -0700679static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700680 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700681 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700682 unsigned int cmd = 0;
683 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700684 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700685
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700686 if (strncmp(command, "shutdown", 8) == 0) {
687 cmd = ANDROID_RB_POWEROFF;
688 len = 8;
689 } else if (strncmp(command, "reboot", 6) == 0) {
690 cmd = ANDROID_RB_RESTART2;
691 len = 6;
692 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700693 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700694 return -EINVAL;
695 }
696
697 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700698 if (cmd == ANDROID_RB_POWEROFF &&
699 !strcmp(&command[len + 1], "userrequested")) {
700 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
701 // Run fsck once the file system is remounted in read-only mode.
702 callback_on_ro_remount = unmount_and_fsck;
703 } else if (cmd == ANDROID_RB_RESTART2) {
704 reboot_target = &command[len + 1];
Vineela Tummalapalli039734c2016-10-28 19:30:07 -0700705 // When rebooting to the bootloader notify the bootloader writing
706 // also the BCB.
707 if (strcmp(reboot_target, "bootloader") == 0) {
708 std::string err;
709 if (!write_reboot_bootloader(&err)) {
710 LOG(ERROR) << "reboot-bootloader: Error writing "
711 "bootloader_message: " << err;
712 }
713 }
Yusuke Satof93d4292015-07-21 15:50:59 -0700714 }
715 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700716 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700717 return -EINVAL;
718 }
719
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800720 std::string timeout = property_get("ro.build.shutdown_timeout");
721 unsigned int delay = 0;
722
Elliott Hughesda46b392016-10-11 17:09:00 -0700723 if (android::base::ParseUint(timeout, &delay) && delay > 0) {
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800724 Timer t;
725 // Ask all services to terminate.
726 ServiceManager::GetInstance().ForEachService(
727 [] (Service* s) { s->Terminate(); });
728
729 while (t.duration() < delay) {
730 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
731
732 int service_count = 0;
733 ServiceManager::GetInstance().ForEachService(
734 [&service_count] (Service* s) {
735 // Count the number of services running.
736 // Exclude the console as it will ignore the SIGTERM signal
737 // and not exit.
738 // Note: SVC_CONSOLE actually means "requires console" but
739 // it is only used by the shell.
740 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
741 service_count++;
742 }
743 });
744
745 if (service_count == 0) {
746 // All terminable services terminated. We can exit early.
747 break;
748 }
749
750 // Wait a bit before recounting the number or running services.
Elliott Hughes290a2282016-11-14 17:08:47 -0800751 std::this_thread::sleep_for(50ms);
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800752 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700753 LOG(VERBOSE) << "Terminating running services took " << t.duration() << " seconds";
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800754 }
755
Yusuke Sato0df08272015-07-08 14:57:07 -0700756 return android_reboot_with_callback(cmd, 0, reboot_target,
757 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700758}
759
Tom Cherryb7349902015-08-26 11:43:36 -0700760static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700761 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700762 return 0;
763}
764
Tom Cherryb7349902015-08-26 11:43:36 -0700765static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700766 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767}
768
Tom Cherryb7349902015-08-26 11:43:36 -0700769static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700770 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800771}
772
Tom Cherryb7349902015-08-26 11:43:36 -0700773static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700774 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800775}
776
Tom Cherryb7349902015-08-26 11:43:36 -0700777static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700778 struct timezone tz = {};
779 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
780 return 0;
781 }
782 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800783}
784
Tom Cherryb7349902015-08-26 11:43:36 -0700785static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700786 int mode = -1;
787 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000788 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700789 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000790 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700791 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000792}
793
Tom Cherryb7349902015-08-26 11:43:36 -0700794static void verity_update_property(fstab_rec *fstab, const char *mount_point,
795 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100796 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
797 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000798}
799
Tom Cherryb7349902015-08-26 11:43:36 -0700800static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700801 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000802}
803
Tom Cherryb7349902015-08-26 11:43:36 -0700804static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700805 const char* path = args[1].c_str();
806 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700807 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700808}
809
Tom Cherryb7349902015-08-26 11:43:36 -0700810static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700811 char *buffer = NULL;
812 int rc = 0;
813 int fd1 = -1, fd2 = -1;
814 struct stat info;
815 int brtw, brtr;
816 char *p;
817
Tom Cherry96f67312015-07-30 13:52:55 -0700818 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700819 return -1;
820
Tom Cherry96f67312015-07-30 13:52:55 -0700821 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700822 goto out_err;
823
Tom Cherry96f67312015-07-30 13:52:55 -0700824 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700825 goto out_err;
826
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800827 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700828 goto out_err;
829
830 p = buffer;
831 brtr = info.st_size;
832 while(brtr) {
833 rc = read(fd1, p, brtr);
834 if (rc < 0)
835 goto out_err;
836 if (rc == 0)
837 break;
838 p += rc;
839 brtr -= rc;
840 }
841
842 p = buffer;
843 brtw = info.st_size;
844 while(brtw) {
845 rc = write(fd2, p, brtw);
846 if (rc < 0)
847 goto out_err;
848 if (rc == 0)
849 break;
850 p += rc;
851 brtw -= rc;
852 }
853
854 rc = 0;
855 goto out;
856out_err:
857 rc = -1;
858out:
859 if (buffer)
860 free(buffer);
861 if (fd1 >= 0)
862 close(fd1);
863 if (fd2 >= 0)
864 close(fd2);
865 return rc;
866}
867
Tom Cherryb7349902015-08-26 11:43:36 -0700868static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700869 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700870 if (args.size() == 3) {
871 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700872 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700873 } else if (args.size() == 4) {
874 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
875 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876 return -errno;
877 } else {
878 return -1;
879 }
880 return 0;
881}
882
883static mode_t get_mode(const char *s) {
884 mode_t mode = 0;
885 while (*s) {
886 if (*s >= '0' && *s <= '7') {
887 mode = (mode<<3) | (*s-'0');
888 } else {
889 return -1;
890 }
891 s++;
892 }
893 return mode;
894}
895
Tom Cherryb7349902015-08-26 11:43:36 -0700896static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700897 mode_t mode = get_mode(args[1].c_str());
898 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700899 return -errno;
900 }
901 return 0;
902}
903
Tom Cherryb7349902015-08-26 11:43:36 -0700904static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400905 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500906
Tom Cherry96f67312015-07-30 13:52:55 -0700907 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
908 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400909 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500910 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400911 return ret;
912}
913
Tom Cherryb7349902015-08-26 11:43:36 -0700914static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400915 int ret = 0;
916
Tom Cherry96f67312015-07-30 13:52:55 -0700917 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
918 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400919 ret = -errno;
920 }
921 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500922}
923
Tom Cherryb7349902015-08-26 11:43:36 -0700924static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700925 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700926 int log_level = -1;
927 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700928 android::base::LogSeverity severity;
929 switch (log_level) {
930 case 7: severity = android::base::DEBUG; break;
931 case 6: severity = android::base::INFO; break;
932 case 5:
933 case 4: severity = android::base::WARNING; break;
934 case 3: severity = android::base::ERROR; break;
935 case 2:
936 case 1:
937 case 0: severity = android::base::FATAL; break;
938 default:
939 LOG(ERROR) << "loglevel: invalid log level " << log_level;
940 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700941 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700942 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700943 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700944}
945
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700946static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700947 load_persist_props();
948 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800949}
950
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700951static int do_load_system_props(const std::vector<std::string>& args) {
952 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700953 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700954}
955
Tom Cherryb7349902015-08-26 11:43:36 -0700956static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700957 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800958 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700959 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700960 int timeout;
961 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800962 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700963 }
964 }
965 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700966}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000967
Paul Lawrence806d10b2015-04-28 22:07:10 +0000968/*
969 * Callback to make a directory from the ext4 code
970 */
Tom Cherryb7349902015-08-26 11:43:36 -0700971static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000972 if (make_dir(dir, 0700) && errno != EEXIST) {
973 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000974 }
975
Paul Lawrence806d10b2015-04-28 22:07:10 +0000976 return 0;
977}
978
Paul Crowley749af8c2015-05-28 17:35:06 +0100979static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700980 std::string value = property_get("ro.crypto.type");
981 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100982}
983
Tom Cherryb7349902015-08-26 11:43:36 -0700984static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100985 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000986 return 0;
987 }
Tom Cherry96f67312015-07-30 13:52:55 -0700988 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000989 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000990}
Paul Crowley749af8c2015-05-28 17:35:06 +0100991
Paul Crowley59497452016-02-01 16:37:13 +0000992static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000993 return e4crypt_do_init_user0();
994}
995
Tom Cherryb7349902015-08-26 11:43:36 -0700996BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
997 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
998 static const Map builtin_functions = {
999 {"bootchart_init", {0, 0, do_bootchart_init}},
1000 {"chmod", {2, 2, do_chmod}},
1001 {"chown", {2, 3, do_chown}},
1002 {"class_reset", {1, 1, do_class_reset}},
1003 {"class_start", {1, 1, do_class_start}},
1004 {"class_stop", {1, 1, do_class_stop}},
1005 {"copy", {2, 2, do_copy}},
1006 {"domainname", {1, 1, do_domainname}},
1007 {"enable", {1, 1, do_enable}},
1008 {"exec", {1, kMax, do_exec}},
1009 {"export", {2, 2, do_export}},
1010 {"hostname", {1, 1, do_hostname}},
1011 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +00001012 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -07001013 {"insmod", {1, kMax, do_insmod}},
1014 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -07001015 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001016 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -07001017 {"loglevel", {1, 1, do_loglevel}},
1018 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +08001019 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -07001020 {"mount", {3, kMax, do_mount}},
1021 {"powerctl", {1, 1, do_powerctl}},
1022 {"restart", {1, 1, do_restart}},
1023 {"restorecon", {1, kMax, do_restorecon}},
1024 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
1025 {"rm", {1, 1, do_rm}},
1026 {"rmdir", {1, 1, do_rmdir}},
1027 {"setprop", {2, 2, do_setprop}},
1028 {"setrlimit", {3, 3, do_setrlimit}},
1029 {"start", {1, 1, do_start}},
1030 {"stop", {1, 1, do_stop}},
1031 {"swapon_all", {1, 1, do_swapon_all}},
1032 {"symlink", {2, 2, do_symlink}},
1033 {"sysclktz", {1, 1, do_sysclktz}},
1034 {"trigger", {1, 1, do_trigger}},
1035 {"verity_load_state", {0, 0, do_verity_load_state}},
1036 {"verity_update_state", {0, 0, do_verity_update_state}},
1037 {"wait", {1, 2, do_wait}},
1038 {"write", {2, 2, do_write}},
1039 };
1040 return builtin_functions;
1041}