blob: 1381d7cd5693ec6afe67ae2efe76328e9e4efc39 [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>
Nick Kralevichf2049162016-03-27 16:55:59 -070032#include <sys/syscall.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080033#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070034#include <sys/types.h>
35#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070036#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070037#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000038#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000039#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041#include <selinux/selinux.h>
42#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043
Elliott Hughesdb3f2672015-03-20 09:45:18 -070044#include <fs_mgr.h>
Mark Salyzyn402fb792016-04-05 13:43:40 -070045#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080046#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/stringprintf.h>
Yabin Cui46e03792016-04-06 15:56:49 -070048#include <bootloader_message_writer.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070049#include <cutils/partition_utils.h>
50#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070051#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070052#include <private/android_filesystem_config.h>
53
Tom Cherryfa0c21c2015-07-23 17:53:11 -070054#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070055#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070057#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070058#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070059#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070060#include "property_service.h"
61#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080062#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070063#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064
Nick Kralevichbc609542015-01-31 21:39:46 -080065#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070066#define UNMOUNT_CHECK_MS 5000
67#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080068
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080069static const int kTerminateServiceDelayMicroSeconds = 50000;
70
Tom Cherryb7349902015-08-26 11:43:36 -070071static int insmod(const char *filename, const char *options) {
Nick Kralevichf2049162016-03-27 16:55:59 -070072 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
73 if (fd == -1) {
74 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080076 }
Nick Kralevichf2049162016-03-27 16:55:59 -070077 int rc = syscall(__NR_finit_module, fd, options, 0);
78 if (rc == -1) {
79 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
80 }
81 close(fd);
82 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083}
84
Tom Cherryb7349902015-08-26 11:43:36 -070085static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086 struct ifreq ifr;
87 int s, ret;
88
89 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
90
91 s = socket(AF_INET, SOCK_DGRAM, 0);
92 if (s < 0)
93 return -1;
94
95 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
96 if (ret < 0) {
97 goto done;
98 }
99
100 if (up)
101 ifr.ifr_flags |= IFF_UP;
102 else
103 ifr.ifr_flags &= ~IFF_UP;
104
105 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107done:
108 close(s);
109 return ret;
110}
111
Mark Salyzyn402fb792016-04-05 13:43:40 -0700112// Turn off backlight while we are performing power down cleanup activities.
113static void turnOffBacklight() {
114 static const char off[] = "0";
115
116 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
117
118 static const char backlightDir[] = "/sys/class/backlight";
119 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
120 if (!dir) {
121 return;
122 }
123
124 struct dirent *dp;
125 while ((dp = readdir(dir.get())) != NULL) {
126 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
127 (dp->d_name[0] == '.')) {
128 continue;
129 }
130
131 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
132 backlightDir,
133 dp->d_name);
134 android::base::WriteStringToFile(off, fileName);
135 }
136}
137
Tom Cherryb7349902015-08-26 11:43:36 -0700138static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700139 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
140 return;
141
142 /* First, lazily unmount the directory. This unmount request finishes when
143 * all processes that open a file or directory in |entry->mnt_dir| exit.
144 */
145 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
146
147 /* Next, kill all processes except init, kthreadd, and kthreadd's
148 * children to finish the lazy unmount. Killing all processes here is okay
149 * because this callback function is only called right before reboot().
150 * It might be cleaner to selectively kill processes that actually use
151 * |entry->mnt_dir| rather than killing all, probably by reusing a function
152 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
153 * not allow init to scan /proc/<pid> files which the utility function
154 * heavily relies on. The policy does not allow the process to execute
155 * killall/pkill binaries either. Note that some processes might
156 * automatically restart after kill(), but that is not really a problem
157 * because |entry->mnt_dir| is no longer visible to such new processes.
158 */
Tom Cherrybac32992015-07-31 12:45:25 -0700159 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700160 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
161
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700162 // Restart Watchdogd to allow us to complete umounting and fsck
163 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
164 if (svc) {
165 do {
166 sched_yield(); // do not be so eager, let cleanup have priority
167 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
168 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
169 svc->Start();
170 }
171
Mark Salyzyn402fb792016-04-05 13:43:40 -0700172 turnOffBacklight();
173
Yusuke Sato0df08272015-07-08 14:57:07 -0700174 int count = 0;
175 while (count++ < UNMOUNT_CHECK_TIMES) {
176 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
177 if (fd >= 0) {
178 /* |entry->mnt_dir| has sucessfully been unmounted. */
179 close(fd);
180 break;
181 } else if (errno == EBUSY) {
182 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
183 * while then retry.
184 */
185 TEMP_FAILURE_RETRY(
186 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
187 continue;
188 } else {
189 /* Cannot open the device. Give up. */
190 return;
191 }
192 }
193
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700194 // NB: With watchdog still running, there is no cap on the time it takes
195 // to complete the fsck, from the users perspective the device graphics
196 // and responses are locked-up and they may choose to hold the power
197 // button in frustration if it drags out.
198
Yusuke Sato0df08272015-07-08 14:57:07 -0700199 int st;
200 if (!strcmp(entry->mnt_type, "f2fs")) {
201 const char *f2fs_argv[] = {
202 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
203 };
204 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700205 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700206 } else if (!strcmp(entry->mnt_type, "ext4")) {
207 const char *ext4_argv[] = {
208 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
209 };
210 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700211 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700212 }
213}
214
Tom Cherryb7349902015-08-26 11:43:36 -0700215static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216 /* Starting a class does not start services
217 * which are explicitly disabled. They must
218 * be started individually.
219 */
Tom Cherrybac32992015-07-31 12:45:25 -0700220 ServiceManager::GetInstance().
221 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222 return 0;
223}
224
Tom Cherryb7349902015-08-26 11:43:36 -0700225static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700226 ServiceManager::GetInstance().
227 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228 return 0;
229}
230
Tom Cherryb7349902015-08-26 11:43:36 -0700231static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700232 ServiceManager::GetInstance().
233 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800234 return 0;
235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700238 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239}
240
Tom Cherryb7349902015-08-26 11:43:36 -0700241static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700242 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
243 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700244 return -1;
245 }
Tom Cherrybac32992015-07-31 12:45:25 -0700246 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700247}
248
Tom Cherryb7349902015-08-26 11:43:36 -0700249static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700250 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
251 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800252 return -1;
253 }
Tom Cherrybac32992015-07-31 12:45:25 -0700254 if (!svc->Start()) {
255 return -1;
256 }
257 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800258 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700262 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263}
264
Tom Cherryb7349902015-08-26 11:43:36 -0700265static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700266 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700267}
268
Tom Cherryb7349902015-08-26 11:43:36 -0700269static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700270 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271}
272
Tom Cherryb7349902015-08-26 11:43:36 -0700273static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700274 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800275
Tom Cherry96f67312015-07-30 13:52:55 -0700276 if (args.size() > 2) {
277 options += args[2];
278 for (std::size_t i = 3; i < args.size(); ++i) {
279 options += ' ';
280 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800281 }
282 }
283
Tom Cherry96f67312015-07-30 13:52:55 -0700284 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800285}
286
Tom Cherryb7349902015-08-26 11:43:36 -0700287static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700289 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290
291 /* mkdir <path> [mode] [owner] [group] */
292
Tom Cherry96f67312015-07-30 13:52:55 -0700293 if (args.size() >= 3) {
294 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 }
296
Tom Cherry96f67312015-07-30 13:52:55 -0700297 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700298 /* chmod in case the directory already exists */
299 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700300 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700301 }
302 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 return -errno;
304 }
305
Tom Cherry96f67312015-07-30 13:52:55 -0700306 if (args.size() >= 4) {
307 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 gid_t gid = -1;
309
Tom Cherry96f67312015-07-30 13:52:55 -0700310 if (args.size() == 5) {
311 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
313
Tom Cherry96f67312015-07-30 13:52:55 -0700314 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 return -errno;
316 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700317
318 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
319 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700320 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700321 if (ret == -1) {
322 return -errno;
323 }
324 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 }
326
Tom Cherry96f67312015-07-30 13:52:55 -0700327 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328}
329
330static struct {
331 const char *name;
332 unsigned flag;
333} mount_flags[] = {
334 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200335 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 { "nosuid", MS_NOSUID },
337 { "nodev", MS_NODEV },
338 { "nodiratime", MS_NODIRATIME },
339 { "ro", MS_RDONLY },
340 { "rw", 0 },
341 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700342 { "bind", MS_BIND },
343 { "rec", MS_REC },
344 { "unbindable", MS_UNBINDABLE },
345 { "private", MS_PRIVATE },
346 { "slave", MS_SLAVE },
347 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 { "defaults", 0 },
349 { 0, 0 },
350};
351
Ken Sumrall752923c2010-12-03 16:33:31 -0800352#define DATA_MNT_POINT "/data"
353
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700355static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700357 const char *source, *target, *system;
358 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700360 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700362 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363
Tom Cherry96f67312015-07-30 13:52:55 -0700364 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700366 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367 flags |= mount_flags[i].flag;
368 break;
369 }
370 }
371
Colin Crosscd0f1732010-04-19 17:10:24 -0700372 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700373 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700374 wait = 1;
375 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700376 else if (na + 1 == args.size())
377 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700378 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 }
380
Tom Cherry96f67312015-07-30 13:52:55 -0700381 system = args[1].c_str();
382 source = args[2].c_str();
383 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 if (!strncmp(source, "mtd@", 4)) {
386 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000387 if (n < 0) {
388 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000390
Yabin Cuie2d63af2015-02-17 19:27:51 -0800391 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000392
Colin Crosscd0f1732010-04-19 17:10:24 -0700393 if (wait)
394 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000395 if (mount(tmp, target, system, flags, options) < 0) {
396 return -1;
397 }
398
Ken Sumralldd4d7862011-02-17 18:09:47 -0800399 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000400 } else if (!strncmp(source, "loop@", 5)) {
401 int mode, loop, fd;
402 struct loop_info info;
403
404 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800405 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000406 if (fd < 0) {
407 return -1;
408 }
409
410 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800411 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800412 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100414 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415 return -1;
416 }
417
418 /* if it is a blank loop device */
419 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
420 /* if it becomes our loop device */
421 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
422 close(fd);
423
424 if (mount(tmp, target, system, flags, options) < 0) {
425 ioctl(loop, LOOP_CLR_FD, 0);
426 close(loop);
427 return -1;
428 }
429
430 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800431 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000432 }
433 }
434
435 close(loop);
436 }
437
438 close(fd);
439 ERROR("out of loopback devices");
440 return -1;
441 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700442 if (wait)
443 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000444 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700445 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000446 }
447
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700448 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800449
450exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800451 return 0;
452
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453}
454
Tom Cherryb7349902015-08-26 11:43:36 -0700455static int wipe_data_via_recovery() {
Yabin Cui46e03792016-04-06 15:56:49 -0700456 const std::vector<std::string> options = {"--wipe_data", "--reason=wipe_data_via_recovery"};
457 std::string err;
458 if (!write_bootloader_message(options, &err)) {
459 ERROR("failed to set bootloader message: %s", err.c_str());
JP Abgrallcee20682014-07-02 14:26:54 -0700460 return -1;
461 }
462 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
463 while (1) { pause(); } // never reached
464}
465
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800466/* Imports .rc files from the specified paths. Default ones are applied if none is given.
467 *
468 * start_index: index of the first path in the args list
469 */
470static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700471 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800472 if (args.size() <= start_index) {
473 // Use the default set if no path is given
474 static const std::vector<std::string> init_directories = {
475 "/system/etc/init",
476 "/vendor/etc/init",
477 "/odm/etc/init"
478 };
479
480 for (const auto& dir : init_directories) {
481 parser.ParseConfig(dir);
482 }
483 } else {
484 for (size_t i = start_index; i < args.size(); ++i) {
485 parser.ParseConfig(args[i]);
486 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700487 }
488}
489
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800490/* mount_all <fstab> [ <path> ]*
491 *
JP Abgrallcee20682014-07-02 14:26:54 -0700492 * This function might request a reboot, in which case it will
493 * not return.
494 */
Tom Cherryb7349902015-08-26 11:43:36 -0700495static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700496 pid_t pid;
497 int ret = -1;
498 int child_ret = -1;
499 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800500 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501
Tom Cherry96f67312015-07-30 13:52:55 -0700502 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700503 /*
504 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
505 * do the call in the child to provide protection to the main init
506 * process if anything goes wrong (crash or memory leak), and wait for
507 * the child to finish in the parent.
508 */
509 pid = fork();
510 if (pid > 0) {
511 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700512 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
513 if (wp_ret < 0) {
514 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700515 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700516 }
517
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700518 if (WIFEXITED(status)) {
519 ret = WEXITSTATUS(status);
520 } else {
521 ret = -1;
522 }
523 } else if (pid == 0) {
524 /* child, call fs_mgr_mount_all() */
525 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800526 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800527 child_ret = fs_mgr_mount_all(fstab);
528 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700529 if (child_ret == -1) {
530 ERROR("fs_mgr_mount_all returned an error\n");
531 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700532 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700533 } else {
534 /* fork failed, return an error */
535 return -1;
536 }
537
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800538 /* Paths of .rc files are specified at the 2nd argument and beyond */
539 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700540
JP Abgrallf22b7452014-07-02 13:16:04 -0700541 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700542 ActionManager::GetInstance().QueueEventTrigger("encrypt");
JP Abgrallf22b7452014-07-02 13:16:04 -0700543 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700544 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000545 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700546 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
JP Abgrallf22b7452014-07-02 13:16:04 -0700547 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700548 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800549 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
550 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
551 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700552 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700553 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
554 /* Setup a wipe via recovery, and reboot into recovery */
555 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
556 ret = wipe_data_via_recovery();
557 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800558 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000559 if (e4crypt_install_keyring()) {
560 return -1;
561 }
562 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000563 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000564
565 // Although encrypted, we have device key, so we do not need to
566 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700567 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700568 } else if (ret > 0) {
569 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700570 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700571 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700572
573 return ret;
574}
575
Tom Cherryb7349902015-08-26 11:43:36 -0700576static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700577 struct fstab *fstab;
578 int ret;
579
Tom Cherry96f67312015-07-30 13:52:55 -0700580 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700581 ret = fs_mgr_swapon_all(fstab);
582 fs_mgr_free_fstab(fstab);
583
584 return ret;
585}
586
Tom Cherryb7349902015-08-26 11:43:36 -0700587static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700588 const char* name = args[1].c_str();
589 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700590 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700591 return 0;
592}
593
Tom Cherryb7349902015-08-26 11:43:36 -0700594static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595 struct rlimit limit;
596 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700597 resource = std::stoi(args[1]);
598 limit.rlim_cur = std::stoi(args[2]);
599 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 return setrlimit(resource, &limit);
601}
602
Tom Cherryb7349902015-08-26 11:43:36 -0700603static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700604 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
605 if (!svc) {
606 ERROR("do_start: Service %s not found\n", args[1].c_str());
607 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700608 }
Tom Cherrybac32992015-07-31 12:45:25 -0700609 if (!svc->Start())
610 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700611 return 0;
612}
613
Tom Cherryb7349902015-08-26 11:43:36 -0700614static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700615 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
616 if (!svc) {
617 ERROR("do_stop: Service %s not found\n", args[1].c_str());
618 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700619 }
Tom Cherrybac32992015-07-31 12:45:25 -0700620 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700621 return 0;
622}
623
Tom Cherryb7349902015-08-26 11:43:36 -0700624static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700625 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
626 if (!svc) {
627 ERROR("do_restart: Service %s not found\n", args[1].c_str());
628 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700629 }
Tom Cherrybac32992015-07-31 12:45:25 -0700630 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700631 return 0;
632}
633
Tom Cherryb7349902015-08-26 11:43:36 -0700634static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700635 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700636 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700637 unsigned int cmd = 0;
638 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700639 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700640
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700641 if (strncmp(command, "shutdown", 8) == 0) {
642 cmd = ANDROID_RB_POWEROFF;
643 len = 8;
644 } else if (strncmp(command, "reboot", 6) == 0) {
645 cmd = ANDROID_RB_RESTART2;
646 len = 6;
647 } else {
648 ERROR("powerctl: unrecognized command '%s'\n", command);
649 return -EINVAL;
650 }
651
652 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700653 if (cmd == ANDROID_RB_POWEROFF &&
654 !strcmp(&command[len + 1], "userrequested")) {
655 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
656 // Run fsck once the file system is remounted in read-only mode.
657 callback_on_ro_remount = unmount_and_fsck;
658 } else if (cmd == ANDROID_RB_RESTART2) {
659 reboot_target = &command[len + 1];
660 }
661 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700662 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
663 return -EINVAL;
664 }
665
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800666 std::string timeout = property_get("ro.build.shutdown_timeout");
667 unsigned int delay = 0;
668
669 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
670 Timer t;
671 // Ask all services to terminate.
672 ServiceManager::GetInstance().ForEachService(
673 [] (Service* s) { s->Terminate(); });
674
675 while (t.duration() < delay) {
676 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
677
678 int service_count = 0;
679 ServiceManager::GetInstance().ForEachService(
680 [&service_count] (Service* s) {
681 // Count the number of services running.
682 // Exclude the console as it will ignore the SIGTERM signal
683 // and not exit.
684 // Note: SVC_CONSOLE actually means "requires console" but
685 // it is only used by the shell.
686 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
687 service_count++;
688 }
689 });
690
691 if (service_count == 0) {
692 // All terminable services terminated. We can exit early.
693 break;
694 }
695
696 // Wait a bit before recounting the number or running services.
697 usleep(kTerminateServiceDelayMicroSeconds);
698 }
699 NOTICE("Terminating running services took %.02f seconds", t.duration());
700 }
701
Yusuke Sato0df08272015-07-08 14:57:07 -0700702 return android_reboot_with_callback(cmd, 0, reboot_target,
703 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700704}
705
Tom Cherryb7349902015-08-26 11:43:36 -0700706static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700707 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700708 return 0;
709}
710
Tom Cherryb7349902015-08-26 11:43:36 -0700711static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700712 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700713}
714
Tom Cherryb7349902015-08-26 11:43:36 -0700715static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700716 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800717}
718
Tom Cherryb7349902015-08-26 11:43:36 -0700719static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700720 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800721}
722
Tom Cherryb7349902015-08-26 11:43:36 -0700723static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800724 struct timezone tz;
725
The Android Open Source Project35237d12008-12-17 18:08:08 -0800726 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700727 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800728 if (settimeofday(NULL, &tz))
729 return -1;
730 return 0;
731}
732
Tom Cherryb7349902015-08-26 11:43:36 -0700733static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700734 int mode = -1;
735 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000736 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700737 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000738 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700739 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000740}
741
Tom Cherryb7349902015-08-26 11:43:36 -0700742static void verity_update_property(fstab_rec *fstab, const char *mount_point,
743 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100744 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
745 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000746}
747
Tom Cherryb7349902015-08-26 11:43:36 -0700748static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700749 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000750}
751
Tom Cherryb7349902015-08-26 11:43:36 -0700752static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700753 const char* path = args[1].c_str();
754 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700755 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700756}
757
Tom Cherryb7349902015-08-26 11:43:36 -0700758static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700759 char *buffer = NULL;
760 int rc = 0;
761 int fd1 = -1, fd2 = -1;
762 struct stat info;
763 int brtw, brtr;
764 char *p;
765
Tom Cherry96f67312015-07-30 13:52:55 -0700766 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700767 return -1;
768
Tom Cherry96f67312015-07-30 13:52:55 -0700769 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700770 goto out_err;
771
Tom Cherry96f67312015-07-30 13:52:55 -0700772 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700773 goto out_err;
774
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800775 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700776 goto out_err;
777
778 p = buffer;
779 brtr = info.st_size;
780 while(brtr) {
781 rc = read(fd1, p, brtr);
782 if (rc < 0)
783 goto out_err;
784 if (rc == 0)
785 break;
786 p += rc;
787 brtr -= rc;
788 }
789
790 p = buffer;
791 brtw = info.st_size;
792 while(brtw) {
793 rc = write(fd2, p, brtw);
794 if (rc < 0)
795 goto out_err;
796 if (rc == 0)
797 break;
798 p += rc;
799 brtw -= rc;
800 }
801
802 rc = 0;
803 goto out;
804out_err:
805 rc = -1;
806out:
807 if (buffer)
808 free(buffer);
809 if (fd1 >= 0)
810 close(fd1);
811 if (fd2 >= 0)
812 close(fd2);
813 return rc;
814}
815
Tom Cherryb7349902015-08-26 11:43:36 -0700816static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700818 if (args.size() == 3) {
819 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700821 } else if (args.size() == 4) {
822 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
823 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824 return -errno;
825 } else {
826 return -1;
827 }
828 return 0;
829}
830
831static mode_t get_mode(const char *s) {
832 mode_t mode = 0;
833 while (*s) {
834 if (*s >= '0' && *s <= '7') {
835 mode = (mode<<3) | (*s-'0');
836 } else {
837 return -1;
838 }
839 s++;
840 }
841 return mode;
842}
843
Tom Cherryb7349902015-08-26 11:43:36 -0700844static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700845 mode_t mode = get_mode(args[1].c_str());
846 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700847 return -errno;
848 }
849 return 0;
850}
851
Tom Cherryb7349902015-08-26 11:43:36 -0700852static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400853 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500854
Tom Cherry96f67312015-07-30 13:52:55 -0700855 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
856 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400857 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500858 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400859 return ret;
860}
861
Tom Cherryb7349902015-08-26 11:43:36 -0700862static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400863 int ret = 0;
864
Tom Cherry96f67312015-07-30 13:52:55 -0700865 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
866 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400867 ret = -errno;
868 }
869 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500870}
871
Tom Cherryb7349902015-08-26 11:43:36 -0700872static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700873 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700874 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
875 ERROR("loglevel: invalid log level'%d'\n", log_level);
876 return -EINVAL;
877 }
878 klog_set_level(log_level);
879 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700880}
881
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700882static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700883 load_persist_props();
884 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800885}
886
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700887static int do_load_system_props(const std::vector<std::string>& args) {
888 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700889 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700890}
891
Tom Cherryb7349902015-08-26 11:43:36 -0700892static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700893 if (args.size() == 2) {
894 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
895 } else if (args.size() == 3) {
896 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800897 } else
898 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700899}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000900
Paul Lawrence806d10b2015-04-28 22:07:10 +0000901/*
902 * Callback to make a directory from the ext4 code
903 */
Tom Cherryb7349902015-08-26 11:43:36 -0700904static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000905 if (make_dir(dir, 0700) && errno != EEXIST) {
906 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000907 }
908
Paul Lawrence806d10b2015-04-28 22:07:10 +0000909 return 0;
910}
911
Paul Crowley749af8c2015-05-28 17:35:06 +0100912static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700913 std::string value = property_get("ro.crypto.type");
914 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100915}
916
Tom Cherryb7349902015-08-26 11:43:36 -0700917static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100918 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000919 return 0;
920 }
Tom Cherry96f67312015-07-30 13:52:55 -0700921 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000922 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000923}
Paul Crowley749af8c2015-05-28 17:35:06 +0100924
Paul Crowley59497452016-02-01 16:37:13 +0000925static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000926 return e4crypt_do_init_user0();
927}
928
Tom Cherryb7349902015-08-26 11:43:36 -0700929BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
930 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
931 static const Map builtin_functions = {
932 {"bootchart_init", {0, 0, do_bootchart_init}},
933 {"chmod", {2, 2, do_chmod}},
934 {"chown", {2, 3, do_chown}},
935 {"class_reset", {1, 1, do_class_reset}},
936 {"class_start", {1, 1, do_class_start}},
937 {"class_stop", {1, 1, do_class_stop}},
938 {"copy", {2, 2, do_copy}},
939 {"domainname", {1, 1, do_domainname}},
940 {"enable", {1, 1, do_enable}},
941 {"exec", {1, kMax, do_exec}},
942 {"export", {2, 2, do_export}},
943 {"hostname", {1, 1, do_hostname}},
944 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000945 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700946 {"insmod", {1, kMax, do_insmod}},
947 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700948 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700949 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700950 {"loglevel", {1, 1, do_loglevel}},
951 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800952 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700953 {"mount", {3, kMax, do_mount}},
954 {"powerctl", {1, 1, do_powerctl}},
955 {"restart", {1, 1, do_restart}},
956 {"restorecon", {1, kMax, do_restorecon}},
957 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
958 {"rm", {1, 1, do_rm}},
959 {"rmdir", {1, 1, do_rmdir}},
960 {"setprop", {2, 2, do_setprop}},
961 {"setrlimit", {3, 3, do_setrlimit}},
962 {"start", {1, 1, do_start}},
963 {"stop", {1, 1, do_stop}},
964 {"swapon_all", {1, 1, do_swapon_all}},
965 {"symlink", {2, 2, do_symlink}},
966 {"sysclktz", {1, 1, do_sysclktz}},
967 {"trigger", {1, 1, do_trigger}},
968 {"verity_load_state", {0, 0, do_verity_load_state}},
969 {"verity_update_state", {0, 0, do_verity_update_state}},
970 {"wait", {1, 2, do_wait}},
971 {"write", {2, 2, do_write}},
972 };
973 return builtin_functions;
974}