blob: 63cc3bc1f05b618dcac2a2089f3d1c7b41084f6c [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 Salyzyna98cc9c2016-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 Salyzynad575e02016-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 Kralevich124a9c92016-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 Salyzyna98cc9c2016-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
Tom Cherryfa0c21c2015-07-23 17:53:11 -070054#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070055#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070057#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070058#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070059#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070060#include "property_service.h"
61#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080062#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070063#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064
Nick Kralevichbc609542015-01-31 21:39:46 -080065#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070066#define UNMOUNT_CHECK_MS 5000
67#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080068
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080069static const int kTerminateServiceDelayMicroSeconds = 50000;
70
Tom Cherryb7349902015-08-26 11:43:36 -070071static int insmod(const char *filename, const char *options) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070072 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
73 if (fd == -1) {
74 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080076 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070077 int rc = syscall(__NR_finit_module, fd, options, 0);
78 if (rc == -1) {
79 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
80 }
81 close(fd);
82 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083}
84
Tom Cherryb7349902015-08-26 11:43:36 -070085static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086 struct ifreq ifr;
87 int s, ret;
88
89 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
90
91 s = socket(AF_INET, SOCK_DGRAM, 0);
92 if (s < 0)
93 return -1;
94
95 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
96 if (ret < 0) {
97 goto done;
98 }
99
100 if (up)
101 ifr.ifr_flags |= IFF_UP;
102 else
103 ifr.ifr_flags &= ~IFF_UP;
104
105 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107done:
108 close(s);
109 return ret;
110}
111
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700112// Turn off backlight while we are performing power down cleanup activities.
113static void turnOffBacklight() {
114 static const char off[] = "0";
115
116 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
117
118 static const char backlightDir[] = "/sys/class/backlight";
119 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
120 if (!dir) {
121 return;
122 }
123
124 struct dirent *dp;
125 while ((dp = readdir(dir.get())) != NULL) {
126 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
127 (dp->d_name[0] == '.')) {
128 continue;
129 }
130
131 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
132 backlightDir,
133 dp->d_name);
134 android::base::WriteStringToFile(off, fileName);
135 }
136}
137
Paul Crowleyaf8be582016-05-10 08:52:06 -0700138static int wipe_data_via_recovery(const std::string& reason) {
139 const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
140 std::string err;
141 if (!write_bootloader_message(options, &err)) {
142 ERROR("failed to set bootloader message: %s", err.c_str());
143 return -1;
144 }
145 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
146 while (1) { pause(); } // never reached
147}
148
Tom Cherryb7349902015-08-26 11:43:36 -0700149static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700150 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
151 return;
152
153 /* First, lazily unmount the directory. This unmount request finishes when
154 * all processes that open a file or directory in |entry->mnt_dir| exit.
155 */
156 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
157
158 /* Next, kill all processes except init, kthreadd, and kthreadd's
159 * children to finish the lazy unmount. Killing all processes here is okay
160 * because this callback function is only called right before reboot().
161 * It might be cleaner to selectively kill processes that actually use
162 * |entry->mnt_dir| rather than killing all, probably by reusing a function
163 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
164 * not allow init to scan /proc/<pid> files which the utility function
165 * heavily relies on. The policy does not allow the process to execute
166 * killall/pkill binaries either. Note that some processes might
167 * automatically restart after kill(), but that is not really a problem
168 * because |entry->mnt_dir| is no longer visible to such new processes.
169 */
Tom Cherrybac32992015-07-31 12:45:25 -0700170 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700171 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
172
Mark Salyzynad575e02016-04-05 08:10:25 -0700173 // Restart Watchdogd to allow us to complete umounting and fsck
174 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
175 if (svc) {
176 do {
177 sched_yield(); // do not be so eager, let cleanup have priority
178 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
179 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
180 svc->Start();
181 }
182
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700183 turnOffBacklight();
184
Yusuke Sato0df08272015-07-08 14:57:07 -0700185 int count = 0;
186 while (count++ < UNMOUNT_CHECK_TIMES) {
187 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
188 if (fd >= 0) {
189 /* |entry->mnt_dir| has sucessfully been unmounted. */
190 close(fd);
191 break;
192 } else if (errno == EBUSY) {
193 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
194 * while then retry.
195 */
196 TEMP_FAILURE_RETRY(
197 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
198 continue;
199 } else {
200 /* Cannot open the device. Give up. */
201 return;
202 }
203 }
204
Mark Salyzynad575e02016-04-05 08:10:25 -0700205 // NB: With watchdog still running, there is no cap on the time it takes
206 // to complete the fsck, from the users perspective the device graphics
207 // and responses are locked-up and they may choose to hold the power
208 // button in frustration if it drags out.
209
Yusuke Sato0df08272015-07-08 14:57:07 -0700210 int st;
211 if (!strcmp(entry->mnt_type, "f2fs")) {
212 const char *f2fs_argv[] = {
213 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
214 };
215 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700216 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700217 } else if (!strcmp(entry->mnt_type, "ext4")) {
218 const char *ext4_argv[] = {
219 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
220 };
221 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700222 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700223 }
224}
225
Tom Cherryb7349902015-08-26 11:43:36 -0700226static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227 /* Starting a class does not start services
228 * which are explicitly disabled. They must
229 * be started individually.
230 */
Tom Cherrybac32992015-07-31 12:45:25 -0700231 ServiceManager::GetInstance().
232 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233 return 0;
234}
235
Tom Cherryb7349902015-08-26 11:43:36 -0700236static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700237 ServiceManager::GetInstance().
238 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239 return 0;
240}
241
Tom Cherryb7349902015-08-26 11:43:36 -0700242static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700243 ServiceManager::GetInstance().
244 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800245 return 0;
246}
247
Tom Cherryb7349902015-08-26 11:43:36 -0700248static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700249 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700250}
251
Tom Cherryb7349902015-08-26 11:43:36 -0700252static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700253 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
254 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700255 return -1;
256 }
Tom Cherrybac32992015-07-31 12:45:25 -0700257 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700258}
259
Tom Cherryb7349902015-08-26 11:43:36 -0700260static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700261 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
262 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800263 return -1;
264 }
Tom Cherrybac32992015-07-31 12:45:25 -0700265 if (!svc->Start()) {
266 return -1;
267 }
268 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800269 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270}
271
Tom Cherryb7349902015-08-26 11:43:36 -0700272static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700273 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700274}
275
Tom Cherryb7349902015-08-26 11:43:36 -0700276static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700277 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700278}
279
Tom Cherryb7349902015-08-26 11:43:36 -0700280static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700281 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282}
283
Tom Cherryb7349902015-08-26 11:43:36 -0700284static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700285 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800286
Tom Cherry96f67312015-07-30 13:52:55 -0700287 if (args.size() > 2) {
288 options += args[2];
289 for (std::size_t i = 3; i < args.size(); ++i) {
290 options += ' ';
291 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800292 }
293 }
294
Tom Cherry96f67312015-07-30 13:52:55 -0700295 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800296}
297
Tom Cherryb7349902015-08-26 11:43:36 -0700298static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700300 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301
302 /* mkdir <path> [mode] [owner] [group] */
303
Tom Cherry96f67312015-07-30 13:52:55 -0700304 if (args.size() >= 3) {
305 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 }
307
Tom Cherry96f67312015-07-30 13:52:55 -0700308 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700309 /* chmod in case the directory already exists */
310 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700311 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700312 }
313 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 return -errno;
315 }
316
Tom Cherry96f67312015-07-30 13:52:55 -0700317 if (args.size() >= 4) {
318 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319 gid_t gid = -1;
320
Tom Cherry96f67312015-07-30 13:52:55 -0700321 if (args.size() == 5) {
322 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 }
324
Tom Cherry96f67312015-07-30 13:52:55 -0700325 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 return -errno;
327 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700328
329 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
330 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700331 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700332 if (ret == -1) {
333 return -errno;
334 }
335 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 }
337
Paul Crowleyaf8be582016-05-10 08:52:06 -0700338 if (e4crypt_is_native()) {
339 if (e4crypt_set_directory_policy(args[1].c_str())) {
340 wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
341 return -1;
342 }
343 }
344 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700345}
346
347static struct {
348 const char *name;
349 unsigned flag;
350} mount_flags[] = {
351 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200352 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353 { "nosuid", MS_NOSUID },
354 { "nodev", MS_NODEV },
355 { "nodiratime", MS_NODIRATIME },
356 { "ro", MS_RDONLY },
357 { "rw", 0 },
358 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700359 { "bind", MS_BIND },
360 { "rec", MS_REC },
361 { "unbindable", MS_UNBINDABLE },
362 { "private", MS_PRIVATE },
363 { "slave", MS_SLAVE },
364 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 { "defaults", 0 },
366 { 0, 0 },
367};
368
Ken Sumrall752923c2010-12-03 16:33:31 -0800369#define DATA_MNT_POINT "/data"
370
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700372static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700373 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700374 const char *source, *target, *system;
375 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700377 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700379 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380
Tom Cherry96f67312015-07-30 13:52:55 -0700381 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700383 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 flags |= mount_flags[i].flag;
385 break;
386 }
387 }
388
Colin Crosscd0f1732010-04-19 17:10:24 -0700389 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700390 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700391 wait = 1;
392 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700393 else if (na + 1 == args.size())
394 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700395 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700396 }
397
Tom Cherry96f67312015-07-30 13:52:55 -0700398 system = args[1].c_str();
399 source = args[2].c_str();
400 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000401
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 if (!strncmp(source, "mtd@", 4)) {
403 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000404 if (n < 0) {
405 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700406 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000407
Yabin Cuie2d63af2015-02-17 19:27:51 -0800408 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000409
Colin Crosscd0f1732010-04-19 17:10:24 -0700410 if (wait)
411 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000412 if (mount(tmp, target, system, flags, options) < 0) {
413 return -1;
414 }
415
Ken Sumralldd4d7862011-02-17 18:09:47 -0800416 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000417 } else if (!strncmp(source, "loop@", 5)) {
418 int mode, loop, fd;
419 struct loop_info info;
420
421 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800422 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000423 if (fd < 0) {
424 return -1;
425 }
426
427 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800428 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800429 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000430 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100431 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000432 return -1;
433 }
434
435 /* if it is a blank loop device */
436 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
437 /* if it becomes our loop device */
438 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
439 close(fd);
440
441 if (mount(tmp, target, system, flags, options) < 0) {
442 ioctl(loop, LOOP_CLR_FD, 0);
443 close(loop);
444 return -1;
445 }
446
447 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800448 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000449 }
450 }
451
452 close(loop);
453 }
454
455 close(fd);
456 ERROR("out of loopback devices");
457 return -1;
458 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700459 if (wait)
460 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000461 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700462 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000463 }
464
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700465 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800466
467exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800468 return 0;
469
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700470}
471
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800472/* Imports .rc files from the specified paths. Default ones are applied if none is given.
473 *
474 * start_index: index of the first path in the args list
475 */
476static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700477 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800478 if (args.size() <= start_index) {
479 // Use the default set if no path is given
480 static const std::vector<std::string> init_directories = {
481 "/system/etc/init",
482 "/vendor/etc/init",
483 "/odm/etc/init"
484 };
485
486 for (const auto& dir : init_directories) {
487 parser.ParseConfig(dir);
488 }
489 } else {
490 for (size_t i = start_index; i < args.size(); ++i) {
491 parser.ParseConfig(args[i]);
492 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700493 }
494}
495
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800496/* mount_all <fstab> [ <path> ]*
497 *
JP Abgrallcee20682014-07-02 14:26:54 -0700498 * This function might request a reboot, in which case it will
499 * not return.
500 */
Tom Cherryb7349902015-08-26 11:43:36 -0700501static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700502 pid_t pid;
503 int ret = -1;
504 int child_ret = -1;
505 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800506 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700507
Tom Cherry96f67312015-07-30 13:52:55 -0700508 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700509 /*
510 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
511 * do the call in the child to provide protection to the main init
512 * process if anything goes wrong (crash or memory leak), and wait for
513 * the child to finish in the parent.
514 */
515 pid = fork();
516 if (pid > 0) {
517 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700518 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
519 if (wp_ret < 0) {
520 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700521 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700522 }
523
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700524 if (WIFEXITED(status)) {
525 ret = WEXITSTATUS(status);
526 } else {
527 ret = -1;
528 }
529 } else if (pid == 0) {
530 /* child, call fs_mgr_mount_all() */
531 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800532 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800533 child_ret = fs_mgr_mount_all(fstab);
534 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700535 if (child_ret == -1) {
536 ERROR("fs_mgr_mount_all returned an error\n");
537 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700538 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700539 } else {
540 /* fork failed, return an error */
541 return -1;
542 }
543
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800544 /* Paths of .rc files are specified at the 2nd argument and beyond */
545 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700546
JP Abgrallf22b7452014-07-02 13:16:04 -0700547 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700548 ActionManager::GetInstance().QueueEventTrigger("encrypt");
JP Abgrallf22b7452014-07-02 13:16:04 -0700549 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700550 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000551 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700552 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
JP Abgrallf22b7452014-07-02 13:16:04 -0700553 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700554 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800555 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
556 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
557 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700558 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700559 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
560 /* Setup a wipe via recovery, and reboot into recovery */
561 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
Paul Crowleyaf8be582016-05-10 08:52:06 -0700562 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700563 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800564 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000565 if (e4crypt_install_keyring()) {
566 return -1;
567 }
568 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000569 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000570
571 // Although encrypted, we have device key, so we do not need to
572 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700573 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700574 } else if (ret > 0) {
575 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700576 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700577 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700578
579 return ret;
580}
581
Tom Cherryb7349902015-08-26 11:43:36 -0700582static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700583 struct fstab *fstab;
584 int ret;
585
Tom Cherry96f67312015-07-30 13:52:55 -0700586 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700587 ret = fs_mgr_swapon_all(fstab);
588 fs_mgr_free_fstab(fstab);
589
590 return ret;
591}
592
Tom Cherryb7349902015-08-26 11:43:36 -0700593static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700594 const char* name = args[1].c_str();
595 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700596 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 return 0;
598}
599
Tom Cherryb7349902015-08-26 11:43:36 -0700600static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700601 struct rlimit limit;
602 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700603 resource = std::stoi(args[1]);
604 limit.rlim_cur = std::stoi(args[2]);
605 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 return setrlimit(resource, &limit);
607}
608
Tom Cherryb7349902015-08-26 11:43:36 -0700609static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700610 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
611 if (!svc) {
612 ERROR("do_start: Service %s not found\n", args[1].c_str());
613 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700614 }
Tom Cherrybac32992015-07-31 12:45:25 -0700615 if (!svc->Start())
616 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 return 0;
618}
619
Tom Cherryb7349902015-08-26 11:43:36 -0700620static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700621 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
622 if (!svc) {
623 ERROR("do_stop: Service %s not found\n", args[1].c_str());
624 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700625 }
Tom Cherrybac32992015-07-31 12:45:25 -0700626 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 return 0;
628}
629
Tom Cherryb7349902015-08-26 11:43:36 -0700630static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700631 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
632 if (!svc) {
633 ERROR("do_restart: Service %s not found\n", args[1].c_str());
634 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700635 }
Tom Cherrybac32992015-07-31 12:45:25 -0700636 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700637 return 0;
638}
639
Tom Cherryb7349902015-08-26 11:43:36 -0700640static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700641 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700642 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700643 unsigned int cmd = 0;
644 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700645 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700647 if (strncmp(command, "shutdown", 8) == 0) {
648 cmd = ANDROID_RB_POWEROFF;
649 len = 8;
650 } else if (strncmp(command, "reboot", 6) == 0) {
651 cmd = ANDROID_RB_RESTART2;
652 len = 6;
653 } else {
654 ERROR("powerctl: unrecognized command '%s'\n", command);
655 return -EINVAL;
656 }
657
658 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700659 if (cmd == ANDROID_RB_POWEROFF &&
660 !strcmp(&command[len + 1], "userrequested")) {
661 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
662 // Run fsck once the file system is remounted in read-only mode.
663 callback_on_ro_remount = unmount_and_fsck;
664 } else if (cmd == ANDROID_RB_RESTART2) {
665 reboot_target = &command[len + 1];
666 }
667 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700668 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
669 return -EINVAL;
670 }
671
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800672 std::string timeout = property_get("ro.build.shutdown_timeout");
673 unsigned int delay = 0;
674
675 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
676 Timer t;
677 // Ask all services to terminate.
678 ServiceManager::GetInstance().ForEachService(
679 [] (Service* s) { s->Terminate(); });
680
681 while (t.duration() < delay) {
682 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
683
684 int service_count = 0;
685 ServiceManager::GetInstance().ForEachService(
686 [&service_count] (Service* s) {
687 // Count the number of services running.
688 // Exclude the console as it will ignore the SIGTERM signal
689 // and not exit.
690 // Note: SVC_CONSOLE actually means "requires console" but
691 // it is only used by the shell.
692 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
693 service_count++;
694 }
695 });
696
697 if (service_count == 0) {
698 // All terminable services terminated. We can exit early.
699 break;
700 }
701
702 // Wait a bit before recounting the number or running services.
703 usleep(kTerminateServiceDelayMicroSeconds);
704 }
705 NOTICE("Terminating running services took %.02f seconds", t.duration());
706 }
707
Yusuke Sato0df08272015-07-08 14:57:07 -0700708 return android_reboot_with_callback(cmd, 0, reboot_target,
709 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700710}
711
Tom Cherryb7349902015-08-26 11:43:36 -0700712static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700713 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700714 return 0;
715}
716
Tom Cherryb7349902015-08-26 11:43:36 -0700717static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700718 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719}
720
Tom Cherryb7349902015-08-26 11:43:36 -0700721static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700722 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800723}
724
Tom Cherryb7349902015-08-26 11:43:36 -0700725static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700726 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800727}
728
Tom Cherryb7349902015-08-26 11:43:36 -0700729static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800730 struct timezone tz;
731
The Android Open Source Project35237d12008-12-17 18:08:08 -0800732 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700733 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800734 if (settimeofday(NULL, &tz))
735 return -1;
736 return 0;
737}
738
Tom Cherryb7349902015-08-26 11:43:36 -0700739static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700740 int mode = -1;
741 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000742 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700743 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000744 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700745 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000746}
747
Tom Cherryb7349902015-08-26 11:43:36 -0700748static void verity_update_property(fstab_rec *fstab, const char *mount_point,
749 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100750 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
751 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000752}
753
Tom Cherryb7349902015-08-26 11:43:36 -0700754static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700755 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000756}
757
Tom Cherryb7349902015-08-26 11:43:36 -0700758static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700759 const char* path = args[1].c_str();
760 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700761 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700762}
763
Tom Cherryb7349902015-08-26 11:43:36 -0700764static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700765 char *buffer = NULL;
766 int rc = 0;
767 int fd1 = -1, fd2 = -1;
768 struct stat info;
769 int brtw, brtr;
770 char *p;
771
Tom Cherry96f67312015-07-30 13:52:55 -0700772 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700773 return -1;
774
Tom Cherry96f67312015-07-30 13:52:55 -0700775 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700776 goto out_err;
777
Tom Cherry96f67312015-07-30 13:52:55 -0700778 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700779 goto out_err;
780
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800781 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700782 goto out_err;
783
784 p = buffer;
785 brtr = info.st_size;
786 while(brtr) {
787 rc = read(fd1, p, brtr);
788 if (rc < 0)
789 goto out_err;
790 if (rc == 0)
791 break;
792 p += rc;
793 brtr -= rc;
794 }
795
796 p = buffer;
797 brtw = info.st_size;
798 while(brtw) {
799 rc = write(fd2, p, brtw);
800 if (rc < 0)
801 goto out_err;
802 if (rc == 0)
803 break;
804 p += rc;
805 brtw -= rc;
806 }
807
808 rc = 0;
809 goto out;
810out_err:
811 rc = -1;
812out:
813 if (buffer)
814 free(buffer);
815 if (fd1 >= 0)
816 close(fd1);
817 if (fd2 >= 0)
818 close(fd2);
819 return rc;
820}
821
Tom Cherryb7349902015-08-26 11:43:36 -0700822static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700823 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700824 if (args.size() == 3) {
825 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700826 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700827 } else if (args.size() == 4) {
828 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
829 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 return -errno;
831 } else {
832 return -1;
833 }
834 return 0;
835}
836
837static mode_t get_mode(const char *s) {
838 mode_t mode = 0;
839 while (*s) {
840 if (*s >= '0' && *s <= '7') {
841 mode = (mode<<3) | (*s-'0');
842 } else {
843 return -1;
844 }
845 s++;
846 }
847 return mode;
848}
849
Tom Cherryb7349902015-08-26 11:43:36 -0700850static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700851 mode_t mode = get_mode(args[1].c_str());
852 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700853 return -errno;
854 }
855 return 0;
856}
857
Tom Cherryb7349902015-08-26 11:43:36 -0700858static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400859 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500860
Tom Cherry96f67312015-07-30 13:52:55 -0700861 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
862 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400863 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500864 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400865 return ret;
866}
867
Tom Cherryb7349902015-08-26 11:43:36 -0700868static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400869 int ret = 0;
870
Tom Cherry96f67312015-07-30 13:52:55 -0700871 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
872 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400873 ret = -errno;
874 }
875 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500876}
877
Tom Cherryb7349902015-08-26 11:43:36 -0700878static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700879 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700880 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
881 ERROR("loglevel: invalid log level'%d'\n", log_level);
882 return -EINVAL;
883 }
884 klog_set_level(log_level);
885 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700886}
887
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700888static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700889 load_persist_props();
890 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800891}
892
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700893static int do_load_system_props(const std::vector<std::string>& args) {
894 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700895 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700896}
897
Tom Cherryb7349902015-08-26 11:43:36 -0700898static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700899 if (args.size() == 2) {
900 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
901 } else if (args.size() == 3) {
902 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800903 } else
904 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700905}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000906
Paul Lawrence806d10b2015-04-28 22:07:10 +0000907/*
908 * Callback to make a directory from the ext4 code
909 */
Tom Cherryb7349902015-08-26 11:43:36 -0700910static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000911 if (make_dir(dir, 0700) && errno != EEXIST) {
912 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000913 }
914
Paul Lawrence806d10b2015-04-28 22:07:10 +0000915 return 0;
916}
917
Paul Crowley749af8c2015-05-28 17:35:06 +0100918static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700919 std::string value = property_get("ro.crypto.type");
920 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100921}
922
Tom Cherryb7349902015-08-26 11:43:36 -0700923static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100924 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000925 return 0;
926 }
Tom Cherry96f67312015-07-30 13:52:55 -0700927 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000928 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000929}
Paul Crowley749af8c2015-05-28 17:35:06 +0100930
Paul Crowley59497452016-02-01 16:37:13 +0000931static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000932 return e4crypt_do_init_user0();
933}
934
Tom Cherryb7349902015-08-26 11:43:36 -0700935BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
936 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
937 static const Map builtin_functions = {
938 {"bootchart_init", {0, 0, do_bootchart_init}},
939 {"chmod", {2, 2, do_chmod}},
940 {"chown", {2, 3, do_chown}},
941 {"class_reset", {1, 1, do_class_reset}},
942 {"class_start", {1, 1, do_class_start}},
943 {"class_stop", {1, 1, do_class_stop}},
944 {"copy", {2, 2, do_copy}},
945 {"domainname", {1, 1, do_domainname}},
946 {"enable", {1, 1, do_enable}},
947 {"exec", {1, kMax, do_exec}},
948 {"export", {2, 2, do_export}},
949 {"hostname", {1, 1, do_hostname}},
950 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000951 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700952 {"insmod", {1, kMax, do_insmod}},
953 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700954 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700955 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700956 {"loglevel", {1, 1, do_loglevel}},
957 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800958 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700959 {"mount", {3, kMax, do_mount}},
960 {"powerctl", {1, 1, do_powerctl}},
961 {"restart", {1, 1, do_restart}},
962 {"restorecon", {1, kMax, do_restorecon}},
963 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
964 {"rm", {1, 1, do_rm}},
965 {"rmdir", {1, 1, do_rmdir}},
966 {"setprop", {2, 2, do_setprop}},
967 {"setrlimit", {3, 3, do_setrlimit}},
968 {"start", {1, 1, do_start}},
969 {"stop", {1, 1, do_stop}},
970 {"swapon_all", {1, 1, do_swapon_all}},
971 {"symlink", {2, 2, do_symlink}},
972 {"sysclktz", {1, 1, do_sysclktz}},
973 {"trigger", {1, 1, do_trigger}},
974 {"verity_load_state", {0, 0, do_verity_load_state}},
975 {"verity_update_state", {0, 0, do_verity_update_state}},
976 {"wait", {1, 2, do_wait}},
977 {"write", {2, 2, do_write}},
978 };
979 return builtin_functions;
980}