blob: 812ac3c89b0c3e69d911241ecdc4fe9869f31f85 [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
Paul Lawrencea8d84342016-11-14 15:40:18 -080043#include <selinux/android.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044#include <selinux/selinux.h>
45#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050046
Elliott Hughesdb3f2672015-03-20 09:45:18 -070047#include <fs_mgr.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070048#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080049#include <android-base/parseint.h>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080050#include <android-base/strings.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080051#include <android-base/stringprintf.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070052#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070053#include <cutils/partition_utils.h>
54#include <cutils/android_reboot.h>
Tao Bao6d881d62016-10-05 17:53:30 -070055#include <ext4_utils/ext4_crypt.h>
56#include <ext4_utils/ext4_crypt_init_extensions.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070057#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070058
Tom Cherryfa0c21c2015-07-23 17:53:11 -070059#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070060#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070063#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070064#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070065#include "property_service.h"
66#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080067#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070068#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069
Nick Kralevichbc609542015-01-31 21:39:46 -080070#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070071#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080072
Elliott Hughes9605a942016-11-10 17:43:47 -080073static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080074
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080075static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070076 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
77 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070078 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080080 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080081 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070082 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070083 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070084 }
85 close(fd);
86 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087}
88
Tom Cherryb7349902015-08-26 11:43:36 -070089static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070090 struct ifreq ifr;
91 int s, ret;
92
93 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
94
95 s = socket(AF_INET, SOCK_DGRAM, 0);
96 if (s < 0)
97 return -1;
98
99 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
100 if (ret < 0) {
101 goto done;
102 }
103
104 if (up)
105 ifr.ifr_flags |= IFF_UP;
106 else
107 ifr.ifr_flags &= ~IFF_UP;
108
109 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800110
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111done:
112 close(s);
113 return ret;
114}
115
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700116// Turn off backlight while we are performing power down cleanup activities.
117static void turnOffBacklight() {
118 static const char off[] = "0";
119
120 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
121
122 static const char backlightDir[] = "/sys/class/backlight";
123 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
124 if (!dir) {
125 return;
126 }
127
128 struct dirent *dp;
129 while ((dp = readdir(dir.get())) != NULL) {
130 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
131 (dp->d_name[0] == '.')) {
132 continue;
133 }
134
135 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
136 backlightDir,
137 dp->d_name);
138 android::base::WriteStringToFile(off, fileName);
139 }
140}
141
Paul Crowleyaf8be582016-05-10 08:52:06 -0700142static int wipe_data_via_recovery(const std::string& reason) {
143 const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
144 std::string err;
145 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700146 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700147 return -1;
148 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000149 reboot("recovery");
Paul Crowleyaf8be582016-05-10 08:52:06 -0700150}
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
Alex Light68ab20f2016-06-23 11:11:39 -0700348/* umount <path> */
349static int do_umount(const std::vector<std::string>& args) {
350 return umount(args[1].c_str());
351}
352
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353static struct {
354 const char *name;
355 unsigned flag;
356} mount_flags[] = {
357 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200358 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 { "nosuid", MS_NOSUID },
360 { "nodev", MS_NODEV },
361 { "nodiratime", MS_NODIRATIME },
362 { "ro", MS_RDONLY },
363 { "rw", 0 },
364 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700365 { "bind", MS_BIND },
366 { "rec", MS_REC },
367 { "unbindable", MS_UNBINDABLE },
368 { "private", MS_PRIVATE },
369 { "slave", MS_SLAVE },
370 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 { "defaults", 0 },
372 { 0, 0 },
373};
374
Ken Sumrall752923c2010-12-03 16:33:31 -0800375#define DATA_MNT_POINT "/data"
376
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700378static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700380 const char *source, *target, *system;
381 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700383 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700385 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386
Tom Cherry96f67312015-07-30 13:52:55 -0700387 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700389 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390 flags |= mount_flags[i].flag;
391 break;
392 }
393 }
394
Colin Crosscd0f1732010-04-19 17:10:24 -0700395 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700396 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700397 wait = 1;
398 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700399 else if (na + 1 == args.size())
400 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700401 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 }
403
Tom Cherry96f67312015-07-30 13:52:55 -0700404 system = args[1].c_str();
405 source = args[2].c_str();
406 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000407
Elliott Hughes31951162016-06-24 15:15:03 -0700408 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000409 int mode, loop, fd;
410 struct loop_info info;
411
412 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800413 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000414 if (fd < 0) {
415 return -1;
416 }
417
418 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800419 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800420 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000421 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100422 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000423 return -1;
424 }
425
426 /* if it is a blank loop device */
427 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
428 /* if it becomes our loop device */
429 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
430 close(fd);
431
432 if (mount(tmp, target, system, flags, options) < 0) {
433 ioctl(loop, LOOP_CLR_FD, 0);
434 close(loop);
435 return -1;
436 }
437
438 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800439 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000440 }
441 }
442
443 close(loop);
444 }
445
446 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700447 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000448 return -1;
449 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700450 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800451 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000452 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700453 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000454 }
455
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700456 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800457
458exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800459 return 0;
460
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700461}
462
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800463/* Imports .rc files from the specified paths. Default ones are applied if none is given.
464 *
465 * start_index: index of the first path in the args list
466 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700467static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700468 Parser& parser = Parser::GetInstance();
Wei Wangd61a7e22016-08-23 11:58:09 -0700469 if (end_index <= start_index) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800470 // Use the default set if no path is given
471 static const std::vector<std::string> init_directories = {
472 "/system/etc/init",
473 "/vendor/etc/init",
474 "/odm/etc/init"
475 };
476
477 for (const auto& dir : init_directories) {
478 parser.ParseConfig(dir);
479 }
480 } else {
Wei Wangd61a7e22016-08-23 11:58:09 -0700481 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800482 parser.ParseConfig(args[i]);
483 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700484 }
485}
486
Wei Wangd61a7e22016-08-23 11:58:09 -0700487/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800488 *
Wei Wangd61a7e22016-08-23 11:58:09 -0700489 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700490 */
Wei Wangd61a7e22016-08-23 11:58:09 -0700491static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700492 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700493
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700494 /*
495 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
496 * do the call in the child to provide protection to the main init
497 * process if anything goes wrong (crash or memory leak), and wait for
498 * the child to finish in the parent.
499 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700500 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501 if (pid > 0) {
502 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700503 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700504 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700505 if (wp_ret == -1) {
506 // Unexpected error code. We will continue anyway.
507 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700508 }
509
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700510 if (WIFEXITED(status)) {
511 ret = WEXITSTATUS(status);
512 } else {
513 ret = -1;
514 }
515 } else if (pid == 0) {
516 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700517
518 // So we can always see what fs_mgr_mount_all() does.
519 // Only needed if someone explicitly changes the default log level in their init.rc.
520 android::base::ScopedLogSeverity info(android::base::INFO);
521
522 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wangd61a7e22016-08-23 11:58:09 -0700523 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800524 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700525 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700526 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700527 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700528 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700529 } else {
530 /* fork failed, return an error */
531 return -1;
532 }
Wei Wangd61a7e22016-08-23 11:58:09 -0700533 return ret;
534}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700535
Wei Wangd61a7e22016-08-23 11:58:09 -0700536/* Queue event based on fs_mgr return code.
537 *
538 * code: return code of fs_mgr_mount_all
539 *
540 * This function might request a reboot, in which case it will
541 * not return.
542 *
543 * return code is processed based on input code
544 */
545static int queue_fs_event(int code) {
546 int ret = code;
547 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700548 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wangd61a7e22016-08-23 11:58:09 -0700549 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700550 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000551 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700552 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wangd61a7e22016-08-23 11:58:09 -0700553 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700554 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800555 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700556 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800557 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700558 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700559 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700560 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700561 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Tao Baofdf1a542016-12-14 12:07:53 -0800562 ret = wipe_data_via_recovery("fs_mgr_mount_all");
JP Abgrallcee20682014-07-02 14:26:54 -0700563 /* If reboot worked, there is no return. */
Wei Wangd61a7e22016-08-23 11:58:09 -0700564 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000565 if (e4crypt_install_keyring()) {
566 return -1;
567 }
568 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000569 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000570
571 // Although encrypted, we have device key, so we do not need to
572 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700573 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wangd61a7e22016-08-23 11:58:09 -0700574 } else if (code > 0) {
575 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700576 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700577 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700578
579 return ret;
580}
581
Wei Wangd61a7e22016-08-23 11:58:09 -0700582/* mount_all <fstab> [ <path> ]* [--<options>]*
583 *
584 * This function might request a reboot, in which case it will
585 * not return.
586 */
587static int do_mount_all(const std::vector<std::string>& args) {
588 std::size_t na = 0;
589 bool import_rc = true;
590 bool queue_event = true;
591 int mount_mode = MOUNT_MODE_DEFAULT;
592 const char* fstabfile = args[1].c_str();
593 std::size_t path_arg_end = args.size();
594
595 for (na = args.size() - 1; na > 1; --na) {
596 if (args[na] == "--early") {
Wei Wangd67a4ab2016-11-16 12:08:30 -0800597 path_arg_end = na;
598 queue_event = false;
599 mount_mode = MOUNT_MODE_EARLY;
Wei Wangd61a7e22016-08-23 11:58:09 -0700600 } else if (args[na] == "--late") {
601 path_arg_end = na;
602 import_rc = false;
603 mount_mode = MOUNT_MODE_LATE;
604 }
605 }
606
607 int ret = mount_fstab(fstabfile, mount_mode);
608
609 if (import_rc) {
610 /* Paths of .rc files are specified at the 2nd argument and beyond */
611 import_late(args, 2, path_arg_end);
612 }
613
614 if (queue_event) {
615 /* queue_fs_event will queue event based on mount_fstab return code
616 * and return processed return code*/
617 ret = queue_fs_event(ret);
618 }
619
620 return ret;
621}
622
Tom Cherryb7349902015-08-26 11:43:36 -0700623static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700624 struct fstab *fstab;
625 int ret;
626
Tom Cherry96f67312015-07-30 13:52:55 -0700627 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700628 ret = fs_mgr_swapon_all(fstab);
629 fs_mgr_free_fstab(fstab);
630
631 return ret;
632}
633
Tom Cherryb7349902015-08-26 11:43:36 -0700634static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700635 const char* name = args[1].c_str();
636 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700637 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700638 return 0;
639}
640
Tom Cherryb7349902015-08-26 11:43:36 -0700641static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700642 struct rlimit limit;
643 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700644 if (android::base::ParseInt(args[1], &resource) &&
645 android::base::ParseUint(args[2], &limit.rlim_cur) &&
646 android::base::ParseUint(args[3], &limit.rlim_max)) {
647 return setrlimit(resource, &limit);
648 }
649 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
650 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700651}
652
Tom Cherryb7349902015-08-26 11:43:36 -0700653static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700654 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
655 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700656 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700657 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700658 }
Tom Cherrybac32992015-07-31 12:45:25 -0700659 if (!svc->Start())
660 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661 return 0;
662}
663
Tom Cherryb7349902015-08-26 11:43:36 -0700664static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700665 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
666 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700667 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700668 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700669 }
Tom Cherrybac32992015-07-31 12:45:25 -0700670 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700671 return 0;
672}
673
Tom Cherryb7349902015-08-26 11:43:36 -0700674static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700675 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
676 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700677 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700678 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700679 }
Tom Cherrybac32992015-07-31 12:45:25 -0700680 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700681 return 0;
682}
683
Tom Cherryb7349902015-08-26 11:43:36 -0700684static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700685 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700686 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700687 unsigned int cmd = 0;
688 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700689 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700690
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700691 if (strncmp(command, "shutdown", 8) == 0) {
692 cmd = ANDROID_RB_POWEROFF;
693 len = 8;
694 } else if (strncmp(command, "reboot", 6) == 0) {
695 cmd = ANDROID_RB_RESTART2;
696 len = 6;
697 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700698 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700699 return -EINVAL;
700 }
701
702 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700703 if (cmd == ANDROID_RB_POWEROFF &&
704 !strcmp(&command[len + 1], "userrequested")) {
705 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
706 // Run fsck once the file system is remounted in read-only mode.
707 callback_on_ro_remount = unmount_and_fsck;
708 } else if (cmd == ANDROID_RB_RESTART2) {
709 reboot_target = &command[len + 1];
Vineela Tummalapalli039734c2016-10-28 19:30:07 -0700710 // When rebooting to the bootloader notify the bootloader writing
711 // also the BCB.
712 if (strcmp(reboot_target, "bootloader") == 0) {
713 std::string err;
714 if (!write_reboot_bootloader(&err)) {
715 LOG(ERROR) << "reboot-bootloader: Error writing "
716 "bootloader_message: " << err;
717 }
718 }
Yusuke Satof93d4292015-07-21 15:50:59 -0700719 }
720 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700721 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700722 return -EINVAL;
723 }
724
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800725 std::string timeout = property_get("ro.build.shutdown_timeout");
726 unsigned int delay = 0;
727
Elliott Hughesda46b392016-10-11 17:09:00 -0700728 if (android::base::ParseUint(timeout, &delay) && delay > 0) {
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800729 Timer t;
730 // Ask all services to terminate.
731 ServiceManager::GetInstance().ForEachService(
732 [] (Service* s) { s->Terminate(); });
733
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000734 while (t.duration_s() < delay) {
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800735 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
736
737 int service_count = 0;
738 ServiceManager::GetInstance().ForEachService(
739 [&service_count] (Service* s) {
740 // Count the number of services running.
741 // Exclude the console as it will ignore the SIGTERM signal
742 // and not exit.
743 // Note: SVC_CONSOLE actually means "requires console" but
744 // it is only used by the shell.
745 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
746 service_count++;
747 }
748 });
749
750 if (service_count == 0) {
751 // All terminable services terminated. We can exit early.
752 break;
753 }
754
755 // Wait a bit before recounting the number or running services.
Elliott Hughes290a2282016-11-14 17:08:47 -0800756 std::this_thread::sleep_for(50ms);
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800757 }
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000758 LOG(VERBOSE) << "Terminating running services took " << t;
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800759 }
760
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000761 return android_reboot_with_callback(cmd, 0, reboot_target, callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700762}
763
Tom Cherryb7349902015-08-26 11:43:36 -0700764static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700765 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766 return 0;
767}
768
Tom Cherryb7349902015-08-26 11:43:36 -0700769static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700770 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700771}
772
Tom Cherryb7349902015-08-26 11:43:36 -0700773static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700774 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800775}
776
Tom Cherryb7349902015-08-26 11:43:36 -0700777static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700778 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800779}
780
Tom Cherryb7349902015-08-26 11:43:36 -0700781static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700782 struct timezone tz = {};
783 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
784 return 0;
785 }
786 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800787}
788
Tom Cherryb7349902015-08-26 11:43:36 -0700789static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700790 int mode = -1;
791 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000792 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700793 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000794 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700795 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000796}
797
Tom Cherryb7349902015-08-26 11:43:36 -0700798static void verity_update_property(fstab_rec *fstab, const char *mount_point,
799 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100800 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
801 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000802}
803
Tom Cherryb7349902015-08-26 11:43:36 -0700804static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700805 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000806}
807
Tom Cherryb7349902015-08-26 11:43:36 -0700808static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700809 const char* path = args[1].c_str();
810 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700811 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700812}
813
Tom Cherryb7349902015-08-26 11:43:36 -0700814static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700815 char *buffer = NULL;
816 int rc = 0;
817 int fd1 = -1, fd2 = -1;
818 struct stat info;
819 int brtw, brtr;
820 char *p;
821
Tom Cherry96f67312015-07-30 13:52:55 -0700822 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700823 return -1;
824
Tom Cherry96f67312015-07-30 13:52:55 -0700825 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700826 goto out_err;
827
Tom Cherry96f67312015-07-30 13:52:55 -0700828 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700829 goto out_err;
830
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800831 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700832 goto out_err;
833
834 p = buffer;
835 brtr = info.st_size;
836 while(brtr) {
837 rc = read(fd1, p, brtr);
838 if (rc < 0)
839 goto out_err;
840 if (rc == 0)
841 break;
842 p += rc;
843 brtr -= rc;
844 }
845
846 p = buffer;
847 brtw = info.st_size;
848 while(brtw) {
849 rc = write(fd2, p, brtw);
850 if (rc < 0)
851 goto out_err;
852 if (rc == 0)
853 break;
854 p += rc;
855 brtw -= rc;
856 }
857
858 rc = 0;
859 goto out;
860out_err:
861 rc = -1;
862out:
863 if (buffer)
864 free(buffer);
865 if (fd1 >= 0)
866 close(fd1);
867 if (fd2 >= 0)
868 close(fd2);
869 return rc;
870}
871
Tom Cherryb7349902015-08-26 11:43:36 -0700872static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700874 if (args.size() == 3) {
875 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700877 } else if (args.size() == 4) {
878 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
879 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880 return -errno;
881 } else {
882 return -1;
883 }
884 return 0;
885}
886
887static mode_t get_mode(const char *s) {
888 mode_t mode = 0;
889 while (*s) {
890 if (*s >= '0' && *s <= '7') {
891 mode = (mode<<3) | (*s-'0');
892 } else {
893 return -1;
894 }
895 s++;
896 }
897 return mode;
898}
899
Tom Cherryb7349902015-08-26 11:43:36 -0700900static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700901 mode_t mode = get_mode(args[1].c_str());
902 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700903 return -errno;
904 }
905 return 0;
906}
907
Tom Cherryb7349902015-08-26 11:43:36 -0700908static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400909 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500910
Paul Lawrencea8d84342016-11-14 15:40:18 -0800911 struct flag_type {const char* name; int value;};
912 static const flag_type flags[] = {
913 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
914 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
915 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
916 {0, 0}
917 };
918
919 int flag = 0;
920
921 bool in_flags = true;
922 for (size_t i = 1; i < args.size(); ++i) {
923 if (android::base::StartsWith(args[i], "--")) {
924 if (!in_flags) {
925 LOG(ERROR) << "restorecon - flags must precede paths";
926 return -1;
927 }
928 bool found = false;
929 for (size_t j = 0; flags[j].name; ++j) {
930 if (args[i] == flags[j].name) {
931 flag |= flags[j].value;
932 found = true;
933 break;
934 }
935 }
936 if (!found) {
937 LOG(ERROR) << "restorecon - bad flag " << args[i];
938 return -1;
939 }
940 } else {
941 in_flags = false;
942 if (restorecon(args[i].c_str(), flag) < 0) {
943 ret = -errno;
944 }
945 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500946 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400947 return ret;
948}
949
Tom Cherryb7349902015-08-26 11:43:36 -0700950static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800951 std::vector<std::string> non_const_args(args);
952 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
953 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500954}
955
Tom Cherryb7349902015-08-26 11:43:36 -0700956static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700957 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700958 int log_level = -1;
959 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700960 android::base::LogSeverity severity;
961 switch (log_level) {
962 case 7: severity = android::base::DEBUG; break;
963 case 6: severity = android::base::INFO; break;
964 case 5:
965 case 4: severity = android::base::WARNING; break;
966 case 3: severity = android::base::ERROR; break;
967 case 2:
968 case 1:
969 case 0: severity = android::base::FATAL; break;
970 default:
971 LOG(ERROR) << "loglevel: invalid log level " << log_level;
972 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700973 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700974 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700975 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700976}
977
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700978static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700979 load_persist_props();
980 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800981}
982
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700983static int do_load_system_props(const std::vector<std::string>& args) {
984 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700985 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700986}
987
Tom Cherryb7349902015-08-26 11:43:36 -0700988static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700989 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800990 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700991 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700992 int timeout;
993 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800994 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700995 }
996 }
997 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700998}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000999
Paul Lawrence806d10b2015-04-28 22:07:10 +00001000/*
1001 * Callback to make a directory from the ext4 code
1002 */
Tom Cherryb7349902015-08-26 11:43:36 -07001003static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +00001004 if (make_dir(dir, 0700) && errno != EEXIST) {
1005 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001006 }
1007
Paul Lawrence806d10b2015-04-28 22:07:10 +00001008 return 0;
1009}
1010
Paul Crowley749af8c2015-05-28 17:35:06 +01001011static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -07001012 std::string value = property_get("ro.crypto.type");
1013 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +01001014}
1015
Tom Cherryb7349902015-08-26 11:43:36 -07001016static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +01001017 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +00001018 return 0;
1019 }
Tom Cherry96f67312015-07-30 13:52:55 -07001020 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +00001021 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001022}
Paul Crowley749af8c2015-05-28 17:35:06 +01001023
Paul Crowley59497452016-02-01 16:37:13 +00001024static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +00001025 return e4crypt_do_init_user0();
1026}
1027
Tom Cherryb7349902015-08-26 11:43:36 -07001028BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
1029 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
1030 static const Map builtin_functions = {
Elliott Hughesa3641af2016-11-10 17:43:47 -08001031 {"bootchart", {1, 1, do_bootchart}},
Tom Cherryb7349902015-08-26 11:43:36 -07001032 {"chmod", {2, 2, do_chmod}},
1033 {"chown", {2, 3, do_chown}},
1034 {"class_reset", {1, 1, do_class_reset}},
1035 {"class_start", {1, 1, do_class_start}},
1036 {"class_stop", {1, 1, do_class_stop}},
1037 {"copy", {2, 2, do_copy}},
1038 {"domainname", {1, 1, do_domainname}},
1039 {"enable", {1, 1, do_enable}},
1040 {"exec", {1, kMax, do_exec}},
1041 {"export", {2, 2, do_export}},
1042 {"hostname", {1, 1, do_hostname}},
1043 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +00001044 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -07001045 {"insmod", {1, kMax, do_insmod}},
1046 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -07001047 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001048 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -07001049 {"loglevel", {1, 1, do_loglevel}},
1050 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +08001051 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -07001052 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -07001053 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -07001054 {"powerctl", {1, 1, do_powerctl}},
1055 {"restart", {1, 1, do_restart}},
1056 {"restorecon", {1, kMax, do_restorecon}},
1057 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
1058 {"rm", {1, 1, do_rm}},
1059 {"rmdir", {1, 1, do_rmdir}},
1060 {"setprop", {2, 2, do_setprop}},
1061 {"setrlimit", {3, 3, do_setrlimit}},
1062 {"start", {1, 1, do_start}},
1063 {"stop", {1, 1, do_stop}},
1064 {"swapon_all", {1, 1, do_swapon_all}},
1065 {"symlink", {2, 2, do_symlink}},
1066 {"sysclktz", {1, 1, do_sysclktz}},
1067 {"trigger", {1, 1, do_trigger}},
1068 {"verity_load_state", {0, 0, do_verity_load_state}},
1069 {"verity_update_state", {0, 0, do_verity_update_state}},
1070 {"wait", {1, 2, do_wait}},
1071 {"write", {2, 2, do_write}},
1072 };
1073 return builtin_functions;
1074}