!2804 fix patam watcher

Merge pull request !2804 from 钟柠/param-watcher
This commit is contained in:
openharmony_ci 2024-05-08 07:35:50 +00:00 committed by Gitee
commit f5380c6419
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 27 additions and 10 deletions

View File

@ -39,10 +39,12 @@ WatcherManager::~WatcherManager()
uint32_t WatcherManager::AddRemoteWatcher(uint32_t id, const sptr<IWatcher> &watcher)
{
#ifndef STARTUP_INIT_TEST
if (id == static_cast<uint32_t>(getpid())) {
WATCHER_LOGE("Failed to add remote watcher %u", id);
return 0;
}
#endif
WATCHER_CHECK(watcher != nullptr, return 0, "Invalid remote watcher");
WATCHER_CHECK(deathRecipient_ != nullptr, return 0, "Invalid deathRecipient_");
sptr<IRemoteObject> object = watcher->AsObject();
@ -58,7 +60,7 @@ uint32_t WatcherManager::AddRemoteWatcher(uint32_t id, const sptr<IWatcher> &wat
// create remote watcher
RemoteWatcher *remoteWatcher = new RemoteWatcher(remoteWatcherId, watcher);
WATCHER_CHECK(remoteWatcher != nullptr, return 0, "Failed to create watcher for %u", id);
remoteWatcher->SetAgentId(id);
remoteWatcher->SetAgentId(GetCallingPid());
AddRemoteWatcher(remoteWatcher);
}
WATCHER_LOGI("Add remote watcher remoteWatcherId %u %u success", remoteWatcherId, id);
@ -72,6 +74,8 @@ int32_t WatcherManager::DelRemoteWatcher(uint32_t remoteWatcherId)
std::lock_guard<std::mutex> lock(watcherMutex_);
RemoteWatcher *remoteWatcher = GetRemoteWatcher(remoteWatcherId);
WATCHER_CHECK(remoteWatcher != nullptr, return 0, "Can not find watcher %u", remoteWatcherId);
WATCHER_CHECK(remoteWatcher->CheckAgent(GetCallingPid()), return 0,
"Can not find watcher %u calling %u", remoteWatcher->GetAgentId(), static_cast<uint32_t>(GetCallingPid()));
WATCHER_LOGI("Del remote watcher remoteWatcherId %u", remoteWatcherId);
watcher = remoteWatcher->GetWatcher();
DelRemoteWatcher(remoteWatcher);
@ -89,6 +93,8 @@ int32_t WatcherManager::AddWatcher(const std::string &keyPrefix, uint32_t remote
// get remote watcher and group
auto remoteWatcher = GetRemoteWatcher(remoteWatcherId);
WATCHER_CHECK(remoteWatcher != nullptr, return -1, "Can not find remote watcher %d", remoteWatcherId);
WATCHER_CHECK(remoteWatcher->CheckAgent(GetCallingPid()), return 0,
"Can not find watcher %u calling %u", remoteWatcher->GetAgentId(), static_cast<uint32_t>(GetCallingPid()));
auto group = AddWatcherGroup(keyPrefix);
WATCHER_CHECK(group != nullptr, return -1, "Failed to create group for %s", keyPrefix.c_str());
{
@ -113,6 +119,8 @@ int32_t WatcherManager::DelWatcher(const std::string &keyPrefix, uint32_t remote
WATCHER_CHECK(group != nullptr, return 0, "Can not find group %s", keyPrefix.c_str());
auto remoteWatcher = GetRemoteWatcher(remoteWatcherId);
WATCHER_CHECK(remoteWatcher != nullptr, return 0, "Can not find watcher %s %d", keyPrefix.c_str(), remoteWatcherId);
WATCHER_CHECK(remoteWatcher->CheckAgent(GetCallingPid()), return -1,
"Can not find watcher %u calling %u", remoteWatcher->GetAgentId(), static_cast<uint32_t>(GetCallingPid()));
WATCHER_LOGI("Delete watcher prefix %s remoteWatcherId %u", keyPrefix.c_str(), remoteWatcherId);
{
// remove watcher from agent and group
@ -129,6 +137,11 @@ int32_t WatcherManager::RefreshWatcher(const std::string &keyPrefix, uint32_t re
{
std::lock_guard<std::mutex> lock(watcherMutex_);
WATCHER_LOGV("Refresh watcher %s remoteWatcherId: %u", keyPrefix.c_str(), remoteWatcherId);
auto remoteWatcher = GetRemoteWatcher(remoteWatcherId);
WATCHER_CHECK(remoteWatcher != nullptr, return 0, "Can not find watcher %s %d", keyPrefix.c_str(), remoteWatcherId);
WATCHER_CHECK(remoteWatcher->CheckAgent(GetCallingPid()), return 0,
"Can not find watcher %u calling %d", remoteWatcher->GetAgentId(), static_cast<uint32_t>(GetCallingPid()));
auto group = GetWatcherGroup(keyPrefix);
WATCHER_CHECK(group != nullptr, return 0, "Can not find group %s", keyPrefix.c_str());
SendLocalChange(keyPrefix, remoteWatcherId);

View File

@ -225,6 +225,10 @@ public:
{
return id_;
}
bool CheckAgent(pid_t calling) const
{
return id_ == static_cast<uint32_t>(calling);
}
void SetAgentId(uint32_t id)
{
id_ = id;

View File

@ -273,7 +273,7 @@ HWTEST_F(WatcherProxyUnitTest, TestAddWatcher, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1000, watcherId); // 1000 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.test1", watcherId);
test.TestRefreshWatcher("test.permission.watcher.test1", watcherId);
test.TestProcessWatcherMessage("test.permission.watcher.test1", watcherId);
@ -284,7 +284,7 @@ HWTEST_F(WatcherProxyUnitTest, TestAddWatcher2, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1001, watcherId); // 1001 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.test2", watcherId);
test.TestAddWatcher("test.permission.watcher.test2", watcherId);
test.TestAddWatcher("test.permission.watcher.test2", watcherId);
@ -296,7 +296,7 @@ HWTEST_F(WatcherProxyUnitTest, TestAddWatcher3, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1003, watcherId); // 1003 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.test3", watcherId);
test.TestWatchAgentDump("test.permission.watcher.test3");
}
@ -305,7 +305,7 @@ HWTEST_F(WatcherProxyUnitTest, TestAddWatcher4, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1004, watcherId); // 1004 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
SystemWriteParam("test.watcher.test4", "1101");
SystemWriteParam("test.watcher.test4.test", "1102");
test.TestAddWatcher("test.watcher.test4*", watcherId);
@ -316,7 +316,7 @@ HWTEST_F(WatcherProxyUnitTest, TestAddWatcher5, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1005, watcherId); // 1005 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.test5", watcherId);
SystemWriteParam("test.permission.watcher.test5", "1101");
test.TestWatchAgentDump("test.permission.watcher.test5");
@ -326,7 +326,7 @@ HWTEST_F(WatcherProxyUnitTest, TestDelWatcher, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1006, watcherId); // 1005 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.testDel", watcherId);
test.TestDelWatcher("test.permission.watcher.testDel", watcherId);
test.TestDelRemoteWatcher(watcherId);
@ -337,7 +337,7 @@ HWTEST_F(WatcherProxyUnitTest, TestDiedWatcher, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1006, watcherId); // 1005 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.testdied", watcherId);
test.TestDelWatcher("test.permission.watcher.testdied", watcherId);
test.TestWatchAgentDied(watcherId);
@ -348,7 +348,7 @@ HWTEST_F(WatcherProxyUnitTest, TestSendLocalChange, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(2006, watcherId); // 2006 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.watcher*", watcherId);
test.TestAddWatcher("test.watcher.", watcherId);
test.TestWatchAgentDump("test.watcher.");
@ -370,7 +370,7 @@ HWTEST_F(WatcherProxyUnitTest, TestStop, TestSize.Level0)
{
WatcherProxyUnitTest test;
uint32_t watcherId = 0;
test.TestAddRemoteWatcher(1007, watcherId); // 1005 test agent
test.TestAddRemoteWatcher(getpid(), watcherId);
test.TestAddWatcher("test.permission.watcher.stop", watcherId);
test.TestStop();
}