From 2d0fdaaafc5d2925b8ef7708a950f6b599892b54 Mon Sep 17 00:00:00 2001 From: Wei Wang Date: Thu, 2 Feb 2017 10:52:39 -0800 Subject: [PATCH] init: clean up exec command Add wait time log for exec service execution time Fix memory leak when exec service failed to start Test: on marlin Bug: 34518530 Change-Id: I01736bd9b1429414f3dc91dd5d02d88a681f0985 --- init/builtins.cpp | 10 +++++++--- init/init.cpp | 24 +++++++++++++++++++++--- init/init.h | 7 +++++-- init/service.cpp | 2 +- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/init/builtins.cpp b/init/builtins.cpp index 965a81fbe..2388edcf7 100644 --- a/init/builtins.cpp +++ b/init/builtins.cpp @@ -265,10 +265,14 @@ static int do_exec(const std::vector& args) { if (!svc) { return -1; } - if (!svc->Start()) { + if (!start_waiting_for_exec()) { + return -1; + } + if (!svc->Start()) { + stop_waiting_for_exec(); + ServiceManager::GetInstance().RemoveService(*svc); return -1; } - waiting_for_exec = true; return 0; } @@ -1018,7 +1022,7 @@ static int do_wait_for_prop(const std::vector& args) { << "\") failed: value too long"; return -1; } - if (!wait_property(name, value)) { + if (!start_waiting_for_property(name, value)) { LOG(ERROR) << "do_wait_for_prop(\"" << name << "\", \"" << value << "\") failed: init already in waiting"; return -1; diff --git a/init/init.cpp b/init/init.cpp index c8c18d2f3..43f601f69 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -82,7 +82,7 @@ static time_t process_needs_restart_at; const char *ENV[32]; -bool waiting_for_exec = false; +static std::unique_ptr waiting_for_exec(nullptr); static int epoll_fd = -1; @@ -131,7 +131,24 @@ int add_environment(const char *key, const char *val) return -1; } -bool wait_property(const char *name, const char *value) +bool start_waiting_for_exec() +{ + if (waiting_for_exec) { + return false; + } + waiting_for_exec.reset(new Timer()); + return true; +} + +void stop_waiting_for_exec() +{ + if (waiting_for_exec) { + LOG(INFO) << "Wait for exec took " << *waiting_for_exec; + waiting_for_exec.reset(); + } +} + +bool start_waiting_for_property(const char *name, const char *value) { if (waiting_for_prop) { return false; @@ -142,7 +159,8 @@ bool wait_property(const char *name, const char *value) wait_prop_value = value; waiting_for_prop.reset(new Timer()); } else { - LOG(INFO) << "wait_property(\"" << name << "\", \"" << value << "\"): already set"; + LOG(INFO) << "start_waiting_for_property(\"" + << name << "\", \"" << value << "\"): already set"; } return true; } diff --git a/init/init.h b/init/init.h index 4e4da328b..3768c02b2 100644 --- a/init/init.h +++ b/init/init.h @@ -23,7 +23,6 @@ class Action; class Service; extern const char *ENV[32]; -extern bool waiting_for_exec; extern std::string default_console; extern struct selabel_handle *sehandle; extern struct selabel_handle *sehandle_prop; @@ -36,6 +35,10 @@ void register_epoll_handler(int fd, void (*fn)()); int add_environment(const char* key, const char* val); -bool wait_property(const char *name, const char *value); +bool start_waiting_for_exec(); + +void stop_waiting_for_exec(); + +bool start_waiting_for_property(const char *name, const char *value); #endif /* _INIT_INIT_H */ diff --git a/init/service.cpp b/init/service.cpp index 0f7f62fe9..e186f27a8 100644 --- a/init/service.cpp +++ b/init/service.cpp @@ -997,7 +997,7 @@ bool ServiceManager::ReapOneProcess() { } if (svc->Reap()) { - waiting_for_exec = false; + stop_waiting_for_exec(); RemoveService(*svc); }