storaged: use sp<> to keep refcount for storaged_t object
BatteryListener takes sp<> as parameter. Passing raw storaged_t ptr
to BatteryListener will cause the ptr to be freed when BatteryListener
releases the sp<>. Keep a refcount in storaged to prevent this from
happening.
Test: kill healthd while storaged is running
Bug: 36652060
Change-Id: I96bc45a3bedb39eb7158b8f6c86334b5b31c9346
diff --git a/storaged/main.cpp b/storaged/main.cpp
index 2f2273d..4d1e430 100644
--- a/storaged/main.cpp
+++ b/storaged/main.cpp
@@ -42,11 +42,11 @@
#include <storaged_service.h>
#include <storaged_utils.h>
-storaged_t storaged;
+sp<storaged_t> storaged;
// Function of storaged's main thread
-void* storaged_main(void* s) {
- storaged_t* storaged = (storaged_t*)s;
+void* storaged_main(void* /* unused */) {
+ storaged = new storaged_t();
storaged->init_battery_service();
@@ -116,7 +116,7 @@
report_storage_health();
// Start the main thread of storaged
pthread_t storaged_main_thread;
- errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged);
+ errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
if (errno != 0) {
PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
return -1;