blob: 3ffa2e82c50340f479e495a64f09da4741ac9b01 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tom Cherryb7349902015-08-26 11:43:36 -070017#include "builtins.h"
18
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070020#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070021#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070023#include <signal.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070024#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <string.h>
27#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/mount.h>
29#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080030#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070031#include <sys/types.h>
32#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070033#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070034#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000035#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000036#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037
Stephen Smalleye46f9d52012-01-13 08:48:47 -050038#include <selinux/selinux.h>
39#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040
Elliott Hughesdb3f2672015-03-20 09:45:18 -070041#include <fs_mgr.h>
42#include <base/stringprintf.h>
43#include <cutils/partition_utils.h>
44#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070045#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070046#include <private/android_filesystem_config.h>
47
Tom Cherryfa0c21c2015-07-23 17:53:11 -070048#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070049#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070051#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070052#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070053#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070054#include "property_service.h"
55#include "service.h"
56#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057
Nick Kralevichbc609542015-01-31 21:39:46 -080058#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070059#define UNMOUNT_CHECK_MS 5000
60#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080061
Elliott Hughesf3cf4382015-02-03 17:12:07 -080062// System call provided by bionic but not in any header file.
63extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064
Tom Cherryb7349902015-08-26 11:43:36 -070065static int insmod(const char *filename, const char *options) {
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
Tom Cherryb7349902015-08-26 11:43:36 -070075static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 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
Tom Cherryb7349902015-08-26 11:43:36 -0700102static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700103 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
104 return;
105
106 /* First, lazily unmount the directory. This unmount request finishes when
107 * all processes that open a file or directory in |entry->mnt_dir| exit.
108 */
109 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
110
111 /* Next, kill all processes except init, kthreadd, and kthreadd's
112 * children to finish the lazy unmount. Killing all processes here is okay
113 * because this callback function is only called right before reboot().
114 * It might be cleaner to selectively kill processes that actually use
115 * |entry->mnt_dir| rather than killing all, probably by reusing a function
116 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
117 * not allow init to scan /proc/<pid> files which the utility function
118 * heavily relies on. The policy does not allow the process to execute
119 * killall/pkill binaries either. Note that some processes might
120 * automatically restart after kill(), but that is not really a problem
121 * because |entry->mnt_dir| is no longer visible to such new processes.
122 */
Tom Cherrybac32992015-07-31 12:45:25 -0700123 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700124 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
125
126 int count = 0;
127 while (count++ < UNMOUNT_CHECK_TIMES) {
128 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
129 if (fd >= 0) {
130 /* |entry->mnt_dir| has sucessfully been unmounted. */
131 close(fd);
132 break;
133 } else if (errno == EBUSY) {
134 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
135 * while then retry.
136 */
137 TEMP_FAILURE_RETRY(
138 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
139 continue;
140 } else {
141 /* Cannot open the device. Give up. */
142 return;
143 }
144 }
145
146 int st;
147 if (!strcmp(entry->mnt_type, "f2fs")) {
148 const char *f2fs_argv[] = {
149 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
150 };
151 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700152 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700153 } else if (!strcmp(entry->mnt_type, "ext4")) {
154 const char *ext4_argv[] = {
155 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
156 };
157 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700158 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700159 }
160}
161
Tom Cherryb7349902015-08-26 11:43:36 -0700162static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700163 /* Starting a class does not start services
164 * which are explicitly disabled. They must
165 * be started individually.
166 */
Tom Cherrybac32992015-07-31 12:45:25 -0700167 ServiceManager::GetInstance().
168 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169 return 0;
170}
171
Tom Cherryb7349902015-08-26 11:43:36 -0700172static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700173 ServiceManager::GetInstance().
174 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 return 0;
176}
177
Tom Cherryb7349902015-08-26 11:43:36 -0700178static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700179 ServiceManager::GetInstance().
180 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800181 return 0;
182}
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700185 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700186}
187
Tom Cherryb7349902015-08-26 11:43:36 -0700188static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700189 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
190 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700191 return -1;
192 }
Tom Cherrybac32992015-07-31 12:45:25 -0700193 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700194}
195
Tom Cherryb7349902015-08-26 11:43:36 -0700196static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700197 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
198 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800199 return -1;
200 }
Tom Cherrybac32992015-07-31 12:45:25 -0700201 if (!svc->Start()) {
202 return -1;
203 }
204 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800205 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206}
207
Tom Cherryb7349902015-08-26 11:43:36 -0700208static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700209 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210}
211
Tom Cherryb7349902015-08-26 11:43:36 -0700212static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700213 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214}
215
Tom Cherryb7349902015-08-26 11:43:36 -0700216static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700217 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218}
219
Tom Cherryb7349902015-08-26 11:43:36 -0700220static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700221 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800222
Tom Cherry96f67312015-07-30 13:52:55 -0700223 if (args.size() > 2) {
224 options += args[2];
225 for (std::size_t i = 3; i < args.size(); ++i) {
226 options += ' ';
227 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800228 }
229 }
230
Tom Cherry96f67312015-07-30 13:52:55 -0700231 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800232}
233
Tom Cherryb7349902015-08-26 11:43:36 -0700234static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700236 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237
238 /* mkdir <path> [mode] [owner] [group] */
239
Tom Cherry96f67312015-07-30 13:52:55 -0700240 if (args.size() >= 3) {
241 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242 }
243
Tom Cherry96f67312015-07-30 13:52:55 -0700244 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700245 /* chmod in case the directory already exists */
246 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700247 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700248 }
249 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250 return -errno;
251 }
252
Tom Cherry96f67312015-07-30 13:52:55 -0700253 if (args.size() >= 4) {
254 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255 gid_t gid = -1;
256
Tom Cherry96f67312015-07-30 13:52:55 -0700257 if (args.size() == 5) {
258 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259 }
260
Tom Cherry96f67312015-07-30 13:52:55 -0700261 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262 return -errno;
263 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700264
265 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
266 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700267 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700268 if (ret == -1) {
269 return -errno;
270 }
271 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272 }
273
Tom Cherry96f67312015-07-30 13:52:55 -0700274 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275}
276
277static struct {
278 const char *name;
279 unsigned flag;
280} mount_flags[] = {
281 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200282 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283 { "nosuid", MS_NOSUID },
284 { "nodev", MS_NODEV },
285 { "nodiratime", MS_NODIRATIME },
286 { "ro", MS_RDONLY },
287 { "rw", 0 },
288 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700289 { "bind", MS_BIND },
290 { "rec", MS_REC },
291 { "unbindable", MS_UNBINDABLE },
292 { "private", MS_PRIVATE },
293 { "slave", MS_SLAVE },
294 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 { "defaults", 0 },
296 { 0, 0 },
297};
298
Ken Sumrall752923c2010-12-03 16:33:31 -0800299#define DATA_MNT_POINT "/data"
300
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700302static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700304 const char *source, *target, *system;
305 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700307 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700309 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310
Tom Cherry96f67312015-07-30 13:52:55 -0700311 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700313 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 flags |= mount_flags[i].flag;
315 break;
316 }
317 }
318
Colin Crosscd0f1732010-04-19 17:10:24 -0700319 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700320 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700321 wait = 1;
322 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700323 else if (na + 1 == args.size())
324 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700325 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 }
327
Tom Cherry96f67312015-07-30 13:52:55 -0700328 system = args[1].c_str();
329 source = args[2].c_str();
330 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000331
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332 if (!strncmp(source, "mtd@", 4)) {
333 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000334 if (n < 0) {
335 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000337
Yabin Cuie2d63af2015-02-17 19:27:51 -0800338 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000339
Colin Crosscd0f1732010-04-19 17:10:24 -0700340 if (wait)
341 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000342 if (mount(tmp, target, system, flags, options) < 0) {
343 return -1;
344 }
345
Ken Sumralldd4d7862011-02-17 18:09:47 -0800346 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000347 } else if (!strncmp(source, "loop@", 5)) {
348 int mode, loop, fd;
349 struct loop_info info;
350
351 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800352 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000353 if (fd < 0) {
354 return -1;
355 }
356
357 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800358 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800359 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000360 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100361 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 return -1;
363 }
364
365 /* if it is a blank loop device */
366 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
367 /* if it becomes our loop device */
368 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
369 close(fd);
370
371 if (mount(tmp, target, system, flags, options) < 0) {
372 ioctl(loop, LOOP_CLR_FD, 0);
373 close(loop);
374 return -1;
375 }
376
377 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800378 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000379 }
380 }
381
382 close(loop);
383 }
384
385 close(fd);
386 ERROR("out of loopback devices");
387 return -1;
388 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700389 if (wait)
390 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700392 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393 }
394
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800396
397exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800398 return 0;
399
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700400}
401
Tom Cherryb7349902015-08-26 11:43:36 -0700402static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700403 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800404 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700405 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700406 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
407 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700408 close(fd);
409 } else {
410 ERROR("could not open /cache/recovery/command\n");
411 return -1;
412 }
413 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
414 while (1) { pause(); } // never reached
415}
416
Tom Cherryb7349902015-08-26 11:43:36 -0700417static void import_late() {
Tom Cherryb8dd0272015-07-22 14:23:33 -0700418 static const std::vector<std::string> init_directories = {
419 "/system/etc/init",
420 "/vendor/etc/init",
421 "/odm/etc/init"
422 };
423
Tom Cherryb7349902015-08-26 11:43:36 -0700424 Parser& parser = Parser::GetInstance();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700425 for (const auto& dir : init_directories) {
Tom Cherryb7349902015-08-26 11:43:36 -0700426 parser.ParseConfig(dir.c_str());
Tom Cherryb8dd0272015-07-22 14:23:33 -0700427 }
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 Cherryb7349902015-08-26 11:43:36 -0700434static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700435 pid_t pid;
436 int ret = -1;
437 int child_ret = -1;
438 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800439 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700440
Tom Cherry96f67312015-07-30 13:52:55 -0700441 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700442 /*
443 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
444 * do the call in the child to provide protection to the main init
445 * process if anything goes wrong (crash or memory leak), and wait for
446 * the child to finish in the parent.
447 */
448 pid = fork();
449 if (pid > 0) {
450 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700451 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
452 if (wp_ret < 0) {
453 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700454 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700455 }
456
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700457 if (WIFEXITED(status)) {
458 ret = WEXITSTATUS(status);
459 } else {
460 ret = -1;
461 }
462 } else if (pid == 0) {
463 /* child, call fs_mgr_mount_all() */
464 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800465 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800466 child_ret = fs_mgr_mount_all(fstab);
467 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700468 if (child_ret == -1) {
469 ERROR("fs_mgr_mount_all returned an error\n");
470 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700471 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700472 } else {
473 /* fork failed, return an error */
474 return -1;
475 }
476
Tom Cherryb8dd0272015-07-22 14:23:33 -0700477 import_late();
478
JP Abgrallf22b7452014-07-02 13:16:04 -0700479 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800480 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700481 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700482 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000483 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800484 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700485 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700486 property_set("ro.crypto.state", "unencrypted");
487 /* If fs_mgr determined this is an unencrypted device, then trigger
488 * that action.
489 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700490 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700491 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
492 /* Setup a wipe via recovery, and reboot into recovery */
493 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
494 ret = wipe_data_via_recovery();
495 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000496 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000497 if (e4crypt_install_keyring()) {
498 return -1;
499 }
500 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000501 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000502
503 // Although encrypted, we have device key, so we do not need to
504 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700505 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000506 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
507 if (e4crypt_install_keyring()) {
508 return -1;
509 }
510 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000511 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000512 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700513 } else if (ret > 0) {
514 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700515 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700516 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700517
518 return ret;
519}
520
Tom Cherryb7349902015-08-26 11:43:36 -0700521static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700522 struct fstab *fstab;
523 int ret;
524
Tom Cherry96f67312015-07-30 13:52:55 -0700525 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700526 ret = fs_mgr_swapon_all(fstab);
527 fs_mgr_free_fstab(fstab);
528
529 return ret;
530}
531
Tom Cherryb7349902015-08-26 11:43:36 -0700532static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700533 const char* name = args[1].c_str();
534 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700535 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700536 return 0;
537}
538
Tom Cherryb7349902015-08-26 11:43:36 -0700539static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700540 struct rlimit limit;
541 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700542 resource = std::stoi(args[1]);
543 limit.rlim_cur = std::stoi(args[2]);
544 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700545 return setrlimit(resource, &limit);
546}
547
Tom Cherryb7349902015-08-26 11:43:36 -0700548static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700549 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
550 if (!svc) {
551 ERROR("do_start: Service %s not found\n", args[1].c_str());
552 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700553 }
Tom Cherrybac32992015-07-31 12:45:25 -0700554 if (!svc->Start())
555 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700556 return 0;
557}
558
Tom Cherryb7349902015-08-26 11:43:36 -0700559static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700560 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
561 if (!svc) {
562 ERROR("do_stop: Service %s not found\n", args[1].c_str());
563 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700564 }
Tom Cherrybac32992015-07-31 12:45:25 -0700565 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700566 return 0;
567}
568
Tom Cherryb7349902015-08-26 11:43:36 -0700569static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700570 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
571 if (!svc) {
572 ERROR("do_restart: 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 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700576 return 0;
577}
578
Tom Cherryb7349902015-08-26 11:43:36 -0700579static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700580 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700581 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700582 unsigned int cmd = 0;
583 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700584 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700585
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700586 if (strncmp(command, "shutdown", 8) == 0) {
587 cmd = ANDROID_RB_POWEROFF;
588 len = 8;
589 } else if (strncmp(command, "reboot", 6) == 0) {
590 cmd = ANDROID_RB_RESTART2;
591 len = 6;
592 } else {
593 ERROR("powerctl: unrecognized command '%s'\n", command);
594 return -EINVAL;
595 }
596
597 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700598 if (cmd == ANDROID_RB_POWEROFF &&
599 !strcmp(&command[len + 1], "userrequested")) {
600 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
601 // Run fsck once the file system is remounted in read-only mode.
602 callback_on_ro_remount = unmount_and_fsck;
603 } else if (cmd == ANDROID_RB_RESTART2) {
604 reboot_target = &command[len + 1];
605 }
606 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700607 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
608 return -EINVAL;
609 }
610
Yusuke Sato0df08272015-07-08 14:57:07 -0700611 return android_reboot_with_callback(cmd, 0, reboot_target,
612 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700613}
614
Tom Cherryb7349902015-08-26 11:43:36 -0700615static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700616 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 return 0;
618}
619
Tom Cherryb7349902015-08-26 11:43:36 -0700620static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700621 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700622}
623
Tom Cherryb7349902015-08-26 11:43:36 -0700624static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700625 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800626}
627
Tom Cherryb7349902015-08-26 11:43:36 -0700628static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700629 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800630}
631
Tom Cherryb7349902015-08-26 11:43:36 -0700632static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800633 struct timezone tz;
634
The Android Open Source Project35237d12008-12-17 18:08:08 -0800635 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700636 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800637 if (settimeofday(NULL, &tz))
638 return -1;
639 return 0;
640}
641
Tom Cherryb7349902015-08-26 11:43:36 -0700642static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700643 int mode = -1;
644 int rc = fs_mgr_load_verity_state(&mode);
645 if (rc == 0 && mode == VERITY_MODE_LOGGING) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700646 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000647 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700648 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000649}
650
Tom Cherryb7349902015-08-26 11:43:36 -0700651static void verity_update_property(fstab_rec *fstab, const char *mount_point,
652 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100653 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
654 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000655}
656
Tom Cherryb7349902015-08-26 11:43:36 -0700657static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700658 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000659}
660
Tom Cherryb7349902015-08-26 11:43:36 -0700661static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700662 const char* path = args[1].c_str();
663 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700664 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700665}
666
Tom Cherryb7349902015-08-26 11:43:36 -0700667static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700668 char *buffer = NULL;
669 int rc = 0;
670 int fd1 = -1, fd2 = -1;
671 struct stat info;
672 int brtw, brtr;
673 char *p;
674
Tom Cherry96f67312015-07-30 13:52:55 -0700675 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700676 return -1;
677
Tom Cherry96f67312015-07-30 13:52:55 -0700678 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700679 goto out_err;
680
Tom Cherry96f67312015-07-30 13:52:55 -0700681 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700682 goto out_err;
683
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800684 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700685 goto out_err;
686
687 p = buffer;
688 brtr = info.st_size;
689 while(brtr) {
690 rc = read(fd1, p, brtr);
691 if (rc < 0)
692 goto out_err;
693 if (rc == 0)
694 break;
695 p += rc;
696 brtr -= rc;
697 }
698
699 p = buffer;
700 brtw = info.st_size;
701 while(brtw) {
702 rc = write(fd2, p, brtw);
703 if (rc < 0)
704 goto out_err;
705 if (rc == 0)
706 break;
707 p += rc;
708 brtw -= rc;
709 }
710
711 rc = 0;
712 goto out;
713out_err:
714 rc = -1;
715out:
716 if (buffer)
717 free(buffer);
718 if (fd1 >= 0)
719 close(fd1);
720 if (fd2 >= 0)
721 close(fd2);
722 return rc;
723}
724
Tom Cherryb7349902015-08-26 11:43:36 -0700725static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700726 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700727 if (args.size() == 3) {
728 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700729 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700730 } else if (args.size() == 4) {
731 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
732 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700733 return -errno;
734 } else {
735 return -1;
736 }
737 return 0;
738}
739
740static mode_t get_mode(const char *s) {
741 mode_t mode = 0;
742 while (*s) {
743 if (*s >= '0' && *s <= '7') {
744 mode = (mode<<3) | (*s-'0');
745 } else {
746 return -1;
747 }
748 s++;
749 }
750 return mode;
751}
752
Tom Cherryb7349902015-08-26 11:43:36 -0700753static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700754 mode_t mode = get_mode(args[1].c_str());
755 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700756 return -errno;
757 }
758 return 0;
759}
760
Tom Cherryb7349902015-08-26 11:43:36 -0700761static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400762 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500763
Tom Cherry96f67312015-07-30 13:52:55 -0700764 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
765 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400766 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500767 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400768 return ret;
769}
770
Tom Cherryb7349902015-08-26 11:43:36 -0700771static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400772 int ret = 0;
773
Tom Cherry96f67312015-07-30 13:52:55 -0700774 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
775 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400776 ret = -errno;
777 }
778 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500779}
780
Tom Cherryb7349902015-08-26 11:43:36 -0700781static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700782 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700783 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
784 ERROR("loglevel: invalid log level'%d'\n", log_level);
785 return -EINVAL;
786 }
787 klog_set_level(log_level);
788 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789}
790
Tom Cherry96f67312015-07-30 13:52:55 -0700791int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700792 load_persist_props();
793 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800794}
795
Tom Cherryb7349902015-08-26 11:43:36 -0700796static int do_load_all_props(const std::vector<std::string>& args) {
797 load_all_props();
798 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700799}
800
Tom Cherryb7349902015-08-26 11:43:36 -0700801static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700802 if (args.size() == 2) {
803 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
804 } else if (args.size() == 3) {
805 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800806 } else
807 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700808}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000809
Paul Lawrence806d10b2015-04-28 22:07:10 +0000810/*
811 * Callback to make a directory from the ext4 code
812 */
Tom Cherryb7349902015-08-26 11:43:36 -0700813static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000814 if (make_dir(dir, 0700) && errno != EEXIST) {
815 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000816 }
817
Paul Lawrence806d10b2015-04-28 22:07:10 +0000818 return 0;
819}
820
Tom Cherryb7349902015-08-26 11:43:36 -0700821static int do_installkey(const std::vector<std::string>& args) {
Yabin Cui74edcea2015-07-24 10:11:05 -0700822 std::string prop_value = property_get("ro.crypto.type");
823 if (prop_value != "file") {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000824 return 0;
825 }
826
Tom Cherry96f67312015-07-30 13:52:55 -0700827 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000828 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000829}
Tom Cherryb7349902015-08-26 11:43:36 -0700830
831BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
832 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
833 static const Map builtin_functions = {
834 {"bootchart_init", {0, 0, do_bootchart_init}},
835 {"chmod", {2, 2, do_chmod}},
836 {"chown", {2, 3, do_chown}},
837 {"class_reset", {1, 1, do_class_reset}},
838 {"class_start", {1, 1, do_class_start}},
839 {"class_stop", {1, 1, do_class_stop}},
840 {"copy", {2, 2, do_copy}},
841 {"domainname", {1, 1, do_domainname}},
842 {"enable", {1, 1, do_enable}},
843 {"exec", {1, kMax, do_exec}},
844 {"export", {2, 2, do_export}},
845 {"hostname", {1, 1, do_hostname}},
846 {"ifup", {1, 1, do_ifup}},
847 {"insmod", {1, kMax, do_insmod}},
848 {"installkey", {1, 1, do_installkey}},
849 {"load_all_props", {0, 0, do_load_all_props}},
850 {"load_persist_props", {0, 0, do_load_persist_props}},
851 {"loglevel", {1, 1, do_loglevel}},
852 {"mkdir", {1, 4, do_mkdir}},
853 {"mount_all", {1, 1, do_mount_all}},
854 {"mount", {3, kMax, do_mount}},
855 {"powerctl", {1, 1, do_powerctl}},
856 {"restart", {1, 1, do_restart}},
857 {"restorecon", {1, kMax, do_restorecon}},
858 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
859 {"rm", {1, 1, do_rm}},
860 {"rmdir", {1, 1, do_rmdir}},
861 {"setprop", {2, 2, do_setprop}},
862 {"setrlimit", {3, 3, do_setrlimit}},
863 {"start", {1, 1, do_start}},
864 {"stop", {1, 1, do_stop}},
865 {"swapon_all", {1, 1, do_swapon_all}},
866 {"symlink", {2, 2, do_symlink}},
867 {"sysclktz", {1, 1, do_sysclktz}},
868 {"trigger", {1, 1, do_trigger}},
869 {"verity_load_state", {0, 0, do_verity_load_state}},
870 {"verity_update_state", {0, 0, do_verity_update_state}},
871 {"wait", {1, 2, do_wait}},
872 {"write", {2, 2, do_write}},
873 };
874 return builtin_functions;
875}