blob: c4cca946c107256fc63a37c939aed02ce39c8fa1 [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 "init.h"
48#include "keywords.h"
49#include "property_service.h"
50#include "devices.h"
Colin Cross6310a822010-04-20 14:29:05 -070051#include "init_parser.h"
Colin Cross3899e9f2010-04-13 20:35:46 -070052#include "util.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054
Nick Kralevichbc609542015-01-31 21:39:46 -080055#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070056#define UNMOUNT_CHECK_MS 5000
57#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080058
James Morrissey381341f2014-05-16 11:36:36 +010059int add_environment(const char *name, const char *value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Elliott Hughesf3cf4382015-02-03 17:12:07 -080061// System call provided by bionic but not in any header file.
62extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
Tom Cherry96f67312015-07-30 13:52:55 -070064static int insmod(const char *filename, const char *options)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065{
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070066 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070067 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070068 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080069 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070
Elliott Hughesf682b472015-02-06 12:19:48 -080071 // TODO: use finit_module for >= 3.8 kernels.
72 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073}
74
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075static int __ifupdown(const char *interface, int up)
76{
77 struct ifreq ifr;
78 int s, ret;
79
80 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
81
82 s = socket(AF_INET, SOCK_DGRAM, 0);
83 if (s < 0)
84 return -1;
85
86 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
87 if (ret < 0) {
88 goto done;
89 }
90
91 if (up)
92 ifr.ifr_flags |= IFF_UP;
93 else
94 ifr.ifr_flags &= ~IFF_UP;
95
96 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -080097
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098done:
99 close(s);
100 return ret;
101}
102
103static void service_start_if_not_disabled(struct service *svc)
104{
105 if (!(svc->flags & SVC_DISABLED)) {
San Mehatf24e2522009-05-19 13:30:46 -0700106 service_start(svc, NULL);
JP Abgrall3beec7e2014-05-02 21:14:29 -0700107 } else {
108 svc->flags |= SVC_DISABLED_START;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109 }
110}
111
Yusuke Sato0df08272015-07-08 14:57:07 -0700112static void unmount_and_fsck(const struct mntent *entry)
113{
114 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
115 return;
116
117 /* First, lazily unmount the directory. This unmount request finishes when
118 * all processes that open a file or directory in |entry->mnt_dir| exit.
119 */
120 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
121
122 /* Next, kill all processes except init, kthreadd, and kthreadd's
123 * children to finish the lazy unmount. Killing all processes here is okay
124 * because this callback function is only called right before reboot().
125 * It might be cleaner to selectively kill processes that actually use
126 * |entry->mnt_dir| rather than killing all, probably by reusing a function
127 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
128 * not allow init to scan /proc/<pid> files which the utility function
129 * heavily relies on. The policy does not allow the process to execute
130 * killall/pkill binaries either. Note that some processes might
131 * automatically restart after kill(), but that is not really a problem
132 * because |entry->mnt_dir| is no longer visible to such new processes.
133 */
134 service_for_each(service_stop);
135 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
136
137 int count = 0;
138 while (count++ < UNMOUNT_CHECK_TIMES) {
139 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
140 if (fd >= 0) {
141 /* |entry->mnt_dir| has sucessfully been unmounted. */
142 close(fd);
143 break;
144 } else if (errno == EBUSY) {
145 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
146 * while then retry.
147 */
148 TEMP_FAILURE_RETRY(
149 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
150 continue;
151 } else {
152 /* Cannot open the device. Give up. */
153 return;
154 }
155 }
156
157 int st;
158 if (!strcmp(entry->mnt_type, "f2fs")) {
159 const char *f2fs_argv[] = {
160 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
161 };
162 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
163 &st, true, LOG_KLOG, true, NULL);
164 } else if (!strcmp(entry->mnt_type, "ext4")) {
165 const char *ext4_argv[] = {
166 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
167 };
168 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
169 &st, true, LOG_KLOG, true, NULL);
170 }
171}
172
Tom Cherry96f67312015-07-30 13:52:55 -0700173int do_class_start(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700174{
175 /* Starting a class does not start services
176 * which are explicitly disabled. They must
177 * be started individually.
178 */
Tom Cherry96f67312015-07-30 13:52:55 -0700179 service_for_each_class(args[1].c_str(), service_start_if_not_disabled);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180 return 0;
181}
182
Tom Cherry96f67312015-07-30 13:52:55 -0700183int do_class_stop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700184{
Tom Cherry96f67312015-07-30 13:52:55 -0700185 service_for_each_class(args[1].c_str(), service_stop);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186 return 0;
187}
188
Tom Cherry96f67312015-07-30 13:52:55 -0700189int do_class_reset(const std::vector<std::string>& args)
Ken Sumrall752923c2010-12-03 16:33:31 -0800190{
Tom Cherry96f67312015-07-30 13:52:55 -0700191 service_for_each_class(args[1].c_str(), service_reset);
Ken Sumrall752923c2010-12-03 16:33:31 -0800192 return 0;
193}
194
Tom Cherry96f67312015-07-30 13:52:55 -0700195int do_domainname(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700196{
Tom Cherry96f67312015-07-30 13:52:55 -0700197 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700198}
199
Tom Cherry96f67312015-07-30 13:52:55 -0700200int do_enable(const std::vector<std::string>& args)
JP Abgrall3beec7e2014-05-02 21:14:29 -0700201{
202 struct service *svc;
Tom Cherry96f67312015-07-30 13:52:55 -0700203 svc = service_find_by_name(args[1].c_str());
JP Abgrall3beec7e2014-05-02 21:14:29 -0700204 if (svc) {
205 svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED);
206 if (svc->flags & SVC_DISABLED_START) {
207 service_start(svc, NULL);
208 }
209 } else {
210 return -1;
211 }
212 return 0;
213}
214
Tom Cherry96f67312015-07-30 13:52:55 -0700215int do_exec(const std::vector<std::string>& args) {
216 std::vector<char*> strs;
217 strs.reserve(args.size());
218 for (const auto& s : args) {
219 strs.push_back(const_cast<char*>(s.c_str()));
220 }
221 service* svc = make_exec_oneshot_service(strs.size(), &strs[0]);
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800222 if (svc == NULL) {
223 return -1;
224 }
225 service_start(svc, NULL);
226 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Tom Cherry96f67312015-07-30 13:52:55 -0700229int do_export(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230{
Tom Cherry96f67312015-07-30 13:52:55 -0700231 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232}
233
Tom Cherry96f67312015-07-30 13:52:55 -0700234int do_hostname(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235{
Tom Cherry96f67312015-07-30 13:52:55 -0700236 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237}
238
Tom Cherry96f67312015-07-30 13:52:55 -0700239int do_ifup(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240{
Tom Cherry96f67312015-07-30 13:52:55 -0700241 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242}
243
Tom Cherry96f67312015-07-30 13:52:55 -0700244int do_insmod(const std::vector<std::string>& args)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800245{
Tom Cherry96f67312015-07-30 13:52:55 -0700246 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800247
Tom Cherry96f67312015-07-30 13:52:55 -0700248 if (args.size() > 2) {
249 options += args[2];
250 for (std::size_t i = 3; i < args.size(); ++i) {
251 options += ' ';
252 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800253 }
254 }
255
Tom Cherry96f67312015-07-30 13:52:55 -0700256 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800257}
258
Tom Cherry96f67312015-07-30 13:52:55 -0700259int do_mkdir(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260{
261 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700262 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263
264 /* mkdir <path> [mode] [owner] [group] */
265
Tom Cherry96f67312015-07-30 13:52:55 -0700266 if (args.size() >= 3) {
267 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268 }
269
Tom Cherry96f67312015-07-30 13:52:55 -0700270 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700271 /* chmod in case the directory already exists */
272 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700273 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700274 }
275 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276 return -errno;
277 }
278
Tom Cherry96f67312015-07-30 13:52:55 -0700279 if (args.size() >= 4) {
280 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281 gid_t gid = -1;
282
Tom Cherry96f67312015-07-30 13:52:55 -0700283 if (args.size() == 5) {
284 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 }
286
Tom Cherry96f67312015-07-30 13:52:55 -0700287 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 return -errno;
289 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700290
291 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
292 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700293 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700294 if (ret == -1) {
295 return -errno;
296 }
297 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700298 }
299
Tom Cherry96f67312015-07-30 13:52:55 -0700300 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301}
302
303static struct {
304 const char *name;
305 unsigned flag;
306} mount_flags[] = {
307 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200308 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 { "nosuid", MS_NOSUID },
310 { "nodev", MS_NODEV },
311 { "nodiratime", MS_NODIRATIME },
312 { "ro", MS_RDONLY },
313 { "rw", 0 },
314 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700315 { "bind", MS_BIND },
316 { "rec", MS_REC },
317 { "unbindable", MS_UNBINDABLE },
318 { "private", MS_PRIVATE },
319 { "slave", MS_SLAVE },
320 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 { "defaults", 0 },
322 { 0, 0 },
323};
324
Ken Sumrall752923c2010-12-03 16:33:31 -0800325#define DATA_MNT_POINT "/data"
326
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherry96f67312015-07-30 13:52:55 -0700328int do_mount(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700329{
330 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700331 const char *source, *target, *system;
332 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700334 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700335 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700336 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337
Tom Cherry96f67312015-07-30 13:52:55 -0700338 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700339 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700340 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700341 flags |= mount_flags[i].flag;
342 break;
343 }
344 }
345
Colin Crosscd0f1732010-04-19 17:10:24 -0700346 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700347 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700348 wait = 1;
349 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700350 else if (na + 1 == args.size())
351 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700352 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353 }
354
Tom Cherry96f67312015-07-30 13:52:55 -0700355 system = args[1].c_str();
356 source = args[2].c_str();
357 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000358
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 if (!strncmp(source, "mtd@", 4)) {
360 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000361 if (n < 0) {
362 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364
Yabin Cuie2d63af2015-02-17 19:27:51 -0800365 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366
Colin Crosscd0f1732010-04-19 17:10:24 -0700367 if (wait)
368 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000369 if (mount(tmp, target, system, flags, options) < 0) {
370 return -1;
371 }
372
Ken Sumralldd4d7862011-02-17 18:09:47 -0800373 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000374 } else if (!strncmp(source, "loop@", 5)) {
375 int mode, loop, fd;
376 struct loop_info info;
377
378 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800379 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000380 if (fd < 0) {
381 return -1;
382 }
383
384 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800385 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800386 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000387 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100388 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000389 return -1;
390 }
391
392 /* if it is a blank loop device */
393 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
394 /* if it becomes our loop device */
395 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
396 close(fd);
397
398 if (mount(tmp, target, system, flags, options) < 0) {
399 ioctl(loop, LOOP_CLR_FD, 0);
400 close(loop);
401 return -1;
402 }
403
404 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800405 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000406 }
407 }
408
409 close(loop);
410 }
411
412 close(fd);
413 ERROR("out of loopback devices");
414 return -1;
415 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700416 if (wait)
417 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700419 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000420 }
421
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700422 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800423
424exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800425 return 0;
426
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427}
428
JP Abgrallcee20682014-07-02 14:26:54 -0700429static int wipe_data_via_recovery()
430{
431 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800432 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700433 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700434 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
435 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700436 close(fd);
437 } else {
438 ERROR("could not open /cache/recovery/command\n");
439 return -1;
440 }
441 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
442 while (1) { pause(); } // never reached
443}
444
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000445/*
JP Abgrallcee20682014-07-02 14:26:54 -0700446 * This function might request a reboot, in which case it will
447 * not return.
448 */
Tom Cherry96f67312015-07-30 13:52:55 -0700449int do_mount_all(const std::vector<std::string>& args)
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700450{
451 pid_t pid;
452 int ret = -1;
453 int child_ret = -1;
454 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800455 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700456
Tom Cherry96f67312015-07-30 13:52:55 -0700457 if (args.size() != 2) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700458 return -1;
459 }
Tom Cherry96f67312015-07-30 13:52:55 -0700460 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700461 /*
462 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
463 * do the call in the child to provide protection to the main init
464 * process if anything goes wrong (crash or memory leak), and wait for
465 * the child to finish in the parent.
466 */
467 pid = fork();
468 if (pid > 0) {
469 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700470 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
471 if (wp_ret < 0) {
472 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700473 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700474 }
475
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700476 if (WIFEXITED(status)) {
477 ret = WEXITSTATUS(status);
478 } else {
479 ret = -1;
480 }
481 } else if (pid == 0) {
482 /* child, call fs_mgr_mount_all() */
483 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800484 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800485 child_ret = fs_mgr_mount_all(fstab);
486 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700487 if (child_ret == -1) {
488 ERROR("fs_mgr_mount_all returned an error\n");
489 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700490 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700491 } else {
492 /* fork failed, return an error */
493 return -1;
494 }
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{
570 struct service *svc;
Tom Cherry96f67312015-07-30 13:52:55 -0700571 svc = service_find_by_name(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 if (svc) {
San Mehatf24e2522009-05-19 13:30:46 -0700573 service_start(svc, NULL);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700574 }
575 return 0;
576}
577
Tom Cherry96f67312015-07-30 13:52:55 -0700578int do_stop(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579{
580 struct service *svc;
Tom Cherry96f67312015-07-30 13:52:55 -0700581 svc = service_find_by_name(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700582 if (svc) {
583 service_stop(svc);
584 }
585 return 0;
586}
587
Tom Cherry96f67312015-07-30 13:52:55 -0700588int do_restart(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589{
590 struct service *svc;
Tom Cherry96f67312015-07-30 13:52:55 -0700591 svc = service_find_by_name(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 if (svc) {
Mike Kasickb54f39f2012-01-25 23:48:46 -0500593 service_restart(svc);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700594 }
595 return 0;
596}
597
Tom Cherry96f67312015-07-30 13:52:55 -0700598int do_powerctl(const std::vector<std::string>& args)
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700599{
Tom Cherry96f67312015-07-30 13:52:55 -0700600 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700601 int len = 0;
602 int cmd = 0;
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800603 const char *reboot_target;
Yusuke Sato0df08272015-07-08 14:57:07 -0700604 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700605
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700606 if (strncmp(command, "shutdown", 8) == 0) {
607 cmd = ANDROID_RB_POWEROFF;
608 len = 8;
Yusuke Sato0df08272015-07-08 14:57:07 -0700609 callback_on_ro_remount = unmount_and_fsck;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700610 } else if (strncmp(command, "reboot", 6) == 0) {
611 cmd = ANDROID_RB_RESTART2;
612 len = 6;
613 } else {
614 ERROR("powerctl: unrecognized command '%s'\n", command);
615 return -EINVAL;
616 }
617
618 if (command[len] == ',') {
619 reboot_target = &command[len + 1];
620 } else if (command[len] == '\0') {
621 reboot_target = "";
622 } else {
623 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
624 return -EINVAL;
625 }
626
Yusuke Sato0df08272015-07-08 14:57:07 -0700627 return android_reboot_with_callback(cmd, 0, reboot_target,
628 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700629}
630
Tom Cherry96f67312015-07-30 13:52:55 -0700631int do_trigger(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700632{
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700633 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700634 return 0;
635}
636
Tom Cherry96f67312015-07-30 13:52:55 -0700637int do_symlink(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700638{
Tom Cherry96f67312015-07-30 13:52:55 -0700639 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640}
641
Tom Cherry96f67312015-07-30 13:52:55 -0700642int do_rm(const std::vector<std::string>& args)
Ken Sumrall203bad52011-01-18 17:37:41 -0800643{
Tom Cherry96f67312015-07-30 13:52:55 -0700644 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800645}
646
Tom Cherry96f67312015-07-30 13:52:55 -0700647int do_rmdir(const std::vector<std::string>& args)
Ken Sumrall203bad52011-01-18 17:37:41 -0800648{
Tom Cherry96f67312015-07-30 13:52:55 -0700649 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800650}
651
Tom Cherry96f67312015-07-30 13:52:55 -0700652int do_sysclktz(const std::vector<std::string>& args)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800653{
654 struct timezone tz;
655
Tom Cherry96f67312015-07-30 13:52:55 -0700656 if (args.size() != 2)
The Android Open Source Project35237d12008-12-17 18:08:08 -0800657 return -1;
658
659 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700660 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800661 if (settimeofday(NULL, &tz))
662 return -1;
663 return 0;
664}
665
Tom Cherry96f67312015-07-30 13:52:55 -0700666int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700667 int mode = -1;
668 int rc = fs_mgr_load_verity_state(&mode);
669 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700670 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000671 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700672 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000673}
674
Sami Tolvanen45474232015-03-30 11:38:38 +0100675static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) {
676 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
677 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000678}
679
Tom Cherry96f67312015-07-30 13:52:55 -0700680int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700681 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000682}
683
Tom Cherry96f67312015-07-30 13:52:55 -0700684int do_write(const std::vector<std::string>& args)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700685{
Tom Cherry96f67312015-07-30 13:52:55 -0700686 const char* path = args[1].c_str();
687 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700688 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700689}
690
Tom Cherry96f67312015-07-30 13:52:55 -0700691int do_copy(const std::vector<std::string>& args)
San Mehat7c44fe52009-08-26 16:39:25 -0700692{
693 char *buffer = NULL;
694 int rc = 0;
695 int fd1 = -1, fd2 = -1;
696 struct stat info;
697 int brtw, brtr;
698 char *p;
699
Tom Cherry96f67312015-07-30 13:52:55 -0700700 if (args.size() != 3)
San Mehat7c44fe52009-08-26 16:39:25 -0700701 return -1;
702
Tom Cherry96f67312015-07-30 13:52:55 -0700703 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700704 return -1;
705
Tom Cherry96f67312015-07-30 13:52:55 -0700706 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700707 goto out_err;
708
Tom Cherry96f67312015-07-30 13:52:55 -0700709 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700710 goto out_err;
711
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800712 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700713 goto out_err;
714
715 p = buffer;
716 brtr = info.st_size;
717 while(brtr) {
718 rc = read(fd1, p, brtr);
719 if (rc < 0)
720 goto out_err;
721 if (rc == 0)
722 break;
723 p += rc;
724 brtr -= rc;
725 }
726
727 p = buffer;
728 brtw = info.st_size;
729 while(brtw) {
730 rc = write(fd2, p, brtw);
731 if (rc < 0)
732 goto out_err;
733 if (rc == 0)
734 break;
735 p += rc;
736 brtw -= rc;
737 }
738
739 rc = 0;
740 goto out;
741out_err:
742 rc = -1;
743out:
744 if (buffer)
745 free(buffer);
746 if (fd1 >= 0)
747 close(fd1);
748 if (fd2 >= 0)
749 close(fd2);
750 return rc;
751}
752
Tom Cherry96f67312015-07-30 13:52:55 -0700753int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700754 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700755 if (args.size() == 3) {
756 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700758 } else if (args.size() == 4) {
759 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
760 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700761 return -errno;
762 } else {
763 return -1;
764 }
765 return 0;
766}
767
768static mode_t get_mode(const char *s) {
769 mode_t mode = 0;
770 while (*s) {
771 if (*s >= '0' && *s <= '7') {
772 mode = (mode<<3) | (*s-'0');
773 } else {
774 return -1;
775 }
776 s++;
777 }
778 return mode;
779}
780
Tom Cherry96f67312015-07-30 13:52:55 -0700781int do_chmod(const std::vector<std::string>& args) {
782 mode_t mode = get_mode(args[1].c_str());
783 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 return -errno;
785 }
786 return 0;
787}
788
Tom Cherry96f67312015-07-30 13:52:55 -0700789int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400790 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500791
Tom Cherry96f67312015-07-30 13:52:55 -0700792 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
793 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400794 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500795 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400796 return ret;
797}
798
Tom Cherry96f67312015-07-30 13:52:55 -0700799int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400800 int ret = 0;
801
Tom Cherry96f67312015-07-30 13:52:55 -0700802 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
803 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400804 ret = -errno;
805 }
806 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807}
808
Tom Cherry96f67312015-07-30 13:52:55 -0700809int do_loglevel(const std::vector<std::string>& args) {
810 if (args.size() != 2) {
Riley Andrews1bbef882014-06-26 13:55:03 -0700811 ERROR("loglevel: missing argument\n");
812 return -EINVAL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 }
Riley Andrews1bbef882014-06-26 13:55:03 -0700814
Tom Cherry96f67312015-07-30 13:52:55 -0700815 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700816 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
817 ERROR("loglevel: invalid log level'%d'\n", log_level);
818 return -EINVAL;
819 }
820 klog_set_level(log_level);
821 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822}
823
Tom Cherry96f67312015-07-30 13:52:55 -0700824int do_load_persist_props(const std::vector<std::string>& args) {
825 if (args.size() == 1) {
Ken Sumrallc5c51032011-03-08 17:01:29 -0800826 load_persist_props();
827 return 0;
828 }
829 return -1;
830}
831
Tom Cherry96f67312015-07-30 13:52:55 -0700832int do_load_all_props(const std::vector<std::string>& args) {
833 if (args.size() == 1) {
Riley Andrewse4b7b292014-06-16 15:06:21 -0700834 load_all_props();
835 return 0;
836 }
837 return -1;
838}
839
Tom Cherry96f67312015-07-30 13:52:55 -0700840int do_wait(const std::vector<std::string>& args)
Colin Crosscd0f1732010-04-19 17:10:24 -0700841{
Tom Cherry96f67312015-07-30 13:52:55 -0700842 if (args.size() == 2) {
843 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
844 } else if (args.size() == 3) {
845 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800846 } else
847 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700848}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000849
Paul Lawrence806d10b2015-04-28 22:07:10 +0000850/*
851 * Callback to make a directory from the ext4 code
852 */
853static int do_installkeys_ensure_dir_exists(const char* dir)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000854{
Paul Lawrence806d10b2015-04-28 22:07:10 +0000855 if (make_dir(dir, 0700) && errno != EEXIST) {
856 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000857 }
858
Paul Lawrence806d10b2015-04-28 22:07:10 +0000859 return 0;
860}
861
Tom Cherry96f67312015-07-30 13:52:55 -0700862int do_installkey(const std::vector<std::string>& args)
Paul Lawrence806d10b2015-04-28 22:07:10 +0000863{
Tom Cherry96f67312015-07-30 13:52:55 -0700864 if (args.size() != 2) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000865 return -1;
866 }
867
Yabin Cui74edcea2015-07-24 10:11:05 -0700868 std::string prop_value = property_get("ro.crypto.type");
869 if (prop_value != "file") {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000870 return 0;
871 }
872
Tom Cherry96f67312015-07-30 13:52:55 -0700873 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000874 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000875}