blob: b227d40b790f615d4891047bc552c86c5b5c855a [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>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080039#include <linux/module.h>
Paul Crowleyaf8be582016-05-10 08:52:06 -070040#include <ext4_crypt.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000041#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043#include <selinux/selinux.h>
44#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050045
Elliott Hughesdb3f2672015-03-20 09:45:18 -070046#include <fs_mgr.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070047#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080048#include <android-base/parseint.h>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080049#include <android-base/strings.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080050#include <android-base/stringprintf.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070051#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070052#include <cutils/partition_utils.h>
53#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070054#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070055
Tom Cherryfa0c21c2015-07-23 17:53:11 -070056#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070057#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070058#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070059#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070060#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070061#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "property_service.h"
63#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080064#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070065#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070066
Nick Kralevichbc609542015-01-31 21:39:46 -080067#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070068#define UNMOUNT_CHECK_MS 5000
69#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080070
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080071static const int kTerminateServiceDelayMicroSeconds = 50000;
72
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080073static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070074 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
75 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070076 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070077 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080078 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080079 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070080 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070081 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070082 }
83 close(fd);
84 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085}
86
Tom Cherryb7349902015-08-26 11:43:36 -070087static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070088 struct ifreq ifr;
89 int s, ret;
90
91 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
92
93 s = socket(AF_INET, SOCK_DGRAM, 0);
94 if (s < 0)
95 return -1;
96
97 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
98 if (ret < 0) {
99 goto done;
100 }
101
102 if (up)
103 ifr.ifr_flags |= IFF_UP;
104 else
105 ifr.ifr_flags &= ~IFF_UP;
106
107 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800108
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700109done:
110 close(s);
111 return ret;
112}
113
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700114// Turn off backlight while we are performing power down cleanup activities.
115static void turnOffBacklight() {
116 static const char off[] = "0";
117
118 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
119
120 static const char backlightDir[] = "/sys/class/backlight";
121 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
122 if (!dir) {
123 return;
124 }
125
126 struct dirent *dp;
127 while ((dp = readdir(dir.get())) != NULL) {
128 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
129 (dp->d_name[0] == '.')) {
130 continue;
131 }
132
133 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
134 backlightDir,
135 dp->d_name);
136 android::base::WriteStringToFile(off, fileName);
137 }
138}
139
Paul Crowleyaf8be582016-05-10 08:52:06 -0700140static int wipe_data_via_recovery(const std::string& reason) {
141 const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
142 std::string err;
143 if (!write_bootloader_message(options, &err)) {
Elliott Hughes7f5b29f2016-06-27 09:54:25 -0700144 LOG(ERROR) << "failed to set bootloader message: " << err;
Paul Crowleyaf8be582016-05-10 08:52:06 -0700145 return -1;
146 }
147 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
148 while (1) { pause(); } // never reached
149}
150
Tom Cherryb7349902015-08-26 11:43:36 -0700151static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700152 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
153 return;
154
155 /* First, lazily unmount the directory. This unmount request finishes when
156 * all processes that open a file or directory in |entry->mnt_dir| exit.
157 */
158 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
159
160 /* Next, kill all processes except init, kthreadd, and kthreadd's
161 * children to finish the lazy unmount. Killing all processes here is okay
162 * because this callback function is only called right before reboot().
163 * It might be cleaner to selectively kill processes that actually use
164 * |entry->mnt_dir| rather than killing all, probably by reusing a function
165 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
166 * not allow init to scan /proc/<pid> files which the utility function
167 * heavily relies on. The policy does not allow the process to execute
168 * killall/pkill binaries either. Note that some processes might
169 * automatically restart after kill(), but that is not really a problem
170 * because |entry->mnt_dir| is no longer visible to such new processes.
171 */
Tom Cherrybac32992015-07-31 12:45:25 -0700172 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700173 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
174
Mark Salyzynad575e02016-04-05 08:10:25 -0700175 // Restart Watchdogd to allow us to complete umounting and fsck
176 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
177 if (svc) {
178 do {
179 sched_yield(); // do not be so eager, let cleanup have priority
180 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
181 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
182 svc->Start();
183 }
184
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700185 turnOffBacklight();
186
Yusuke Sato0df08272015-07-08 14:57:07 -0700187 int count = 0;
188 while (count++ < UNMOUNT_CHECK_TIMES) {
189 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
190 if (fd >= 0) {
191 /* |entry->mnt_dir| has sucessfully been unmounted. */
192 close(fd);
193 break;
194 } else if (errno == EBUSY) {
195 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
196 * while then retry.
197 */
198 TEMP_FAILURE_RETRY(
199 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
200 continue;
201 } else {
202 /* Cannot open the device. Give up. */
203 return;
204 }
205 }
206
Mark Salyzynad575e02016-04-05 08:10:25 -0700207 // NB: With watchdog still running, there is no cap on the time it takes
208 // to complete the fsck, from the users perspective the device graphics
209 // and responses are locked-up and they may choose to hold the power
210 // button in frustration if it drags out.
211
Yusuke Sato0df08272015-07-08 14:57:07 -0700212 int st;
213 if (!strcmp(entry->mnt_type, "f2fs")) {
214 const char *f2fs_argv[] = {
215 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
216 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700217 android_fork_execvp_ext(arraysize(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700218 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700219 } else if (!strcmp(entry->mnt_type, "ext4")) {
220 const char *ext4_argv[] = {
221 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
222 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700223 android_fork_execvp_ext(arraysize(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700224 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700225 }
226}
227
Tom Cherryb7349902015-08-26 11:43:36 -0700228static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229 /* Starting a class does not start services
230 * which are explicitly disabled. They must
231 * be started individually.
232 */
Tom Cherrybac32992015-07-31 12:45:25 -0700233 ServiceManager::GetInstance().
234 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235 return 0;
236}
237
Tom Cherryb7349902015-08-26 11:43:36 -0700238static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700239 ServiceManager::GetInstance().
240 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241 return 0;
242}
243
Tom Cherryb7349902015-08-26 11:43:36 -0700244static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700245 ServiceManager::GetInstance().
246 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800247 return 0;
248}
249
Tom Cherryb7349902015-08-26 11:43:36 -0700250static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700251 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252}
253
Tom Cherryb7349902015-08-26 11:43:36 -0700254static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700255 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
256 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700257 return -1;
258 }
Tom Cherrybac32992015-07-31 12:45:25 -0700259 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700260}
261
Tom Cherryb7349902015-08-26 11:43:36 -0700262static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700263 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
264 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800265 return -1;
266 }
Tom Cherrybac32992015-07-31 12:45:25 -0700267 if (!svc->Start()) {
268 return -1;
269 }
270 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800271 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700272}
273
Tom Cherryb7349902015-08-26 11:43:36 -0700274static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700275 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700276}
277
Tom Cherryb7349902015-08-26 11:43:36 -0700278static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700279 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700280}
281
Tom Cherryb7349902015-08-26 11:43:36 -0700282static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700283 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700284}
285
Tom Cherryb7349902015-08-26 11:43:36 -0700286static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800287 int flags = 0;
288 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800289
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800290 if (!(*it).compare("-f")) {
291 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
292 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800293 }
294
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800295 std::string filename = *it++;
296 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
297 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800298}
299
Tom Cherryb7349902015-08-26 11:43:36 -0700300static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700302 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303
304 /* mkdir <path> [mode] [owner] [group] */
305
Tom Cherry96f67312015-07-30 13:52:55 -0700306 if (args.size() >= 3) {
307 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 }
309
Tom Cherry96f67312015-07-30 13:52:55 -0700310 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700311 /* chmod in case the directory already exists */
312 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700313 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700314 }
315 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 return -errno;
317 }
318
Tom Cherry96f67312015-07-30 13:52:55 -0700319 if (args.size() >= 4) {
320 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321 gid_t gid = -1;
322
Tom Cherry96f67312015-07-30 13:52:55 -0700323 if (args.size() == 5) {
324 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 }
326
Tom Cherry96f67312015-07-30 13:52:55 -0700327 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328 return -errno;
329 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700330
331 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
332 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700333 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700334 if (ret == -1) {
335 return -errno;
336 }
337 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338 }
339
Paul Crowleyaf8be582016-05-10 08:52:06 -0700340 if (e4crypt_is_native()) {
341 if (e4crypt_set_directory_policy(args[1].c_str())) {
342 wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
343 return -1;
344 }
345 }
346 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347}
348
Alex Light68ab20f2016-06-23 11:11:39 -0700349/* umount <path> */
350static int do_umount(const std::vector<std::string>& args) {
351 return umount(args[1].c_str());
352}
353
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354static struct {
355 const char *name;
356 unsigned flag;
357} mount_flags[] = {
358 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200359 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 { "nosuid", MS_NOSUID },
361 { "nodev", MS_NODEV },
362 { "nodiratime", MS_NODIRATIME },
363 { "ro", MS_RDONLY },
364 { "rw", 0 },
365 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700366 { "bind", MS_BIND },
367 { "rec", MS_REC },
368 { "unbindable", MS_UNBINDABLE },
369 { "private", MS_PRIVATE },
370 { "slave", MS_SLAVE },
371 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372 { "defaults", 0 },
373 { 0, 0 },
374};
375
Ken Sumrall752923c2010-12-03 16:33:31 -0800376#define DATA_MNT_POINT "/data"
377
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700379static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700381 const char *source, *target, *system;
382 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700384 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700386 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387
Tom Cherry96f67312015-07-30 13:52:55 -0700388 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700390 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700391 flags |= mount_flags[i].flag;
392 break;
393 }
394 }
395
Colin Crosscd0f1732010-04-19 17:10:24 -0700396 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700397 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700398 wait = 1;
399 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700400 else if (na + 1 == args.size())
401 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700402 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700403 }
404
Tom Cherry96f67312015-07-30 13:52:55 -0700405 system = args[1].c_str();
406 source = args[2].c_str();
407 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000408
Elliott Hughes31951162016-06-24 15:15:03 -0700409 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410 int mode, loop, fd;
411 struct loop_info info;
412
413 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800414 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415 if (fd < 0) {
416 return -1;
417 }
418
419 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800420 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800421 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000422 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100423 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000424 return -1;
425 }
426
427 /* if it is a blank loop device */
428 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
429 /* if it becomes our loop device */
430 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
431 close(fd);
432
433 if (mount(tmp, target, system, flags, options) < 0) {
434 ioctl(loop, LOOP_CLR_FD, 0);
435 close(loop);
436 return -1;
437 }
438
439 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800440 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000441 }
442 }
443
444 close(loop);
445 }
446
447 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700448 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000449 return -1;
450 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700451 if (wait)
452 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000453 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700454 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000455 }
456
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700457 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800458
459exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800460 return 0;
461
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700462}
463
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800464/* Imports .rc files from the specified paths. Default ones are applied if none is given.
465 *
466 * start_index: index of the first path in the args list
467 */
468static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700469 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800470 if (args.size() <= start_index) {
471 // Use the default set if no path is given
472 static const std::vector<std::string> init_directories = {
473 "/system/etc/init",
474 "/vendor/etc/init",
475 "/odm/etc/init"
476 };
477
478 for (const auto& dir : init_directories) {
479 parser.ParseConfig(dir);
480 }
481 } else {
482 for (size_t i = start_index; i < args.size(); ++i) {
483 parser.ParseConfig(args[i]);
484 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700485 }
486}
487
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800488/* mount_all <fstab> [ <path> ]*
489 *
JP Abgrallcee20682014-07-02 14:26:54 -0700490 * This function might request a reboot, in which case it will
491 * not return.
492 */
Tom Cherryb7349902015-08-26 11:43:36 -0700493static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700494 pid_t pid;
495 int ret = -1;
496 int child_ret = -1;
497 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800498 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700499
Tom Cherry96f67312015-07-30 13:52:55 -0700500 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501 /*
502 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
503 * do the call in the child to provide protection to the main init
504 * process if anything goes wrong (crash or memory leak), and wait for
505 * the child to finish in the parent.
506 */
507 pid = fork();
508 if (pid > 0) {
509 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700510 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700511 if (wp_ret == -1) {
512 // Unexpected error code. We will continue anyway.
513 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700514 }
515
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700516 if (WIFEXITED(status)) {
517 ret = WEXITSTATUS(status);
518 } else {
519 ret = -1;
520 }
521 } else if (pid == 0) {
522 /* child, call fs_mgr_mount_all() */
523 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800524 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800525 child_ret = fs_mgr_mount_all(fstab);
526 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700527 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700528 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700529 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700530 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700531 } else {
532 /* fork failed, return an error */
533 return -1;
534 }
535
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800536 /* Paths of .rc files are specified at the 2nd argument and beyond */
537 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700538
JP Abgrallf22b7452014-07-02 13:16:04 -0700539 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700540 ActionManager::GetInstance().QueueEventTrigger("encrypt");
JP Abgrallf22b7452014-07-02 13:16:04 -0700541 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700542 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000543 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700544 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
JP Abgrallf22b7452014-07-02 13:16:04 -0700545 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700546 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800547 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
548 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
549 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700550 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700551 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
552 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700553 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
Paul Crowleyaf8be582016-05-10 08:52:06 -0700554 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700555 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800556 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000557 if (e4crypt_install_keyring()) {
558 return -1;
559 }
560 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000561 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000562
563 // Although encrypted, we have device key, so we do not need to
564 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700565 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700566 } else if (ret > 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700567 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << ret;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700568 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700569 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700570
571 return ret;
572}
573
Tom Cherryb7349902015-08-26 11:43:36 -0700574static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700575 struct fstab *fstab;
576 int ret;
577
Tom Cherry96f67312015-07-30 13:52:55 -0700578 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700579 ret = fs_mgr_swapon_all(fstab);
580 fs_mgr_free_fstab(fstab);
581
582 return ret;
583}
584
Tom Cherryb7349902015-08-26 11:43:36 -0700585static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700586 const char* name = args[1].c_str();
587 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700588 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589 return 0;
590}
591
Tom Cherryb7349902015-08-26 11:43:36 -0700592static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 struct rlimit limit;
594 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700595 resource = std::stoi(args[1]);
596 limit.rlim_cur = std::stoi(args[2]);
597 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700598 return setrlimit(resource, &limit);
599}
600
Tom Cherryb7349902015-08-26 11:43:36 -0700601static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700602 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
603 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700604 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700605 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 }
Tom Cherrybac32992015-07-31 12:45:25 -0700607 if (!svc->Start())
608 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700609 return 0;
610}
611
Tom Cherryb7349902015-08-26 11:43:36 -0700612static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700613 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
614 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700615 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700616 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 }
Tom Cherrybac32992015-07-31 12:45:25 -0700618 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700619 return 0;
620}
621
Tom Cherryb7349902015-08-26 11:43:36 -0700622static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700623 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
624 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700625 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700626 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 }
Tom Cherrybac32992015-07-31 12:45:25 -0700628 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700629 return 0;
630}
631
Tom Cherryb7349902015-08-26 11:43:36 -0700632static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700633 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700634 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700635 unsigned int cmd = 0;
636 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700637 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700638
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700639 if (strncmp(command, "shutdown", 8) == 0) {
640 cmd = ANDROID_RB_POWEROFF;
641 len = 8;
642 } else if (strncmp(command, "reboot", 6) == 0) {
643 cmd = ANDROID_RB_RESTART2;
644 len = 6;
645 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700646 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700647 return -EINVAL;
648 }
649
650 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700651 if (cmd == ANDROID_RB_POWEROFF &&
652 !strcmp(&command[len + 1], "userrequested")) {
653 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
654 // Run fsck once the file system is remounted in read-only mode.
655 callback_on_ro_remount = unmount_and_fsck;
656 } else if (cmd == ANDROID_RB_RESTART2) {
657 reboot_target = &command[len + 1];
658 }
659 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700660 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700661 return -EINVAL;
662 }
663
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800664 std::string timeout = property_get("ro.build.shutdown_timeout");
665 unsigned int delay = 0;
666
667 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
668 Timer t;
669 // Ask all services to terminate.
670 ServiceManager::GetInstance().ForEachService(
671 [] (Service* s) { s->Terminate(); });
672
673 while (t.duration() < delay) {
674 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
675
676 int service_count = 0;
677 ServiceManager::GetInstance().ForEachService(
678 [&service_count] (Service* s) {
679 // Count the number of services running.
680 // Exclude the console as it will ignore the SIGTERM signal
681 // and not exit.
682 // Note: SVC_CONSOLE actually means "requires console" but
683 // it is only used by the shell.
684 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
685 service_count++;
686 }
687 });
688
689 if (service_count == 0) {
690 // All terminable services terminated. We can exit early.
691 break;
692 }
693
694 // Wait a bit before recounting the number or running services.
695 usleep(kTerminateServiceDelayMicroSeconds);
696 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700697 LOG(VERBOSE) << "Terminating running services took " << t.duration() << " seconds";
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800698 }
699
Yusuke Sato0df08272015-07-08 14:57:07 -0700700 return android_reboot_with_callback(cmd, 0, reboot_target,
701 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700702}
703
Tom Cherryb7349902015-08-26 11:43:36 -0700704static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700705 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700706 return 0;
707}
708
Tom Cherryb7349902015-08-26 11:43:36 -0700709static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700710 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700711}
712
Tom Cherryb7349902015-08-26 11:43:36 -0700713static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700714 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800715}
716
Tom Cherryb7349902015-08-26 11:43:36 -0700717static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700718 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800719}
720
Tom Cherryb7349902015-08-26 11:43:36 -0700721static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800722 struct timezone tz;
723
The Android Open Source Project35237d12008-12-17 18:08:08 -0800724 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700725 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800726 if (settimeofday(NULL, &tz))
727 return -1;
728 return 0;
729}
730
Tom Cherryb7349902015-08-26 11:43:36 -0700731static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700732 int mode = -1;
733 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000734 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700735 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000736 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700737 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000738}
739
Tom Cherryb7349902015-08-26 11:43:36 -0700740static void verity_update_property(fstab_rec *fstab, const char *mount_point,
741 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100742 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
743 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000744}
745
Tom Cherryb7349902015-08-26 11:43:36 -0700746static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700747 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000748}
749
Tom Cherryb7349902015-08-26 11:43:36 -0700750static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700751 const char* path = args[1].c_str();
752 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700753 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700754}
755
Tom Cherryb7349902015-08-26 11:43:36 -0700756static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700757 char *buffer = NULL;
758 int rc = 0;
759 int fd1 = -1, fd2 = -1;
760 struct stat info;
761 int brtw, brtr;
762 char *p;
763
Tom Cherry96f67312015-07-30 13:52:55 -0700764 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700765 return -1;
766
Tom Cherry96f67312015-07-30 13:52:55 -0700767 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700768 goto out_err;
769
Tom Cherry96f67312015-07-30 13:52:55 -0700770 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700771 goto out_err;
772
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800773 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700774 goto out_err;
775
776 p = buffer;
777 brtr = info.st_size;
778 while(brtr) {
779 rc = read(fd1, p, brtr);
780 if (rc < 0)
781 goto out_err;
782 if (rc == 0)
783 break;
784 p += rc;
785 brtr -= rc;
786 }
787
788 p = buffer;
789 brtw = info.st_size;
790 while(brtw) {
791 rc = write(fd2, p, brtw);
792 if (rc < 0)
793 goto out_err;
794 if (rc == 0)
795 break;
796 p += rc;
797 brtw -= rc;
798 }
799
800 rc = 0;
801 goto out;
802out_err:
803 rc = -1;
804out:
805 if (buffer)
806 free(buffer);
807 if (fd1 >= 0)
808 close(fd1);
809 if (fd2 >= 0)
810 close(fd2);
811 return rc;
812}
813
Tom Cherryb7349902015-08-26 11:43:36 -0700814static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700816 if (args.size() == 3) {
817 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700819 } else if (args.size() == 4) {
820 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
821 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700822 return -errno;
823 } else {
824 return -1;
825 }
826 return 0;
827}
828
829static mode_t get_mode(const char *s) {
830 mode_t mode = 0;
831 while (*s) {
832 if (*s >= '0' && *s <= '7') {
833 mode = (mode<<3) | (*s-'0');
834 } else {
835 return -1;
836 }
837 s++;
838 }
839 return mode;
840}
841
Tom Cherryb7349902015-08-26 11:43:36 -0700842static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700843 mode_t mode = get_mode(args[1].c_str());
844 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700845 return -errno;
846 }
847 return 0;
848}
849
Tom Cherryb7349902015-08-26 11:43:36 -0700850static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400851 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500852
Tom Cherry96f67312015-07-30 13:52:55 -0700853 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
854 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400855 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500856 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400857 return ret;
858}
859
Tom Cherryb7349902015-08-26 11:43:36 -0700860static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400861 int ret = 0;
862
Tom Cherry96f67312015-07-30 13:52:55 -0700863 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
Jeff Sharkey1635afe2016-07-15 16:21:34 -0600864 /* The contents of CE paths are encrypted on FBE devices until user
865 * credentials are presented (filenames inside are mangled), so we need
866 * to delay restorecon of those until vold explicitly requests it. */
867 if (restorecon_recursive_skipce(it->c_str()) < 0) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400868 ret = -errno;
Jeff Sharkey1635afe2016-07-15 16:21:34 -0600869 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400870 }
871 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500872}
873
Tom Cherryb7349902015-08-26 11:43:36 -0700874static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700875 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700876 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700877 LOG(ERROR) << "loglevel: invalid log level " << log_level;
Riley Andrews1bbef882014-06-26 13:55:03 -0700878 return -EINVAL;
879 }
880 klog_set_level(log_level);
881 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700882}
883
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700884static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700885 load_persist_props();
886 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800887}
888
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700889static int do_load_system_props(const std::vector<std::string>& args) {
890 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700891 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700892}
893
Tom Cherryb7349902015-08-26 11:43:36 -0700894static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700895 if (args.size() == 2) {
896 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
897 } else if (args.size() == 3) {
898 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800899 } else
900 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700901}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000902
Paul Lawrence806d10b2015-04-28 22:07:10 +0000903/*
904 * Callback to make a directory from the ext4 code
905 */
Tom Cherryb7349902015-08-26 11:43:36 -0700906static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000907 if (make_dir(dir, 0700) && errno != EEXIST) {
908 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000909 }
910
Paul Lawrence806d10b2015-04-28 22:07:10 +0000911 return 0;
912}
913
Paul Crowley749af8c2015-05-28 17:35:06 +0100914static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700915 std::string value = property_get("ro.crypto.type");
916 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100917}
918
Tom Cherryb7349902015-08-26 11:43:36 -0700919static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100920 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000921 return 0;
922 }
Tom Cherry96f67312015-07-30 13:52:55 -0700923 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000924 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000925}
Paul Crowley749af8c2015-05-28 17:35:06 +0100926
Paul Crowley59497452016-02-01 16:37:13 +0000927static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000928 return e4crypt_do_init_user0();
929}
930
Tom Cherryb7349902015-08-26 11:43:36 -0700931BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
932 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
933 static const Map builtin_functions = {
934 {"bootchart_init", {0, 0, do_bootchart_init}},
935 {"chmod", {2, 2, do_chmod}},
936 {"chown", {2, 3, do_chown}},
937 {"class_reset", {1, 1, do_class_reset}},
938 {"class_start", {1, 1, do_class_start}},
939 {"class_stop", {1, 1, do_class_stop}},
940 {"copy", {2, 2, do_copy}},
941 {"domainname", {1, 1, do_domainname}},
942 {"enable", {1, 1, do_enable}},
943 {"exec", {1, kMax, do_exec}},
944 {"export", {2, 2, do_export}},
945 {"hostname", {1, 1, do_hostname}},
946 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000947 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700948 {"insmod", {1, kMax, do_insmod}},
949 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700950 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700951 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700952 {"loglevel", {1, 1, do_loglevel}},
953 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800954 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700955 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700956 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700957 {"powerctl", {1, 1, do_powerctl}},
958 {"restart", {1, 1, do_restart}},
959 {"restorecon", {1, kMax, do_restorecon}},
960 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
961 {"rm", {1, 1, do_rm}},
962 {"rmdir", {1, 1, do_rmdir}},
963 {"setprop", {2, 2, do_setprop}},
964 {"setrlimit", {3, 3, do_setrlimit}},
965 {"start", {1, 1, do_start}},
966 {"stop", {1, 1, do_stop}},
967 {"swapon_all", {1, 1, do_swapon_all}},
968 {"symlink", {2, 2, do_symlink}},
969 {"sysclktz", {1, 1, do_sysclktz}},
970 {"trigger", {1, 1, do_trigger}},
971 {"verity_load_state", {0, 0, do_verity_load_state}},
972 {"verity_update_state", {0, 0, do_verity_update_state}},
973 {"wait", {1, 2, do_wait}},
974 {"write", {2, 2, do_write}},
975 };
976 return builtin_functions;
977}