blob: 97151c01b58a09e37065134ddc079e0c976c3151 [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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070017#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070018#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070019#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070020#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070021#include <signal.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070023#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070024#include <string.h>
25#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <sys/mount.h>
27#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080028#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070029#include <sys/types.h>
30#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070031#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070032#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000033#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000034#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070035
Stephen Smalleye46f9d52012-01-13 08:48:47 -050036#include <selinux/selinux.h>
37#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038
Elliott Hughesdb3f2672015-03-20 09:45:18 -070039#include <fs_mgr.h>
40#include <base/stringprintf.h>
41#include <cutils/partition_utils.h>
42#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070043#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070044#include <private/android_filesystem_config.h>
45
Tom Cherryfa0c21c2015-07-23 17:53:11 -070046#include "action.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070047#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070048#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070049#include "init_parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070050#include "keywords.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070051#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070052#include "property_service.h"
53#include "service.h"
54#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055
Nick Kralevichbc609542015-01-31 21:39:46 -080056#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070057#define UNMOUNT_CHECK_MS 5000
58#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080059
Elliott Hughesf3cf4382015-02-03 17:12:07 -080060// System call provided by bionic but not in any header file.
61extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Tom Cherry96f67312015-07-30 13:52:55 -070063static int insmod(const char *filename, const char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064{
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070065 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070066 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080068 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069
Elliott Hughesf682b472015-02-06 12:19:48 -080070 // TODO: use finit_module for >= 3.8 kernels.
71 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072}
73
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074static int __ifupdown(const char *interface, int up)
75{
76 struct ifreq ifr;
77 int s, ret;
78
79 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
80
81 s = socket(AF_INET, SOCK_DGRAM, 0);
82 if (s < 0)
83 return -1;
84
85 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
86 if (ret < 0) {
87 goto done;
88 }
89
90 if (up)
91 ifr.ifr_flags |= IFF_UP;
92 else
93 ifr.ifr_flags &= ~IFF_UP;
94
95 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080096
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070097done:
98 close(s);
99 return ret;
100}
101
Yusuke Sato0df08272015-07-08 14:57:07 -0700102static void unmount_and_fsck(const struct mntent *entry)
103{
104 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
105 return;
106
107 /* First, lazily unmount the directory. This unmount request finishes when
108 * all processes that open a file or directory in |entry->mnt_dir| exit.
109 */
110 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
111
112 /* Next, kill all processes except init, kthreadd, and kthreadd's
113 * children to finish the lazy unmount. Killing all processes here is okay
114 * because this callback function is only called right before reboot().
115 * It might be cleaner to selectively kill processes that actually use
116 * |entry->mnt_dir| rather than killing all, probably by reusing a function
117 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
118 * not allow init to scan /proc/<pid> files which the utility function
119 * heavily relies on. The policy does not allow the process to execute
120 * killall/pkill binaries either. Note that some processes might
121 * automatically restart after kill(), but that is not really a problem
122 * because |entry->mnt_dir| is no longer visible to such new processes.
123 */
Tom Cherrybac32992015-07-31 12:45:25 -0700124 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700125 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
126
127 int count = 0;
128 while (count++ < UNMOUNT_CHECK_TIMES) {
129 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
130 if (fd >= 0) {
131 /* |entry->mnt_dir| has sucessfully been unmounted. */
132 close(fd);
133 break;
134 } else if (errno == EBUSY) {
135 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
136 * while then retry.
137 */
138 TEMP_FAILURE_RETRY(
139 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
140 continue;
141 } else {
142 /* Cannot open the device. Give up. */
143 return;
144 }
145 }
146
147 int st;
148 if (!strcmp(entry->mnt_type, "f2fs")) {
149 const char *f2fs_argv[] = {
150 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
151 };
152 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700153 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700154 } else if (!strcmp(entry->mnt_type, "ext4")) {
155 const char *ext4_argv[] = {
156 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
157 };
158 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700159 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700160 }
161}
162
Tom Cherry96f67312015-07-30 13:52:55 -0700163int do_class_start(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700164{
165 /* Starting a class does not start services
166 * which are explicitly disabled. They must
167 * be started individually.
168 */
Tom Cherrybac32992015-07-31 12:45:25 -0700169 ServiceManager::GetInstance().
170 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700171 return 0;
172}
173
Tom Cherry96f67312015-07-30 13:52:55 -0700174int do_class_stop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175{
Tom Cherrybac32992015-07-31 12:45:25 -0700176 ServiceManager::GetInstance().
177 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700178 return 0;
179}
180
Tom Cherry96f67312015-07-30 13:52:55 -0700181int do_class_reset(const std::vector<std::string>& args)
Ken Sumrall752923c2010-12-03 16:33:31 -0800182{
Tom Cherrybac32992015-07-31 12:45:25 -0700183 ServiceManager::GetInstance().
184 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800185 return 0;
186}
187
Tom Cherry96f67312015-07-30 13:52:55 -0700188int do_domainname(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700189{
Tom Cherry96f67312015-07-30 13:52:55 -0700190 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191}
192
Tom Cherry96f67312015-07-30 13:52:55 -0700193int do_enable(const std::vector<std::string>& args)
JP Abgrall3beec7e2014-05-02 21:14:29 -0700194{
Tom Cherrybac32992015-07-31 12:45:25 -0700195 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
196 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700197 return -1;
198 }
Tom Cherrybac32992015-07-31 12:45:25 -0700199 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700200}
201
Tom Cherry96f67312015-07-30 13:52:55 -0700202int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700203 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
204 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800205 return -1;
206 }
Tom Cherrybac32992015-07-31 12:45:25 -0700207 if (!svc->Start()) {
208 return -1;
209 }
210 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800211 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212}
213
Tom Cherry96f67312015-07-30 13:52:55 -0700214int do_export(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215{
Tom Cherry96f67312015-07-30 13:52:55 -0700216 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700217}
218
Tom Cherry96f67312015-07-30 13:52:55 -0700219int do_hostname(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220{
Tom Cherry96f67312015-07-30 13:52:55 -0700221 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222}
223
Tom Cherry96f67312015-07-30 13:52:55 -0700224int do_ifup(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225{
Tom Cherry96f67312015-07-30 13:52:55 -0700226 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Tom Cherry96f67312015-07-30 13:52:55 -0700229int do_insmod(const std::vector<std::string>& args)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800230{
Tom Cherry96f67312015-07-30 13:52:55 -0700231 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800232
Tom Cherry96f67312015-07-30 13:52:55 -0700233 if (args.size() > 2) {
234 options += args[2];
235 for (std::size_t i = 3; i < args.size(); ++i) {
236 options += ' ';
237 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800238 }
239 }
240
Tom Cherry96f67312015-07-30 13:52:55 -0700241 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800242}
243
Tom Cherry96f67312015-07-30 13:52:55 -0700244int do_mkdir(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700245{
246 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700247 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248
249 /* mkdir <path> [mode] [owner] [group] */
250
Tom Cherry96f67312015-07-30 13:52:55 -0700251 if (args.size() >= 3) {
252 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700253 }
254
Tom Cherry96f67312015-07-30 13:52:55 -0700255 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700256 /* chmod in case the directory already exists */
257 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700258 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700259 }
260 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261 return -errno;
262 }
263
Tom Cherry96f67312015-07-30 13:52:55 -0700264 if (args.size() >= 4) {
265 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266 gid_t gid = -1;
267
Tom Cherry96f67312015-07-30 13:52:55 -0700268 if (args.size() == 5) {
269 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270 }
271
Tom Cherry96f67312015-07-30 13:52:55 -0700272 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700273 return -errno;
274 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700275
276 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
277 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700278 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700279 if (ret == -1) {
280 return -errno;
281 }
282 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283 }
284
Tom Cherry96f67312015-07-30 13:52:55 -0700285 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286}
287
288static struct {
289 const char *name;
290 unsigned flag;
291} mount_flags[] = {
292 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200293 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 { "nosuid", MS_NOSUID },
295 { "nodev", MS_NODEV },
296 { "nodiratime", MS_NODIRATIME },
297 { "ro", MS_RDONLY },
298 { "rw", 0 },
299 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700300 { "bind", MS_BIND },
301 { "rec", MS_REC },
302 { "unbindable", MS_UNBINDABLE },
303 { "private", MS_PRIVATE },
304 { "slave", MS_SLAVE },
305 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 { "defaults", 0 },
307 { 0, 0 },
308};
309
Ken Sumrall752923c2010-12-03 16:33:31 -0800310#define DATA_MNT_POINT "/data"
311
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherry96f67312015-07-30 13:52:55 -0700313int do_mount(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314{
315 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700316 const char *source, *target, *system;
317 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700319 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700321 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322
Tom Cherry96f67312015-07-30 13:52:55 -0700323 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700325 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 flags |= mount_flags[i].flag;
327 break;
328 }
329 }
330
Colin Crosscd0f1732010-04-19 17:10:24 -0700331 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700332 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700333 wait = 1;
334 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700335 else if (na + 1 == args.size())
336 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700337 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338 }
339
Tom Cherry96f67312015-07-30 13:52:55 -0700340 system = args[1].c_str();
341 source = args[2].c_str();
342 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000343
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700344 if (!strncmp(source, "mtd@", 4)) {
345 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000346 if (n < 0) {
347 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000349
Yabin Cuie2d63af2015-02-17 19:27:51 -0800350 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000351
Colin Crosscd0f1732010-04-19 17:10:24 -0700352 if (wait)
353 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000354 if (mount(tmp, target, system, flags, options) < 0) {
355 return -1;
356 }
357
Ken Sumralldd4d7862011-02-17 18:09:47 -0800358 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359 } else if (!strncmp(source, "loop@", 5)) {
360 int mode, loop, fd;
361 struct loop_info info;
362
363 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800364 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000365 if (fd < 0) {
366 return -1;
367 }
368
369 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800370 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800371 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000372 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100373 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000374 return -1;
375 }
376
377 /* if it is a blank loop device */
378 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
379 /* if it becomes our loop device */
380 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
381 close(fd);
382
383 if (mount(tmp, target, system, flags, options) < 0) {
384 ioctl(loop, LOOP_CLR_FD, 0);
385 close(loop);
386 return -1;
387 }
388
389 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800390 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391 }
392 }
393
394 close(loop);
395 }
396
397 close(fd);
398 ERROR("out of loopback devices");
399 return -1;
400 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700401 if (wait)
402 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000403 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700404 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000405 }
406
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800408
409exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800410 return 0;
411
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412}
413
JP Abgrallcee20682014-07-02 14:26:54 -0700414static int wipe_data_via_recovery()
415{
416 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800417 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700418 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700419 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
420 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700421 close(fd);
422 } else {
423 ERROR("could not open /cache/recovery/command\n");
424 return -1;
425 }
426 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
427 while (1) { pause(); } // never reached
428}
429
Tom Cherryb8dd0272015-07-22 14:23:33 -0700430void import_late()
431{
432 static const std::vector<std::string> init_directories = {
433 "/system/etc/init",
434 "/vendor/etc/init",
435 "/odm/etc/init"
436 };
437
438 for (const auto& dir : init_directories) {
439 init_parse_config(dir.c_str());
440 }
441}
442
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000443/*
JP Abgrallcee20682014-07-02 14:26:54 -0700444 * This function might request a reboot, in which case it will
445 * not return.
446 */
Tom Cherry96f67312015-07-30 13:52:55 -0700447int do_mount_all(const std::vector<std::string>& args)
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700448{
449 pid_t pid;
450 int ret = -1;
451 int child_ret = -1;
452 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800453 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700454
Tom Cherry96f67312015-07-30 13:52:55 -0700455 if (args.size() != 2) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700456 return -1;
457 }
Tom Cherry96f67312015-07-30 13:52:55 -0700458 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700459 /*
460 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
461 * do the call in the child to provide protection to the main init
462 * process if anything goes wrong (crash or memory leak), and wait for
463 * the child to finish in the parent.
464 */
465 pid = fork();
466 if (pid > 0) {
467 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700468 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
469 if (wp_ret < 0) {
470 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700471 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700472 }
473
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700474 if (WIFEXITED(status)) {
475 ret = WEXITSTATUS(status);
476 } else {
477 ret = -1;
478 }
479 } else if (pid == 0) {
480 /* child, call fs_mgr_mount_all() */
481 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800482 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800483 child_ret = fs_mgr_mount_all(fstab);
484 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700485 if (child_ret == -1) {
486 ERROR("fs_mgr_mount_all returned an error\n");
487 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700488 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700489 } else {
490 /* fork failed, return an error */
491 return -1;
492 }
493
Tom Cherryb8dd0272015-07-22 14:23:33 -0700494 import_late();
495
JP Abgrallf22b7452014-07-02 13:16:04 -0700496 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800497 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700498 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700499 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000500 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800501 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700502 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700503 property_set("ro.crypto.state", "unencrypted");
504 /* If fs_mgr determined this is an unencrypted device, then trigger
505 * that action.
506 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700507 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700508 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
509 /* Setup a wipe via recovery, and reboot into recovery */
510 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
511 ret = wipe_data_via_recovery();
512 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000513 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000514 if (e4crypt_install_keyring()) {
515 return -1;
516 }
517 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000518 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000519
520 // Although encrypted, we have device key, so we do not need to
521 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700522 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000523 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
524 if (e4crypt_install_keyring()) {
525 return -1;
526 }
527 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000528 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000529 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700530 } else if (ret > 0) {
531 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700532 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700533 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700534
535 return ret;
536}
537
Tom Cherry96f67312015-07-30 13:52:55 -0700538int do_swapon_all(const std::vector<std::string>& args)
Ken Sumralla76baaa2013-07-09 18:42:09 -0700539{
540 struct fstab *fstab;
541 int ret;
542
Tom Cherry96f67312015-07-30 13:52:55 -0700543 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700544 ret = fs_mgr_swapon_all(fstab);
545 fs_mgr_free_fstab(fstab);
546
547 return ret;
548}
549
Tom Cherry96f67312015-07-30 13:52:55 -0700550int do_setprop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700551{
Tom Cherry96f67312015-07-30 13:52:55 -0700552 const char* name = args[1].c_str();
553 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700554 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700555 return 0;
556}
557
Tom Cherry96f67312015-07-30 13:52:55 -0700558int do_setrlimit(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559{
560 struct rlimit limit;
561 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700562 resource = std::stoi(args[1]);
563 limit.rlim_cur = std::stoi(args[2]);
564 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700565 return setrlimit(resource, &limit);
566}
567
Tom Cherry96f67312015-07-30 13:52:55 -0700568int do_start(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700569{
Tom Cherrybac32992015-07-31 12:45:25 -0700570 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
571 if (!svc) {
572 ERROR("do_start: Service %s not found\n", args[1].c_str());
573 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574 }
Tom Cherrybac32992015-07-31 12:45:25 -0700575 if (!svc->Start())
576 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577 return 0;
578}
579
Tom Cherry96f67312015-07-30 13:52:55 -0700580int do_stop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581{
Tom Cherrybac32992015-07-31 12:45:25 -0700582 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
583 if (!svc) {
584 ERROR("do_stop: Service %s not found\n", args[1].c_str());
585 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700586 }
Tom Cherrybac32992015-07-31 12:45:25 -0700587 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588 return 0;
589}
590
Tom Cherry96f67312015-07-30 13:52:55 -0700591int do_restart(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592{
Tom Cherrybac32992015-07-31 12:45:25 -0700593 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
594 if (!svc) {
595 ERROR("do_restart: Service %s not found\n", args[1].c_str());
596 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 }
Tom Cherrybac32992015-07-31 12:45:25 -0700598 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700599 return 0;
600}
601
Tom Cherry96f67312015-07-30 13:52:55 -0700602int do_powerctl(const std::vector<std::string>& args)
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700603{
Tom Cherry96f67312015-07-30 13:52:55 -0700604 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700605 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700606 unsigned int cmd = 0;
607 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700608 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700609
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700610 if (strncmp(command, "shutdown", 8) == 0) {
611 cmd = ANDROID_RB_POWEROFF;
612 len = 8;
613 } else if (strncmp(command, "reboot", 6) == 0) {
614 cmd = ANDROID_RB_RESTART2;
615 len = 6;
616 } else {
617 ERROR("powerctl: unrecognized command '%s'\n", command);
618 return -EINVAL;
619 }
620
621 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700622 if (cmd == ANDROID_RB_POWEROFF &&
623 !strcmp(&command[len + 1], "userrequested")) {
624 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
625 // Run fsck once the file system is remounted in read-only mode.
626 callback_on_ro_remount = unmount_and_fsck;
627 } else if (cmd == ANDROID_RB_RESTART2) {
628 reboot_target = &command[len + 1];
629 }
630 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700631 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
632 return -EINVAL;
633 }
634
Yusuke Sato0df08272015-07-08 14:57:07 -0700635 return android_reboot_with_callback(cmd, 0, reboot_target,
636 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700637}
638
Tom Cherry96f67312015-07-30 13:52:55 -0700639int do_trigger(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700641 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700642 return 0;
643}
644
Tom Cherry96f67312015-07-30 13:52:55 -0700645int do_symlink(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700646{
Tom Cherry96f67312015-07-30 13:52:55 -0700647 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700648}
649
Tom Cherry96f67312015-07-30 13:52:55 -0700650int do_rm(const std::vector<std::string>& args)
Ken Sumrall203bad52011-01-18 17:37:41 -0800651{
Tom Cherry96f67312015-07-30 13:52:55 -0700652 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800653}
654
Tom Cherry96f67312015-07-30 13:52:55 -0700655int do_rmdir(const std::vector<std::string>& args)
Ken Sumrall203bad52011-01-18 17:37:41 -0800656{
Tom Cherry96f67312015-07-30 13:52:55 -0700657 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800658}
659
Tom Cherry96f67312015-07-30 13:52:55 -0700660int do_sysclktz(const std::vector<std::string>& args)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800661{
662 struct timezone tz;
663
Tom Cherry96f67312015-07-30 13:52:55 -0700664 if (args.size() != 2)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800665 return -1;
666
667 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700668 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800669 if (settimeofday(NULL, &tz))
670 return -1;
671 return 0;
672}
673
Tom Cherry96f67312015-07-30 13:52:55 -0700674int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700675 int mode = -1;
676 int rc = fs_mgr_load_verity_state(&mode);
677 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700678 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000679 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700680 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000681}
682
Sami Tolvanen45474232015-03-30 11:38:38 +0100683static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
684 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
685 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000686}
687
Tom Cherry96f67312015-07-30 13:52:55 -0700688int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700689 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000690}
691
Tom Cherry96f67312015-07-30 13:52:55 -0700692int do_write(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700693{
Tom Cherry96f67312015-07-30 13:52:55 -0700694 const char* path = args[1].c_str();
695 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700696 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700697}
698
Tom Cherry96f67312015-07-30 13:52:55 -0700699int do_copy(const std::vector<std::string>& args)
San Mehat7c44fe52009-08-26 16:39:25 -0700700{
701 char *buffer = NULL;
702 int rc = 0;
703 int fd1 = -1, fd2 = -1;
704 struct stat info;
705 int brtw, brtr;
706 char *p;
707
Tom Cherry96f67312015-07-30 13:52:55 -0700708 if (args.size() != 3)
San Mehat7c44fe52009-08-26 16:39:25 -0700709 return -1;
710
Tom Cherry96f67312015-07-30 13:52:55 -0700711 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700712 return -1;
713
Tom Cherry96f67312015-07-30 13:52:55 -0700714 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700715 goto out_err;
716
Tom Cherry96f67312015-07-30 13:52:55 -0700717 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700718 goto out_err;
719
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800720 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700721 goto out_err;
722
723 p = buffer;
724 brtr = info.st_size;
725 while(brtr) {
726 rc = read(fd1, p, brtr);
727 if (rc < 0)
728 goto out_err;
729 if (rc == 0)
730 break;
731 p += rc;
732 brtr -= rc;
733 }
734
735 p = buffer;
736 brtw = info.st_size;
737 while(brtw) {
738 rc = write(fd2, p, brtw);
739 if (rc < 0)
740 goto out_err;
741 if (rc == 0)
742 break;
743 p += rc;
744 brtw -= rc;
745 }
746
747 rc = 0;
748 goto out;
749out_err:
750 rc = -1;
751out:
752 if (buffer)
753 free(buffer);
754 if (fd1 >= 0)
755 close(fd1);
756 if (fd2 >= 0)
757 close(fd2);
758 return rc;
759}
760
Tom Cherry96f67312015-07-30 13:52:55 -0700761int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700762 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700763 if (args.size() == 3) {
764 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700766 } else if (args.size() == 4) {
767 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
768 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769 return -errno;
770 } else {
771 return -1;
772 }
773 return 0;
774}
775
776static mode_t get_mode(const char *s) {
777 mode_t mode = 0;
778 while (*s) {
779 if (*s >= '0' && *s <= '7') {
780 mode = (mode<<3) | (*s-'0');
781 } else {
782 return -1;
783 }
784 s++;
785 }
786 return mode;
787}
788
Tom Cherry96f67312015-07-30 13:52:55 -0700789int do_chmod(const std::vector<std::string>& args) {
790 mode_t mode = get_mode(args[1].c_str());
791 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792 return -errno;
793 }
794 return 0;
795}
796
Tom Cherry96f67312015-07-30 13:52:55 -0700797int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400798 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500799
Tom Cherry96f67312015-07-30 13:52:55 -0700800 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
801 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400802 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400804 return ret;
805}
806
Tom Cherry96f67312015-07-30 13:52:55 -0700807int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400808 int ret = 0;
809
Tom Cherry96f67312015-07-30 13:52:55 -0700810 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
811 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400812 ret = -errno;
813 }
814 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500815}
816
Tom Cherry96f67312015-07-30 13:52:55 -0700817int do_loglevel(const std::vector<std::string>& args) {
818 if (args.size() != 2) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700819 ERROR("loglevel: missing argument\n");
820 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700822
Tom Cherry96f67312015-07-30 13:52:55 -0700823 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700824 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
825 ERROR("loglevel: invalid log level'%d'\n", log_level);
826 return -EINVAL;
827 }
828 klog_set_level(log_level);
829 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830}
831
Tom Cherry96f67312015-07-30 13:52:55 -0700832int do_load_persist_props(const std::vector<std::string>& args) {
833 if (args.size() == 1) {
Ken Sumrallc5c51032011-03-08 17:01:29 -0800834 load_persist_props();
835 return 0;
836 }
837 return -1;
838}
839
Tom Cherry96f67312015-07-30 13:52:55 -0700840int do_load_all_props(const std::vector<std::string>& args) {
841 if (args.size() == 1) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700842 load_all_props();
843 return 0;
844 }
845 return -1;
846}
847
Tom Cherry96f67312015-07-30 13:52:55 -0700848int do_wait(const std::vector<std::string>& args)
Colin Crosscd0f1732010-04-19 17:10:24 -0700849{
Tom Cherry96f67312015-07-30 13:52:55 -0700850 if (args.size() == 2) {
851 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
852 } else if (args.size() == 3) {
853 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800854 } else
855 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700856}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000857
Paul Lawrence806d10b2015-04-28 22:07:10 +0000858/*
859 * Callback to make a directory from the ext4 code
860 */
861static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000862{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000863 if (make_dir(dir, 0700) && errno != EEXIST) {
864 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000865 }
866
Paul Lawrence806d10b2015-04-28 22:07:10 +0000867 return 0;
868}
869
Tom Cherry96f67312015-07-30 13:52:55 -0700870int do_installkey(const std::vector<std::string>& args)
Paul Lawrence806d10b2015-04-28 22:07:10 +0000871{
Tom Cherry96f67312015-07-30 13:52:55 -0700872 if (args.size() != 2) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000873 return -1;
874 }
875
Yabin Cui74edcea2015-07-24 10:11:05 -0700876 std::string prop_value = property_get("ro.crypto.type");
877 if (prop_value != "file") {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000878 return 0;
879 }
880
Tom Cherry96f67312015-07-30 13:52:55 -0700881 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000882 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000883}