From 35a3c244e15fa90d57cde7b0fbbdac594d242196 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Wed, 15 Jun 2022 03:57:28 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: I8240f0540176e211969d8e52c54d60df0d88b53c --- services/miscdevice_service/BUILD.gn | 1 + .../include/miscdevice_dump.h | 59 +++++ .../include/miscdevice_service.h | 2 + .../src/miscdevice_dump.cpp | 202 ++++++++++++++++++ .../src/miscdevice_service.cpp | 23 +- utils/include/sensors_errors.h | 3 +- 6 files changed, 288 insertions(+), 2 deletions(-) create mode 100755 services/miscdevice_service/include/miscdevice_dump.h create mode 100755 services/miscdevice_service/src/miscdevice_dump.cpp diff --git a/services/miscdevice_service/BUILD.gn b/services/miscdevice_service/BUILD.gn index 8520579..8b7c92a 100644 --- a/services/miscdevice_service/BUILD.gn +++ b/services/miscdevice_service/BUILD.gn @@ -19,6 +19,7 @@ ohos_shared_library("libmiscdevice_service") { "hdi_connection/adpter/src/compatible_connection.cpp", "hdi_connection/adpter/src/hdi_connection.cpp", "hdi_connection/interface/src/vibrator_hdi_connection.cpp", + "src/miscdevice_dump.cpp", "src/miscdevice_service.cpp", "src/miscdevice_service_stub.cpp", ] diff --git a/services/miscdevice_service/include/miscdevice_dump.h b/services/miscdevice_service/include/miscdevice_dump.h new file mode 100755 index 0000000..d3eaba2 --- /dev/null +++ b/services/miscdevice_service/include/miscdevice_dump.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MISCDEVICE_DUMP_H +#define MISCDEVICE_DUMP_H + +#include + +#include "accesstoken_kit.h" +#include "singleton.h" + +#include "nocopyable.h" + +namespace OHOS { +namespace Sensors { +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; +struct VibrateRecord { + std::string startTime; + uint32_t duration = 0; + int32_t uid = 0; + int32_t pid = 0; + std::string mode; + std::string effect; + std::string packageName; +}; +class MiscdeviceDump : public Singleton { +public: + MiscdeviceDump() = default; + ~MiscdeviceDump() = default; + void DumpHelp(int32_t fd); + void DumpMiscdeviceRecord(int32_t fd); + void ParseCommand(int32_t fd, const std::vector& args); + void SaveVibrator(AccessTokenID callerToken, int32_t uid, int32_t pid, uint32_t timeOut); + void SaveVibratorEffect(AccessTokenID callerToken, int32_t uid, int32_t pid, const std::string &effect); + +private: + DISALLOW_COPY_AND_MOVE(MiscdeviceDump); + std::queue> dumpQueue_; + std::mutex recordQueueMutex_; + void DumpCurrentTime(std::string &startTime); + void UpdateRecordQueue(std::shared_ptr record); + std::string GetPackageName(AccessTokenID tokenId); +}; +} // namespace Sensors +} // namespace OHOS +#endif // MISCDEVICE_DUMP_H diff --git a/services/miscdevice_service/include/miscdevice_service.h b/services/miscdevice_service/include/miscdevice_service.h index e2e7d02..51528a4 100644 --- a/services/miscdevice_service/include/miscdevice_service.h +++ b/services/miscdevice_service/include/miscdevice_service.h @@ -28,6 +28,7 @@ #include "thread_ex.h" #include "miscdevice_common.h" +#include "miscdevice_dump.h" #include "miscdevice_service_stub.h" #include "nocopyable.h" #include "vibrator_hdi_connection.h" @@ -114,6 +115,7 @@ private: static std::mutex conditionVarMutex_; static std::condition_variable conditionVar_; static std::unordered_map hapticRingMap_; + MiscdeviceDump &miscdeviceDump_ = MiscdeviceDump::GetInstance(); }; } // namespace Sensors } // namespace OHOS diff --git a/services/miscdevice_service/src/miscdevice_dump.cpp b/services/miscdevice_service/src/miscdevice_dump.cpp new file mode 100755 index 0000000..87ca5fc --- /dev/null +++ b/services/miscdevice_service/src/miscdevice_dump.cpp @@ -0,0 +1,202 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "miscdevice_dump.h" + +#include + +#include +#include +#include + +#include "securec.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace OHOS::HiviewDFX; +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "MiscdeviceDump" }; +constexpr uint32_t MAX_DUMP_RECORD_SIZE = 30; +constexpr uint32_t BASE_YEAR = 1900; +constexpr uint32_t BASE_MON = 1; +constexpr int32_t INVALID_PID = -1; +constexpr int32_t INVALID_UID = -1; +} // namespace + +void MiscdeviceDump::ParseCommand(int32_t fd, const std::vector& args) +{ + int32_t optionIndex = 0; + struct option dumpOptions[] = { + {"record", no_argument, 0, 'r'}, + {"help", no_argument, 0, 'h'}, + {NULL, 0, 0, 0} + }; + char **argv = new char *[args.size()]; + for (size_t i = 0; i < args.size(); ++i) { + argv[i] = new char[args[i].size() + 1]; + if (strcpy_s(argv[i], args[i].size() + 1, args[i].c_str()) != EOK) { + MISC_HILOGE("strcpy_s error"); + goto RELEASE_RES; + return; + } + } + optind = 1; + int32_t c; + while ((c = getopt_long(args.size(), argv, "rh", dumpOptions, &optionIndex)) != -1) { + switch (c) { + case 'r': { + DumpMiscdeviceRecord(fd); + break; + } + case 'h': { + DumpHelp(fd); + break; + } + default: { + dprintf(fd, "cmd param is error\n"); + DumpHelp(fd); + break; + } + } + } + RELEASE_RES: + for (size_t i = 0; i < args.size(); ++i) { + delete[] argv[i]; + } + delete[] argv; +} + +void MiscdeviceDump::DumpHelp(int32_t fd) +{ + dprintf(fd, "Usage:\n"); + dprintf(fd, " -h: dump help\n"); + dprintf(fd, " -r: dump the list of vibrate recorded\n"); +} + +void MiscdeviceDump::DumpMiscdeviceRecord(int32_t fd) +{ + std::lock_guard queueLock(recordQueueMutex_); + if (dumpQueue_.empty()) { + MISC_HILOGW("dumpQueue_ is empty"); + return; + } + size_t length = dumpQueue_.size() > MAX_DUMP_RECORD_SIZE ? MAX_DUMP_RECORD_SIZE : dumpQueue_.size(); + for (size_t i = 0; i < length; ++i) { + auto record = dumpQueue_.front(); + CHKPV(record); + dumpQueue_.push(record); + dumpQueue_.pop(); + if (record->mode == "duration") { + dprintf(fd, "startTime:%s | uid:%d | pid:%d | mode:%s | duration:%8u | packageName:%s\n", + record->startTime.c_str(), record->uid, record->pid, record->mode.c_str(), record->duration, + record->packageName.c_str()); + } else { + dprintf(fd, "startTime:%s | uid:%d | pid:%d | mode:%s | effect:%s | packageName:%s\n", + record->startTime.c_str(), record->uid, record->pid, record->mode.c_str(), record->effect.c_str(), + record->packageName.c_str()); + } + } +} + +void MiscdeviceDump::DumpCurrentTime(std::string &startTime) +{ + timespec curTime; + clock_gettime(CLOCK_REALTIME, &curTime); + struct tm *timeinfo = localtime(&(curTime.tv_sec)); + CHKPV(timeinfo); + startTime.append(std::to_string(timeinfo->tm_year + BASE_YEAR)).append("-") + .append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday)) + .append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min)) + .append(":").append(std::to_string(timeinfo->tm_sec)).append(".") + .append(std::to_string(curTime.tv_nsec / 1000000)); +} + +std::string MiscdeviceDump::GetPackageName(AccessTokenID tokenId) +{ + std::string packageName; + int32_t tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId); + switch (tokenType) { + case ATokenTypeEnum::TOKEN_NATIVE: { + NativeTokenInfo tokenInfo; + if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) != 0) { + MISC_HILOGE("get native token info fail"); + return {}; + } + packageName = tokenInfo.processName; + break; + } + case ATokenTypeEnum::TOKEN_HAP: { + HapTokenInfo hapInfo; + if (AccessTokenKit::GetHapTokenInfo(tokenId, hapInfo) != 0) { + MISC_HILOGE("get hap token info fail"); + return {}; + } + packageName = hapInfo.bundleName; + break; + } + default: { + MISC_HILOGW("token type not match"); + break; + } + } + return packageName; +} + +void MiscdeviceDump::UpdateRecordQueue(std::shared_ptr record) +{ + std::lock_guard queueLock(recordQueueMutex_); + dumpQueue_.push(record); + if (dumpQueue_.size() > MAX_DUMP_RECORD_SIZE) { + dumpQueue_.pop(); + } +} + +void MiscdeviceDump::SaveVibrator(AccessTokenID callerToken, int32_t uid, int32_t pid, uint32_t timeOut) +{ + if ((uid == INVALID_UID) || (pid <= INVALID_PID)) { + MISC_HILOGE("uid or pid is invalid"); + return; + } + auto record = std::make_shared(); + CHKPV(record); + record->uid = uid; + record->pid = pid; + record->packageName = GetPackageName(callerToken); + record->duration = timeOut; + record->mode = "duration"; + DumpCurrentTime(record->startTime); + UpdateRecordQueue(record); +} + +void MiscdeviceDump::SaveVibratorEffect(AccessTokenID callerToken, int32_t uid, int32_t pid, + const std::string &effect) +{ + if ((uid == INVALID_UID) || (pid <= INVALID_PID)) { + MISC_HILOGE("uid or pid is invalid"); + return; + } + auto record = std::make_shared(); + CHKPV(record); + record->uid = uid; + record->pid = pid; + record->packageName = GetPackageName(callerToken); + record->effect = effect; + record->mode = "preset"; + DumpCurrentTime(record->startTime); + UpdateRecordQueue(record); +} +} // namespace Sensors +} // namespace OHOS diff --git a/services/miscdevice_service/src/miscdevice_service.cpp b/services/miscdevice_service/src/miscdevice_service.cpp index 48c116b..8fd5fb6 100644 --- a/services/miscdevice_service/src/miscdevice_service.cpp +++ b/services/miscdevice_service/src/miscdevice_service.cpp @@ -15,6 +15,8 @@ #include "miscdevice_service.h" +#include + #include "sensors_errors.h" #include "system_ability_definition.h" @@ -175,6 +177,7 @@ int32_t MiscdeviceService::Vibrate(int32_t vibratorId, uint32_t timeOut) return ERR_INVALID_VALUE; } std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + miscdeviceDump_.SaveVibrator(GetCallingTokenID(), GetCallingUid(), GetCallingPid(), timeOut); auto it = vibratorEffectMap_.find(vibratorId); if (it != vibratorEffectMap_.end()) { if (it->second == "time") { @@ -211,6 +214,7 @@ int32_t MiscdeviceService::CancelVibrator(int32_t vibratorId) int32_t MiscdeviceService::PlayVibratorEffect(int32_t vibratorId, const std::string &effect, bool isLooping) { std::lock_guard vibratorEffectLock(vibratorEffectMutex_); + miscdeviceDump_.SaveVibratorEffect(GetCallingTokenID(), GetCallingUid(), GetCallingPid(), effect); auto it = vibratorEffectMap_.find(vibratorId); if (it != vibratorEffectMap_.end()) { if (it->second == "time") { @@ -400,7 +404,24 @@ void MiscdeviceService::VibratorEffectThread::UpdateVibratorEffectData(const std int32_t MiscdeviceService::Dump(int32_t fd, const std::vector &args) { - return 0; + CALL_LOG_ENTER; + if (fd < 0) { + MISC_HILOGE("fd is invalid"); + return DUMP_PARAM_ERR; + } + if (args.empty()) { + MISC_HILOGE("args cannot be empty"); + dprintf(fd, "args cannot be empty\n"); + miscdeviceDump_.DumpHelp(fd); + return DUMP_PARAM_ERR; + } + std::vector argList = { "" }; + std::transform(args.begin(), args.end(), std::back_inserter(argList), + [](const std::u16string &arg) { + return Str16ToStr8(arg); + }); + miscdeviceDump_.ParseCommand(fd, argList); + return ERR_OK; } } // namespace Sensors } // namespace OHOS diff --git a/utils/include/sensors_errors.h b/utils/include/sensors_errors.h index 943391d..1493362 100644 --- a/utils/include/sensors_errors.h +++ b/utils/include/sensors_errors.h @@ -68,7 +68,8 @@ enum { VIBRATOR_PLAY_EFFECT_ERR = VIBRATOR_OFF_ERR + 1, VIBRATOR_STOP_EFFECT_ERR = VIBRATOR_PLAY_EFFECT_ERR + 1, VIBRATOR_SET_PARA_ERR = VIBRATOR_STOP_EFFECT_ERR + 1, - WRITE_MSG_ERR = VIBRATOR_SET_PARA_ERR + 1, + DUMP_PARAM_ERR = VIBRATOR_SET_PARA_ERR + 1, + WRITE_MSG_ERR = DUMP_PARAM_ERR + 1, }; // Error code for Sensor native -- Gitee From 98f7fdb3c1a6b9f201ebb16f5c6cee9669b2bc16 Mon Sep 17 00:00:00 2001 From: wuzhihuitmac Date: Wed, 15 Jun 2022 06:30:38 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wuzhihuitmac Change-Id: Ib57c60ebe73dc4b8f63d275fba95687ff59c9664 --- services/miscdevice_service/src/miscdevice_dump.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/miscdevice_service/src/miscdevice_dump.cpp b/services/miscdevice_service/src/miscdevice_dump.cpp index 87ca5fc..5ca134f 100755 --- a/services/miscdevice_service/src/miscdevice_dump.cpp +++ b/services/miscdevice_service/src/miscdevice_dump.cpp @@ -121,7 +121,7 @@ void MiscdeviceDump::DumpCurrentTime(std::string &startTime) .append(std::to_string(timeinfo->tm_mon + BASE_MON)).append("-").append(std::to_string(timeinfo->tm_mday)) .append(" ").append(std::to_string(timeinfo->tm_hour)).append(":").append(std::to_string(timeinfo->tm_min)) .append(":").append(std::to_string(timeinfo->tm_sec)).append(".") - .append(std::to_string(curTime.tv_nsec / 1000000)); + .append(std::to_string(curTime.tv_nsec / (1000 * 1000))); } std::string MiscdeviceDump::GetPackageName(AccessTokenID tokenId) -- Gitee