blob: b3bd418010e1f7d814ba6d2b57bd786cde9df518 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tom Cherryb7349902015-08-26 11:43:36 -070017#include "builtins.h"
18
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070020#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070021#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070023#include <signal.h>
Mark Salyzynad575e02016-04-05 08:10:25 -070024#include <sched.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070025#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070027#include <string.h>
28#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070029#include <sys/mount.h>
30#include <sys/resource.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070031#include <sys/syscall.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>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080044#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080045#include <android-base/stringprintf.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070046#include <cutils/partition_utils.h>
47#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070048#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070049#include <private/android_filesystem_config.h>
50
Tom Cherryfa0c21c2015-07-23 17:53:11 -070051#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070052#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070053#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070054#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070055#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070056#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070057#include "property_service.h"
58#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080059#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070060#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070061
Nick Kralevichbc609542015-01-31 21:39:46 -080062#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070063#define UNMOUNT_CHECK_MS 5000
64#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080065
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080066static const int kTerminateServiceDelayMicroSeconds = 50000;
67
Tom Cherryb7349902015-08-26 11:43:36 -070068static int insmod(const char *filename, const char *options) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070069 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
70 if (fd == -1) {
71 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070072 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080073 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070074 int rc = syscall(__NR_finit_module, fd, options, 0);
75 if (rc == -1) {
76 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
77 }
78 close(fd);
79 return rc;
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
Tom Cherryb7349902015-08-26 11:43:36 -0700109static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700110 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
111 return;
112
113 /* First, lazily unmount the directory. This unmount request finishes when
114 * all processes that open a file or directory in |entry->mnt_dir| exit.
115 */
116 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
117
118 /* Next, kill all processes except init, kthreadd, and kthreadd's
119 * children to finish the lazy unmount. Killing all processes here is okay
120 * because this callback function is only called right before reboot().
121 * It might be cleaner to selectively kill processes that actually use
122 * |entry->mnt_dir| rather than killing all, probably by reusing a function
123 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
124 * not allow init to scan /proc/<pid> files which the utility function
125 * heavily relies on. The policy does not allow the process to execute
126 * killall/pkill binaries either. Note that some processes might
127 * automatically restart after kill(), but that is not really a problem
128 * because |entry->mnt_dir| is no longer visible to such new processes.
129 */
Tom Cherrybac32992015-07-31 12:45:25 -0700130 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700131 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
132
Mark Salyzynad575e02016-04-05 08:10:25 -0700133 // Restart Watchdogd to allow us to complete umounting and fsck
134 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
135 if (svc) {
136 do {
137 sched_yield(); // do not be so eager, let cleanup have priority
138 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
139 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
140 svc->Start();
141 }
142
Yusuke Sato0df08272015-07-08 14:57:07 -0700143 int count = 0;
144 while (count++ < UNMOUNT_CHECK_TIMES) {
145 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
146 if (fd >= 0) {
147 /* |entry->mnt_dir| has sucessfully been unmounted. */
148 close(fd);
149 break;
150 } else if (errno == EBUSY) {
151 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
152 * while then retry.
153 */
154 TEMP_FAILURE_RETRY(
155 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
156 continue;
157 } else {
158 /* Cannot open the device. Give up. */
159 return;
160 }
161 }
162
Mark Salyzynad575e02016-04-05 08:10:25 -0700163 // NB: With watchdog still running, there is no cap on the time it takes
164 // to complete the fsck, from the users perspective the device graphics
165 // and responses are locked-up and they may choose to hold the power
166 // button in frustration if it drags out.
167
Yusuke Sato0df08272015-07-08 14:57:07 -0700168 int st;
169 if (!strcmp(entry->mnt_type, "f2fs")) {
170 const char *f2fs_argv[] = {
171 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
172 };
173 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700174 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700175 } else if (!strcmp(entry->mnt_type, "ext4")) {
176 const char *ext4_argv[] = {
177 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
178 };
179 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700180 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700181 }
182}
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700185 /* Starting a class does not start services
186 * which are explicitly disabled. They must
187 * be started individually.
188 */
Tom Cherrybac32992015-07-31 12:45:25 -0700189 ServiceManager::GetInstance().
190 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191 return 0;
192}
193
Tom Cherryb7349902015-08-26 11:43:36 -0700194static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700195 ServiceManager::GetInstance().
196 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197 return 0;
198}
199
Tom Cherryb7349902015-08-26 11:43:36 -0700200static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700201 ServiceManager::GetInstance().
202 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800203 return 0;
204}
205
Tom Cherryb7349902015-08-26 11:43:36 -0700206static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700207 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700208}
209
Tom Cherryb7349902015-08-26 11:43:36 -0700210static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700211 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
212 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700213 return -1;
214 }
Tom Cherrybac32992015-07-31 12:45:25 -0700215 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700216}
217
Tom Cherryb7349902015-08-26 11:43:36 -0700218static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700219 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
220 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800221 return -1;
222 }
Tom Cherrybac32992015-07-31 12:45:25 -0700223 if (!svc->Start()) {
224 return -1;
225 }
226 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800227 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228}
229
Tom Cherryb7349902015-08-26 11:43:36 -0700230static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700231 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700232}
233
Tom Cherryb7349902015-08-26 11:43:36 -0700234static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700235 return write_file("/proc/sys/kernel/hostname", 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_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700239 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240}
241
Tom Cherryb7349902015-08-26 11:43:36 -0700242static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700243 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800244
Tom Cherry96f67312015-07-30 13:52:55 -0700245 if (args.size() > 2) {
246 options += args[2];
247 for (std::size_t i = 3; i < args.size(); ++i) {
248 options += ' ';
249 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800250 }
251 }
252
Tom Cherry96f67312015-07-30 13:52:55 -0700253 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800254}
255
Tom Cherryb7349902015-08-26 11:43:36 -0700256static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700258 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259
260 /* mkdir <path> [mode] [owner] [group] */
261
Tom Cherry96f67312015-07-30 13:52:55 -0700262 if (args.size() >= 3) {
263 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700264 }
265
Tom Cherry96f67312015-07-30 13:52:55 -0700266 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700267 /* chmod in case the directory already exists */
268 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700269 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700270 }
271 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272 return -errno;
273 }
274
Tom Cherry96f67312015-07-30 13:52:55 -0700275 if (args.size() >= 4) {
276 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700277 gid_t gid = -1;
278
Tom Cherry96f67312015-07-30 13:52:55 -0700279 if (args.size() == 5) {
280 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281 }
282
Tom Cherry96f67312015-07-30 13:52:55 -0700283 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284 return -errno;
285 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700286
287 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
288 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700289 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700290 if (ret == -1) {
291 return -errno;
292 }
293 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 }
295
Tom Cherry96f67312015-07-30 13:52:55 -0700296 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700297}
298
299static struct {
300 const char *name;
301 unsigned flag;
302} mount_flags[] = {
303 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200304 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700305 { "nosuid", MS_NOSUID },
306 { "nodev", MS_NODEV },
307 { "nodiratime", MS_NODIRATIME },
308 { "ro", MS_RDONLY },
309 { "rw", 0 },
310 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700311 { "bind", MS_BIND },
312 { "rec", MS_REC },
313 { "unbindable", MS_UNBINDABLE },
314 { "private", MS_PRIVATE },
315 { "slave", MS_SLAVE },
316 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700317 { "defaults", 0 },
318 { 0, 0 },
319};
320
Ken Sumrall752923c2010-12-03 16:33:31 -0800321#define DATA_MNT_POINT "/data"
322
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700324static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700326 const char *source, *target, *system;
327 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700329 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700331 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332
Tom Cherry96f67312015-07-30 13:52:55 -0700333 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700335 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 flags |= mount_flags[i].flag;
337 break;
338 }
339 }
340
Colin Crosscd0f1732010-04-19 17:10:24 -0700341 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700342 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700343 wait = 1;
344 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700345 else if (na + 1 == args.size())
346 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700347 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 }
349
Tom Cherry96f67312015-07-30 13:52:55 -0700350 system = args[1].c_str();
351 source = args[2].c_str();
352 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000353
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 if (!strncmp(source, "mtd@", 4)) {
355 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000356 if (n < 0) {
357 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359
Yabin Cuie2d63af2015-02-17 19:27:51 -0800360 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000361
Colin Crosscd0f1732010-04-19 17:10:24 -0700362 if (wait)
363 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000364 if (mount(tmp, target, system, flags, options) < 0) {
365 return -1;
366 }
367
Ken Sumralldd4d7862011-02-17 18:09:47 -0800368 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000369 } else if (!strncmp(source, "loop@", 5)) {
370 int mode, loop, fd;
371 struct loop_info info;
372
373 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800374 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000375 if (fd < 0) {
376 return -1;
377 }
378
379 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800380 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800381 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000382 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100383 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384 return -1;
385 }
386
387 /* if it is a blank loop device */
388 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
389 /* if it becomes our loop device */
390 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
391 close(fd);
392
393 if (mount(tmp, target, system, flags, options) < 0) {
394 ioctl(loop, LOOP_CLR_FD, 0);
395 close(loop);
396 return -1;
397 }
398
399 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800400 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000401 }
402 }
403
404 close(loop);
405 }
406
407 close(fd);
408 ERROR("out of loopback devices");
409 return -1;
410 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700411 if (wait)
412 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700414 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415 }
416
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800418
419exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800420 return 0;
421
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700422}
423
Tom Cherryb7349902015-08-26 11:43:36 -0700424static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700425 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800426 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700427 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700428 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
429 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700430 close(fd);
431 } else {
432 ERROR("could not open /cache/recovery/command\n");
433 return -1;
434 }
435 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
436 while (1) { pause(); } // never reached
437}
438
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800439/* Imports .rc files from the specified paths. Default ones are applied if none is given.
440 *
441 * start_index: index of the first path in the args list
442 */
443static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700444 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800445 if (args.size() <= start_index) {
446 // Use the default set if no path is given
447 static const std::vector<std::string> init_directories = {
448 "/system/etc/init",
449 "/vendor/etc/init",
450 "/odm/etc/init"
451 };
452
453 for (const auto& dir : init_directories) {
454 parser.ParseConfig(dir);
455 }
456 } else {
457 for (size_t i = start_index; i < args.size(); ++i) {
458 parser.ParseConfig(args[i]);
459 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700460 }
461}
462
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800463/* mount_all <fstab> [ <path> ]*
464 *
JP Abgrallcee20682014-07-02 14:26:54 -0700465 * This function might request a reboot, in which case it will
466 * not return.
467 */
Tom Cherryb7349902015-08-26 11:43:36 -0700468static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700469 pid_t pid;
470 int ret = -1;
471 int child_ret = -1;
472 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800473 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700474
Tom Cherry96f67312015-07-30 13:52:55 -0700475 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700476 /*
477 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
478 * do the call in the child to provide protection to the main init
479 * process if anything goes wrong (crash or memory leak), and wait for
480 * the child to finish in the parent.
481 */
482 pid = fork();
483 if (pid > 0) {
484 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700485 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
486 if (wp_ret < 0) {
487 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700488 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700489 }
490
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700491 if (WIFEXITED(status)) {
492 ret = WEXITSTATUS(status);
493 } else {
494 ret = -1;
495 }
496 } else if (pid == 0) {
497 /* child, call fs_mgr_mount_all() */
498 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800499 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800500 child_ret = fs_mgr_mount_all(fstab);
501 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700502 if (child_ret == -1) {
503 ERROR("fs_mgr_mount_all returned an error\n");
504 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700505 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700506 } else {
507 /* fork failed, return an error */
508 return -1;
509 }
510
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800511 /* Paths of .rc files are specified at the 2nd argument and beyond */
512 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700513
JP Abgrallf22b7452014-07-02 13:16:04 -0700514 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800515 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700516 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700517 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000518 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800519 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700520 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 property_set("ro.crypto.state", "unencrypted");
522 /* If fs_mgr determined this is an unencrypted device, then trigger
523 * that action.
524 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700525 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700526 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
527 /* Setup a wipe via recovery, and reboot into recovery */
528 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
529 ret = wipe_data_via_recovery();
530 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000531 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000532 if (e4crypt_install_keyring()) {
533 return -1;
534 }
535 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000536 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000537
538 // Although encrypted, we have device key, so we do not need to
539 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700540 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000541 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
542 if (e4crypt_install_keyring()) {
543 return -1;
544 }
545 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000546 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000547 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700548 } else if (ret > 0) {
549 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700550 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700551 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700552
553 return ret;
554}
555
Tom Cherryb7349902015-08-26 11:43:36 -0700556static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700557 struct fstab *fstab;
558 int ret;
559
Tom Cherry96f67312015-07-30 13:52:55 -0700560 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700561 ret = fs_mgr_swapon_all(fstab);
562 fs_mgr_free_fstab(fstab);
563
564 return ret;
565}
566
Tom Cherryb7349902015-08-26 11:43:36 -0700567static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700568 const char* name = args[1].c_str();
569 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700570 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571 return 0;
572}
573
Tom Cherryb7349902015-08-26 11:43:36 -0700574static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700575 struct rlimit limit;
576 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700577 resource = std::stoi(args[1]);
578 limit.rlim_cur = std::stoi(args[2]);
579 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700580 return setrlimit(resource, &limit);
581}
582
Tom Cherryb7349902015-08-26 11:43:36 -0700583static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700584 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
585 if (!svc) {
586 ERROR("do_start: Service %s not found\n", args[1].c_str());
587 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588 }
Tom Cherrybac32992015-07-31 12:45:25 -0700589 if (!svc->Start())
590 return -1;
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_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700595 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
596 if (!svc) {
597 ERROR("do_stop: Service %s not found\n", args[1].c_str());
598 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700599 }
Tom Cherrybac32992015-07-31 12:45:25 -0700600 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700601 return 0;
602}
603
Tom Cherryb7349902015-08-26 11:43:36 -0700604static int do_restart(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_restart: 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 svc->Restart();
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_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700615 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700616 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700617 unsigned int cmd = 0;
618 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700619 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700620
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700621 if (strncmp(command, "shutdown", 8) == 0) {
622 cmd = ANDROID_RB_POWEROFF;
623 len = 8;
624 } else if (strncmp(command, "reboot", 6) == 0) {
625 cmd = ANDROID_RB_RESTART2;
626 len = 6;
627 } else {
628 ERROR("powerctl: unrecognized command '%s'\n", command);
629 return -EINVAL;
630 }
631
632 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700633 if (cmd == ANDROID_RB_POWEROFF &&
634 !strcmp(&command[len + 1], "userrequested")) {
635 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
636 // Run fsck once the file system is remounted in read-only mode.
637 callback_on_ro_remount = unmount_and_fsck;
638 } else if (cmd == ANDROID_RB_RESTART2) {
639 reboot_target = &command[len + 1];
640 }
641 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700642 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
643 return -EINVAL;
644 }
645
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800646 std::string timeout = property_get("ro.build.shutdown_timeout");
647 unsigned int delay = 0;
648
649 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
650 Timer t;
651 // Ask all services to terminate.
652 ServiceManager::GetInstance().ForEachService(
653 [] (Service* s) { s->Terminate(); });
654
655 while (t.duration() < delay) {
656 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
657
658 int service_count = 0;
659 ServiceManager::GetInstance().ForEachService(
660 [&service_count] (Service* s) {
661 // Count the number of services running.
662 // Exclude the console as it will ignore the SIGTERM signal
663 // and not exit.
664 // Note: SVC_CONSOLE actually means "requires console" but
665 // it is only used by the shell.
666 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
667 service_count++;
668 }
669 });
670
671 if (service_count == 0) {
672 // All terminable services terminated. We can exit early.
673 break;
674 }
675
676 // Wait a bit before recounting the number or running services.
677 usleep(kTerminateServiceDelayMicroSeconds);
678 }
679 NOTICE("Terminating running services took %.02f seconds", t.duration());
680 }
681
Yusuke Sato0df08272015-07-08 14:57:07 -0700682 return android_reboot_with_callback(cmd, 0, reboot_target,
683 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700684}
685
Tom Cherryb7349902015-08-26 11:43:36 -0700686static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700687 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700688 return 0;
689}
690
Tom Cherryb7349902015-08-26 11:43:36 -0700691static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700692 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700693}
694
Tom Cherryb7349902015-08-26 11:43:36 -0700695static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700696 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800697}
698
Tom Cherryb7349902015-08-26 11:43:36 -0700699static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700700 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800701}
702
Tom Cherryb7349902015-08-26 11:43:36 -0700703static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800704 struct timezone tz;
705
The Android Open Source Project35237d12008-12-17 18:08:08 -0800706 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700707 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800708 if (settimeofday(NULL, &tz))
709 return -1;
710 return 0;
711}
712
Tom Cherryb7349902015-08-26 11:43:36 -0700713static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700714 int mode = -1;
715 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000716 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700717 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000718 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700719 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000720}
721
Tom Cherryb7349902015-08-26 11:43:36 -0700722static void verity_update_property(fstab_rec *fstab, const char *mount_point,
723 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100724 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
725 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000726}
727
Tom Cherryb7349902015-08-26 11:43:36 -0700728static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700729 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000730}
731
Tom Cherryb7349902015-08-26 11:43:36 -0700732static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700733 const char* path = args[1].c_str();
734 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700735 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700736}
737
Tom Cherryb7349902015-08-26 11:43:36 -0700738static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700739 char *buffer = NULL;
740 int rc = 0;
741 int fd1 = -1, fd2 = -1;
742 struct stat info;
743 int brtw, brtr;
744 char *p;
745
Tom Cherry96f67312015-07-30 13:52:55 -0700746 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700747 return -1;
748
Tom Cherry96f67312015-07-30 13:52:55 -0700749 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700750 goto out_err;
751
Tom Cherry96f67312015-07-30 13:52:55 -0700752 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700753 goto out_err;
754
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800755 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700756 goto out_err;
757
758 p = buffer;
759 brtr = info.st_size;
760 while(brtr) {
761 rc = read(fd1, p, brtr);
762 if (rc < 0)
763 goto out_err;
764 if (rc == 0)
765 break;
766 p += rc;
767 brtr -= rc;
768 }
769
770 p = buffer;
771 brtw = info.st_size;
772 while(brtw) {
773 rc = write(fd2, p, brtw);
774 if (rc < 0)
775 goto out_err;
776 if (rc == 0)
777 break;
778 p += rc;
779 brtw -= rc;
780 }
781
782 rc = 0;
783 goto out;
784out_err:
785 rc = -1;
786out:
787 if (buffer)
788 free(buffer);
789 if (fd1 >= 0)
790 close(fd1);
791 if (fd2 >= 0)
792 close(fd2);
793 return rc;
794}
795
Tom Cherryb7349902015-08-26 11:43:36 -0700796static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700797 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700798 if (args.size() == 3) {
799 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700800 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700801 } else if (args.size() == 4) {
802 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
803 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700804 return -errno;
805 } else {
806 return -1;
807 }
808 return 0;
809}
810
811static mode_t get_mode(const char *s) {
812 mode_t mode = 0;
813 while (*s) {
814 if (*s >= '0' && *s <= '7') {
815 mode = (mode<<3) | (*s-'0');
816 } else {
817 return -1;
818 }
819 s++;
820 }
821 return mode;
822}
823
Tom Cherryb7349902015-08-26 11:43:36 -0700824static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700825 mode_t mode = get_mode(args[1].c_str());
826 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 return -errno;
828 }
829 return 0;
830}
831
Tom Cherryb7349902015-08-26 11:43:36 -0700832static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400833 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500834
Tom Cherry96f67312015-07-30 13:52:55 -0700835 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
836 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400837 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500838 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400839 return ret;
840}
841
Tom Cherryb7349902015-08-26 11:43:36 -0700842static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400843 int ret = 0;
844
Tom Cherry96f67312015-07-30 13:52:55 -0700845 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
846 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400847 ret = -errno;
848 }
849 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500850}
851
Tom Cherryb7349902015-08-26 11:43:36 -0700852static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700853 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700854 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
855 ERROR("loglevel: invalid log level'%d'\n", log_level);
856 return -EINVAL;
857 }
858 klog_set_level(log_level);
859 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700860}
861
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700862static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700863 load_persist_props();
864 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800865}
866
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700867static int do_load_system_props(const std::vector<std::string>& args) {
868 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700869 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700870}
871
Tom Cherryb7349902015-08-26 11:43:36 -0700872static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700873 if (args.size() == 2) {
874 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
875 } else if (args.size() == 3) {
876 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800877 } else
878 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700879}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000880
Paul Lawrence806d10b2015-04-28 22:07:10 +0000881/*
882 * Callback to make a directory from the ext4 code
883 */
Tom Cherryb7349902015-08-26 11:43:36 -0700884static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000885 if (make_dir(dir, 0700) && errno != EEXIST) {
886 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000887 }
888
Paul Lawrence806d10b2015-04-28 22:07:10 +0000889 return 0;
890}
891
Paul Crowley749af8c2015-05-28 17:35:06 +0100892static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700893 std::string value = property_get("ro.crypto.type");
894 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100895}
896
Tom Cherryb7349902015-08-26 11:43:36 -0700897static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100898 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000899 return 0;
900 }
Tom Cherry96f67312015-07-30 13:52:55 -0700901 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000902 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000903}
Paul Crowley749af8c2015-05-28 17:35:06 +0100904
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700905static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100906 if (!is_file_crypto()) {
907 return 0;
908 }
Tom Cherry087cd352015-08-03 14:19:35 -0700909 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100910}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700911
Tom Cherryb7349902015-08-26 11:43:36 -0700912BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
913 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
914 static const Map builtin_functions = {
915 {"bootchart_init", {0, 0, do_bootchart_init}},
916 {"chmod", {2, 2, do_chmod}},
917 {"chown", {2, 3, do_chown}},
918 {"class_reset", {1, 1, do_class_reset}},
919 {"class_start", {1, 1, do_class_start}},
920 {"class_stop", {1, 1, do_class_stop}},
921 {"copy", {2, 2, do_copy}},
922 {"domainname", {1, 1, do_domainname}},
923 {"enable", {1, 1, do_enable}},
924 {"exec", {1, kMax, do_exec}},
925 {"export", {2, 2, do_export}},
926 {"hostname", {1, 1, do_hostname}},
927 {"ifup", {1, 1, do_ifup}},
928 {"insmod", {1, kMax, do_insmod}},
929 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700930 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700931 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700932 {"loglevel", {1, 1, do_loglevel}},
933 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800934 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700935 {"mount", {3, kMax, do_mount}},
936 {"powerctl", {1, 1, do_powerctl}},
937 {"restart", {1, 1, do_restart}},
938 {"restorecon", {1, kMax, do_restorecon}},
939 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
940 {"rm", {1, 1, do_rm}},
941 {"rmdir", {1, 1, do_rmdir}},
942 {"setprop", {2, 2, do_setprop}},
943 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700944 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700945 {"start", {1, 1, do_start}},
946 {"stop", {1, 1, do_stop}},
947 {"swapon_all", {1, 1, do_swapon_all}},
948 {"symlink", {2, 2, do_symlink}},
949 {"sysclktz", {1, 1, do_sysclktz}},
950 {"trigger", {1, 1, do_trigger}},
951 {"verity_load_state", {0, 0, do_verity_load_state}},
952 {"verity_update_state", {0, 0, do_verity_update_state}},
953 {"wait", {1, 2, do_wait}},
954 {"write", {2, 2, do_write}},
955 };
956 return builtin_functions;
957}