blob: 1fddc63140234cf93ef84b7b61272ce6e1706436 [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>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070047#include <cutils/partition_utils.h>
48#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070049#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070050#include <private/android_filesystem_config.h>
51
Tom Cherryfa0c21c2015-07-23 17:53:11 -070052#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070053#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070055#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070057#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070058#include "property_service.h"
59#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080060#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Nick Kralevichbc609542015-01-31 21:39:46 -080063#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070064#define UNMOUNT_CHECK_MS 5000
65#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080066
Elliott Hughesf3cf4382015-02-03 17:12:07 -080067// System call provided by bionic but not in any header file.
68extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070069
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080070static const int kTerminateServiceDelayMicroSeconds = 50000;
71
Tom Cherryb7349902015-08-26 11:43:36 -070072static int insmod(const char *filename, const char *options) {
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070073 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070074 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080076 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077
Elliott Hughesf682b472015-02-06 12:19:48 -080078 // TODO: use finit_module for >= 3.8 kernels.
79 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070080}
81
Tom Cherryb7349902015-08-26 11:43:36 -070082static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083 struct ifreq ifr;
84 int s, ret;
85
86 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
87
88 s = socket(AF_INET, SOCK_DGRAM, 0);
89 if (s < 0)
90 return -1;
91
92 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
93 if (ret < 0) {
94 goto done;
95 }
96
97 if (up)
98 ifr.ifr_flags |= IFF_UP;
99 else
100 ifr.ifr_flags &= ~IFF_UP;
101
102 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800103
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104done:
105 close(s);
106 return ret;
107}
108
Mark Salyzyn402fb792016-04-05 13:43:40 -0700109// Turn off backlight while we are performing power down cleanup activities.
110static void turnOffBacklight() {
111 static const char off[] = "0";
112
113 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
114
115 static const char backlightDir[] = "/sys/class/backlight";
116 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
117 if (!dir) {
118 return;
119 }
120
121 struct dirent *dp;
122 while ((dp = readdir(dir.get())) != NULL) {
123 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
124 (dp->d_name[0] == '.')) {
125 continue;
126 }
127
128 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
129 backlightDir,
130 dp->d_name);
131 android::base::WriteStringToFile(off, fileName);
132 }
133}
134
Tom Cherryb7349902015-08-26 11:43:36 -0700135static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700136 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
137 return;
138
139 /* First, lazily unmount the directory. This unmount request finishes when
140 * all processes that open a file or directory in |entry->mnt_dir| exit.
141 */
142 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
143
144 /* Next, kill all processes except init, kthreadd, and kthreadd's
145 * children to finish the lazy unmount. Killing all processes here is okay
146 * because this callback function is only called right before reboot().
147 * It might be cleaner to selectively kill processes that actually use
148 * |entry->mnt_dir| rather than killing all, probably by reusing a function
149 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
150 * not allow init to scan /proc/<pid> files which the utility function
151 * heavily relies on. The policy does not allow the process to execute
152 * killall/pkill binaries either. Note that some processes might
153 * automatically restart after kill(), but that is not really a problem
154 * because |entry->mnt_dir| is no longer visible to such new processes.
155 */
Tom Cherrybac32992015-07-31 12:45:25 -0700156 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700157 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
158
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700159 // Restart Watchdogd to allow us to complete umounting and fsck
160 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
161 if (svc) {
162 do {
163 sched_yield(); // do not be so eager, let cleanup have priority
164 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
165 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
166 svc->Start();
167 }
168
Mark Salyzyn402fb792016-04-05 13:43:40 -0700169 turnOffBacklight();
170
Yusuke Sato0df08272015-07-08 14:57:07 -0700171 int count = 0;
172 while (count++ < UNMOUNT_CHECK_TIMES) {
173 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
174 if (fd >= 0) {
175 /* |entry->mnt_dir| has sucessfully been unmounted. */
176 close(fd);
177 break;
178 } else if (errno == EBUSY) {
179 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
180 * while then retry.
181 */
182 TEMP_FAILURE_RETRY(
183 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
184 continue;
185 } else {
186 /* Cannot open the device. Give up. */
187 return;
188 }
189 }
190
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700191 // NB: With watchdog still running, there is no cap on the time it takes
192 // to complete the fsck, from the users perspective the device graphics
193 // and responses are locked-up and they may choose to hold the power
194 // button in frustration if it drags out.
195
Yusuke Sato0df08272015-07-08 14:57:07 -0700196 int st;
197 if (!strcmp(entry->mnt_type, "f2fs")) {
198 const char *f2fs_argv[] = {
199 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
200 };
201 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700202 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700203 } else if (!strcmp(entry->mnt_type, "ext4")) {
204 const char *ext4_argv[] = {
205 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
206 };
207 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700208 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700209 }
210}
211
Tom Cherryb7349902015-08-26 11:43:36 -0700212static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700213 /* Starting a class does not start services
214 * which are explicitly disabled. They must
215 * be started individually.
216 */
Tom Cherrybac32992015-07-31 12:45:25 -0700217 ServiceManager::GetInstance().
218 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700219 return 0;
220}
221
Tom Cherryb7349902015-08-26 11:43:36 -0700222static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700223 ServiceManager::GetInstance().
224 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225 return 0;
226}
227
Tom Cherryb7349902015-08-26 11:43:36 -0700228static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700229 ServiceManager::GetInstance().
230 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800231 return 0;
232}
233
Tom Cherryb7349902015-08-26 11:43:36 -0700234static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700235 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700236}
237
Tom Cherryb7349902015-08-26 11:43:36 -0700238static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700239 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
240 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700241 return -1;
242 }
Tom Cherrybac32992015-07-31 12:45:25 -0700243 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700244}
245
Tom Cherryb7349902015-08-26 11:43:36 -0700246static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700247 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
248 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800249 return -1;
250 }
Tom Cherrybac32992015-07-31 12:45:25 -0700251 if (!svc->Start()) {
252 return -1;
253 }
254 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800255 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256}
257
Tom Cherryb7349902015-08-26 11:43:36 -0700258static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700259 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700260}
261
Tom Cherryb7349902015-08-26 11:43:36 -0700262static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700263 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264}
265
Tom Cherryb7349902015-08-26 11:43:36 -0700266static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700267 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268}
269
Tom Cherryb7349902015-08-26 11:43:36 -0700270static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700271 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800272
Tom Cherry96f67312015-07-30 13:52:55 -0700273 if (args.size() > 2) {
274 options += args[2];
275 for (std::size_t i = 3; i < args.size(); ++i) {
276 options += ' ';
277 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800278 }
279 }
280
Tom Cherry96f67312015-07-30 13:52:55 -0700281 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800282}
283
Tom Cherryb7349902015-08-26 11:43:36 -0700284static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700285 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700286 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287
288 /* mkdir <path> [mode] [owner] [group] */
289
Tom Cherry96f67312015-07-30 13:52:55 -0700290 if (args.size() >= 3) {
291 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292 }
293
Tom Cherry96f67312015-07-30 13:52:55 -0700294 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700295 /* chmod in case the directory already exists */
296 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700297 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700298 }
299 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 return -errno;
301 }
302
Tom Cherry96f67312015-07-30 13:52:55 -0700303 if (args.size() >= 4) {
304 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 gid_t gid = -1;
306
Tom Cherry96f67312015-07-30 13:52:55 -0700307 if (args.size() == 5) {
308 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 }
310
Tom Cherry96f67312015-07-30 13:52:55 -0700311 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 return -errno;
313 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700314
315 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
316 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700317 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700318 if (ret == -1) {
319 return -errno;
320 }
321 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322 }
323
Tom Cherry96f67312015-07-30 13:52:55 -0700324 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325}
326
327static struct {
328 const char *name;
329 unsigned flag;
330} mount_flags[] = {
331 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200332 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700333 { "nosuid", MS_NOSUID },
334 { "nodev", MS_NODEV },
335 { "nodiratime", MS_NODIRATIME },
336 { "ro", MS_RDONLY },
337 { "rw", 0 },
338 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700339 { "bind", MS_BIND },
340 { "rec", MS_REC },
341 { "unbindable", MS_UNBINDABLE },
342 { "private", MS_PRIVATE },
343 { "slave", MS_SLAVE },
344 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700345 { "defaults", 0 },
346 { 0, 0 },
347};
348
Ken Sumrall752923c2010-12-03 16:33:31 -0800349#define DATA_MNT_POINT "/data"
350
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700351/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700352static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700354 const char *source, *target, *system;
355 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700357 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700359 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360
Tom Cherry96f67312015-07-30 13:52:55 -0700361 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700363 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364 flags |= mount_flags[i].flag;
365 break;
366 }
367 }
368
Colin Crosscd0f1732010-04-19 17:10:24 -0700369 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700370 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700371 wait = 1;
372 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700373 else if (na + 1 == args.size())
374 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700375 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376 }
377
Tom Cherry96f67312015-07-30 13:52:55 -0700378 system = args[1].c_str();
379 source = args[2].c_str();
380 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000381
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 if (!strncmp(source, "mtd@", 4)) {
383 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384 if (n < 0) {
385 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000387
Yabin Cuie2d63af2015-02-17 19:27:51 -0800388 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000389
Colin Crosscd0f1732010-04-19 17:10:24 -0700390 if (wait)
391 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000392 if (mount(tmp, target, system, flags, options) < 0) {
393 return -1;
394 }
395
Ken Sumralldd4d7862011-02-17 18:09:47 -0800396 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000397 } else if (!strncmp(source, "loop@", 5)) {
398 int mode, loop, fd;
399 struct loop_info info;
400
401 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800402 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000403 if (fd < 0) {
404 return -1;
405 }
406
407 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800408 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800409 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100411 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000412 return -1;
413 }
414
415 /* if it is a blank loop device */
416 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
417 /* if it becomes our loop device */
418 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
419 close(fd);
420
421 if (mount(tmp, target, system, flags, options) < 0) {
422 ioctl(loop, LOOP_CLR_FD, 0);
423 close(loop);
424 return -1;
425 }
426
427 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800428 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000429 }
430 }
431
432 close(loop);
433 }
434
435 close(fd);
436 ERROR("out of loopback devices");
437 return -1;
438 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700439 if (wait)
440 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000441 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700442 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000443 }
444
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700445 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800446
447exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800448 return 0;
449
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700450}
451
Tom Cherryb7349902015-08-26 11:43:36 -0700452static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700453 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800454 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700455 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700456 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
457 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700458 close(fd);
459 } else {
460 ERROR("could not open /cache/recovery/command\n");
461 return -1;
462 }
463 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
464 while (1) { pause(); } // never reached
465}
466
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800467/* Imports .rc files from the specified paths. Default ones are applied if none is given.
468 *
469 * start_index: index of the first path in the args list
470 */
471static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700472 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800473 if (args.size() <= start_index) {
474 // Use the default set if no path is given
475 static const std::vector<std::string> init_directories = {
476 "/system/etc/init",
477 "/vendor/etc/init",
478 "/odm/etc/init"
479 };
480
481 for (const auto& dir : init_directories) {
482 parser.ParseConfig(dir);
483 }
484 } else {
485 for (size_t i = start_index; i < args.size(); ++i) {
486 parser.ParseConfig(args[i]);
487 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700488 }
489}
490
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800491/* mount_all <fstab> [ <path> ]*
492 *
JP Abgrallcee20682014-07-02 14:26:54 -0700493 * This function might request a reboot, in which case it will
494 * not return.
495 */
Tom Cherryb7349902015-08-26 11:43:36 -0700496static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700497 pid_t pid;
498 int ret = -1;
499 int child_ret = -1;
500 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800501 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700502
Tom Cherry96f67312015-07-30 13:52:55 -0700503 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 /*
505 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
506 * do the call in the child to provide protection to the main init
507 * process if anything goes wrong (crash or memory leak), and wait for
508 * the child to finish in the parent.
509 */
510 pid = fork();
511 if (pid > 0) {
512 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700513 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
514 if (wp_ret < 0) {
515 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700516 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700517 }
518
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 if (WIFEXITED(status)) {
520 ret = WEXITSTATUS(status);
521 } else {
522 ret = -1;
523 }
524 } else if (pid == 0) {
525 /* child, call fs_mgr_mount_all() */
526 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800527 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800528 child_ret = fs_mgr_mount_all(fstab);
529 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700530 if (child_ret == -1) {
531 ERROR("fs_mgr_mount_all returned an error\n");
532 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700533 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700534 } else {
535 /* fork failed, return an error */
536 return -1;
537 }
538
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800539 /* Paths of .rc files are specified at the 2nd argument and beyond */
540 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700541
JP Abgrallf22b7452014-07-02 13:16:04 -0700542 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800543 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700544 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700545 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000546 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800547 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700548 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700549 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800550 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
551 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
552 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700553 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700554 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
555 /* Setup a wipe via recovery, and reboot into recovery */
556 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
557 ret = wipe_data_via_recovery();
558 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800559 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000560 if (e4crypt_install_keyring()) {
561 return -1;
562 }
563 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000564 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000565
566 // Although encrypted, we have device key, so we do not need to
567 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700568 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700569 } else if (ret > 0) {
570 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700571 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700572 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700573
574 return ret;
575}
576
Tom Cherryb7349902015-08-26 11:43:36 -0700577static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700578 struct fstab *fstab;
579 int ret;
580
Tom Cherry96f67312015-07-30 13:52:55 -0700581 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700582 ret = fs_mgr_swapon_all(fstab);
583 fs_mgr_free_fstab(fstab);
584
585 return ret;
586}
587
Tom Cherryb7349902015-08-26 11:43:36 -0700588static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700589 const char* name = args[1].c_str();
590 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700591 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 return 0;
593}
594
Tom Cherryb7349902015-08-26 11:43:36 -0700595static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700596 struct rlimit limit;
597 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700598 resource = std::stoi(args[1]);
599 limit.rlim_cur = std::stoi(args[2]);
600 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700601 return setrlimit(resource, &limit);
602}
603
Tom Cherryb7349902015-08-26 11:43:36 -0700604static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700605 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
606 if (!svc) {
607 ERROR("do_start: Service %s not found\n", args[1].c_str());
608 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700609 }
Tom Cherrybac32992015-07-31 12:45:25 -0700610 if (!svc->Start())
611 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700612 return 0;
613}
614
Tom Cherryb7349902015-08-26 11:43:36 -0700615static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700616 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
617 if (!svc) {
618 ERROR("do_stop: Service %s not found\n", args[1].c_str());
619 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620 }
Tom Cherrybac32992015-07-31 12:45:25 -0700621 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700622 return 0;
623}
624
Tom Cherryb7349902015-08-26 11:43:36 -0700625static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700626 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
627 if (!svc) {
628 ERROR("do_restart: Service %s not found\n", args[1].c_str());
629 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700630 }
Tom Cherrybac32992015-07-31 12:45:25 -0700631 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700632 return 0;
633}
634
Tom Cherryb7349902015-08-26 11:43:36 -0700635static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700636 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700637 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700638 unsigned int cmd = 0;
639 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700640 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700641
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700642 if (strncmp(command, "shutdown", 8) == 0) {
643 cmd = ANDROID_RB_POWEROFF;
644 len = 8;
645 } else if (strncmp(command, "reboot", 6) == 0) {
646 cmd = ANDROID_RB_RESTART2;
647 len = 6;
648 } else {
649 ERROR("powerctl: unrecognized command '%s'\n", command);
650 return -EINVAL;
651 }
652
653 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700654 if (cmd == ANDROID_RB_POWEROFF &&
655 !strcmp(&command[len + 1], "userrequested")) {
656 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
657 // Run fsck once the file system is remounted in read-only mode.
658 callback_on_ro_remount = unmount_and_fsck;
659 } else if (cmd == ANDROID_RB_RESTART2) {
660 reboot_target = &command[len + 1];
661 }
662 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700663 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
664 return -EINVAL;
665 }
666
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800667 std::string timeout = property_get("ro.build.shutdown_timeout");
668 unsigned int delay = 0;
669
670 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
671 Timer t;
672 // Ask all services to terminate.
673 ServiceManager::GetInstance().ForEachService(
674 [] (Service* s) { s->Terminate(); });
675
676 while (t.duration() < delay) {
677 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
678
679 int service_count = 0;
680 ServiceManager::GetInstance().ForEachService(
681 [&service_count] (Service* s) {
682 // Count the number of services running.
683 // Exclude the console as it will ignore the SIGTERM signal
684 // and not exit.
685 // Note: SVC_CONSOLE actually means "requires console" but
686 // it is only used by the shell.
687 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
688 service_count++;
689 }
690 });
691
692 if (service_count == 0) {
693 // All terminable services terminated. We can exit early.
694 break;
695 }
696
697 // Wait a bit before recounting the number or running services.
698 usleep(kTerminateServiceDelayMicroSeconds);
699 }
700 NOTICE("Terminating running services took %.02f seconds", t.duration());
701 }
702
Yusuke Sato0df08272015-07-08 14:57:07 -0700703 return android_reboot_with_callback(cmd, 0, reboot_target,
704 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700705}
706
Tom Cherryb7349902015-08-26 11:43:36 -0700707static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700708 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709 return 0;
710}
711
Tom Cherryb7349902015-08-26 11:43:36 -0700712static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700713 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714}
715
Tom Cherryb7349902015-08-26 11:43:36 -0700716static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700717 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800718}
719
Tom Cherryb7349902015-08-26 11:43:36 -0700720static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700721 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800722}
723
Tom Cherryb7349902015-08-26 11:43:36 -0700724static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800725 struct timezone tz;
726
The Android Open Source Project35237d12008-12-17 18:08:08 -0800727 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700728 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800729 if (settimeofday(NULL, &tz))
730 return -1;
731 return 0;
732}
733
Tom Cherryb7349902015-08-26 11:43:36 -0700734static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700735 int mode = -1;
736 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000737 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700738 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000739 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700740 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000741}
742
Tom Cherryb7349902015-08-26 11:43:36 -0700743static void verity_update_property(fstab_rec *fstab, const char *mount_point,
744 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100745 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
746 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000747}
748
Tom Cherryb7349902015-08-26 11:43:36 -0700749static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700750 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000751}
752
Tom Cherryb7349902015-08-26 11:43:36 -0700753static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700754 const char* path = args[1].c_str();
755 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700756 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700757}
758
Tom Cherryb7349902015-08-26 11:43:36 -0700759static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700760 char *buffer = NULL;
761 int rc = 0;
762 int fd1 = -1, fd2 = -1;
763 struct stat info;
764 int brtw, brtr;
765 char *p;
766
Tom Cherry96f67312015-07-30 13:52:55 -0700767 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700768 return -1;
769
Tom Cherry96f67312015-07-30 13:52:55 -0700770 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700771 goto out_err;
772
Tom Cherry96f67312015-07-30 13:52:55 -0700773 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700774 goto out_err;
775
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800776 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700777 goto out_err;
778
779 p = buffer;
780 brtr = info.st_size;
781 while(brtr) {
782 rc = read(fd1, p, brtr);
783 if (rc < 0)
784 goto out_err;
785 if (rc == 0)
786 break;
787 p += rc;
788 brtr -= rc;
789 }
790
791 p = buffer;
792 brtw = info.st_size;
793 while(brtw) {
794 rc = write(fd2, p, brtw);
795 if (rc < 0)
796 goto out_err;
797 if (rc == 0)
798 break;
799 p += rc;
800 brtw -= rc;
801 }
802
803 rc = 0;
804 goto out;
805out_err:
806 rc = -1;
807out:
808 if (buffer)
809 free(buffer);
810 if (fd1 >= 0)
811 close(fd1);
812 if (fd2 >= 0)
813 close(fd2);
814 return rc;
815}
816
Tom Cherryb7349902015-08-26 11:43:36 -0700817static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700819 if (args.size() == 3) {
820 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700822 } else if (args.size() == 4) {
823 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
824 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700825 return -errno;
826 } else {
827 return -1;
828 }
829 return 0;
830}
831
832static mode_t get_mode(const char *s) {
833 mode_t mode = 0;
834 while (*s) {
835 if (*s >= '0' && *s <= '7') {
836 mode = (mode<<3) | (*s-'0');
837 } else {
838 return -1;
839 }
840 s++;
841 }
842 return mode;
843}
844
Tom Cherryb7349902015-08-26 11:43:36 -0700845static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700846 mode_t mode = get_mode(args[1].c_str());
847 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700848 return -errno;
849 }
850 return 0;
851}
852
Tom Cherryb7349902015-08-26 11:43:36 -0700853static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400854 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500855
Tom Cherry96f67312015-07-30 13:52:55 -0700856 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
857 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400858 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500859 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400860 return ret;
861}
862
Tom Cherryb7349902015-08-26 11:43:36 -0700863static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400864 int ret = 0;
865
Tom Cherry96f67312015-07-30 13:52:55 -0700866 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
867 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400868 ret = -errno;
869 }
870 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500871}
872
Tom Cherryb7349902015-08-26 11:43:36 -0700873static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700874 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700875 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
876 ERROR("loglevel: invalid log level'%d'\n", log_level);
877 return -EINVAL;
878 }
879 klog_set_level(log_level);
880 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881}
882
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700883static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700884 load_persist_props();
885 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800886}
887
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700888static int do_load_system_props(const std::vector<std::string>& args) {
889 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700890 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700891}
892
Tom Cherryb7349902015-08-26 11:43:36 -0700893static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700894 if (args.size() == 2) {
895 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
896 } else if (args.size() == 3) {
897 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800898 } else
899 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700900}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000901
Paul Lawrence806d10b2015-04-28 22:07:10 +0000902/*
903 * Callback to make a directory from the ext4 code
904 */
Tom Cherryb7349902015-08-26 11:43:36 -0700905static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000906 if (make_dir(dir, 0700) && errno != EEXIST) {
907 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000908 }
909
Paul Lawrence806d10b2015-04-28 22:07:10 +0000910 return 0;
911}
912
Paul Crowley749af8c2015-05-28 17:35:06 +0100913static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700914 std::string value = property_get("ro.crypto.type");
915 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100916}
917
Tom Cherryb7349902015-08-26 11:43:36 -0700918static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100919 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000920 return 0;
921 }
Tom Cherry96f67312015-07-30 13:52:55 -0700922 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000923 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000924}
Paul Crowley749af8c2015-05-28 17:35:06 +0100925
Paul Crowley59497452016-02-01 16:37:13 +0000926static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000927 return e4crypt_do_init_user0();
928}
929
Tom Cherryb7349902015-08-26 11:43:36 -0700930BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
931 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
932 static const Map builtin_functions = {
933 {"bootchart_init", {0, 0, do_bootchart_init}},
934 {"chmod", {2, 2, do_chmod}},
935 {"chown", {2, 3, do_chown}},
936 {"class_reset", {1, 1, do_class_reset}},
937 {"class_start", {1, 1, do_class_start}},
938 {"class_stop", {1, 1, do_class_stop}},
939 {"copy", {2, 2, do_copy}},
940 {"domainname", {1, 1, do_domainname}},
941 {"enable", {1, 1, do_enable}},
942 {"exec", {1, kMax, do_exec}},
943 {"export", {2, 2, do_export}},
944 {"hostname", {1, 1, do_hostname}},
945 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000946 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700947 {"insmod", {1, kMax, do_insmod}},
948 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700949 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700950 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700951 {"loglevel", {1, 1, do_loglevel}},
952 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800953 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700954 {"mount", {3, kMax, do_mount}},
955 {"powerctl", {1, 1, do_powerctl}},
956 {"restart", {1, 1, do_restart}},
957 {"restorecon", {1, kMax, do_restorecon}},
958 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
959 {"rm", {1, 1, do_rm}},
960 {"rmdir", {1, 1, do_rmdir}},
961 {"setprop", {2, 2, do_setprop}},
962 {"setrlimit", {3, 3, do_setrlimit}},
963 {"start", {1, 1, do_start}},
964 {"stop", {1, 1, do_stop}},
965 {"swapon_all", {1, 1, do_swapon_all}},
966 {"symlink", {2, 2, do_symlink}},
967 {"sysclktz", {1, 1, do_sysclktz}},
968 {"trigger", {1, 1, do_trigger}},
969 {"verity_load_state", {0, 0, do_verity_load_state}},
970 {"verity_update_state", {0, 0, do_verity_update_state}},
971 {"wait", {1, 2, do_wait}},
972 {"write", {2, 2, do_write}},
973 };
974 return builtin_functions;
975}