blob: 6469ec4886483a2e02d034cb620f674eabcd8029 [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>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070024#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070025#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <string.h>
27#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070028#include <sys/mount.h>
29#include <sys/resource.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070030#include <sys/syscall.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
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080065static const int kTerminateServiceDelayMicroSeconds = 50000;
66
Tom Cherryb7349902015-08-26 11:43:36 -070067static int insmod(const char *filename, const char *options) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070068 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
69 if (fd == -1) {
70 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080072 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070073 int rc = syscall(__NR_finit_module, fd, options, 0);
74 if (rc == -1) {
75 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
76 }
77 close(fd);
78 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070079}
80
Tom Cherryb7349902015-08-26 11:43:36 -070081static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082 struct ifreq ifr;
83 int s, ret;
84
85 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
86
87 s = socket(AF_INET, SOCK_DGRAM, 0);
88 if (s < 0)
89 return -1;
90
91 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
92 if (ret < 0) {
93 goto done;
94 }
95
96 if (up)
97 ifr.ifr_flags |= IFF_UP;
98 else
99 ifr.ifr_flags &= ~IFF_UP;
100
101 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800102
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103done:
104 close(s);
105 return ret;
106}
107
Tom Cherryb7349902015-08-26 11:43:36 -0700108static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700109 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
110 return;
111
112 /* First, lazily unmount the directory. This unmount request finishes when
113 * all processes that open a file or directory in |entry->mnt_dir| exit.
114 */
115 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
116
117 /* Next, kill all processes except init, kthreadd, and kthreadd's
118 * children to finish the lazy unmount. Killing all processes here is okay
119 * because this callback function is only called right before reboot().
120 * It might be cleaner to selectively kill processes that actually use
121 * |entry->mnt_dir| rather than killing all, probably by reusing a function
122 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
123 * not allow init to scan /proc/<pid> files which the utility function
124 * heavily relies on. The policy does not allow the process to execute
125 * killall/pkill binaries either. Note that some processes might
126 * automatically restart after kill(), but that is not really a problem
127 * because |entry->mnt_dir| is no longer visible to such new processes.
128 */
Tom Cherrybac32992015-07-31 12:45:25 -0700129 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700130 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
131
132 int count = 0;
133 while (count++ < UNMOUNT_CHECK_TIMES) {
134 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
135 if (fd >= 0) {
136 /* |entry->mnt_dir| has sucessfully been unmounted. */
137 close(fd);
138 break;
139 } else if (errno == EBUSY) {
140 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
141 * while then retry.
142 */
143 TEMP_FAILURE_RETRY(
144 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
145 continue;
146 } else {
147 /* Cannot open the device. Give up. */
148 return;
149 }
150 }
151
152 int st;
153 if (!strcmp(entry->mnt_type, "f2fs")) {
154 const char *f2fs_argv[] = {
155 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
156 };
157 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700158 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700159 } else if (!strcmp(entry->mnt_type, "ext4")) {
160 const char *ext4_argv[] = {
161 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
162 };
163 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700164 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700165 }
166}
167
Tom Cherryb7349902015-08-26 11:43:36 -0700168static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700169 /* Starting a class does not start services
170 * which are explicitly disabled. They must
171 * be started individually.
172 */
Tom Cherrybac32992015-07-31 12:45:25 -0700173 ServiceManager::GetInstance().
174 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700175 return 0;
176}
177
Tom Cherryb7349902015-08-26 11:43:36 -0700178static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700179 ServiceManager::GetInstance().
180 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700181 return 0;
182}
183
Tom Cherryb7349902015-08-26 11:43:36 -0700184static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700185 ServiceManager::GetInstance().
186 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800187 return 0;
188}
189
Tom Cherryb7349902015-08-26 11:43:36 -0700190static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700191 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192}
193
Tom Cherryb7349902015-08-26 11:43:36 -0700194static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700195 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
196 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700197 return -1;
198 }
Tom Cherrybac32992015-07-31 12:45:25 -0700199 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700200}
201
Tom Cherryb7349902015-08-26 11:43:36 -0700202static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700203 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
204 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800205 return -1;
206 }
Tom Cherrybac32992015-07-31 12:45:25 -0700207 if (!svc->Start()) {
208 return -1;
209 }
210 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800211 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700212}
213
Tom Cherryb7349902015-08-26 11:43:36 -0700214static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700215 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700216}
217
Tom Cherryb7349902015-08-26 11:43:36 -0700218static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700219 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220}
221
Tom Cherryb7349902015-08-26 11:43:36 -0700222static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700223 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700224}
225
Tom Cherryb7349902015-08-26 11:43:36 -0700226static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700227 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800228
Tom Cherry96f67312015-07-30 13:52:55 -0700229 if (args.size() > 2) {
230 options += args[2];
231 for (std::size_t i = 3; i < args.size(); ++i) {
232 options += ' ';
233 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800234 }
235 }
236
Tom Cherry96f67312015-07-30 13:52:55 -0700237 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800238}
239
Tom Cherryb7349902015-08-26 11:43:36 -0700240static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700241 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700242 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243
244 /* mkdir <path> [mode] [owner] [group] */
245
Tom Cherry96f67312015-07-30 13:52:55 -0700246 if (args.size() >= 3) {
247 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700248 }
249
Tom Cherry96f67312015-07-30 13:52:55 -0700250 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700251 /* chmod in case the directory already exists */
252 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700253 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700254 }
255 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700256 return -errno;
257 }
258
Tom Cherry96f67312015-07-30 13:52:55 -0700259 if (args.size() >= 4) {
260 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261 gid_t gid = -1;
262
Tom Cherry96f67312015-07-30 13:52:55 -0700263 if (args.size() == 5) {
264 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265 }
266
Tom Cherry96f67312015-07-30 13:52:55 -0700267 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700268 return -errno;
269 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700270
271 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
272 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700273 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700274 if (ret == -1) {
275 return -errno;
276 }
277 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700278 }
279
Tom Cherry96f67312015-07-30 13:52:55 -0700280 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700281}
282
283static struct {
284 const char *name;
285 unsigned flag;
286} mount_flags[] = {
287 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200288 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289 { "nosuid", MS_NOSUID },
290 { "nodev", MS_NODEV },
291 { "nodiratime", MS_NODIRATIME },
292 { "ro", MS_RDONLY },
293 { "rw", 0 },
294 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700295 { "bind", MS_BIND },
296 { "rec", MS_REC },
297 { "unbindable", MS_UNBINDABLE },
298 { "private", MS_PRIVATE },
299 { "slave", MS_SLAVE },
300 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 { "defaults", 0 },
302 { 0, 0 },
303};
304
Ken Sumrall752923c2010-12-03 16:33:31 -0800305#define DATA_MNT_POINT "/data"
306
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700308static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700309 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700310 const char *source, *target, *system;
311 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700312 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700313 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700315 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316
Tom Cherry96f67312015-07-30 13:52:55 -0700317 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700318 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700319 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700320 flags |= mount_flags[i].flag;
321 break;
322 }
323 }
324
Colin Crosscd0f1732010-04-19 17:10:24 -0700325 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700326 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700327 wait = 1;
328 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700329 else if (na + 1 == args.size())
330 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700331 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700332 }
333
Tom Cherry96f67312015-07-30 13:52:55 -0700334 system = args[1].c_str();
335 source = args[2].c_str();
336 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000337
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700338 if (!strncmp(source, "mtd@", 4)) {
339 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000340 if (n < 0) {
341 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700342 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000343
Yabin Cuie2d63af2015-02-17 19:27:51 -0800344 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000345
Colin Crosscd0f1732010-04-19 17:10:24 -0700346 if (wait)
347 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000348 if (mount(tmp, target, system, flags, options) < 0) {
349 return -1;
350 }
351
Ken Sumralldd4d7862011-02-17 18:09:47 -0800352 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000353 } else if (!strncmp(source, "loop@", 5)) {
354 int mode, loop, fd;
355 struct loop_info info;
356
357 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800358 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000359 if (fd < 0) {
360 return -1;
361 }
362
363 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800364 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800365 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000366 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100367 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000368 return -1;
369 }
370
371 /* if it is a blank loop device */
372 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
373 /* if it becomes our loop device */
374 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
375 close(fd);
376
377 if (mount(tmp, target, system, flags, options) < 0) {
378 ioctl(loop, LOOP_CLR_FD, 0);
379 close(loop);
380 return -1;
381 }
382
383 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800384 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000385 }
386 }
387
388 close(loop);
389 }
390
391 close(fd);
392 ERROR("out of loopback devices");
393 return -1;
394 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700395 if (wait)
396 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000397 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700398 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000399 }
400
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800402
403exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800404 return 0;
405
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700406}
407
Tom Cherryb7349902015-08-26 11:43:36 -0700408static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700409 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800410 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700411 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700412 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
413 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700414 close(fd);
415 } else {
416 ERROR("could not open /cache/recovery/command\n");
417 return -1;
418 }
419 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
420 while (1) { pause(); } // never reached
421}
422
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800423/* Imports .rc files from the specified paths. Default ones are applied if none is given.
424 *
425 * start_index: index of the first path in the args list
426 */
427static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700428 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800429 if (args.size() <= start_index) {
430 // Use the default set if no path is given
431 static const std::vector<std::string> init_directories = {
432 "/system/etc/init",
433 "/vendor/etc/init",
434 "/odm/etc/init"
435 };
436
437 for (const auto& dir : init_directories) {
438 parser.ParseConfig(dir);
439 }
440 } else {
441 for (size_t i = start_index; i < args.size(); ++i) {
442 parser.ParseConfig(args[i]);
443 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700444 }
445}
446
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800447/* mount_all <fstab> [ <path> ]*
448 *
JP Abgrallcee20682014-07-02 14:26:54 -0700449 * This function might request a reboot, in which case it will
450 * not return.
451 */
Tom Cherryb7349902015-08-26 11:43:36 -0700452static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700453 pid_t pid;
454 int ret = -1;
455 int child_ret = -1;
456 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800457 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700458
Tom Cherry96f67312015-07-30 13:52:55 -0700459 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700460 /*
461 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
462 * do the call in the child to provide protection to the main init
463 * process if anything goes wrong (crash or memory leak), and wait for
464 * the child to finish in the parent.
465 */
466 pid = fork();
467 if (pid > 0) {
468 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700469 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
470 if (wp_ret < 0) {
471 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700472 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700473 }
474
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700475 if (WIFEXITED(status)) {
476 ret = WEXITSTATUS(status);
477 } else {
478 ret = -1;
479 }
480 } else if (pid == 0) {
481 /* child, call fs_mgr_mount_all() */
482 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800483 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800484 child_ret = fs_mgr_mount_all(fstab);
485 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700486 if (child_ret == -1) {
487 ERROR("fs_mgr_mount_all returned an error\n");
488 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700489 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700490 } else {
491 /* fork failed, return an error */
492 return -1;
493 }
494
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800495 /* Paths of .rc files are specified at the 2nd argument and beyond */
496 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700497
JP Abgrallf22b7452014-07-02 13:16:04 -0700498 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800499 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700500 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700501 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000502 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800503 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700504 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700505 property_set("ro.crypto.state", "unencrypted");
506 /* If fs_mgr determined this is an unencrypted device, then trigger
507 * that action.
508 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700509 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700510 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
511 /* Setup a wipe via recovery, and reboot into recovery */
512 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
513 ret = wipe_data_via_recovery();
514 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000515 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000516 if (e4crypt_install_keyring()) {
517 return -1;
518 }
519 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000520 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000521
522 // Although encrypted, we have device key, so we do not need to
523 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700524 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000525 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
526 if (e4crypt_install_keyring()) {
527 return -1;
528 }
529 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000530 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000531 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700532 } else if (ret > 0) {
533 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700534 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700535 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700536
537 return ret;
538}
539
Tom Cherryb7349902015-08-26 11:43:36 -0700540static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700541 struct fstab *fstab;
542 int ret;
543
Tom Cherry96f67312015-07-30 13:52:55 -0700544 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700545 ret = fs_mgr_swapon_all(fstab);
546 fs_mgr_free_fstab(fstab);
547
548 return ret;
549}
550
Tom Cherryb7349902015-08-26 11:43:36 -0700551static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700552 const char* name = args[1].c_str();
553 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700554 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700555 return 0;
556}
557
Tom Cherryb7349902015-08-26 11:43:36 -0700558static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700559 struct rlimit limit;
560 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700561 resource = std::stoi(args[1]);
562 limit.rlim_cur = std::stoi(args[2]);
563 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700564 return setrlimit(resource, &limit);
565}
566
Tom Cherryb7349902015-08-26 11:43:36 -0700567static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700568 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
569 if (!svc) {
570 ERROR("do_start: Service %s not found\n", args[1].c_str());
571 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700572 }
Tom Cherrybac32992015-07-31 12:45:25 -0700573 if (!svc->Start())
574 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700575 return 0;
576}
577
Tom Cherryb7349902015-08-26 11:43:36 -0700578static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700579 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
580 if (!svc) {
581 ERROR("do_stop: Service %s not found\n", args[1].c_str());
582 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700583 }
Tom Cherrybac32992015-07-31 12:45:25 -0700584 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700585 return 0;
586}
587
Tom Cherryb7349902015-08-26 11:43:36 -0700588static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700589 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
590 if (!svc) {
591 ERROR("do_restart: Service %s not found\n", args[1].c_str());
592 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700593 }
Tom Cherrybac32992015-07-31 12:45:25 -0700594 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700595 return 0;
596}
597
Tom Cherryb7349902015-08-26 11:43:36 -0700598static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700599 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700600 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700601 unsigned int cmd = 0;
602 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700603 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700604
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700605 if (strncmp(command, "shutdown", 8) == 0) {
606 cmd = ANDROID_RB_POWEROFF;
607 len = 8;
608 } else if (strncmp(command, "reboot", 6) == 0) {
609 cmd = ANDROID_RB_RESTART2;
610 len = 6;
611 } else {
612 ERROR("powerctl: unrecognized command '%s'\n", command);
613 return -EINVAL;
614 }
615
616 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700617 if (cmd == ANDROID_RB_POWEROFF &&
618 !strcmp(&command[len + 1], "userrequested")) {
619 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
620 // Run fsck once the file system is remounted in read-only mode.
621 callback_on_ro_remount = unmount_and_fsck;
622 } else if (cmd == ANDROID_RB_RESTART2) {
623 reboot_target = &command[len + 1];
624 }
625 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700626 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
627 return -EINVAL;
628 }
629
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800630 std::string timeout = property_get("ro.build.shutdown_timeout");
631 unsigned int delay = 0;
632
633 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
634 Timer t;
635 // Ask all services to terminate.
636 ServiceManager::GetInstance().ForEachService(
637 [] (Service* s) { s->Terminate(); });
638
639 while (t.duration() < delay) {
640 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
641
642 int service_count = 0;
643 ServiceManager::GetInstance().ForEachService(
644 [&service_count] (Service* s) {
645 // Count the number of services running.
646 // Exclude the console as it will ignore the SIGTERM signal
647 // and not exit.
648 // Note: SVC_CONSOLE actually means "requires console" but
649 // it is only used by the shell.
650 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
651 service_count++;
652 }
653 });
654
655 if (service_count == 0) {
656 // All terminable services terminated. We can exit early.
657 break;
658 }
659
660 // Wait a bit before recounting the number or running services.
661 usleep(kTerminateServiceDelayMicroSeconds);
662 }
663 NOTICE("Terminating running services took %.02f seconds", t.duration());
664 }
665
Yusuke Sato0df08272015-07-08 14:57:07 -0700666 return android_reboot_with_callback(cmd, 0, reboot_target,
667 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700668}
669
Tom Cherryb7349902015-08-26 11:43:36 -0700670static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700671 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700672 return 0;
673}
674
Tom Cherryb7349902015-08-26 11:43:36 -0700675static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700676 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700677}
678
Tom Cherryb7349902015-08-26 11:43:36 -0700679static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700680 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800681}
682
Tom Cherryb7349902015-08-26 11:43:36 -0700683static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700684 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800685}
686
Tom Cherryb7349902015-08-26 11:43:36 -0700687static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800688 struct timezone tz;
689
The Android Open Source Project35237d12008-12-17 18:08:08 -0800690 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700691 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800692 if (settimeofday(NULL, &tz))
693 return -1;
694 return 0;
695}
696
Tom Cherryb7349902015-08-26 11:43:36 -0700697static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700698 int mode = -1;
699 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000700 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700701 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000702 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700703 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000704}
705
Tom Cherryb7349902015-08-26 11:43:36 -0700706static void verity_update_property(fstab_rec *fstab, const char *mount_point,
707 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100708 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
709 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000710}
711
Tom Cherryb7349902015-08-26 11:43:36 -0700712static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700713 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000714}
715
Tom Cherryb7349902015-08-26 11:43:36 -0700716static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700717 const char* path = args[1].c_str();
718 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700719 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700720}
721
Tom Cherryb7349902015-08-26 11:43:36 -0700722static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700723 char *buffer = NULL;
724 int rc = 0;
725 int fd1 = -1, fd2 = -1;
726 struct stat info;
727 int brtw, brtr;
728 char *p;
729
Tom Cherry96f67312015-07-30 13:52:55 -0700730 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700731 return -1;
732
Tom Cherry96f67312015-07-30 13:52:55 -0700733 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700734 goto out_err;
735
Tom Cherry96f67312015-07-30 13:52:55 -0700736 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700737 goto out_err;
738
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800739 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700740 goto out_err;
741
742 p = buffer;
743 brtr = info.st_size;
744 while(brtr) {
745 rc = read(fd1, p, brtr);
746 if (rc < 0)
747 goto out_err;
748 if (rc == 0)
749 break;
750 p += rc;
751 brtr -= rc;
752 }
753
754 p = buffer;
755 brtw = info.st_size;
756 while(brtw) {
757 rc = write(fd2, p, brtw);
758 if (rc < 0)
759 goto out_err;
760 if (rc == 0)
761 break;
762 p += rc;
763 brtw -= rc;
764 }
765
766 rc = 0;
767 goto out;
768out_err:
769 rc = -1;
770out:
771 if (buffer)
772 free(buffer);
773 if (fd1 >= 0)
774 close(fd1);
775 if (fd2 >= 0)
776 close(fd2);
777 return rc;
778}
779
Tom Cherryb7349902015-08-26 11:43:36 -0700780static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700782 if (args.size() == 3) {
783 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700784 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700785 } else if (args.size() == 4) {
786 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
787 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700788 return -errno;
789 } else {
790 return -1;
791 }
792 return 0;
793}
794
795static mode_t get_mode(const char *s) {
796 mode_t mode = 0;
797 while (*s) {
798 if (*s >= '0' && *s <= '7') {
799 mode = (mode<<3) | (*s-'0');
800 } else {
801 return -1;
802 }
803 s++;
804 }
805 return mode;
806}
807
Tom Cherryb7349902015-08-26 11:43:36 -0700808static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700809 mode_t mode = get_mode(args[1].c_str());
810 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 return -errno;
812 }
813 return 0;
814}
815
Tom Cherryb7349902015-08-26 11:43:36 -0700816static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400817 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500818
Tom Cherry96f67312015-07-30 13:52:55 -0700819 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
820 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400821 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500822 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400823 return ret;
824}
825
Tom Cherryb7349902015-08-26 11:43:36 -0700826static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400827 int ret = 0;
828
Tom Cherry96f67312015-07-30 13:52:55 -0700829 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
830 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400831 ret = -errno;
832 }
833 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500834}
835
Tom Cherryb7349902015-08-26 11:43:36 -0700836static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700837 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700838 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
839 ERROR("loglevel: invalid log level'%d'\n", log_level);
840 return -EINVAL;
841 }
842 klog_set_level(log_level);
843 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700844}
845
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700846static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700847 load_persist_props();
848 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800849}
850
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700851static int do_load_system_props(const std::vector<std::string>& args) {
852 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700853 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700854}
855
Tom Cherryb7349902015-08-26 11:43:36 -0700856static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700857 if (args.size() == 2) {
858 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
859 } else if (args.size() == 3) {
860 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800861 } else
862 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700863}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000864
Paul Lawrence806d10b2015-04-28 22:07:10 +0000865/*
866 * Callback to make a directory from the ext4 code
867 */
Tom Cherryb7349902015-08-26 11:43:36 -0700868static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000869 if (make_dir(dir, 0700) && errno != EEXIST) {
870 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000871 }
872
Paul Lawrence806d10b2015-04-28 22:07:10 +0000873 return 0;
874}
875
Paul Crowley749af8c2015-05-28 17:35:06 +0100876static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700877 std::string value = property_get("ro.crypto.type");
878 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100879}
880
Tom Cherryb7349902015-08-26 11:43:36 -0700881static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100882 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000883 return 0;
884 }
Tom Cherry96f67312015-07-30 13:52:55 -0700885 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000886 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000887}
Paul Crowley749af8c2015-05-28 17:35:06 +0100888
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700889static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100890 if (!is_file_crypto()) {
891 return 0;
892 }
Tom Cherry087cd352015-08-03 14:19:35 -0700893 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100894}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700895
Tom Cherryb7349902015-08-26 11:43:36 -0700896BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
897 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
898 static const Map builtin_functions = {
899 {"bootchart_init", {0, 0, do_bootchart_init}},
900 {"chmod", {2, 2, do_chmod}},
901 {"chown", {2, 3, do_chown}},
902 {"class_reset", {1, 1, do_class_reset}},
903 {"class_start", {1, 1, do_class_start}},
904 {"class_stop", {1, 1, do_class_stop}},
905 {"copy", {2, 2, do_copy}},
906 {"domainname", {1, 1, do_domainname}},
907 {"enable", {1, 1, do_enable}},
908 {"exec", {1, kMax, do_exec}},
909 {"export", {2, 2, do_export}},
910 {"hostname", {1, 1, do_hostname}},
911 {"ifup", {1, 1, do_ifup}},
912 {"insmod", {1, kMax, do_insmod}},
913 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700914 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700915 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700916 {"loglevel", {1, 1, do_loglevel}},
917 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800918 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700919 {"mount", {3, kMax, do_mount}},
920 {"powerctl", {1, 1, do_powerctl}},
921 {"restart", {1, 1, do_restart}},
922 {"restorecon", {1, kMax, do_restorecon}},
923 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
924 {"rm", {1, 1, do_rm}},
925 {"rmdir", {1, 1, do_rmdir}},
926 {"setprop", {2, 2, do_setprop}},
927 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700928 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700929 {"start", {1, 1, do_start}},
930 {"stop", {1, 1, do_stop}},
931 {"swapon_all", {1, 1, do_swapon_all}},
932 {"symlink", {2, 2, do_symlink}},
933 {"sysclktz", {1, 1, do_sysclktz}},
934 {"trigger", {1, 1, do_trigger}},
935 {"verity_load_state", {0, 0, do_verity_load_state}},
936 {"verity_update_state", {0, 0, do_verity_update_state}},
937 {"wait", {1, 2, do_wait}},
938 {"write", {2, 2, do_write}},
939 };
940 return builtin_functions;
941}