blob: 3d220c53b15df314beaf3b54931fc6c24430cdc2 [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 Lawrence806d10b2015-04-28 22:07:10 +000040#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042#include <selinux/selinux.h>
43#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044
Elliott Hughesdb3f2672015-03-20 09:45:18 -070045#include <fs_mgr.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070046#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080047#include <android-base/parseint.h>
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080048#include <android-base/strings.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080049#include <android-base/stringprintf.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070050#include <cutils/partition_utils.h>
51#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070052#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070053
Tom Cherryfa0c21c2015-07-23 17:53:11 -070054#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070055#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070056#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070057#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070058#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070059#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070060#include "property_service.h"
61#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080062#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070063#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070064
Nick Kralevichbc609542015-01-31 21:39:46 -080065#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070066#define UNMOUNT_CHECK_MS 5000
67#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080068
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080069static const int kTerminateServiceDelayMicroSeconds = 50000;
70
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080071static int insmod(const char *filename, const char *options, int flags) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070072 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
73 if (fd == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070074 PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed";
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080076 }
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +080077 int rc = syscall(__NR_finit_module, fd, options, flags);
Nick Kralevich124a9c92016-03-27 16:55:59 -070078 if (rc == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070079 PLOG(ERROR) << "finit_module for \"" << filename << "\" failed";
Nick Kralevich124a9c92016-03-27 16:55:59 -070080 }
81 close(fd);
82 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083}
84
Tom Cherryb7349902015-08-26 11:43:36 -070085static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086 struct ifreq ifr;
87 int s, ret;
88
89 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
90
91 s = socket(AF_INET, SOCK_DGRAM, 0);
92 if (s < 0)
93 return -1;
94
95 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
96 if (ret < 0) {
97 goto done;
98 }
99
100 if (up)
101 ifr.ifr_flags |= IFF_UP;
102 else
103 ifr.ifr_flags &= ~IFF_UP;
104
105 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107done:
108 close(s);
109 return ret;
110}
111
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700112// Turn off backlight while we are performing power down cleanup activities.
113static void turnOffBacklight() {
114 static const char off[] = "0";
115
116 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
117
118 static const char backlightDir[] = "/sys/class/backlight";
119 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
120 if (!dir) {
121 return;
122 }
123
124 struct dirent *dp;
125 while ((dp = readdir(dir.get())) != NULL) {
126 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
127 (dp->d_name[0] == '.')) {
128 continue;
129 }
130
131 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
132 backlightDir,
133 dp->d_name);
134 android::base::WriteStringToFile(off, fileName);
135 }
136}
137
Tom Cherryb7349902015-08-26 11:43:36 -0700138static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700139 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
140 return;
141
142 /* First, lazily unmount the directory. This unmount request finishes when
143 * all processes that open a file or directory in |entry->mnt_dir| exit.
144 */
145 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
146
147 /* Next, kill all processes except init, kthreadd, and kthreadd's
148 * children to finish the lazy unmount. Killing all processes here is okay
149 * because this callback function is only called right before reboot().
150 * It might be cleaner to selectively kill processes that actually use
151 * |entry->mnt_dir| rather than killing all, probably by reusing a function
152 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
153 * not allow init to scan /proc/<pid> files which the utility function
154 * heavily relies on. The policy does not allow the process to execute
155 * killall/pkill binaries either. Note that some processes might
156 * automatically restart after kill(), but that is not really a problem
157 * because |entry->mnt_dir| is no longer visible to such new processes.
158 */
Tom Cherrybac32992015-07-31 12:45:25 -0700159 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700160 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
161
Mark Salyzynad575e02016-04-05 08:10:25 -0700162 // Restart Watchdogd to allow us to complete umounting and fsck
163 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
164 if (svc) {
165 do {
166 sched_yield(); // do not be so eager, let cleanup have priority
167 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
168 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
169 svc->Start();
170 }
171
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700172 turnOffBacklight();
173
Yusuke Sato0df08272015-07-08 14:57:07 -0700174 int count = 0;
175 while (count++ < UNMOUNT_CHECK_TIMES) {
176 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
177 if (fd >= 0) {
178 /* |entry->mnt_dir| has sucessfully been unmounted. */
179 close(fd);
180 break;
181 } else if (errno == EBUSY) {
182 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
183 * while then retry.
184 */
185 TEMP_FAILURE_RETRY(
186 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
187 continue;
188 } else {
189 /* Cannot open the device. Give up. */
190 return;
191 }
192 }
193
Mark Salyzynad575e02016-04-05 08:10:25 -0700194 // NB: With watchdog still running, there is no cap on the time it takes
195 // to complete the fsck, from the users perspective the device graphics
196 // and responses are locked-up and they may choose to hold the power
197 // button in frustration if it drags out.
198
Yusuke Sato0df08272015-07-08 14:57:07 -0700199 int st;
200 if (!strcmp(entry->mnt_type, "f2fs")) {
201 const char *f2fs_argv[] = {
202 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
203 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700204 android_fork_execvp_ext(arraysize(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700205 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700206 } else if (!strcmp(entry->mnt_type, "ext4")) {
207 const char *ext4_argv[] = {
208 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
209 };
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700210 android_fork_execvp_ext(arraysize(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700211 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700212 }
213}
214
Tom Cherryb7349902015-08-26 11:43:36 -0700215static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216 /* Starting a class does not start services
217 * which are explicitly disabled. They must
218 * be started individually.
219 */
Tom Cherrybac32992015-07-31 12:45:25 -0700220 ServiceManager::GetInstance().
221 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700222 return 0;
223}
224
Tom Cherryb7349902015-08-26 11:43:36 -0700225static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700226 ServiceManager::GetInstance().
227 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228 return 0;
229}
230
Tom Cherryb7349902015-08-26 11:43:36 -0700231static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700232 ServiceManager::GetInstance().
233 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800234 return 0;
235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700238 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239}
240
Tom Cherryb7349902015-08-26 11:43:36 -0700241static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700242 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
243 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700244 return -1;
245 }
Tom Cherrybac32992015-07-31 12:45:25 -0700246 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700247}
248
Tom Cherryb7349902015-08-26 11:43:36 -0700249static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700250 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
251 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800252 return -1;
253 }
Tom Cherrybac32992015-07-31 12:45:25 -0700254 if (!svc->Start()) {
255 return -1;
256 }
257 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800258 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700262 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700263}
264
Tom Cherryb7349902015-08-26 11:43:36 -0700265static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700266 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700267}
268
Tom Cherryb7349902015-08-26 11:43:36 -0700269static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700270 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271}
272
Tom Cherryb7349902015-08-26 11:43:36 -0700273static int do_insmod(const std::vector<std::string>& args) {
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800274 int flags = 0;
275 auto it = args.begin() + 1;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800276
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800277 if (!(*it).compare("-f")) {
278 flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
279 it++;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800280 }
281
Hung-ying Tyanbfa6d752016-05-17 18:49:10 +0800282 std::string filename = *it++;
283 std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' ');
284 return insmod(filename.c_str(), options.c_str(), flags);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800285}
286
Tom Cherryb7349902015-08-26 11:43:36 -0700287static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700289 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700290
291 /* mkdir <path> [mode] [owner] [group] */
292
Tom Cherry96f67312015-07-30 13:52:55 -0700293 if (args.size() >= 3) {
294 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295 }
296
Tom Cherry96f67312015-07-30 13:52:55 -0700297 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700298 /* chmod in case the directory already exists */
299 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700300 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700301 }
302 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 return -errno;
304 }
305
Tom Cherry96f67312015-07-30 13:52:55 -0700306 if (args.size() >= 4) {
307 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700308 gid_t gid = -1;
309
Tom Cherry96f67312015-07-30 13:52:55 -0700310 if (args.size() == 5) {
311 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 }
313
Tom Cherry96f67312015-07-30 13:52:55 -0700314 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 return -errno;
316 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700317
318 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
319 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700320 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700321 if (ret == -1) {
322 return -errno;
323 }
324 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700325 }
326
Tom Cherry96f67312015-07-30 13:52:55 -0700327 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328}
329
330static struct {
331 const char *name;
332 unsigned flag;
333} mount_flags[] = {
334 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200335 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700336 { "nosuid", MS_NOSUID },
337 { "nodev", MS_NODEV },
338 { "nodiratime", MS_NODIRATIME },
339 { "ro", MS_RDONLY },
340 { "rw", 0 },
341 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700342 { "bind", MS_BIND },
343 { "rec", MS_REC },
344 { "unbindable", MS_UNBINDABLE },
345 { "private", MS_PRIVATE },
346 { "slave", MS_SLAVE },
347 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700348 { "defaults", 0 },
349 { 0, 0 },
350};
351
Ken Sumrall752923c2010-12-03 16:33:31 -0800352#define DATA_MNT_POINT "/data"
353
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700355static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700357 const char *source, *target, *system;
358 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700360 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700362 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363
Tom Cherry96f67312015-07-30 13:52:55 -0700364 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700366 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700367 flags |= mount_flags[i].flag;
368 break;
369 }
370 }
371
Colin Crosscd0f1732010-04-19 17:10:24 -0700372 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700373 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700374 wait = 1;
375 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700376 else if (na + 1 == args.size())
377 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700378 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 }
380
Tom Cherry96f67312015-07-30 13:52:55 -0700381 system = args[1].c_str();
382 source = args[2].c_str();
383 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000384
Elliott Hughes31951162016-06-24 15:15:03 -0700385 if (!strncmp(source, "loop@", 5)) {
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000386 int mode, loop, fd;
387 struct loop_info info;
388
389 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800390 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391 if (fd < 0) {
392 return -1;
393 }
394
395 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800396 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800397 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000398 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100399 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000400 return -1;
401 }
402
403 /* if it is a blank loop device */
404 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
405 /* if it becomes our loop device */
406 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
407 close(fd);
408
409 if (mount(tmp, target, system, flags, options) < 0) {
410 ioctl(loop, LOOP_CLR_FD, 0);
411 close(loop);
412 return -1;
413 }
414
415 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800416 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000417 }
418 }
419
420 close(loop);
421 }
422
423 close(fd);
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700424 LOG(ERROR) << "out of loopback devices";
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000425 return -1;
426 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700427 if (wait)
428 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000429 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700430 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000431 }
432
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800434
435exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800436 return 0;
437
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700438}
439
Tom Cherryb7349902015-08-26 11:43:36 -0700440static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700441 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800442 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700443 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700444 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
445 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700446 close(fd);
447 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700448 PLOG(ERROR) << "could not open /cache/recovery/command";
JP Abgrallcee20682014-07-02 14:26:54 -0700449 return -1;
450 }
451 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
452 while (1) { pause(); } // never reached
453}
454
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800455/* Imports .rc files from the specified paths. Default ones are applied if none is given.
456 *
457 * start_index: index of the first path in the args list
458 */
459static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700460 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800461 if (args.size() <= start_index) {
462 // Use the default set if no path is given
463 static const std::vector<std::string> init_directories = {
464 "/system/etc/init",
465 "/vendor/etc/init",
466 "/odm/etc/init"
467 };
468
469 for (const auto& dir : init_directories) {
470 parser.ParseConfig(dir);
471 }
472 } else {
473 for (size_t i = start_index; i < args.size(); ++i) {
474 parser.ParseConfig(args[i]);
475 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700476 }
477}
478
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800479/* mount_all <fstab> [ <path> ]*
480 *
JP Abgrallcee20682014-07-02 14:26:54 -0700481 * This function might request a reboot, in which case it will
482 * not return.
483 */
Tom Cherryb7349902015-08-26 11:43:36 -0700484static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700485 int ret = -1;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700486
Tom Cherry96f67312015-07-30 13:52:55 -0700487 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700488 /*
489 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
490 * do the call in the child to provide protection to the main init
491 * process if anything goes wrong (crash or memory leak), and wait for
492 * the child to finish in the parent.
493 */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700494 pid_t pid = fork();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700495 if (pid > 0) {
496 /* Parent. Wait for the child to return */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700497 int status;
Paul Lawrence40af0922014-09-16 14:31:23 -0700498 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700499 if (wp_ret == -1) {
500 // Unexpected error code. We will continue anyway.
501 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700502 }
503
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 if (WIFEXITED(status)) {
505 ret = WEXITSTATUS(status);
506 } else {
507 ret = -1;
508 }
509 } else if (pid == 0) {
510 /* child, call fs_mgr_mount_all() */
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700511
512 // So we can always see what fs_mgr_mount_all() does.
513 // Only needed if someone explicitly changes the default log level in their init.rc.
514 android::base::ScopedLogSeverity info(android::base::INFO);
515
516 struct fstab* fstab = fs_mgr_read_fstab(fstabfile);
517 int child_ret = fs_mgr_mount_all(fstab);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800518 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700520 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700522 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700523 } else {
524 /* fork failed, return an error */
525 return -1;
526 }
527
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800528 /* Paths of .rc files are specified at the 2nd argument and beyond */
529 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700530
JP Abgrallf22b7452014-07-02 13:16:04 -0700531 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800532 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700533 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700534 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000535 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800536 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700537 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700538 property_set("ro.crypto.state", "unencrypted");
539 /* If fs_mgr determined this is an unencrypted device, then trigger
540 * that action.
541 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700542 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700543 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
544 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700545 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
JP Abgrallcee20682014-07-02 14:26:54 -0700546 ret = wipe_data_via_recovery();
547 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000548 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000549 if (e4crypt_install_keyring()) {
550 return -1;
551 }
552 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000553 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000554
555 // Although encrypted, we have device key, so we do not need to
556 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700557 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000558 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
559 if (e4crypt_install_keyring()) {
560 return -1;
561 }
562 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000563 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000564 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700565 } else if (ret > 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700566 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << ret;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700567 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700568 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700569
570 return ret;
571}
572
Tom Cherryb7349902015-08-26 11:43:36 -0700573static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700574 struct fstab *fstab;
575 int ret;
576
Tom Cherry96f67312015-07-30 13:52:55 -0700577 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700578 ret = fs_mgr_swapon_all(fstab);
579 fs_mgr_free_fstab(fstab);
580
581 return ret;
582}
583
Tom Cherryb7349902015-08-26 11:43:36 -0700584static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700585 const char* name = args[1].c_str();
586 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700587 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700588 return 0;
589}
590
Tom Cherryb7349902015-08-26 11:43:36 -0700591static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 struct rlimit limit;
593 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700594 resource = std::stoi(args[1]);
595 limit.rlim_cur = std::stoi(args[2]);
596 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700597 return setrlimit(resource, &limit);
598}
599
Tom Cherryb7349902015-08-26 11:43:36 -0700600static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700601 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
602 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700603 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700604 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700605 }
Tom Cherrybac32992015-07-31 12:45:25 -0700606 if (!svc->Start())
607 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700608 return 0;
609}
610
Tom Cherryb7349902015-08-26 11:43:36 -0700611static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700612 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
613 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700614 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700615 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700616 }
Tom Cherrybac32992015-07-31 12:45:25 -0700617 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700618 return 0;
619}
620
Tom Cherryb7349902015-08-26 11:43:36 -0700621static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700622 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
623 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700624 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700625 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700626 }
Tom Cherrybac32992015-07-31 12:45:25 -0700627 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700628 return 0;
629}
630
Tom Cherryb7349902015-08-26 11:43:36 -0700631static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700632 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700633 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700634 unsigned int cmd = 0;
635 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700636 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700637
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700638 if (strncmp(command, "shutdown", 8) == 0) {
639 cmd = ANDROID_RB_POWEROFF;
640 len = 8;
641 } else if (strncmp(command, "reboot", 6) == 0) {
642 cmd = ANDROID_RB_RESTART2;
643 len = 6;
644 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700645 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646 return -EINVAL;
647 }
648
649 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700650 if (cmd == ANDROID_RB_POWEROFF &&
651 !strcmp(&command[len + 1], "userrequested")) {
652 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
653 // Run fsck once the file system is remounted in read-only mode.
654 callback_on_ro_remount = unmount_and_fsck;
655 } else if (cmd == ANDROID_RB_RESTART2) {
656 reboot_target = &command[len + 1];
657 }
658 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700659 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700660 return -EINVAL;
661 }
662
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800663 std::string timeout = property_get("ro.build.shutdown_timeout");
664 unsigned int delay = 0;
665
666 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
667 Timer t;
668 // Ask all services to terminate.
669 ServiceManager::GetInstance().ForEachService(
670 [] (Service* s) { s->Terminate(); });
671
672 while (t.duration() < delay) {
673 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
674
675 int service_count = 0;
676 ServiceManager::GetInstance().ForEachService(
677 [&service_count] (Service* s) {
678 // Count the number of services running.
679 // Exclude the console as it will ignore the SIGTERM signal
680 // and not exit.
681 // Note: SVC_CONSOLE actually means "requires console" but
682 // it is only used by the shell.
683 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
684 service_count++;
685 }
686 });
687
688 if (service_count == 0) {
689 // All terminable services terminated. We can exit early.
690 break;
691 }
692
693 // Wait a bit before recounting the number or running services.
694 usleep(kTerminateServiceDelayMicroSeconds);
695 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700696 LOG(VERBOSE) << "Terminating running services took " << t.duration() << " seconds";
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800697 }
698
Yusuke Sato0df08272015-07-08 14:57:07 -0700699 return android_reboot_with_callback(cmd, 0, reboot_target,
700 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700701}
702
Tom Cherryb7349902015-08-26 11:43:36 -0700703static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700704 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700705 return 0;
706}
707
Tom Cherryb7349902015-08-26 11:43:36 -0700708static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700709 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700710}
711
Tom Cherryb7349902015-08-26 11:43:36 -0700712static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700713 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800714}
715
Tom Cherryb7349902015-08-26 11:43:36 -0700716static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700717 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800718}
719
Tom Cherryb7349902015-08-26 11:43:36 -0700720static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800721 struct timezone tz;
722
The Android Open Source Project35237d12008-12-17 18:08:08 -0800723 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700724 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800725 if (settimeofday(NULL, &tz))
726 return -1;
727 return 0;
728}
729
Tom Cherryb7349902015-08-26 11:43:36 -0700730static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700731 int mode = -1;
732 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000733 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700734 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000735 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700736 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000737}
738
Tom Cherryb7349902015-08-26 11:43:36 -0700739static void verity_update_property(fstab_rec *fstab, const char *mount_point,
740 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100741 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
742 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000743}
744
Tom Cherryb7349902015-08-26 11:43:36 -0700745static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700746 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000747}
748
Tom Cherryb7349902015-08-26 11:43:36 -0700749static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700750 const char* path = args[1].c_str();
751 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700752 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700753}
754
Tom Cherryb7349902015-08-26 11:43:36 -0700755static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700756 char *buffer = NULL;
757 int rc = 0;
758 int fd1 = -1, fd2 = -1;
759 struct stat info;
760 int brtw, brtr;
761 char *p;
762
Tom Cherry96f67312015-07-30 13:52:55 -0700763 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700764 return -1;
765
Tom Cherry96f67312015-07-30 13:52:55 -0700766 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700767 goto out_err;
768
Tom Cherry96f67312015-07-30 13:52:55 -0700769 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700770 goto out_err;
771
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800772 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700773 goto out_err;
774
775 p = buffer;
776 brtr = info.st_size;
777 while(brtr) {
778 rc = read(fd1, p, brtr);
779 if (rc < 0)
780 goto out_err;
781 if (rc == 0)
782 break;
783 p += rc;
784 brtr -= rc;
785 }
786
787 p = buffer;
788 brtw = info.st_size;
789 while(brtw) {
790 rc = write(fd2, p, brtw);
791 if (rc < 0)
792 goto out_err;
793 if (rc == 0)
794 break;
795 p += rc;
796 brtw -= rc;
797 }
798
799 rc = 0;
800 goto out;
801out_err:
802 rc = -1;
803out:
804 if (buffer)
805 free(buffer);
806 if (fd1 >= 0)
807 close(fd1);
808 if (fd2 >= 0)
809 close(fd2);
810 return rc;
811}
812
Tom Cherryb7349902015-08-26 11:43:36 -0700813static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700814 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700815 if (args.size() == 3) {
816 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700817 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700818 } else if (args.size() == 4) {
819 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
820 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821 return -errno;
822 } else {
823 return -1;
824 }
825 return 0;
826}
827
828static mode_t get_mode(const char *s) {
829 mode_t mode = 0;
830 while (*s) {
831 if (*s >= '0' && *s <= '7') {
832 mode = (mode<<3) | (*s-'0');
833 } else {
834 return -1;
835 }
836 s++;
837 }
838 return mode;
839}
840
Tom Cherryb7349902015-08-26 11:43:36 -0700841static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700842 mode_t mode = get_mode(args[1].c_str());
843 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700844 return -errno;
845 }
846 return 0;
847}
848
Tom Cherryb7349902015-08-26 11:43:36 -0700849static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400850 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500851
Tom Cherry96f67312015-07-30 13:52:55 -0700852 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
853 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400854 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500855 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400856 return ret;
857}
858
Tom Cherryb7349902015-08-26 11:43:36 -0700859static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400860 int ret = 0;
861
Tom Cherry96f67312015-07-30 13:52:55 -0700862 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
863 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400864 ret = -errno;
865 }
866 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500867}
868
Tom Cherryb7349902015-08-26 11:43:36 -0700869static int do_loglevel(const std::vector<std::string>& args) {
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700870 // TODO: support names instead/as well?
Tom Cherry96f67312015-07-30 13:52:55 -0700871 int log_level = std::stoi(args[1]);
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700872 android::base::LogSeverity severity;
873 switch (log_level) {
874 case 7: severity = android::base::DEBUG; break;
875 case 6: severity = android::base::INFO; break;
876 case 5:
877 case 4: severity = android::base::WARNING; break;
878 case 3: severity = android::base::ERROR; break;
879 case 2:
880 case 1:
881 case 0: severity = android::base::FATAL; break;
882 default:
883 LOG(ERROR) << "loglevel: invalid log level " << log_level;
884 return -EINVAL;
Riley Andrews1bbef882014-06-26 13:55:03 -0700885 }
Elliott Hughes7bc87a52016-08-04 16:09:39 -0700886 android::base::SetMinimumLogSeverity(severity);
Riley Andrews1bbef882014-06-26 13:55:03 -0700887 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700888}
889
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700890static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700891 load_persist_props();
892 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800893}
894
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700895static int do_load_system_props(const std::vector<std::string>& args) {
896 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700897 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700898}
899
Tom Cherryb7349902015-08-26 11:43:36 -0700900static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700901 if (args.size() == 2) {
902 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
903 } else if (args.size() == 3) {
904 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800905 } else
906 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700907}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000908
Paul Lawrence806d10b2015-04-28 22:07:10 +0000909/*
910 * Callback to make a directory from the ext4 code
911 */
Tom Cherryb7349902015-08-26 11:43:36 -0700912static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000913 if (make_dir(dir, 0700) && errno != EEXIST) {
914 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000915 }
916
Paul Lawrence806d10b2015-04-28 22:07:10 +0000917 return 0;
918}
919
Paul Crowley749af8c2015-05-28 17:35:06 +0100920static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700921 std::string value = property_get("ro.crypto.type");
922 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100923}
924
Tom Cherryb7349902015-08-26 11:43:36 -0700925static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100926 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000927 return 0;
928 }
Tom Cherry96f67312015-07-30 13:52:55 -0700929 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000930 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000931}
Paul Crowley749af8c2015-05-28 17:35:06 +0100932
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700933static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100934 if (!is_file_crypto()) {
935 return 0;
936 }
Tom Cherry087cd352015-08-03 14:19:35 -0700937 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100938}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700939
Tom Cherryb7349902015-08-26 11:43:36 -0700940BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
941 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
942 static const Map builtin_functions = {
943 {"bootchart_init", {0, 0, do_bootchart_init}},
944 {"chmod", {2, 2, do_chmod}},
945 {"chown", {2, 3, do_chown}},
946 {"class_reset", {1, 1, do_class_reset}},
947 {"class_start", {1, 1, do_class_start}},
948 {"class_stop", {1, 1, do_class_stop}},
949 {"copy", {2, 2, do_copy}},
950 {"domainname", {1, 1, do_domainname}},
951 {"enable", {1, 1, do_enable}},
952 {"exec", {1, kMax, do_exec}},
953 {"export", {2, 2, do_export}},
954 {"hostname", {1, 1, do_hostname}},
955 {"ifup", {1, 1, do_ifup}},
956 {"insmod", {1, kMax, do_insmod}},
957 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700958 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700959 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700960 {"loglevel", {1, 1, do_loglevel}},
961 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800962 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700963 {"mount", {3, kMax, do_mount}},
964 {"powerctl", {1, 1, do_powerctl}},
965 {"restart", {1, 1, do_restart}},
966 {"restorecon", {1, kMax, do_restorecon}},
967 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
968 {"rm", {1, 1, do_rm}},
969 {"rmdir", {1, 1, do_rmdir}},
970 {"setprop", {2, 2, do_setprop}},
971 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700972 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700973 {"start", {1, 1, do_start}},
974 {"stop", {1, 1, do_stop}},
975 {"swapon_all", {1, 1, do_swapon_all}},
976 {"symlink", {2, 2, do_symlink}},
977 {"sysclktz", {1, 1, do_sysclktz}},
978 {"trigger", {1, 1, do_trigger}},
979 {"verity_load_state", {0, 0, do_verity_load_state}},
980 {"verity_update_state", {0, 0, do_verity_update_state}},
981 {"wait", {1, 2, do_wait}},
982 {"write", {2, 2, do_write}},
983 };
984 return builtin_functions;
985}