blob: 20d4d3ab84687637a3bfc17e2b6d2dd3ce61d0c3 [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 Cui1051e102016-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 }
149 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
150 while (1) { pause(); } // never reached
151}
152
Tom Cherryb7349902015-08-26 11:43:36 -0700153static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700154 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
155 return;
156
157 /* First, lazily unmount the directory. This unmount request finishes when
158 * all processes that open a file or directory in |entry->mnt_dir| exit.
159 */
160 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
161
162 /* Next, kill all processes except init, kthreadd, and kthreadd's
163 * children to finish the lazy unmount. Killing all processes here is okay
164 * because this callback function is only called right before reboot().
165 * It might be cleaner to selectively kill processes that actually use
166 * |entry->mnt_dir| rather than killing all, probably by reusing a function
167 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
168 * not allow init to scan /proc/<pid> files which the utility function
169 * heavily relies on. The policy does not allow the process to execute
170 * killall/pkill binaries either. Note that some processes might
171 * automatically restart after kill(), but that is not really a problem
172 * because |entry->mnt_dir| is no longer visible to such new processes.
173 */
Tom Cherrybac32992015-07-31 12:45:25 -0700174 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700175 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
176
Mark Salyzynad575e02016-04-05 08:10:25 -0700177 // Restart Watchdogd to allow us to complete umounting and fsck
178 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
179 if (svc) {
180 do {
181 sched_yield(); // do not be so eager, let cleanup have priority
182 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
183 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
184 svc->Start();
185 }
186
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700187 turnOffBacklight();
188
Yusuke Sato0df08272015-07-08 14:57:07 -0700189 int count = 0;
190 while (count++ < UNMOUNT_CHECK_TIMES) {
191 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
192 if (fd >= 0) {
193 /* |entry->mnt_dir| has sucessfully been unmounted. */
194 close(fd);
195 break;
196 } else if (errno == EBUSY) {
Elliott Hughes290a2282016-11-14 17:08:47 -0800197 // Some processes using |entry->mnt_dir| are still alive. Wait for a
198 // while then retry.
199 std::this_thread::sleep_for(5000ms / UNMOUNT_CHECK_TIMES);
Yusuke Sato0df08272015-07-08 14:57:07 -0700200 continue;
201 } else {
202 /* Cannot open the device. Give up. */
203 return;
204 }
205 }
206
Mark Salyzynad575e02016-04-05 08:10:25 -0700207 // NB: With watchdog still running, there is no cap on the time it takes
208 // to complete the fsck, from the users perspective the device graphics
209 // and responses are locked-up and they may choose to hold the power
210 // button in frustration if it drags out.
211
Yusuke Sato0df08272015-07-08 14:57:07 -0700212 int st;
213 if (!strcmp(entry->mnt_type, "f2fs")) {
214 const char *f2fs_argv[] = {
215 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
216 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700217 android_fork_execvp_ext(arraysize(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700218 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700219 } else if (!strcmp(entry->mnt_type, "ext4")) {
220 const char *ext4_argv[] = {
221 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
222 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700223 android_fork_execvp_ext(arraysize(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700224 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700225 }
226}
227
Tom Cherryb7349902015-08-26 11:43:36 -0700228static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229 /* Starting a class does not start services
230 * which are explicitly disabled. They must
231 * be started individually.
232 */
Tom Cherrybac32992015-07-31 12:45:25 -0700233 ServiceManager::GetInstance().
234 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235 return 0;
236}
237
Tom Cherryb7349902015-08-26 11:43:36 -0700238static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700239 ServiceManager::GetInstance().
240 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241 return 0;
242}
243
Tom Cherryb7349902015-08-26 11:43:36 -0700244static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700245 ServiceManager::GetInstance().
246 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800247 return 0;
248}
249
Tom Cherryb7349902015-08-26 11:43:36 -0700250static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700251 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252}
253
Tom Cherryb7349902015-08-26 11:43:36 -0700254static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700255 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
256 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700257 return -1;
258 }
Tom Cherrybac32992015-07-31 12:45:25 -0700259 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700260}
261
Tom Cherryb7349902015-08-26 11:43:36 -0700262static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700263 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
264 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800265 return -1;
266 }
Tom Cherrybac32992015-07-31 12:45:25 -0700267 if (!svc->Start()) {
268 return -1;
269 }
270 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800271 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272}
273
Tom Cherryb7349902015-08-26 11:43:36 -0700274static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700275 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276}
277
Tom Cherryb7349902015-08-26 11:43:36 -0700278static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700279 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280}
281
Tom Cherryb7349902015-08-26 11:43:36 -0700282static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700283 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284}
285
Tom Cherryb7349902015-08-26 11:43:36 -0700286static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800287 int flags = 0;
288 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800289
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800290 if (!(*it).compare("-f")) {
291 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
292 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800293 }
294
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800295 std::string filename = *it++;
296 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
297 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800298}
299
Tom Cherryb7349902015-08-26 11:43:36 -0700300static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700302 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303
304 /* mkdir <path> [mode] [owner] [group] */
305
Tom Cherry96f67312015-07-30 13:52:55 -0700306 if (args.size() >= 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700307 mode = std::strtoul(args[2].c_str(), 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 }
309
Tom Cherry96f67312015-07-30 13:52:55 -0700310 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700311 /* chmod in case the directory already exists */
312 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700313 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700314 }
315 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 return -errno;
317 }
318
Tom Cherry96f67312015-07-30 13:52:55 -0700319 if (args.size() >= 4) {
320 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 gid_t gid = -1;
322
Tom Cherry96f67312015-07-30 13:52:55 -0700323 if (args.size() == 5) {
324 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 }
326
Tom Cherry96f67312015-07-30 13:52:55 -0700327 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328 return -errno;
329 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700330
331 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
332 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700333 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700334 if (ret == -1) {
335 return -errno;
336 }
337 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338 }
339
Paul Crowleyaf8be582016-05-10 08:52:06 -0700340 if (e4crypt_is_native()) {
341 if (e4crypt_set_directory_policy(args[1].c_str())) {
342 wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
343 return -1;
344 }
345 }
346 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347}
348
349static struct {
350 const char *name;
351 unsigned flag;
352} mount_flags[] = {
353 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200354 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355 { "nosuid", MS_NOSUID },
356 { "nodev", MS_NODEV },
357 { "nodiratime", MS_NODIRATIME },
358 { "ro", MS_RDONLY },
359 { "rw", 0 },
360 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700361 { "bind", MS_BIND },
362 { "rec", MS_REC },
363 { "unbindable", MS_UNBINDABLE },
364 { "private", MS_PRIVATE },
365 { "slave", MS_SLAVE },
366 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367 { "defaults", 0 },
368 { 0, 0 },
369};
370
Ken Sumrall752923c2010-12-03 16:33:31 -0800371#define DATA_MNT_POINT "/data"
372
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700374static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700375 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700376 const char *source, *target, *system;
377 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700379 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700381 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382
Tom Cherry96f67312015-07-30 13:52:55 -0700383 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700385 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386 flags |= mount_flags[i].flag;
387 break;
388 }
389 }
390
Colin Crosscd0f1732010-04-19 17:10:24 -0700391 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700392 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700393 wait = 1;
394 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700395 else if (na + 1 == args.size())
396 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700397 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700398 }
399
Tom Cherry96f67312015-07-30 13:52:55 -0700400 system = args[1].c_str();
401 source = args[2].c_str();
402 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000403
Elliott Hughes31951162016-06-24 15:15:03 -0700404 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000405 int mode, loop, fd;
406 struct loop_info info;
407
408 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800409 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410 if (fd < 0) {
411 return -1;
412 }
413
414 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800415 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800416 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000417 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100418 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000419 return -1;
420 }
421
422 /* if it is a blank loop device */
423 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
424 /* if it becomes our loop device */
425 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
426 close(fd);
427
428 if (mount(tmp, target, system, flags, options) < 0) {
429 ioctl(loop, LOOP_CLR_FD, 0);
430 close(loop);
431 return -1;
432 }
433
434 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800435 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000436 }
437 }
438
439 close(loop);
440 }
441
442 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700443 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000444 return -1;
445 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700446 if (wait)
Elliott Hughes9605a942016-11-10 17:43:47 -0800447 wait_for_file(source, kCommandRetryTimeout);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000448 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700449 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000450 }
451
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700452 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800453
454exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800455 return 0;
456
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457}
458
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800459/* Imports .rc files from the specified paths. Default ones are applied if none is given.
460 *
461 * start_index: index of the first path in the args list
462 */
Wei Wang254f4432016-08-23 11:58:09 -0700463static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700464 Parser& parser = Parser::GetInstance();
Wei Wang254f4432016-08-23 11:58:09 -0700465 if (end_index <= start_index) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800466 // Use the default set if no path is given
467 static const std::vector<std::string> init_directories = {
468 "/system/etc/init",
469 "/vendor/etc/init",
470 "/odm/etc/init"
471 };
472
473 for (const auto& dir : init_directories) {
474 parser.ParseConfig(dir);
475 }
476 } else {
Wei Wang254f4432016-08-23 11:58:09 -0700477 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800478 parser.ParseConfig(args[i]);
479 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700480 }
481}
482
Wei Wang254f4432016-08-23 11:58:09 -0700483/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800484 *
Wei Wang254f4432016-08-23 11:58:09 -0700485 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700486 */
Wei Wang254f4432016-08-23 11:58:09 -0700487static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700488 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700489
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700490 /*
491 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
492 * do the call in the child to provide protection to the main init
493 * process if anything goes wrong (crash or memory leak), and wait for
494 * the child to finish in the parent.
495 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700496 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700497 if (pid > 0) {
498 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700499 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700500 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700501 if (wp_ret == -1) {
502 // Unexpected error code. We will continue anyway.
503 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700504 }
505
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700506 if (WIFEXITED(status)) {
507 ret = WEXITSTATUS(status);
508 } else {
509 ret = -1;
510 }
511 } else if (pid == 0) {
512 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700513
514 // So we can always see what fs_mgr_mount_all() does.
515 // Only needed if someone explicitly changes the default log level in their init.rc.
516 android::base::ScopedLogSeverity info(android::base::INFO);
517
518 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
Wei Wang254f4432016-08-23 11:58:09 -0700519 int child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800520 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700522 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700523 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700524 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700525 } else {
526 /* fork failed, return an error */
527 return -1;
528 }
Wei Wang254f4432016-08-23 11:58:09 -0700529 return ret;
530}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700531
Wei Wang254f4432016-08-23 11:58:09 -0700532/* Queue event based on fs_mgr return code.
533 *
534 * code: return code of fs_mgr_mount_all
535 *
536 * This function might request a reboot, in which case it will
537 * not return.
538 *
539 * return code is processed based on input code
540 */
541static int queue_fs_event(int code) {
542 int ret = code;
543 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700544 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wang254f4432016-08-23 11:58:09 -0700545 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700546 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000547 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700548 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wang254f4432016-08-23 11:58:09 -0700549 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700550 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800551 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang254f4432016-08-23 11:58:09 -0700552 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800553 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700554 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang254f4432016-08-23 11:58:09 -0700555 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700556 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700557 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowleyaf8be582016-05-10 08:52:06 -0700558 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700559 /* If reboot worked, there is no return. */
Wei Wang254f4432016-08-23 11:58:09 -0700560 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000561 if (e4crypt_install_keyring()) {
562 return -1;
563 }
564 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000565 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000566
567 // Although encrypted, we have device key, so we do not need to
568 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700569 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang254f4432016-08-23 11:58:09 -0700570 } else if (code > 0) {
571 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700572 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700573 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700574
575 return ret;
576}
577
Wei Wang254f4432016-08-23 11:58:09 -0700578/* mount_all <fstab> [ <path> ]* [--<options>]*
579 *
580 * This function might request a reboot, in which case it will
581 * not return.
582 */
583static int do_mount_all(const std::vector<std::string>& args) {
584 std::size_t na = 0;
585 bool import_rc = true;
586 bool queue_event = true;
587 int mount_mode = MOUNT_MODE_DEFAULT;
588 const char* fstabfile = args[1].c_str();
589 std::size_t path_arg_end = args.size();
590
591 for (na = args.size() - 1; na > 1; --na) {
592 if (args[na] == "--early") {
593 path_arg_end = na;
594 queue_event = false;
595 mount_mode = MOUNT_MODE_EARLY;
596 } else if (args[na] == "--late") {
597 path_arg_end = na;
598 import_rc = false;
599 mount_mode = MOUNT_MODE_LATE;
600 }
601 }
602
603 int ret = mount_fstab(fstabfile, mount_mode);
604
605 if (import_rc) {
606 /* Paths of .rc files are specified at the 2nd argument and beyond */
607 import_late(args, 2, path_arg_end);
608 }
609
610 if (queue_event) {
611 /* queue_fs_event will queue event based on mount_fstab return code
612 * and return processed return code*/
613 ret = queue_fs_event(ret);
614 }
615
616 return ret;
617}
618
Tom Cherryb7349902015-08-26 11:43:36 -0700619static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700620 struct fstab *fstab;
621 int ret;
622
Tom Cherry96f67312015-07-30 13:52:55 -0700623 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700624 ret = fs_mgr_swapon_all(fstab);
625 fs_mgr_free_fstab(fstab);
626
627 return ret;
628}
629
Tom Cherryb7349902015-08-26 11:43:36 -0700630static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700631 const char* name = args[1].c_str();
632 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700633 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700634 return 0;
635}
636
Tom Cherryb7349902015-08-26 11:43:36 -0700637static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700638 struct rlimit limit;
639 int resource;
Elliott Hughesda46b392016-10-11 17:09:00 -0700640 if (android::base::ParseInt(args[1], &resource) &&
641 android::base::ParseUint(args[2], &limit.rlim_cur) &&
642 android::base::ParseUint(args[3], &limit.rlim_max)) {
643 return setrlimit(resource, &limit);
644 }
645 LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3];
646 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700647}
648
Tom Cherryb7349902015-08-26 11:43:36 -0700649static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700650 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
651 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700652 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700653 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700654 }
Tom Cherrybac32992015-07-31 12:45:25 -0700655 if (!svc->Start())
656 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700657 return 0;
658}
659
Tom Cherryb7349902015-08-26 11:43:36 -0700660static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700661 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
662 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700663 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700664 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700665 }
Tom Cherrybac32992015-07-31 12:45:25 -0700666 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700667 return 0;
668}
669
Tom Cherryb7349902015-08-26 11:43:36 -0700670static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700671 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
672 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700673 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700674 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700675 }
Tom Cherrybac32992015-07-31 12:45:25 -0700676 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700677 return 0;
678}
679
Tom Cherryb7349902015-08-26 11:43:36 -0700680static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700681 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700682 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700683 unsigned int cmd = 0;
684 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700685 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700686
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700687 if (strncmp(command, "shutdown", 8) == 0) {
688 cmd = ANDROID_RB_POWEROFF;
689 len = 8;
690 } else if (strncmp(command, "reboot", 6) == 0) {
691 cmd = ANDROID_RB_RESTART2;
692 len = 6;
693 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700694 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700695 return -EINVAL;
696 }
697
698 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700699 if (cmd == ANDROID_RB_POWEROFF &&
700 !strcmp(&command[len + 1], "userrequested")) {
701 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
702 // Run fsck once the file system is remounted in read-only mode.
703 callback_on_ro_remount = unmount_and_fsck;
704 } else if (cmd == ANDROID_RB_RESTART2) {
705 reboot_target = &command[len + 1];
Vineela Tummalapalli039734c2016-10-28 19:30:07 -0700706 // When rebooting to the bootloader notify the bootloader writing
707 // also the BCB.
708 if (strcmp(reboot_target, "bootloader") == 0) {
709 std::string err;
710 if (!write_reboot_bootloader(&err)) {
711 LOG(ERROR) << "reboot-bootloader: Error writing "
712 "bootloader_message: " << err;
713 }
714 }
Yusuke Satof93d4292015-07-21 15:50:59 -0700715 }
716 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700717 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700718 return -EINVAL;
719 }
720
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800721 std::string timeout = property_get("ro.build.shutdown_timeout");
722 unsigned int delay = 0;
723
Elliott Hughesda46b392016-10-11 17:09:00 -0700724 if (android::base::ParseUint(timeout, &delay) && delay > 0) {
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800725 Timer t;
726 // Ask all services to terminate.
727 ServiceManager::GetInstance().ForEachService(
728 [] (Service* s) { s->Terminate(); });
729
730 while (t.duration() < delay) {
731 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
732
733 int service_count = 0;
734 ServiceManager::GetInstance().ForEachService(
735 [&service_count] (Service* s) {
736 // Count the number of services running.
737 // Exclude the console as it will ignore the SIGTERM signal
738 // and not exit.
739 // Note: SVC_CONSOLE actually means "requires console" but
740 // it is only used by the shell.
741 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
742 service_count++;
743 }
744 });
745
746 if (service_count == 0) {
747 // All terminable services terminated. We can exit early.
748 break;
749 }
750
751 // Wait a bit before recounting the number or running services.
Elliott Hughes290a2282016-11-14 17:08:47 -0800752 std::this_thread::sleep_for(50ms);
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800753 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700754 LOG(VERBOSE) << "Terminating running services took " << t.duration() << " seconds";
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800755 }
756
Yusuke Sato0df08272015-07-08 14:57:07 -0700757 return android_reboot_with_callback(cmd, 0, reboot_target,
758 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700759}
760
Tom Cherryb7349902015-08-26 11:43:36 -0700761static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700762 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700763 return 0;
764}
765
Tom Cherryb7349902015-08-26 11:43:36 -0700766static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700767 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768}
769
Tom Cherryb7349902015-08-26 11:43:36 -0700770static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700771 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800772}
773
Tom Cherryb7349902015-08-26 11:43:36 -0700774static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700775 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800776}
777
Tom Cherryb7349902015-08-26 11:43:36 -0700778static int do_sysclktz(const std::vector<std::string>& args) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700779 struct timezone tz = {};
780 if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) {
781 return 0;
782 }
783 return -1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800784}
785
Tom Cherryb7349902015-08-26 11:43:36 -0700786static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700787 int mode = -1;
788 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000789 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700790 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000791 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700792 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000793}
794
Tom Cherryb7349902015-08-26 11:43:36 -0700795static void verity_update_property(fstab_rec *fstab, const char *mount_point,
796 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100797 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
798 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000799}
800
Tom Cherryb7349902015-08-26 11:43:36 -0700801static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700802 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000803}
804
Tom Cherryb7349902015-08-26 11:43:36 -0700805static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700806 const char* path = args[1].c_str();
807 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700808 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700809}
810
Tom Cherryb7349902015-08-26 11:43:36 -0700811static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700812 char *buffer = NULL;
813 int rc = 0;
814 int fd1 = -1, fd2 = -1;
815 struct stat info;
816 int brtw, brtr;
817 char *p;
818
Tom Cherry96f67312015-07-30 13:52:55 -0700819 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700820 return -1;
821
Tom Cherry96f67312015-07-30 13:52:55 -0700822 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700823 goto out_err;
824
Tom Cherry96f67312015-07-30 13:52:55 -0700825 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700826 goto out_err;
827
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800828 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700829 goto out_err;
830
831 p = buffer;
832 brtr = info.st_size;
833 while(brtr) {
834 rc = read(fd1, p, brtr);
835 if (rc < 0)
836 goto out_err;
837 if (rc == 0)
838 break;
839 p += rc;
840 brtr -= rc;
841 }
842
843 p = buffer;
844 brtw = info.st_size;
845 while(brtw) {
846 rc = write(fd2, p, brtw);
847 if (rc < 0)
848 goto out_err;
849 if (rc == 0)
850 break;
851 p += rc;
852 brtw -= rc;
853 }
854
855 rc = 0;
856 goto out;
857out_err:
858 rc = -1;
859out:
860 if (buffer)
861 free(buffer);
862 if (fd1 >= 0)
863 close(fd1);
864 if (fd2 >= 0)
865 close(fd2);
866 return rc;
867}
868
Tom Cherryb7349902015-08-26 11:43:36 -0700869static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700870 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700871 if (args.size() == 3) {
872 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700873 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700874 } else if (args.size() == 4) {
875 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
876 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700877 return -errno;
878 } else {
879 return -1;
880 }
881 return 0;
882}
883
884static mode_t get_mode(const char *s) {
885 mode_t mode = 0;
886 while (*s) {
887 if (*s >= '0' && *s <= '7') {
888 mode = (mode<<3) | (*s-'0');
889 } else {
890 return -1;
891 }
892 s++;
893 }
894 return mode;
895}
896
Tom Cherryb7349902015-08-26 11:43:36 -0700897static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700898 mode_t mode = get_mode(args[1].c_str());
899 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700900 return -errno;
901 }
902 return 0;
903}
904
Tom Cherryb7349902015-08-26 11:43:36 -0700905static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400906 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500907
Paul Lawrencea8d84342016-11-14 15:40:18 -0800908 struct flag_type {const char* name; int value;};
909 static const flag_type flags[] = {
910 {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE},
911 {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE},
912 {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS},
913 {0, 0}
914 };
915
916 int flag = 0;
917
918 bool in_flags = true;
919 for (size_t i = 1; i < args.size(); ++i) {
920 if (android::base::StartsWith(args[i], "--")) {
921 if (!in_flags) {
922 LOG(ERROR) << "restorecon - flags must precede paths";
923 return -1;
924 }
925 bool found = false;
926 for (size_t j = 0; flags[j].name; ++j) {
927 if (args[i] == flags[j].name) {
928 flag |= flags[j].value;
929 found = true;
930 break;
931 }
932 }
933 if (!found) {
934 LOG(ERROR) << "restorecon - bad flag " << args[i];
935 return -1;
936 }
937 } else {
938 in_flags = false;
939 if (restorecon(args[i].c_str(), flag) < 0) {
940 ret = -errno;
941 }
942 }
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500943 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400944 return ret;
945}
946
Tom Cherryb7349902015-08-26 11:43:36 -0700947static int do_restorecon_recursive(const std::vector<std::string>& args) {
Paul Lawrencea8d84342016-11-14 15:40:18 -0800948 std::vector<std::string> non_const_args(args);
949 non_const_args.insert(std::next(non_const_args.begin()), "--recursive");
950 return do_restorecon(non_const_args);
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500951}
952
Tom Cherryb7349902015-08-26 11:43:36 -0700953static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700954 // TODO: support names instead/as well?
Elliott Hughesda46b392016-10-11 17:09:00 -0700955 int log_level = -1;
956 android::base::ParseInt(args[1], &log_level);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700957 android::base::LogSeverity severity;
958 switch (log_level) {
959 case 7: severity = android::base::DEBUG; break;
960 case 6: severity = android::base::INFO; break;
961 case 5:
962 case 4: severity = android::base::WARNING; break;
963 case 3: severity = android::base::ERROR; break;
964 case 2:
965 case 1:
966 case 0: severity = android::base::FATAL; break;
967 default:
968 LOG(ERROR) << "loglevel: invalid log level " << log_level;
969 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700970 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700971 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700972 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700973}
974
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700975static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700976 load_persist_props();
977 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800978}
979
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700980static int do_load_system_props(const std::vector<std::string>& args) {
981 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700982 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700983}
984
Tom Cherryb7349902015-08-26 11:43:36 -0700985static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700986 if (args.size() == 2) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800987 return wait_for_file(args[1].c_str(), kCommandRetryTimeout);
Tom Cherry96f67312015-07-30 13:52:55 -0700988 } else if (args.size() == 3) {
Elliott Hughesda46b392016-10-11 17:09:00 -0700989 int timeout;
990 if (android::base::ParseInt(args[2], &timeout)) {
Elliott Hughes9605a942016-11-10 17:43:47 -0800991 return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout));
Elliott Hughesda46b392016-10-11 17:09:00 -0700992 }
993 }
994 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700995}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000996
Paul Lawrence806d10b2015-04-28 22:07:10 +0000997/*
998 * Callback to make a directory from the ext4 code
999 */
Tom Cherryb7349902015-08-26 11:43:36 -07001000static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +00001001 if (make_dir(dir, 0700) && errno != EEXIST) {
1002 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001003 }
1004
Paul Lawrence806d10b2015-04-28 22:07:10 +00001005 return 0;
1006}
1007
Paul Crowley749af8c2015-05-28 17:35:06 +01001008static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -07001009 std::string value = property_get("ro.crypto.type");
1010 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +01001011}
1012
Tom Cherryb7349902015-08-26 11:43:36 -07001013static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +01001014 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +00001015 return 0;
1016 }
Tom Cherry96f67312015-07-30 13:52:55 -07001017 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +00001018 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +00001019}
Paul Crowley749af8c2015-05-28 17:35:06 +01001020
Paul Crowley59497452016-02-01 16:37:13 +00001021static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +00001022 return e4crypt_do_init_user0();
1023}
1024
Tom Cherryb7349902015-08-26 11:43:36 -07001025BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
1026 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
1027 static const Map builtin_functions = {
1028 {"bootchart_init", {0, 0, do_bootchart_init}},
1029 {"chmod", {2, 2, do_chmod}},
1030 {"chown", {2, 3, do_chown}},
1031 {"class_reset", {1, 1, do_class_reset}},
1032 {"class_start", {1, 1, do_class_start}},
1033 {"class_stop", {1, 1, do_class_stop}},
1034 {"copy", {2, 2, do_copy}},
1035 {"domainname", {1, 1, do_domainname}},
1036 {"enable", {1, 1, do_enable}},
1037 {"exec", {1, kMax, do_exec}},
1038 {"export", {2, 2, do_export}},
1039 {"hostname", {1, 1, do_hostname}},
1040 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +00001041 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -07001042 {"insmod", {1, kMax, do_insmod}},
1043 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -07001044 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001045 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -07001046 {"loglevel", {1, 1, do_loglevel}},
1047 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +08001048 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -07001049 {"mount", {3, kMax, do_mount}},
1050 {"powerctl", {1, 1, do_powerctl}},
1051 {"restart", {1, 1, do_restart}},
1052 {"restorecon", {1, kMax, do_restorecon}},
1053 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
1054 {"rm", {1, 1, do_rm}},
1055 {"rmdir", {1, 1, do_rmdir}},
1056 {"setprop", {2, 2, do_setprop}},
1057 {"setrlimit", {3, 3, do_setrlimit}},
1058 {"start", {1, 1, do_start}},
1059 {"stop", {1, 1, do_stop}},
1060 {"swapon_all", {1, 1, do_swapon_all}},
1061 {"symlink", {2, 2, do_symlink}},
1062 {"sysclktz", {1, 1, do_sysclktz}},
1063 {"trigger", {1, 1, do_trigger}},
1064 {"verity_load_state", {0, 0, do_verity_load_state}},
1065 {"verity_update_state", {0, 0, do_verity_update_state}},
1066 {"wait", {1, 2, do_wait}},
1067 {"write", {2, 2, do_write}},
1068 };
1069 return builtin_functions;
1070}