From 1c3a53f03ca3c2c647f83cd8b8ae7e18c5c7bc69 Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Thu, 22 Jun 2017 16:50:31 -0700 Subject: [PATCH] init: cleanup some string usage 1) property_set() takes const std::string& for both of its arguments, so stop using .c_str() with its parameters 2) Simplify a few places where StringPrintf() is used to concatenate strings 3) Use std::to_string() instead of StringPrintf() where it's better suited Test: Boot bullhead Test: init unit tests Change-Id: I68ebda0e469f6230c8f9ad3c8d5f9444e0c4fdfd --- init/action.cpp | 8 +++----- init/builtins.cpp | 13 ++++--------- init/descriptors.cpp | 5 +++-- init/init.cpp | 17 ++++++++--------- init/property_service.cpp | 5 +---- init/service.cpp | 17 ++++++++--------- init/signal_handler.cpp | 1 - init/ueventd.cpp | 1 - 8 files changed, 27 insertions(+), 40 deletions(-) diff --git a/init/action.cpp b/init/action.cpp index 21abe0273..206100ece 100644 --- a/init/action.cpp +++ b/init/action.cpp @@ -18,13 +18,11 @@ #include #include -#include #include #include "util.h" using android::base::Join; -using android::base::StringPrintf; Command::Command(BuiltinFunction f, const std::vector& args, int line) : func_(f), args_(args), line_(line) {} @@ -98,10 +96,10 @@ void Action::ExecuteCommand(const Command& command) const { android::base::GetMinimumLogSeverity() <= android::base::DEBUG) { std::string trigger_name = BuildTriggersString(); std::string cmd_str = command.BuildCommandString(); - std::string source = StringPrintf(" (%s:%d)", filename_.c_str(), command.line()); - LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << source - << " returned " << result << " took " << duration_ms << "ms."; + LOG(INFO) << "Command '" << cmd_str << "' action=" << trigger_name << " (" << filename_ + << ":" << command.line() << ") returned " << result << " took " << duration_ms + << "ms."; } } diff --git a/init/builtins.cpp b/init/builtins.cpp index 1eacb363e..c78bec7db 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -43,7 +43,6 @@ #include #include #include -#include #include #include #include @@ -535,11 +534,10 @@ static int do_mount_all(const std::vector& args) { } } - std::string prop_name = android::base::StringPrintf("ro.boottime.init.mount_all.%s", - prop_post_fix); + std::string prop_name = "ro.boottime.init.mount_all."s + prop_post_fix; Timer t; int ret = mount_fstab(fstabfile, mount_mode); - property_set(prop_name.c_str(), std::to_string(t.duration_ms()).c_str()); + property_set(prop_name, std::to_string(t.duration_ms())); if (import_rc) { /* Paths of .rc files are specified at the 2nd argument and beyond */ @@ -567,9 +565,7 @@ static int do_swapon_all(const std::vector& args) { } static int do_setprop(const std::vector& args) { - const char* name = args[1].c_str(); - const char* value = args[2].c_str(); - property_set(name, value); + property_set(args[1], args[2]); return 0; } @@ -652,8 +648,7 @@ static int do_verity_load_state(const std::vector& args) { static void verity_update_property(fstab_rec *fstab, const char *mount_point, int mode, int status) { - property_set(android::base::StringPrintf("partition.%s.verified", mount_point).c_str(), - android::base::StringPrintf("%d", mode).c_str()); + property_set("partition."s + mount_point + ".verified", std::to_string(mode)); } static int do_verity_update_state(const std::vector& args) { diff --git a/init/descriptors.cpp b/init/descriptors.cpp index 6f729a372..ed65a055a 100644 --- a/init/descriptors.cpp +++ b/init/descriptors.cpp @@ -58,7 +58,7 @@ void DescriptorInfo::CreateAndPublish(const std::string& globalContext) const { std::for_each(publishedName.begin(), publishedName.end(), [] (char& c) { c = isalnum(c) ? c : '_'; }); - std::string val = android::base::StringPrintf("%d", fd); + std::string val = std::to_string(fd); add_environment(publishedName.c_str(), val.c_str()); // make sure we don't close on exec @@ -74,7 +74,8 @@ SocketInfo::SocketInfo(const std::string& name, const std::string& type, uid_t u } void SocketInfo::Clean() const { - unlink(android::base::StringPrintf(ANDROID_SOCKET_DIR "/%s", name().c_str()).c_str()); + std::string path = android::base::StringPrintf("%s/%s", ANDROID_SOCKET_DIR, name().c_str()); + unlink(path.c_str()); } int SocketInfo::Create(const std::string& context) const { diff --git a/init/init.cpp b/init/init.cpp index 999fadad7..d6e913069 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include #include @@ -71,9 +70,10 @@ #include "util.h" #include "watchdogd.h" +using namespace std::string_literals; + using android::base::boot_clock; using android::base::GetProperty; -using android::base::StringPrintf; struct selabel_handle *sehandle; struct selabel_handle *sehandle_prop; @@ -219,7 +219,7 @@ static int wait_for_coldboot_done_action(const std::vector& args) { panic(); } - property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration_ms()).c_str()); + property_set("ro.boottime.init.cold_boot_wait", std::to_string(t.duration_ms())); return 0; } @@ -449,14 +449,14 @@ static void import_kernel_nv(const std::string& key, const std::string& value, b if (for_emulator) { // In the emulator, export any kernel option with the "ro.kernel." prefix. - property_set(StringPrintf("ro.kernel.%s", key.c_str()).c_str(), value.c_str()); + property_set("ro.kernel." + key, value); return; } if (key == "qemu") { strlcpy(qemu, value.c_str(), sizeof(qemu)); } else if (android::base::StartsWith(key, "androidboot.")) { - property_set(StringPrintf("ro.boot.%s", key.c_str() + 12).c_str(), value.c_str()); + property_set("ro.boot." + key.substr(12), value); } } @@ -487,7 +487,7 @@ static void export_kernel_boot_props() { }; for (size_t i = 0; i < arraysize(prop_map); i++) { std::string value = GetProperty(prop_map[i].src_prop, ""); - property_set(prop_map[i].dst_prop, (!value.empty()) ? value.c_str() : prop_map[i].default_value); + property_set(prop_map[i].dst_prop, (!value.empty()) ? value : prop_map[i].default_value); } } @@ -511,8 +511,7 @@ static void process_kernel_dt() { android::base::ReadFileToString(file_name, &dt_file); std::replace(dt_file.begin(), dt_file.end(), ',', '.'); - std::string property_name = StringPrintf("ro.boot.%s", dp->d_name); - property_set(property_name.c_str(), dt_file.c_str()); + property_set("ro.boot."s + dp->d_name, dt_file); } } @@ -1002,7 +1001,7 @@ int main(int argc, char** argv) { static constexpr uint32_t kNanosecondsPerMillisecond = 1e6; uint64_t start_ms = start_time.time_since_epoch().count() / kNanosecondsPerMillisecond; - setenv("INIT_STARTED_AT", StringPrintf("%" PRIu64, start_ms).c_str(), 1); + setenv("INIT_STARTED_AT", std::to_string(start_ms).c_str(), 1); char* path = argv[0]; char* args[] = { path, nullptr }; diff --git a/init/property_service.cpp b/init/property_service.cpp index b43da4eec..31e6e4810 100644 --- a/init/property_service.cpp +++ b/init/property_service.cpp @@ -45,7 +45,6 @@ #include #include #include -#include #include #include #include @@ -56,8 +55,6 @@ #include "init.h" #include "util.h" -using android::base::StringPrintf; - #define PERSISTENT_PROPERTY_DIR "/data/property" #define RECOVERY_MOUNT_POINT "/recovery" @@ -714,7 +711,7 @@ void load_recovery_id_prop() { boot_img_hdr hdr; if (android::base::ReadFully(fd, &hdr, sizeof(hdr))) { std::string hex = bytes_to_hex(reinterpret_cast(hdr.id), sizeof(hdr.id)); - property_set("ro.recovery_id", hex.c_str()); + property_set("ro.recovery_id", hex); } else { PLOG(ERROR) << "error reading /recovery"; } diff --git a/init/service.cpp b/init/service.cpp index b73ddfb69..d8c687500 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -198,13 +198,12 @@ void Service::NotifyStateChange(const std::string& new_state) const { return; } - std::string prop_name = StringPrintf("init.svc.%s", name_.c_str()); - property_set(prop_name.c_str(), new_state.c_str()); + std::string prop_name = "init.svc." + name_; + property_set(prop_name, new_state); if (new_state == "running") { uint64_t start_ns = time_started_.time_since_epoch().count(); - property_set(StringPrintf("ro.boottime.%s", name_.c_str()).c_str(), - StringPrintf("%" PRIu64, start_ns).c_str()); + property_set("ro.boottime." + name_, std::to_string(start_ns)); } } @@ -716,7 +715,7 @@ bool Service::Start() { StringPrintf("/dev/cpuset%stasks", default_cpuset.c_str())); } } - std::string pid_str = StringPrintf("%d", getpid()); + std::string pid_str = std::to_string(getpid()); for (const auto& file : writepid_files_) { if (!WriteStringToFile(pid_str, file)) { PLOG(ERROR) << "couldn't write " << pid_str << " to " << file; @@ -757,7 +756,7 @@ bool Service::Start() { } if (oom_score_adjust_ != -1000) { - std::string oom_str = StringPrintf("%d", oom_score_adjust_); + std::string oom_str = std::to_string(oom_score_adjust_); std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid); if (!WriteStringToFile(oom_str, oom_file)) { PLOG(ERROR) << "couldn't write oom_score_adj: " << strerror(errno); @@ -776,9 +775,9 @@ bool Service::Start() { } if ((flags_ & SVC_EXEC) != 0) { - LOG(INFO) << android::base::StringPrintf( - "SVC_EXEC pid %d (uid %d gid %d+%zu context %s) started; waiting...", pid_, uid_, gid_, - supp_gids_.size(), !seclabel_.empty() ? seclabel_.c_str() : "default"); + LOG(INFO) << "SVC_EXEC pid " << pid_ << " (uid " << uid_ << " gid " << gid_ << "+" + << supp_gids_.size() << " context " + << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting..."; } NotifyStateChange("running"); diff --git a/init/signal_handler.cpp b/init/signal_handler.cpp index 4d56d8472..183837b57 100644 --- a/init/signal_handler.cpp +++ b/init/signal_handler.cpp @@ -21,7 +21,6 @@ #include #include -#include #include "init.h" #include "service.h" diff --git a/init/ueventd.cpp b/init/ueventd.cpp index 4982b7774..2fd805873 100644 --- a/init/ueventd.cpp +++ b/init/ueventd.cpp @@ -29,7 +29,6 @@ #include #include -#include #include #include