blob: d2291bb50156a3b1b8bc63dfd096b99ce753a893 [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>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080042#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080043#include <android-base/stringprintf.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070044#include <cutils/partition_utils.h>
45#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070046#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070047#include <private/android_filesystem_config.h>
48
Tom Cherryfa0c21c2015-07-23 17:53:11 -070049#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070050#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070052#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070053#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070054#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070055#include "property_service.h"
56#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080057#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070058#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070059
Nick Kralevichbc609542015-01-31 21:39:46 -080060#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070061#define UNMOUNT_CHECK_MS 5000
62#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080063
Elliott Hughesf3cf4382015-02-03 17:12:07 -080064// System call provided by bionic but not in any header file.
65extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080067static const int kTerminateServiceDelayMicroSeconds = 50000;
68
Tom Cherryb7349902015-08-26 11:43:36 -070069static int insmod(const char *filename, const char *options) {
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070070 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070071 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080073 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074
Elliott Hughesf682b472015-02-06 12:19:48 -080075 // TODO: use finit_module for >= 3.8 kernels.
76 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077}
78
Tom Cherryb7349902015-08-26 11:43:36 -070079static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080 struct ifreq ifr;
81 int s, ret;
82
83 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
84
85 s = socket(AF_INET, SOCK_DGRAM, 0);
86 if (s < 0)
87 return -1;
88
89 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
90 if (ret < 0) {
91 goto done;
92 }
93
94 if (up)
95 ifr.ifr_flags |= IFF_UP;
96 else
97 ifr.ifr_flags &= ~IFF_UP;
98
99 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800100
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101done:
102 close(s);
103 return ret;
104}
105
Tom Cherryb7349902015-08-26 11:43:36 -0700106static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700107 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
108 return;
109
110 /* First, lazily unmount the directory. This unmount request finishes when
111 * all processes that open a file or directory in |entry->mnt_dir| exit.
112 */
113 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
114
115 /* Next, kill all processes except init, kthreadd, and kthreadd's
116 * children to finish the lazy unmount. Killing all processes here is okay
117 * because this callback function is only called right before reboot().
118 * It might be cleaner to selectively kill processes that actually use
119 * |entry->mnt_dir| rather than killing all, probably by reusing a function
120 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
121 * not allow init to scan /proc/<pid> files which the utility function
122 * heavily relies on. The policy does not allow the process to execute
123 * killall/pkill binaries either. Note that some processes might
124 * automatically restart after kill(), but that is not really a problem
125 * because |entry->mnt_dir| is no longer visible to such new processes.
126 */
Tom Cherrybac32992015-07-31 12:45:25 -0700127 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700128 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
129
130 int count = 0;
131 while (count++ < UNMOUNT_CHECK_TIMES) {
132 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
133 if (fd >= 0) {
134 /* |entry->mnt_dir| has sucessfully been unmounted. */
135 close(fd);
136 break;
137 } else if (errno == EBUSY) {
138 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
139 * while then retry.
140 */
141 TEMP_FAILURE_RETRY(
142 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
143 continue;
144 } else {
145 /* Cannot open the device. Give up. */
146 return;
147 }
148 }
149
150 int st;
151 if (!strcmp(entry->mnt_type, "f2fs")) {
152 const char *f2fs_argv[] = {
153 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
154 };
155 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700156 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700157 } else if (!strcmp(entry->mnt_type, "ext4")) {
158 const char *ext4_argv[] = {
159 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
160 };
161 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700162 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700163 }
164}
165
Tom Cherryb7349902015-08-26 11:43:36 -0700166static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700167 /* Starting a class does not start services
168 * which are explicitly disabled. They must
169 * be started individually.
170 */
Tom Cherrybac32992015-07-31 12:45:25 -0700171 ServiceManager::GetInstance().
172 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700173 return 0;
174}
175
Tom Cherryb7349902015-08-26 11:43:36 -0700176static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700177 ServiceManager::GetInstance().
178 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700179 return 0;
180}
181
Tom Cherryb7349902015-08-26 11:43:36 -0700182static int do_class_reset(const std::vector<std::string>& args) {
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 Cherryb7349902015-08-26 11:43:36 -0700188static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700189 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190}
191
Tom Cherryb7349902015-08-26 11:43:36 -0700192static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700193 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
194 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700195 return -1;
196 }
Tom Cherrybac32992015-07-31 12:45:25 -0700197 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700198}
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700201 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
202 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800203 return -1;
204 }
Tom Cherrybac32992015-07-31 12:45:25 -0700205 if (!svc->Start()) {
206 return -1;
207 }
208 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800209 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700210}
211
Tom Cherryb7349902015-08-26 11:43:36 -0700212static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700213 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214}
215
Tom Cherryb7349902015-08-26 11:43:36 -0700216static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700217 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700218}
219
Tom Cherryb7349902015-08-26 11:43:36 -0700220static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700221 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222}
223
Tom Cherryb7349902015-08-26 11:43:36 -0700224static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700225 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800226
Tom Cherry96f67312015-07-30 13:52:55 -0700227 if (args.size() > 2) {
228 options += args[2];
229 for (std::size_t i = 3; i < args.size(); ++i) {
230 options += ' ';
231 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800232 }
233 }
234
Tom Cherry96f67312015-07-30 13:52:55 -0700235 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800236}
237
Tom Cherryb7349902015-08-26 11:43:36 -0700238static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700240 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241
242 /* mkdir <path> [mode] [owner] [group] */
243
Tom Cherry96f67312015-07-30 13:52:55 -0700244 if (args.size() >= 3) {
245 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700246 }
247
Tom Cherry96f67312015-07-30 13:52:55 -0700248 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700249 /* chmod in case the directory already exists */
250 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700251 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700252 }
253 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700254 return -errno;
255 }
256
Tom Cherry96f67312015-07-30 13:52:55 -0700257 if (args.size() >= 4) {
258 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259 gid_t gid = -1;
260
Tom Cherry96f67312015-07-30 13:52:55 -0700261 if (args.size() == 5) {
262 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263 }
264
Tom Cherry96f67312015-07-30 13:52:55 -0700265 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266 return -errno;
267 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700268
269 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
270 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700271 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700272 if (ret == -1) {
273 return -errno;
274 }
275 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276 }
277
Tom Cherry96f67312015-07-30 13:52:55 -0700278 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279}
280
281static struct {
282 const char *name;
283 unsigned flag;
284} mount_flags[] = {
285 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200286 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287 { "nosuid", MS_NOSUID },
288 { "nodev", MS_NODEV },
289 { "nodiratime", MS_NODIRATIME },
290 { "ro", MS_RDONLY },
291 { "rw", 0 },
292 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700293 { "bind", MS_BIND },
294 { "rec", MS_REC },
295 { "unbindable", MS_UNBINDABLE },
296 { "private", MS_PRIVATE },
297 { "slave", MS_SLAVE },
298 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 { "defaults", 0 },
300 { 0, 0 },
301};
302
Ken Sumrall752923c2010-12-03 16:33:31 -0800303#define DATA_MNT_POINT "/data"
304
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700306static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700308 const char *source, *target, *system;
309 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700311 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700313 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314
Tom Cherry96f67312015-07-30 13:52:55 -0700315 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700317 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 flags |= mount_flags[i].flag;
319 break;
320 }
321 }
322
Colin Crosscd0f1732010-04-19 17:10:24 -0700323 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700324 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700325 wait = 1;
326 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700327 else if (na + 1 == args.size())
328 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700329 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330 }
331
Tom Cherry96f67312015-07-30 13:52:55 -0700332 system = args[1].c_str();
333 source = args[2].c_str();
334 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000335
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 if (!strncmp(source, "mtd@", 4)) {
337 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000338 if (n < 0) {
339 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700340 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000341
Yabin Cuie2d63af2015-02-17 19:27:51 -0800342 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000343
Colin Crosscd0f1732010-04-19 17:10:24 -0700344 if (wait)
345 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000346 if (mount(tmp, target, system, flags, options) < 0) {
347 return -1;
348 }
349
Ken Sumralldd4d7862011-02-17 18:09:47 -0800350 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000351 } else if (!strncmp(source, "loop@", 5)) {
352 int mode, loop, fd;
353 struct loop_info info;
354
355 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800356 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000357 if (fd < 0) {
358 return -1;
359 }
360
361 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800362 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800363 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100365 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366 return -1;
367 }
368
369 /* if it is a blank loop device */
370 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
371 /* if it becomes our loop device */
372 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
373 close(fd);
374
375 if (mount(tmp, target, system, flags, options) < 0) {
376 ioctl(loop, LOOP_CLR_FD, 0);
377 close(loop);
378 return -1;
379 }
380
381 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800382 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000383 }
384 }
385
386 close(loop);
387 }
388
389 close(fd);
390 ERROR("out of loopback devices");
391 return -1;
392 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700393 if (wait)
394 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000395 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700396 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000397 }
398
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700399 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800400
401exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800402 return 0;
403
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700404}
405
Tom Cherryb7349902015-08-26 11:43:36 -0700406static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700407 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800408 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700409 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700410 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
411 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700412 close(fd);
413 } else {
414 ERROR("could not open /cache/recovery/command\n");
415 return -1;
416 }
417 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
418 while (1) { pause(); } // never reached
419}
420
Tom Cherryb7349902015-08-26 11:43:36 -0700421static void import_late() {
Tom Cherryb8dd0272015-07-22 14:23:33 -0700422 static const std::vector<std::string> init_directories = {
423 "/system/etc/init",
424 "/vendor/etc/init",
425 "/odm/etc/init"
426 };
427
Tom Cherryb7349902015-08-26 11:43:36 -0700428 Parser& parser = Parser::GetInstance();
Tom Cherryb8dd0272015-07-22 14:23:33 -0700429 for (const auto& dir : init_directories) {
Tom Cherryb7349902015-08-26 11:43:36 -0700430 parser.ParseConfig(dir.c_str());
Tom Cherryb8dd0272015-07-22 14:23:33 -0700431 }
432}
433
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000434/*
JP Abgrallcee20682014-07-02 14:26:54 -0700435 * This function might request a reboot, in which case it will
436 * not return.
437 */
Tom Cherryb7349902015-08-26 11:43:36 -0700438static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700439 pid_t pid;
440 int ret = -1;
441 int child_ret = -1;
442 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800443 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700444
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
Tom Cherryb8dd0272015-07-22 14:23:33 -0700481 import_late();
482
JP Abgrallf22b7452014-07-02 13:16:04 -0700483 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800484 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700485 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700486 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000487 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800488 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700489 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700490 property_set("ro.crypto.state", "unencrypted");
491 /* If fs_mgr determined this is an unencrypted device, then trigger
492 * that action.
493 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700494 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700495 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
496 /* Setup a wipe via recovery, and reboot into recovery */
497 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
498 ret = wipe_data_via_recovery();
499 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000500 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000501 if (e4crypt_install_keyring()) {
502 return -1;
503 }
504 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000505 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000506
507 // Although encrypted, we have device key, so we do not need to
508 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700509 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000510 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
511 if (e4crypt_install_keyring()) {
512 return -1;
513 }
514 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000515 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000516 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700517 } else if (ret > 0) {
518 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700520 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521
522 return ret;
523}
524
Tom Cherryb7349902015-08-26 11:43:36 -0700525static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700526 struct fstab *fstab;
527 int ret;
528
Tom Cherry96f67312015-07-30 13:52:55 -0700529 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700530 ret = fs_mgr_swapon_all(fstab);
531 fs_mgr_free_fstab(fstab);
532
533 return ret;
534}
535
Tom Cherryb7349902015-08-26 11:43:36 -0700536static int do_setprop(const std::vector<std::string>& args) {
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 Cherryb7349902015-08-26 11:43:36 -0700543static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700544 struct rlimit limit;
545 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700546 resource = std::stoi(args[1]);
547 limit.rlim_cur = std::stoi(args[2]);
548 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700549 return setrlimit(resource, &limit);
550}
551
Tom Cherryb7349902015-08-26 11:43:36 -0700552static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700553 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
554 if (!svc) {
555 ERROR("do_start: Service %s not found\n", args[1].c_str());
556 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700557 }
Tom Cherrybac32992015-07-31 12:45:25 -0700558 if (!svc->Start())
559 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700560 return 0;
561}
562
Tom Cherryb7349902015-08-26 11:43:36 -0700563static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700564 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
565 if (!svc) {
566 ERROR("do_stop: Service %s not found\n", args[1].c_str());
567 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700568 }
Tom Cherrybac32992015-07-31 12:45:25 -0700569 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700570 return 0;
571}
572
Tom Cherryb7349902015-08-26 11:43:36 -0700573static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700574 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
575 if (!svc) {
576 ERROR("do_restart: Service %s not found\n", args[1].c_str());
577 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700578 }
Tom Cherrybac32992015-07-31 12:45:25 -0700579 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700580 return 0;
581}
582
Tom Cherryb7349902015-08-26 11:43:36 -0700583static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700584 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700585 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700586 unsigned int cmd = 0;
587 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700588 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700589
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700590 if (strncmp(command, "shutdown", 8) == 0) {
591 cmd = ANDROID_RB_POWEROFF;
592 len = 8;
593 } else if (strncmp(command, "reboot", 6) == 0) {
594 cmd = ANDROID_RB_RESTART2;
595 len = 6;
596 } else {
597 ERROR("powerctl: unrecognized command '%s'\n", command);
598 return -EINVAL;
599 }
600
601 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700602 if (cmd == ANDROID_RB_POWEROFF &&
603 !strcmp(&command[len + 1], "userrequested")) {
604 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
605 // Run fsck once the file system is remounted in read-only mode.
606 callback_on_ro_remount = unmount_and_fsck;
607 } else if (cmd == ANDROID_RB_RESTART2) {
608 reboot_target = &command[len + 1];
609 }
610 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700611 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
612 return -EINVAL;
613 }
614
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800615 std::string timeout = property_get("ro.build.shutdown_timeout");
616 unsigned int delay = 0;
617
618 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
619 Timer t;
620 // Ask all services to terminate.
621 ServiceManager::GetInstance().ForEachService(
622 [] (Service* s) { s->Terminate(); });
623
624 while (t.duration() < delay) {
625 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
626
627 int service_count = 0;
628 ServiceManager::GetInstance().ForEachService(
629 [&service_count] (Service* s) {
630 // Count the number of services running.
631 // Exclude the console as it will ignore the SIGTERM signal
632 // and not exit.
633 // Note: SVC_CONSOLE actually means "requires console" but
634 // it is only used by the shell.
635 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
636 service_count++;
637 }
638 });
639
640 if (service_count == 0) {
641 // All terminable services terminated. We can exit early.
642 break;
643 }
644
645 // Wait a bit before recounting the number or running services.
646 usleep(kTerminateServiceDelayMicroSeconds);
647 }
648 NOTICE("Terminating running services took %.02f seconds", t.duration());
649 }
650
Yusuke Sato0df08272015-07-08 14:57:07 -0700651 return android_reboot_with_callback(cmd, 0, reboot_target,
652 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700653}
654
Tom Cherryb7349902015-08-26 11:43:36 -0700655static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700656 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700657 return 0;
658}
659
Tom Cherryb7349902015-08-26 11:43:36 -0700660static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700661 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700662}
663
Tom Cherryb7349902015-08-26 11:43:36 -0700664static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700665 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800666}
667
Tom Cherryb7349902015-08-26 11:43:36 -0700668static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700669 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800670}
671
Tom Cherryb7349902015-08-26 11:43:36 -0700672static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800673 struct timezone tz;
674
The Android Open Source Project35237d12008-12-17 18:08:08 -0800675 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700676 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800677 if (settimeofday(NULL, &tz))
678 return -1;
679 return 0;
680}
681
Tom Cherryb7349902015-08-26 11:43:36 -0700682static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700683 int mode = -1;
684 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000685 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700686 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000687 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700688 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000689}
690
Tom Cherryb7349902015-08-26 11:43:36 -0700691static void verity_update_property(fstab_rec *fstab, const char *mount_point,
692 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100693 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
694 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000695}
696
Tom Cherryb7349902015-08-26 11:43:36 -0700697static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700698 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000699}
700
Tom Cherryb7349902015-08-26 11:43:36 -0700701static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700702 const char* path = args[1].c_str();
703 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700704 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705}
706
Tom Cherryb7349902015-08-26 11:43:36 -0700707static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700708 char *buffer = NULL;
709 int rc = 0;
710 int fd1 = -1, fd2 = -1;
711 struct stat info;
712 int brtw, brtr;
713 char *p;
714
Tom Cherry96f67312015-07-30 13:52:55 -0700715 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700716 return -1;
717
Tom Cherry96f67312015-07-30 13:52:55 -0700718 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700719 goto out_err;
720
Tom Cherry96f67312015-07-30 13:52:55 -0700721 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700722 goto out_err;
723
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800724 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700725 goto out_err;
726
727 p = buffer;
728 brtr = info.st_size;
729 while(brtr) {
730 rc = read(fd1, p, brtr);
731 if (rc < 0)
732 goto out_err;
733 if (rc == 0)
734 break;
735 p += rc;
736 brtr -= rc;
737 }
738
739 p = buffer;
740 brtw = info.st_size;
741 while(brtw) {
742 rc = write(fd2, p, brtw);
743 if (rc < 0)
744 goto out_err;
745 if (rc == 0)
746 break;
747 p += rc;
748 brtw -= rc;
749 }
750
751 rc = 0;
752 goto out;
753out_err:
754 rc = -1;
755out:
756 if (buffer)
757 free(buffer);
758 if (fd1 >= 0)
759 close(fd1);
760 if (fd2 >= 0)
761 close(fd2);
762 return rc;
763}
764
Tom Cherryb7349902015-08-26 11:43:36 -0700765static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700767 if (args.size() == 3) {
768 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700770 } else if (args.size() == 4) {
771 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
772 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700773 return -errno;
774 } else {
775 return -1;
776 }
777 return 0;
778}
779
780static mode_t get_mode(const char *s) {
781 mode_t mode = 0;
782 while (*s) {
783 if (*s >= '0' && *s <= '7') {
784 mode = (mode<<3) | (*s-'0');
785 } else {
786 return -1;
787 }
788 s++;
789 }
790 return mode;
791}
792
Tom Cherryb7349902015-08-26 11:43:36 -0700793static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700794 mode_t mode = get_mode(args[1].c_str());
795 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796 return -errno;
797 }
798 return 0;
799}
800
Tom Cherryb7349902015-08-26 11:43:36 -0700801static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400802 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500803
Tom Cherry96f67312015-07-30 13:52:55 -0700804 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
805 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400806 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500807 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400808 return ret;
809}
810
Tom Cherryb7349902015-08-26 11:43:36 -0700811static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400812 int ret = 0;
813
Tom Cherry96f67312015-07-30 13:52:55 -0700814 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
815 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400816 ret = -errno;
817 }
818 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500819}
820
Tom Cherryb7349902015-08-26 11:43:36 -0700821static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700822 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700823 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
824 ERROR("loglevel: invalid log level'%d'\n", log_level);
825 return -EINVAL;
826 }
827 klog_set_level(log_level);
828 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829}
830
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700831static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700832 load_persist_props();
833 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800834}
835
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700836static int do_load_system_props(const std::vector<std::string>& args) {
837 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700838 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700839}
840
Tom Cherryb7349902015-08-26 11:43:36 -0700841static int do_wait(const std::vector<std::string>& args) {
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 */
Tom Cherryb7349902015-08-26 11:43:36 -0700853static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000854 if (make_dir(dir, 0700) && errno != EEXIST) {
855 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000856 }
857
Paul Lawrence806d10b2015-04-28 22:07:10 +0000858 return 0;
859}
860
Paul Crowley749af8c2015-05-28 17:35:06 +0100861static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700862 std::string value = property_get("ro.crypto.type");
863 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100864}
865
Tom Cherryb7349902015-08-26 11:43:36 -0700866static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100867 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000868 return 0;
869 }
Tom Cherry96f67312015-07-30 13:52:55 -0700870 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000871 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000872}
Paul Crowley749af8c2015-05-28 17:35:06 +0100873
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700874static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100875 if (!is_file_crypto()) {
876 return 0;
877 }
Tom Cherry087cd352015-08-03 14:19:35 -0700878 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100879}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700880
Tom Cherryb7349902015-08-26 11:43:36 -0700881BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
882 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
883 static const Map builtin_functions = {
884 {"bootchart_init", {0, 0, do_bootchart_init}},
885 {"chmod", {2, 2, do_chmod}},
886 {"chown", {2, 3, do_chown}},
887 {"class_reset", {1, 1, do_class_reset}},
888 {"class_start", {1, 1, do_class_start}},
889 {"class_stop", {1, 1, do_class_stop}},
890 {"copy", {2, 2, do_copy}},
891 {"domainname", {1, 1, do_domainname}},
892 {"enable", {1, 1, do_enable}},
893 {"exec", {1, kMax, do_exec}},
894 {"export", {2, 2, do_export}},
895 {"hostname", {1, 1, do_hostname}},
896 {"ifup", {1, 1, do_ifup}},
897 {"insmod", {1, kMax, do_insmod}},
898 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700899 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700900 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700901 {"loglevel", {1, 1, do_loglevel}},
902 {"mkdir", {1, 4, do_mkdir}},
903 {"mount_all", {1, 1, do_mount_all}},
904 {"mount", {3, kMax, do_mount}},
905 {"powerctl", {1, 1, do_powerctl}},
906 {"restart", {1, 1, do_restart}},
907 {"restorecon", {1, kMax, do_restorecon}},
908 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
909 {"rm", {1, 1, do_rm}},
910 {"rmdir", {1, 1, do_rmdir}},
911 {"setprop", {2, 2, do_setprop}},
912 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700913 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700914 {"start", {1, 1, do_start}},
915 {"stop", {1, 1, do_stop}},
916 {"swapon_all", {1, 1, do_swapon_all}},
917 {"symlink", {2, 2, do_symlink}},
918 {"sysclktz", {1, 1, do_sysclktz}},
919 {"trigger", {1, 1, do_trigger}},
920 {"verity_load_state", {0, 0, do_verity_load_state}},
921 {"verity_update_state", {0, 0, do_verity_update_state}},
922 {"wait", {1, 2, do_wait}},
923 {"write", {2, 2, do_write}},
924 };
925 return builtin_functions;
926}