blob: d050ed783580101f1dd39116bf6df0e27c15b3b1 [file] [log] [blame]
Tom Cherry44aceed2018-08-03 13:36:18 -07001/*
2 * Copyright (C) 2018 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 Cherry7bfea3d2018-11-06 14:12:05 -080017#include "first_stage_init.h"
18
Tom Cherry866c08c2018-11-06 10:32:53 -080019#include <dirent.h>
20#include <fcntl.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070021#include <paths.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070022#include <stdlib.h>
23#include <sys/mount.h>
24#include <sys/stat.h>
25#include <sys/sysmacros.h>
26#include <sys/types.h>
Steve Muckled6d38c32020-05-29 16:31:19 -070027#include <sys/utsname.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070028#include <unistd.h>
29
Kelvin Zhang22929da2022-03-17 15:18:36 -070030#include <chrono>
Bowgo Tsai30afda72019-04-11 23:57:24 +080031#include <filesystem>
Tom Cherry44aceed2018-08-03 13:36:18 -070032#include <string>
33#include <vector>
34
35#include <android-base/chrono_utils.h>
Tom Cherry866c08c2018-11-06 10:32:53 -080036#include <android-base/file.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070037#include <android-base/logging.h>
Steve Muckle18b981e2019-04-15 17:43:02 -070038#include <modprobe/modprobe.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070039#include <private/android_filesystem_config.h>
Tom Cherry44aceed2018-08-03 13:36:18 -070040
Bowgo Tsai30afda72019-04-11 23:57:24 +080041#include "debug_ramdisk.h"
Steve Mucklea4bf2ce2019-11-01 13:58:02 -070042#include "first_stage_console.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070043#include "first_stage_mount.h"
44#include "reboot_utils.h"
Yifan Honga68ee762020-10-06 16:58:19 -070045#include "second_stage_resources.h"
David Anderson491e4da2020-12-08 00:21:20 -080046#include "snapuserd_transition.h"
Tom Cherry866c08c2018-11-06 10:32:53 -080047#include "switch_root.h"
Tom Cherry44aceed2018-08-03 13:36:18 -070048#include "util.h"
49
50using android::base::boot_clock;
51
Tom Cherry866c08c2018-11-06 10:32:53 -080052using namespace std::literals;
53
Bowgo Tsai30afda72019-04-11 23:57:24 +080054namespace fs = std::filesystem;
55
Tom Cherry44aceed2018-08-03 13:36:18 -070056namespace android {
57namespace init {
58
Tom Cherry866c08c2018-11-06 10:32:53 -080059namespace {
60
61void FreeRamdisk(DIR* dir, dev_t dev) {
62 int dfd = dirfd(dir);
63
64 dirent* de;
65 while ((de = readdir(dir)) != nullptr) {
66 if (de->d_name == "."s || de->d_name == ".."s) {
67 continue;
68 }
69
70 bool is_dir = false;
71
72 if (de->d_type == DT_DIR || de->d_type == DT_UNKNOWN) {
73 struct stat info;
74 if (fstatat(dfd, de->d_name, &info, AT_SYMLINK_NOFOLLOW) != 0) {
75 continue;
76 }
77
78 if (info.st_dev != dev) {
79 continue;
80 }
81
82 if (S_ISDIR(info.st_mode)) {
83 is_dir = true;
Tom Cherry247ffbf2019-07-08 15:09:36 -070084 auto fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
Tom Cherry866c08c2018-11-06 10:32:53 -080085 if (fd >= 0) {
86 auto subdir =
87 std::unique_ptr<DIR, decltype(&closedir)>{fdopendir(fd), closedir};
88 if (subdir) {
89 FreeRamdisk(subdir.get(), dev);
90 } else {
91 close(fd);
92 }
93 }
94 }
David Anderson491e4da2020-12-08 00:21:20 -080095 } else if (de->d_type == DT_REG) {
96 // Do not free snapuserd if we will need the ramdisk copy during the
97 // selinux transition.
98 if (de->d_name == "snapuserd"s && IsFirstStageSnapuserdRunning()) {
99 continue;
100 }
Tom Cherry866c08c2018-11-06 10:32:53 -0800101 }
102 unlinkat(dfd, de->d_name, is_dir ? AT_REMOVEDIR : 0);
103 }
104}
105
Devin Moore79058482021-03-03 13:57:43 -0800106bool ForceNormalBoot(const std::string& cmdline, const std::string& bootconfig) {
107 return bootconfig.find("androidboot.force_normal_boot = \"1\"") != std::string::npos ||
108 cmdline.find("androidboot.force_normal_boot=1") != std::string::npos;
Tom Cherry866c08c2018-11-06 10:32:53 -0800109}
110
Kelvin Zhang22929da2022-03-17 15:18:36 -0700111static void Copy(const char* src, const char* dst) {
112 if (link(src, dst) == 0) {
113 LOG(INFO) << "hard linking " << src << " to " << dst << " succeeded";
114 return;
115 }
Kelvin Zhangf887e742022-04-06 14:02:13 -0700116 PLOG(FATAL) << "hard linking " << src << " to " << dst << " failed";
Kelvin Zhang22929da2022-03-17 15:18:36 -0700117}
118
Kelvin Zhangf887e742022-04-06 14:02:13 -0700119// Move snapuserd before switching root, so that it is available at the same path
Kelvin Zhang22929da2022-03-17 15:18:36 -0700120// after switching root.
121void PrepareSwitchRoot() {
122 constexpr const char* src = "/system/bin/snapuserd";
123 constexpr const char* dst = "/first_stage_ramdisk/system/bin/snapuserd";
124
125 if (access(dst, X_OK) == 0) {
126 LOG(INFO) << dst << " already exists and it can be executed";
127 return;
128 }
129
130 if (access(src, F_OK) != 0) {
131 PLOG(INFO) << "Not moving " << src << " because it cannot be accessed";
132 return;
133 }
134
135 auto dst_dir = android::base::Dirname(dst);
136 std::error_code ec;
137 if (access(dst_dir.c_str(), F_OK) != 0) {
138 if (!fs::create_directories(dst_dir, ec)) {
139 LOG(FATAL) << "Cannot create " << dst_dir << ": " << ec.message();
140 }
141 }
142 Copy(src, dst);
143}
Tom Cherry866c08c2018-11-06 10:32:53 -0800144} // namespace
145
Steve Muckled6d38c32020-05-29 16:31:19 -0700146std::string GetModuleLoadList(bool recovery, const std::string& dir_path) {
147 auto module_load_file = "modules.load";
148 if (recovery) {
149 struct stat fileStat;
150 std::string recovery_load_path = dir_path + "/modules.load.recovery";
151 if (!stat(recovery_load_path.c_str(), &fileStat)) {
152 module_load_file = "modules.load.recovery";
153 }
154 }
155
156 return module_load_file;
157}
158
159#define MODULE_BASE_DIR "/lib/modules"
Chungkaic60300a2021-02-03 20:30:07 -0800160bool LoadKernelModules(bool recovery, bool want_console, bool want_parallel, int& modules_loaded) {
Steve Muckled6d38c32020-05-29 16:31:19 -0700161 struct utsname uts;
162 if (uname(&uts)) {
163 LOG(FATAL) << "Failed to get kernel version.";
164 }
165 int major, minor;
166 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
167 LOG(FATAL) << "Failed to parse kernel version " << uts.release;
168 }
169
170 std::unique_ptr<DIR, decltype(&closedir)> base_dir(opendir(MODULE_BASE_DIR), closedir);
171 if (!base_dir) {
172 LOG(INFO) << "Unable to open /lib/modules, skipping module loading.";
173 return true;
174 }
175 dirent* entry;
176 std::vector<std::string> module_dirs;
177 while ((entry = readdir(base_dir.get()))) {
178 if (entry->d_type != DT_DIR) {
179 continue;
180 }
181 int dir_major, dir_minor;
182 if (sscanf(entry->d_name, "%d.%d", &dir_major, &dir_minor) != 2 || dir_major != major ||
183 dir_minor != minor) {
184 continue;
185 }
186 module_dirs.emplace_back(entry->d_name);
187 }
188
189 // Sort the directories so they are iterated over during module loading
190 // in a consistent order. Alphabetical sorting is fine here because the
191 // kernel version at the beginning of the directory name must match the
192 // current kernel version, so the sort only applies to a label that
193 // follows the kernel version, for example /lib/modules/5.4 vs.
194 // /lib/modules/5.4-gki.
195 std::sort(module_dirs.begin(), module_dirs.end());
196
197 for (const auto& module_dir : module_dirs) {
198 std::string dir_path = MODULE_BASE_DIR "/";
199 dir_path.append(module_dir);
200 Modprobe m({dir_path}, GetModuleLoadList(recovery, dir_path));
201 bool retval = m.LoadListedModules(!want_console);
Lisa Liu08c862f2021-02-03 18:22:21 +0800202 modules_loaded = m.GetModuleCount();
Steve Muckled6d38c32020-05-29 16:31:19 -0700203 if (modules_loaded > 0) {
204 return retval;
205 }
206 }
207
208 Modprobe m({MODULE_BASE_DIR}, GetModuleLoadList(recovery, MODULE_BASE_DIR));
Chungkaic60300a2021-02-03 20:30:07 -0800209 bool retval = (want_parallel) ? m.LoadModulesParallel(std::thread::hardware_concurrency())
210 : m.LoadListedModules(!want_console);
Lisa Liu08c862f2021-02-03 18:22:21 +0800211 modules_loaded = m.GetModuleCount();
Steve Muckled6d38c32020-05-29 16:31:19 -0700212 if (modules_loaded > 0) {
213 return retval;
214 }
215 return true;
216}
217
Tom Cherry7bfea3d2018-11-06 14:12:05 -0800218int FirstStageMain(int argc, char** argv) {
Tom Cherry44aceed2018-08-03 13:36:18 -0700219 if (REBOOT_BOOTLOADER_ON_PANIC) {
220 InstallRebootSignalHandlers();
221 }
222
223 boot_clock::time_point start_time = boot_clock::now();
224
225 std::vector<std::pair<std::string, int>> errors;
226#define CHECKCALL(x) \
Tom Cherry247ffbf2019-07-08 15:09:36 -0700227 if ((x) != 0) errors.emplace_back(#x " failed", errno);
Tom Cherry44aceed2018-08-03 13:36:18 -0700228
229 // Clear the umask.
230 umask(0);
231
232 CHECKCALL(clearenv());
233 CHECKCALL(setenv("PATH", _PATH_DEFPATH, 1));
234 // Get the basic filesystem setup we need put together in the initramdisk
235 // on / and then we'll let the rc file figure out the rest.
236 CHECKCALL(mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755"));
237 CHECKCALL(mkdir("/dev/pts", 0755));
238 CHECKCALL(mkdir("/dev/socket", 0755));
David Anderson1ff75812020-11-13 00:31:47 -0800239 CHECKCALL(mkdir("/dev/dm-user", 0755));
Tom Cherry44aceed2018-08-03 13:36:18 -0700240 CHECKCALL(mount("devpts", "/dev/pts", "devpts", 0, NULL));
241#define MAKE_STR(x) __STRING(x)
242 CHECKCALL(mount("proc", "/proc", "proc", 0, "hidepid=2,gid=" MAKE_STR(AID_READPROC)));
243#undef MAKE_STR
244 // Don't expose the raw commandline to unprivileged processes.
245 CHECKCALL(chmod("/proc/cmdline", 0440));
Steve Muckled75f30a2019-05-21 15:50:39 -0700246 std::string cmdline;
247 android::base::ReadFileToString("/proc/cmdline", &cmdline);
Devin Moorea4ef15b2020-12-15 16:14:37 -0800248 // Don't expose the raw bootconfig to unprivileged processes.
249 chmod("/proc/bootconfig", 0440);
Devin Moore79058482021-03-03 13:57:43 -0800250 std::string bootconfig;
251 android::base::ReadFileToString("/proc/bootconfig", &bootconfig);
Tom Cherry44aceed2018-08-03 13:36:18 -0700252 gid_t groups[] = {AID_READPROC};
253 CHECKCALL(setgroups(arraysize(groups), groups));
254 CHECKCALL(mount("sysfs", "/sys", "sysfs", 0, NULL));
255 CHECKCALL(mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL));
256
257 CHECKCALL(mknod("/dev/kmsg", S_IFCHR | 0600, makedev(1, 11)));
258
259 if constexpr (WORLD_WRITABLE_KMSG) {
260 CHECKCALL(mknod("/dev/kmsg_debug", S_IFCHR | 0622, makedev(1, 11)));
261 }
262
263 CHECKCALL(mknod("/dev/random", S_IFCHR | 0666, makedev(1, 8)));
264 CHECKCALL(mknod("/dev/urandom", S_IFCHR | 0666, makedev(1, 9)));
265
266 // This is needed for log wrapper, which gets called before ueventd runs.
267 CHECKCALL(mknod("/dev/ptmx", S_IFCHR | 0666, makedev(5, 2)));
268 CHECKCALL(mknod("/dev/null", S_IFCHR | 0666, makedev(1, 3)));
269
Tom Cherry21824dd2018-10-02 10:07:05 -0700270 // These below mounts are done in first stage init so that first stage mount can mount
271 // subdirectories of /mnt/{vendor,product}/. Other mounts, not required by first stage mount,
272 // should be done in rc files.
Tom Cherry44aceed2018-08-03 13:36:18 -0700273 // Mount staging areas for devices managed by vold
274 // See storage config details at http://source.android.com/devices/storage/
275 CHECKCALL(mount("tmpfs", "/mnt", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
276 "mode=0755,uid=0,gid=1000"));
277 // /mnt/vendor is used to mount vendor-specific partitions that can not be
278 // part of the vendor partition, e.g. because they are mounted read-write.
279 CHECKCALL(mkdir("/mnt/vendor", 0755));
280 // /mnt/product is used to mount product-specific partitions that can not be
281 // part of the product partition, e.g. because they are mounted read-write.
282 CHECKCALL(mkdir("/mnt/product", 0755));
283
Bowgo Tsai30afda72019-04-11 23:57:24 +0800284 // /debug_ramdisk is used to preserve additional files from the debug ramdisk
285 CHECKCALL(mount("tmpfs", "/debug_ramdisk", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
286 "mode=0755,uid=0,gid=0"));
Yifan Honga68ee762020-10-06 16:58:19 -0700287
288 // /second_stage_resources is used to preserve files from first to second
289 // stage init
290 CHECKCALL(mount("tmpfs", kSecondStageRes, "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
291 "mode=0755,uid=0,gid=0"))
Jeffrey Vander Stoepbaeece62022-02-08 12:42:33 +0000292
293 // First stage init stores Mainline sepolicy here.
294 CHECKCALL(mkdir("/dev/selinux", 0744));
Tom Cherry44aceed2018-08-03 13:36:18 -0700295#undef CHECKCALL
296
Tom Cherry59656fb2019-05-28 10:19:44 -0700297 SetStdioToDevNull(argv);
Tom Cherry44aceed2018-08-03 13:36:18 -0700298 // Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
299 // talk to the outside world...
Tom Cherry59656fb2019-05-28 10:19:44 -0700300 InitKernelLogging(argv);
Tom Cherry44aceed2018-08-03 13:36:18 -0700301
302 if (!errors.empty()) {
303 for (const auto& [error_string, error_errno] : errors) {
304 LOG(ERROR) << error_string << " " << strerror(error_errno);
305 }
306 LOG(FATAL) << "Init encountered errors starting first stage, aborting";
307 }
308
309 LOG(INFO) << "init first stage started!";
310
Tom Cherry866c08c2018-11-06 10:32:53 -0800311 auto old_root_dir = std::unique_ptr<DIR, decltype(&closedir)>{opendir("/"), closedir};
312 if (!old_root_dir) {
313 PLOG(ERROR) << "Could not opendir(\"/\"), not freeing ramdisk";
314 }
315
316 struct stat old_root_info;
317 if (stat("/", &old_root_info) != 0) {
318 PLOG(ERROR) << "Could not stat(\"/\"), not freeing ramdisk";
319 old_root_dir.reset();
320 }
321
Devin Moore79058482021-03-03 13:57:43 -0800322 auto want_console = ALLOW_FIRST_STAGE_CONSOLE ? FirstStageConsole(cmdline, bootconfig) : 0;
Chungkaic60300a2021-02-03 20:30:07 -0800323 auto want_parallel =
324 bootconfig.find("androidboot.load_modules_parallel = \"true\"") != std::string::npos;
Steve Muckled6d38c32020-05-29 16:31:19 -0700325
Lisa Liu08c862f2021-02-03 18:22:21 +0800326 boot_clock::time_point module_start_time = boot_clock::now();
327 int module_count = 0;
Devin Moore79058482021-03-03 13:57:43 -0800328 if (!LoadKernelModules(IsRecoveryMode() && !ForceNormalBoot(cmdline, bootconfig), want_console,
Chungkaic60300a2021-02-03 20:30:07 -0800329 want_parallel, module_count)) {
Will McVickerc8907422020-05-01 16:47:12 -0700330 if (want_console != FirstStageConsoleParam::DISABLED) {
Mark Salyzyna7144f72019-10-31 08:02:10 -0700331 LOG(ERROR) << "Failed to load kernel modules, starting console";
332 } else {
333 LOG(FATAL) << "Failed to load kernel modules";
334 }
Steve Muckle18b981e2019-04-15 17:43:02 -0700335 }
Lisa Liu08c862f2021-02-03 18:22:21 +0800336 if (module_count > 0) {
337 auto module_elapse_time = std::chrono::duration_cast<std::chrono::milliseconds>(
338 boot_clock::now() - module_start_time);
339 setenv(kEnvInitModuleDurationMs, std::to_string(module_elapse_time.count()).c_str(), 1);
340 LOG(INFO) << "Loaded " << module_count << " kernel modules took "
341 << module_elapse_time.count() << " ms";
342 }
Steve Muckle18b981e2019-04-15 17:43:02 -0700343
Elliot Bermand96d0f72021-01-25 09:53:36 -0800344 bool created_devices = false;
Will McVickerc8907422020-05-01 16:47:12 -0700345 if (want_console == FirstStageConsoleParam::CONSOLE_ON_FAILURE) {
Elliot Bermand96d0f72021-01-25 09:53:36 -0800346 if (!IsRecoveryMode()) {
347 created_devices = DoCreateDevices();
Kelvin Zhang22929da2022-03-17 15:18:36 -0700348 if (!created_devices) {
Elliot Bermand96d0f72021-01-25 09:53:36 -0800349 LOG(ERROR) << "Failed to create device nodes early";
350 }
351 }
Steve Mucklecc3410e2020-11-02 15:31:19 -0800352 StartConsole(cmdline);
Steve Muckled75f30a2019-05-21 15:50:39 -0700353 }
354
Yifan Honga68ee762020-10-06 16:58:19 -0700355 if (access(kBootImageRamdiskProp, F_OK) == 0) {
356 std::string dest = GetRamdiskPropForSecondStage();
357 std::string dir = android::base::Dirname(dest);
358 std::error_code ec;
Yifan Hong7e7f8812020-11-19 12:12:11 -0800359 if (!fs::create_directories(dir, ec) && !!ec) {
Yifan Honga68ee762020-10-06 16:58:19 -0700360 LOG(FATAL) << "Can't mkdir " << dir << ": " << ec.message();
361 }
362 if (!fs::copy_file(kBootImageRamdiskProp, dest, ec)) {
363 LOG(FATAL) << "Can't copy " << kBootImageRamdiskProp << " to " << dest << ": "
364 << ec.message();
365 }
366 LOG(INFO) << "Copied ramdisk prop to " << dest;
367 }
368
Bowgo Tsai64e92f92021-04-23 11:16:58 +0800369 // If "/force_debuggable" is present, the second-stage init will use a userdebug
370 // sepolicy and load adb_debug.prop to allow adb root, if the device is unlocked.
371 if (access("/force_debuggable", F_OK) == 0) {
Yi-Yo Chiangbb77c542021-09-23 14:14:16 +0000372 constexpr const char adb_debug_prop_src[] = "/adb_debug.prop";
373 constexpr const char userdebug_plat_sepolicy_cil_src[] = "/userdebug_plat_sepolicy.cil";
Bowgo Tsai64e92f92021-04-23 11:16:58 +0800374 std::error_code ec; // to invoke the overloaded copy_file() that won't throw.
Yi-Yo Chiangbb77c542021-09-23 14:14:16 +0000375 if (access(adb_debug_prop_src, F_OK) == 0 &&
376 !fs::copy_file(adb_debug_prop_src, kDebugRamdiskProp, ec)) {
377 LOG(WARNING) << "Can't copy " << adb_debug_prop_src << " to " << kDebugRamdiskProp
378 << ": " << ec.message();
Bowgo Tsai64e92f92021-04-23 11:16:58 +0800379 }
Yi-Yo Chiangbb77c542021-09-23 14:14:16 +0000380 if (access(userdebug_plat_sepolicy_cil_src, F_OK) == 0 &&
381 !fs::copy_file(userdebug_plat_sepolicy_cil_src, kDebugRamdiskSEPolicy, ec)) {
382 LOG(WARNING) << "Can't copy " << userdebug_plat_sepolicy_cil_src << " to "
383 << kDebugRamdiskSEPolicy << ": " << ec.message();
384 }
385 // setenv for second-stage init to read above kDebugRamdisk* files.
386 setenv("INIT_FORCE_DEBUGGABLE", "true", 1);
Bowgo Tsai64e92f92021-04-23 11:16:58 +0800387 }
388
Devin Moore79058482021-03-03 13:57:43 -0800389 if (ForceNormalBoot(cmdline, bootconfig)) {
Tom Cherry866c08c2018-11-06 10:32:53 -0800390 mkdir("/first_stage_ramdisk", 0755);
Kelvin Zhang22929da2022-03-17 15:18:36 -0700391 PrepareSwitchRoot();
Tom Cherry866c08c2018-11-06 10:32:53 -0800392 // SwitchRoot() must be called with a mount point as the target, so we bind mount the
393 // target directory to itself here.
394 if (mount("/first_stage_ramdisk", "/first_stage_ramdisk", nullptr, MS_BIND, nullptr) != 0) {
Kelvin Zhang22929da2022-03-17 15:18:36 -0700395 PLOG(FATAL) << "Could not bind mount /first_stage_ramdisk to itself";
Tom Cherry866c08c2018-11-06 10:32:53 -0800396 }
397 SwitchRoot("/first_stage_ramdisk");
398 }
399
Elliot Bermand96d0f72021-01-25 09:53:36 -0800400 if (!DoFirstStageMount(!created_devices)) {
Tom Cherry44aceed2018-08-03 13:36:18 -0700401 LOG(FATAL) << "Failed to mount required partitions early ...";
402 }
403
Tom Cherry866c08c2018-11-06 10:32:53 -0800404 struct stat new_root_info;
405 if (stat("/", &new_root_info) != 0) {
406 PLOG(ERROR) << "Could not stat(\"/\"), not freeing ramdisk";
407 old_root_dir.reset();
408 }
409
410 if (old_root_dir && old_root_info.st_dev != new_root_info.st_dev) {
411 FreeRamdisk(old_root_dir.get(), old_root_info.st_dev);
412 }
413
Tom Cherry44aceed2018-08-03 13:36:18 -0700414 SetInitAvbVersionInRecovery();
415
Mark Salyzyn44505ec2019-05-08 12:44:50 -0700416 setenv(kEnvFirstStageStartedAt, std::to_string(start_time.time_since_epoch().count()).c_str(),
Mark Salyzyn10377df2019-03-27 08:10:41 -0700417 1);
Tom Cherry44aceed2018-08-03 13:36:18 -0700418
Tom Cherry31438482018-07-20 14:57:00 -0700419 const char* path = "/system/bin/init";
Tom Cherry7bfea3d2018-11-06 14:12:05 -0800420 const char* args[] = {path, "selinux_setup", nullptr};
Mark Salyzynbeb6abe2019-07-29 09:35:18 -0700421 auto fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC);
422 dup2(fd, STDOUT_FILENO);
423 dup2(fd, STDERR_FILENO);
424 close(fd);
Tom Cherry44aceed2018-08-03 13:36:18 -0700425 execv(path, const_cast<char**>(args));
426
427 // execv() only returns if an error happened, in which case we
428 // panic and never fall through this conditional.
429 PLOG(FATAL) << "execv(\"" << path << "\") failed";
430
431 return 1;
432}
433
434} // namespace init
435} // namespace android