已开启
update: 更新文件 device_info_service.cpp #4954
sunqingao创建于 3 天前
update: 更新文件 device_info_service.cpp #4954
已开启
共 2 个文件变更+101-3
| @@ -77,7 +77,12 @@ if (!defined(ohos_lite)) { | |||
| 77 | external_deps += [ "access_token:libaccesstoken_sdk" ] | 77 | external_deps += [ "access_token:libaccesstoken_sdk" ] |
| 78 | defines += [ "INIT_SUPPORT_ACCESS_TOKEN" ] | 78 | defines += [ "INIT_SUPPORT_ACCESS_TOKEN" ] |
| 79 | } | 79 | } |
| 80 | + if (defined(global_parts_info) && | ||
| 81 | + defined(global_parts_info.resourceschedule_memmgr)) { | ||
| 82 | + defines += [ "INIT_SUPPORT_MEMMGR" ] | ||
| 83 | + } | ||
| 80 | install_images = [ "system" ] | 84 | install_images = [ "system" ] |
| 85 | + libs = [ "dl" ] | ||
| 81 | part_name = "init" | 86 | part_name = "init" |
| 82 | subsystem_name = "startup" | 87 | subsystem_name = "startup" |
| 83 | } | 88 | } |
| @@ -16,6 +16,8 @@ | |||
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | + | ||
| 20 | + | ||
| 19 | 21 | ||
| 20 | 22 | ||
| 21 | 23 | ||
| @@ -50,6 +52,82 @@ static const int DEVICE_INFO_EXIT_TIMEOUT_S = 3; | |||
| 50 | 52 | ||
| 51 | static const int DEVICE_INFO_EXIT_WAITTIMES = 12; | 53 | static const int DEVICE_INFO_EXIT_WAITTIMES = 12; |
| 52 | 54 | ||
| 55 | + | ||
| 56 | +namespace { | ||
| 57 | +constexpr const char *MEMMGR_CLIENT_SO = "libmemmgrclient.z.so"; | ||
| 58 | +constexpr int MEMMGR_SA_ID = 1909; | ||
| 59 | +constexpr int DEVINFO_SA_ID = 3902; | ||
| 60 | +constexpr int DEVINFO_SERVICE_TYPE = 1; // SA service | ||
| 61 | + | ||
| 62 | +using NotifyProcessStatusFunc = int32_t (*)(int32_t pid, int32_t type, int32_t status, int32_t saId); | ||
| 63 | +using SetCriticalFunc = int32_t (*)(int32_t pid, bool isCritical, int32_t saId); | ||
| 64 | + | ||
| 65 | +struct MemMgrHandle { | ||
| 66 | + void *handle = nullptr; | ||
| 67 | + NotifyProcessStatusFunc notifyProcessStatus = nullptr; | ||
| 68 | + SetCriticalFunc setCritical = nullptr; | ||
| 69 | + bool available = false; | ||
| 70 | +}; | ||
| 71 | + | ||
| 72 | +static MemMgrHandle g_memmgrHandle; | ||
| 73 | +static std::once_flag g_memmgrInitFlag; | ||
| 74 | + | ||
| 75 | +static void InitMemMgrHandle(void) | ||
| 76 | +{ | ||
| 77 | + g_memmgrHandle.handle = dlopen(MEMMGR_CLIENT_SO, RTLD_NOW); | ||
| 78 | + if (g_memmgrHandle.handle == nullptr) { | ||
| 79 | + DINFO_LOGE("dlopen %{public}s failed: %{public}s", MEMMGR_CLIENT_SO, dlerror()); | ||
| 80 | + return; | ||
| 81 | + } | ||
| 82 | + g_memmgrHandle.notifyProcessStatus = reinterpret_cast<NotifyProcessStatusFunc>( | ||
| 83 | + dlsym(g_memmgrHandle.handle, "NotifyProcessStatus")); | ||
| 84 | + g_memmgrHandle.setCritical = reinterpret_cast<SetCriticalFunc>( | ||
| 85 | + dlsym(g_memmgrHandle.handle, "SetCritical")); | ||
| 86 | + if (g_memmgrHandle.notifyProcessStatus == nullptr || g_memmgrHandle.setCritical == nullptr) { | ||
| 87 | + DINFO_LOGE("dlsym NotifyProcessStatus or SetCritical failed: %{public}s", dlerror()); | ||
| 88 | + dlclose(g_memmgrHandle.handle); | ||
| 89 | + g_memmgrHandle.handle = nullptr; | ||
| 90 | + return; | ||
| 91 | + } | ||
| 92 | + g_memmgrHandle.available = true; | ||
| 93 | + DINFO_LOGI("MemMgr client library loaded successfully"); | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +static void MemMgrNotifyProcessStatus(int32_t pid, int32_t type, int32_t status, int32_t saId) | ||
| 97 | +{ | ||
| 98 | + std::call_once(g_memmgrInitFlag, InitMemMgrHandle); | ||
| 99 | + if (!g_memmgrHandle.available) { | ||
| 100 | + return; | ||
| 101 | + } | ||
| 102 | + int32_t ret = g_memmgrHandle.notifyProcessStatus(pid, type, status, saId); | ||
| 103 | + if (ret != 0) { | ||
| 104 | + DINFO_LOGE("NotifyProcessStatus failed, ret=%{public}d", ret); | ||
| 105 | + } | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +static void MemMgrSetCritical(int32_t pid, bool isCritical, int32_t saId) | ||
| 109 | +{ | ||
| 110 | + std::call_once(g_memmgrInitFlag, InitMemMgrHandle); | ||
| 111 | + if (!g_memmgrHandle.available) { | ||
| 112 | + return; | ||
| 113 | + } | ||
| 114 | + int32_t ret = g_memmgrHandle.setCritical(pid, isCritical, saId); | ||
| 115 | + if (ret != 0) { | ||
| 116 | + DINFO_LOGE("SetCritical(%{public}d) failed, ret=%{public}d", isCritical, ret); | ||
| 117 | + } | ||
| 118 | +} | ||
| 119 | + | ||
| 120 | +static void MemMgrCleanup(void) | ||
| 121 | +{ | ||
| 122 | + if (g_memmgrHandle.handle != nullptr) { | ||
| 123 | + dlclose(g_memmgrHandle.handle); | ||
| 124 | + g_memmgrHandle.handle = nullptr; | ||
| 125 | + } | ||
| 126 | + g_memmgrHandle.available = false; | ||
| 127 | +} | ||
| 128 | +} // anonymous namespace | ||
| 129 | + | ||
| 130 | + | ||
| 53 | static int UnloadDeviceInfoSa(void) | 131 | static int UnloadDeviceInfoSa(void) |
| 54 | { | 132 | { |
| 55 | { | 133 | { |
| @@ -74,7 +152,7 @@ int32_t DeviceInfoService::GetUdid(std::string& result) | |||
| 74 | int ret = ERR_FAIL; | 152 | int ret = ERR_FAIL; |
| 75 | char localDeviceInfo[UDID_LEN] = {0}; | 153 | char localDeviceInfo[UDID_LEN] = {0}; |
| 76 | ret = GetDevUdid_(localDeviceInfo, UDID_LEN); | 154 | ret = GetDevUdid_(localDeviceInfo, UDID_LEN); |
| 77 | - DINFO_CHECK(ret == 0, return ret, "failed get dev udid"); | 155 | + DINFO_CHECK(ret == 0, return ret, "Failed to get dev udid"); |
| 78 | result = std::string(localDeviceInfo); | 156 | result = std::string(localDeviceInfo); |
| 79 | return ret; | 157 | return ret; |
| 80 | } | 158 | } |
| @@ -83,7 +161,7 @@ int32_t DeviceInfoService::GetSerialID(std::string& result) | |||
| 83 | { | 161 | { |
| 84 | int ret = ERR_FAIL; | 162 | int ret = ERR_FAIL; |
| 85 | const char *serialNumber = GetSerial_(); | 163 | const char *serialNumber = GetSerial_(); |
| 86 | - DINFO_CHECK(serialNumber != nullptr, return ret, "failed get serialNumber"); | 164 | + DINFO_CHECK(serialNumber != nullptr, return ret, "Failed to get serialNumber"); |
| 87 | result = std::string(serialNumber); | 165 | result = std::string(serialNumber); |
| 88 | return 0; | 166 | return 0; |
| 89 | } | 167 | } |
| @@ -93,7 +171,7 @@ int32_t DeviceInfoService::GetDiskSN(std::string& result) | |||
| 93 | int ret = ERR_FAIL; | 171 | int ret = ERR_FAIL; |
| 94 | char diskSN[DISK_SN_LEN] = {0}; | 172 | char diskSN[DISK_SN_LEN] = {0}; |
| 95 | ret = GetDiskSN_(diskSN, DISK_SN_LEN); | 173 | ret = GetDiskSN_(diskSN, DISK_SN_LEN); |
| 96 | - DINFO_CHECK(ret == 0, return ret, "failed get disk SN"); | 174 | + DINFO_CHECK(ret == 0, return ret, "Failed to get disk SN"); |
| 97 | result = std::string(diskSN); | 175 | result = std::string(diskSN); |
| 98 | return ret; | 176 | return ret; |
| 99 | } | 177 | } |
| @@ -120,11 +198,17 @@ int32_t DeviceInfoService::CallbackEnter(uint32_t code) | |||
| 120 | return SYSPARAM_PERMISSION_DENIED; | 198 | return SYSPARAM_PERMISSION_DENIED; |
| 121 | } | 199 | } |
| 122 | } | 200 | } |
| 201 | + | ||
| 202 | + MemMgrSetCritical(getpid(), true, DEVINFO_SA_ID); | ||
| 203 | + | ||
| 123 | return 0; | 204 | return 0; |
| 124 | } | 205 | } |
| 125 | 206 | ||
| 126 | int32_t DeviceInfoService::CallbackExit(uint32_t code, int32_t result) | 207 | int32_t DeviceInfoService::CallbackExit(uint32_t code, int32_t result) |
| 127 | { | 208 | { |
| 209 | + | ||
| 210 | + MemMgrSetCritical(getpid(), false, DEVINFO_SA_ID); | ||
| 211 | + | ||
| 128 | return 0; | 212 | return 0; |
| 129 | } | 213 | } |
| 130 | 214 | ||
| @@ -156,6 +240,10 @@ void DeviceInfoService::OnStart(void) | |||
| 156 | int level = GetIntParameter(INIT_DEBUG_LEVEL, (int)INIT_INFO); | 240 | int level = GetIntParameter(INIT_DEBUG_LEVEL, (int)INIT_INFO); |
| 157 | SetInitLogLevel((InitLogLevel)level); | 241 | SetInitLogLevel((InitLogLevel)level); |
| 158 | DINFO_LOGI("DeviceInfoService OnStart"); | 242 | DINFO_LOGI("DeviceInfoService OnStart"); |
| 243 | + | ||
| 244 | + MemMgrNotifyProcessStatus(getpid(), DEVINFO_SERVICE_TYPE, 1, DEVINFO_SA_ID); | ||
| 245 | + MemMgrSetCritical(getpid(), true, DEVINFO_SA_ID); | ||
| 246 | + | ||
| 159 | bool res = Publish(this); | 247 | bool res = Publish(this); |
| 160 | if (!res) { | 248 | if (!res) { |
| 161 | DINFO_LOGE("DeviceInfoService Publish failed"); | 249 | DINFO_LOGE("DeviceInfoService Publish failed"); |
| @@ -166,6 +254,11 @@ void DeviceInfoService::OnStart(void) | |||
| 166 | 254 | ||
| 167 | void DeviceInfoService::OnStop(void) | 255 | void DeviceInfoService::OnStop(void) |
| 168 | { | 256 | { |
| 257 | + | ||
| 258 | + MemMgrSetCritical(getpid(), false, DEVINFO_SA_ID); | ||
| 259 | + MemMgrNotifyProcessStatus(getpid(), DEVINFO_SERVICE_TYPE, 0, DEVINFO_SA_ID); | ||
| 260 | + MemMgrCleanup(); | ||
| 261 | + | ||
| 169 | threadStarted_ = false; | 262 | threadStarted_ = false; |
| 170 | DINFO_LOGI("DeviceInfoService OnStop"); | 263 | DINFO_LOGI("DeviceInfoService OnStop"); |
| 171 | } | 264 | } |