@@ -85,12 +85,11 @@ static bool shutting_down = false;
85
85
86
86
static const std::set<std::string> kDebuggingServices {" tombstoned" , " logd" , " adbd" , " console" };
87
87
88
- static std::vector<Service*> GetDebuggingServices (bool only_post_data) {
89
- std::vector<Service*> ret;
90
- ret.reserve (kDebuggingServices .size ());
88
+ static std::set<std::string> GetPostDataDebuggingServices () {
89
+ std::set<std::string> ret;
91
90
for (const auto & s : ServiceList::GetInstance ()) {
92
- if (kDebuggingServices .count (s->name ()) && (!only_post_data || s->is_post_data () )) {
93
- ret.push_back (s. get ());
91
+ if (kDebuggingServices .count (s->name ()) && s->is_post_data ()) {
92
+ ret.insert (s-> name ());
94
93
}
95
94
}
96
95
return ret;
@@ -503,13 +502,18 @@ static Result<void> KillZramBackingDevice() {
503
502
504
503
// Stops given services, waits for them to be stopped for |timeout| ms.
505
504
// If terminate is true, then SIGTERM is sent to services, otherwise SIGKILL is sent.
506
- static void StopServices (const std::vector<Service*>& services, std::chrono::milliseconds timeout,
505
+ // Note that services are stopped in order given by |ServiceList::services_in_shutdown_order|
506
+ // function.
507
+ static void StopServices (const std::set<std::string>& services, std::chrono::milliseconds timeout,
507
508
bool terminate) {
508
509
LOG (INFO) << " Stopping " << services.size () << " services by sending "
509
510
<< (terminate ? " SIGTERM" : " SIGKILL" );
510
511
std::vector<pid_t > pids;
511
512
pids.reserve (services.size ());
512
- for (const auto & s : services) {
513
+ for (const auto & s : ServiceList::GetInstance ().services_in_shutdown_order ()) {
514
+ if (services.count (s->name ()) == 0 ) {
515
+ continue ;
516
+ }
513
517
if (s->pid () > 0 ) {
514
518
pids.push_back (s->pid ());
515
519
}
@@ -529,12 +533,12 @@ static void StopServices(const std::vector<Service*>& services, std::chrono::mil
529
533
530
534
// Like StopServices, but also logs all the services that failed to stop after the provided timeout.
531
535
// Returns number of violators.
532
- static int StopServicesAndLogViolations (const std::vector<Service* >& services,
536
+ static int StopServicesAndLogViolations (const std::set<std::string >& services,
533
537
std::chrono::milliseconds timeout, bool terminate) {
534
538
StopServices (services, timeout, terminate);
535
539
int still_running = 0 ;
536
- for (const auto & s : services ) {
537
- if (s->IsRunning ()) {
540
+ for (const auto & s : ServiceList::GetInstance () ) {
541
+ if (s->IsRunning () && services. count (s-> name ()) ) {
538
542
LOG (ERROR) << " [service-misbehaving] : service '" << s->name () << " ' is still running "
539
543
<< timeout.count () << " ms after receiving "
540
544
<< (terminate ? " SIGTERM" : " SIGKILL" );
@@ -620,8 +624,7 @@ static void DoReboot(unsigned int cmd, const std::string& reason, const std::str
620
624
621
625
// watchdogd is a vendor specific component but should be alive to complete shutdown safely.
622
626
const std::set<std::string> to_starts{" watchdogd" };
623
- std::vector<Service*> stop_first;
624
- stop_first.reserve (ServiceList::GetInstance ().services ().size ());
627
+ std::set<std::string> stop_first;
625
628
for (const auto & s : ServiceList::GetInstance ()) {
626
629
if (kDebuggingServices .count (s->name ())) {
627
630
// keep debugging tools until non critical ones are all gone.
@@ -639,7 +642,7 @@ static void DoReboot(unsigned int cmd, const std::string& reason, const std::str
639
642
<< " ': " << result.error ();
640
643
}
641
644
} else {
642
- stop_first.push_back (s. get ());
645
+ stop_first.insert (s-> name ());
643
646
}
644
647
}
645
648
@@ -702,7 +705,7 @@ static void DoReboot(unsigned int cmd, const std::string& reason, const std::str
702
705
LOG (INFO) << " vold not running, skipping vold shutdown" ;
703
706
}
704
707
// logcat stopped here
705
- StopServices (GetDebuggingServices ( false /* only_post_data */ ) , 0ms, false /* SIGKILL */ );
708
+ StopServices (kDebuggingServices , 0ms, false /* SIGKILL */ );
706
709
// 4. sync, try umount, and optionally run fsck for user shutdown
707
710
{
708
711
Timer sync_timer;
@@ -784,17 +787,17 @@ static Result<void> DoUserspaceReboot() {
784
787
sub_reason = " resetprop" ;
785
788
return Error () << " Failed to reset sys.powerctl property" ;
786
789
}
787
- std::vector<Service* > stop_first;
790
+ std::set<std::string > stop_first;
788
791
// Remember the services that were enabled. We will need to manually enable them again otherwise
789
792
// triggers like class_start won't restart them.
790
- std::vector<Service*> were_enabled;
791
- stop_first.reserve (ServiceList::GetInstance ().services ().size ());
793
+ std::set<std::string> were_enabled;
792
794
for (const auto & s : ServiceList::GetInstance ().services_in_shutdown_order ()) {
793
795
if (s->is_post_data () && !kDebuggingServices .count (s->name ())) {
794
- stop_first.push_back (s );
796
+ stop_first.insert (s-> name () );
795
797
}
798
+ // TODO(ioffe): we should also filter out temporary services here.
796
799
if (s->is_post_data () && s->IsEnabled ()) {
797
- were_enabled.push_back (s );
800
+ were_enabled.insert (s-> name () );
798
801
}
799
802
}
800
803
{
@@ -814,8 +817,8 @@ static Result<void> DoUserspaceReboot() {
814
817
r > 0 ) {
815
818
auto fd = unique_fd (TEMP_FAILURE_RETRY (open (services_file_name.c_str (), flags, 0666 )));
816
819
android::base::WriteStringToFd (" Post-data services still running: \n " , fd);
817
- for (const auto & s : stop_first ) {
818
- if (s->IsRunning ()) {
820
+ for (const auto & s : ServiceList::GetInstance () ) {
821
+ if (s->IsRunning () && stop_first. count (s-> name ()) ) {
819
822
android::base::WriteStringToFd (s->name () + " \n " , fd);
820
823
}
821
824
}
@@ -830,13 +833,14 @@ static Result<void> DoUserspaceReboot() {
830
833
sub_reason = " vold_reset" ;
831
834
return result;
832
835
}
833
- if (int r = StopServicesAndLogViolations (GetDebuggingServices (true /* only_post_data */ ),
834
- sigkill_timeout, false /* SIGKILL */ );
836
+ const auto & debugging_services = GetPostDataDebuggingServices ();
837
+ if (int r = StopServicesAndLogViolations (debugging_services, sigkill_timeout,
838
+ false /* SIGKILL */ );
835
839
r > 0 ) {
836
840
auto fd = unique_fd (TEMP_FAILURE_RETRY (open (services_file_name.c_str (), flags, 0666 )));
837
841
android::base::WriteStringToFd (" Debugging services still running: \n " , fd);
838
- for (const auto & s : GetDebuggingServices ( true )) {
839
- if (s->IsRunning ()) {
842
+ for (const auto & s : ServiceList::GetInstance ( )) {
843
+ if (s->IsRunning () && debugging_services. count (s-> name ()) ) {
840
844
android::base::WriteStringToFd (s->name () + " \n " , fd);
841
845
}
842
846
}
@@ -866,9 +870,11 @@ static Result<void> DoUserspaceReboot() {
866
870
return false ;
867
871
});
868
872
// Re-enable services
869
- for (const auto & s : were_enabled) {
870
- LOG (INFO) << " Re-enabling service '" << s->name () << " '" ;
871
- s->Enable ();
873
+ for (const auto & s : ServiceList::GetInstance ()) {
874
+ if (were_enabled.count (s->name ())) {
875
+ LOG (INFO) << " Re-enabling service '" << s->name () << " '" ;
876
+ s->Enable ();
877
+ }
872
878
}
873
879
ServiceList::GetInstance ().ResetState ();
874
880
LeaveShutdown ();
0 commit comments