blob: d64c3d2658550743124facfca4e3d45ce3138c8b [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 pid_t pid;
486 int ret = -1;
487 int child_ret = -1;
488 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800489 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700490
Tom Cherry96f67312015-07-30 13:52:55 -0700491 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700492 /*
493 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
494 * do the call in the child to provide protection to the main init
495 * process if anything goes wrong (crash or memory leak), and wait for
496 * the child to finish in the parent.
497 */
498 pid = fork();
499 if (pid > 0) {
500 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700501 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700502 if (wp_ret == -1) {
503 // Unexpected error code. We will continue anyway.
504 PLOG(WARNING) << "waitpid failed";
Paul Lawrence40af0922014-09-16 14:31:23 -0700505 }
506
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700507 if (WIFEXITED(status)) {
508 ret = WEXITSTATUS(status);
509 } else {
510 ret = -1;
511 }
512 } else if (pid == 0) {
513 /* child, call fs_mgr_mount_all() */
514 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800515 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800516 child_ret = fs_mgr_mount_all(fstab);
517 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700518 if (child_ret == -1) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700519 PLOG(ERROR) << "fs_mgr_mount_all returned an error";
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700520 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700521 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700522 } else {
523 /* fork failed, return an error */
524 return -1;
525 }
526
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800527 /* Paths of .rc files are specified at the 2nd argument and beyond */
528 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700529
JP Abgrallf22b7452014-07-02 13:16:04 -0700530 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800531 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700532 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700533 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000534 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800535 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700536 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700537 property_set("ro.crypto.state", "unencrypted");
538 /* If fs_mgr determined this is an unencrypted device, then trigger
539 * that action.
540 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700541 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700542 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
543 /* Setup a wipe via recovery, and reboot into recovery */
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700544 PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery.";
JP Abgrallcee20682014-07-02 14:26:54 -0700545 ret = wipe_data_via_recovery();
546 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000547 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000548 if (e4crypt_install_keyring()) {
549 return -1;
550 }
551 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000552 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000553
554 // Although encrypted, we have device key, so we do not need to
555 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700556 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000557 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
558 if (e4crypt_install_keyring()) {
559 return -1;
560 }
561 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000562 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000563 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700564 } else if (ret > 0) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700565 PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << ret;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700566 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700567 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700568
569 return ret;
570}
571
Tom Cherryb7349902015-08-26 11:43:36 -0700572static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700573 struct fstab *fstab;
574 int ret;
575
Tom Cherry96f67312015-07-30 13:52:55 -0700576 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700577 ret = fs_mgr_swapon_all(fstab);
578 fs_mgr_free_fstab(fstab);
579
580 return ret;
581}
582
Tom Cherryb7349902015-08-26 11:43:36 -0700583static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700584 const char* name = args[1].c_str();
585 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700586 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700587 return 0;
588}
589
Tom Cherryb7349902015-08-26 11:43:36 -0700590static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700591 struct rlimit limit;
592 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700593 resource = std::stoi(args[1]);
594 limit.rlim_cur = std::stoi(args[2]);
595 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700596 return setrlimit(resource, &limit);
597}
598
Tom Cherryb7349902015-08-26 11:43:36 -0700599static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700600 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
601 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700602 LOG(ERROR) << "do_start: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700603 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700604 }
Tom Cherrybac32992015-07-31 12:45:25 -0700605 if (!svc->Start())
606 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700607 return 0;
608}
609
Tom Cherryb7349902015-08-26 11:43:36 -0700610static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700611 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
612 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700613 LOG(ERROR) << "do_stop: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700614 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700615 }
Tom Cherrybac32992015-07-31 12:45:25 -0700616 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 return 0;
618}
619
Tom Cherryb7349902015-08-26 11:43:36 -0700620static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700621 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
622 if (!svc) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700623 LOG(ERROR) << "do_restart: Service " << args[1] << " not found";
Tom Cherrybac32992015-07-31 12:45:25 -0700624 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700625 }
Tom Cherrybac32992015-07-31 12:45:25 -0700626 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700627 return 0;
628}
629
Tom Cherryb7349902015-08-26 11:43:36 -0700630static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700631 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700632 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700633 unsigned int cmd = 0;
634 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700635 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700636
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700637 if (strncmp(command, "shutdown", 8) == 0) {
638 cmd = ANDROID_RB_POWEROFF;
639 len = 8;
640 } else if (strncmp(command, "reboot", 6) == 0) {
641 cmd = ANDROID_RB_RESTART2;
642 len = 6;
643 } else {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700644 LOG(ERROR) << "powerctl: unrecognized command '" << command << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700645 return -EINVAL;
646 }
647
648 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700649 if (cmd == ANDROID_RB_POWEROFF &&
650 !strcmp(&command[len + 1], "userrequested")) {
651 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
652 // Run fsck once the file system is remounted in read-only mode.
653 callback_on_ro_remount = unmount_and_fsck;
654 } else if (cmd == ANDROID_RB_RESTART2) {
655 reboot_target = &command[len + 1];
656 }
657 } else if (command[len] != '\0') {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700658 LOG(ERROR) << "powerctl: unrecognized reboot target '" << &command[len] << "'";
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700659 return -EINVAL;
660 }
661
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800662 std::string timeout = property_get("ro.build.shutdown_timeout");
663 unsigned int delay = 0;
664
665 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
666 Timer t;
667 // Ask all services to terminate.
668 ServiceManager::GetInstance().ForEachService(
669 [] (Service* s) { s->Terminate(); });
670
671 while (t.duration() < delay) {
672 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
673
674 int service_count = 0;
675 ServiceManager::GetInstance().ForEachService(
676 [&service_count] (Service* s) {
677 // Count the number of services running.
678 // Exclude the console as it will ignore the SIGTERM signal
679 // and not exit.
680 // Note: SVC_CONSOLE actually means "requires console" but
681 // it is only used by the shell.
682 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
683 service_count++;
684 }
685 });
686
687 if (service_count == 0) {
688 // All terminable services terminated. We can exit early.
689 break;
690 }
691
692 // Wait a bit before recounting the number or running services.
693 usleep(kTerminateServiceDelayMicroSeconds);
694 }
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700695 LOG(VERBOSE) << "Terminating running services took " << t.duration() << " seconds";
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800696 }
697
Yusuke Sato0df08272015-07-08 14:57:07 -0700698 return android_reboot_with_callback(cmd, 0, reboot_target,
699 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700700}
701
Tom Cherryb7349902015-08-26 11:43:36 -0700702static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700703 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700704 return 0;
705}
706
Tom Cherryb7349902015-08-26 11:43:36 -0700707static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700708 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700709}
710
Tom Cherryb7349902015-08-26 11:43:36 -0700711static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700712 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800713}
714
Tom Cherryb7349902015-08-26 11:43:36 -0700715static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700716 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800717}
718
Tom Cherryb7349902015-08-26 11:43:36 -0700719static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800720 struct timezone tz;
721
The Android Open Source Project35237d12008-12-17 18:08:08 -0800722 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700723 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800724 if (settimeofday(NULL, &tz))
725 return -1;
726 return 0;
727}
728
Tom Cherryb7349902015-08-26 11:43:36 -0700729static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700730 int mode = -1;
731 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000732 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700733 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000734 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700735 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000736}
737
Tom Cherryb7349902015-08-26 11:43:36 -0700738static void verity_update_property(fstab_rec *fstab, const char *mount_point,
739 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100740 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
741 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000742}
743
Tom Cherryb7349902015-08-26 11:43:36 -0700744static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700745 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000746}
747
Tom Cherryb7349902015-08-26 11:43:36 -0700748static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700749 const char* path = args[1].c_str();
750 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700751 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700752}
753
Tom Cherryb7349902015-08-26 11:43:36 -0700754static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700755 char *buffer = NULL;
756 int rc = 0;
757 int fd1 = -1, fd2 = -1;
758 struct stat info;
759 int brtw, brtr;
760 char *p;
761
Tom Cherry96f67312015-07-30 13:52:55 -0700762 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700763 return -1;
764
Tom Cherry96f67312015-07-30 13:52:55 -0700765 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700766 goto out_err;
767
Tom Cherry96f67312015-07-30 13:52:55 -0700768 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700769 goto out_err;
770
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800771 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700772 goto out_err;
773
774 p = buffer;
775 brtr = info.st_size;
776 while(brtr) {
777 rc = read(fd1, p, brtr);
778 if (rc < 0)
779 goto out_err;
780 if (rc == 0)
781 break;
782 p += rc;
783 brtr -= rc;
784 }
785
786 p = buffer;
787 brtw = info.st_size;
788 while(brtw) {
789 rc = write(fd2, p, brtw);
790 if (rc < 0)
791 goto out_err;
792 if (rc == 0)
793 break;
794 p += rc;
795 brtw -= rc;
796 }
797
798 rc = 0;
799 goto out;
800out_err:
801 rc = -1;
802out:
803 if (buffer)
804 free(buffer);
805 if (fd1 >= 0)
806 close(fd1);
807 if (fd2 >= 0)
808 close(fd2);
809 return rc;
810}
811
Tom Cherryb7349902015-08-26 11:43:36 -0700812static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700813 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700814 if (args.size() == 3) {
815 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700817 } else if (args.size() == 4) {
818 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
819 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700820 return -errno;
821 } else {
822 return -1;
823 }
824 return 0;
825}
826
827static mode_t get_mode(const char *s) {
828 mode_t mode = 0;
829 while (*s) {
830 if (*s >= '0' && *s <= '7') {
831 mode = (mode<<3) | (*s-'0');
832 } else {
833 return -1;
834 }
835 s++;
836 }
837 return mode;
838}
839
Tom Cherryb7349902015-08-26 11:43:36 -0700840static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700841 mode_t mode = get_mode(args[1].c_str());
842 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700843 return -errno;
844 }
845 return 0;
846}
847
Tom Cherryb7349902015-08-26 11:43:36 -0700848static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400849 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500850
Tom Cherry96f67312015-07-30 13:52:55 -0700851 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
852 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400853 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500854 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400855 return ret;
856}
857
Tom Cherryb7349902015-08-26 11:43:36 -0700858static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400859 int ret = 0;
860
Tom Cherry96f67312015-07-30 13:52:55 -0700861 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
862 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400863 ret = -errno;
864 }
865 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500866}
867
Tom Cherryb7349902015-08-26 11:43:36 -0700868static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700869 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700870 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700871 LOG(ERROR) << "loglevel: invalid log level " << log_level;
Riley Andrews1bbef882014-06-26 13:55:03 -0700872 return -EINVAL;
873 }
874 klog_set_level(log_level);
875 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700876}
877
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700878static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700879 load_persist_props();
880 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800881}
882
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700883static int do_load_system_props(const std::vector<std::string>& args) {
884 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700885 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700886}
887
Tom Cherryb7349902015-08-26 11:43:36 -0700888static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700889 if (args.size() == 2) {
890 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
891 } else if (args.size() == 3) {
892 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800893 } else
894 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700895}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000896
Paul Lawrence806d10b2015-04-28 22:07:10 +0000897/*
898 * Callback to make a directory from the ext4 code
899 */
Tom Cherryb7349902015-08-26 11:43:36 -0700900static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000901 if (make_dir(dir, 0700) && errno != EEXIST) {
902 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000903 }
904
Paul Lawrence806d10b2015-04-28 22:07:10 +0000905 return 0;
906}
907
Paul Crowley749af8c2015-05-28 17:35:06 +0100908static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700909 std::string value = property_get("ro.crypto.type");
910 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100911}
912
Tom Cherryb7349902015-08-26 11:43:36 -0700913static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100914 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000915 return 0;
916 }
Tom Cherry96f67312015-07-30 13:52:55 -0700917 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000918 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000919}
Paul Crowley749af8c2015-05-28 17:35:06 +0100920
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700921static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100922 if (!is_file_crypto()) {
923 return 0;
924 }
Tom Cherry087cd352015-08-03 14:19:35 -0700925 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100926}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700927
Tom Cherryb7349902015-08-26 11:43:36 -0700928BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
929 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
930 static const Map builtin_functions = {
931 {"bootchart_init", {0, 0, do_bootchart_init}},
932 {"chmod", {2, 2, do_chmod}},
933 {"chown", {2, 3, do_chown}},
934 {"class_reset", {1, 1, do_class_reset}},
935 {"class_start", {1, 1, do_class_start}},
936 {"class_stop", {1, 1, do_class_stop}},
937 {"copy", {2, 2, do_copy}},
938 {"domainname", {1, 1, do_domainname}},
939 {"enable", {1, 1, do_enable}},
940 {"exec", {1, kMax, do_exec}},
941 {"export", {2, 2, do_export}},
942 {"hostname", {1, 1, do_hostname}},
943 {"ifup", {1, 1, do_ifup}},
944 {"insmod", {1, kMax, do_insmod}},
945 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700946 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700947 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700948 {"loglevel", {1, 1, do_loglevel}},
949 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800950 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700951 {"mount", {3, kMax, do_mount}},
952 {"powerctl", {1, 1, do_powerctl}},
953 {"restart", {1, 1, do_restart}},
954 {"restorecon", {1, kMax, do_restorecon}},
955 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
956 {"rm", {1, 1, do_rm}},
957 {"rmdir", {1, 1, do_rmdir}},
958 {"setprop", {2, 2, do_setprop}},
959 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700960 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700961 {"start", {1, 1, do_start}},
962 {"stop", {1, 1, do_stop}},
963 {"swapon_all", {1, 1, do_swapon_all}},
964 {"symlink", {2, 2, do_symlink}},
965 {"sysclktz", {1, 1, do_sysclktz}},
966 {"trigger", {1, 1, do_trigger}},
967 {"verity_load_state", {0, 0, do_verity_load_state}},
968 {"verity_update_state", {0, 0, do_verity_update_state}},
969 {"wait", {1, 2, do_wait}},
970 {"write", {2, 2, do_write}},
971 };
972 return builtin_functions;
973}