blob: 56318771ff9432a412b2c2a6cececf94bee88dd0 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tom Cherryb7349902015-08-26 11:43:36 -070017#include "builtins.h"
18
Mark Salyzyn402fb792016-04-05 13:43:40 -070019#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070021#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070022#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070023#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070024#include <signal.h>
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -070025#include <sched.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070028#include <string.h>
29#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/mount.h>
31#include <sys/resource.h>
Nick Kralevichf2049162016-03-27 16:55:59 -070032#include <sys/syscall.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080033#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070034#include <sys/types.h>
35#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070036#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070037#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000038#include <linux/loop.h>
Paul Crowleyaf8be582016-05-10 08:52:06 -070039#include <ext4_crypt.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000040#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Stephen Smalleye46f9d52012-01-13 08:48:47 -050042#include <selinux/selinux.h>
43#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050044
Elliott Hughesdb3f2672015-03-20 09:45:18 -070045#include <fs_mgr.h>
Mark Salyzyn402fb792016-04-05 13:43:40 -070046#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080047#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080048#include <android-base/stringprintf.h>
Yabin Cui0b1252c2016-06-24 18:28:03 -070049#include <bootloader_message/bootloader_message.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070050#include <cutils/partition_utils.h>
51#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070052#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070053#include <private/android_filesystem_config.h>
54
Tom Cherryfa0c21c2015-07-23 17:53:11 -070055#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070056#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070057#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070058#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070059#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070060#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "property_service.h"
62#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080063#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070064#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070065
Nick Kralevichbc609542015-01-31 21:39:46 -080066#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070067#define UNMOUNT_CHECK_MS 5000
68#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080069
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080070static const int kTerminateServiceDelayMicroSeconds = 50000;
71
Tom Cherryb7349902015-08-26 11:43:36 -070072static int insmod(const char *filename, const char *options) {
Nick Kralevichf2049162016-03-27 16:55:59 -070073 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
74 if (fd == -1) {
75 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070076 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080077 }
Nick Kralevichf2049162016-03-27 16:55:59 -070078 int rc = syscall(__NR_finit_module, fd, options, 0);
79 if (rc == -1) {
80 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
81 }
82 close(fd);
83 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084}
85
Tom Cherryb7349902015-08-26 11:43:36 -070086static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070087 struct ifreq ifr;
88 int s, ret;
89
90 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
91
92 s = socket(AF_INET, SOCK_DGRAM, 0);
93 if (s < 0)
94 return -1;
95
96 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
97 if (ret < 0) {
98 goto done;
99 }
100
101 if (up)
102 ifr.ifr_flags |= IFF_UP;
103 else
104 ifr.ifr_flags &= ~IFF_UP;
105
106 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800107
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700108done:
109 close(s);
110 return ret;
111}
112
Mark Salyzyn402fb792016-04-05 13:43:40 -0700113// Turn off backlight while we are performing power down cleanup activities.
114static void turnOffBacklight() {
115 static const char off[] = "0";
116
117 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
118
119 static const char backlightDir[] = "/sys/class/backlight";
120 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
121 if (!dir) {
122 return;
123 }
124
125 struct dirent *dp;
126 while ((dp = readdir(dir.get())) != NULL) {
127 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
128 (dp->d_name[0] == '.')) {
129 continue;
130 }
131
132 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
133 backlightDir,
134 dp->d_name);
135 android::base::WriteStringToFile(off, fileName);
136 }
137}
138
Paul Crowleyaf8be582016-05-10 08:52:06 -0700139static int wipe_data_via_recovery(const std::string& reason) {
140 const std::vector<std::string> options = {"--wipe_data", std::string() + "--reason=" + reason};
141 std::string err;
142 if (!write_bootloader_message(options, &err)) {
143 ERROR("failed to set bootloader message: %s", err.c_str());
144 return -1;
145 }
146 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
147 while (1) { pause(); } // never reached
148}
149
Tom Cherryb7349902015-08-26 11:43:36 -0700150static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700151 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
152 return;
153
154 /* First, lazily unmount the directory. This unmount request finishes when
155 * all processes that open a file or directory in |entry->mnt_dir| exit.
156 */
157 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
158
159 /* Next, kill all processes except init, kthreadd, and kthreadd's
160 * children to finish the lazy unmount. Killing all processes here is okay
161 * because this callback function is only called right before reboot().
162 * It might be cleaner to selectively kill processes that actually use
163 * |entry->mnt_dir| rather than killing all, probably by reusing a function
164 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
165 * not allow init to scan /proc/<pid> files which the utility function
166 * heavily relies on. The policy does not allow the process to execute
167 * killall/pkill binaries either. Note that some processes might
168 * automatically restart after kill(), but that is not really a problem
169 * because |entry->mnt_dir| is no longer visible to such new processes.
170 */
Tom Cherrybac32992015-07-31 12:45:25 -0700171 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700172 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
173
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700174 // Restart Watchdogd to allow us to complete umounting and fsck
175 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
176 if (svc) {
177 do {
178 sched_yield(); // do not be so eager, let cleanup have priority
179 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
180 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
181 svc->Start();
182 }
183
Mark Salyzyn402fb792016-04-05 13:43:40 -0700184 turnOffBacklight();
185
Yusuke Sato0df08272015-07-08 14:57:07 -0700186 int count = 0;
187 while (count++ < UNMOUNT_CHECK_TIMES) {
188 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
189 if (fd >= 0) {
190 /* |entry->mnt_dir| has sucessfully been unmounted. */
191 close(fd);
192 break;
193 } else if (errno == EBUSY) {
194 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
195 * while then retry.
196 */
197 TEMP_FAILURE_RETRY(
198 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
199 continue;
200 } else {
201 /* Cannot open the device. Give up. */
202 return;
203 }
204 }
205
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700206 // NB: With watchdog still running, there is no cap on the time it takes
207 // to complete the fsck, from the users perspective the device graphics
208 // and responses are locked-up and they may choose to hold the power
209 // button in frustration if it drags out.
210
Yusuke Sato0df08272015-07-08 14:57:07 -0700211 int st;
212 if (!strcmp(entry->mnt_type, "f2fs")) {
213 const char *f2fs_argv[] = {
214 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
215 };
216 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700217 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700218 } else if (!strcmp(entry->mnt_type, "ext4")) {
219 const char *ext4_argv[] = {
220 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
221 };
222 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700223 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700224 }
225}
226
Tom Cherryb7349902015-08-26 11:43:36 -0700227static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700228 /* Starting a class does not start services
229 * which are explicitly disabled. They must
230 * be started individually.
231 */
Tom Cherrybac32992015-07-31 12:45:25 -0700232 ServiceManager::GetInstance().
233 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234 return 0;
235}
236
Tom Cherryb7349902015-08-26 11:43:36 -0700237static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700238 ServiceManager::GetInstance().
239 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700240 return 0;
241}
242
Tom Cherryb7349902015-08-26 11:43:36 -0700243static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700244 ServiceManager::GetInstance().
245 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800246 return 0;
247}
248
Tom Cherryb7349902015-08-26 11:43:36 -0700249static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700250 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700251}
252
Tom Cherryb7349902015-08-26 11:43:36 -0700253static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700254 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
255 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700256 return -1;
257 }
Tom Cherrybac32992015-07-31 12:45:25 -0700258 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700259}
260
Tom Cherryb7349902015-08-26 11:43:36 -0700261static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700262 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
263 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800264 return -1;
265 }
Tom Cherrybac32992015-07-31 12:45:25 -0700266 if (!svc->Start()) {
267 return -1;
268 }
269 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800270 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700271}
272
Tom Cherryb7349902015-08-26 11:43:36 -0700273static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700274 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275}
276
Tom Cherryb7349902015-08-26 11:43:36 -0700277static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700278 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279}
280
Tom Cherryb7349902015-08-26 11:43:36 -0700281static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700282 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700283}
284
Tom Cherryb7349902015-08-26 11:43:36 -0700285static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700286 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800287
Tom Cherry96f67312015-07-30 13:52:55 -0700288 if (args.size() > 2) {
289 options += args[2];
290 for (std::size_t i = 3; i < args.size(); ++i) {
291 options += ' ';
292 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800293 }
294 }
295
Tom Cherry96f67312015-07-30 13:52:55 -0700296 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800297}
298
Tom Cherryb7349902015-08-26 11:43:36 -0700299static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700300 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700301 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
303 /* mkdir <path> [mode] [owner] [group] */
304
Tom Cherry96f67312015-07-30 13:52:55 -0700305 if (args.size() >= 3) {
306 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 }
308
Tom Cherry96f67312015-07-30 13:52:55 -0700309 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700310 /* chmod in case the directory already exists */
311 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700312 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700313 }
314 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 return -errno;
316 }
317
Tom Cherry96f67312015-07-30 13:52:55 -0700318 if (args.size() >= 4) {
319 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 gid_t gid = -1;
321
Tom Cherry96f67312015-07-30 13:52:55 -0700322 if (args.size() == 5) {
323 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
325
Tom Cherry96f67312015-07-30 13:52:55 -0700326 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327 return -errno;
328 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700329
330 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
331 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700332 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700333 if (ret == -1) {
334 return -errno;
335 }
336 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700337 }
338
Paul Crowleyaf8be582016-05-10 08:52:06 -0700339 if (e4crypt_is_native()) {
340 if (e4crypt_set_directory_policy(args[1].c_str())) {
341 wipe_data_via_recovery(std::string() + "set_policy_failed:" + args[1]);
342 return -1;
343 }
344 }
345 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346}
347
Alex Light68ab20f2016-06-23 11:11:39 -0700348/* umount <path> */
349static int do_umount(const std::vector<std::string>& args) {
350 return umount(args[1].c_str());
351}
352
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353static struct {
354 const char *name;
355 unsigned flag;
356} mount_flags[] = {
357 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200358 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 { "nosuid", MS_NOSUID },
360 { "nodev", MS_NODEV },
361 { "nodiratime", MS_NODIRATIME },
362 { "ro", MS_RDONLY },
363 { "rw", 0 },
364 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700365 { "bind", MS_BIND },
366 { "rec", MS_REC },
367 { "unbindable", MS_UNBINDABLE },
368 { "private", MS_PRIVATE },
369 { "slave", MS_SLAVE },
370 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700371 { "defaults", 0 },
372 { 0, 0 },
373};
374
Ken Sumrall752923c2010-12-03 16:33:31 -0800375#define DATA_MNT_POINT "/data"
376
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700378static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700379 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700380 const char *source, *target, *system;
381 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700382 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700383 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700385 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700386
Tom Cherry96f67312015-07-30 13:52:55 -0700387 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700389 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700390 flags |= mount_flags[i].flag;
391 break;
392 }
393 }
394
Colin Crosscd0f1732010-04-19 17:10:24 -0700395 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700396 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700397 wait = 1;
398 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700399 else if (na + 1 == args.size())
400 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700401 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 }
403
Tom Cherry96f67312015-07-30 13:52:55 -0700404 system = args[1].c_str();
405 source = args[2].c_str();
406 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000407
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408 if (!strncmp(source, "mtd@", 4)) {
409 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000410 if (n < 0) {
411 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413
Yabin Cuie2d63af2015-02-17 19:27:51 -0800414 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000415
Colin Crosscd0f1732010-04-19 17:10:24 -0700416 if (wait)
417 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000418 if (mount(tmp, target, system, flags, options) < 0) {
419 return -1;
420 }
421
Ken Sumralldd4d7862011-02-17 18:09:47 -0800422 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000423 } else if (!strncmp(source, "loop@", 5)) {
424 int mode, loop, fd;
425 struct loop_info info;
426
427 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800428 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000429 if (fd < 0) {
430 return -1;
431 }
432
433 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800434 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800435 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000436 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100437 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000438 return -1;
439 }
440
441 /* if it is a blank loop device */
442 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
443 /* if it becomes our loop device */
444 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
445 close(fd);
446
447 if (mount(tmp, target, system, flags, options) < 0) {
448 ioctl(loop, LOOP_CLR_FD, 0);
449 close(loop);
450 return -1;
451 }
452
453 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800454 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000455 }
456 }
457
458 close(loop);
459 }
460
461 close(fd);
462 ERROR("out of loopback devices");
463 return -1;
464 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700465 if (wait)
466 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000467 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700468 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000469 }
470
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800472
473exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800474 return 0;
475
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476}
477
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800478/* Imports .rc files from the specified paths. Default ones are applied if none is given.
479 *
480 * start_index: index of the first path in the args list
481 */
482static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700483 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800484 if (args.size() <= start_index) {
485 // Use the default set if no path is given
486 static const std::vector<std::string> init_directories = {
487 "/system/etc/init",
488 "/vendor/etc/init",
489 "/odm/etc/init"
490 };
491
492 for (const auto& dir : init_directories) {
493 parser.ParseConfig(dir);
494 }
495 } else {
496 for (size_t i = start_index; i < args.size(); ++i) {
497 parser.ParseConfig(args[i]);
498 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700499 }
500}
501
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800502/* mount_all <fstab> [ <path> ]*
503 *
JP Abgrallcee20682014-07-02 14:26:54 -0700504 * This function might request a reboot, in which case it will
505 * not return.
506 */
Tom Cherryb7349902015-08-26 11:43:36 -0700507static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700508 pid_t pid;
509 int ret = -1;
510 int child_ret = -1;
511 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800512 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700513
Tom Cherry96f67312015-07-30 13:52:55 -0700514 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700515 /*
516 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
517 * do the call in the child to provide protection to the main init
518 * process if anything goes wrong (crash or memory leak), and wait for
519 * the child to finish in the parent.
520 */
521 pid = fork();
522 if (pid > 0) {
523 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700524 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
525 if (wp_ret < 0) {
526 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700527 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700528 }
529
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700530 if (WIFEXITED(status)) {
531 ret = WEXITSTATUS(status);
532 } else {
533 ret = -1;
534 }
535 } else if (pid == 0) {
536 /* child, call fs_mgr_mount_all() */
537 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800538 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800539 child_ret = fs_mgr_mount_all(fstab);
540 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700541 if (child_ret == -1) {
542 ERROR("fs_mgr_mount_all returned an error\n");
543 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700544 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700545 } else {
546 /* fork failed, return an error */
547 return -1;
548 }
549
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800550 /* Paths of .rc files are specified at the 2nd argument and beyond */
551 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700552
JP Abgrallf22b7452014-07-02 13:16:04 -0700553 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence1f992182016-04-18 15:37:31 -0700554 ActionManager::GetInstance().QueueEventTrigger("encrypt");
JP Abgrallf22b7452014-07-02 13:16:04 -0700555 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700556 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000557 property_set("ro.crypto.type", "block");
Paul Lawrence1f992182016-04-18 15:37:31 -0700558 ActionManager::GetInstance().QueueEventTrigger("defaultcrypto");
JP Abgrallf22b7452014-07-02 13:16:04 -0700559 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700560 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800561 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
562 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
563 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700564 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700565 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
566 /* Setup a wipe via recovery, and reboot into recovery */
567 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
Paul Crowleyaf8be582016-05-10 08:52:06 -0700568 ret = wipe_data_via_recovery("wipe_data_via_recovery");
JP Abgrallcee20682014-07-02 14:26:54 -0700569 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800570 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000571 if (e4crypt_install_keyring()) {
572 return -1;
573 }
574 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000575 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000576
577 // Although encrypted, we have device key, so we do not need to
578 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700579 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700580 } else if (ret > 0) {
581 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700582 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700583 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700584
585 return ret;
586}
587
Tom Cherryb7349902015-08-26 11:43:36 -0700588static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700589 struct fstab *fstab;
590 int ret;
591
Tom Cherry96f67312015-07-30 13:52:55 -0700592 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700593 ret = fs_mgr_swapon_all(fstab);
594 fs_mgr_free_fstab(fstab);
595
596 return ret;
597}
598
Tom Cherryb7349902015-08-26 11:43:36 -0700599static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700600 const char* name = args[1].c_str();
601 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700602 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700603 return 0;
604}
605
Tom Cherryb7349902015-08-26 11:43:36 -0700606static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700607 struct rlimit limit;
608 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700609 resource = std::stoi(args[1]);
610 limit.rlim_cur = std::stoi(args[2]);
611 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700612 return setrlimit(resource, &limit);
613}
614
Tom Cherryb7349902015-08-26 11:43:36 -0700615static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700616 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
617 if (!svc) {
618 ERROR("do_start: Service %s not found\n", args[1].c_str());
619 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620 }
Tom Cherrybac32992015-07-31 12:45:25 -0700621 if (!svc->Start())
622 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700623 return 0;
624}
625
Tom Cherryb7349902015-08-26 11:43:36 -0700626static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700627 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
628 if (!svc) {
629 ERROR("do_stop: Service %s not found\n", args[1].c_str());
630 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700631 }
Tom Cherrybac32992015-07-31 12:45:25 -0700632 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700633 return 0;
634}
635
Tom Cherryb7349902015-08-26 11:43:36 -0700636static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700637 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
638 if (!svc) {
639 ERROR("do_restart: Service %s not found\n", args[1].c_str());
640 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641 }
Tom Cherrybac32992015-07-31 12:45:25 -0700642 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700643 return 0;
644}
645
Tom Cherryb7349902015-08-26 11:43:36 -0700646static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700647 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700648 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700649 unsigned int cmd = 0;
650 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700651 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700652
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700653 if (strncmp(command, "shutdown", 8) == 0) {
654 cmd = ANDROID_RB_POWEROFF;
655 len = 8;
656 } else if (strncmp(command, "reboot", 6) == 0) {
657 cmd = ANDROID_RB_RESTART2;
658 len = 6;
659 } else {
660 ERROR("powerctl: unrecognized command '%s'\n", command);
661 return -EINVAL;
662 }
663
664 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700665 if (cmd == ANDROID_RB_POWEROFF &&
666 !strcmp(&command[len + 1], "userrequested")) {
667 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
668 // Run fsck once the file system is remounted in read-only mode.
669 callback_on_ro_remount = unmount_and_fsck;
670 } else if (cmd == ANDROID_RB_RESTART2) {
671 reboot_target = &command[len + 1];
672 }
673 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700674 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
675 return -EINVAL;
676 }
677
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800678 std::string timeout = property_get("ro.build.shutdown_timeout");
679 unsigned int delay = 0;
680
681 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
682 Timer t;
683 // Ask all services to terminate.
684 ServiceManager::GetInstance().ForEachService(
685 [] (Service* s) { s->Terminate(); });
686
687 while (t.duration() < delay) {
688 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
689
690 int service_count = 0;
691 ServiceManager::GetInstance().ForEachService(
692 [&service_count] (Service* s) {
693 // Count the number of services running.
694 // Exclude the console as it will ignore the SIGTERM signal
695 // and not exit.
696 // Note: SVC_CONSOLE actually means "requires console" but
697 // it is only used by the shell.
698 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
699 service_count++;
700 }
701 });
702
703 if (service_count == 0) {
704 // All terminable services terminated. We can exit early.
705 break;
706 }
707
708 // Wait a bit before recounting the number or running services.
709 usleep(kTerminateServiceDelayMicroSeconds);
710 }
711 NOTICE("Terminating running services took %.02f seconds", t.duration());
712 }
713
Yusuke Sato0df08272015-07-08 14:57:07 -0700714 return android_reboot_with_callback(cmd, 0, reboot_target,
715 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700716}
717
Tom Cherryb7349902015-08-26 11:43:36 -0700718static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700719 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720 return 0;
721}
722
Tom Cherryb7349902015-08-26 11:43:36 -0700723static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700724 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700725}
726
Tom Cherryb7349902015-08-26 11:43:36 -0700727static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700728 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800729}
730
Tom Cherryb7349902015-08-26 11:43:36 -0700731static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700732 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800733}
734
Tom Cherryb7349902015-08-26 11:43:36 -0700735static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800736 struct timezone tz;
737
The Android Open Source Project35237d12008-12-17 18:08:08 -0800738 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700739 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800740 if (settimeofday(NULL, &tz))
741 return -1;
742 return 0;
743}
744
Tom Cherryb7349902015-08-26 11:43:36 -0700745static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700746 int mode = -1;
747 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000748 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700749 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000750 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700751 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000752}
753
Tom Cherryb7349902015-08-26 11:43:36 -0700754static void verity_update_property(fstab_rec *fstab, const char *mount_point,
755 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100756 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
757 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000758}
759
Tom Cherryb7349902015-08-26 11:43:36 -0700760static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700761 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000762}
763
Tom Cherryb7349902015-08-26 11:43:36 -0700764static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700765 const char* path = args[1].c_str();
766 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700767 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768}
769
Tom Cherryb7349902015-08-26 11:43:36 -0700770static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700771 char *buffer = NULL;
772 int rc = 0;
773 int fd1 = -1, fd2 = -1;
774 struct stat info;
775 int brtw, brtr;
776 char *p;
777
Tom Cherry96f67312015-07-30 13:52:55 -0700778 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700779 return -1;
780
Tom Cherry96f67312015-07-30 13:52:55 -0700781 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700782 goto out_err;
783
Tom Cherry96f67312015-07-30 13:52:55 -0700784 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700785 goto out_err;
786
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800787 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700788 goto out_err;
789
790 p = buffer;
791 brtr = info.st_size;
792 while(brtr) {
793 rc = read(fd1, p, brtr);
794 if (rc < 0)
795 goto out_err;
796 if (rc == 0)
797 break;
798 p += rc;
799 brtr -= rc;
800 }
801
802 p = buffer;
803 brtw = info.st_size;
804 while(brtw) {
805 rc = write(fd2, p, brtw);
806 if (rc < 0)
807 goto out_err;
808 if (rc == 0)
809 break;
810 p += rc;
811 brtw -= rc;
812 }
813
814 rc = 0;
815 goto out;
816out_err:
817 rc = -1;
818out:
819 if (buffer)
820 free(buffer);
821 if (fd1 >= 0)
822 close(fd1);
823 if (fd2 >= 0)
824 close(fd2);
825 return rc;
826}
827
Tom Cherryb7349902015-08-26 11:43:36 -0700828static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700830 if (args.size() == 3) {
831 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700832 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700833 } else if (args.size() == 4) {
834 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
835 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700836 return -errno;
837 } else {
838 return -1;
839 }
840 return 0;
841}
842
843static mode_t get_mode(const char *s) {
844 mode_t mode = 0;
845 while (*s) {
846 if (*s >= '0' && *s <= '7') {
847 mode = (mode<<3) | (*s-'0');
848 } else {
849 return -1;
850 }
851 s++;
852 }
853 return mode;
854}
855
Tom Cherryb7349902015-08-26 11:43:36 -0700856static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700857 mode_t mode = get_mode(args[1].c_str());
858 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700859 return -errno;
860 }
861 return 0;
862}
863
Tom Cherryb7349902015-08-26 11:43:36 -0700864static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400865 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500866
Tom Cherry96f67312015-07-30 13:52:55 -0700867 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
868 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400869 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500870 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400871 return ret;
872}
873
Tom Cherryb7349902015-08-26 11:43:36 -0700874static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400875 int ret = 0;
876
Tom Cherry96f67312015-07-30 13:52:55 -0700877 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
878 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400879 ret = -errno;
880 }
881 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500882}
883
Tom Cherryb7349902015-08-26 11:43:36 -0700884static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700885 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700886 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
887 ERROR("loglevel: invalid log level'%d'\n", log_level);
888 return -EINVAL;
889 }
890 klog_set_level(log_level);
891 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700892}
893
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700894static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700895 load_persist_props();
896 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800897}
898
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700899static int do_load_system_props(const std::vector<std::string>& args) {
900 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700901 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700902}
903
Tom Cherryb7349902015-08-26 11:43:36 -0700904static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700905 if (args.size() == 2) {
906 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
907 } else if (args.size() == 3) {
908 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800909 } else
910 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700911}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000912
Paul Lawrence806d10b2015-04-28 22:07:10 +0000913/*
914 * Callback to make a directory from the ext4 code
915 */
Tom Cherryb7349902015-08-26 11:43:36 -0700916static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000917 if (make_dir(dir, 0700) && errno != EEXIST) {
918 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000919 }
920
Paul Lawrence806d10b2015-04-28 22:07:10 +0000921 return 0;
922}
923
Paul Crowley749af8c2015-05-28 17:35:06 +0100924static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700925 std::string value = property_get("ro.crypto.type");
926 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100927}
928
Tom Cherryb7349902015-08-26 11:43:36 -0700929static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100930 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000931 return 0;
932 }
Tom Cherry96f67312015-07-30 13:52:55 -0700933 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000934 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000935}
Paul Crowley749af8c2015-05-28 17:35:06 +0100936
Paul Crowley59497452016-02-01 16:37:13 +0000937static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000938 return e4crypt_do_init_user0();
939}
940
Tom Cherryb7349902015-08-26 11:43:36 -0700941BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
942 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
943 static const Map builtin_functions = {
944 {"bootchart_init", {0, 0, do_bootchart_init}},
945 {"chmod", {2, 2, do_chmod}},
946 {"chown", {2, 3, do_chown}},
947 {"class_reset", {1, 1, do_class_reset}},
948 {"class_start", {1, 1, do_class_start}},
949 {"class_stop", {1, 1, do_class_stop}},
950 {"copy", {2, 2, do_copy}},
951 {"domainname", {1, 1, do_domainname}},
952 {"enable", {1, 1, do_enable}},
953 {"exec", {1, kMax, do_exec}},
954 {"export", {2, 2, do_export}},
955 {"hostname", {1, 1, do_hostname}},
956 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000957 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700958 {"insmod", {1, kMax, do_insmod}},
959 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700960 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700961 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700962 {"loglevel", {1, 1, do_loglevel}},
963 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800964 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700965 {"mount", {3, kMax, do_mount}},
Alex Light68ab20f2016-06-23 11:11:39 -0700966 {"umount", {1, 1, do_umount}},
Tom Cherryb7349902015-08-26 11:43:36 -0700967 {"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}},
975 {"start", {1, 1, do_start}},
976 {"stop", {1, 1, do_stop}},
977 {"swapon_all", {1, 1, do_swapon_all}},
978 {"symlink", {2, 2, do_symlink}},
979 {"sysclktz", {1, 1, do_sysclktz}},
980 {"trigger", {1, 1, do_trigger}},
981 {"verity_load_state", {0, 0, do_verity_load_state}},
982 {"verity_update_state", {0, 0, do_verity_update_state}},
983 {"wait", {1, 2, do_wait}},
984 {"write", {2, 2, do_write}},
985 };
986 return builtin_functions;
987}