blob: 98595da0e7653de4b3a919ba920f4ec45b40ba79 [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) {
74 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080076 }
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) {
79 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
80 }
81 close(fd);
82 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070083}
84
Tom Cherryb7349902015-08-26 11:43:36 -070085static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070086 struct ifreq ifr;
87 int s, ret;
88
89 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
90
91 s = socket(AF_INET, SOCK_DGRAM, 0);
92 if (s < 0)
93 return -1;
94
95 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
96 if (ret < 0) {
97 goto done;
98 }
99
100 if (up)
101 ifr.ifr_flags |= IFF_UP;
102 else
103 ifr.ifr_flags &= ~IFF_UP;
104
105 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800106
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107done:
108 close(s);
109 return ret;
110}
111
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700112// Turn off backlight while we are performing power down cleanup activities.
113static void turnOffBacklight() {
114 static const char off[] = "0";
115
116 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
117
118 static const char backlightDir[] = "/sys/class/backlight";
119 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
120 if (!dir) {
121 return;
122 }
123
124 struct dirent *dp;
125 while ((dp = readdir(dir.get())) != NULL) {
126 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
127 (dp->d_name[0] == '.')) {
128 continue;
129 }
130
131 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
132 backlightDir,
133 dp->d_name);
134 android::base::WriteStringToFile(off, fileName);
135 }
136}
137
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 };
204 android_fork_execvp_ext(ARRAY_SIZE(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 };
210 android_fork_execvp_ext(ARRAY_SIZE(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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700385 if (!strncmp(source, "mtd@", 4)) {
386 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000387 if (n < 0) {
388 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700389 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000390
Yabin Cuie2d63af2015-02-17 19:27:51 -0800391 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000392
Colin Crosscd0f1732010-04-19 17:10:24 -0700393 if (wait)
394 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000395 if (mount(tmp, target, system, flags, options) < 0) {
396 return -1;
397 }
398
Ken Sumralldd4d7862011-02-17 18:09:47 -0800399 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000400 } else if (!strncmp(source, "loop@", 5)) {
401 int mode, loop, fd;
402 struct loop_info info;
403
404 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800405 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000406 if (fd < 0) {
407 return -1;
408 }
409
410 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800411 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800412 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100414 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415 return -1;
416 }
417
418 /* if it is a blank loop device */
419 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
420 /* if it becomes our loop device */
421 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
422 close(fd);
423
424 if (mount(tmp, target, system, flags, options) < 0) {
425 ioctl(loop, LOOP_CLR_FD, 0);
426 close(loop);
427 return -1;
428 }
429
430 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800431 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000432 }
433 }
434
435 close(loop);
436 }
437
438 close(fd);
439 ERROR("out of loopback devices");
440 return -1;
441 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700442 if (wait)
443 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000444 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700445 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000446 }
447
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700448 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800449
450exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800451 return 0;
452
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453}
454
Tom Cherryb7349902015-08-26 11:43:36 -0700455static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700456 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800457 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700458 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700459 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
460 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700461 close(fd);
462 } else {
463 ERROR("could not open /cache/recovery/command\n");
464 return -1;
465 }
466 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
467 while (1) { pause(); } // never reached
468}
469
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800470/* Imports .rc files from the specified paths. Default ones are applied if none is given.
471 *
472 * start_index: index of the first path in the args list
473 */
474static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700475 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800476 if (args.size() <= start_index) {
477 // Use the default set if no path is given
478 static const std::vector<std::string> init_directories = {
479 "/system/etc/init",
480 "/vendor/etc/init",
481 "/odm/etc/init"
482 };
483
484 for (const auto& dir : init_directories) {
485 parser.ParseConfig(dir);
486 }
487 } else {
488 for (size_t i = start_index; i < args.size(); ++i) {
489 parser.ParseConfig(args[i]);
490 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700491 }
492}
493
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800494/* mount_all <fstab> [ <path> ]*
495 *
JP Abgrallcee20682014-07-02 14:26:54 -0700496 * This function might request a reboot, in which case it will
497 * not return.
498 */
Tom Cherryb7349902015-08-26 11:43:36 -0700499static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700500 pid_t pid;
501 int ret = -1;
502 int child_ret = -1;
503 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800504 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700505
Tom Cherry96f67312015-07-30 13:52:55 -0700506 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700507 /*
508 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
509 * do the call in the child to provide protection to the main init
510 * process if anything goes wrong (crash or memory leak), and wait for
511 * the child to finish in the parent.
512 */
513 pid = fork();
514 if (pid > 0) {
515 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700516 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
517 if (wp_ret < 0) {
518 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700519 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700520 }
521
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700522 if (WIFEXITED(status)) {
523 ret = WEXITSTATUS(status);
524 } else {
525 ret = -1;
526 }
527 } else if (pid == 0) {
528 /* child, call fs_mgr_mount_all() */
529 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800530 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800531 child_ret = fs_mgr_mount_all(fstab);
532 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700533 if (child_ret == -1) {
534 ERROR("fs_mgr_mount_all returned an error\n");
535 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700536 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700537 } else {
538 /* fork failed, return an error */
539 return -1;
540 }
541
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800542 /* Paths of .rc files are specified at the 2nd argument and beyond */
543 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700544
JP Abgrallf22b7452014-07-02 13:16:04 -0700545 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800546 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700547 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700548 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000549 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800550 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700551 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700552 property_set("ro.crypto.state", "unencrypted");
553 /* If fs_mgr determined this is an unencrypted device, then trigger
554 * that action.
555 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700556 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700557 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
558 /* Setup a wipe via recovery, and reboot into recovery */
559 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
560 ret = wipe_data_via_recovery();
561 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000562 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000563 if (e4crypt_install_keyring()) {
564 return -1;
565 }
566 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000567 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000568
569 // Although encrypted, we have device key, so we do not need to
570 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700571 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000572 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
573 if (e4crypt_install_keyring()) {
574 return -1;
575 }
576 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000577 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000578 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700579 } else if (ret > 0) {
580 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700581 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700582 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700583
584 return ret;
585}
586
Tom Cherryb7349902015-08-26 11:43:36 -0700587static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700588 struct fstab *fstab;
589 int ret;
590
Tom Cherry96f67312015-07-30 13:52:55 -0700591 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700592 ret = fs_mgr_swapon_all(fstab);
593 fs_mgr_free_fstab(fstab);
594
595 return ret;
596}
597
Tom Cherryb7349902015-08-26 11:43:36 -0700598static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700599 const char* name = args[1].c_str();
600 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700601 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700602 return 0;
603}
604
Tom Cherryb7349902015-08-26 11:43:36 -0700605static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700606 struct rlimit limit;
607 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700608 resource = std::stoi(args[1]);
609 limit.rlim_cur = std::stoi(args[2]);
610 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700611 return setrlimit(resource, &limit);
612}
613
Tom Cherryb7349902015-08-26 11:43:36 -0700614static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700615 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
616 if (!svc) {
617 ERROR("do_start: Service %s not found\n", args[1].c_str());
618 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700619 }
Tom Cherrybac32992015-07-31 12:45:25 -0700620 if (!svc->Start())
621 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700622 return 0;
623}
624
Tom Cherryb7349902015-08-26 11:43:36 -0700625static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700626 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
627 if (!svc) {
628 ERROR("do_stop: Service %s not found\n", args[1].c_str());
629 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700630 }
Tom Cherrybac32992015-07-31 12:45:25 -0700631 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700632 return 0;
633}
634
Tom Cherryb7349902015-08-26 11:43:36 -0700635static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700636 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
637 if (!svc) {
638 ERROR("do_restart: Service %s not found\n", args[1].c_str());
639 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640 }
Tom Cherrybac32992015-07-31 12:45:25 -0700641 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700642 return 0;
643}
644
Tom Cherryb7349902015-08-26 11:43:36 -0700645static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700646 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700647 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700648 unsigned int cmd = 0;
649 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700650 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700651
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700652 if (strncmp(command, "shutdown", 8) == 0) {
653 cmd = ANDROID_RB_POWEROFF;
654 len = 8;
655 } else if (strncmp(command, "reboot", 6) == 0) {
656 cmd = ANDROID_RB_RESTART2;
657 len = 6;
658 } else {
659 ERROR("powerctl: unrecognized command '%s'\n", command);
660 return -EINVAL;
661 }
662
663 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700664 if (cmd == ANDROID_RB_POWEROFF &&
665 !strcmp(&command[len + 1], "userrequested")) {
666 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
667 // Run fsck once the file system is remounted in read-only mode.
668 callback_on_ro_remount = unmount_and_fsck;
669 } else if (cmd == ANDROID_RB_RESTART2) {
670 reboot_target = &command[len + 1];
671 }
672 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700673 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
674 return -EINVAL;
675 }
676
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800677 std::string timeout = property_get("ro.build.shutdown_timeout");
678 unsigned int delay = 0;
679
680 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
681 Timer t;
682 // Ask all services to terminate.
683 ServiceManager::GetInstance().ForEachService(
684 [] (Service* s) { s->Terminate(); });
685
686 while (t.duration() < delay) {
687 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
688
689 int service_count = 0;
690 ServiceManager::GetInstance().ForEachService(
691 [&service_count] (Service* s) {
692 // Count the number of services running.
693 // Exclude the console as it will ignore the SIGTERM signal
694 // and not exit.
695 // Note: SVC_CONSOLE actually means "requires console" but
696 // it is only used by the shell.
697 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
698 service_count++;
699 }
700 });
701
702 if (service_count == 0) {
703 // All terminable services terminated. We can exit early.
704 break;
705 }
706
707 // Wait a bit before recounting the number or running services.
708 usleep(kTerminateServiceDelayMicroSeconds);
709 }
710 NOTICE("Terminating running services took %.02f seconds", t.duration());
711 }
712
Yusuke Sato0df08272015-07-08 14:57:07 -0700713 return android_reboot_with_callback(cmd, 0, reboot_target,
714 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700715}
716
Tom Cherryb7349902015-08-26 11:43:36 -0700717static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700718 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700719 return 0;
720}
721
Tom Cherryb7349902015-08-26 11:43:36 -0700722static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700723 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700724}
725
Tom Cherryb7349902015-08-26 11:43:36 -0700726static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700727 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800728}
729
Tom Cherryb7349902015-08-26 11:43:36 -0700730static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700731 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800732}
733
Tom Cherryb7349902015-08-26 11:43:36 -0700734static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800735 struct timezone tz;
736
The Android Open Source Project35237d12008-12-17 18:08:08 -0800737 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700738 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800739 if (settimeofday(NULL, &tz))
740 return -1;
741 return 0;
742}
743
Tom Cherryb7349902015-08-26 11:43:36 -0700744static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700745 int mode = -1;
746 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000747 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700748 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000749 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700750 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000751}
752
Tom Cherryb7349902015-08-26 11:43:36 -0700753static void verity_update_property(fstab_rec *fstab, const char *mount_point,
754 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100755 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
756 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000757}
758
Tom Cherryb7349902015-08-26 11:43:36 -0700759static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700760 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000761}
762
Tom Cherryb7349902015-08-26 11:43:36 -0700763static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700764 const char* path = args[1].c_str();
765 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700766 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700767}
768
Tom Cherryb7349902015-08-26 11:43:36 -0700769static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700770 char *buffer = NULL;
771 int rc = 0;
772 int fd1 = -1, fd2 = -1;
773 struct stat info;
774 int brtw, brtr;
775 char *p;
776
Tom Cherry96f67312015-07-30 13:52:55 -0700777 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700778 return -1;
779
Tom Cherry96f67312015-07-30 13:52:55 -0700780 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700781 goto out_err;
782
Tom Cherry96f67312015-07-30 13:52:55 -0700783 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700784 goto out_err;
785
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800786 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700787 goto out_err;
788
789 p = buffer;
790 brtr = info.st_size;
791 while(brtr) {
792 rc = read(fd1, p, brtr);
793 if (rc < 0)
794 goto out_err;
795 if (rc == 0)
796 break;
797 p += rc;
798 brtr -= rc;
799 }
800
801 p = buffer;
802 brtw = info.st_size;
803 while(brtw) {
804 rc = write(fd2, p, brtw);
805 if (rc < 0)
806 goto out_err;
807 if (rc == 0)
808 break;
809 p += rc;
810 brtw -= rc;
811 }
812
813 rc = 0;
814 goto out;
815out_err:
816 rc = -1;
817out:
818 if (buffer)
819 free(buffer);
820 if (fd1 >= 0)
821 close(fd1);
822 if (fd2 >= 0)
823 close(fd2);
824 return rc;
825}
826
Tom Cherryb7349902015-08-26 11:43:36 -0700827static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700828 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700829 if (args.size() == 3) {
830 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700831 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700832 } else if (args.size() == 4) {
833 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
834 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700835 return -errno;
836 } else {
837 return -1;
838 }
839 return 0;
840}
841
842static mode_t get_mode(const char *s) {
843 mode_t mode = 0;
844 while (*s) {
845 if (*s >= '0' && *s <= '7') {
846 mode = (mode<<3) | (*s-'0');
847 } else {
848 return -1;
849 }
850 s++;
851 }
852 return mode;
853}
854
Tom Cherryb7349902015-08-26 11:43:36 -0700855static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700856 mode_t mode = get_mode(args[1].c_str());
857 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700858 return -errno;
859 }
860 return 0;
861}
862
Tom Cherryb7349902015-08-26 11:43:36 -0700863static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400864 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500865
Tom Cherry96f67312015-07-30 13:52:55 -0700866 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
867 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400868 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500869 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400870 return ret;
871}
872
Tom Cherryb7349902015-08-26 11:43:36 -0700873static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400874 int ret = 0;
875
Tom Cherry96f67312015-07-30 13:52:55 -0700876 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
877 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400878 ret = -errno;
879 }
880 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500881}
882
Tom Cherryb7349902015-08-26 11:43:36 -0700883static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700884 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700885 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
886 ERROR("loglevel: invalid log level'%d'\n", log_level);
887 return -EINVAL;
888 }
889 klog_set_level(log_level);
890 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700891}
892
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700893static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700894 load_persist_props();
895 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800896}
897
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700898static int do_load_system_props(const std::vector<std::string>& args) {
899 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700900 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700901}
902
Tom Cherryb7349902015-08-26 11:43:36 -0700903static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700904 if (args.size() == 2) {
905 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
906 } else if (args.size() == 3) {
907 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800908 } else
909 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700910}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000911
Paul Lawrence806d10b2015-04-28 22:07:10 +0000912/*
913 * Callback to make a directory from the ext4 code
914 */
Tom Cherryb7349902015-08-26 11:43:36 -0700915static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000916 if (make_dir(dir, 0700) && errno != EEXIST) {
917 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000918 }
919
Paul Lawrence806d10b2015-04-28 22:07:10 +0000920 return 0;
921}
922
Paul Crowley749af8c2015-05-28 17:35:06 +0100923static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700924 std::string value = property_get("ro.crypto.type");
925 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100926}
927
Tom Cherryb7349902015-08-26 11:43:36 -0700928static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100929 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000930 return 0;
931 }
Tom Cherry96f67312015-07-30 13:52:55 -0700932 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000933 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000934}
Paul Crowley749af8c2015-05-28 17:35:06 +0100935
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700936static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100937 if (!is_file_crypto()) {
938 return 0;
939 }
Tom Cherry087cd352015-08-03 14:19:35 -0700940 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100941}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700942
Tom Cherryb7349902015-08-26 11:43:36 -0700943BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
944 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
945 static const Map builtin_functions = {
946 {"bootchart_init", {0, 0, do_bootchart_init}},
947 {"chmod", {2, 2, do_chmod}},
948 {"chown", {2, 3, do_chown}},
949 {"class_reset", {1, 1, do_class_reset}},
950 {"class_start", {1, 1, do_class_start}},
951 {"class_stop", {1, 1, do_class_stop}},
952 {"copy", {2, 2, do_copy}},
953 {"domainname", {1, 1, do_domainname}},
954 {"enable", {1, 1, do_enable}},
955 {"exec", {1, kMax, do_exec}},
956 {"export", {2, 2, do_export}},
957 {"hostname", {1, 1, do_hostname}},
958 {"ifup", {1, 1, do_ifup}},
959 {"insmod", {1, kMax, do_insmod}},
960 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700961 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700962 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700963 {"loglevel", {1, 1, do_loglevel}},
964 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800965 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700966 {"mount", {3, kMax, do_mount}},
967 {"powerctl", {1, 1, do_powerctl}},
968 {"restart", {1, 1, do_restart}},
969 {"restorecon", {1, kMax, do_restorecon}},
970 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
971 {"rm", {1, 1, do_rm}},
972 {"rmdir", {1, 1, do_rmdir}},
973 {"setprop", {2, 2, do_setprop}},
974 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700975 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700976 {"start", {1, 1, do_start}},
977 {"stop", {1, 1, do_stop}},
978 {"swapon_all", {1, 1, do_swapon_all}},
979 {"symlink", {2, 2, do_symlink}},
980 {"sysclktz", {1, 1, do_sysclktz}},
981 {"trigger", {1, 1, do_trigger}},
982 {"verity_load_state", {0, 0, do_verity_load_state}},
983 {"verity_update_state", {0, 0, do_verity_update_state}},
984 {"wait", {1, 2, do_wait}},
985 {"write", {2, 2, do_write}},
986 };
987 return builtin_functions;
988}