blob: 89f6c68c82382811b2807e64ae24ab9784280d5c [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tom Cherryb7349902015-08-26 11:43:36 -070017#include "builtins.h"
18
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070019#include <dirent.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070020#include <errno.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070021#include <fcntl.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070022#include <mntent.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070023#include <net/if.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070024#include <signal.h>
Mark Salyzynad575e02016-04-05 08:10:25 -070025#include <sched.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070026#include <stdio.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070027#include <stdlib.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070028#include <string.h>
29#include <sys/socket.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070030#include <sys/mount.h>
31#include <sys/resource.h>
Nick Kralevich124a9c92016-03-27 16:55:59 -070032#include <sys/syscall.h>
Elliott Hughes3d74d7a2015-01-29 21:31:23 -080033#include <sys/time.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070034#include <sys/types.h>
35#include <sys/stat.h>
Ken Sumrall0e9dd902012-04-17 17:20:16 -070036#include <sys/wait.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070037#include <unistd.h>
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +000038#include <linux/loop.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000039#include <ext4_crypt_init_extensions.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Stephen Smalleye46f9d52012-01-13 08:48:47 -050041#include <selinux/selinux.h>
42#include <selinux/label.h>
Stephen Smalleye46f9d52012-01-13 08:48:47 -050043
Elliott Hughesdb3f2672015-03-20 09:45:18 -070044#include <fs_mgr.h>
Mark Salyzyna98cc9c2016-04-05 13:43:40 -070045#include <android-base/file.h>
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080046#include <android-base/parseint.h>
Elliott Hughes4f713192015-12-04 22:00:26 -080047#include <android-base/stringprintf.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070048#include <cutils/partition_utils.h>
49#include <cutils/android_reboot.h>
Yusuke Sato0df08272015-07-08 14:57:07 -070050#include <logwrap/logwrap.h>
Elliott Hughesdb3f2672015-03-20 09:45:18 -070051
Tom Cherryfa0c21c2015-07-23 17:53:11 -070052#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070053#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070055#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070056#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070057#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070058#include "property_service.h"
59#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080060#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070061#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070062
Nick Kralevichbc609542015-01-31 21:39:46 -080063#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070064#define UNMOUNT_CHECK_MS 5000
65#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080066
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080067static const int kTerminateServiceDelayMicroSeconds = 50000;
68
Tom Cherryb7349902015-08-26 11:43:36 -070069static int insmod(const char *filename, const char *options) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070070 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
71 if (fd == -1) {
72 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070073 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080074 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070075 int rc = syscall(__NR_finit_module, fd, options, 0);
76 if (rc == -1) {
77 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
78 }
79 close(fd);
80 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070081}
82
Tom Cherryb7349902015-08-26 11:43:36 -070083static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070084 struct ifreq ifr;
85 int s, ret;
86
87 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
88
89 s = socket(AF_INET, SOCK_DGRAM, 0);
90 if (s < 0)
91 return -1;
92
93 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
94 if (ret < 0) {
95 goto done;
96 }
97
98 if (up)
99 ifr.ifr_flags |= IFF_UP;
100 else
101 ifr.ifr_flags &= ~IFF_UP;
102
103 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800104
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700105done:
106 close(s);
107 return ret;
108}
109
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700110// Turn off backlight while we are performing power down cleanup activities.
111static void turnOffBacklight() {
112 static const char off[] = "0";
113
114 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
115
116 static const char backlightDir[] = "/sys/class/backlight";
117 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
118 if (!dir) {
119 return;
120 }
121
122 struct dirent *dp;
123 while ((dp = readdir(dir.get())) != NULL) {
124 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
125 (dp->d_name[0] == '.')) {
126 continue;
127 }
128
129 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
130 backlightDir,
131 dp->d_name);
132 android::base::WriteStringToFile(off, fileName);
133 }
134}
135
Tom Cherryb7349902015-08-26 11:43:36 -0700136static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700137 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
138 return;
139
140 /* First, lazily unmount the directory. This unmount request finishes when
141 * all processes that open a file or directory in |entry->mnt_dir| exit.
142 */
143 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
144
145 /* Next, kill all processes except init, kthreadd, and kthreadd's
146 * children to finish the lazy unmount. Killing all processes here is okay
147 * because this callback function is only called right before reboot().
148 * It might be cleaner to selectively kill processes that actually use
149 * |entry->mnt_dir| rather than killing all, probably by reusing a function
150 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
151 * not allow init to scan /proc/<pid> files which the utility function
152 * heavily relies on. The policy does not allow the process to execute
153 * killall/pkill binaries either. Note that some processes might
154 * automatically restart after kill(), but that is not really a problem
155 * because |entry->mnt_dir| is no longer visible to such new processes.
156 */
Tom Cherrybac32992015-07-31 12:45:25 -0700157 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700158 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
159
Mark Salyzynad575e02016-04-05 08:10:25 -0700160 // Restart Watchdogd to allow us to complete umounting and fsck
161 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
162 if (svc) {
163 do {
164 sched_yield(); // do not be so eager, let cleanup have priority
165 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
166 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
167 svc->Start();
168 }
169
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700170 turnOffBacklight();
171
Yusuke Sato0df08272015-07-08 14:57:07 -0700172 int count = 0;
173 while (count++ < UNMOUNT_CHECK_TIMES) {
174 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
175 if (fd >= 0) {
176 /* |entry->mnt_dir| has sucessfully been unmounted. */
177 close(fd);
178 break;
179 } else if (errno == EBUSY) {
180 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
181 * while then retry.
182 */
183 TEMP_FAILURE_RETRY(
184 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
185 continue;
186 } else {
187 /* Cannot open the device. Give up. */
188 return;
189 }
190 }
191
Mark Salyzynad575e02016-04-05 08:10:25 -0700192 // NB: With watchdog still running, there is no cap on the time it takes
193 // to complete the fsck, from the users perspective the device graphics
194 // and responses are locked-up and they may choose to hold the power
195 // button in frustration if it drags out.
196
Yusuke Sato0df08272015-07-08 14:57:07 -0700197 int st;
198 if (!strcmp(entry->mnt_type, "f2fs")) {
199 const char *f2fs_argv[] = {
200 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
201 };
202 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700203 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700204 } else if (!strcmp(entry->mnt_type, "ext4")) {
205 const char *ext4_argv[] = {
206 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
207 };
208 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700209 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700210 }
211}
212
Tom Cherryb7349902015-08-26 11:43:36 -0700213static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700214 /* Starting a class does not start services
215 * which are explicitly disabled. They must
216 * be started individually.
217 */
Tom Cherrybac32992015-07-31 12:45:25 -0700218 ServiceManager::GetInstance().
219 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700220 return 0;
221}
222
Tom Cherryb7349902015-08-26 11:43:36 -0700223static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700224 ServiceManager::GetInstance().
225 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700226 return 0;
227}
228
Tom Cherryb7349902015-08-26 11:43:36 -0700229static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700230 ServiceManager::GetInstance().
231 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800232 return 0;
233}
234
Tom Cherryb7349902015-08-26 11:43:36 -0700235static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700236 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237}
238
Tom Cherryb7349902015-08-26 11:43:36 -0700239static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700240 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
241 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700242 return -1;
243 }
Tom Cherrybac32992015-07-31 12:45:25 -0700244 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700245}
246
Tom Cherryb7349902015-08-26 11:43:36 -0700247static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700248 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
249 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800250 return -1;
251 }
Tom Cherrybac32992015-07-31 12:45:25 -0700252 if (!svc->Start()) {
253 return -1;
254 }
255 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800256 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700257}
258
Tom Cherryb7349902015-08-26 11:43:36 -0700259static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700260 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700261}
262
Tom Cherryb7349902015-08-26 11:43:36 -0700263static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700264 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700265}
266
Tom Cherryb7349902015-08-26 11:43:36 -0700267static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700268 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700269}
270
Tom Cherryb7349902015-08-26 11:43:36 -0700271static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700272 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800273
Tom Cherry96f67312015-07-30 13:52:55 -0700274 if (args.size() > 2) {
275 options += args[2];
276 for (std::size_t i = 3; i < args.size(); ++i) {
277 options += ' ';
278 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800279 }
280 }
281
Tom Cherry96f67312015-07-30 13:52:55 -0700282 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800283}
284
Tom Cherryb7349902015-08-26 11:43:36 -0700285static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700286 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700287 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700288
289 /* mkdir <path> [mode] [owner] [group] */
290
Tom Cherry96f67312015-07-30 13:52:55 -0700291 if (args.size() >= 3) {
292 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700293 }
294
Tom Cherry96f67312015-07-30 13:52:55 -0700295 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700296 /* chmod in case the directory already exists */
297 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700298 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700299 }
300 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301 return -errno;
302 }
303
Tom Cherry96f67312015-07-30 13:52:55 -0700304 if (args.size() >= 4) {
305 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700306 gid_t gid = -1;
307
Tom Cherry96f67312015-07-30 13:52:55 -0700308 if (args.size() == 5) {
309 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700310 }
311
Tom Cherry96f67312015-07-30 13:52:55 -0700312 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700313 return -errno;
314 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700315
316 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
317 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700318 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700319 if (ret == -1) {
320 return -errno;
321 }
322 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700323 }
324
Tom Cherry96f67312015-07-30 13:52:55 -0700325 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700326}
327
328static struct {
329 const char *name;
330 unsigned flag;
331} mount_flags[] = {
332 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200333 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700334 { "nosuid", MS_NOSUID },
335 { "nodev", MS_NODEV },
336 { "nodiratime", MS_NODIRATIME },
337 { "ro", MS_RDONLY },
338 { "rw", 0 },
339 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700340 { "bind", MS_BIND },
341 { "rec", MS_REC },
342 { "unbindable", MS_UNBINDABLE },
343 { "private", MS_PRIVATE },
344 { "slave", MS_SLAVE },
345 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700346 { "defaults", 0 },
347 { 0, 0 },
348};
349
Ken Sumrall752923c2010-12-03 16:33:31 -0800350#define DATA_MNT_POINT "/data"
351
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700352/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700353static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700354 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700355 const char *source, *target, *system;
356 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700357 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700358 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700359 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700360 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361
Tom Cherry96f67312015-07-30 13:52:55 -0700362 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700364 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700365 flags |= mount_flags[i].flag;
366 break;
367 }
368 }
369
Colin Crosscd0f1732010-04-19 17:10:24 -0700370 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700371 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700372 wait = 1;
373 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700374 else if (na + 1 == args.size())
375 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700376 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700377 }
378
Tom Cherry96f67312015-07-30 13:52:55 -0700379 system = args[1].c_str();
380 source = args[2].c_str();
381 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000382
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700383 if (!strncmp(source, "mtd@", 4)) {
384 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000385 if (n < 0) {
386 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700387 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000388
Yabin Cuie2d63af2015-02-17 19:27:51 -0800389 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000390
Colin Crosscd0f1732010-04-19 17:10:24 -0700391 if (wait)
392 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000393 if (mount(tmp, target, system, flags, options) < 0) {
394 return -1;
395 }
396
Ken Sumralldd4d7862011-02-17 18:09:47 -0800397 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000398 } else if (!strncmp(source, "loop@", 5)) {
399 int mode, loop, fd;
400 struct loop_info info;
401
402 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800403 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000404 if (fd < 0) {
405 return -1;
406 }
407
408 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800409 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800410 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000411 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100412 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000413 return -1;
414 }
415
416 /* if it is a blank loop device */
417 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
418 /* if it becomes our loop device */
419 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
420 close(fd);
421
422 if (mount(tmp, target, system, flags, options) < 0) {
423 ioctl(loop, LOOP_CLR_FD, 0);
424 close(loop);
425 return -1;
426 }
427
428 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800429 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000430 }
431 }
432
433 close(loop);
434 }
435
436 close(fd);
437 ERROR("out of loopback devices");
438 return -1;
439 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700440 if (wait)
441 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000442 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700443 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000444 }
445
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700446 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800447
448exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800449 return 0;
450
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700451}
452
Tom Cherryb7349902015-08-26 11:43:36 -0700453static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700454 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800455 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700456 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700457 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
458 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700459 close(fd);
460 } else {
461 ERROR("could not open /cache/recovery/command\n");
462 return -1;
463 }
464 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
465 while (1) { pause(); } // never reached
466}
467
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800468/* Imports .rc files from the specified paths. Default ones are applied if none is given.
469 *
470 * start_index: index of the first path in the args list
471 */
472static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700473 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800474 if (args.size() <= start_index) {
475 // Use the default set if no path is given
476 static const std::vector<std::string> init_directories = {
477 "/system/etc/init",
478 "/vendor/etc/init",
479 "/odm/etc/init"
480 };
481
482 for (const auto& dir : init_directories) {
483 parser.ParseConfig(dir);
484 }
485 } else {
486 for (size_t i = start_index; i < args.size(); ++i) {
487 parser.ParseConfig(args[i]);
488 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700489 }
490}
491
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800492/* mount_all <fstab> [ <path> ]*
493 *
JP Abgrallcee20682014-07-02 14:26:54 -0700494 * This function might request a reboot, in which case it will
495 * not return.
496 */
Tom Cherryb7349902015-08-26 11:43:36 -0700497static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700498 pid_t pid;
499 int ret = -1;
500 int child_ret = -1;
501 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800502 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700503
Tom Cherry96f67312015-07-30 13:52:55 -0700504 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700505 /*
506 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
507 * do the call in the child to provide protection to the main init
508 * process if anything goes wrong (crash or memory leak), and wait for
509 * the child to finish in the parent.
510 */
511 pid = fork();
512 if (pid > 0) {
513 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700514 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
515 if (wp_ret < 0) {
516 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700517 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700518 }
519
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700520 if (WIFEXITED(status)) {
521 ret = WEXITSTATUS(status);
522 } else {
523 ret = -1;
524 }
525 } else if (pid == 0) {
526 /* child, call fs_mgr_mount_all() */
527 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800528 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800529 child_ret = fs_mgr_mount_all(fstab);
530 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700531 if (child_ret == -1) {
532 ERROR("fs_mgr_mount_all returned an error\n");
533 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700534 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700535 } else {
536 /* fork failed, return an error */
537 return -1;
538 }
539
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800540 /* Paths of .rc files are specified at the 2nd argument and beyond */
541 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700542
JP Abgrallf22b7452014-07-02 13:16:04 -0700543 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800544 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700545 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700546 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000547 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800548 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700549 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700550 property_set("ro.crypto.state", "unencrypted");
551 /* If fs_mgr determined this is an unencrypted device, then trigger
552 * that action.
553 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700554 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700555 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
556 /* Setup a wipe via recovery, and reboot into recovery */
557 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
558 ret = wipe_data_via_recovery();
559 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000560 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000561 if (e4crypt_install_keyring()) {
562 return -1;
563 }
564 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000565 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000566
567 // Although encrypted, we have device key, so we do not need to
568 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700569 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000570 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
571 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 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700577 } else if (ret > 0) {
578 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700579 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700580 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700581
582 return ret;
583}
584
Tom Cherryb7349902015-08-26 11:43:36 -0700585static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700586 struct fstab *fstab;
587 int ret;
588
Tom Cherry96f67312015-07-30 13:52:55 -0700589 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700590 ret = fs_mgr_swapon_all(fstab);
591 fs_mgr_free_fstab(fstab);
592
593 return ret;
594}
595
Tom Cherryb7349902015-08-26 11:43:36 -0700596static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700597 const char* name = args[1].c_str();
598 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700599 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700600 return 0;
601}
602
Tom Cherryb7349902015-08-26 11:43:36 -0700603static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700604 struct rlimit limit;
605 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700606 resource = std::stoi(args[1]);
607 limit.rlim_cur = std::stoi(args[2]);
608 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700609 return setrlimit(resource, &limit);
610}
611
Tom Cherryb7349902015-08-26 11:43:36 -0700612static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700613 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
614 if (!svc) {
615 ERROR("do_start: Service %s not found\n", args[1].c_str());
616 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700617 }
Tom Cherrybac32992015-07-31 12:45:25 -0700618 if (!svc->Start())
619 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700620 return 0;
621}
622
Tom Cherryb7349902015-08-26 11:43:36 -0700623static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700624 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
625 if (!svc) {
626 ERROR("do_stop: Service %s not found\n", args[1].c_str());
627 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700628 }
Tom Cherrybac32992015-07-31 12:45:25 -0700629 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700630 return 0;
631}
632
Tom Cherryb7349902015-08-26 11:43:36 -0700633static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700634 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
635 if (!svc) {
636 ERROR("do_restart: Service %s not found\n", args[1].c_str());
637 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700638 }
Tom Cherrybac32992015-07-31 12:45:25 -0700639 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700640 return 0;
641}
642
Tom Cherryb7349902015-08-26 11:43:36 -0700643static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700644 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700645 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700646 unsigned int cmd = 0;
647 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700648 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700649
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700650 if (strncmp(command, "shutdown", 8) == 0) {
651 cmd = ANDROID_RB_POWEROFF;
652 len = 8;
653 } else if (strncmp(command, "reboot", 6) == 0) {
654 cmd = ANDROID_RB_RESTART2;
655 len = 6;
656 } else {
657 ERROR("powerctl: unrecognized command '%s'\n", command);
658 return -EINVAL;
659 }
660
661 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700662 if (cmd == ANDROID_RB_POWEROFF &&
663 !strcmp(&command[len + 1], "userrequested")) {
664 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
665 // Run fsck once the file system is remounted in read-only mode.
666 callback_on_ro_remount = unmount_and_fsck;
667 } else if (cmd == ANDROID_RB_RESTART2) {
668 reboot_target = &command[len + 1];
669 }
670 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700671 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
672 return -EINVAL;
673 }
674
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800675 std::string timeout = property_get("ro.build.shutdown_timeout");
676 unsigned int delay = 0;
677
678 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
679 Timer t;
680 // Ask all services to terminate.
681 ServiceManager::GetInstance().ForEachService(
682 [] (Service* s) { s->Terminate(); });
683
684 while (t.duration() < delay) {
685 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
686
687 int service_count = 0;
688 ServiceManager::GetInstance().ForEachService(
689 [&service_count] (Service* s) {
690 // Count the number of services running.
691 // Exclude the console as it will ignore the SIGTERM signal
692 // and not exit.
693 // Note: SVC_CONSOLE actually means "requires console" but
694 // it is only used by the shell.
695 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
696 service_count++;
697 }
698 });
699
700 if (service_count == 0) {
701 // All terminable services terminated. We can exit early.
702 break;
703 }
704
705 // Wait a bit before recounting the number or running services.
706 usleep(kTerminateServiceDelayMicroSeconds);
707 }
708 NOTICE("Terminating running services took %.02f seconds", t.duration());
709 }
710
Yusuke Sato0df08272015-07-08 14:57:07 -0700711 return android_reboot_with_callback(cmd, 0, reboot_target,
712 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700713}
714
Tom Cherryb7349902015-08-26 11:43:36 -0700715static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700716 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700717 return 0;
718}
719
Tom Cherryb7349902015-08-26 11:43:36 -0700720static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700721 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700722}
723
Tom Cherryb7349902015-08-26 11:43:36 -0700724static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700725 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800726}
727
Tom Cherryb7349902015-08-26 11:43:36 -0700728static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700729 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800730}
731
Tom Cherryb7349902015-08-26 11:43:36 -0700732static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800733 struct timezone tz;
734
The Android Open Source Project35237d12008-12-17 18:08:08 -0800735 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700736 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800737 if (settimeofday(NULL, &tz))
738 return -1;
739 return 0;
740}
741
Tom Cherryb7349902015-08-26 11:43:36 -0700742static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700743 int mode = -1;
744 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000745 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700746 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000747 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700748 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000749}
750
Tom Cherryb7349902015-08-26 11:43:36 -0700751static void verity_update_property(fstab_rec *fstab, const char *mount_point,
752 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100753 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
754 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000755}
756
Tom Cherryb7349902015-08-26 11:43:36 -0700757static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700758 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000759}
760
Tom Cherryb7349902015-08-26 11:43:36 -0700761static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700762 const char* path = args[1].c_str();
763 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700764 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765}
766
Tom Cherryb7349902015-08-26 11:43:36 -0700767static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700768 char *buffer = NULL;
769 int rc = 0;
770 int fd1 = -1, fd2 = -1;
771 struct stat info;
772 int brtw, brtr;
773 char *p;
774
Tom Cherry96f67312015-07-30 13:52:55 -0700775 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700776 return -1;
777
Tom Cherry96f67312015-07-30 13:52:55 -0700778 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700779 goto out_err;
780
Tom Cherry96f67312015-07-30 13:52:55 -0700781 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700782 goto out_err;
783
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800784 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700785 goto out_err;
786
787 p = buffer;
788 brtr = info.st_size;
789 while(brtr) {
790 rc = read(fd1, p, brtr);
791 if (rc < 0)
792 goto out_err;
793 if (rc == 0)
794 break;
795 p += rc;
796 brtr -= rc;
797 }
798
799 p = buffer;
800 brtw = info.st_size;
801 while(brtw) {
802 rc = write(fd2, p, brtw);
803 if (rc < 0)
804 goto out_err;
805 if (rc == 0)
806 break;
807 p += rc;
808 brtw -= rc;
809 }
810
811 rc = 0;
812 goto out;
813out_err:
814 rc = -1;
815out:
816 if (buffer)
817 free(buffer);
818 if (fd1 >= 0)
819 close(fd1);
820 if (fd2 >= 0)
821 close(fd2);
822 return rc;
823}
824
Tom Cherryb7349902015-08-26 11:43:36 -0700825static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700826 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700827 if (args.size() == 3) {
828 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700829 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700830 } else if (args.size() == 4) {
831 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
832 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700833 return -errno;
834 } else {
835 return -1;
836 }
837 return 0;
838}
839
840static mode_t get_mode(const char *s) {
841 mode_t mode = 0;
842 while (*s) {
843 if (*s >= '0' && *s <= '7') {
844 mode = (mode<<3) | (*s-'0');
845 } else {
846 return -1;
847 }
848 s++;
849 }
850 return mode;
851}
852
Tom Cherryb7349902015-08-26 11:43:36 -0700853static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700854 mode_t mode = get_mode(args[1].c_str());
855 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700856 return -errno;
857 }
858 return 0;
859}
860
Tom Cherryb7349902015-08-26 11:43:36 -0700861static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400862 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500863
Tom Cherry96f67312015-07-30 13:52:55 -0700864 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
865 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400866 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500867 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400868 return ret;
869}
870
Tom Cherryb7349902015-08-26 11:43:36 -0700871static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400872 int ret = 0;
873
Tom Cherry96f67312015-07-30 13:52:55 -0700874 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
875 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400876 ret = -errno;
877 }
878 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500879}
880
Tom Cherryb7349902015-08-26 11:43:36 -0700881static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700882 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700883 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
884 ERROR("loglevel: invalid log level'%d'\n", log_level);
885 return -EINVAL;
886 }
887 klog_set_level(log_level);
888 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700889}
890
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700891static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700892 load_persist_props();
893 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800894}
895
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700896static int do_load_system_props(const std::vector<std::string>& args) {
897 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700898 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700899}
900
Tom Cherryb7349902015-08-26 11:43:36 -0700901static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700902 if (args.size() == 2) {
903 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
904 } else if (args.size() == 3) {
905 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800906 } else
907 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700908}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000909
Paul Lawrence806d10b2015-04-28 22:07:10 +0000910/*
911 * Callback to make a directory from the ext4 code
912 */
Tom Cherryb7349902015-08-26 11:43:36 -0700913static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000914 if (make_dir(dir, 0700) && errno != EEXIST) {
915 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000916 }
917
Paul Lawrence806d10b2015-04-28 22:07:10 +0000918 return 0;
919}
920
Paul Crowley749af8c2015-05-28 17:35:06 +0100921static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700922 std::string value = property_get("ro.crypto.type");
923 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100924}
925
Tom Cherryb7349902015-08-26 11:43:36 -0700926static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100927 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000928 return 0;
929 }
Tom Cherry96f67312015-07-30 13:52:55 -0700930 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000931 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000932}
Paul Crowley749af8c2015-05-28 17:35:06 +0100933
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700934static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100935 if (!is_file_crypto()) {
936 return 0;
937 }
Tom Cherry087cd352015-08-03 14:19:35 -0700938 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100939}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700940
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}},
957 {"insmod", {1, kMax, do_insmod}},
958 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700959 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700960 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700961 {"loglevel", {1, 1, do_loglevel}},
962 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800963 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700964 {"mount", {3, kMax, do_mount}},
965 {"powerctl", {1, 1, do_powerctl}},
966 {"restart", {1, 1, do_restart}},
967 {"restorecon", {1, kMax, do_restorecon}},
968 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
969 {"rm", {1, 1, do_rm}},
970 {"rmdir", {1, 1, do_rmdir}},
971 {"setprop", {2, 2, do_setprop}},
972 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700973 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700974 {"start", {1, 1, do_start}},
975 {"stop", {1, 1, do_stop}},
976 {"swapon_all", {1, 1, do_swapon_all}},
977 {"symlink", {2, 2, do_symlink}},
978 {"sysclktz", {1, 1, do_sysclktz}},
979 {"trigger", {1, 1, do_trigger}},
980 {"verity_load_state", {0, 0, do_verity_load_state}},
981 {"verity_update_state", {0, 0, do_verity_update_state}},
982 {"wait", {1, 2, do_wait}},
983 {"write", {2, 2, do_write}},
984 };
985 return builtin_functions;
986}