From ffa52e9c6f8b37e8a9430c66ef117a07c43c797e Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Thu, 14 May 2020 09:20:30 -0700 Subject: [PATCH] init: support wait timeout with more precision A one second timeout is so coarse and can affect boot time when the possibility that the file does not exist. Switch to accepting a floating point number for seconds for the wait for file command. Signed-off-by: Mark Salyzyn Bug: 151950334 Test: wait_for_file sleep 0.05 reports an appropriate delay Change-Id: I8d8ed386519ab54270b05ce91663d0add30f12e7 --- init/README.md | 3 ++- init/builtins.cpp | 8 +++++--- init/check_builtins.cpp | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/init/README.md b/init/README.md index 726c0cc94..b70366b8f 100644 --- a/init/README.md +++ b/init/README.md @@ -644,7 +644,8 @@ provides the `aidl_lazy_test_1` interface. `wait [ ]` > Poll for the existence of the given file and return when found, or the timeout has been reached. If timeout is not specified it - currently defaults to five seconds. + currently defaults to five seconds. The timeout value can be + fractional seconds, specified in floating point notation. `wait_for_prop ` > Wait for system property _name_ to be _value_. Properties are expanded diff --git a/init/builtins.cpp b/init/builtins.cpp index 200bfff7d..149a766b3 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -1065,11 +1066,12 @@ static Result do_load_system_props(const BuiltinArguments& args) { static Result do_wait(const BuiltinArguments& args) { auto timeout = kCommandRetryTimeout; if (args.size() == 3) { - int timeout_int; - if (!android::base::ParseInt(args[2], &timeout_int)) { + double timeout_double; + if (!android::base::ParseDouble(args[2], &timeout_double, 0)) { return Error() << "failed to parse timeout"; } - timeout = std::chrono::seconds(timeout_int); + timeout = std::chrono::duration_cast( + std::chrono::duration(timeout_double)); } if (wait_for_file(args[1].c_str(), timeout) != 0) { diff --git a/init/check_builtins.cpp b/init/check_builtins.cpp index d62ecb080..d1a84f306 100644 --- a/init/check_builtins.cpp +++ b/init/check_builtins.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -205,8 +206,8 @@ Result check_sysclktz(const BuiltinArguments& args) { Result check_wait(const BuiltinArguments& args) { if (args.size() == 3 && !args[2].empty()) { - int timeout_int; - if (!android::base::ParseInt(args[2], &timeout_int)) { + double timeout_double; + if (!android::base::ParseDouble(args[2], &timeout_double, 0)) { return Error() << "failed to parse timeout"; } }