merge som events

Signed-off-by:zhang_along<zhangalong@huawei.com>

Signed-off-by: zhangalong <zhangalong@huawei.com>
This commit is contained in:
zhangalong 2022-05-30 11:23:31 +08:00
parent 498b3a5a6f
commit 37b2dd32e7
7 changed files with 57 additions and 86 deletions

View File

@ -27,12 +27,13 @@ const std::string DOMAIN_STR = std::string(HiSysEventNameSpace::Domain::ACCOUNT)
#endif // HAS_HISYSEVENT_PART
}
void ReportServiceStartFail(int32_t errCode)
void ReportServiceStartFail(int32_t errCode, const std::string& errMsg)
{
#ifdef HAS_HISYSEVENT_PART
int ret = HiSysEventNameSpace::Write(DOMAIN_STR, "SERVICE_START_FAILED",
HiSysEventNameSpace::EventType::FAULT,
"ERROR_TYPE", errCode);
"ERROR_TYPE", errCode,
"ERROR_MSG", errMsg);
if (ret != 0) {
ACCOUNT_LOGE("hisysevent write failed! ret %{public}d. errCode %{public}d", ret, errCode);
}
@ -60,38 +61,6 @@ void ReportPermissionFail(int32_t callerUid, int32_t callerPid, const std::strin
#endif // HAS_HISYSEVENT_PART
}
void ReportTimeoutFail(const std::string& errMsg)
{
#ifdef HAS_HISYSEVENT_PART
int ret = HiSysEventNameSpace::Write(DOMAIN_STR, "TIMEOUT_EXCEPTION",
HiSysEventNameSpace::EventType::FAULT,
"ERROR_MSG", errMsg);
if (ret != 0) {
ACCOUNT_LOGE("hisysevent write failed! ret %{public}d. errMsg %{public}s.",
ret, errMsg.c_str());
}
#else // HAS_HISYSEVENT_PART
(void)errMsg;
#endif // HAS_HISYSEVENT_PART
}
void ReportOsAccountCESFail(int32_t id, const std::string& errMsg)
{
#ifdef HAS_HISYSEVENT_PART
int ret = HiSysEventNameSpace::Write(DOMAIN_STR, "OS_ACCOUNT_COMMON_EVENT_FAILED",
HiSysEventNameSpace::EventType::FAULT,
"OSACCOUNT_ID", id,
"ERROR_MSG", errMsg);
if (ret != 0) {
ACCOUNT_LOGE("hisysevent write failed! ret %{public}d. id %{public}d, errMsg %{public}s.",
ret, id, errMsg.c_str());
}
#else // HAS_HISYSEVENT_PART
(void)id;
(void)errMsg;
#endif // HAS_HISYSEVENT_PART
}
void ReportOhosAccountCESFail(int32_t oldStat, int32_t newStat, int32_t id)
{
#ifdef HAS_HISYSEVENT_PART

View File

@ -20,10 +20,8 @@
namespace OHOS {
namespace AccountSA {
void ReportServiceStartFail(int32_t errCode);
void ReportServiceStartFail(int32_t errCode, const std::string& errMsg);
void ReportPermissionFail(int32_t callerUid, int32_t callerPid, const std::string& permName);
void ReportTimeoutFail(const std::string& errMsg);
void ReportOsAccountCESFail(int32_t id, const std::string& errMsg);
void ReportOhosAccountCESFail(int32_t oldStat, int32_t newStat, int32_t id);
void ReportOhosAccountStateChange(int32_t id, int32_t operateType, int32_t oldStat, int32_t newStat);
void ReportKvStoreAccessFail(int32_t status, const std::string& errMsg);

View File

@ -16,13 +16,7 @@ domain: ACCOUNT
SERVICE_START_FAILED:
__BASE: {type: FAULT, level: CRITICAL, tag: usability, desc: service start errors}
ERROR_TYPE: {type: INT32, desc: error code}
OHOS_ACCOUNT_STATE_CHANGE_EVENT:
__BASE: {type: BEHAVIOR, level: MINOR, desc: ohosaccount state change events}
USER_ID: {type: INT32, desc: local account id}
OPERATION_TYPE: {type: INT32, desc: operation type}
OLD_STATE: {type: INT32, desc: ohos account old state}
NEW_STATE: {type: INT32, desc: ohos account new state}
ERROR_MSG: {type: STRING, desc: error message}
OHOS_ACCOUNT_COMMON_EVENT_FAILED:
__BASE: {type: FAULT, level: MINOR, tag: usability, desc: ohosaccount publish event errors}
@ -30,11 +24,6 @@ OHOS_ACCOUNT_COMMON_EVENT_FAILED:
NEW_STATE: {type: INT32, desc: ohos account new state}
USER_ID: {type: INT32, desc: local account id}
OS_ACCOUNT_COMMON_EVENT_FAILED:
__BASE: {type: FAULT, level: MINOR, tag: usability, desc: osaccount publish event errors}
OSACCOUNT_ID: {type: INT32, desc: the id of os account}
ERROR_MSG: {type: STRING, desc: error message}
KVSTORE_ACCESS_FAILED:
__BASE: {type: FAULT, level: MINOR, tag: usability, desc: kvstore related errors}
KVSTORE_STATUS: {type: INT32, desc: the status returned by kvstore}
@ -46,10 +35,6 @@ PERMISSION_EXCEPTION:
CALLER_PID: {type: INT32, desc: caller pid}
PERMISSION_NAME: {type: STRING, desc: permission name}
TIMEOUT_EXCEPTION:
__BASE: {type: FAULT, level: CRITICAL, tag: usability, desc: timeout related exceptions}
ERROR_MSG: {type: STRING, desc: timeout message}
OS_ACCOUNT_OPERATE_FAILED:
__BASE: {type: FAULT, level: CRITICAL, tag: usability, desc: osaccount operation errors}
OSACCOUNT_ID: {type: INT32, desc: the id of os account}
@ -72,3 +57,10 @@ OS_ACCOUNT_SWITCH_EVENT:
__BASE: {type: BEHAVIOR, level: MINOR, tag: usability, desc: osaccount switch event}
CURRENT_ID: {type: INT32, desc: the id of os account after switch}
OLD_ID: {type: INT32, desc: the id of os account before switch}
OHOS_ACCOUNT_STATE_CHANGE_EVENT:
__BASE: {type: BEHAVIOR, level: MINOR, desc: ohosaccount state change events}
USER_ID: {type: INT32, desc: local account id}
OPERATION_TYPE: {type: INT32, desc: operation type}
OLD_STATE: {type: INT32, desc: ohos account old state}
NEW_STATE: {type: INT32, desc: ohos account new state}

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "account_file_operator.h"
#include <cerrno>
#include <fstream>
#include <nlohmann/json.hpp>
#include <sstream>
@ -38,14 +39,15 @@ ErrCode AccountFileOperator::CreateDir(const std::string &path)
ACCOUNT_LOGI("enter");
if (!OHOS::ForceCreateDirectory(path)) {
ReportFileOperationFail(-1, "ForceCreateDirectory", path);
ReportFileOperationFail(errno, "ForceCreateDirectory", path);
ACCOUNT_LOGE("failed to create %{public}s, errno %{public}d.", path.c_str(), errno);
return ERR_OSACCOUNT_SERVICE_FILE_CREATE_DIR_ERROR;
}
mode_t mode = S_IRWXU;
bool createFlag = OHOS::ChangeModeDirectory(path, mode);
if (!createFlag) {
ACCOUNT_LOGE("failed to create dir, path = %{public}s", path.c_str());
ReportFileOperationFail(-1, "ChangeModeDirectory", path);
ACCOUNT_LOGE("failed to change mode for %{public}s, errno %{public}d.", path.c_str(), errno);
ReportFileOperationFail(errno, "ChangeModeDirectory", path);
return ERR_OSACCOUNT_SERVICE_FILE_CHANGE_DIR_MODE_ERROR;
}
@ -62,7 +64,8 @@ ErrCode AccountFileOperator::DeleteDirOrFile(const std::string &path)
delFlag = OHOS::ForceRemoveDirectory(path);
}
if (!delFlag) {
ReportFileOperationFail(-1, "DeleteDirOrFile", path);
ReportFileOperationFail(errno, "DeleteDirOrFile", path);
ACCOUNT_LOGE("DeleteDirOrFile failed, path %{public}s errno %{public}d.", path.c_str(), errno);
return ERR_OSACCOUNT_SERVICE_FILE_DELE_ERROR;
}
@ -77,14 +80,13 @@ ErrCode AccountFileOperator::InputFileByPathAndContent(const std::string &path,
ErrCode errCode = CreateDir(str);
if (errCode != ERR_OK) {
ACCOUNT_LOGE("failed to create dir, str = %{public}s errCode %{public}d.", str.c_str(), errCode);
ReportFileOperationFail(errCode, "CreateDir", str);
return ERR_OSACCOUNT_SERVICE_FILE_FIND_DIR_ERROR;
}
}
std::ofstream o(path);
if (!o.is_open()) {
ACCOUNT_LOGE("failed to open file, path = %{public}s", path.c_str());
ReportFileOperationFail(-1, "OpenFileToWrite", path);
ACCOUNT_LOGE("failed to open %{public}s, errno %{public}d.", path.c_str(), errno);
ReportFileOperationFail(errno, "OpenFileToWrite", path);
return ERR_OSACCOUNT_SERVICE_FILE_CREATE_FILE_FAILED_ERROR;
}
o << content;
@ -94,8 +96,8 @@ ErrCode AccountFileOperator::InputFileByPathAndContent(const std::string &path,
#endif // WITH_SELINUX
// change mode
if (!ChangeModeFile(path, S_IRUSR | S_IWUSR)) {
ACCOUNT_LOGW("failed to change mode for file %{public}s", path.c_str());
ReportFileOperationFail(-1, "ChangeModeFile", path);
ACCOUNT_LOGW("failed to change mode for file %{public}s, errno %{public}d.", path.c_str(), errno);
ReportFileOperationFail(errno, "ChangeModeFile", path);
}
ACCOUNT_LOGI("end");
@ -111,8 +113,8 @@ ErrCode AccountFileOperator::GetFileContentByPath(const std::string &path, std::
std::stringstream buffer;
std::ifstream i(path);
if (!i.is_open()) {
ACCOUNT_LOGE("cannot open file, path = %{public}s", path.c_str());
ReportFileOperationFail(-1, "OpenFileToRead", path);
ACCOUNT_LOGE("cannot open file %{public}s, errno %{public}d.", path.c_str(), errno);
ReportFileOperationFail(errno, "OpenFileToRead", path);
return ERR_OSACCOUNT_SERVICE_FILE_CREATE_FILE_FAILED_ERROR;
}
buffer << i.rdbuf();

View File

@ -14,6 +14,7 @@
*/
#include "account_mgr_service.h"
#include <cerrno>
#include "account_dump_helper.h"
#include "account_log_wrapper.h"
#include "app_account_manager_service.h"
@ -41,11 +42,13 @@ void CreateDeviceDir()
if (!OHOS::FileExists(DEVICE_OWNER_DIR)) {
ACCOUNT_LOGI("Device owner dir not exist, create!");
if (!OHOS::ForceCreateDirectory(DEVICE_OWNER_DIR)) {
ACCOUNT_LOGW("Create device owner dir failure!");
ACCOUNT_LOGW("Create device owner dir failure! errno %{public}d.", errno);
ReportFileOperationFail(errno, "ForceCreateDirectory", DEVICE_OWNER_DIR);
} else {
if (!OHOS::ChangeModeDirectory(DEVICE_OWNER_DIR, S_IRWXU)) {
ReportFileOperationFail(-1, "ChangeModeDirectory", DEVICE_OWNER_DIR);
ACCOUNT_LOGW("failed to create dir, path = %{public}s", DEVICE_OWNER_DIR.c_str());
ReportFileOperationFail(errno, "ChangeModeDirectory", DEVICE_OWNER_DIR);
ACCOUNT_LOGW("failed to create dir, path = %{public}s errno %{public}d.",
DEVICE_OWNER_DIR.c_str(), errno);
}
}
}
@ -162,7 +165,7 @@ bool AccountMgrService::Init()
if (!registerToService_) {
ret = Publish(&DelayedRefSingleton<AccountMgrService>::GetInstance());
if (!ret) {
ReportServiceStartFail(ERR_ACCOUNT_MGR_ADD_TO_SA_ERROR);
ReportServiceStartFail(ERR_ACCOUNT_MGR_ADD_TO_SA_ERROR, "Publish service failed!");
ACCOUNT_LOGE("AccountMgrService::Init Publish failed!");
return false;
}
@ -173,13 +176,15 @@ bool AccountMgrService::Init()
ret = ohosAccountMgr_->OnInitialize();
if (!ret) {
ACCOUNT_LOGE("Ohos account manager initialize failed");
ReportServiceStartFail(ERR_ACCOUNT_MGR_OHOS_MGR_INIT_ERROR);
ReportServiceStartFail(ERR_ACCOUNT_MGR_OHOS_MGR_INIT_ERROR, "OnInitialize failed!");
return ret;
}
IAccountContext::SetInstance(this);
auto appAccountManagerService = new (std::nothrow) AppAccountManagerService();
if (appAccountManagerService == nullptr) {
ReportServiceStartFail(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
"Insufficient memory to create app account manager service");
ACCOUNT_LOGE("memory alloc failed for appAccountManagerService!");
return false;
}
@ -187,6 +192,8 @@ bool AccountMgrService::Init()
if (osAccountManagerServiceOrg_ == nullptr) {
ACCOUNT_LOGE("memory alloc failed for osAccountManagerServiceOrg_!");
delete appAccountManagerService;
ReportServiceStartFail(ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR,
"Insufficient memory to create os account manager service");
return false;
}
dumpHelper_ = std::make_unique<AccountDumpHelper>(ohosAccountMgr_, osAccountManagerServiceOrg_);

View File

@ -14,6 +14,7 @@
*/
#include "ohos_account_data_deal.h"
#include <cerrno>
#include <fstream>
#include <iostream>
#include <vector>
@ -55,8 +56,8 @@ ErrCode OhosAccountDataDeal::Init(std::int32_t userId)
std::ifstream fin(configFile);
if (!fin) {
ACCOUNT_LOGE("Failed to open config file %{public}s", configFile.c_str());
ReportFileOperationFail(-1, "OhosAccountInitOpenFileToRead", configFile);
ACCOUNT_LOGE("Failed to open config file %{public}s, errno %{public}d.", configFile.c_str(), errno);
ReportFileOperationFail(errno, "OhosAccountInitOpenFileToRead", configFile);
return ERR_ACCOUNT_DATADEAL_INPUT_FILE_ERROR;
}
@ -67,8 +68,8 @@ ErrCode OhosAccountDataDeal::Init(std::int32_t userId)
if (!jsonData_.is_structured()) {
ACCOUNT_LOGE("Invalid json file, remove");
if (RemoveFile(configFile)) {
ACCOUNT_LOGE("Remove invalid json file failed");
ReportFileOperationFail(-1, "OhosAccountRemoveFile", configFile);
ACCOUNT_LOGE("Remove invalid json file %{public}s failed, errno %{public}d.", configFile.c_str(), errno);
ReportFileOperationFail(errno, "OhosAccountRemoveFile", configFile);
}
return ERR_ACCOUNT_DATADEAL_JSON_FILE_CORRUPTION;
}
@ -91,8 +92,8 @@ ErrCode OhosAccountDataDeal::AccountInfoFromJson(AccountInfo &accountInfo, const
}
std::ifstream fin(configFile);
if (!fin) {
ACCOUNT_LOGE("Failed to open config file %{public}s", configFile.c_str());
ReportFileOperationFail(-1, "OhosAccountOpenFileToRead", configFile);
ACCOUNT_LOGE("Failed to open config file %{public}s, errno %{public}d.", configFile.c_str(), errno);
ReportFileOperationFail(errno, "OhosAccountOpenFileToRead", configFile);
return ERR_ACCOUNT_DATADEAL_INPUT_FILE_ERROR;
}
@ -152,8 +153,8 @@ void OhosAccountDataDeal::SaveAccountInfo(const AccountInfo &accountInfo) const
std::string configFile = configFileDir_ + std::to_string(accountInfo.userId_) + ACCOUNT_CFG_FILE_NAME;
std::ofstream out(configFile);
if (!out) {
ACCOUNT_LOGE("Failed to open file %{public}s", configFile.c_str());
ReportFileOperationFail(-1, "OhosAccountOpenFileToWrite", configFile);
ACCOUNT_LOGE("Failed to open file %{public}s, errno %{public}d.", configFile.c_str(), errno);
ReportFileOperationFail(errno, "OhosAccountOpenFileToWrite", configFile);
return;
}
out << jsonData;
@ -161,8 +162,8 @@ void OhosAccountDataDeal::SaveAccountInfo(const AccountInfo &accountInfo) const
// change mode
if (!ChangeModeFile(configFile, S_IRUSR | S_IWUSR)) {
ACCOUNT_LOGW("failed to change mode for file %{public}s", configFile.c_str());
ReportFileOperationFail(-1, "ChangeModeFile", configFile);
ACCOUNT_LOGW("failed to change mode for file %{public}s, errno %{public}d.", configFile.c_str(), errno);
ReportFileOperationFail(errno, "ChangeModeFile", configFile);
}
}

View File

@ -77,7 +77,7 @@ ErrCode OsAccountInterface::SendToAMSAccountStop(OsAccountInfo &osAccountInfo)
osAccountStopUserCallback);
if (code != ERR_OK) {
ACCOUNT_LOGE("failed to AbilityManagerAdapter stop errcode is %{public}d", code);
ReportAccountOperationFail(osAccountInfo.GetLocalId(), code, "stop", "AbilityManagerAdapter StopUser failed!");
ReportAccountOperationFail(osAccountInfo.GetLocalId(), code, "stop", "AbilityManagerService StopUser failed!");
return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
}
struct tm startTime = {0};
@ -90,8 +90,9 @@ ErrCode OsAccountInterface::SendToAMSAccountStop(OsAccountInfo &osAccountInfo)
OHOS::GetSystemCurrentTime(&nowTime);
}
if (!osAccountStopUserCallback->isReturnOk_) {
ACCOUNT_LOGE("failed to AbilityManagerAdapter stop in call back");
ReportTimeoutFail("AbilityManagerService StopUser timeout!");
ACCOUNT_LOGE("failed to AbilityManagerService stop in call back");
ReportAccountOperationFail(osAccountInfo.GetLocalId(), -1, "stop",
"AbilityManagerService StopUser timeout!");
return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_AM_ACCOUNT_START_ERROR;
}
ACCOUNT_LOGI("send AM to stop is ok");
@ -141,7 +142,8 @@ ErrCode OsAccountInterface::SendToIDMAccountDelete(OsAccountInfo &osAccountInfo)
}
if (!callback->isIdmOnResultCallBack_) {
ACCOUNT_LOGE("idm did not call back! timeout!");
ReportTimeoutFail("UserIDMClient EnforceDelUser timeout!");
ReportAccountOperationFail(osAccountInfo.GetLocalId(), -1, "delete",
"UserIDMClient EnforceDelUser timeout!");
return ERR_OK; // do not return fail
}
ACCOUNT_LOGI("send to idm account delete and get callback succeed!");
@ -161,7 +163,7 @@ void OsAccountInterface::SendToCESAccountCreate(OsAccountInfo &osAccountInfo)
data.SetWant(want);
if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
ACCOUNT_LOGE("PublishCommonEvent for create account %{public}d failed!", osAccountID);
ReportOsAccountCESFail(osAccountID, "send common event for account create fail");
ReportAccountOperationFail(osAccountID, -1, "create", "PublishCommonEvent failed!");
} else {
ACCOUNT_LOGI("PublishCommonEvent for create account %{public}d succeed!", osAccountID);
}
@ -182,7 +184,7 @@ void OsAccountInterface::SendToCESAccountDelete(OsAccountInfo &osAccountInfo)
data.SetWant(want);
if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
ACCOUNT_LOGE("PublishCommonEvent for delete account %{public}d failed!", osAccountID);
ReportOsAccountCESFail(osAccountID, "send common event for account delete fail");
ReportAccountOperationFail(osAccountID, -1, "delete", "PublishCommonEvent failed!");
} else {
ACCOUNT_LOGI("PublishCommonEvent for delete account %{public}d succeed!", osAccountID);
}
@ -203,7 +205,7 @@ void OsAccountInterface::SendToCESAccountSwitched(OsAccountInfo &osAccountInfo)
data.SetWant(want);
if (!OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data)) {
ACCOUNT_LOGE("PublishCommonEvent for switched to account %{public}d failed!", osAccountID);
ReportOsAccountCESFail(osAccountID, "send common event for account switch fail");
ReportAccountOperationFail(osAccountID, -1, "switch", "PublishCommonEvent failed!");
} else {
ACCOUNT_LOGI("PublishCommonEvent for switched to account %{public}d succeed!", osAccountID);
}