The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 17 | #include "builtins.h" |
| 18 | |
Mark Salyzyn | a98cc9c | 2016-04-05 13:43:40 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | #include <errno.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 21 | #include <fcntl.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 22 | #include <mntent.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 23 | #include <net/if.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 24 | #include <signal.h> |
Mark Salyzyn | ad575e0 | 2016-04-05 08:10:25 -0700 | [diff] [blame] | 25 | #include <sched.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | #include <stdlib.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 28 | #include <string.h> |
| 29 | #include <sys/socket.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 30 | #include <sys/mount.h> |
| 31 | #include <sys/resource.h> |
Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 32 | #include <sys/syscall.h> |
Elliott Hughes | 3d74d7a | 2015-01-29 21:31:23 -0800 | [diff] [blame] | 33 | #include <sys/time.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 34 | #include <sys/types.h> |
| 35 | #include <sys/stat.h> |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 36 | #include <sys/wait.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 37 | #include <unistd.h> |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 38 | #include <linux/loop.h> |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 39 | #include <linux/module.h> |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 40 | |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 41 | #include <string> |
Elliott Hughes | 290a228 | 2016-11-14 17:08:47 -0800 | [diff] [blame] | 42 | #include <thread> |
| 43 | |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 44 | #include <selinux/android.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 45 | #include <selinux/selinux.h> |
| 46 | #include <selinux/label.h> |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 47 | |
Mark Salyzyn | a98cc9c | 2016-04-05 13:43:40 -0700 | [diff] [blame] | 48 | #include <android-base/file.h> |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 49 | #include <android-base/parseint.h> |
Tom Cherry | e249097 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 50 | #include <android-base/properties.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 51 | #include <android-base/stringprintf.h> |
Tom Cherry | e249097 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 52 | #include <android-base/strings.h> |
Yabin Cui | 0b1252c | 2016-06-24 18:28:03 -0700 | [diff] [blame] | 53 | #include <bootloader_message/bootloader_message.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 54 | #include <cutils/android_reboot.h> |
Tao Bao | 6d881d6 | 2016-10-05 17:53:30 -0700 | [diff] [blame] | 55 | #include <ext4_utils/ext4_crypt.h> |
| 56 | #include <ext4_utils/ext4_crypt_init_extensions.h> |
Tom Cherry | e249097 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 57 | #include <fs_mgr.h> |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 58 | #include <logwrap/logwrap.h> |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 59 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 60 | #include "action.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 61 | #include "bootchart.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | #include "devices.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 63 | #include "init.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 64 | #include "init_parser.h" |
Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 65 | #include "log.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 66 | #include "property_service.h" |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 67 | #include "reboot.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 68 | #include "service.h" |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 69 | #include "signal_handler.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 70 | #include "util.h" |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 71 | |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 72 | using namespace std::literals::string_literals; |
| 73 | |
Nick Kralevich | bc60954 | 2015-01-31 21:39:46 -0800 | [diff] [blame] | 74 | #define chmod DO_NOT_USE_CHMOD_USE_FCHMODAT_SYMLINK_NOFOLLOW |
| 75 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 76 | static constexpr std::chrono::nanoseconds kCommandRetryTimeout = 5s; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 77 | |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 78 | static int insmod(const char *filename, const char *options, int flags) { |
Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 79 | int fd = open(filename, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); |
| 80 | if (fd == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 81 | PLOG(ERROR) << "insmod: open(\"" << filename << "\") failed"; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | return -1; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 83 | } |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 84 | int rc = syscall(__NR_finit_module, fd, options, flags); |
Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 85 | if (rc == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 86 | PLOG(ERROR) << "finit_module for \"" << filename << "\" failed"; |
Nick Kralevich | 124a9c9 | 2016-03-27 16:55:59 -0700 | [diff] [blame] | 87 | } |
| 88 | close(fd); |
| 89 | return rc; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 92 | static int __ifupdown(const char *interface, int up) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | struct ifreq ifr; |
| 94 | int s, ret; |
| 95 | |
| 96 | strlcpy(ifr.ifr_name, interface, IFNAMSIZ); |
| 97 | |
| 98 | s = socket(AF_INET, SOCK_DGRAM, 0); |
| 99 | if (s < 0) |
| 100 | return -1; |
| 101 | |
| 102 | ret = ioctl(s, SIOCGIFFLAGS, &ifr); |
| 103 | if (ret < 0) { |
| 104 | goto done; |
| 105 | } |
| 106 | |
| 107 | if (up) |
| 108 | ifr.ifr_flags |= IFF_UP; |
| 109 | else |
| 110 | ifr.ifr_flags &= ~IFF_UP; |
| 111 | |
| 112 | ret = ioctl(s, SIOCSIFFLAGS, &ifr); |
Elliott Hughes | 2462790 | 2015-02-04 10:25:09 -0800 | [diff] [blame] | 113 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | done: |
| 115 | close(s); |
| 116 | return ret; |
| 117 | } |
| 118 | |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 119 | static int reboot_into_recovery(const std::vector<std::string>& options) { |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 120 | std::string err; |
| 121 | if (!write_bootloader_message(options, &err)) { |
Elliott Hughes | 7f5b29f | 2016-06-27 09:54:25 -0700 | [diff] [blame] | 122 | LOG(ERROR) << "failed to set bootloader message: " << err; |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 123 | return -1; |
| 124 | } |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 125 | DoReboot(ANDROID_RB_RESTART2, "reboot", "recovery", false); |
| 126 | return 0; |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 129 | static int do_class_start(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | /* Starting a class does not start services |
| 131 | * which are explicitly disabled. They must |
| 132 | * be started individually. |
| 133 | */ |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 134 | ServiceManager::GetInstance(). |
| 135 | ForEachServiceInClass(args[1], [] (Service* s) { s->StartIfNotDisabled(); }); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 139 | static int do_class_stop(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 140 | ServiceManager::GetInstance(). |
| 141 | ForEachServiceInClass(args[1], [] (Service* s) { s->Stop(); }); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 145 | static int do_class_reset(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 146 | ServiceManager::GetInstance(). |
| 147 | ForEachServiceInClass(args[1], [] (Service* s) { s->Reset(); }); |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 148 | return 0; |
| 149 | } |
| 150 | |
Steven Moreland | 2b63d54 | 2017-03-10 14:04:37 -0800 | [diff] [blame] | 151 | static int do_class_restart(const std::vector<std::string>& args) { |
| 152 | ServiceManager::GetInstance(). |
| 153 | ForEachServiceInClass(args[1], [] (Service* s) { s->Restart(); }); |
| 154 | return 0; |
| 155 | } |
| 156 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 157 | static int do_domainname(const std::vector<std::string>& args) { |
Jorge Lucangeli Obes | 77f0e9f | 2016-12-28 14:07:02 -0500 | [diff] [blame] | 158 | return write_file("/proc/sys/kernel/domainname", args[1].c_str()) ? 0 : 1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 161 | static int do_enable(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 162 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 163 | if (!svc) { |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 164 | return -1; |
| 165 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 166 | return svc->Enable(); |
JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 169 | static int do_exec(const std::vector<std::string>& args) { |
Tom Cherry | 2d80467 | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 170 | return ServiceManager::GetInstance().Exec(args) ? 0 : -1; |
| 171 | } |
| 172 | |
| 173 | static int do_exec_start(const std::vector<std::string>& args) { |
| 174 | return ServiceManager::GetInstance().ExecStart(args[1]) ? 0 : -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 177 | static int do_export(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 178 | return add_environment(args[1].c_str(), args[2].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 181 | static int do_hostname(const std::vector<std::string>& args) { |
Jorge Lucangeli Obes | 77f0e9f | 2016-12-28 14:07:02 -0500 | [diff] [blame] | 182 | return write_file("/proc/sys/kernel/hostname", args[1].c_str()) ? 0 : 1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 185 | static int do_ifup(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 186 | return __ifupdown(args[1].c_str(), 1); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 189 | static int do_insmod(const std::vector<std::string>& args) { |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 190 | int flags = 0; |
| 191 | auto it = args.begin() + 1; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 192 | |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 193 | if (!(*it).compare("-f")) { |
| 194 | flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS; |
| 195 | it++; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Hung-ying Tyan | bfa6d75 | 2016-05-17 18:49:10 +0800 | [diff] [blame] | 198 | std::string filename = *it++; |
| 199 | std::string options = android::base::Join(std::vector<std::string>(it, args.end()), ' '); |
| 200 | return insmod(filename.c_str(), options.c_str(), flags); |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 203 | static int do_mkdir(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | mode_t mode = 0755; |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 205 | int ret; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 206 | |
| 207 | /* mkdir <path> [mode] [owner] [group] */ |
| 208 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 209 | if (args.size() >= 3) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 210 | mode = std::strtoul(args[2].c_str(), 0, 8); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 213 | ret = make_dir(args[1].c_str(), mode); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 214 | /* chmod in case the directory already exists */ |
| 215 | if (ret == -1 && errno == EEXIST) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 216 | ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW); |
Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 217 | } |
| 218 | if (ret == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | return -errno; |
| 220 | } |
| 221 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 222 | if (args.size() >= 4) { |
| 223 | uid_t uid = decode_uid(args[3].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 224 | gid_t gid = -1; |
| 225 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 226 | if (args.size() == 5) { |
| 227 | gid = decode_uid(args[4].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 230 | if (lchown(args[1].c_str(), uid, gid) == -1) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | return -errno; |
| 232 | } |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 233 | |
| 234 | /* chown may have cleared S_ISUID and S_ISGID, chmod again */ |
| 235 | if (mode & (S_ISUID | S_ISGID)) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 236 | ret = fchmodat(AT_FDCWD, args[1].c_str(), mode, AT_SYMLINK_NOFOLLOW); |
Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 237 | if (ret == -1) { |
| 238 | return -errno; |
| 239 | } |
| 240 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 243 | if (e4crypt_is_native()) { |
| 244 | if (e4crypt_set_directory_policy(args[1].c_str())) { |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 245 | const std::vector<std::string> options = { |
| 246 | "--prompt_and_wipe_data", |
| 247 | "--reason=set_policy_failed:"s + args[1]}; |
| 248 | reboot_into_recovery(options); |
Paul Crowley | af8be58 | 2016-05-10 08:52:06 -0700 | [diff] [blame] | 249 | return -1; |
| 250 | } |
| 251 | } |
| 252 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 255 | /* umount <path> */ |
| 256 | static int do_umount(const std::vector<std::string>& args) { |
| 257 | return umount(args[1].c_str()); |
| 258 | } |
| 259 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 260 | static struct { |
| 261 | const char *name; |
| 262 | unsigned flag; |
| 263 | } mount_flags[] = { |
| 264 | { "noatime", MS_NOATIME }, |
Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 265 | { "noexec", MS_NOEXEC }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | { "nosuid", MS_NOSUID }, |
| 267 | { "nodev", MS_NODEV }, |
| 268 | { "nodiratime", MS_NODIRATIME }, |
| 269 | { "ro", MS_RDONLY }, |
| 270 | { "rw", 0 }, |
| 271 | { "remount", MS_REMOUNT }, |
Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 272 | { "bind", MS_BIND }, |
| 273 | { "rec", MS_REC }, |
| 274 | { "unbindable", MS_UNBINDABLE }, |
| 275 | { "private", MS_PRIVATE }, |
| 276 | { "slave", MS_SLAVE }, |
| 277 | { "shared", MS_SHARED }, |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 278 | { "defaults", 0 }, |
| 279 | { 0, 0 }, |
| 280 | }; |
| 281 | |
Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 282 | #define DATA_MNT_POINT "/data" |
| 283 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 284 | /* mount <type> <device> <path> <flags ...> <options> */ |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 285 | static int do_mount(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 286 | char tmp[64]; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 287 | const char *source, *target, *system; |
| 288 | const char *options = NULL; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 289 | unsigned flags = 0; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 290 | std::size_t na = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 291 | int n, i; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 292 | int wait = 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 293 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 294 | for (na = 4; na < args.size(); na++) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 295 | for (i = 0; mount_flags[i].name; i++) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 296 | if (!args[na].compare(mount_flags[i].name)) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | flags |= mount_flags[i].flag; |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 302 | if (!mount_flags[i].name) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 303 | if (!args[na].compare("wait")) |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 304 | wait = 1; |
| 305 | /* if our last argument isn't a flag, wolf it up as an option string */ |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 306 | else if (na + 1 == args.size()) |
| 307 | options = args[na].c_str(); |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 308 | } |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 311 | system = args[1].c_str(); |
| 312 | source = args[2].c_str(); |
| 313 | target = args[3].c_str(); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 314 | |
Elliott Hughes | 3195116 | 2016-06-24 15:15:03 -0700 | [diff] [blame] | 315 | if (!strncmp(source, "loop@", 5)) { |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 316 | int mode, loop, fd; |
| 317 | struct loop_info info; |
| 318 | |
| 319 | mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 320 | fd = open(source + 5, mode | O_CLOEXEC); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 321 | if (fd < 0) { |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | for (n = 0; ; n++) { |
Yabin Cui | e2d63af | 2015-02-17 19:27:51 -0800 | [diff] [blame] | 326 | snprintf(tmp, sizeof(tmp), "/dev/block/loop%d", n); |
Nick Kralevich | 45a884f | 2015-02-02 14:37:22 -0800 | [diff] [blame] | 327 | loop = open(tmp, mode | O_CLOEXEC); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 328 | if (loop < 0) { |
Tomasz Kondel | bfdcc40 | 2014-02-06 08:57:27 +0100 | [diff] [blame] | 329 | close(fd); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | /* if it is a blank loop device */ |
| 334 | if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { |
| 335 | /* if it becomes our loop device */ |
| 336 | if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { |
| 337 | close(fd); |
| 338 | |
| 339 | if (mount(tmp, target, system, flags, options) < 0) { |
| 340 | ioctl(loop, LOOP_CLR_FD, 0); |
| 341 | close(loop); |
| 342 | return -1; |
| 343 | } |
| 344 | |
| 345 | close(loop); |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 346 | goto exit_success; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | close(loop); |
| 351 | } |
| 352 | |
| 353 | close(fd); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 354 | LOG(ERROR) << "out of loopback devices"; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 355 | return -1; |
| 356 | } else { |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 357 | if (wait) |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 358 | wait_for_file(source, kCommandRetryTimeout); |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 359 | if (mount(source, target, system, flags, options) < 0) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 360 | return -1; |
Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 361 | } |
| 362 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 363 | } |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 364 | |
| 365 | exit_success: |
Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 366 | return 0; |
| 367 | |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 370 | /* Imports .rc files from the specified paths. Default ones are applied if none is given. |
| 371 | * |
| 372 | * start_index: index of the first path in the args list |
| 373 | */ |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 374 | static void import_late(const std::vector<std::string>& args, size_t start_index, size_t end_index) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 375 | Parser& parser = Parser::GetInstance(); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 376 | if (end_index <= start_index) { |
Jaekyun Seok | 4ec72cc | 2017-02-22 20:37:57 +0900 | [diff] [blame] | 377 | // Fallbacks for partitions on which early mount isn't enabled. |
| 378 | if (!parser.is_system_etc_init_loaded()) { |
| 379 | parser.ParseConfig("/system/etc/init"); |
| 380 | parser.set_is_system_etc_init_loaded(true); |
| 381 | } |
| 382 | if (!parser.is_vendor_etc_init_loaded()) { |
| 383 | parser.ParseConfig("/vendor/etc/init"); |
| 384 | parser.set_is_vendor_etc_init_loaded(true); |
| 385 | } |
| 386 | if (!parser.is_odm_etc_init_loaded()) { |
| 387 | parser.ParseConfig("/odm/etc/init"); |
| 388 | parser.set_is_odm_etc_init_loaded(true); |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 389 | } |
| 390 | } else { |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 391 | for (size_t i = start_index; i < end_index; ++i) { |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 392 | parser.ParseConfig(args[i]); |
| 393 | } |
Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 394 | } |
Tom Cherry | d8a7257 | 2017-03-13 12:24:49 -0700 | [diff] [blame] | 395 | |
| 396 | // Turning this on and letting the INFO logging be discarded adds 0.2s to |
| 397 | // Nexus 9 boot time, so it's disabled by default. |
| 398 | if (false) parser.DumpState(); |
Tom Cherry | b8dd027 | 2015-07-22 14:23:33 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 401 | /* mount_fstab |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 402 | * |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 403 | * Call fs_mgr_mount_all() to mount the given fstab |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 404 | */ |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 405 | static int mount_fstab(const char* fstabfile, int mount_mode) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 406 | int ret = -1; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 407 | |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 408 | /* |
| 409 | * Call fs_mgr_mount_all() to mount all filesystems. We fork(2) and |
| 410 | * do the call in the child to provide protection to the main init |
| 411 | * process if anything goes wrong (crash or memory leak), and wait for |
| 412 | * the child to finish in the parent. |
| 413 | */ |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 414 | pid_t pid = fork(); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 415 | if (pid > 0) { |
| 416 | /* Parent. Wait for the child to return */ |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 417 | int status; |
Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 418 | int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 419 | if (wp_ret == -1) { |
| 420 | // Unexpected error code. We will continue anyway. |
| 421 | PLOG(WARNING) << "waitpid failed"; |
Paul Lawrence | 40af092 | 2014-09-16 14:31:23 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 424 | if (WIFEXITED(status)) { |
| 425 | ret = WEXITSTATUS(status); |
| 426 | } else { |
| 427 | ret = -1; |
| 428 | } |
| 429 | } else if (pid == 0) { |
| 430 | /* child, call fs_mgr_mount_all() */ |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 431 | |
| 432 | // So we can always see what fs_mgr_mount_all() does. |
| 433 | // Only needed if someone explicitly changes the default log level in their init.rc. |
| 434 | android::base::ScopedLogSeverity info(android::base::INFO); |
| 435 | |
| 436 | struct fstab* fstab = fs_mgr_read_fstab(fstabfile); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 437 | int child_ret = fs_mgr_mount_all(fstab, mount_mode); |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 438 | fs_mgr_free_fstab(fstab); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 439 | if (child_ret == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 440 | PLOG(ERROR) << "fs_mgr_mount_all returned an error"; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 441 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 442 | _exit(child_ret); |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 443 | } else { |
| 444 | /* fork failed, return an error */ |
| 445 | return -1; |
| 446 | } |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 447 | return ret; |
| 448 | } |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 449 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 450 | /* Queue event based on fs_mgr return code. |
| 451 | * |
| 452 | * code: return code of fs_mgr_mount_all |
| 453 | * |
| 454 | * This function might request a reboot, in which case it will |
| 455 | * not return. |
| 456 | * |
| 457 | * return code is processed based on input code |
| 458 | */ |
| 459 | static int queue_fs_event(int code) { |
| 460 | int ret = code; |
| 461 | if (code == FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION) { |
Paul Lawrence | 1f99218 | 2016-04-18 15:37:31 -0700 | [diff] [blame] | 462 | ActionManager::GetInstance().QueueEventTrigger("encrypt"); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 463 | } else if (code == FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 464 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 465 | property_set("ro.crypto.type", "block"); |
Paul Lawrence | 1f99218 | 2016-04-18 15:37:31 -0700 | [diff] [blame] | 466 | ActionManager::GetInstance().QueueEventTrigger("defaultcrypto"); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 467 | } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 468 | property_set("ro.crypto.state", "unencrypted"); |
Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 469 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 470 | } else if (code == FS_MGR_MNTALL_DEV_NOT_ENCRYPTABLE) { |
Paul Lawrence | 1098aac | 2016-03-04 15:52:33 -0800 | [diff] [blame] | 471 | property_set("ro.crypto.state", "unsupported"); |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 472 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 473 | } else if (code == FS_MGR_MNTALL_DEV_NEEDS_RECOVERY) { |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 474 | /* Setup a wipe via recovery, and reboot into recovery */ |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 475 | PLOG(ERROR) << "fs_mgr_mount_all suggested recovery, so wiping data via recovery."; |
Paul Crowley | 0b8b230 | 2016-12-19 13:03:47 -0800 | [diff] [blame] | 476 | const std::vector<std::string> options = {"--wipe_data", "--reason=fs_mgr_mount_all" }; |
| 477 | ret = reboot_into_recovery(options); |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 478 | /* If reboot worked, there is no return. */ |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 479 | } else if (code == FS_MGR_MNTALL_DEV_FILE_ENCRYPTED) { |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 480 | if (e4crypt_install_keyring()) { |
| 481 | return -1; |
| 482 | } |
| 483 | property_set("ro.crypto.state", "encrypted"); |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 484 | property_set("ro.crypto.type", "file"); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 485 | |
| 486 | // Although encrypted, we have device key, so we do not need to |
| 487 | // do anything different from the nonencrypted case. |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 488 | ActionManager::GetInstance().QueueEventTrigger("nonencrypted"); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 489 | } else if (code > 0) { |
| 490 | PLOG(ERROR) << "fs_mgr_mount_all returned unexpected error " << code; |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 491 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 492 | /* else ... < 0: error */ |
Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 493 | |
| 494 | return ret; |
| 495 | } |
| 496 | |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 497 | /* mount_all <fstab> [ <path> ]* [--<options>]* |
| 498 | * |
| 499 | * This function might request a reboot, in which case it will |
| 500 | * not return. |
| 501 | */ |
| 502 | static int do_mount_all(const std::vector<std::string>& args) { |
| 503 | std::size_t na = 0; |
| 504 | bool import_rc = true; |
| 505 | bool queue_event = true; |
| 506 | int mount_mode = MOUNT_MODE_DEFAULT; |
| 507 | const char* fstabfile = args[1].c_str(); |
| 508 | std::size_t path_arg_end = args.size(); |
Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 509 | const char* prop_post_fix = "default"; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 510 | |
| 511 | for (na = args.size() - 1; na > 1; --na) { |
| 512 | if (args[na] == "--early") { |
Wei Wang | d67a4ab | 2016-11-16 12:08:30 -0800 | [diff] [blame] | 513 | path_arg_end = na; |
| 514 | queue_event = false; |
| 515 | mount_mode = MOUNT_MODE_EARLY; |
Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 516 | prop_post_fix = "early"; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 517 | } else if (args[na] == "--late") { |
| 518 | path_arg_end = na; |
| 519 | import_rc = false; |
| 520 | mount_mode = MOUNT_MODE_LATE; |
Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 521 | prop_post_fix = "late"; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 525 | std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s", |
| 526 | prop_post_fix); |
| 527 | Timer t; |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 528 | int ret = mount_fstab(fstabfile, mount_mode); |
Keun-young Park | 69fae7b | 2017-03-03 18:57:09 -0800 | [diff] [blame] | 529 | property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str()); |
Wei Wang | d61a7e2 | 2016-08-23 11:58:09 -0700 | [diff] [blame] | 530 | |
| 531 | if (import_rc) { |
| 532 | /* Paths of .rc files are specified at the 2nd argument and beyond */ |
| 533 | import_late(args, 2, path_arg_end); |
| 534 | } |
| 535 | |
| 536 | if (queue_event) { |
| 537 | /* queue_fs_event will queue event based on mount_fstab return code |
| 538 | * and return processed return code*/ |
| 539 | ret = queue_fs_event(ret); |
| 540 | } |
| 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 545 | static int do_swapon_all(const std::vector<std::string>& args) { |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 546 | struct fstab *fstab; |
| 547 | int ret; |
| 548 | |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 549 | fstab = fs_mgr_read_fstab(args[1].c_str()); |
Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 550 | ret = fs_mgr_swapon_all(fstab); |
| 551 | fs_mgr_free_fstab(fstab); |
| 552 | |
| 553 | return ret; |
| 554 | } |
| 555 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 556 | static int do_setprop(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 557 | const char* name = args[1].c_str(); |
| 558 | const char* value = args[2].c_str(); |
Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 559 | property_set(name, value); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 563 | static int do_setrlimit(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 564 | struct rlimit limit; |
| 565 | int resource; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 566 | if (android::base::ParseInt(args[1], &resource) && |
| 567 | android::base::ParseUint(args[2], &limit.rlim_cur) && |
| 568 | android::base::ParseUint(args[3], &limit.rlim_max)) { |
| 569 | return setrlimit(resource, &limit); |
| 570 | } |
| 571 | LOG(WARNING) << "ignoring setrlimit " << args[1] << " " << args[2] << " " << args[3]; |
| 572 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 573 | } |
| 574 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 575 | static int do_start(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 576 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 577 | if (!svc) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 578 | LOG(ERROR) << "do_start: Service " << args[1] << " not found"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 579 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 580 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 581 | if (!svc->Start()) |
| 582 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 583 | return 0; |
| 584 | } |
| 585 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 586 | static int do_stop(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 587 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 588 | if (!svc) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 589 | LOG(ERROR) << "do_stop: Service " << args[1] << " not found"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 590 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 591 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 592 | svc->Stop(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 596 | static int do_restart(const std::vector<std::string>& args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 597 | Service* svc = ServiceManager::GetInstance().FindServiceByName(args[1]); |
| 598 | if (!svc) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 599 | LOG(ERROR) << "do_restart: Service " << args[1] << " not found"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 600 | return -1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 601 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 602 | svc->Restart(); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 606 | static int do_powerctl(const std::vector<std::string>& args) { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 607 | const std::string& command = args[1]; |
Yusuke Sato | f93d429 | 2015-07-21 15:50:59 -0700 | [diff] [blame] | 608 | unsigned int cmd = 0; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 609 | std::vector<std::string> cmd_params = android::base::Split(command, ","); |
| 610 | std::string reason_string = cmd_params[0]; |
| 611 | std::string reboot_target = ""; |
| 612 | bool runFsck = false; |
| 613 | bool commandInvalid = false; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 614 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 615 | if (cmd_params.size() > 2) { |
| 616 | commandInvalid = true; |
| 617 | } else if (cmd_params[0] == "shutdown") { |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 618 | cmd = ANDROID_RB_POWEROFF; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 619 | if (cmd_params.size() == 2 && cmd_params[1] == "userrequested") { |
| 620 | // The shutdown reason is PowerManager.SHUTDOWN_USER_REQUESTED. |
| 621 | // Run fsck once the file system is remounted in read-only mode. |
| 622 | runFsck = true; |
| 623 | reason_string = cmd_params[1]; |
| 624 | } |
| 625 | } else if (cmd_params[0] == "reboot") { |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 626 | cmd = ANDROID_RB_RESTART2; |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 627 | if (cmd_params.size() == 2) { |
| 628 | reboot_target = cmd_params[1]; |
| 629 | // When rebooting to the bootloader notify the bootloader writing |
| 630 | // also the BCB. |
| 631 | if (reboot_target == "bootloader") { |
| 632 | std::string err; |
| 633 | if (!write_reboot_bootloader(&err)) { |
| 634 | LOG(ERROR) << "reboot-bootloader: Error writing " |
| 635 | "bootloader_message: " |
| 636 | << err; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } else if (command == "thermal-shutdown") { // no additional parameter allowed |
Todd Poynor | 16663a2 | 2017-01-31 14:15:20 -0800 | [diff] [blame] | 641 | cmd = ANDROID_RB_THERMOFF; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 642 | } else { |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 643 | commandInvalid = true; |
| 644 | } |
| 645 | if (commandInvalid) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 646 | LOG(ERROR) << "powerctl: unrecognized command '" << command << "'"; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 647 | return -EINVAL; |
| 648 | } |
| 649 | |
Keun-young Park | 8d01f63 | 2017-03-13 11:54:47 -0700 | [diff] [blame] | 650 | DoReboot(cmd, reason_string, reboot_target, runFsck); |
| 651 | return 0; |
Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 654 | static int do_trigger(const std::vector<std::string>& args) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 655 | ActionManager::GetInstance().QueueEventTrigger(args[1]); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 659 | static int do_symlink(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 660 | return symlink(args[1].c_str(), args[2].c_str()); |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 663 | static int do_rm(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 664 | return unlink(args[1].c_str()); |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 665 | } |
| 666 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 667 | static int do_rmdir(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 668 | return rmdir(args[1].c_str()); |
Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 669 | } |
| 670 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 671 | static int do_sysclktz(const std::vector<std::string>& args) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 672 | struct timezone tz = {}; |
| 673 | if (android::base::ParseInt(args[1], &tz.tz_minuteswest) && settimeofday(NULL, &tz) != -1) { |
| 674 | return 0; |
| 675 | } |
| 676 | return -1; |
The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 677 | } |
| 678 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 679 | static int do_verity_load_state(const std::vector<std::string>& args) { |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 680 | int mode = -1; |
| 681 | int rc = fs_mgr_load_verity_state(&mode); |
Sami Tolvanen | 90f52df | 2015-12-02 14:23:09 +0000 | [diff] [blame] | 682 | if (rc == 0 && mode != VERITY_MODE_DEFAULT) { |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 683 | ActionManager::GetInstance().QueueEventTrigger("verity-logging"); |
Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 684 | } |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 685 | return rc; |
Sami Tolvanen | 8ff0190 | 2015-02-16 11:03:34 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 688 | static void verity_update_property(fstab_rec *fstab, const char *mount_point, |
| 689 | int mode, int status) { |
Sami Tolvanen | 4547423 | 2015-03-30 11:38:38 +0100 | [diff] [blame] | 690 | property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(), |
| 691 | android::base::StringPrintf("%d", mode).c_str()); |
Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 694 | static int do_verity_update_state(const std::vector<std::string>& args) { |
Elliott Hughes | db3f267 | 2015-03-20 09:45:18 -0700 | [diff] [blame] | 695 | return fs_mgr_update_verity_state(verity_update_property); |
Sami Tolvanen | acbf9be | 2015-03-19 10:00:34 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 698 | static int do_write(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 699 | const char* path = args[1].c_str(); |
| 700 | const char* value = args[2].c_str(); |
Jorge Lucangeli Obes | 77f0e9f | 2016-12-28 14:07:02 -0500 | [diff] [blame] | 701 | return write_file(path, value) ? 0 : 1; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 704 | static int do_copy(const std::vector<std::string>& args) { |
Wonsik Kim | 281531b | 2017-03-30 16:38:22 -0700 | [diff] [blame] | 705 | char* buffer = NULL; |
| 706 | int rc = 0; |
| 707 | int fd1 = -1, fd2 = -1; |
| 708 | struct stat info; |
| 709 | int brtw, brtr; |
| 710 | char* p; |
| 711 | |
| 712 | if (stat(args[1].c_str(), &info) < 0) return -1; |
| 713 | |
| 714 | if ((fd1 = open(args[1].c_str(), O_RDONLY | O_CLOEXEC)) < 0) goto out_err; |
| 715 | |
| 716 | if ((fd2 = open(args[2].c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0660)) < 0) |
| 717 | goto out_err; |
| 718 | |
| 719 | if (!(buffer = (char*)malloc(info.st_size))) goto out_err; |
| 720 | |
| 721 | p = buffer; |
| 722 | brtr = info.st_size; |
| 723 | while (brtr) { |
| 724 | rc = read(fd1, p, brtr); |
| 725 | if (rc < 0) goto out_err; |
| 726 | if (rc == 0) break; |
| 727 | p += rc; |
| 728 | brtr -= rc; |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 729 | } |
Wonsik Kim | 281531b | 2017-03-30 16:38:22 -0700 | [diff] [blame] | 730 | |
| 731 | p = buffer; |
| 732 | brtw = info.st_size; |
| 733 | while (brtw) { |
| 734 | rc = write(fd2, p, brtw); |
| 735 | if (rc < 0) goto out_err; |
| 736 | if (rc == 0) break; |
| 737 | p += rc; |
| 738 | brtw -= rc; |
| 739 | } |
| 740 | |
| 741 | rc = 0; |
| 742 | goto out; |
| 743 | out_err: |
| 744 | rc = -1; |
| 745 | out: |
| 746 | if (buffer) free(buffer); |
| 747 | if (fd1 >= 0) close(fd1); |
| 748 | if (fd2 >= 0) close(fd2); |
| 749 | return rc; |
San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 752 | static int do_chown(const std::vector<std::string>& args) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 753 | /* GID is optional. */ |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 754 | if (args.size() == 3) { |
| 755 | if (lchown(args[2].c_str(), decode_uid(args[1].c_str()), -1) == -1) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 756 | return -errno; |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 757 | } else if (args.size() == 4) { |
| 758 | if (lchown(args[3].c_str(), decode_uid(args[1].c_str()), |
| 759 | decode_uid(args[2].c_str())) == -1) |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 760 | return -errno; |
| 761 | } else { |
| 762 | return -1; |
| 763 | } |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | static mode_t get_mode(const char *s) { |
| 768 | mode_t mode = 0; |
| 769 | while (*s) { |
| 770 | if (*s >= '0' && *s <= '7') { |
| 771 | mode = (mode<<3) | (*s-'0'); |
| 772 | } else { |
| 773 | return -1; |
| 774 | } |
| 775 | s++; |
| 776 | } |
| 777 | return mode; |
| 778 | } |
| 779 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 780 | static int do_chmod(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 781 | mode_t mode = get_mode(args[1].c_str()); |
| 782 | if (fchmodat(AT_FDCWD, args[2].c_str(), mode, AT_SYMLINK_NOFOLLOW) < 0) { |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 783 | return -errno; |
| 784 | } |
| 785 | return 0; |
| 786 | } |
| 787 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 788 | static int do_restorecon(const std::vector<std::string>& args) { |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 789 | int ret = 0; |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 790 | |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 791 | struct flag_type {const char* name; int value;}; |
| 792 | static const flag_type flags[] = { |
| 793 | {"--recursive", SELINUX_ANDROID_RESTORECON_RECURSE}, |
| 794 | {"--skip-ce", SELINUX_ANDROID_RESTORECON_SKIPCE}, |
| 795 | {"--cross-filesystems", SELINUX_ANDROID_RESTORECON_CROSS_FILESYSTEMS}, |
| 796 | {0, 0} |
| 797 | }; |
| 798 | |
| 799 | int flag = 0; |
| 800 | |
| 801 | bool in_flags = true; |
| 802 | for (size_t i = 1; i < args.size(); ++i) { |
| 803 | if (android::base::StartsWith(args[i], "--")) { |
| 804 | if (!in_flags) { |
| 805 | LOG(ERROR) << "restorecon - flags must precede paths"; |
| 806 | return -1; |
| 807 | } |
| 808 | bool found = false; |
| 809 | for (size_t j = 0; flags[j].name; ++j) { |
| 810 | if (args[i] == flags[j].name) { |
| 811 | flag |= flags[j].value; |
| 812 | found = true; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | if (!found) { |
| 817 | LOG(ERROR) << "restorecon - bad flag " << args[i]; |
| 818 | return -1; |
| 819 | } |
| 820 | } else { |
| 821 | in_flags = false; |
| 822 | if (restorecon(args[i].c_str(), flag) < 0) { |
| 823 | ret = -errno; |
| 824 | } |
| 825 | } |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 826 | } |
Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 827 | return ret; |
| 828 | } |
| 829 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 830 | static int do_restorecon_recursive(const std::vector<std::string>& args) { |
Paul Lawrence | a8d8434 | 2016-11-14 15:40:18 -0800 | [diff] [blame] | 831 | std::vector<std::string> non_const_args(args); |
| 832 | non_const_args.insert(std::next(non_const_args.begin()), "--recursive"); |
| 833 | return do_restorecon(non_const_args); |
Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 834 | } |
| 835 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 836 | static int do_loglevel(const std::vector<std::string>& args) { |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 837 | // TODO: support names instead/as well? |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 838 | int log_level = -1; |
| 839 | android::base::ParseInt(args[1], &log_level); |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 840 | android::base::LogSeverity severity; |
| 841 | switch (log_level) { |
| 842 | case 7: severity = android::base::DEBUG; break; |
| 843 | case 6: severity = android::base::INFO; break; |
| 844 | case 5: |
| 845 | case 4: severity = android::base::WARNING; break; |
| 846 | case 3: severity = android::base::ERROR; break; |
| 847 | case 2: |
| 848 | case 1: |
| 849 | case 0: severity = android::base::FATAL; break; |
| 850 | default: |
| 851 | LOG(ERROR) << "loglevel: invalid log level " << log_level; |
| 852 | return -EINVAL; |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 853 | } |
Elliott Hughes | 7bc87a5 | 2016-08-04 16:09:39 -0700 | [diff] [blame] | 854 | android::base::SetMinimumLogSeverity(severity); |
Riley Andrews | 1bbef88 | 2014-06-26 13:55:03 -0700 | [diff] [blame] | 855 | return 0; |
The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 858 | static int do_load_persist_props(const std::vector<std::string>& args) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 859 | load_persist_props(); |
| 860 | return 0; |
Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 861 | } |
| 862 | |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 863 | static int do_load_system_props(const std::vector<std::string>& args) { |
| 864 | load_system_props(); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 865 | return 0; |
Riley Andrews | e4b7b29 | 2014-06-16 15:06:21 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 868 | static int do_wait(const std::vector<std::string>& args) { |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 869 | if (args.size() == 2) { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 870 | return wait_for_file(args[1].c_str(), kCommandRetryTimeout); |
Tom Cherry | 96f6731 | 2015-07-30 13:52:55 -0700 | [diff] [blame] | 871 | } else if (args.size() == 3) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 872 | int timeout; |
| 873 | if (android::base::ParseInt(args[2], &timeout)) { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 874 | return wait_for_file(args[1].c_str(), std::chrono::seconds(timeout)); |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | return -1; |
Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 878 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 879 | |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 880 | static int do_wait_for_prop(const std::vector<std::string>& args) { |
| 881 | const char* name = args[1].c_str(); |
| 882 | const char* value = args[2].c_str(); |
| 883 | size_t value_len = strlen(value); |
| 884 | |
| 885 | if (!is_legal_property_name(name)) { |
| 886 | LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value |
| 887 | << "\") failed: bad name"; |
| 888 | return -1; |
| 889 | } |
| 890 | if (value_len >= PROP_VALUE_MAX) { |
| 891 | LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value |
| 892 | << "\") failed: value too long"; |
| 893 | return -1; |
| 894 | } |
Wei Wang | 2d0fdaa | 2017-02-02 10:52:39 -0800 | [diff] [blame] | 895 | if (!start_waiting_for_property(name, value)) { |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 896 | LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value |
| 897 | << "\") failed: init already in waiting"; |
| 898 | return -1; |
| 899 | } |
| 900 | return 0; |
| 901 | } |
| 902 | |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 903 | /* |
| 904 | * Callback to make a directory from the ext4 code |
| 905 | */ |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 906 | static int do_installkeys_ensure_dir_exists(const char* dir) { |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 907 | if (make_dir(dir, 0700) && errno != EEXIST) { |
| 908 | return -1; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 911 | return 0; |
| 912 | } |
| 913 | |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 914 | static bool is_file_crypto() { |
Tom Cherry | e249097 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 915 | return android::base::GetProperty("ro.crypto.type", "") == "file"; |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 916 | } |
| 917 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 918 | static int do_installkey(const std::vector<std::string>& args) { |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 919 | if (!is_file_crypto()) { |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | } |
Janis Danisevskis | 650d22b | 2017-03-29 14:50:01 -0700 | [diff] [blame] | 922 | auto unencrypted_dir = args[1] + e4crypt_unencrypted_folder; |
| 923 | if (do_installkeys_ensure_dir_exists(unencrypted_dir.c_str())) { |
| 924 | PLOG(ERROR) << "Failed to create " << unencrypted_dir; |
| 925 | return -1; |
| 926 | } |
| 927 | std::vector<std::string> exec_args = {"exec", "/system/bin/vdc", "--wait", "cryptfs", |
| 928 | "enablefilecrypto"}; |
| 929 | return do_exec(exec_args); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 930 | } |
Paul Crowley | 749af8c | 2015-05-28 17:35:06 +0100 | [diff] [blame] | 931 | |
Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 932 | static int do_init_user0(const std::vector<std::string>& args) { |
Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 933 | return e4crypt_do_init_user0(); |
| 934 | } |
| 935 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 936 | BuiltinFunctionMap::Map& BuiltinFunctionMap::map() const { |
| 937 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
Tom Cherry | 2d80467 | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 938 | // clang-format off |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 939 | static const Map builtin_functions = { |
Elliott Hughes | a3641af | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 940 | {"bootchart", {1, 1, do_bootchart}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 941 | {"chmod", {2, 2, do_chmod}}, |
| 942 | {"chown", {2, 3, do_chown}}, |
| 943 | {"class_reset", {1, 1, do_class_reset}}, |
Steven Moreland | 2b63d54 | 2017-03-10 14:04:37 -0800 | [diff] [blame] | 944 | {"class_restart", {1, 1, do_class_restart}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 945 | {"class_start", {1, 1, do_class_start}}, |
| 946 | {"class_stop", {1, 1, do_class_stop}}, |
| 947 | {"copy", {2, 2, do_copy}}, |
| 948 | {"domainname", {1, 1, do_domainname}}, |
| 949 | {"enable", {1, 1, do_enable}}, |
| 950 | {"exec", {1, kMax, do_exec}}, |
Tom Cherry | 2d80467 | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 951 | {"exec_start", {1, 1, do_exec_start}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 952 | {"export", {2, 2, do_export}}, |
| 953 | {"hostname", {1, 1, do_hostname}}, |
| 954 | {"ifup", {1, 1, do_ifup}}, |
Paul Crowley | 5949745 | 2016-02-01 16:37:13 +0000 | [diff] [blame] | 955 | {"init_user0", {0, 0, do_init_user0}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 956 | {"insmod", {1, kMax, do_insmod}}, |
| 957 | {"installkey", {1, 1, do_installkey}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 958 | {"load_persist_props", {0, 0, do_load_persist_props}}, |
Tom Cherry | af20a7c | 2015-09-01 15:05:34 -0700 | [diff] [blame] | 959 | {"load_system_props", {0, 0, do_load_system_props}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 960 | {"loglevel", {1, 1, do_loglevel}}, |
| 961 | {"mkdir", {1, 4, do_mkdir}}, |
Hung-ying Tyan | dc738ea | 2016-01-14 11:18:21 +0800 | [diff] [blame] | 962 | {"mount_all", {1, kMax, do_mount_all}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 963 | {"mount", {3, kMax, do_mount}}, |
Alex Light | 68ab20f | 2016-06-23 11:11:39 -0700 | [diff] [blame] | 964 | {"umount", {1, 1, do_umount}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 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}}, |
| 973 | {"start", {1, 1, do_start}}, |
| 974 | {"stop", {1, 1, do_stop}}, |
| 975 | {"swapon_all", {1, 1, do_swapon_all}}, |
| 976 | {"symlink", {2, 2, do_symlink}}, |
| 977 | {"sysclktz", {1, 1, do_sysclktz}}, |
| 978 | {"trigger", {1, 1, do_trigger}}, |
| 979 | {"verity_load_state", {0, 0, do_verity_load_state}}, |
| 980 | {"verity_update_state", {0, 0, do_verity_update_state}}, |
| 981 | {"wait", {1, 2, do_wait}}, |
Wei Wang | 132ac31 | 2017-01-25 16:27:03 -0800 | [diff] [blame] | 982 | {"wait_for_prop", {2, 2, do_wait_for_prop}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 983 | {"write", {2, 2, do_write}}, |
| 984 | }; |
Tom Cherry | 2d80467 | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 985 | // clang-format on |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 986 | return builtin_functions; |
| 987 | } |