blob: b77f9ad320619311d86ff9ad1b2c391e7492ca54 [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
Mark Salyzyn402fb792016-04-05 13:43:40 -070019#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070021#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070022#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070023#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070024#include <signal.h>
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -070025#include <sched.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070028#include <string.h>
29#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/mount.h>
31#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080032#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070033#include <sys/types.h>
34#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070035#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070036#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000037#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000038#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070039
Stephen Smalleye46f9d52012-01-13 08:48:47 -050040#include <selinux/selinux.h>
41#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042
Elliott Hughesdb3f2672015-03-20 09:45:18 -070043#include <fs_mgr.h>
Mark Salyzyn402fb792016-04-05 13:43:40 -070044#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080045#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080046#include <android-base/stringprintf.h>
Yabin Cui46e03792016-04-06 15:56:49 -070047#include <bootloader_message_writer.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070048#include <cutils/partition_utils.h>
49#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070050#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070051#include <private/android_filesystem_config.h>
52
Tom Cherryfa0c21c2015-07-23 17:53:11 -070053#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070054#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070056#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070057#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070058#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070059#include "property_service.h"
60#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080061#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
Nick Kralevichbc609542015-01-31 21:39:46 -080064#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070065#define UNMOUNT_CHECK_MS 5000
66#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080067
Elliott Hughesf3cf4382015-02-03 17:12:07 -080068// System call provided by bionic but not in any header file.
69extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080071static const int kTerminateServiceDelayMicroSeconds = 50000;
72
Tom Cherryb7349902015-08-26 11:43:36 -070073static int insmod(const char *filename, const char *options) {
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070074 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070075 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080077 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078
Elliott Hughesf682b472015-02-06 12:19:48 -080079 // TODO: use finit_module for >= 3.8 kernels.
80 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081}
82
Tom Cherryb7349902015-08-26 11:43:36 -070083static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084 struct ifreq ifr;
85 int s, ret;
86
87 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
88
89 s = socket(AF_INET, SOCK_DGRAM, 0);
90 if (s < 0)
91 return -1;
92
93 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
94 if (ret < 0) {
95 goto done;
96 }
97
98 if (up)
99 ifr.ifr_flags |= IFF_UP;
100 else
101 ifr.ifr_flags &= ~IFF_UP;
102
103 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800104
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105done:
106 close(s);
107 return ret;
108}
109
Mark Salyzyn402fb792016-04-05 13:43:40 -0700110// Turn off backlight while we are performing power down cleanup activities.
111static void turnOffBacklight() {
112 static const char off[] = "0";
113
114 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
115
116 static const char backlightDir[] = "/sys/class/backlight";
117 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
118 if (!dir) {
119 return;
120 }
121
122 struct dirent *dp;
123 while ((dp = readdir(dir.get())) != NULL) {
124 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
125 (dp->d_name[0] == '.')) {
126 continue;
127 }
128
129 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
130 backlightDir,
131 dp->d_name);
132 android::base::WriteStringToFile(off, fileName);
133 }
134}
135
Tom Cherryb7349902015-08-26 11:43:36 -0700136static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700137 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
138 return;
139
140 /* First, lazily unmount the directory. This unmount request finishes when
141 * all processes that open a file or directory in |entry->mnt_dir| exit.
142 */
143 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
144
145 /* Next, kill all processes except init, kthreadd, and kthreadd's
146 * children to finish the lazy unmount. Killing all processes here is okay
147 * because this callback function is only called right before reboot().
148 * It might be cleaner to selectively kill processes that actually use
149 * |entry->mnt_dir| rather than killing all, probably by reusing a function
150 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
151 * not allow init to scan /proc/<pid> files which the utility function
152 * heavily relies on. The policy does not allow the process to execute
153 * killall/pkill binaries either. Note that some processes might
154 * automatically restart after kill(), but that is not really a problem
155 * because |entry->mnt_dir| is no longer visible to such new processes.
156 */
Tom Cherrybac32992015-07-31 12:45:25 -0700157 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700158 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
159
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700160 // Restart Watchdogd to allow us to complete umounting and fsck
161 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
162 if (svc) {
163 do {
164 sched_yield(); // do not be so eager, let cleanup have priority
165 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
166 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
167 svc->Start();
168 }
169
Mark Salyzyn402fb792016-04-05 13:43:40 -0700170 turnOffBacklight();
171
Yusuke Sato0df08272015-07-08 14:57:07 -0700172 int count = 0;
173 while (count++ < UNMOUNT_CHECK_TIMES) {
174 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
175 if (fd >= 0) {
176 /* |entry->mnt_dir| has sucessfully been unmounted. */
177 close(fd);
178 break;
179 } else if (errno == EBUSY) {
180 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
181 * while then retry.
182 */
183 TEMP_FAILURE_RETRY(
184 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
185 continue;
186 } else {
187 /* Cannot open the device. Give up. */
188 return;
189 }
190 }
191
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700192 // NB: With watchdog still running, there is no cap on the time it takes
193 // to complete the fsck, from the users perspective the device graphics
194 // and responses are locked-up and they may choose to hold the power
195 // button in frustration if it drags out.
196
Yusuke Sato0df08272015-07-08 14:57:07 -0700197 int st;
198 if (!strcmp(entry->mnt_type, "f2fs")) {
199 const char *f2fs_argv[] = {
200 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
201 };
202 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700203 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700204 } else if (!strcmp(entry->mnt_type, "ext4")) {
205 const char *ext4_argv[] = {
206 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
207 };
208 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700209 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700210 }
211}
212
Tom Cherryb7349902015-08-26 11:43:36 -0700213static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 /* Starting a class does not start services
215 * which are explicitly disabled. They must
216 * be started individually.
217 */
Tom Cherrybac32992015-07-31 12:45:25 -0700218 ServiceManager::GetInstance().
219 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 return 0;
221}
222
Tom Cherryb7349902015-08-26 11:43:36 -0700223static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700224 ServiceManager::GetInstance().
225 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 return 0;
227}
228
Tom Cherryb7349902015-08-26 11:43:36 -0700229static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700230 ServiceManager::GetInstance().
231 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800232 return 0;
233}
234
Tom Cherryb7349902015-08-26 11:43:36 -0700235static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700236 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237}
238
Tom Cherryb7349902015-08-26 11:43:36 -0700239static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700240 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
241 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700242 return -1;
243 }
Tom Cherrybac32992015-07-31 12:45:25 -0700244 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700245}
246
Tom Cherryb7349902015-08-26 11:43:36 -0700247static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700248 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
249 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800250 return -1;
251 }
Tom Cherrybac32992015-07-31 12:45:25 -0700252 if (!svc->Start()) {
253 return -1;
254 }
255 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800256 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257}
258
Tom Cherryb7349902015-08-26 11:43:36 -0700259static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700260 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261}
262
Tom Cherryb7349902015-08-26 11:43:36 -0700263static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700264 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265}
266
Tom Cherryb7349902015-08-26 11:43:36 -0700267static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700268 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700269}
270
Tom Cherryb7349902015-08-26 11:43:36 -0700271static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700272 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800273
Tom Cherry96f67312015-07-30 13:52:55 -0700274 if (args.size() > 2) {
275 options += args[2];
276 for (std::size_t i = 3; i < args.size(); ++i) {
277 options += ' ';
278 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800279 }
280 }
281
Tom Cherry96f67312015-07-30 13:52:55 -0700282 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800283}
284
Tom Cherryb7349902015-08-26 11:43:36 -0700285static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700287 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288
289 /* mkdir <path> [mode] [owner] [group] */
290
Tom Cherry96f67312015-07-30 13:52:55 -0700291 if (args.size() >= 3) {
292 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293 }
294
Tom Cherry96f67312015-07-30 13:52:55 -0700295 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700296 /* chmod in case the directory already exists */
297 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700298 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700299 }
300 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 return -errno;
302 }
303
Tom Cherry96f67312015-07-30 13:52:55 -0700304 if (args.size() >= 4) {
305 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 gid_t gid = -1;
307
Tom Cherry96f67312015-07-30 13:52:55 -0700308 if (args.size() == 5) {
309 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 }
311
Tom Cherry96f67312015-07-30 13:52:55 -0700312 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313 return -errno;
314 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700315
316 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
317 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700318 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700319 if (ret == -1) {
320 return -errno;
321 }
322 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 }
324
Tom Cherry96f67312015-07-30 13:52:55 -0700325 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326}
327
328static struct {
329 const char *name;
330 unsigned flag;
331} mount_flags[] = {
332 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200333 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334 { "nosuid", MS_NOSUID },
335 { "nodev", MS_NODEV },
336 { "nodiratime", MS_NODIRATIME },
337 { "ro", MS_RDONLY },
338 { "rw", 0 },
339 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700340 { "bind", MS_BIND },
341 { "rec", MS_REC },
342 { "unbindable", MS_UNBINDABLE },
343 { "private", MS_PRIVATE },
344 { "slave", MS_SLAVE },
345 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346 { "defaults", 0 },
347 { 0, 0 },
348};
349
Ken Sumrall752923c2010-12-03 16:33:31 -0800350#define DATA_MNT_POINT "/data"
351
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700353static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700355 const char *source, *target, *system;
356 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700358 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700360 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361
Tom Cherry96f67312015-07-30 13:52:55 -0700362 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700364 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 flags |= mount_flags[i].flag;
366 break;
367 }
368 }
369
Colin Crosscd0f1732010-04-19 17:10:24 -0700370 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700371 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700372 wait = 1;
373 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700374 else if (na + 1 == args.size())
375 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700376 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 }
378
Tom Cherry96f67312015-07-30 13:52:55 -0700379 system = args[1].c_str();
380 source = args[2].c_str();
381 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000382
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 if (!strncmp(source, "mtd@", 4)) {
384 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000385 if (n < 0) {
386 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000388
Yabin Cuie2d63af2015-02-17 19:27:51 -0800389 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000390
Colin Crosscd0f1732010-04-19 17:10:24 -0700391 if (wait)
392 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393 if (mount(tmp, target, system, flags, options) < 0) {
394 return -1;
395 }
396
Ken Sumralldd4d7862011-02-17 18:09:47 -0800397 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000398 } else if (!strncmp(source, "loop@", 5)) {
399 int mode, loop, fd;
400 struct loop_info info;
401
402 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800403 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000404 if (fd < 0) {
405 return -1;
406 }
407
408 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800409 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800410 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000411 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100412 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 return -1;
414 }
415
416 /* if it is a blank loop device */
417 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
418 /* if it becomes our loop device */
419 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
420 close(fd);
421
422 if (mount(tmp, target, system, flags, options) < 0) {
423 ioctl(loop, LOOP_CLR_FD, 0);
424 close(loop);
425 return -1;
426 }
427
428 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800429 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000430 }
431 }
432
433 close(loop);
434 }
435
436 close(fd);
437 ERROR("out of loopback devices");
438 return -1;
439 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700440 if (wait)
441 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000442 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700443 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000444 }
445
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700446 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800447
448exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800449 return 0;
450
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700451}
452
Tom Cherryb7349902015-08-26 11:43:36 -0700453static int wipe_data_via_recovery() {
Yabin Cui46e03792016-04-06 15:56:49 -0700454 const std::vector<std::string> options = {"--wipe_data", "--reason=wipe_data_via_recovery"};
455 std::string err;
456 if (!write_bootloader_message(options, &err)) {
457 ERROR("failed to set bootloader message: %s", err.c_str());
JP Abgrallcee20682014-07-02 14:26:54 -0700458 return -1;
459 }
460 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
461 while (1) { pause(); } // never reached
462}
463
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800464/* Imports .rc files from the specified paths. Default ones are applied if none is given.
465 *
466 * start_index: index of the first path in the args list
467 */
468static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700469 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800470 if (args.size() <= start_index) {
471 // Use the default set if no path is given
472 static const std::vector<std::string> init_directories = {
473 "/system/etc/init",
474 "/vendor/etc/init",
475 "/odm/etc/init"
476 };
477
478 for (const auto& dir : init_directories) {
479 parser.ParseConfig(dir);
480 }
481 } else {
482 for (size_t i = start_index; i < args.size(); ++i) {
483 parser.ParseConfig(args[i]);
484 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700485 }
486}
487
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800488/* mount_all <fstab> [ <path> ]*
489 *
JP Abgrallcee20682014-07-02 14:26:54 -0700490 * This function might request a reboot, in which case it will
491 * not return.
492 */
Tom Cherryb7349902015-08-26 11:43:36 -0700493static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700494 pid_t pid;
495 int ret = -1;
496 int child_ret = -1;
497 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800498 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700499
Tom Cherry96f67312015-07-30 13:52:55 -0700500 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501 /*
502 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
503 * do the call in the child to provide protection to the main init
504 * process if anything goes wrong (crash or memory leak), and wait for
505 * the child to finish in the parent.
506 */
507 pid = fork();
508 if (pid > 0) {
509 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700510 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
511 if (wp_ret < 0) {
512 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700513 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700514 }
515
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700516 if (WIFEXITED(status)) {
517 ret = WEXITSTATUS(status);
518 } else {
519 ret = -1;
520 }
521 } else if (pid == 0) {
522 /* child, call fs_mgr_mount_all() */
523 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800524 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800525 child_ret = fs_mgr_mount_all(fstab);
526 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700527 if (child_ret == -1) {
528 ERROR("fs_mgr_mount_all returned an error\n");
529 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700530 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700531 } else {
532 /* fork failed, return an error */
533 return -1;
534 }
535
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800536 /* Paths of .rc files are specified at the 2nd argument and beyond */
537 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700538
JP Abgrallf22b7452014-07-02 13:16:04 -0700539 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700540 ActionManager::GetInstance().QueueEventTrigger("encrypt");
JP Abgrallf22b7452014-07-02 13:16:04 -0700541 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700542 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000543 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700544 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
JP Abgrallf22b7452014-07-02 13:16:04 -0700545 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700546 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800547 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
548 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
549 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700550 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700551 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
552 /* Setup a wipe via recovery, and reboot into recovery */
553 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
554 ret = wipe_data_via_recovery();
555 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800556 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000557 if (e4crypt_install_keyring()) {
558 return -1;
559 }
560 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000561 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000562
563 // Although encrypted, we have device key, so we do not need to
564 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700565 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700566 } else if (ret > 0) {
567 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700568 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700569 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700570
571 return ret;
572}
573
Tom Cherryb7349902015-08-26 11:43:36 -0700574static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700575 struct fstab *fstab;
576 int ret;
577
Tom Cherry96f67312015-07-30 13:52:55 -0700578 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700579 ret = fs_mgr_swapon_all(fstab);
580 fs_mgr_free_fstab(fstab);
581
582 return ret;
583}
584
Tom Cherryb7349902015-08-26 11:43:36 -0700585static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700586 const char* name = args[1].c_str();
587 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700588 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589 return 0;
590}
591
Tom Cherryb7349902015-08-26 11:43:36 -0700592static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 struct rlimit limit;
594 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700595 resource = std::stoi(args[1]);
596 limit.rlim_cur = std::stoi(args[2]);
597 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 return setrlimit(resource, &limit);
599}
600
Tom Cherryb7349902015-08-26 11:43:36 -0700601static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700602 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
603 if (!svc) {
604 ERROR("do_start: Service %s not found\n", args[1].c_str());
605 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 }
Tom Cherrybac32992015-07-31 12:45:25 -0700607 if (!svc->Start())
608 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700609 return 0;
610}
611
Tom Cherryb7349902015-08-26 11:43:36 -0700612static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700613 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
614 if (!svc) {
615 ERROR("do_stop: Service %s not found\n", args[1].c_str());
616 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 }
Tom Cherrybac32992015-07-31 12:45:25 -0700618 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700619 return 0;
620}
621
Tom Cherryb7349902015-08-26 11:43:36 -0700622static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700623 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
624 if (!svc) {
625 ERROR("do_restart: Service %s not found\n", args[1].c_str());
626 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 }
Tom Cherrybac32992015-07-31 12:45:25 -0700628 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700629 return 0;
630}
631
Tom Cherryb7349902015-08-26 11:43:36 -0700632static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700633 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700634 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700635 unsigned int cmd = 0;
636 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700637 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700638
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700639 if (strncmp(command, "shutdown", 8) == 0) {
640 cmd = ANDROID_RB_POWEROFF;
641 len = 8;
642 } else if (strncmp(command, "reboot", 6) == 0) {
643 cmd = ANDROID_RB_RESTART2;
644 len = 6;
645 } else {
646 ERROR("powerctl: unrecognized command '%s'\n", command);
647 return -EINVAL;
648 }
649
650 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700651 if (cmd == ANDROID_RB_POWEROFF &&
652 !strcmp(&command[len + 1], "userrequested")) {
653 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
654 // Run fsck once the file system is remounted in read-only mode.
655 callback_on_ro_remount = unmount_and_fsck;
656 } else if (cmd == ANDROID_RB_RESTART2) {
657 reboot_target = &command[len + 1];
658 }
659 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700660 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
661 return -EINVAL;
662 }
663
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800664 std::string timeout = property_get("ro.build.shutdown_timeout");
665 unsigned int delay = 0;
666
667 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
668 Timer t;
669 // Ask all services to terminate.
670 ServiceManager::GetInstance().ForEachService(
671 [] (Service* s) { s->Terminate(); });
672
673 while (t.duration() < delay) {
674 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
675
676 int service_count = 0;
677 ServiceManager::GetInstance().ForEachService(
678 [&service_count] (Service* s) {
679 // Count the number of services running.
680 // Exclude the console as it will ignore the SIGTERM signal
681 // and not exit.
682 // Note: SVC_CONSOLE actually means "requires console" but
683 // it is only used by the shell.
684 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
685 service_count++;
686 }
687 });
688
689 if (service_count == 0) {
690 // All terminable services terminated. We can exit early.
691 break;
692 }
693
694 // Wait a bit before recounting the number or running services.
695 usleep(kTerminateServiceDelayMicroSeconds);
696 }
697 NOTICE("Terminating running services took %.02f seconds", t.duration());
698 }
699
Yusuke Sato0df08272015-07-08 14:57:07 -0700700 return android_reboot_with_callback(cmd, 0, reboot_target,
701 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700702}
703
Tom Cherryb7349902015-08-26 11:43:36 -0700704static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700705 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706 return 0;
707}
708
Tom Cherryb7349902015-08-26 11:43:36 -0700709static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700710 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700711}
712
Tom Cherryb7349902015-08-26 11:43:36 -0700713static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700714 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800715}
716
Tom Cherryb7349902015-08-26 11:43:36 -0700717static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700718 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800719}
720
Tom Cherryb7349902015-08-26 11:43:36 -0700721static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800722 struct timezone tz;
723
The Android Open Source Project35237d12008-12-17 18:08:08 -0800724 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700725 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800726 if (settimeofday(NULL, &tz))
727 return -1;
728 return 0;
729}
730
Tom Cherryb7349902015-08-26 11:43:36 -0700731static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700732 int mode = -1;
733 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000734 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700735 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000736 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700737 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000738}
739
Tom Cherryb7349902015-08-26 11:43:36 -0700740static void verity_update_property(fstab_rec *fstab, const char *mount_point,
741 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100742 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
743 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000744}
745
Tom Cherryb7349902015-08-26 11:43:36 -0700746static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700747 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000748}
749
Tom Cherryb7349902015-08-26 11:43:36 -0700750static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700751 const char* path = args[1].c_str();
752 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700753 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700754}
755
Tom Cherryb7349902015-08-26 11:43:36 -0700756static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700757 char *buffer = NULL;
758 int rc = 0;
759 int fd1 = -1, fd2 = -1;
760 struct stat info;
761 int brtw, brtr;
762 char *p;
763
Tom Cherry96f67312015-07-30 13:52:55 -0700764 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700765 return -1;
766
Tom Cherry96f67312015-07-30 13:52:55 -0700767 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700768 goto out_err;
769
Tom Cherry96f67312015-07-30 13:52:55 -0700770 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700771 goto out_err;
772
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800773 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700774 goto out_err;
775
776 p = buffer;
777 brtr = info.st_size;
778 while(brtr) {
779 rc = read(fd1, p, brtr);
780 if (rc < 0)
781 goto out_err;
782 if (rc == 0)
783 break;
784 p += rc;
785 brtr -= rc;
786 }
787
788 p = buffer;
789 brtw = info.st_size;
790 while(brtw) {
791 rc = write(fd2, p, brtw);
792 if (rc < 0)
793 goto out_err;
794 if (rc == 0)
795 break;
796 p += rc;
797 brtw -= rc;
798 }
799
800 rc = 0;
801 goto out;
802out_err:
803 rc = -1;
804out:
805 if (buffer)
806 free(buffer);
807 if (fd1 >= 0)
808 close(fd1);
809 if (fd2 >= 0)
810 close(fd2);
811 return rc;
812}
813
Tom Cherryb7349902015-08-26 11:43:36 -0700814static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700816 if (args.size() == 3) {
817 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700819 } else if (args.size() == 4) {
820 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
821 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822 return -errno;
823 } else {
824 return -1;
825 }
826 return 0;
827}
828
829static mode_t get_mode(const char *s) {
830 mode_t mode = 0;
831 while (*s) {
832 if (*s >= '0' && *s <= '7') {
833 mode = (mode<<3) | (*s-'0');
834 } else {
835 return -1;
836 }
837 s++;
838 }
839 return mode;
840}
841
Tom Cherryb7349902015-08-26 11:43:36 -0700842static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700843 mode_t mode = get_mode(args[1].c_str());
844 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700845 return -errno;
846 }
847 return 0;
848}
849
Tom Cherryb7349902015-08-26 11:43:36 -0700850static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400851 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500852
Tom Cherry96f67312015-07-30 13:52:55 -0700853 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
854 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400855 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500856 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400857 return ret;
858}
859
Tom Cherryb7349902015-08-26 11:43:36 -0700860static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400861 int ret = 0;
862
Tom Cherry96f67312015-07-30 13:52:55 -0700863 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
864 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400865 ret = -errno;
866 }
867 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500868}
869
Tom Cherryb7349902015-08-26 11:43:36 -0700870static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700871 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700872 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
873 ERROR("loglevel: invalid log level'%d'\n", log_level);
874 return -EINVAL;
875 }
876 klog_set_level(log_level);
877 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700878}
879
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700880static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700881 load_persist_props();
882 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800883}
884
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700885static int do_load_system_props(const std::vector<std::string>& args) {
886 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700887 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700888}
889
Tom Cherryb7349902015-08-26 11:43:36 -0700890static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700891 if (args.size() == 2) {
892 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
893 } else if (args.size() == 3) {
894 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800895 } else
896 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700897}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000898
Paul Lawrence806d10b2015-04-28 22:07:10 +0000899/*
900 * Callback to make a directory from the ext4 code
901 */
Tom Cherryb7349902015-08-26 11:43:36 -0700902static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000903 if (make_dir(dir, 0700) && errno != EEXIST) {
904 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000905 }
906
Paul Lawrence806d10b2015-04-28 22:07:10 +0000907 return 0;
908}
909
Paul Crowley749af8c2015-05-28 17:35:06 +0100910static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700911 std::string value = property_get("ro.crypto.type");
912 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100913}
914
Tom Cherryb7349902015-08-26 11:43:36 -0700915static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100916 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000917 return 0;
918 }
Tom Cherry96f67312015-07-30 13:52:55 -0700919 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000920 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000921}
Paul Crowley749af8c2015-05-28 17:35:06 +0100922
Paul Crowley59497452016-02-01 16:37:13 +0000923static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000924 return e4crypt_do_init_user0();
925}
926
Tom Cherryb7349902015-08-26 11:43:36 -0700927BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
928 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
929 static const Map builtin_functions = {
930 {"bootchart_init", {0, 0, do_bootchart_init}},
931 {"chmod", {2, 2, do_chmod}},
932 {"chown", {2, 3, do_chown}},
933 {"class_reset", {1, 1, do_class_reset}},
934 {"class_start", {1, 1, do_class_start}},
935 {"class_stop", {1, 1, do_class_stop}},
936 {"copy", {2, 2, do_copy}},
937 {"domainname", {1, 1, do_domainname}},
938 {"enable", {1, 1, do_enable}},
939 {"exec", {1, kMax, do_exec}},
940 {"export", {2, 2, do_export}},
941 {"hostname", {1, 1, do_hostname}},
942 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000943 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700944 {"insmod", {1, kMax, do_insmod}},
945 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700946 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700947 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700948 {"loglevel", {1, 1, do_loglevel}},
949 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800950 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700951 {"mount", {3, kMax, do_mount}},
952 {"powerctl", {1, 1, do_powerctl}},
953 {"restart", {1, 1, do_restart}},
954 {"restorecon", {1, kMax, do_restorecon}},
955 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
956 {"rm", {1, 1, do_rm}},
957 {"rmdir", {1, 1, do_rmdir}},
958 {"setprop", {2, 2, do_setprop}},
959 {"setrlimit", {3, 3, do_setrlimit}},
960 {"start", {1, 1, do_start}},
961 {"stop", {1, 1, do_stop}},
962 {"swapon_all", {1, 1, do_swapon_all}},
963 {"symlink", {2, 2, do_symlink}},
964 {"sysclktz", {1, 1, do_sysclktz}},
965 {"trigger", {1, 1, do_trigger}},
966 {"verity_load_state", {0, 0, do_verity_load_state}},
967 {"verity_update_state", {0, 0, do_verity_update_state}},
968 {"wait", {1, 2, do_wait}},
969 {"write", {2, 2, do_write}},
970 };
971 return builtin_functions;
972}