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
This commit is contained in:
Wei Wang 2017-02-02 10:52:39 -08:00
parent d85cf0fb7d
commit 2d0fdaaafc
4 changed files with 34 additions and 9 deletions

View File

@ -265,10 +265,14 @@ static int do_exec(const std::vector<std::string>& 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<std::string>& 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;

View File

@ -82,7 +82,7 @@ static time_t process_needs_restart_at;
const char *ENV[32];
bool waiting_for_exec = false;
static std::unique_ptr<Timer> 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;
}

View File

@ -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 */

View File

@ -997,7 +997,7 @@ bool ServiceManager::ReapOneProcess() {
}
if (svc->Reap()) {
waiting_for_exec = false;
stop_waiting_for_exec();
RemoveService(*svc);
}