Skip to content

Commit fc17492

Browse files
Tom CherryGerrit Code Review
authored andcommitted
Merge "init: do not fork before doing (u)mount_all()"
2 parents c3d5e60 + 990483d commit fc17492

File tree

1 file changed

+14
-80
lines changed

1 file changed

+14
-80
lines changed

init/builtins.cpp

Lines changed: 14 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -451,78 +451,6 @@ static void import_late(const std::vector<std::string>& args, size_t start_index
451451
if (false) DumpState();
452452
}
453453

454-
/* handle_fstab
455-
*
456-
* Read the given fstab file and execute func on it.
457-
*/
458-
static Result<int> handle_fstab(const std::string& fstabfile, std::function<int(Fstab*)> func) {
459-
/*
460-
* Call fs_mgr_[u]mount_all() to [u]mount all filesystems. We fork(2) and
461-
* do the call in the child to provide protection to the main init
462-
* process if anything goes wrong (crash or memory leak), and wait for
463-
* the child to finish in the parent.
464-
*/
465-
pid_t pid = fork();
466-
if (pid > 0) {
467-
/* Parent. Wait for the child to return */
468-
int status;
469-
int wp_ret = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
470-
if (wp_ret == -1) {
471-
// Unexpected error code. We will continue anyway.
472-
PLOG(WARNING) << "waitpid failed";
473-
}
474-
475-
if (WIFEXITED(status)) {
476-
return WEXITSTATUS(status);
477-
} else {
478-
return Error() << "child aborted";
479-
}
480-
} else if (pid == 0) {
481-
/* child, call fs_mgr_[u]mount_all() */
482-
483-
// So we can always see what fs_mgr_[u]mount_all() does.
484-
// Only needed if someone explicitly changes the default log level in their init.rc.
485-
android::base::ScopedLogSeverity info(android::base::INFO);
486-
487-
Fstab fstab;
488-
ReadFstabFromFile(fstabfile, &fstab);
489-
490-
int child_ret = func(&fstab);
491-
492-
_exit(child_ret);
493-
} else {
494-
return Error() << "fork() failed";
495-
}
496-
}
497-
498-
/* mount_fstab
499-
*
500-
* Call fs_mgr_mount_all() to mount the given fstab
501-
*/
502-
static Result<int> mount_fstab(const std::string& fstabfile, int mount_mode) {
503-
return handle_fstab(fstabfile, [mount_mode](Fstab* fstab) {
504-
int ret = fs_mgr_mount_all(fstab, mount_mode);
505-
if (ret == -1) {
506-
PLOG(ERROR) << "fs_mgr_mount_all returned an error";
507-
}
508-
return ret;
509-
});
510-
}
511-
512-
/* umount_fstab
513-
*
514-
* Call fs_mgr_umount_all() to umount the given fstab
515-
*/
516-
static Result<int> umount_fstab(const std::string& fstabfile) {
517-
return handle_fstab(fstabfile, [](Fstab* fstab) {
518-
int ret = fs_mgr_umount_all(fstab);
519-
if (ret != 0) {
520-
PLOG(ERROR) << "fs_mgr_umount_all returned " << ret;
521-
}
522-
return ret;
523-
});
524-
}
525-
526454
/* Queue event based on fs_mgr return code.
527455
*
528456
* code: return code of fs_mgr_mount_all
@@ -609,7 +537,7 @@ static Result<Success> do_mount_all(const BuiltinArguments& args) {
609537
bool import_rc = true;
610538
bool queue_event = true;
611539
int mount_mode = MOUNT_MODE_DEFAULT;
612-
const auto& fstabfile = args[1];
540+
const auto& fstab_file = args[1];
613541
std::size_t path_arg_end = args.size();
614542
const char* prop_post_fix = "default";
615543

@@ -629,10 +557,12 @@ static Result<Success> do_mount_all(const BuiltinArguments& args) {
629557

630558
std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix;
631559
android::base::Timer t;
632-
auto mount_fstab_return_code = mount_fstab(fstabfile, mount_mode);
633-
if (!mount_fstab_return_code) {
634-
return Error() << "mount_fstab() failed " << mount_fstab_return_code.error();
560+
561+
Fstab fstab;
562+
if (!ReadFstabFromFile(fstab_file, &fstab)) {
563+
return Error() << "Could not read fstab";
635564
}
565+
auto mount_fstab_return_code = fs_mgr_mount_all(&fstab, mount_mode);
636566
property_set(prop_name, std::to_string(t.duration().count()));
637567

638568
if (import_rc) {
@@ -643,7 +573,7 @@ static Result<Success> do_mount_all(const BuiltinArguments& args) {
643573
if (queue_event) {
644574
/* queue_fs_event will queue event based on mount_fstab return code
645575
* and return processed return code*/
646-
auto queue_fs_result = queue_fs_event(*mount_fstab_return_code);
576+
auto queue_fs_result = queue_fs_event(mount_fstab_return_code);
647577
if (!queue_fs_result) {
648578
return Error() << "queue_fs_event() failed: " << queue_fs_result.error();
649579
}
@@ -654,9 +584,13 @@ static Result<Success> do_mount_all(const BuiltinArguments& args) {
654584

655585
/* umount_all <fstab> */
656586
static Result<Success> do_umount_all(const BuiltinArguments& args) {
657-
auto umount_fstab_return_code = umount_fstab(args[1]);
658-
if (!umount_fstab_return_code) {
659-
return Error() << "umount_fstab() failed " << umount_fstab_return_code.error();
587+
Fstab fstab;
588+
if (!ReadFstabFromFile(args[1], &fstab)) {
589+
return Error() << "Could not read fstab";
590+
}
591+
592+
if (auto result = fs_mgr_umount_all(&fstab); result != 0) {
593+
return Error() << "umount_fstab() failed " << result;
660594
}
661595
return Success();
662596
}

0 commit comments

Comments
 (0)