blob: f3f04c2ad8da37ab9aad229f15e31fce9fa73e4f [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 Crowleyaf8be582016-05-10 08:52:06 -070039#include <ext4_crypt.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000040#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042#include <selinux/selinux.h>
43#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044
Elliott Hughesdb3f2672015-03-20 09:45:18 -070045#include <fs_mgr.h>
Mark Salyzyn402fb792016-04-05 13:43:40 -070046#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080047#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080048#include <android-base/stringprintf.h>
Yabin Cui46e03792016-04-06 15:56:49 -070049#include <bootloader_message_writer.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070050#include <cutils/partition_utils.h>
51#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070052#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070053#include <private/android_filesystem_config.h>
54
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070056#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070058#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070059#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070060#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "property_service.h"
62#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080063#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070064#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065
Nick Kralevichbc609542015-01-31 21:39:46 -080066#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070067#define UNMOUNT_CHECK_MS 5000
68#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080069
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080070static const int kTerminateServiceDelayMicroSeconds = 50000;
71
Tom Cherryb7349902015-08-26 11:43:36 -070072static int insmod(const char *filename, const char *options) {
Nick Kralevichf2049162016-03-27 16:55:59 -070073 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
74 if (fd == -1) {
75 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080077 }
Nick Kralevichf2049162016-03-27 16:55:59 -070078 int rc = syscall(__NR_finit_module, fd, options, 0);
79 if (rc == -1) {
80 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
81 }
82 close(fd);
83 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084}
85
Tom Cherryb7349902015-08-26 11:43:36 -070086static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087 struct ifreq ifr;
88 int s, ret;
89
90 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
91
92 s = socket(AF_INET, SOCK_DGRAM, 0);
93 if (s < 0)
94 return -1;
95
96 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
97 if (ret < 0) {
98 goto done;
99 }
100
101 if (up)
102 ifr.ifr_flags |= IFF_UP;
103 else
104 ifr.ifr_flags &= ~IFF_UP;
105
106 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800107
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108done:
109 close(s);
110 return ret;
111}
112
Mark Salyzyn402fb792016-04-05 13:43:40 -0700113// Turn off backlight while we are performing power down cleanup activities.
114static void turnOffBacklight() {
115 static const char off[] = "0";
116
117 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
118
119 static const char backlightDir[] = "/sys/class/backlight";
120 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
121 if (!dir) {
122 return;
123 }
124
125 struct dirent *dp;
126 while ((dp = readdir(dir.get())) != NULL) {
127 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
128 (dp->d_name[0] == '.')) {
129 continue;
130 }
131
132 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
133 backlightDir,
134 dp->d_name);
135 android::base::WriteStringToFile(off, fileName);
136 }
137}
138
Paul Crowleyaf8be582016-05-10 08:52:06 -0700139static int wipe_data_via_recovery(const std::string& reason) {
140 const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
141 std::string err;
142 if (!write_bootloader_message(options, &err)) {
143 ERROR("failed to set bootloader message: %s", err.c_str());
144 return -1;
145 }
146 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
147 while (1) { pause(); } // never reached
148}
149
Tom Cherryb7349902015-08-26 11:43:36 -0700150static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700151 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
152 return;
153
154 /* First, lazily unmount the directory. This unmount request finishes when
155 * all processes that open a file or directory in |entry->mnt_dir| exit.
156 */
157 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
158
159 /* Next, kill all processes except init, kthreadd, and kthreadd's
160 * children to finish the lazy unmount. Killing all processes here is okay
161 * because this callback function is only called right before reboot().
162 * It might be cleaner to selectively kill processes that actually use
163 * |entry->mnt_dir| rather than killing all, probably by reusing a function
164 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
165 * not allow init to scan /proc/<pid> files which the utility function
166 * heavily relies on. The policy does not allow the process to execute
167 * killall/pkill binaries either. Note that some processes might
168 * automatically restart after kill(), but that is not really a problem
169 * because |entry->mnt_dir| is no longer visible to such new processes.
170 */
Tom Cherrybac32992015-07-31 12:45:25 -0700171 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700172 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
173
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700174 // Restart Watchdogd to allow us to complete umounting and fsck
175 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
176 if (svc) {
177 do {
178 sched_yield(); // do not be so eager, let cleanup have priority
179 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
180 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
181 svc->Start();
182 }
183
Mark Salyzyn402fb792016-04-05 13:43:40 -0700184 turnOffBacklight();
185
Yusuke Sato0df08272015-07-08 14:57:07 -0700186 int count = 0;
187 while (count++ < UNMOUNT_CHECK_TIMES) {
188 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
189 if (fd >= 0) {
190 /* |entry->mnt_dir| has sucessfully been unmounted. */
191 close(fd);
192 break;
193 } else if (errno == EBUSY) {
194 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
195 * while then retry.
196 */
197 TEMP_FAILURE_RETRY(
198 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
199 continue;
200 } else {
201 /* Cannot open the device. Give up. */
202 return;
203 }
204 }
205
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700206 // NB: With watchdog still running, there is no cap on the time it takes
207 // to complete the fsck, from the users perspective the device graphics
208 // and responses are locked-up and they may choose to hold the power
209 // button in frustration if it drags out.
210
Yusuke Sato0df08272015-07-08 14:57:07 -0700211 int st;
212 if (!strcmp(entry->mnt_type, "f2fs")) {
213 const char *f2fs_argv[] = {
214 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
215 };
216 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700217 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700218 } else if (!strcmp(entry->mnt_type, "ext4")) {
219 const char *ext4_argv[] = {
220 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
221 };
222 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700223 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700224 }
225}
226
Tom Cherryb7349902015-08-26 11:43:36 -0700227static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228 /* Starting a class does not start services
229 * which are explicitly disabled. They must
230 * be started individually.
231 */
Tom Cherrybac32992015-07-31 12:45:25 -0700232 ServiceManager::GetInstance().
233 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234 return 0;
235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700238 ServiceManager::GetInstance().
239 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240 return 0;
241}
242
Tom Cherryb7349902015-08-26 11:43:36 -0700243static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700244 ServiceManager::GetInstance().
245 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800246 return 0;
247}
248
Tom Cherryb7349902015-08-26 11:43:36 -0700249static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700250 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251}
252
Tom Cherryb7349902015-08-26 11:43:36 -0700253static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700254 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
255 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700256 return -1;
257 }
Tom Cherrybac32992015-07-31 12:45:25 -0700258 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700262 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
263 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800264 return -1;
265 }
Tom Cherrybac32992015-07-31 12:45:25 -0700266 if (!svc->Start()) {
267 return -1;
268 }
269 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800270 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271}
272
Tom Cherryb7349902015-08-26 11:43:36 -0700273static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700274 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275}
276
Tom Cherryb7349902015-08-26 11:43:36 -0700277static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700278 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279}
280
Tom Cherryb7349902015-08-26 11:43:36 -0700281static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700282 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283}
284
Tom Cherryb7349902015-08-26 11:43:36 -0700285static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700286 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800287
Tom Cherry96f67312015-07-30 13:52:55 -0700288 if (args.size() > 2) {
289 options += args[2];
290 for (std::size_t i = 3; i < args.size(); ++i) {
291 options += ' ';
292 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800293 }
294 }
295
Tom Cherry96f67312015-07-30 13:52:55 -0700296 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800297}
298
Tom Cherryb7349902015-08-26 11:43:36 -0700299static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700301 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
303 /* mkdir <path> [mode] [owner] [group] */
304
Tom Cherry96f67312015-07-30 13:52:55 -0700305 if (args.size() >= 3) {
306 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 }
308
Tom Cherry96f67312015-07-30 13:52:55 -0700309 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700310 /* chmod in case the directory already exists */
311 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700312 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700313 }
314 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 return -errno;
316 }
317
Tom Cherry96f67312015-07-30 13:52:55 -0700318 if (args.size() >= 4) {
319 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 gid_t gid = -1;
321
Tom Cherry96f67312015-07-30 13:52:55 -0700322 if (args.size() == 5) {
323 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
325
Tom Cherry96f67312015-07-30 13:52:55 -0700326 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 return -errno;
328 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700329
330 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
331 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700332 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700333 if (ret == -1) {
334 return -errno;
335 }
336 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 }
338
Paul Crowleyaf8be582016-05-10 08:52:06 -0700339 if (e4crypt_is_native()) {
340 if (e4crypt_set_directory_policy(args[1].c_str())) {
341 wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
342 return -1;
343 }
344 }
345 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346}
347
348static struct {
349 const char *name;
350 unsigned flag;
351} mount_flags[] = {
352 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200353 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 { "nosuid", MS_NOSUID },
355 { "nodev", MS_NODEV },
356 { "nodiratime", MS_NODIRATIME },
357 { "ro", MS_RDONLY },
358 { "rw", 0 },
359 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700360 { "bind", MS_BIND },
361 { "rec", MS_REC },
362 { "unbindable", MS_UNBINDABLE },
363 { "private", MS_PRIVATE },
364 { "slave", MS_SLAVE },
365 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 { "defaults", 0 },
367 { 0, 0 },
368};
369
Ken Sumrall752923c2010-12-03 16:33:31 -0800370#define DATA_MNT_POINT "/data"
371
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700373static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700375 const char *source, *target, *system;
376 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700378 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700380 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700381
Tom Cherry96f67312015-07-30 13:52:55 -0700382 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700384 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 flags |= mount_flags[i].flag;
386 break;
387 }
388 }
389
Colin Crosscd0f1732010-04-19 17:10:24 -0700390 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700391 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700392 wait = 1;
393 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700394 else if (na + 1 == args.size())
395 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700396 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 }
398
Tom Cherry96f67312015-07-30 13:52:55 -0700399 system = args[1].c_str();
400 source = args[2].c_str();
401 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000402
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 if (!strncmp(source, "mtd@", 4)) {
404 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000405 if (n < 0) {
406 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000408
Yabin Cuie2d63af2015-02-17 19:27:51 -0800409 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410
Colin Crosscd0f1732010-04-19 17:10:24 -0700411 if (wait)
412 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 if (mount(tmp, target, system, flags, options) < 0) {
414 return -1;
415 }
416
Ken Sumralldd4d7862011-02-17 18:09:47 -0800417 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 } else if (!strncmp(source, "loop@", 5)) {
419 int mode, loop, fd;
420 struct loop_info info;
421
422 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800423 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000424 if (fd < 0) {
425 return -1;
426 }
427
428 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800429 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800430 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000431 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100432 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000433 return -1;
434 }
435
436 /* if it is a blank loop device */
437 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
438 /* if it becomes our loop device */
439 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
440 close(fd);
441
442 if (mount(tmp, target, system, flags, options) < 0) {
443 ioctl(loop, LOOP_CLR_FD, 0);
444 close(loop);
445 return -1;
446 }
447
448 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800449 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000450 }
451 }
452
453 close(loop);
454 }
455
456 close(fd);
457 ERROR("out of loopback devices");
458 return -1;
459 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700460 if (wait)
461 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000462 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700463 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000464 }
465
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700466 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800467
468exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800469 return 0;
470
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471}
472
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800473/* Imports .rc files from the specified paths. Default ones are applied if none is given.
474 *
475 * start_index: index of the first path in the args list
476 */
477static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700478 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800479 if (args.size() <= start_index) {
480 // Use the default set if no path is given
481 static const std::vector<std::string> init_directories = {
482 "/system/etc/init",
483 "/vendor/etc/init",
484 "/odm/etc/init"
485 };
486
487 for (const auto& dir : init_directories) {
488 parser.ParseConfig(dir);
489 }
490 } else {
491 for (size_t i = start_index; i < args.size(); ++i) {
492 parser.ParseConfig(args[i]);
493 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700494 }
495}
496
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800497/* mount_all <fstab> [ <path> ]*
498 *
JP Abgrallcee20682014-07-02 14:26:54 -0700499 * This function might request a reboot, in which case it will
500 * not return.
501 */
Tom Cherryb7349902015-08-26 11:43:36 -0700502static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700503 pid_t pid;
504 int ret = -1;
505 int child_ret = -1;
506 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800507 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700508
Tom Cherry96f67312015-07-30 13:52:55 -0700509 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700510 /*
511 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
512 * do the call in the child to provide protection to the main init
513 * process if anything goes wrong (crash or memory leak), and wait for
514 * the child to finish in the parent.
515 */
516 pid = fork();
517 if (pid > 0) {
518 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700519 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
520 if (wp_ret < 0) {
521 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700522 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700523 }
524
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700525 if (WIFEXITED(status)) {
526 ret = WEXITSTATUS(status);
527 } else {
528 ret = -1;
529 }
530 } else if (pid == 0) {
531 /* child, call fs_mgr_mount_all() */
532 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800533 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800534 child_ret = fs_mgr_mount_all(fstab);
535 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700536 if (child_ret == -1) {
537 ERROR("fs_mgr_mount_all returned an error\n");
538 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700539 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700540 } else {
541 /* fork failed, return an error */
542 return -1;
543 }
544
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800545 /* Paths of .rc files are specified at the 2nd argument and beyond */
546 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700547
JP Abgrallf22b7452014-07-02 13:16:04 -0700548 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700549 ActionManager::GetInstance().QueueEventTrigger("encrypt");
JP Abgrallf22b7452014-07-02 13:16:04 -0700550 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700551 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000552 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700553 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
JP Abgrallf22b7452014-07-02 13:16:04 -0700554 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700555 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800556 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
557 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
558 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700559 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700560 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
561 /* Setup a wipe via recovery, and reboot into recovery */
562 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
Paul Crowleyaf8be582016-05-10 08:52:06 -0700563 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700564 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800565 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000566 if (e4crypt_install_keyring()) {
567 return -1;
568 }
569 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000570 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000571
572 // Although encrypted, we have device key, so we do not need to
573 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700574 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700575 } else if (ret > 0) {
576 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700577 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700578 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700579
580 return ret;
581}
582
Tom Cherryb7349902015-08-26 11:43:36 -0700583static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700584 struct fstab *fstab;
585 int ret;
586
Tom Cherry96f67312015-07-30 13:52:55 -0700587 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700588 ret = fs_mgr_swapon_all(fstab);
589 fs_mgr_free_fstab(fstab);
590
591 return ret;
592}
593
Tom Cherryb7349902015-08-26 11:43:36 -0700594static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700595 const char* name = args[1].c_str();
596 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700597 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 return 0;
599}
600
Tom Cherryb7349902015-08-26 11:43:36 -0700601static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602 struct rlimit limit;
603 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700604 resource = std::stoi(args[1]);
605 limit.rlim_cur = std::stoi(args[2]);
606 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700607 return setrlimit(resource, &limit);
608}
609
Tom Cherryb7349902015-08-26 11:43:36 -0700610static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700611 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
612 if (!svc) {
613 ERROR("do_start: Service %s not found\n", args[1].c_str());
614 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700615 }
Tom Cherrybac32992015-07-31 12:45:25 -0700616 if (!svc->Start())
617 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700618 return 0;
619}
620
Tom Cherryb7349902015-08-26 11:43:36 -0700621static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700622 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
623 if (!svc) {
624 ERROR("do_stop: Service %s not found\n", args[1].c_str());
625 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700626 }
Tom Cherrybac32992015-07-31 12:45:25 -0700627 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700628 return 0;
629}
630
Tom Cherryb7349902015-08-26 11:43:36 -0700631static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700632 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
633 if (!svc) {
634 ERROR("do_restart: Service %s not found\n", args[1].c_str());
635 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700636 }
Tom Cherrybac32992015-07-31 12:45:25 -0700637 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700638 return 0;
639}
640
Tom Cherryb7349902015-08-26 11:43:36 -0700641static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700642 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700643 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700644 unsigned int cmd = 0;
645 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700646 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700647
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700648 if (strncmp(command, "shutdown", 8) == 0) {
649 cmd = ANDROID_RB_POWEROFF;
650 len = 8;
651 } else if (strncmp(command, "reboot", 6) == 0) {
652 cmd = ANDROID_RB_RESTART2;
653 len = 6;
654 } else {
655 ERROR("powerctl: unrecognized command '%s'\n", command);
656 return -EINVAL;
657 }
658
659 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700660 if (cmd == ANDROID_RB_POWEROFF &&
661 !strcmp(&command[len + 1], "userrequested")) {
662 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
663 // Run fsck once the file system is remounted in read-only mode.
664 callback_on_ro_remount = unmount_and_fsck;
665 } else if (cmd == ANDROID_RB_RESTART2) {
666 reboot_target = &command[len + 1];
667 }
668 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700669 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
670 return -EINVAL;
671 }
672
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800673 std::string timeout = property_get("ro.build.shutdown_timeout");
674 unsigned int delay = 0;
675
676 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
677 Timer t;
678 // Ask all services to terminate.
679 ServiceManager::GetInstance().ForEachService(
680 [] (Service* s) { s->Terminate(); });
681
682 while (t.duration() < delay) {
683 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
684
685 int service_count = 0;
686 ServiceManager::GetInstance().ForEachService(
687 [&service_count] (Service* s) {
688 // Count the number of services running.
689 // Exclude the console as it will ignore the SIGTERM signal
690 // and not exit.
691 // Note: SVC_CONSOLE actually means "requires console" but
692 // it is only used by the shell.
693 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
694 service_count++;
695 }
696 });
697
698 if (service_count == 0) {
699 // All terminable services terminated. We can exit early.
700 break;
701 }
702
703 // Wait a bit before recounting the number or running services.
704 usleep(kTerminateServiceDelayMicroSeconds);
705 }
706 NOTICE("Terminating running services took %.02f seconds", t.duration());
707 }
708
Yusuke Sato0df08272015-07-08 14:57:07 -0700709 return android_reboot_with_callback(cmd, 0, reboot_target,
710 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700711}
712
Tom Cherryb7349902015-08-26 11:43:36 -0700713static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700714 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700715 return 0;
716}
717
Tom Cherryb7349902015-08-26 11:43:36 -0700718static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700719 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720}
721
Tom Cherryb7349902015-08-26 11:43:36 -0700722static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700723 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800724}
725
Tom Cherryb7349902015-08-26 11:43:36 -0700726static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700727 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800728}
729
Tom Cherryb7349902015-08-26 11:43:36 -0700730static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800731 struct timezone tz;
732
The Android Open Source Project35237d12008-12-17 18:08:08 -0800733 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700734 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800735 if (settimeofday(NULL, &tz))
736 return -1;
737 return 0;
738}
739
Tom Cherryb7349902015-08-26 11:43:36 -0700740static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700741 int mode = -1;
742 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000743 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700744 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000745 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700746 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000747}
748
Tom Cherryb7349902015-08-26 11:43:36 -0700749static void verity_update_property(fstab_rec *fstab, const char *mount_point,
750 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100751 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
752 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000753}
754
Tom Cherryb7349902015-08-26 11:43:36 -0700755static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700756 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000757}
758
Tom Cherryb7349902015-08-26 11:43:36 -0700759static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700760 const char* path = args[1].c_str();
761 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700762 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700763}
764
Tom Cherryb7349902015-08-26 11:43:36 -0700765static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700766 char *buffer = NULL;
767 int rc = 0;
768 int fd1 = -1, fd2 = -1;
769 struct stat info;
770 int brtw, brtr;
771 char *p;
772
Tom Cherry96f67312015-07-30 13:52:55 -0700773 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700774 return -1;
775
Tom Cherry96f67312015-07-30 13:52:55 -0700776 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700777 goto out_err;
778
Tom Cherry96f67312015-07-30 13:52:55 -0700779 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700780 goto out_err;
781
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800782 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700783 goto out_err;
784
785 p = buffer;
786 brtr = info.st_size;
787 while(brtr) {
788 rc = read(fd1, p, brtr);
789 if (rc < 0)
790 goto out_err;
791 if (rc == 0)
792 break;
793 p += rc;
794 brtr -= rc;
795 }
796
797 p = buffer;
798 brtw = info.st_size;
799 while(brtw) {
800 rc = write(fd2, p, brtw);
801 if (rc < 0)
802 goto out_err;
803 if (rc == 0)
804 break;
805 p += rc;
806 brtw -= rc;
807 }
808
809 rc = 0;
810 goto out;
811out_err:
812 rc = -1;
813out:
814 if (buffer)
815 free(buffer);
816 if (fd1 >= 0)
817 close(fd1);
818 if (fd2 >= 0)
819 close(fd2);
820 return rc;
821}
822
Tom Cherryb7349902015-08-26 11:43:36 -0700823static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700824 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700825 if (args.size() == 3) {
826 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700828 } else if (args.size() == 4) {
829 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
830 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 return -errno;
832 } else {
833 return -1;
834 }
835 return 0;
836}
837
838static mode_t get_mode(const char *s) {
839 mode_t mode = 0;
840 while (*s) {
841 if (*s >= '0' && *s <= '7') {
842 mode = (mode<<3) | (*s-'0');
843 } else {
844 return -1;
845 }
846 s++;
847 }
848 return mode;
849}
850
Tom Cherryb7349902015-08-26 11:43:36 -0700851static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700852 mode_t mode = get_mode(args[1].c_str());
853 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700854 return -errno;
855 }
856 return 0;
857}
858
Tom Cherryb7349902015-08-26 11:43:36 -0700859static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400860 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500861
Tom Cherry96f67312015-07-30 13:52:55 -0700862 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
863 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400864 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500865 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400866 return ret;
867}
868
Tom Cherryb7349902015-08-26 11:43:36 -0700869static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400870 int ret = 0;
871
Tom Cherry96f67312015-07-30 13:52:55 -0700872 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
873 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400874 ret = -errno;
875 }
876 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500877}
878
Tom Cherryb7349902015-08-26 11:43:36 -0700879static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700880 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700881 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
882 ERROR("loglevel: invalid log level'%d'\n", log_level);
883 return -EINVAL;
884 }
885 klog_set_level(log_level);
886 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700887}
888
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700889static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700890 load_persist_props();
891 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800892}
893
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700894static int do_load_system_props(const std::vector<std::string>& args) {
895 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700896 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700897}
898
Tom Cherryb7349902015-08-26 11:43:36 -0700899static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700900 if (args.size() == 2) {
901 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
902 } else if (args.size() == 3) {
903 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800904 } else
905 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700906}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000907
Paul Lawrence806d10b2015-04-28 22:07:10 +0000908/*
909 * Callback to make a directory from the ext4 code
910 */
Tom Cherryb7349902015-08-26 11:43:36 -0700911static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000912 if (make_dir(dir, 0700) && errno != EEXIST) {
913 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000914 }
915
Paul Lawrence806d10b2015-04-28 22:07:10 +0000916 return 0;
917}
918
Paul Crowley749af8c2015-05-28 17:35:06 +0100919static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700920 std::string value = property_get("ro.crypto.type");
921 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100922}
923
Tom Cherryb7349902015-08-26 11:43:36 -0700924static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100925 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000926 return 0;
927 }
Tom Cherry96f67312015-07-30 13:52:55 -0700928 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000929 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000930}
Paul Crowley749af8c2015-05-28 17:35:06 +0100931
Paul Crowley59497452016-02-01 16:37:13 +0000932static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000933 return e4crypt_do_init_user0();
934}
935
Tom Cherryb7349902015-08-26 11:43:36 -0700936BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
937 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
938 static const Map builtin_functions = {
939 {"bootchart_init", {0, 0, do_bootchart_init}},
940 {"chmod", {2, 2, do_chmod}},
941 {"chown", {2, 3, do_chown}},
942 {"class_reset", {1, 1, do_class_reset}},
943 {"class_start", {1, 1, do_class_start}},
944 {"class_stop", {1, 1, do_class_stop}},
945 {"copy", {2, 2, do_copy}},
946 {"domainname", {1, 1, do_domainname}},
947 {"enable", {1, 1, do_enable}},
948 {"exec", {1, kMax, do_exec}},
949 {"export", {2, 2, do_export}},
950 {"hostname", {1, 1, do_hostname}},
951 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000952 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700953 {"insmod", {1, kMax, do_insmod}},
954 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700955 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700956 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700957 {"loglevel", {1, 1, do_loglevel}},
958 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800959 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700960 {"mount", {3, kMax, do_mount}},
961 {"powerctl", {1, 1, do_powerctl}},
962 {"restart", {1, 1, do_restart}},
963 {"restorecon", {1, kMax, do_restorecon}},
964 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
965 {"rm", {1, 1, do_rm}},
966 {"rmdir", {1, 1, do_rmdir}},
967 {"setprop", {2, 2, do_setprop}},
968 {"setrlimit", {3, 3, do_setrlimit}},
969 {"start", {1, 1, do_start}},
970 {"stop", {1, 1, do_stop}},
971 {"swapon_all", {1, 1, do_swapon_all}},
972 {"symlink", {2, 2, do_symlink}},
973 {"sysclktz", {1, 1, do_sysclktz}},
974 {"trigger", {1, 1, do_trigger}},
975 {"verity_load_state", {0, 0, do_verity_load_state}},
976 {"verity_update_state", {0, 0, do_verity_update_state}},
977 {"wait", {1, 2, do_wait}},
978 {"write", {2, 2, do_write}},
979 };
980 return builtin_functions;
981}