blob: 44217f0f0fb59c0a9e6c09efbf1bbb532fcc51f7 [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 Cui0b1252c2016-06-24 18:28:03 -070049#include <bootloader_message/bootloader_message.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
Alex Light68ab20f2016-06-23 11:11:39 -0700348/* umount <path> */
349static int do_umount(const std::vector<std::string>& args) {
350 return umount(args[1].c_str());
351}
352
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353static struct {
354 const char *name;
355 unsigned flag;
356} mount_flags[] = {
357 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200358 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 { "nosuid", MS_NOSUID },
360 { "nodev", MS_NODEV },
361 { "nodiratime", MS_NODIRATIME },
362 { "ro", MS_RDONLY },
363 { "rw", 0 },
364 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700365 { "bind", MS_BIND },
366 { "rec", MS_REC },
367 { "unbindable", MS_UNBINDABLE },
368 { "private", MS_PRIVATE },
369 { "slave", MS_SLAVE },
370 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 { "defaults", 0 },
372 { 0, 0 },
373};
374
Ken Sumrall752923c2010-12-03 16:33:31 -0800375#define DATA_MNT_POINT "/data"
376
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700378static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700380 const char *source, *target, *system;
381 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700383 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700385 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386
Tom Cherry96f67312015-07-30 13:52:55 -0700387 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700389 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390 flags |= mount_flags[i].flag;
391 break;
392 }
393 }
394
Colin Crosscd0f1732010-04-19 17:10:24 -0700395 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700396 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700397 wait = 1;
398 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700399 else if (na + 1 == args.size())
400 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700401 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 }
403
Tom Cherry96f67312015-07-30 13:52:55 -0700404 system = args[1].c_str();
405 source = args[2].c_str();
406 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000407
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408 if (!strncmp(source, "mtd@", 4)) {
409 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410 if (n < 0) {
411 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413
Yabin Cuie2d63af2015-02-17 19:27:51 -0800414 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415
Colin Crosscd0f1732010-04-19 17:10:24 -0700416 if (wait)
417 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 if (mount(tmp, target, system, flags, options) < 0) {
419 return -1;
420 }
421
Ken Sumralldd4d7862011-02-17 18:09:47 -0800422 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000423 } else if (!strncmp(source, "loop@", 5)) {
424 int mode, loop, fd;
425 struct loop_info info;
426
427 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800428 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000429 if (fd < 0) {
430 return -1;
431 }
432
433 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800434 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800435 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000436 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100437 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000438 return -1;
439 }
440
441 /* if it is a blank loop device */
442 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
443 /* if it becomes our loop device */
444 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
445 close(fd);
446
447 if (mount(tmp, target, system, flags, options) < 0) {
448 ioctl(loop, LOOP_CLR_FD, 0);
449 close(loop);
450 return -1;
451 }
452
453 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800454 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000455 }
456 }
457
458 close(loop);
459 }
460
461 close(fd);
462 ERROR("out of loopback devices");
463 return -1;
464 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700465 if (wait)
466 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000467 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700468 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000469 }
470
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800472
473exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800474 return 0;
475
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476}
477
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800478/* Imports .rc files from the specified paths. Default ones are applied if none is given.
479 *
480 * start_index: index of the first path in the args list
481 */
Wei Wang8e5c9b82016-08-23 11:58:09 -0700482static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700483 Parser& parser = Parser::GetInstance();
Wei Wang8e5c9b82016-08-23 11:58:09 -0700484 if (end_index <= start_index) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800485 // Use the default set if no path is given
486 static const std::vector<std::string> init_directories = {
487 "/system/etc/init",
488 "/vendor/etc/init",
489 "/odm/etc/init"
490 };
491
492 for (const auto& dir : init_directories) {
493 parser.ParseConfig(dir);
494 }
495 } else {
Wei Wang8e5c9b82016-08-23 11:58:09 -0700496 for (size_t i = start_index; i < end_index; ++i) {
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800497 parser.ParseConfig(args[i]);
498 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700499 }
500}
501
Wei Wang8e5c9b82016-08-23 11:58:09 -0700502/* mount_fstab
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800503 *
Wei Wang8e5c9b82016-08-23 11:58:09 -0700504 * Call fs_mgr_mount_all() to mount the given fstab
JP Abgrallcee20682014-07-02 14:26:54 -0700505 */
Wei Wang8e5c9b82016-08-23 11:58:09 -0700506static int mount_fstab(const char* fstabfile, int mount_mode) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700507 pid_t pid;
508 int ret = -1;
509 int child_ret = -1;
510 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800511 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700512
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700513 /*
514 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
515 * do the call in the child to provide protection to the main init
516 * process if anything goes wrong (crash or memory leak), and wait for
517 * the child to finish in the parent.
518 */
519 pid = fork();
520 if (pid > 0) {
521 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700522 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
523 if (wp_ret < 0) {
524 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700525 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700526 }
527
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700528 if (WIFEXITED(status)) {
529 ret = WEXITSTATUS(status);
530 } else {
531 ret = -1;
532 }
533 } else if (pid == 0) {
534 /* child, call fs_mgr_mount_all() */
535 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800536 fstab = fs_mgr_read_fstab(fstabfile);
Wei Wang8e5c9b82016-08-23 11:58:09 -0700537 child_ret = fs_mgr_mount_all(fstab, mount_mode);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800538 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700539 if (child_ret == -1) {
540 ERROR("fs_mgr_mount_all returned an error\n");
541 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700542 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700543 } else {
544 /* fork failed, return an error */
545 return -1;
546 }
Wei Wang8e5c9b82016-08-23 11:58:09 -0700547 return ret;
548}
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700549
Wei Wang8e5c9b82016-08-23 11:58:09 -0700550/* Queue event based on fs_mgr return code.
551 *
552 * code: return code of fs_mgr_mount_all
553 *
554 * This function might request a reboot, in which case it will
555 * not return.
556 *
557 * return code is processed based on input code
558 */
559static int queue_fs_event(int code) {
560 int ret = code;
561 if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700562 ActionManager::GetInstance().QueueEventTrigger("encrypt");
Wei Wang8e5c9b82016-08-23 11:58:09 -0700563 } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700564 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000565 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700566 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
Wei Wang8e5c9b82016-08-23 11:58:09 -0700567 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700568 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800569 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang8e5c9b82016-08-23 11:58:09 -0700570 } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
Paul Lawrence1098aac2016-03-04 15:52:33 -0800571 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700572 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang8e5c9b82016-08-23 11:58:09 -0700573 } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
JP Abgrallcee20682014-07-02 14:26:54 -0700574 /* Setup a wipe via recovery, and reboot into recovery */
575 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
Paul Crowleyaf8be582016-05-10 08:52:06 -0700576 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700577 /* If reboot worked, there is no return. */
Wei Wang8e5c9b82016-08-23 11:58:09 -0700578 } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000579 if (e4crypt_install_keyring()) {
580 return -1;
581 }
582 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000583 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000584
585 // Although encrypted, we have device key, so we do not need to
586 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700587 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Wei Wang8e5c9b82016-08-23 11:58:09 -0700588 } else if (code > 0) {
589 ERROR("fs_mgr_mount_all returned unexpected error %d\n", code);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700590 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700591 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700592
593 return ret;
594}
595
Wei Wang8e5c9b82016-08-23 11:58:09 -0700596/* mount_all <fstab> [ <path> ]* [--<options>]*
597 *
598 * This function might request a reboot, in which case it will
599 * not return.
600 */
601static int do_mount_all(const std::vector<std::string>& args) {
602 std::size_t na = 0;
603 bool import_rc = true;
604 bool queue_event = true;
605 int mount_mode = MOUNT_MODE_DEFAULT;
606 const char* fstabfile = args[1].c_str();
607 std::size_t path_arg_end = args.size();
608
609 for (na = args.size() - 1; na > 1; --na) {
610 if (args[na] == "--early") {
611 path_arg_end = na;
612 queue_event = false;
613 mount_mode = MOUNT_MODE_EARLY;
614 } else if (args[na] == "--late") {
615 path_arg_end = na;
616 import_rc = false;
617 mount_mode = MOUNT_MODE_LATE;
618 }
619 }
620
621 int ret = mount_fstab(fstabfile, mount_mode);
622
623 if (import_rc) {
624 /* Paths of .rc files are specified at the 2nd argument and beyond */
625 import_late(args, 2, path_arg_end);
626 }
627
628 if (queue_event) {
629 /* queue_fs_event will queue event based on mount_fstab return code
630 * and return processed return code*/
631 ret = queue_fs_event(ret);
632 }
633
634 return ret;
635}
636
Tom Cherryb7349902015-08-26 11:43:36 -0700637static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700638 struct fstab *fstab;
639 int ret;
640
Tom Cherry96f67312015-07-30 13:52:55 -0700641 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700642 ret = fs_mgr_swapon_all(fstab);
643 fs_mgr_free_fstab(fstab);
644
645 return ret;
646}
647
Tom Cherryb7349902015-08-26 11:43:36 -0700648static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700649 const char* name = args[1].c_str();
650 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700651 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700652 return 0;
653}
654
Tom Cherryb7349902015-08-26 11:43:36 -0700655static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700656 struct rlimit limit;
657 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700658 resource = std::stoi(args[1]);
659 limit.rlim_cur = std::stoi(args[2]);
660 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700661 return setrlimit(resource, &limit);
662}
663
Tom Cherryb7349902015-08-26 11:43:36 -0700664static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700665 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
666 if (!svc) {
667 ERROR("do_start: Service %s not found\n", args[1].c_str());
668 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700669 }
Tom Cherrybac32992015-07-31 12:45:25 -0700670 if (!svc->Start())
671 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700672 return 0;
673}
674
Tom Cherryb7349902015-08-26 11:43:36 -0700675static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700676 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
677 if (!svc) {
678 ERROR("do_stop: Service %s not found\n", args[1].c_str());
679 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700680 }
Tom Cherrybac32992015-07-31 12:45:25 -0700681 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700682 return 0;
683}
684
Tom Cherryb7349902015-08-26 11:43:36 -0700685static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700686 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
687 if (!svc) {
688 ERROR("do_restart: Service %s not found\n", args[1].c_str());
689 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700690 }
Tom Cherrybac32992015-07-31 12:45:25 -0700691 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700692 return 0;
693}
694
Tom Cherryb7349902015-08-26 11:43:36 -0700695static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700696 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700697 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700698 unsigned int cmd = 0;
699 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700700 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700701
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700702 if (strncmp(command, "shutdown", 8) == 0) {
703 cmd = ANDROID_RB_POWEROFF;
704 len = 8;
705 } else if (strncmp(command, "reboot", 6) == 0) {
706 cmd = ANDROID_RB_RESTART2;
707 len = 6;
708 } else {
709 ERROR("powerctl: unrecognized command '%s'\n", command);
710 return -EINVAL;
711 }
712
713 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700714 if (cmd == ANDROID_RB_POWEROFF &&
715 !strcmp(&command[len + 1], "userrequested")) {
716 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
717 // Run fsck once the file system is remounted in read-only mode.
718 callback_on_ro_remount = unmount_and_fsck;
719 } else if (cmd == ANDROID_RB_RESTART2) {
720 reboot_target = &command[len + 1];
721 }
722 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700723 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
724 return -EINVAL;
725 }
726
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800727 std::string timeout = property_get("ro.build.shutdown_timeout");
728 unsigned int delay = 0;
729
730 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
731 Timer t;
732 // Ask all services to terminate.
733 ServiceManager::GetInstance().ForEachService(
734 [] (Service* s) { s->Terminate(); });
735
736 while (t.duration() < delay) {
737 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
738
739 int service_count = 0;
740 ServiceManager::GetInstance().ForEachService(
741 [&service_count] (Service* s) {
742 // Count the number of services running.
743 // Exclude the console as it will ignore the SIGTERM signal
744 // and not exit.
745 // Note: SVC_CONSOLE actually means "requires console" but
746 // it is only used by the shell.
747 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
748 service_count++;
749 }
750 });
751
752 if (service_count == 0) {
753 // All terminable services terminated. We can exit early.
754 break;
755 }
756
757 // Wait a bit before recounting the number or running services.
758 usleep(kTerminateServiceDelayMicroSeconds);
759 }
760 NOTICE("Terminating running services took %.02f seconds", t.duration());
761 }
762
Yusuke Sato0df08272015-07-08 14:57:07 -0700763 return android_reboot_with_callback(cmd, 0, reboot_target,
764 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700765}
766
Tom Cherryb7349902015-08-26 11:43:36 -0700767static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700768 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700769 return 0;
770}
771
Tom Cherryb7349902015-08-26 11:43:36 -0700772static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700773 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700774}
775
Tom Cherryb7349902015-08-26 11:43:36 -0700776static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700777 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800778}
779
Tom Cherryb7349902015-08-26 11:43:36 -0700780static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700781 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800782}
783
Tom Cherryb7349902015-08-26 11:43:36 -0700784static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800785 struct timezone tz;
786
The Android Open Source Project35237d12008-12-17 18:08:08 -0800787 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700788 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800789 if (settimeofday(NULL, &tz))
790 return -1;
791 return 0;
792}
793
Tom Cherryb7349902015-08-26 11:43:36 -0700794static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700795 int mode = -1;
796 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000797 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700798 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000799 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700800 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000801}
802
Tom Cherryb7349902015-08-26 11:43:36 -0700803static void verity_update_property(fstab_rec *fstab, const char *mount_point,
804 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100805 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
806 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000807}
808
Tom Cherryb7349902015-08-26 11:43:36 -0700809static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700810 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000811}
812
Tom Cherryb7349902015-08-26 11:43:36 -0700813static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700814 const char* path = args[1].c_str();
815 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700816 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817}
818
Tom Cherryb7349902015-08-26 11:43:36 -0700819static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700820 char *buffer = NULL;
821 int rc = 0;
822 int fd1 = -1, fd2 = -1;
823 struct stat info;
824 int brtw, brtr;
825 char *p;
826
Tom Cherry96f67312015-07-30 13:52:55 -0700827 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700828 return -1;
829
Tom Cherry96f67312015-07-30 13:52:55 -0700830 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700831 goto out_err;
832
Tom Cherry96f67312015-07-30 13:52:55 -0700833 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700834 goto out_err;
835
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800836 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700837 goto out_err;
838
839 p = buffer;
840 brtr = info.st_size;
841 while(brtr) {
842 rc = read(fd1, p, brtr);
843 if (rc < 0)
844 goto out_err;
845 if (rc == 0)
846 break;
847 p += rc;
848 brtr -= rc;
849 }
850
851 p = buffer;
852 brtw = info.st_size;
853 while(brtw) {
854 rc = write(fd2, p, brtw);
855 if (rc < 0)
856 goto out_err;
857 if (rc == 0)
858 break;
859 p += rc;
860 brtw -= rc;
861 }
862
863 rc = 0;
864 goto out;
865out_err:
866 rc = -1;
867out:
868 if (buffer)
869 free(buffer);
870 if (fd1 >= 0)
871 close(fd1);
872 if (fd2 >= 0)
873 close(fd2);
874 return rc;
875}
876
Tom Cherryb7349902015-08-26 11:43:36 -0700877static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700878 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700879 if (args.size() == 3) {
880 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700881 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700882 } else if (args.size() == 4) {
883 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
884 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700885 return -errno;
886 } else {
887 return -1;
888 }
889 return 0;
890}
891
892static mode_t get_mode(const char *s) {
893 mode_t mode = 0;
894 while (*s) {
895 if (*s >= '0' && *s <= '7') {
896 mode = (mode<<3) | (*s-'0');
897 } else {
898 return -1;
899 }
900 s++;
901 }
902 return mode;
903}
904
Tom Cherryb7349902015-08-26 11:43:36 -0700905static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700906 mode_t mode = get_mode(args[1].c_str());
907 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700908 return -errno;
909 }
910 return 0;
911}
912
Tom Cherryb7349902015-08-26 11:43:36 -0700913static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400914 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500915
Tom Cherry96f67312015-07-30 13:52:55 -0700916 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
917 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400918 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500919 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400920 return ret;
921}
922
Tom Cherryb7349902015-08-26 11:43:36 -0700923static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400924 int ret = 0;
925
Tom Cherry96f67312015-07-30 13:52:55 -0700926 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
Jeff Sharkey1635afe2016-07-15 16:21:34 -0600927 /* The contents of CE paths are encrypted on FBE devices until user
928 * credentials are presented (filenames inside are mangled), so we need
929 * to delay restorecon of those until vold explicitly requests it. */
930 if (restorecon_recursive_skipce(it->c_str()) < 0) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400931 ret = -errno;
Jeff Sharkey1635afe2016-07-15 16:21:34 -0600932 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400933 }
934 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500935}
936
Tom Cherryb7349902015-08-26 11:43:36 -0700937static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700938 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700939 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
940 ERROR("loglevel: invalid log level'%d'\n", log_level);
941 return -EINVAL;
942 }
943 klog_set_level(log_level);
944 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700945}
946
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700947static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700948 load_persist_props();
949 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800950}
951
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700952static int do_load_system_props(const std::vector<std::string>& args) {
953 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700954 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700955}
956
Tom Cherryb7349902015-08-26 11:43:36 -0700957static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700958 if (args.size() == 2) {
959 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
960 } else if (args.size() == 3) {
961 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800962 } else
963 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700964}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000965
Paul Lawrence806d10b2015-04-28 22:07:10 +0000966/*
967 * Callback to make a directory from the ext4 code
968 */
Tom Cherryb7349902015-08-26 11:43:36 -0700969static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000970 if (make_dir(dir, 0700) && errno != EEXIST) {
971 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000972 }
973
Paul Lawrence806d10b2015-04-28 22:07:10 +0000974 return 0;
975}
976
Paul Crowley749af8c2015-05-28 17:35:06 +0100977static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700978 std::string value = property_get("ro.crypto.type");
979 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100980}
981
Tom Cherryb7349902015-08-26 11:43:36 -0700982static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100983 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000984 return 0;
985 }
Tom Cherry96f67312015-07-30 13:52:55 -0700986 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000987 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000988}
Paul Crowley749af8c2015-05-28 17:35:06 +0100989
Paul Crowley59497452016-02-01 16:37:13 +0000990static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000991 return e4crypt_do_init_user0();
992}
993
Tom Cherryb7349902015-08-26 11:43:36 -0700994BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
995 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
996 static const Map builtin_functions = {
997 {"bootchart_init", {0, 0, do_bootchart_init}},
998 {"chmod", {2, 2, do_chmod}},
999 {"chown", {2, 3, do_chown}},
1000 {"class_reset", {1, 1, do_class_reset}},
1001 {"class_start", {1, 1, do_class_start}},
1002 {"class_stop", {1, 1, do_class_stop}},
1003 {"copy", {2, 2, do_copy}},
1004 {"domainname", {1, 1, do_domainname}},
1005 {"enable", {1, 1, do_enable}},
1006 {"exec", {1, kMax, do_exec}},
1007 {"export", {2, 2, do_export}},
1008 {"hostname", {1, 1, do_hostname}},
1009 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +00001010 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -07001011 {"insmod", {1, kMax, do_insmod}},
1012 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -07001013 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -07001014 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -07001015 {"loglevel", {1, 1, do_loglevel}},
1016 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +08001017 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -07001018 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -07001019 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -07001020 {"powerctl", {1, 1, do_powerctl}},
1021 {"restart", {1, 1, do_restart}},
1022 {"restorecon", {1, kMax, do_restorecon}},
1023 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
1024 {"rm", {1, 1, do_rm}},
1025 {"rmdir", {1, 1, do_rmdir}},
1026 {"setprop", {2, 2, do_setprop}},
1027 {"setrlimit", {3, 3, do_setrlimit}},
1028 {"start", {1, 1, do_start}},
1029 {"stop", {1, 1, do_stop}},
1030 {"swapon_all", {1, 1, do_swapon_all}},
1031 {"symlink", {2, 2, do_symlink}},
1032 {"sysclktz", {1, 1, do_sysclktz}},
1033 {"trigger", {1, 1, do_trigger}},
1034 {"verity_load_state", {0, 0, do_verity_load_state}},
1035 {"verity_update_state", {0, 0, do_verity_update_state}},
1036 {"wait", {1, 2, do_wait}},
1037 {"write", {2, 2, do_write}},
1038 };
1039 return builtin_functions;
1040}