blob: 30d648bbae9735fcf533960e59afc26e5e1752a4 [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
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070019#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070020#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070021#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070022#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070023#include <signal.h>
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -070024#include <sched.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070025#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070026#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070027#include <string.h>
28#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070029#include <sys/mount.h>
30#include <sys/resource.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080031#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070032#include <sys/types.h>
33#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070034#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070035#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000036#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000037#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070038
Stephen Smalleye46f9d52012-01-13 08:48:47 -050039#include <selinux/selinux.h>
40#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041
Elliott Hughesdb3f2672015-03-20 09:45:18 -070042#include <fs_mgr.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080043#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080044#include <android-base/stringprintf.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070045#include <cutils/partition_utils.h>
46#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070047#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070048#include <private/android_filesystem_config.h>
49
Tom Cherryfa0c21c2015-07-23 17:53:11 -070050#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070051#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070052#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070053#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070054#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070055#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070056#include "property_service.h"
57#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080058#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070059#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070060
Nick Kralevichbc609542015-01-31 21:39:46 -080061#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070062#define UNMOUNT_CHECK_MS 5000
63#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080064
Elliott Hughesf3cf4382015-02-03 17:12:07 -080065// System call provided by bionic but not in any header file.
66extern "C" int init_module(void *, unsigned long, const char *);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070067
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080068static const int kTerminateServiceDelayMicroSeconds = 50000;
69
Tom Cherryb7349902015-08-26 11:43:36 -070070static int insmod(const char *filename, const char *options) {
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070071 std::string module;
Yabin Cui00ede7d2015-07-24 13:26:04 -070072 if (!read_file(filename, &module)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080074 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070075
Elliott Hughesf682b472015-02-06 12:19:48 -080076 // TODO: use finit_module for >= 3.8 kernels.
77 return init_module(&module[0], module.size(), options);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070078}
79
Tom Cherryb7349902015-08-26 11:43:36 -070080static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081 struct ifreq ifr;
82 int s, ret;
83
84 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
85
86 s = socket(AF_INET, SOCK_DGRAM, 0);
87 if (s < 0)
88 return -1;
89
90 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
91 if (ret < 0) {
92 goto done;
93 }
94
95 if (up)
96 ifr.ifr_flags |= IFF_UP;
97 else
98 ifr.ifr_flags &= ~IFF_UP;
99
100 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800101
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700102done:
103 close(s);
104 return ret;
105}
106
Tom Cherryb7349902015-08-26 11:43:36 -0700107static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700108 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
109 return;
110
111 /* First, lazily unmount the directory. This unmount request finishes when
112 * all processes that open a file or directory in |entry->mnt_dir| exit.
113 */
114 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
115
116 /* Next, kill all processes except init, kthreadd, and kthreadd's
117 * children to finish the lazy unmount. Killing all processes here is okay
118 * because this callback function is only called right before reboot().
119 * It might be cleaner to selectively kill processes that actually use
120 * |entry->mnt_dir| rather than killing all, probably by reusing a function
121 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
122 * not allow init to scan /proc/<pid> files which the utility function
123 * heavily relies on. The policy does not allow the process to execute
124 * killall/pkill binaries either. Note that some processes might
125 * automatically restart after kill(), but that is not really a problem
126 * because |entry->mnt_dir| is no longer visible to such new processes.
127 */
Tom Cherrybac32992015-07-31 12:45:25 -0700128 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700129 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
130
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700131 // Restart Watchdogd to allow us to complete umounting and fsck
132 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
133 if (svc) {
134 do {
135 sched_yield(); // do not be so eager, let cleanup have priority
136 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
137 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
138 svc->Start();
139 }
140
Yusuke Sato0df08272015-07-08 14:57:07 -0700141 int count = 0;
142 while (count++ < UNMOUNT_CHECK_TIMES) {
143 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
144 if (fd >= 0) {
145 /* |entry->mnt_dir| has sucessfully been unmounted. */
146 close(fd);
147 break;
148 } else if (errno == EBUSY) {
149 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
150 * while then retry.
151 */
152 TEMP_FAILURE_RETRY(
153 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
154 continue;
155 } else {
156 /* Cannot open the device. Give up. */
157 return;
158 }
159 }
160
Mark Salyzyn0fcc2ee2016-04-05 08:10:25 -0700161 // NB: With watchdog still running, there is no cap on the time it takes
162 // to complete the fsck, from the users perspective the device graphics
163 // and responses are locked-up and they may choose to hold the power
164 // button in frustration if it drags out.
165
Yusuke Sato0df08272015-07-08 14:57:07 -0700166 int st;
167 if (!strcmp(entry->mnt_type, "f2fs")) {
168 const char *f2fs_argv[] = {
169 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
170 };
171 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700172 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700173 } else if (!strcmp(entry->mnt_type, "ext4")) {
174 const char *ext4_argv[] = {
175 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
176 };
177 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700178 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700179 }
180}
181
Tom Cherryb7349902015-08-26 11:43:36 -0700182static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700183 /* Starting a class does not start services
184 * which are explicitly disabled. They must
185 * be started individually.
186 */
Tom Cherrybac32992015-07-31 12:45:25 -0700187 ServiceManager::GetInstance().
188 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700189 return 0;
190}
191
Tom Cherryb7349902015-08-26 11:43:36 -0700192static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700193 ServiceManager::GetInstance().
194 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195 return 0;
196}
197
Tom Cherryb7349902015-08-26 11:43:36 -0700198static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700199 ServiceManager::GetInstance().
200 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800201 return 0;
202}
203
Tom Cherryb7349902015-08-26 11:43:36 -0700204static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700205 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700206}
207
Tom Cherryb7349902015-08-26 11:43:36 -0700208static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700209 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
210 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700211 return -1;
212 }
Tom Cherrybac32992015-07-31 12:45:25 -0700213 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700214}
215
Tom Cherryb7349902015-08-26 11:43:36 -0700216static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700217 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
218 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800219 return -1;
220 }
Tom Cherrybac32992015-07-31 12:45:25 -0700221 if (!svc->Start()) {
222 return -1;
223 }
224 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800225 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226}
227
Tom Cherryb7349902015-08-26 11:43:36 -0700228static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700229 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700230}
231
Tom Cherryb7349902015-08-26 11:43:36 -0700232static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700233 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700234}
235
Tom Cherryb7349902015-08-26 11:43:36 -0700236static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700237 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700238}
239
Tom Cherryb7349902015-08-26 11:43:36 -0700240static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700241 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800242
Tom Cherry96f67312015-07-30 13:52:55 -0700243 if (args.size() > 2) {
244 options += args[2];
245 for (std::size_t i = 3; i < args.size(); ++i) {
246 options += ' ';
247 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800248 }
249 }
250
Tom Cherry96f67312015-07-30 13:52:55 -0700251 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800252}
253
Tom Cherryb7349902015-08-26 11:43:36 -0700254static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700256 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257
258 /* mkdir <path> [mode] [owner] [group] */
259
Tom Cherry96f67312015-07-30 13:52:55 -0700260 if (args.size() >= 3) {
261 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262 }
263
Tom Cherry96f67312015-07-30 13:52:55 -0700264 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700265 /* chmod in case the directory already exists */
266 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700267 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700268 }
269 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270 return -errno;
271 }
272
Tom Cherry96f67312015-07-30 13:52:55 -0700273 if (args.size() >= 4) {
274 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700275 gid_t gid = -1;
276
Tom Cherry96f67312015-07-30 13:52:55 -0700277 if (args.size() == 5) {
278 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700279 }
280
Tom Cherry96f67312015-07-30 13:52:55 -0700281 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700282 return -errno;
283 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700284
285 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
286 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700287 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700288 if (ret == -1) {
289 return -errno;
290 }
291 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292 }
293
Tom Cherry96f67312015-07-30 13:52:55 -0700294 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700295}
296
297static struct {
298 const char *name;
299 unsigned flag;
300} mount_flags[] = {
301 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200302 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700303 { "nosuid", MS_NOSUID },
304 { "nodev", MS_NODEV },
305 { "nodiratime", MS_NODIRATIME },
306 { "ro", MS_RDONLY },
307 { "rw", 0 },
308 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700309 { "bind", MS_BIND },
310 { "rec", MS_REC },
311 { "unbindable", MS_UNBINDABLE },
312 { "private", MS_PRIVATE },
313 { "slave", MS_SLAVE },
314 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700315 { "defaults", 0 },
316 { 0, 0 },
317};
318
Ken Sumrall752923c2010-12-03 16:33:31 -0800319#define DATA_MNT_POINT "/data"
320
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700321/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700322static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700324 const char *source, *target, *system;
325 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700327 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700328 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700329 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700330
Tom Cherry96f67312015-07-30 13:52:55 -0700331 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700333 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334 flags |= mount_flags[i].flag;
335 break;
336 }
337 }
338
Colin Crosscd0f1732010-04-19 17:10:24 -0700339 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700340 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700341 wait = 1;
342 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700343 else if (na + 1 == args.size())
344 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700345 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346 }
347
Tom Cherry96f67312015-07-30 13:52:55 -0700348 system = args[1].c_str();
349 source = args[2].c_str();
350 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000351
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352 if (!strncmp(source, "mtd@", 4)) {
353 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000354 if (n < 0) {
355 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000357
Yabin Cuie2d63af2015-02-17 19:27:51 -0800358 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359
Colin Crosscd0f1732010-04-19 17:10:24 -0700360 if (wait)
361 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000362 if (mount(tmp, target, system, flags, options) < 0) {
363 return -1;
364 }
365
Ken Sumralldd4d7862011-02-17 18:09:47 -0800366 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000367 } else if (!strncmp(source, "loop@", 5)) {
368 int mode, loop, fd;
369 struct loop_info info;
370
371 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800372 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000373 if (fd < 0) {
374 return -1;
375 }
376
377 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800378 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800379 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000380 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100381 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000382 return -1;
383 }
384
385 /* if it is a blank loop device */
386 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
387 /* if it becomes our loop device */
388 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
389 close(fd);
390
391 if (mount(tmp, target, system, flags, options) < 0) {
392 ioctl(loop, LOOP_CLR_FD, 0);
393 close(loop);
394 return -1;
395 }
396
397 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800398 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000399 }
400 }
401
402 close(loop);
403 }
404
405 close(fd);
406 ERROR("out of loopback devices");
407 return -1;
408 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700409 if (wait)
410 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000411 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700412 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 }
414
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800416
417exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800418 return 0;
419
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700420}
421
Tom Cherryb7349902015-08-26 11:43:36 -0700422static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700423 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800424 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700425 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700426 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
427 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700428 close(fd);
429 } else {
430 ERROR("could not open /cache/recovery/command\n");
431 return -1;
432 }
433 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
434 while (1) { pause(); } // never reached
435}
436
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800437/* Imports .rc files from the specified paths. Default ones are applied if none is given.
438 *
439 * start_index: index of the first path in the args list
440 */
441static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700442 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800443 if (args.size() <= start_index) {
444 // Use the default set if no path is given
445 static const std::vector<std::string> init_directories = {
446 "/system/etc/init",
447 "/vendor/etc/init",
448 "/odm/etc/init"
449 };
450
451 for (const auto& dir : init_directories) {
452 parser.ParseConfig(dir);
453 }
454 } else {
455 for (size_t i = start_index; i < args.size(); ++i) {
456 parser.ParseConfig(args[i]);
457 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700458 }
459}
460
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800461/* mount_all <fstab> [ <path> ]*
462 *
JP Abgrallcee20682014-07-02 14:26:54 -0700463 * This function might request a reboot, in which case it will
464 * not return.
465 */
Tom Cherryb7349902015-08-26 11:43:36 -0700466static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700467 pid_t pid;
468 int ret = -1;
469 int child_ret = -1;
470 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800471 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700472
Tom Cherry96f67312015-07-30 13:52:55 -0700473 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700474 /*
475 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
476 * do the call in the child to provide protection to the main init
477 * process if anything goes wrong (crash or memory leak), and wait for
478 * the child to finish in the parent.
479 */
480 pid = fork();
481 if (pid > 0) {
482 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700483 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
484 if (wp_ret < 0) {
485 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700486 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700487 }
488
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700489 if (WIFEXITED(status)) {
490 ret = WEXITSTATUS(status);
491 } else {
492 ret = -1;
493 }
494 } else if (pid == 0) {
495 /* child, call fs_mgr_mount_all() */
496 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800497 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800498 child_ret = fs_mgr_mount_all(fstab);
499 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700500 if (child_ret == -1) {
501 ERROR("fs_mgr_mount_all returned an error\n");
502 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700503 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504 } else {
505 /* fork failed, return an error */
506 return -1;
507 }
508
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800509 /* Paths of .rc files are specified at the 2nd argument and beyond */
510 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700511
JP Abgrallf22b7452014-07-02 13:16:04 -0700512 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800513 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700514 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700515 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000516 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800517 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700518 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700519 property_set("ro.crypto.state", "unencrypted");
Paul Lawrence1098aac2016-03-04 15:52:33 -0800520 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
521 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) {
522 property_set("ro.crypto.state", "unsupported");
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700523 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700524 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
525 /* Setup a wipe via recovery, and reboot into recovery */
526 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
527 ret = wipe_data_via_recovery();
528 /* If reboot worked, there is no return. */
Paul Lawrence69080182016-02-02 10:31:30 -0800529 } else if (ret == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000530 if (e4crypt_install_keyring()) {
531 return -1;
532 }
533 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000534 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000535
536 // Although encrypted, we have device key, so we do not need to
537 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700538 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700539 } else if (ret > 0) {
540 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700541 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700542 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700543
544 return ret;
545}
546
Tom Cherryb7349902015-08-26 11:43:36 -0700547static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700548 struct fstab *fstab;
549 int ret;
550
Tom Cherry96f67312015-07-30 13:52:55 -0700551 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700552 ret = fs_mgr_swapon_all(fstab);
553 fs_mgr_free_fstab(fstab);
554
555 return ret;
556}
557
Tom Cherryb7349902015-08-26 11:43:36 -0700558static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700559 const char* name = args[1].c_str();
560 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700561 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700562 return 0;
563}
564
Tom Cherryb7349902015-08-26 11:43:36 -0700565static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700566 struct rlimit limit;
567 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700568 resource = std::stoi(args[1]);
569 limit.rlim_cur = std::stoi(args[2]);
570 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700571 return setrlimit(resource, &limit);
572}
573
Tom Cherryb7349902015-08-26 11:43:36 -0700574static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700575 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
576 if (!svc) {
577 ERROR("do_start: Service %s not found\n", args[1].c_str());
578 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700579 }
Tom Cherrybac32992015-07-31 12:45:25 -0700580 if (!svc->Start())
581 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700582 return 0;
583}
584
Tom Cherryb7349902015-08-26 11:43:36 -0700585static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700586 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
587 if (!svc) {
588 ERROR("do_stop: Service %s not found\n", args[1].c_str());
589 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700590 }
Tom Cherrybac32992015-07-31 12:45:25 -0700591 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700592 return 0;
593}
594
Tom Cherryb7349902015-08-26 11:43:36 -0700595static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700596 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
597 if (!svc) {
598 ERROR("do_restart: Service %s not found\n", args[1].c_str());
599 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 }
Tom Cherrybac32992015-07-31 12:45:25 -0700601 svc->Restart();
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_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700606 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700607 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700608 unsigned int cmd = 0;
609 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700610 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700611
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700612 if (strncmp(command, "shutdown", 8) == 0) {
613 cmd = ANDROID_RB_POWEROFF;
614 len = 8;
615 } else if (strncmp(command, "reboot", 6) == 0) {
616 cmd = ANDROID_RB_RESTART2;
617 len = 6;
618 } else {
619 ERROR("powerctl: unrecognized command '%s'\n", command);
620 return -EINVAL;
621 }
622
623 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700624 if (cmd == ANDROID_RB_POWEROFF &&
625 !strcmp(&command[len + 1], "userrequested")) {
626 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
627 // Run fsck once the file system is remounted in read-only mode.
628 callback_on_ro_remount = unmount_and_fsck;
629 } else if (cmd == ANDROID_RB_RESTART2) {
630 reboot_target = &command[len + 1];
631 }
632 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700633 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
634 return -EINVAL;
635 }
636
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800637 std::string timeout = property_get("ro.build.shutdown_timeout");
638 unsigned int delay = 0;
639
640 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
641 Timer t;
642 // Ask all services to terminate.
643 ServiceManager::GetInstance().ForEachService(
644 [] (Service* s) { s->Terminate(); });
645
646 while (t.duration() < delay) {
647 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
648
649 int service_count = 0;
650 ServiceManager::GetInstance().ForEachService(
651 [&service_count] (Service* s) {
652 // Count the number of services running.
653 // Exclude the console as it will ignore the SIGTERM signal
654 // and not exit.
655 // Note: SVC_CONSOLE actually means "requires console" but
656 // it is only used by the shell.
657 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
658 service_count++;
659 }
660 });
661
662 if (service_count == 0) {
663 // All terminable services terminated. We can exit early.
664 break;
665 }
666
667 // Wait a bit before recounting the number or running services.
668 usleep(kTerminateServiceDelayMicroSeconds);
669 }
670 NOTICE("Terminating running services took %.02f seconds", t.duration());
671 }
672
Yusuke Sato0df08272015-07-08 14:57:07 -0700673 return android_reboot_with_callback(cmd, 0, reboot_target,
674 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700675}
676
Tom Cherryb7349902015-08-26 11:43:36 -0700677static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700678 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700679 return 0;
680}
681
Tom Cherryb7349902015-08-26 11:43:36 -0700682static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700683 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700684}
685
Tom Cherryb7349902015-08-26 11:43:36 -0700686static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700687 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800688}
689
Tom Cherryb7349902015-08-26 11:43:36 -0700690static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700691 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800692}
693
Tom Cherryb7349902015-08-26 11:43:36 -0700694static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800695 struct timezone tz;
696
The Android Open Source Project35237d12008-12-17 18:08:08 -0800697 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700698 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800699 if (settimeofday(NULL, &tz))
700 return -1;
701 return 0;
702}
703
Tom Cherryb7349902015-08-26 11:43:36 -0700704static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700705 int mode = -1;
706 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000707 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700708 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000709 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700710 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000711}
712
Tom Cherryb7349902015-08-26 11:43:36 -0700713static void verity_update_property(fstab_rec *fstab, const char *mount_point,
714 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100715 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
716 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000717}
718
Tom Cherryb7349902015-08-26 11:43:36 -0700719static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700720 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000721}
722
Tom Cherryb7349902015-08-26 11:43:36 -0700723static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700724 const char* path = args[1].c_str();
725 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700726 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700727}
728
Tom Cherryb7349902015-08-26 11:43:36 -0700729static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700730 char *buffer = NULL;
731 int rc = 0;
732 int fd1 = -1, fd2 = -1;
733 struct stat info;
734 int brtw, brtr;
735 char *p;
736
Tom Cherry96f67312015-07-30 13:52:55 -0700737 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700738 return -1;
739
Tom Cherry96f67312015-07-30 13:52:55 -0700740 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700741 goto out_err;
742
Tom Cherry96f67312015-07-30 13:52:55 -0700743 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700744 goto out_err;
745
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800746 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700747 goto out_err;
748
749 p = buffer;
750 brtr = info.st_size;
751 while(brtr) {
752 rc = read(fd1, p, brtr);
753 if (rc < 0)
754 goto out_err;
755 if (rc == 0)
756 break;
757 p += rc;
758 brtr -= rc;
759 }
760
761 p = buffer;
762 brtw = info.st_size;
763 while(brtw) {
764 rc = write(fd2, p, brtw);
765 if (rc < 0)
766 goto out_err;
767 if (rc == 0)
768 break;
769 p += rc;
770 brtw -= rc;
771 }
772
773 rc = 0;
774 goto out;
775out_err:
776 rc = -1;
777out:
778 if (buffer)
779 free(buffer);
780 if (fd1 >= 0)
781 close(fd1);
782 if (fd2 >= 0)
783 close(fd2);
784 return rc;
785}
786
Tom Cherryb7349902015-08-26 11:43:36 -0700787static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700788 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700789 if (args.size() == 3) {
790 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700791 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700792 } else if (args.size() == 4) {
793 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
794 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700795 return -errno;
796 } else {
797 return -1;
798 }
799 return 0;
800}
801
802static mode_t get_mode(const char *s) {
803 mode_t mode = 0;
804 while (*s) {
805 if (*s >= '0' && *s <= '7') {
806 mode = (mode<<3) | (*s-'0');
807 } else {
808 return -1;
809 }
810 s++;
811 }
812 return mode;
813}
814
Tom Cherryb7349902015-08-26 11:43:36 -0700815static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700816 mode_t mode = get_mode(args[1].c_str());
817 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700818 return -errno;
819 }
820 return 0;
821}
822
Tom Cherryb7349902015-08-26 11:43:36 -0700823static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400824 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500825
Tom Cherry96f67312015-07-30 13:52:55 -0700826 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
827 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400828 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500829 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400830 return ret;
831}
832
Tom Cherryb7349902015-08-26 11:43:36 -0700833static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400834 int ret = 0;
835
Tom Cherry96f67312015-07-30 13:52:55 -0700836 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
837 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400838 ret = -errno;
839 }
840 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500841}
842
Tom Cherryb7349902015-08-26 11:43:36 -0700843static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700844 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700845 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
846 ERROR("loglevel: invalid log level'%d'\n", log_level);
847 return -EINVAL;
848 }
849 klog_set_level(log_level);
850 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700851}
852
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700853static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700854 load_persist_props();
855 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800856}
857
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700858static int do_load_system_props(const std::vector<std::string>& args) {
859 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700860 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700861}
862
Tom Cherryb7349902015-08-26 11:43:36 -0700863static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700864 if (args.size() == 2) {
865 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
866 } else if (args.size() == 3) {
867 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800868 } else
869 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700870}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000871
Paul Lawrence806d10b2015-04-28 22:07:10 +0000872/*
873 * Callback to make a directory from the ext4 code
874 */
Tom Cherryb7349902015-08-26 11:43:36 -0700875static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000876 if (make_dir(dir, 0700) && errno != EEXIST) {
877 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000878 }
879
Paul Lawrence806d10b2015-04-28 22:07:10 +0000880 return 0;
881}
882
Paul Crowley749af8c2015-05-28 17:35:06 +0100883static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700884 std::string value = property_get("ro.crypto.type");
885 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100886}
887
Tom Cherryb7349902015-08-26 11:43:36 -0700888static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100889 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000890 return 0;
891 }
Tom Cherry96f67312015-07-30 13:52:55 -0700892 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000893 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000894}
Paul Crowley749af8c2015-05-28 17:35:06 +0100895
Paul Crowley59497452016-02-01 16:37:13 +0000896static int do_init_user0(const std::vector<std::string>& args) {
Paul Crowley59497452016-02-01 16:37:13 +0000897 return e4crypt_do_init_user0();
898}
899
Tom Cherryb7349902015-08-26 11:43:36 -0700900BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
901 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
902 static const Map builtin_functions = {
903 {"bootchart_init", {0, 0, do_bootchart_init}},
904 {"chmod", {2, 2, do_chmod}},
905 {"chown", {2, 3, do_chown}},
906 {"class_reset", {1, 1, do_class_reset}},
907 {"class_start", {1, 1, do_class_start}},
908 {"class_stop", {1, 1, do_class_stop}},
909 {"copy", {2, 2, do_copy}},
910 {"domainname", {1, 1, do_domainname}},
911 {"enable", {1, 1, do_enable}},
912 {"exec", {1, kMax, do_exec}},
913 {"export", {2, 2, do_export}},
914 {"hostname", {1, 1, do_hostname}},
915 {"ifup", {1, 1, do_ifup}},
Paul Crowley59497452016-02-01 16:37:13 +0000916 {"init_user0", {0, 0, do_init_user0}},
Tom Cherryb7349902015-08-26 11:43:36 -0700917 {"insmod", {1, kMax, do_insmod}},
918 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700919 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700920 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700921 {"loglevel", {1, 1, do_loglevel}},
922 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800923 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700924 {"mount", {3, kMax, do_mount}},
925 {"powerctl", {1, 1, do_powerctl}},
926 {"restart", {1, 1, do_restart}},
927 {"restorecon", {1, kMax, do_restorecon}},
928 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
929 {"rm", {1, 1, do_rm}},
930 {"rmdir", {1, 1, do_rmdir}},
931 {"setprop", {2, 2, do_setprop}},
932 {"setrlimit", {3, 3, do_setrlimit}},
933 {"start", {1, 1, do_start}},
934 {"stop", {1, 1, do_stop}},
935 {"swapon_all", {1, 1, do_swapon_all}},
936 {"symlink", {2, 2, do_symlink}},
937 {"sysclktz", {1, 1, do_sysclktz}},
938 {"trigger", {1, 1, do_trigger}},
939 {"verity_load_state", {0, 0, do_verity_load_state}},
940 {"verity_update_state", {0, 0, do_verity_update_state}},
941 {"wait", {1, 2, do_wait}},
942 {"write", {2, 2, do_write}},
943 };
944 return builtin_functions;
945}