blob: 470437cf0541787873f7e4a84e21bb5de5acb82f [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,
153 &st, true, LOG_KLOG, true, NULL);
154 } 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,
159 &st, true, LOG_KLOG, true, NULL);
160 }
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
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000430/*
JP Abgrallcee20682014-07-02 14:26:54 -0700431 * This function might request a reboot, in which case it will
432 * not return.
433 */
Tom Cherry96f67312015-07-30 13:52:55 -0700434int do_mount_all(const std::vector<std::string>& args)
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700435{
436 pid_t pid;
437 int ret = -1;
438 int child_ret = -1;
439 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800440 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700441
Tom Cherry96f67312015-07-30 13:52:55 -0700442 if (args.size() != 2) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700443 return -1;
444 }
Tom Cherry96f67312015-07-30 13:52:55 -0700445 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700446 /*
447 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
448 * do the call in the child to provide protection to the main init
449 * process if anything goes wrong (crash or memory leak), and wait for
450 * the child to finish in the parent.
451 */
452 pid = fork();
453 if (pid > 0) {
454 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700455 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
456 if (wp_ret < 0) {
457 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700458 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700459 }
460
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700461 if (WIFEXITED(status)) {
462 ret = WEXITSTATUS(status);
463 } else {
464 ret = -1;
465 }
466 } else if (pid == 0) {
467 /* child, call fs_mgr_mount_all() */
468 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800469 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800470 child_ret = fs_mgr_mount_all(fstab);
471 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700472 if (child_ret == -1) {
473 ERROR("fs_mgr_mount_all returned an error\n");
474 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700475 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700476 } else {
477 /* fork failed, return an error */
478 return -1;
479 }
480
JP Abgrallf22b7452014-07-02 13:16:04 -0700481 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800482 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700483 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700484 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000485 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800486 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700487 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700488 property_set("ro.crypto.state", "unencrypted");
489 /* If fs_mgr determined this is an unencrypted device, then trigger
490 * that action.
491 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700492 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700493 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
494 /* Setup a wipe via recovery, and reboot into recovery */
495 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
496 ret = wipe_data_via_recovery();
497 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000498 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000499 if (e4crypt_install_keyring()) {
500 return -1;
501 }
502 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000503 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000504
505 // Although encrypted, we have device key, so we do not need to
506 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700507 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000508 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
509 if (e4crypt_install_keyring()) {
510 return -1;
511 }
512 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000513 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000514 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700515 } else if (ret > 0) {
516 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700517 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700518 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519
520 return ret;
521}
522
Tom Cherry96f67312015-07-30 13:52:55 -0700523int do_swapon_all(const std::vector<std::string>& args)
Ken Sumralla76baaa2013-07-09 18:42:09 -0700524{
525 struct fstab *fstab;
526 int ret;
527
Tom Cherry96f67312015-07-30 13:52:55 -0700528 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700529 ret = fs_mgr_swapon_all(fstab);
530 fs_mgr_free_fstab(fstab);
531
532 return ret;
533}
534
Tom Cherry96f67312015-07-30 13:52:55 -0700535int do_setprop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700536{
Tom Cherry96f67312015-07-30 13:52:55 -0700537 const char* name = args[1].c_str();
538 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700539 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 return 0;
541}
542
Tom Cherry96f67312015-07-30 13:52:55 -0700543int do_setrlimit(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700544{
545 struct rlimit limit;
546 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700547 resource = std::stoi(args[1]);
548 limit.rlim_cur = std::stoi(args[2]);
549 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700550 return setrlimit(resource, &limit);
551}
552
Tom Cherry96f67312015-07-30 13:52:55 -0700553int do_start(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700554{
Tom Cherrybac32992015-07-31 12:45:25 -0700555 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
556 if (!svc) {
557 ERROR("do_start: Service %s not found\n", args[1].c_str());
558 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 }
Tom Cherrybac32992015-07-31 12:45:25 -0700560 if (!svc->Start())
561 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700562 return 0;
563}
564
Tom Cherry96f67312015-07-30 13:52:55 -0700565int do_stop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700566{
Tom Cherrybac32992015-07-31 12:45:25 -0700567 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
568 if (!svc) {
569 ERROR("do_stop: Service %s not found\n", args[1].c_str());
570 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571 }
Tom Cherrybac32992015-07-31 12:45:25 -0700572 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700573 return 0;
574}
575
Tom Cherry96f67312015-07-30 13:52:55 -0700576int do_restart(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577{
Tom Cherrybac32992015-07-31 12:45:25 -0700578 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
579 if (!svc) {
580 ERROR("do_restart: Service %s not found\n", args[1].c_str());
581 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700582 }
Tom Cherrybac32992015-07-31 12:45:25 -0700583 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700584 return 0;
585}
586
Tom Cherry96f67312015-07-30 13:52:55 -0700587int do_powerctl(const std::vector<std::string>& args)
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700588{
Tom Cherry96f67312015-07-30 13:52:55 -0700589 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700590 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700591 unsigned int cmd = 0;
592 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700593 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700594
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700595 if (strncmp(command, "shutdown", 8) == 0) {
596 cmd = ANDROID_RB_POWEROFF;
597 len = 8;
598 } else if (strncmp(command, "reboot", 6) == 0) {
599 cmd = ANDROID_RB_RESTART2;
600 len = 6;
601 } else {
602 ERROR("powerctl: unrecognized command '%s'\n", command);
603 return -EINVAL;
604 }
605
606 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700607 if (cmd == ANDROID_RB_POWEROFF &&
608 !strcmp(&command[len + 1], "userrequested")) {
609 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
610 // Run fsck once the file system is remounted in read-only mode.
611 callback_on_ro_remount = unmount_and_fsck;
612 } else if (cmd == ANDROID_RB_RESTART2) {
613 reboot_target = &command[len + 1];
614 }
615 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700616 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
617 return -EINVAL;
618 }
619
Yusuke Sato0df08272015-07-08 14:57:07 -0700620 return android_reboot_with_callback(cmd, 0, reboot_target,
621 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700622}
623
Tom Cherry96f67312015-07-30 13:52:55 -0700624int do_trigger(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700625{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700626 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 return 0;
628}
629
Tom Cherry96f67312015-07-30 13:52:55 -0700630int do_symlink(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700631{
Tom Cherry96f67312015-07-30 13:52:55 -0700632 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700633}
634
Tom Cherry96f67312015-07-30 13:52:55 -0700635int do_rm(const std::vector<std::string>& args)
Ken Sumrall203bad52011-01-18 17:37:41 -0800636{
Tom Cherry96f67312015-07-30 13:52:55 -0700637 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800638}
639
Tom Cherry96f67312015-07-30 13:52:55 -0700640int do_rmdir(const std::vector<std::string>& args)
Ken Sumrall203bad52011-01-18 17:37:41 -0800641{
Tom Cherry96f67312015-07-30 13:52:55 -0700642 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800643}
644
Tom Cherry96f67312015-07-30 13:52:55 -0700645int do_sysclktz(const std::vector<std::string>& args)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800646{
647 struct timezone tz;
648
Tom Cherry96f67312015-07-30 13:52:55 -0700649 if (args.size() != 2)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800650 return -1;
651
652 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700653 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800654 if (settimeofday(NULL, &tz))
655 return -1;
656 return 0;
657}
658
Tom Cherry96f67312015-07-30 13:52:55 -0700659int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700660 int mode = -1;
661 int rc = fs_mgr_load_verity_state(&mode);
662 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700663 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000664 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700665 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000666}
667
Sami Tolvanen45474232015-03-30 11:38:38 +0100668static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
669 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
670 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000671}
672
Tom Cherry96f67312015-07-30 13:52:55 -0700673int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700674 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000675}
676
Tom Cherry96f67312015-07-30 13:52:55 -0700677int do_write(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700678{
Tom Cherry96f67312015-07-30 13:52:55 -0700679 const char* path = args[1].c_str();
680 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700681 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682}
683
Tom Cherry96f67312015-07-30 13:52:55 -0700684int do_copy(const std::vector<std::string>& args)
San Mehat7c44fe52009-08-26 16:39:25 -0700685{
686 char *buffer = NULL;
687 int rc = 0;
688 int fd1 = -1, fd2 = -1;
689 struct stat info;
690 int brtw, brtr;
691 char *p;
692
Tom Cherry96f67312015-07-30 13:52:55 -0700693 if (args.size() != 3)
San Mehat7c44fe52009-08-26 16:39:25 -0700694 return -1;
695
Tom Cherry96f67312015-07-30 13:52:55 -0700696 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700697 return -1;
698
Tom Cherry96f67312015-07-30 13:52:55 -0700699 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700700 goto out_err;
701
Tom Cherry96f67312015-07-30 13:52:55 -0700702 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700703 goto out_err;
704
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800705 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700706 goto out_err;
707
708 p = buffer;
709 brtr = info.st_size;
710 while(brtr) {
711 rc = read(fd1, p, brtr);
712 if (rc < 0)
713 goto out_err;
714 if (rc == 0)
715 break;
716 p += rc;
717 brtr -= rc;
718 }
719
720 p = buffer;
721 brtw = info.st_size;
722 while(brtw) {
723 rc = write(fd2, p, brtw);
724 if (rc < 0)
725 goto out_err;
726 if (rc == 0)
727 break;
728 p += rc;
729 brtw -= rc;
730 }
731
732 rc = 0;
733 goto out;
734out_err:
735 rc = -1;
736out:
737 if (buffer)
738 free(buffer);
739 if (fd1 >= 0)
740 close(fd1);
741 if (fd2 >= 0)
742 close(fd2);
743 return rc;
744}
745
Tom Cherry96f67312015-07-30 13:52:55 -0700746int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700747 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700748 if (args.size() == 3) {
749 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700751 } else if (args.size() == 4) {
752 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
753 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700754 return -errno;
755 } else {
756 return -1;
757 }
758 return 0;
759}
760
761static mode_t get_mode(const char *s) {
762 mode_t mode = 0;
763 while (*s) {
764 if (*s >= '0' && *s <= '7') {
765 mode = (mode<<3) | (*s-'0');
766 } else {
767 return -1;
768 }
769 s++;
770 }
771 return mode;
772}
773
Tom Cherry96f67312015-07-30 13:52:55 -0700774int do_chmod(const std::vector<std::string>& args) {
775 mode_t mode = get_mode(args[1].c_str());
776 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700777 return -errno;
778 }
779 return 0;
780}
781
Tom Cherry96f67312015-07-30 13:52:55 -0700782int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400783 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500784
Tom Cherry96f67312015-07-30 13:52:55 -0700785 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
786 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400787 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500788 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400789 return ret;
790}
791
Tom Cherry96f67312015-07-30 13:52:55 -0700792int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400793 int ret = 0;
794
Tom Cherry96f67312015-07-30 13:52:55 -0700795 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
796 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400797 ret = -errno;
798 }
799 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500800}
801
Tom Cherry96f67312015-07-30 13:52:55 -0700802int do_loglevel(const std::vector<std::string>& args) {
803 if (args.size() != 2) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700804 ERROR("loglevel: missing argument\n");
805 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700807
Tom Cherry96f67312015-07-30 13:52:55 -0700808 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700809 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
810 ERROR("loglevel: invalid log level'%d'\n", log_level);
811 return -EINVAL;
812 }
813 klog_set_level(log_level);
814 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815}
816
Tom Cherry96f67312015-07-30 13:52:55 -0700817int do_load_persist_props(const std::vector<std::string>& args) {
818 if (args.size() == 1) {
Ken Sumrallc5c51032011-03-08 17:01:29 -0800819 load_persist_props();
820 return 0;
821 }
822 return -1;
823}
824
Tom Cherry96f67312015-07-30 13:52:55 -0700825int do_load_all_props(const std::vector<std::string>& args) {
826 if (args.size() == 1) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700827 load_all_props();
828 return 0;
829 }
830 return -1;
831}
832
Tom Cherry96f67312015-07-30 13:52:55 -0700833int do_wait(const std::vector<std::string>& args)
Colin Crosscd0f1732010-04-19 17:10:24 -0700834{
Tom Cherry96f67312015-07-30 13:52:55 -0700835 if (args.size() == 2) {
836 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
837 } else if (args.size() == 3) {
838 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800839 } else
840 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700841}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000842
Paul Lawrence806d10b2015-04-28 22:07:10 +0000843/*
844 * Callback to make a directory from the ext4 code
845 */
846static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000847{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000848 if (make_dir(dir, 0700) && errno != EEXIST) {
849 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000850 }
851
Paul Lawrence806d10b2015-04-28 22:07:10 +0000852 return 0;
853}
854
Tom Cherry96f67312015-07-30 13:52:55 -0700855int do_installkey(const std::vector<std::string>& args)
Paul Lawrence806d10b2015-04-28 22:07:10 +0000856{
Tom Cherry96f67312015-07-30 13:52:55 -0700857 if (args.size() != 2) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000858 return -1;
859 }
860
Yabin Cui74edcea2015-07-24 10:11:05 -0700861 std::string prop_value = property_get("ro.crypto.type");
862 if (prop_value != "file") {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000863 return 0;
864 }
865
Tom Cherry96f67312015-07-30 13:52:55 -0700866 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000867 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000868}