blob: 95e4c8804e111d68a2785bb7927159edce0b8685 [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#include <private/android_filesystem_config.h>
52
Tom Cherryfa0c21c2015-07-23 17:53:11 -070053#include "action.h"
Tom Cherryb7349902015-08-26 11:43:36 -070054#include "bootchart.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055#include "devices.h"
Tom Cherrybac32992015-07-31 12:45:25 -070056#include "init.h"
Colin Cross6310a822010-04-20 14:29:05 -070057#include "init_parser.h"
Colin Crossed8a7d82010-04-19 17:05:34 -070058#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070059#include "property_service.h"
60#include "service.h"
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -080061#include "signal_handler.h"
Tom Cherrybac32992015-07-31 12:45:25 -070062#include "util.h"
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070063
Nick Kralevichbc609542015-01-31 21:39:46 -080064#define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW
Yusuke Sato0df08272015-07-08 14:57:07 -070065#define UNMOUNT_CHECK_MS 5000
66#define UNMOUNT_CHECK_TIMES 10
Nick Kralevichbc609542015-01-31 21:39:46 -080067
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) {
Nick Kralevich124a9c92016-03-27 16:55:59 -070071 int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
72 if (fd == -1) {
73 ERROR("insmod: open(\"%s\") failed: %s", filename, strerror(errno));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070074 return -1;
Elliott Hughesf682b472015-02-06 12:19:48 -080075 }
Nick Kralevich124a9c92016-03-27 16:55:59 -070076 int rc = syscall(__NR_finit_module, fd, options, 0);
77 if (rc == -1) {
78 ERROR("finit_module for \"%s\" failed: %s", filename, strerror(errno));
79 }
80 close(fd);
81 return rc;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070082}
83
Tom Cherryb7349902015-08-26 11:43:36 -070084static int __ifupdown(const char *interface, int up) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070085 struct ifreq ifr;
86 int s, ret;
87
88 strlcpy(ifr.ifr_name, interface, IFNAMSIZ);
89
90 s = socket(AF_INET, SOCK_DGRAM, 0);
91 if (s < 0)
92 return -1;
93
94 ret = ioctl(s, SIOCGIFFLAGS, &ifr);
95 if (ret < 0) {
96 goto done;
97 }
98
99 if (up)
100 ifr.ifr_flags |= IFF_UP;
101 else
102 ifr.ifr_flags &= ~IFF_UP;
103
104 ret = ioctl(s, SIOCSIFFLAGS, &ifr);
Elliott Hughes24627902015-02-04 10:25:09 -0800105
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700106done:
107 close(s);
108 return ret;
109}
110
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700111// Turn off backlight while we are performing power down cleanup activities.
112static void turnOffBacklight() {
113 static const char off[] = "0";
114
115 android::base::WriteStringToFile(off, "/sys/class/leds/lcd-backlight/brightness");
116
117 static const char backlightDir[] = "/sys/class/backlight";
118 std::unique_ptr<DIR, int(*)(DIR*)> dir(opendir(backlightDir), closedir);
119 if (!dir) {
120 return;
121 }
122
123 struct dirent *dp;
124 while ((dp = readdir(dir.get())) != NULL) {
125 if (((dp->d_type != DT_DIR) && (dp->d_type != DT_LNK)) ||
126 (dp->d_name[0] == '.')) {
127 continue;
128 }
129
130 std::string fileName = android::base::StringPrintf("%s/%s/brightness",
131 backlightDir,
132 dp->d_name);
133 android::base::WriteStringToFile(off, fileName);
134 }
135}
136
Tom Cherryb7349902015-08-26 11:43:36 -0700137static void unmount_and_fsck(const struct mntent *entry) {
Yusuke Sato0df08272015-07-08 14:57:07 -0700138 if (strcmp(entry->mnt_type, "f2fs") && strcmp(entry->mnt_type, "ext4"))
139 return;
140
141 /* First, lazily unmount the directory. This unmount request finishes when
142 * all processes that open a file or directory in |entry->mnt_dir| exit.
143 */
144 TEMP_FAILURE_RETRY(umount2(entry->mnt_dir, MNT_DETACH));
145
146 /* Next, kill all processes except init, kthreadd, and kthreadd's
147 * children to finish the lazy unmount. Killing all processes here is okay
148 * because this callback function is only called right before reboot().
149 * It might be cleaner to selectively kill processes that actually use
150 * |entry->mnt_dir| rather than killing all, probably by reusing a function
151 * like killProcessesWithOpenFiles() in vold/, but the selinux policy does
152 * not allow init to scan /proc/<pid> files which the utility function
153 * heavily relies on. The policy does not allow the process to execute
154 * killall/pkill binaries either. Note that some processes might
155 * automatically restart after kill(), but that is not really a problem
156 * because |entry->mnt_dir| is no longer visible to such new processes.
157 */
Tom Cherrybac32992015-07-31 12:45:25 -0700158 ServiceManager::GetInstance().ForEachService([] (Service* s) { s->Stop(); });
Yusuke Sato0df08272015-07-08 14:57:07 -0700159 TEMP_FAILURE_RETRY(kill(-1, SIGKILL));
160
Mark Salyzynad575e02016-04-05 08:10:25 -0700161 // Restart Watchdogd to allow us to complete umounting and fsck
162 Service *svc = ServiceManager::GetInstance().FindServiceByName("watchdogd");
163 if (svc) {
164 do {
165 sched_yield(); // do not be so eager, let cleanup have priority
166 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
167 } while (svc->flags() & SVC_RUNNING); // Paranoid Cargo
168 svc->Start();
169 }
170
Mark Salyzyna98cc9c2016-04-05 13:43:40 -0700171 turnOffBacklight();
172
Yusuke Sato0df08272015-07-08 14:57:07 -0700173 int count = 0;
174 while (count++ < UNMOUNT_CHECK_TIMES) {
175 int fd = TEMP_FAILURE_RETRY(open(entry->mnt_fsname, O_RDONLY | O_EXCL));
176 if (fd >= 0) {
177 /* |entry->mnt_dir| has sucessfully been unmounted. */
178 close(fd);
179 break;
180 } else if (errno == EBUSY) {
181 /* Some processes using |entry->mnt_dir| are still alive. Wait for a
182 * while then retry.
183 */
184 TEMP_FAILURE_RETRY(
185 usleep(UNMOUNT_CHECK_MS * 1000 / UNMOUNT_CHECK_TIMES));
186 continue;
187 } else {
188 /* Cannot open the device. Give up. */
189 return;
190 }
191 }
192
Mark Salyzynad575e02016-04-05 08:10:25 -0700193 // NB: With watchdog still running, there is no cap on the time it takes
194 // to complete the fsck, from the users perspective the device graphics
195 // and responses are locked-up and they may choose to hold the power
196 // button in frustration if it drags out.
197
Yusuke Sato0df08272015-07-08 14:57:07 -0700198 int st;
199 if (!strcmp(entry->mnt_type, "f2fs")) {
200 const char *f2fs_argv[] = {
201 "/system/bin/fsck.f2fs", "-f", entry->mnt_fsname,
202 };
203 android_fork_execvp_ext(ARRAY_SIZE(f2fs_argv), (char **)f2fs_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700204 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700205 } else if (!strcmp(entry->mnt_type, "ext4")) {
206 const char *ext4_argv[] = {
207 "/system/bin/e2fsck", "-f", "-y", entry->mnt_fsname,
208 };
209 android_fork_execvp_ext(ARRAY_SIZE(ext4_argv), (char **)ext4_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700210 &st, true, LOG_KLOG, true, NULL, NULL, 0);
Yusuke Sato0df08272015-07-08 14:57:07 -0700211 }
212}
213
Tom Cherryb7349902015-08-26 11:43:36 -0700214static int do_class_start(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700215 /* Starting a class does not start services
216 * which are explicitly disabled. They must
217 * be started individually.
218 */
Tom Cherrybac32992015-07-31 12:45:25 -0700219 ServiceManager::GetInstance().
220 ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700221 return 0;
222}
223
Tom Cherryb7349902015-08-26 11:43:36 -0700224static int do_class_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700225 ServiceManager::GetInstance().
226 ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); });
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227 return 0;
228}
229
Tom Cherryb7349902015-08-26 11:43:36 -0700230static int do_class_reset(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700231 ServiceManager::GetInstance().
232 ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); });
Ken Sumrall752923c2010-12-03 16:33:31 -0800233 return 0;
234}
235
Tom Cherryb7349902015-08-26 11:43:36 -0700236static int do_domainname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700237 return write_file("/proc/sys/kernel/domainname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700238}
239
Tom Cherryb7349902015-08-26 11:43:36 -0700240static int do_enable(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700241 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
242 if (!svc) {
JP Abgrall3beec7e2014-05-02 21:14:29 -0700243 return -1;
244 }
Tom Cherrybac32992015-07-31 12:45:25 -0700245 return svc->Enable();
JP Abgrall3beec7e2014-05-02 21:14:29 -0700246}
247
Tom Cherryb7349902015-08-26 11:43:36 -0700248static int do_exec(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700249 Service* svc = ServiceManager::GetInstance().MakeExecOneshotService(args);
250 if (!svc) {
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800251 return -1;
252 }
Tom Cherrybac32992015-07-31 12:45:25 -0700253 if (!svc->Start()) {
254 return -1;
255 }
256 waiting_for_exec = true;
Elliott Hughes8d82ea02015-02-06 20:15:18 -0800257 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700258}
259
Tom Cherryb7349902015-08-26 11:43:36 -0700260static int do_export(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700261 return add_environment(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700262}
263
Tom Cherryb7349902015-08-26 11:43:36 -0700264static int do_hostname(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700265 return write_file("/proc/sys/kernel/hostname", args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700266}
267
Tom Cherryb7349902015-08-26 11:43:36 -0700268static int do_ifup(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700269 return __ifupdown(args[1].c_str(), 1);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700270}
271
Tom Cherryb7349902015-08-26 11:43:36 -0700272static int do_insmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700273 std::string options;
The Android Open Source Project35237d12008-12-17 18:08:08 -0800274
Tom Cherry96f67312015-07-30 13:52:55 -0700275 if (args.size() > 2) {
276 options += args[2];
277 for (std::size_t i = 3; i < args.size(); ++i) {
278 options += ' ';
279 options += args[i];
The Android Open Source Project35237d12008-12-17 18:08:08 -0800280 }
281 }
282
Tom Cherry96f67312015-07-30 13:52:55 -0700283 return insmod(args[1].c_str(), options.c_str());
The Android Open Source Project35237d12008-12-17 18:08:08 -0800284}
285
Tom Cherryb7349902015-08-26 11:43:36 -0700286static int do_mkdir(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700287 mode_t mode = 0755;
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700288 int ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700289
290 /* mkdir <path> [mode] [owner] [group] */
291
Tom Cherry96f67312015-07-30 13:52:55 -0700292 if (args.size() >= 3) {
293 mode = std::stoul(args[2], 0, 8);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700294 }
295
Tom Cherry96f67312015-07-30 13:52:55 -0700296 ret = make_dir(args[1].c_str(), mode);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700297 /* chmod in case the directory already exists */
298 if (ret == -1 && errno == EEXIST) {
Tom Cherry96f67312015-07-30 13:52:55 -0700299 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Chia-chi Yeh27164dc2011-07-08 12:57:36 -0700300 }
301 if (ret == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302 return -errno;
303 }
304
Tom Cherry96f67312015-07-30 13:52:55 -0700305 if (args.size() >= 4) {
306 uid_t uid = decode_uid(args[3].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307 gid_t gid = -1;
308
Tom Cherry96f67312015-07-30 13:52:55 -0700309 if (args.size() == 5) {
310 gid = decode_uid(args[4].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700311 }
312
Tom Cherry96f67312015-07-30 13:52:55 -0700313 if (lchown(args[1].c_str(), uid, gid) == -1) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700314 return -errno;
315 }
Benoit Goby5c8574b2012-08-14 15:43:46 -0700316
317 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
318 if (mode & (S_ISUID | S_ISGID)) {
Tom Cherry96f67312015-07-30 13:52:55 -0700319 ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW);
Benoit Goby5c8574b2012-08-14 15:43:46 -0700320 if (ret == -1) {
321 return -errno;
322 }
323 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700324 }
325
Tom Cherry96f67312015-07-30 13:52:55 -0700326 return e4crypt_set_directory_policy(args[1].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700327}
328
329static struct {
330 const char *name;
331 unsigned flag;
332} mount_flags[] = {
333 { "noatime", MS_NOATIME },
Lars Svenssonb6ee25e2011-07-14 13:39:09 +0200334 { "noexec", MS_NOEXEC },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700335 { "nosuid", MS_NOSUID },
336 { "nodev", MS_NODEV },
337 { "nodiratime", MS_NODIRATIME },
338 { "ro", MS_RDONLY },
339 { "rw", 0 },
340 { "remount", MS_REMOUNT },
Jeff Sharkeye50ac5f2012-08-14 11:34:34 -0700341 { "bind", MS_BIND },
342 { "rec", MS_REC },
343 { "unbindable", MS_UNBINDABLE },
344 { "private", MS_PRIVATE },
345 { "slave", MS_SLAVE },
346 { "shared", MS_SHARED },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700347 { "defaults", 0 },
348 { 0, 0 },
349};
350
Ken Sumrall752923c2010-12-03 16:33:31 -0800351#define DATA_MNT_POINT "/data"
352
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700353/* mount <type> <device> <path> <flags ...> <options> */
Tom Cherryb7349902015-08-26 11:43:36 -0700354static int do_mount(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700355 char tmp[64];
Tom Cherry96f67312015-07-30 13:52:55 -0700356 const char *source, *target, *system;
357 const char *options = NULL;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358 unsigned flags = 0;
Tom Cherry96f67312015-07-30 13:52:55 -0700359 std::size_t na = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360 int n, i;
Colin Crosscd0f1732010-04-19 17:10:24 -0700361 int wait = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700362
Tom Cherry96f67312015-07-30 13:52:55 -0700363 for (na = 4; na < args.size(); na++) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700364 for (i = 0; mount_flags[i].name; i++) {
Tom Cherry96f67312015-07-30 13:52:55 -0700365 if (!args[na].compare(mount_flags[i].name)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700366 flags |= mount_flags[i].flag;
367 break;
368 }
369 }
370
Colin Crosscd0f1732010-04-19 17:10:24 -0700371 if (!mount_flags[i].name) {
Tom Cherry96f67312015-07-30 13:52:55 -0700372 if (!args[na].compare("wait"))
Colin Crosscd0f1732010-04-19 17:10:24 -0700373 wait = 1;
374 /* if our last argument isn't a flag, wolf it up as an option string */
Tom Cherry96f67312015-07-30 13:52:55 -0700375 else if (na + 1 == args.size())
376 options = args[na].c_str();
Colin Crosscd0f1732010-04-19 17:10:24 -0700377 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378 }
379
Tom Cherry96f67312015-07-30 13:52:55 -0700380 system = args[1].c_str();
381 source = args[2].c_str();
382 target = args[3].c_str();
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000383
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700384 if (!strncmp(source, "mtd@", 4)) {
385 n = mtd_name_to_number(source + 4);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000386 if (n < 0) {
387 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700388 }
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000389
Yabin Cuie2d63af2015-02-17 19:27:51 -0800390 snprintf(tmp, sizeof(tmp), "/dev/block/mtdblock%d", n);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000391
Colin Crosscd0f1732010-04-19 17:10:24 -0700392 if (wait)
393 wait_for_file(tmp, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000394 if (mount(tmp, target, system, flags, options) < 0) {
395 return -1;
396 }
397
Ken Sumralldd4d7862011-02-17 18:09:47 -0800398 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000399 } else if (!strncmp(source, "loop@", 5)) {
400 int mode, loop, fd;
401 struct loop_info info;
402
403 mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR;
Nick Kralevich45a884f2015-02-02 14:37:22 -0800404 fd = open(source + 5, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000405 if (fd < 0) {
406 return -1;
407 }
408
409 for (n = 0; ; n++) {
Yabin Cuie2d63af2015-02-17 19:27:51 -0800410 snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800411 loop = open(tmp, mode | O_CLOEXEC);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000412 if (loop < 0) {
Tomasz Kondelbfdcc402014-02-06 08:57:27 +0100413 close(fd);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000414 return -1;
415 }
416
417 /* if it is a blank loop device */
418 if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) {
419 /* if it becomes our loop device */
420 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) {
421 close(fd);
422
423 if (mount(tmp, target, system, flags, options) < 0) {
424 ioctl(loop, LOOP_CLR_FD, 0);
425 close(loop);
426 return -1;
427 }
428
429 close(loop);
Ken Sumralldd4d7862011-02-17 18:09:47 -0800430 goto exit_success;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000431 }
432 }
433
434 close(loop);
435 }
436
437 close(fd);
438 ERROR("out of loopback devices");
439 return -1;
440 } else {
Colin Crosscd0f1732010-04-19 17:10:24 -0700441 if (wait)
442 wait_for_file(source, COMMAND_RETRY_TIMEOUT);
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000443 if (mount(source, target, system, flags, options) < 0) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700444 return -1;
Jay Freeman (saurik)e520d032008-11-20 03:37:30 +0000445 }
446
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700447 }
Ken Sumralldd4d7862011-02-17 18:09:47 -0800448
449exit_success:
Ken Sumralldd4d7862011-02-17 18:09:47 -0800450 return 0;
451
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700452}
453
Tom Cherryb7349902015-08-26 11:43:36 -0700454static int wipe_data_via_recovery() {
JP Abgrallcee20682014-07-02 14:26:54 -0700455 mkdir("/cache/recovery", 0700);
Nick Kralevich45a884f2015-02-02 14:37:22 -0800456 int fd = open("/cache/recovery/command", O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0600);
JP Abgrallcee20682014-07-02 14:26:54 -0700457 if (fd >= 0) {
Jeff Sharkeyd26135b2014-09-24 11:46:36 -0700458 write(fd, "--wipe_data\n", strlen("--wipe_data\n") + 1);
459 write(fd, "--reason=wipe_data_via_recovery\n", strlen("--reason=wipe_data_via_recovery\n") + 1);
JP Abgrallcee20682014-07-02 14:26:54 -0700460 close(fd);
461 } else {
462 ERROR("could not open /cache/recovery/command\n");
463 return -1;
464 }
465 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
466 while (1) { pause(); } // never reached
467}
468
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800469/* Imports .rc files from the specified paths. Default ones are applied if none is given.
470 *
471 * start_index: index of the first path in the args list
472 */
473static void import_late(const std::vector<std::string>& args, size_t start_index) {
Tom Cherryb7349902015-08-26 11:43:36 -0700474 Parser& parser = Parser::GetInstance();
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800475 if (args.size() <= start_index) {
476 // Use the default set if no path is given
477 static const std::vector<std::string> init_directories = {
478 "/system/etc/init",
479 "/vendor/etc/init",
480 "/odm/etc/init"
481 };
482
483 for (const auto& dir : init_directories) {
484 parser.ParseConfig(dir);
485 }
486 } else {
487 for (size_t i = start_index; i < args.size(); ++i) {
488 parser.ParseConfig(args[i]);
489 }
Tom Cherryb8dd0272015-07-22 14:23:33 -0700490 }
491}
492
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800493/* mount_all <fstab> [ <path> ]*
494 *
JP Abgrallcee20682014-07-02 14:26:54 -0700495 * This function might request a reboot, in which case it will
496 * not return.
497 */
Tom Cherryb7349902015-08-26 11:43:36 -0700498static int do_mount_all(const std::vector<std::string>& args) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700499 pid_t pid;
500 int ret = -1;
501 int child_ret = -1;
502 int status;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800503 struct fstab *fstab;
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700504
Tom Cherry96f67312015-07-30 13:52:55 -0700505 const char* fstabfile = args[1].c_str();
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700506 /*
507 * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and
508 * do the call in the child to provide protection to the main init
509 * process if anything goes wrong (crash or memory leak), and wait for
510 * the child to finish in the parent.
511 */
512 pid = fork();
513 if (pid > 0) {
514 /* Parent. Wait for the child to return */
Paul Lawrence40af0922014-09-16 14:31:23 -0700515 int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
516 if (wp_ret < 0) {
517 /* Unexpected error code. We will continue anyway. */
Elliott Hughescd67f002015-03-20 17:05:56 -0700518 NOTICE("waitpid failed rc=%d: %s\n", wp_ret, strerror(errno));
Paul Lawrence40af0922014-09-16 14:31:23 -0700519 }
520
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700521 if (WIFEXITED(status)) {
522 ret = WEXITSTATUS(status);
523 } else {
524 ret = -1;
525 }
526 } else if (pid == 0) {
527 /* child, call fs_mgr_mount_all() */
528 klog_set_level(6); /* So we can see what fs_mgr_mount_all() does */
Nan Liu12df1e12015-07-21 19:44:07 +0800529 fstab = fs_mgr_read_fstab(fstabfile);
Ken Sumrallab6b8522013-02-13 12:58:40 -0800530 child_ret = fs_mgr_mount_all(fstab);
531 fs_mgr_free_fstab(fstab);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700532 if (child_ret == -1) {
533 ERROR("fs_mgr_mount_all returned an error\n");
534 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700535 _exit(child_ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700536 } else {
537 /* fork failed, return an error */
538 return -1;
539 }
540
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800541 /* Paths of .rc files are specified at the 2nd argument and beyond */
542 import_late(args, 2);
Tom Cherryb8dd0272015-07-22 14:23:33 -0700543
JP Abgrallf22b7452014-07-02 13:16:04 -0700544 if (ret == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800545 property_set("vold.decrypt", "trigger_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700546 } else if (ret == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700547 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000548 property_set("ro.crypto.type", "block");
Paul Lawrence13d5bb42014-01-30 10:43:52 -0800549 property_set("vold.decrypt", "trigger_default_encryption");
JP Abgrallf22b7452014-07-02 13:16:04 -0700550 } else if (ret == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700551 property_set("ro.crypto.state", "unencrypted");
552 /* If fs_mgr determined this is an unencrypted device, then trigger
553 * that action.
554 */
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700555 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
JP Abgrallcee20682014-07-02 14:26:54 -0700556 } else if (ret == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) {
557 /* Setup a wipe via recovery, and reboot into recovery */
558 ERROR("fs_mgr_mount_all suggested recovery, so wiping data via recovery.\n");
559 ret = wipe_data_via_recovery();
560 /* If reboot worked, there is no return. */
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000561 } else if (ret == FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000562 if (e4crypt_install_keyring()) {
563 return -1;
564 }
565 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000566 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000567
568 // Although encrypted, we have device key, so we do not need to
569 // do anything different from the nonencrypted case.
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700570 ActionManager::GetInstance().QueueEventTrigger("nonencrypted");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000571 } else if (ret == FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED) {
572 if (e4crypt_install_keyring()) {
573 return -1;
574 }
575 property_set("ro.crypto.state", "encrypted");
Paul Lawrence806d10b2015-04-28 22:07:10 +0000576 property_set("ro.crypto.type", "file");
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000577 property_set("vold.decrypt", "trigger_restart_min_framework");
JP Abgrallcee20682014-07-02 14:26:54 -0700578 } else if (ret > 0) {
579 ERROR("fs_mgr_mount_all returned unexpected error %d\n", ret);
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700580 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700581 /* else ... < 0: error */
Ken Sumrall0e9dd902012-04-17 17:20:16 -0700582
583 return ret;
584}
585
Tom Cherryb7349902015-08-26 11:43:36 -0700586static int do_swapon_all(const std::vector<std::string>& args) {
Ken Sumralla76baaa2013-07-09 18:42:09 -0700587 struct fstab *fstab;
588 int ret;
589
Tom Cherry96f67312015-07-30 13:52:55 -0700590 fstab = fs_mgr_read_fstab(args[1].c_str());
Ken Sumralla76baaa2013-07-09 18:42:09 -0700591 ret = fs_mgr_swapon_all(fstab);
592 fs_mgr_free_fstab(fstab);
593
594 return ret;
595}
596
Tom Cherryb7349902015-08-26 11:43:36 -0700597static int do_setprop(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700598 const char* name = args[1].c_str();
599 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700600 property_set(name, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700601 return 0;
602}
603
Tom Cherryb7349902015-08-26 11:43:36 -0700604static int do_setrlimit(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700605 struct rlimit limit;
606 int resource;
Tom Cherry96f67312015-07-30 13:52:55 -0700607 resource = std::stoi(args[1]);
608 limit.rlim_cur = std::stoi(args[2]);
609 limit.rlim_max = std::stoi(args[3]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700610 return setrlimit(resource, &limit);
611}
612
Tom Cherryb7349902015-08-26 11:43:36 -0700613static int do_start(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700614 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
615 if (!svc) {
616 ERROR("do_start: Service %s not found\n", args[1].c_str());
617 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700618 }
Tom Cherrybac32992015-07-31 12:45:25 -0700619 if (!svc->Start())
620 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700621 return 0;
622}
623
Tom Cherryb7349902015-08-26 11:43:36 -0700624static int do_stop(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700625 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
626 if (!svc) {
627 ERROR("do_stop: Service %s not found\n", args[1].c_str());
628 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700629 }
Tom Cherrybac32992015-07-31 12:45:25 -0700630 svc->Stop();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700631 return 0;
632}
633
Tom Cherryb7349902015-08-26 11:43:36 -0700634static int do_restart(const std::vector<std::string>& args) {
Tom Cherrybac32992015-07-31 12:45:25 -0700635 Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]);
636 if (!svc) {
637 ERROR("do_restart: Service %s not found\n", args[1].c_str());
638 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700639 }
Tom Cherrybac32992015-07-31 12:45:25 -0700640 svc->Restart();
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700641 return 0;
642}
643
Tom Cherryb7349902015-08-26 11:43:36 -0700644static int do_powerctl(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700645 const char* command = args[1].c_str();
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700646 int len = 0;
Yusuke Satof93d4292015-07-21 15:50:59 -0700647 unsigned int cmd = 0;
648 const char *reboot_target = "";
Yusuke Sato0df08272015-07-08 14:57:07 -0700649 void (*callback_on_ro_remount)(const struct mntent*) = NULL;
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700650
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700651 if (strncmp(command, "shutdown", 8) == 0) {
652 cmd = ANDROID_RB_POWEROFF;
653 len = 8;
654 } else if (strncmp(command, "reboot", 6) == 0) {
655 cmd = ANDROID_RB_RESTART2;
656 len = 6;
657 } else {
658 ERROR("powerctl: unrecognized command '%s'\n", command);
659 return -EINVAL;
660 }
661
662 if (command[len] == ',') {
Yusuke Satof93d4292015-07-21 15:50:59 -0700663 if (cmd == ANDROID_RB_POWEROFF &&
664 !strcmp(&command[len + 1], "userrequested")) {
665 // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED.
666 // Run fsck once the file system is remounted in read-only mode.
667 callback_on_ro_remount = unmount_and_fsck;
668 } else if (cmd == ANDROID_RB_RESTART2) {
669 reboot_target = &command[len + 1];
670 }
671 } else if (command[len] != '\0') {
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700672 ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]);
673 return -EINVAL;
674 }
675
Bertrand SIMONNETb7e03e82015-12-18 11:39:59 -0800676 std::string timeout = property_get("ro.build.shutdown_timeout");
677 unsigned int delay = 0;
678
679 if (android::base::ParseUint(timeout.c_str(), &delay) && delay > 0) {
680 Timer t;
681 // Ask all services to terminate.
682 ServiceManager::GetInstance().ForEachService(
683 [] (Service* s) { s->Terminate(); });
684
685 while (t.duration() < delay) {
686 ServiceManager::GetInstance().ReapAnyOutstandingChildren();
687
688 int service_count = 0;
689 ServiceManager::GetInstance().ForEachService(
690 [&service_count] (Service* s) {
691 // Count the number of services running.
692 // Exclude the console as it will ignore the SIGTERM signal
693 // and not exit.
694 // Note: SVC_CONSOLE actually means "requires console" but
695 // it is only used by the shell.
696 if (s->pid() != 0 && (s->flags() & SVC_CONSOLE) == 0) {
697 service_count++;
698 }
699 });
700
701 if (service_count == 0) {
702 // All terminable services terminated. We can exit early.
703 break;
704 }
705
706 // Wait a bit before recounting the number or running services.
707 usleep(kTerminateServiceDelayMicroSeconds);
708 }
709 NOTICE("Terminating running services took %.02f seconds", t.duration());
710 }
711
Yusuke Sato0df08272015-07-08 14:57:07 -0700712 return android_reboot_with_callback(cmd, 0, reboot_target,
713 callback_on_ro_remount);
Nick Kralevichca8e66a2013-04-18 12:20:02 -0700714}
715
Tom Cherryb7349902015-08-26 11:43:36 -0700716static int do_trigger(const std::vector<std::string>& args) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700717 ActionManager::GetInstance().QueueEventTrigger(args[1]);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700718 return 0;
719}
720
Tom Cherryb7349902015-08-26 11:43:36 -0700721static int do_symlink(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700722 return symlink(args[1].c_str(), args[2].c_str());
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700723}
724
Tom Cherryb7349902015-08-26 11:43:36 -0700725static int do_rm(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700726 return unlink(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800727}
728
Tom Cherryb7349902015-08-26 11:43:36 -0700729static int do_rmdir(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700730 return rmdir(args[1].c_str());
Ken Sumrall203bad52011-01-18 17:37:41 -0800731}
732
Tom Cherryb7349902015-08-26 11:43:36 -0700733static int do_sysclktz(const std::vector<std::string>& args) {
The Android Open Source Project35237d12008-12-17 18:08:08 -0800734 struct timezone tz;
735
The Android Open Source Project35237d12008-12-17 18:08:08 -0800736 memset(&tz, 0, sizeof(tz));
Tom Cherry96f67312015-07-30 13:52:55 -0700737 tz.tz_minuteswest = std::stoi(args[1]);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800738 if (settimeofday(NULL, &tz))
739 return -1;
740 return 0;
741}
742
Tom Cherryb7349902015-08-26 11:43:36 -0700743static int do_verity_load_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700744 int mode = -1;
745 int rc = fs_mgr_load_verity_state(&mode);
Sami Tolvanen90f52df2015-12-02 14:23:09 +0000746 if (rc == 0 && mode != VERITY_MODE_DEFAULT) {
Tom Cherryfa0c21c2015-07-23 17:53:11 -0700747 ActionManager::GetInstance().QueueEventTrigger("verity-logging");
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000748 }
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700749 return rc;
Sami Tolvanen8ff01902015-02-16 11:03:34 +0000750}
751
Tom Cherryb7349902015-08-26 11:43:36 -0700752static void verity_update_property(fstab_rec *fstab, const char *mount_point,
753 int mode, int status) {
Sami Tolvanen45474232015-03-30 11:38:38 +0100754 property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(),
755 android::base::StringPrintf("%d", mode).c_str());
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000756}
757
Tom Cherryb7349902015-08-26 11:43:36 -0700758static int do_verity_update_state(const std::vector<std::string>& args) {
Elliott Hughesdb3f2672015-03-20 09:45:18 -0700759 return fs_mgr_update_verity_state(verity_update_property);
Sami Tolvanenacbf9be2015-03-19 10:00:34 +0000760}
761
Tom Cherryb7349902015-08-26 11:43:36 -0700762static int do_write(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700763 const char* path = args[1].c_str();
764 const char* value = args[2].c_str();
Yabin Cui00ede7d2015-07-24 13:26:04 -0700765 return write_file(path, value);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700766}
767
Tom Cherryb7349902015-08-26 11:43:36 -0700768static int do_copy(const std::vector<std::string>& args) {
San Mehat7c44fe52009-08-26 16:39:25 -0700769 char *buffer = NULL;
770 int rc = 0;
771 int fd1 = -1, fd2 = -1;
772 struct stat info;
773 int brtw, brtr;
774 char *p;
775
Tom Cherry96f67312015-07-30 13:52:55 -0700776 if (stat(args[1].c_str(), &info) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700777 return -1;
778
Tom Cherry96f67312015-07-30 13:52:55 -0700779 if ((fd1 = open(args[1].c_str(), O_RDONLY|O_CLOEXEC)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700780 goto out_err;
781
Tom Cherry96f67312015-07-30 13:52:55 -0700782 if ((fd2 = open(args[2].c_str(), O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
San Mehat7c44fe52009-08-26 16:39:25 -0700783 goto out_err;
784
Elliott Hughesf3cf4382015-02-03 17:12:07 -0800785 if (!(buffer = (char*) malloc(info.st_size)))
San Mehat7c44fe52009-08-26 16:39:25 -0700786 goto out_err;
787
788 p = buffer;
789 brtr = info.st_size;
790 while(brtr) {
791 rc = read(fd1, p, brtr);
792 if (rc < 0)
793 goto out_err;
794 if (rc == 0)
795 break;
796 p += rc;
797 brtr -= rc;
798 }
799
800 p = buffer;
801 brtw = info.st_size;
802 while(brtw) {
803 rc = write(fd2, p, brtw);
804 if (rc < 0)
805 goto out_err;
806 if (rc == 0)
807 break;
808 p += rc;
809 brtw -= rc;
810 }
811
812 rc = 0;
813 goto out;
814out_err:
815 rc = -1;
816out:
817 if (buffer)
818 free(buffer);
819 if (fd1 >= 0)
820 close(fd1);
821 if (fd2 >= 0)
822 close(fd2);
823 return rc;
824}
825
Tom Cherryb7349902015-08-26 11:43:36 -0700826static int do_chown(const std::vector<std::string>& args) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700827 /* GID is optional. */
Tom Cherry96f67312015-07-30 13:52:55 -0700828 if (args.size() == 3) {
829 if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700830 return -errno;
Tom Cherry96f67312015-07-30 13:52:55 -0700831 } else if (args.size() == 4) {
832 if (lchown(args[3].c_str(), decode_uid(args[1].c_str()),
833 decode_uid(args[2].c_str())) == -1)
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700834 return -errno;
835 } else {
836 return -1;
837 }
838 return 0;
839}
840
841static mode_t get_mode(const char *s) {
842 mode_t mode = 0;
843 while (*s) {
844 if (*s >= '0' && *s <= '7') {
845 mode = (mode<<3) | (*s-'0');
846 } else {
847 return -1;
848 }
849 s++;
850 }
851 return mode;
852}
853
Tom Cherryb7349902015-08-26 11:43:36 -0700854static int do_chmod(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700855 mode_t mode = get_mode(args[1].c_str());
856 if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700857 return -errno;
858 }
859 return 0;
860}
861
Tom Cherryb7349902015-08-26 11:43:36 -0700862static int do_restorecon(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400863 int ret = 0;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500864
Tom Cherry96f67312015-07-30 13:52:55 -0700865 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
866 if (restorecon(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400867 ret = -errno;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500868 }
Stephen Smalley726e8f72013-10-09 16:02:09 -0400869 return ret;
870}
871
Tom Cherryb7349902015-08-26 11:43:36 -0700872static int do_restorecon_recursive(const std::vector<std::string>& args) {
Stephen Smalley726e8f72013-10-09 16:02:09 -0400873 int ret = 0;
874
Tom Cherry96f67312015-07-30 13:52:55 -0700875 for (auto it = std::next(args.begin()); it != args.end(); ++it) {
876 if (restorecon_recursive(it->c_str()) < 0)
Stephen Smalley726e8f72013-10-09 16:02:09 -0400877 ret = -errno;
878 }
879 return ret;
Stephen Smalleye46f9d52012-01-13 08:48:47 -0500880}
881
Tom Cherryb7349902015-08-26 11:43:36 -0700882static int do_loglevel(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700883 int log_level = std::stoi(args[1]);
Riley Andrews1bbef882014-06-26 13:55:03 -0700884 if (log_level < KLOG_ERROR_LEVEL || log_level > KLOG_DEBUG_LEVEL) {
885 ERROR("loglevel: invalid log level'%d'\n", log_level);
886 return -EINVAL;
887 }
888 klog_set_level(log_level);
889 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700890}
891
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700892static int do_load_persist_props(const std::vector<std::string>& args) {
Tom Cherryb7349902015-08-26 11:43:36 -0700893 load_persist_props();
894 return 0;
Ken Sumrallc5c51032011-03-08 17:01:29 -0800895}
896
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700897static int do_load_system_props(const std::vector<std::string>& args) {
898 load_system_props();
Tom Cherryb7349902015-08-26 11:43:36 -0700899 return 0;
Riley Andrewse4b7b292014-06-16 15:06:21 -0700900}
901
Tom Cherryb7349902015-08-26 11:43:36 -0700902static int do_wait(const std::vector<std::string>& args) {
Tom Cherry96f67312015-07-30 13:52:55 -0700903 if (args.size() == 2) {
904 return wait_for_file(args[1].c_str(), COMMAND_RETRY_TIMEOUT);
905 } else if (args.size() == 3) {
906 return wait_for_file(args[1].c_str(), std::stoi(args[2]));
Patrick McCormick96d0a4d2011-02-04 10:51:39 -0800907 } else
908 return -1;
Colin Crosscd0f1732010-04-19 17:10:24 -0700909}
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000910
Paul Lawrence806d10b2015-04-28 22:07:10 +0000911/*
912 * Callback to make a directory from the ext4 code
913 */
Tom Cherryb7349902015-08-26 11:43:36 -0700914static int do_installkeys_ensure_dir_exists(const char* dir) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000915 if (make_dir(dir, 0700) && errno != EEXIST) {
916 return -1;
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000917 }
918
Paul Lawrence806d10b2015-04-28 22:07:10 +0000919 return 0;
920}
921
Paul Crowley749af8c2015-05-28 17:35:06 +0100922static bool is_file_crypto() {
Yabin Cui0ff85902015-07-24 13:58:03 -0700923 std::string value = property_get("ro.crypto.type");
924 return value == "file";
Paul Crowley749af8c2015-05-28 17:35:06 +0100925}
926
Tom Cherryb7349902015-08-26 11:43:36 -0700927static int do_installkey(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100928 if (!is_file_crypto()) {
Paul Lawrence806d10b2015-04-28 22:07:10 +0000929 return 0;
930 }
Tom Cherry96f67312015-07-30 13:52:55 -0700931 return e4crypt_create_device_key(args[1].c_str(),
Paul Lawrence806d10b2015-04-28 22:07:10 +0000932 do_installkeys_ensure_dir_exists);
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000933}
Paul Crowley749af8c2015-05-28 17:35:06 +0100934
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700935static int do_setusercryptopolicies(const std::vector<std::string>& args) {
Paul Crowley749af8c2015-05-28 17:35:06 +0100936 if (!is_file_crypto()) {
937 return 0;
938 }
Tom Cherry087cd352015-08-03 14:19:35 -0700939 return e4crypt_set_user_crypto_policies(args[1].c_str());
Paul Crowley749af8c2015-05-28 17:35:06 +0100940}
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700941
Tom Cherryb7349902015-08-26 11:43:36 -0700942BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const {
943 constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
944 static const Map builtin_functions = {
945 {"bootchart_init", {0, 0, do_bootchart_init}},
946 {"chmod", {2, 2, do_chmod}},
947 {"chown", {2, 3, do_chown}},
948 {"class_reset", {1, 1, do_class_reset}},
949 {"class_start", {1, 1, do_class_start}},
950 {"class_stop", {1, 1, do_class_stop}},
951 {"copy", {2, 2, do_copy}},
952 {"domainname", {1, 1, do_domainname}},
953 {"enable", {1, 1, do_enable}},
954 {"exec", {1, kMax, do_exec}},
955 {"export", {2, 2, do_export}},
956 {"hostname", {1, 1, do_hostname}},
957 {"ifup", {1, 1, do_ifup}},
958 {"insmod", {1, kMax, do_insmod}},
959 {"installkey", {1, 1, do_installkey}},
Tom Cherryb7349902015-08-26 11:43:36 -0700960 {"load_persist_props", {0, 0, do_load_persist_props}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700961 {"load_system_props", {0, 0, do_load_system_props}},
Tom Cherryb7349902015-08-26 11:43:36 -0700962 {"loglevel", {1, 1, do_loglevel}},
963 {"mkdir", {1, 4, do_mkdir}},
Hung-ying Tyandc738ea2016-01-14 11:18:21 +0800964 {"mount_all", {1, kMax, do_mount_all}},
Tom Cherryb7349902015-08-26 11:43:36 -0700965 {"mount", {3, kMax, do_mount}},
966 {"powerctl", {1, 1, do_powerctl}},
967 {"restart", {1, 1, do_restart}},
968 {"restorecon", {1, kMax, do_restorecon}},
969 {"restorecon_recursive", {1, kMax, do_restorecon_recursive}},
970 {"rm", {1, 1, do_rm}},
971 {"rmdir", {1, 1, do_rmdir}},
972 {"setprop", {2, 2, do_setprop}},
973 {"setrlimit", {3, 3, do_setrlimit}},
Tom Cherryaf20a7c2015-09-01 15:05:34 -0700974 {"setusercryptopolicies", {1, 1, do_setusercryptopolicies}},
Tom Cherryb7349902015-08-26 11:43:36 -0700975 {"start", {1, 1, do_start}},
976 {"stop", {1, 1, do_stop}},
977 {"swapon_all", {1, 1, do_swapon_all}},
978 {"symlink", {2, 2, do_symlink}},
979 {"sysclktz", {1, 1, do_sysclktz}},
980 {"trigger", {1, 1, do_trigger}},
981 {"verity_load_state", {0, 0, do_verity_load_state}},
982 {"verity_update_state", {0, 0, do_verity_update_state}},
983 {"wait", {1, 2, do_wait}},
984 {"write", {2, 2, do_write}},
985 };
986 return builtin_functions;
987}