1 /*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common_event_manager_service.h"
17
18 #include "ability_manager_helper.h"
19 #include "access_token_helper.h"
20 #include "accesstoken_kit.h"
21 #include "bundle_manager_helper.h"
22 #include "common_event_constant.h"
23 #include "datetime_ex.h"
24 #include "event_log_wrapper.h"
25 #include "event_trace_wrapper.h"
26 #include "hitrace_meter_adapter.h"
27 #include "ipc_skeleton.h"
28 #include "parameters.h"
29 #include "publish_manager.h"
30 #include "refbase.h"
31 #include "system_ability_definition.h"
32 #include "os_account_manager_helper.h"
33 #include "xcollie/watchdog.h"
34 #include "ces_inner_error_code.h"
35 #include "system_time.h"
36 #include <mutex>
37 #include <new>
38
39 namespace OHOS {
40 namespace EventFwk {
41 namespace {
42 const std::string NOTIFICATION_CES_CHECK_SA_PERMISSION = "notification.ces.check.sa.permission";
43 } // namespace
44
45 using namespace OHOS::Notification;
46
47 sptr<CommonEventManagerService> CommonEventManagerService::instance_;
48 ffrt::mutex CommonEventManagerService::instanceMutex_;
49
GetInstance()50 sptr<CommonEventManagerService> CommonEventManagerService::GetInstance()
51 {
52 if (instance_ == nullptr) {
53 std::lock_guard<ffrt::mutex> lock(instanceMutex_);
54 if (instance_ == nullptr) {
55 instance_ = new (std::nothrow) CommonEventManagerService();
56 }
57 }
58 return instance_;
59 }
60
CommonEventManagerService()61 CommonEventManagerService::CommonEventManagerService()
62 : serviceRunningState_(ServiceRunningState::STATE_NOT_START),
63 runner_(nullptr),
64 handler_(nullptr)
65 {
66 supportCheckSaPermission_ = OHOS::system::GetParameter(NOTIFICATION_CES_CHECK_SA_PERMISSION, "false");
67 EVENT_LOGD("instance created");
68 }
69
~CommonEventManagerService()70 CommonEventManagerService::~CommonEventManagerService()
71 {
72 EVENT_LOGD("instance destroyed");
73 }
74
Init()75 ErrCode __attribute__((weak)) CommonEventManagerService::Init()
76 {
77 EVENT_LOGD("ready to init");
78 innerCommonEventManager_ = std::make_shared<InnerCommonEventManager>();
79 if (!innerCommonEventManager_) {
80 EVENT_LOGE("Failed to init without inner service");
81 return ERR_INVALID_OPERATION;
82 }
83
84 commonEventSrvQueue_ = std::make_shared<ffrt::queue>("CesSrvMain");
85 serviceRunningState_ = ServiceRunningState::STATE_RUNNING;
86
87 return ERR_OK;
88 }
89
IsReady() const90 bool CommonEventManagerService::IsReady() const
91 {
92 if (!innerCommonEventManager_) {
93 EVENT_LOGE("innerCommonEventManager is null");
94 return false;
95 }
96
97 if (!commonEventSrvQueue_) {
98 EVENT_LOGE("queue object is null");
99 return false;
100 }
101
102 return true;
103 }
104
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,int32_t userId,int32_t & funcResult)105 ErrCode CommonEventManagerService::PublishCommonEvent(
106 const CommonEventData& event, const CommonEventPublishInfo& publishinfo, int32_t userId, int32_t& funcResult)
107 {
108 EVENT_LOGD("enter");
109 return PublishCommonEvent(event, publishinfo, nullptr, userId, funcResult);
110 }
111
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,int32_t userId,int32_t & funcResult)112 ErrCode CommonEventManagerService::PublishCommonEvent(const CommonEventData& event,
113 const CommonEventPublishInfo& publishinfo, const sptr<IRemoteObject>& commonEventListener, int32_t userId,
114 int32_t& funcResult)
115 {
116 EVENT_LOGD("enter");
117
118 if (!IsReady()) {
119 EVENT_LOGE("CommonEventManagerService not ready");
120 funcResult = ERR_NOTIFICATION_CESM_ERROR;
121 return ERR_OK;
122 }
123
124 if (userId != ALL_USER && userId != CURRENT_USER && userId != UNDEFINED_USER) {
125 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
126 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
127 EVENT_LOGE("publish to special user must be system application.");
128 funcResult = ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
129 return ERR_OK;
130 }
131 }
132
133 funcResult = PublishCommonEventDetailed(event, publishinfo, commonEventListener, IPCSkeleton::GetCallingPid(),
134 IPCSkeleton::GetCallingUid(), IPCSkeleton::GetCallingTokenID(), userId);
135 return ERR_OK;
136 }
137
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,uint32_t uid,int32_t callerToken,int32_t userId,bool & funcResult)138 ErrCode CommonEventManagerService::PublishCommonEvent(const CommonEventData& event,
139 const CommonEventPublishInfo& publishinfo, uint32_t uid, int32_t callerToken, int32_t userId, bool& funcResult)
140 {
141 EVENT_LOGD("enter");
142 return PublishCommonEvent(event, publishinfo, nullptr, uid, callerToken, userId, funcResult);
143 }
144
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,uint32_t uid,int32_t callerToken,int32_t userId,bool & funcResult)145 ErrCode CommonEventManagerService::PublishCommonEvent(const CommonEventData& event,
146 const CommonEventPublishInfo& publishinfo, const sptr<IRemoteObject>& commonEventListener, uint32_t uid,
147 int32_t callerToken, int32_t userId, bool& funcResult)
148 {
149 EVENT_LOGD("enter");
150
151 if (!IsReady()) {
152 EVENT_LOGE("PublishCommonEvent not ready");
153 funcResult = false;
154 return ERR_OK;
155 }
156
157 auto callingUid = IPCSkeleton::GetCallingUid();
158 if (callingUid != FOUNDATION_UID) {
159 EVENT_LOGE("Only foundation can publish common event as proxy uid = %{public}d.", callingUid);
160 funcResult = false;
161 return ERR_OK;
162 }
163 funcResult = PublishCommonEventDetailed(event, publishinfo, commonEventListener, UNDEFINED_PID, uid, callerToken,
164 userId) == ERR_OK ? true : false;
165 return ERR_OK;
166 }
167
PublishCommonEventDetailed(const CommonEventData & event,const CommonEventPublishInfo & publishinfo,const sptr<IRemoteObject> & commonEventListener,const pid_t & pid,const uid_t & uid,const int32_t & clientToken,const int32_t & userId)168 int32_t CommonEventManagerService::PublishCommonEventDetailed(const CommonEventData &event,
169 const CommonEventPublishInfo &publishinfo, const sptr<IRemoteObject> &commonEventListener, const pid_t &pid,
170 const uid_t &uid, const int32_t &clientToken, const int32_t &userId)
171 {
172 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
173 if (AccessTokenHelper::IsDlpHap(clientToken)) {
174 EVENT_LOGE("DLP hap not allowed to send common event");
175 return ERR_NOTIFICATION_CES_NOT_SA_SYSTEM_APP;
176 }
177 struct tm recordTime = {0};
178 if (!GetSystemCurrentTime(&recordTime)) {
179 EVENT_LOGE("Failed to GetSystemCurrentTime");
180 return ERR_NOTIFICATION_SYS_ERROR;
181 }
182 int32_t errCode = CheckUserIdParams(userId);
183 if (errCode != ERR_OK) {
184 return errCode;
185 }
186
187 if (DelayedSingleton<PublishManager>::GetInstance()->CheckIsFloodAttack(uid)) {
188 EVENT_LOGE("Too many common events have been sent in a short period from (pid = %{public}d, uid = "
189 "%{public}d, userId = %{public}d)", pid, uid, userId);
190 return ERR_NOTIFICATION_CES_EVENT_FREQ_TOO_HIGH;
191 }
192
193 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
194 wptr<CommonEventManagerService> weakThis = this;
195 std::function<void()> publishCommonEventFunc = [wp,
196 event,
197 publishinfo,
198 commonEventListener,
199 recordTime,
200 pid,
201 uid,
202 clientToken,
203 userId,
204 weakThis] () {
205 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
206 if (innerCommonEventManager == nullptr) {
207 EVENT_LOGE("innerCommonEventManager not exist");
208 return;
209 }
210 std::string bundleName = "";
211 if (!AccessTokenHelper::VerifyNativeToken(clientToken)) {
212 bundleName = DelayedSingleton<BundleManagerHelper>::GetInstance()->GetBundleName(uid);
213 }
214 sptr<CommonEventManagerService> commonEventManagerService = weakThis.promote();
215 if (commonEventManagerService == nullptr) {
216 EVENT_LOGE("CommonEventManager not exist");
217 return;
218 }
219 bool ret = innerCommonEventManager->PublishCommonEvent(event,
220 publishinfo,
221 commonEventListener,
222 recordTime,
223 pid,
224 uid,
225 clientToken,
226 userId,
227 bundleName,
228 commonEventManagerService);
229 if (!ret) {
230 EVENT_LOGE("failed to publish event %{public}s", event.GetWant().GetAction().c_str());
231 }
232 };
233 EVENT_LOGD("Start to submit publish commonEvent <%{public}d>", uid);
234 commonEventSrvQueue_->submit(publishCommonEventFunc);
235 return ERR_OK;
236 }
237
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener,int32_t instanceKey,int32_t & funcResult)238 ErrCode CommonEventManagerService::SubscribeCommonEvent(const CommonEventSubscribeInfo& subscribeInfo,
239 const sptr<IRemoteObject>& commonEventListener, int32_t instanceKey, int32_t& funcResult)
240 {
241 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
242 EVENT_LOGD("enter");
243
244 if (!IsReady()) {
245 EVENT_LOGE("CommonEventManagerService not ready");
246 funcResult = ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
247 return ERR_OK;
248 }
249
250 struct tm recordTime = {0};
251 if (!GetSystemCurrentTime(&recordTime)) {
252 EVENT_LOGE("Failed to GetSystemCurrentTime");
253 funcResult = ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
254 return ERR_OK;
255 }
256
257 int32_t errCode = CheckUserIdParams(subscribeInfo.GetUserId());
258 if (errCode != ERR_OK) {
259 funcResult = errCode;
260 return ERR_OK;
261 }
262
263 auto callingUid = IPCSkeleton::GetCallingUid();
264 auto callingPid = IPCSkeleton::GetCallingPid();
265 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
266 std::string bundleName = "";
267 if (!AccessTokenHelper::VerifyNativeToken(callerToken)) {
268 bundleName = DelayedSingleton<BundleManagerHelper>::GetInstance()->GetBundleName(callingUid);
269 }
270 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
271 int64_t startTime = SystemTime::GetNowSysTime();
272 std::function<void()> subscribeCommonEventFunc = [wp,
273 subscribeInfo,
274 commonEventListener,
275 recordTime,
276 callingPid,
277 callingUid,
278 callerToken,
279 bundleName,
280 instanceKey,
281 startTime] () {
282 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
283 if (innerCommonEventManager == nullptr) {
284 EVENT_LOGE("innerCommonEventManager not exist");
285 return;
286 }
287 bool ret = innerCommonEventManager->SubscribeCommonEvent(subscribeInfo,
288 commonEventListener,
289 recordTime,
290 callingPid,
291 callingUid,
292 callerToken,
293 bundleName,
294 instanceKey,
295 startTime);
296 if (!ret) {
297 EVENT_LOGE("failed to subscribe event");
298 }
299 };
300
301 EVENT_LOGD("Start to submit subscribe commonEvent <%{public}d>", callingUid);
302 commonEventSrvQueue_->submit(subscribeCommonEventFunc);
303 funcResult = ERR_OK;
304 return ERR_OK;
305 }
306
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener,int32_t & funcResult)307 ErrCode CommonEventManagerService::UnsubscribeCommonEvent(const sptr<IRemoteObject>& commonEventListener,
308 int32_t& funcResult)
309 {
310 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
311 EVENT_LOGD("enter");
312
313 if (!IsReady()) {
314 EVENT_LOGE("CommonEventManagerService not ready");
315 funcResult = ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
316 return ERR_OK;
317 }
318
319 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
320 std::function<void()> unsubscribeCommonEventFunc = [wp, commonEventListener] () {
321 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
322 if (innerCommonEventManager == nullptr) {
323 EVENT_LOGE("innerCommonEventManager not exist");
324 return;
325 }
326 bool ret = innerCommonEventManager->UnsubscribeCommonEvent(commonEventListener);
327 if (!ret) {
328 EVENT_LOGE("failed to unsubscribe event");
329 }
330 };
331
332 commonEventSrvQueue_->submit(unsubscribeCommonEventFunc);
333 funcResult = ERR_OK;
334 return ERR_OK;
335 }
336
UnsubscribeCommonEventSync(const sptr<IRemoteObject> & commonEventListener,int32_t & funcResult)337 ErrCode CommonEventManagerService::UnsubscribeCommonEventSync(const sptr<IRemoteObject>& commonEventListener,
338 int32_t& funcResult)
339 {
340 NOTIFICATION_HITRACE(HITRACE_TAG_NOTIFICATION);
341 EVENT_LOGD("enter");
342
343 if (!IsReady()) {
344 EVENT_LOGE("CommonEventManagerService not ready");
345 funcResult = ERR_NOTIFICATION_CES_COMMON_PARAM_INVALID;
346 return ERR_OK;
347 }
348
349 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
350 std::function<void()> unsubscribeCommonEventFunc = [wp, commonEventListener] () {
351 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
352 if (innerCommonEventManager == nullptr) {
353 EVENT_LOGE("innerCommonEventManager not exist");
354 return;
355 }
356 bool ret = innerCommonEventManager->UnsubscribeCommonEvent(commonEventListener);
357 if (!ret) {
358 EVENT_LOGE("failed to unsubscribe event");
359 }
360 };
361
362 ffrt::task_handle handler = commonEventSrvQueue_->submit_h(unsubscribeCommonEventFunc);
363 commonEventSrvQueue_->wait(handler);
364 funcResult = ERR_OK;
365 return ERR_OK;
366 }
367
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData,bool & funcResult)368 ErrCode CommonEventManagerService::GetStickyCommonEvent(const std::string& event, CommonEventData& eventData,
369 bool& funcResult)
370 {
371 EVENT_LOGD("enter");
372
373 if (!IsReady()) {
374 EVENT_LOGE("CommonEventManagerService not ready");
375 funcResult = false;
376 return ERR_OK;
377 }
378
379 if (event.empty()) {
380 EVENT_LOGE("event is empty");
381 funcResult = false;
382 return ERR_OK;
383 }
384 auto callerToken = IPCSkeleton::GetCallingTokenID();
385 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(callerToken);
386 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
387 EVENT_LOGE("Not system application or subsystem request.");
388 funcResult = ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
389 return ERR_OK;
390 }
391 auto callingUid = IPCSkeleton::GetCallingUid();
392 std::string bundleName = DelayedSingleton<BundleManagerHelper>::GetInstance()->GetBundleName(callingUid);
393 const std::string permission = "ohos.permission.COMMONEVENT_STICKY";
394 bool ret = AccessTokenHelper::VerifyAccessToken(callerToken, permission);
395 if (!ret) {
396 EVENT_LOGE("No permission to get a sticky common event from %{public}s (uid = %{public}d)",
397 bundleName.c_str(),
398 callingUid);
399 funcResult = false;
400 return ERR_OK;
401 }
402 funcResult = innerCommonEventManager_->GetStickyCommonEvent(event, eventData);
403 return ERR_OK;
404 }
405
DumpState(uint8_t dumpType,const std::string & event,int32_t userId,std::vector<std::string> & state,bool & funcResult)406 ErrCode CommonEventManagerService::DumpState(uint8_t dumpType, const std::string& event, int32_t userId,
407 std::vector<std::string>& state, bool& funcResult)
408 {
409 EVENT_LOGD("enter");
410
411 auto callerToken = IPCSkeleton::GetCallingTokenID();
412 if (!AccessTokenHelper::VerifyShellToken(callerToken) && !AccessTokenHelper::VerifyNativeToken(callerToken)) {
413 EVENT_LOGE("Not subsystem or shell request");
414 funcResult = false;
415 return ERR_INVALID_VALUE;
416 }
417 if (!IsReady()) {
418 EVENT_LOGE("CommonEventManagerService not ready");
419 funcResult = false;
420 return ERR_INVALID_VALUE;
421 }
422
423 innerCommonEventManager_->DumpState(dumpType, event, userId, state);
424 if (state.size() > MAX_HISTORY_SIZE) {
425 state.erase(state.begin() + MAX_HISTORY_SIZE, state.end());
426 }
427 funcResult = true;
428 return ERR_OK;
429 }
430
FinishReceiver(const sptr<IRemoteObject> & proxy,int32_t code,const std::string & receiverData,bool abortEvent,bool & funcResult)431 ErrCode CommonEventManagerService::FinishReceiver(const sptr<IRemoteObject>& proxy, int32_t code,
432 const std::string& receiverData, bool abortEvent, bool& funcResult)
433 {
434 EVENT_LOGD("enter");
435
436 if (!IsReady()) {
437 EVENT_LOGE("CommonEventManagerService not ready");
438 funcResult = false;
439 return ERR_OK;
440 }
441 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
442 std::function<void()> finishReceiverFunc = [wp, proxy, code, receiverData, abortEvent] () {
443 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
444 if (innerCommonEventManager == nullptr) {
445 EVENT_LOGE("innerCommonEventManager not exist");
446 return;
447 }
448 innerCommonEventManager->FinishReceiver(proxy, code, receiverData, abortEvent);
449 };
450
451 commonEventSrvQueue_->submit(finishReceiverFunc);
452 funcResult = true;
453 return ERR_OK;
454 }
455
Freeze(uint32_t uid,bool & funcResult)456 ErrCode CommonEventManagerService::Freeze(uint32_t uid, bool& funcResult)
457 {
458 EVENT_LOGD("enter");
459
460 auto tokenId = IPCSkeleton::GetCallingTokenID();
461 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
462 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
463 EVENT_LOGE("Not subsystem request");
464 funcResult = false;
465 return ERR_OK;
466 }
467
468 if (!IsReady()) {
469 EVENT_LOGE("CommonEventManagerService not ready");
470 funcResult = false;
471 return ERR_OK;
472 }
473 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
474 std::function<void()> freezeFunc = [wp, uid] () {
475 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
476 if (innerCommonEventManager == nullptr) {
477 EVENT_LOGE("innerCommonEventManager not exist");
478 return;
479 }
480 innerCommonEventManager->Freeze(uid);
481 };
482
483 commonEventSrvQueue_->submit(freezeFunc);
484 funcResult = true;
485 return ERR_OK;
486 }
487
Unfreeze(uint32_t uid,bool & funcResult)488 ErrCode CommonEventManagerService::Unfreeze(uint32_t uid, bool& funcResult)
489 {
490 EVENT_LOGD("enter");
491
492 auto tokenId = IPCSkeleton::GetCallingTokenID();
493 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
494 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
495 EVENT_LOGE("Not subsystem request");
496 funcResult = false;
497 return ERR_OK;
498 }
499
500 if (!IsReady()) {
501 EVENT_LOGE("CommonEventManagerService not ready");
502 funcResult = false;
503 return ERR_OK;
504 }
505
506 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
507 std::function<void()> unfreezeFunc = [wp, uid] () {
508 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
509 if (innerCommonEventManager == nullptr) {
510 EVENT_LOGE("innerCommonEventManager not exist");
511 return;
512 }
513 innerCommonEventManager->Unfreeze(uid);
514 };
515
516 commonEventSrvQueue_->submit(unfreezeFunc);
517 funcResult = true;
518 return ERR_OK;
519 }
520
UnfreezeAll(bool & funcResult)521 ErrCode CommonEventManagerService::UnfreezeAll(bool& funcResult)
522 {
523 EVENT_LOGD("enter");
524
525 auto tokenId = IPCSkeleton::GetCallingTokenID();
526 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
527 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
528 EVENT_LOGE("Not subsystem request");
529 funcResult = false;
530 return ERR_OK;
531 }
532
533 if (!IsReady()) {
534 EVENT_LOGE("CommonEventManagerService not ready");
535 funcResult = false;
536 return ERR_OK;
537 }
538
539 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
540 std::function<void()> unfreezeAllFunc = [wp] () {
541 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
542 if (innerCommonEventManager == nullptr) {
543 EVENT_LOGE("innerCommonEventManager not exist");
544 return;
545 }
546 innerCommonEventManager->UnfreezeAll();
547 };
548
549 commonEventSrvQueue_->submit(unfreezeAllFunc);
550 funcResult = true;
551 return ERR_OK;
552 }
553
Dump(int fd,const std::vector<std::u16string> & args)554 int CommonEventManagerService::Dump(int fd, const std::vector<std::u16string> &args)
555 {
556 EVENT_LOGD("enter");
557
558 auto callerToken = IPCSkeleton::GetCallingTokenID();
559 if (!AccessTokenHelper::VerifyShellToken(callerToken) && !AccessTokenHelper::VerifyNativeToken(callerToken)) {
560 EVENT_LOGE("Not subsystem or shell request");
561 return ERR_NOTIFICATION_CES_COMMON_PERMISSION_DENIED;
562 }
563 if (!IsReady()) {
564 EVENT_LOGE("CommonEventManagerService not ready");
565 return ERR_INVALID_VALUE;
566 }
567 std::string result;
568 innerCommonEventManager_->HiDump(args, result);
569 int ret = dprintf(fd, "%s\n", result.c_str());
570 if (ret < 0) {
571 EVENT_LOGE("dprintf error");
572 return ERR_INVALID_VALUE;
573 }
574 return ERR_OK;
575 }
576
RemoveStickyCommonEvent(const std::string & event,int32_t & funcResult)577 ErrCode CommonEventManagerService::RemoveStickyCommonEvent(const std::string& event, int32_t& funcResult)
578 {
579 EVENT_LOGI("enter %{public}s", event.c_str());
580
581 if (!IsReady()) {
582 EVENT_LOGE("CommonEventManagerService not ready");
583 funcResult = ERR_NOTIFICATION_CESM_ERROR;
584 return ERR_OK;
585 }
586
587 auto tokenId = IPCSkeleton::GetCallingTokenID();
588 bool isSubsystem = AccessTokenHelper::VerifyNativeToken(tokenId);
589 if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
590 EVENT_LOGE("Not system application or subsystem request.");
591 funcResult = ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
592 return ERR_OK;
593 }
594
595 const std::string permission = "ohos.permission.COMMONEVENT_STICKY";
596 bool ret = AccessTokenHelper::VerifyAccessToken(tokenId, permission);
597 if (!ret && (!isSubsystem || supportCheckSaPermission_.compare("true") == 0)) {
598 EVENT_LOGE("No permission.");
599 funcResult = ERR_NOTIFICATION_CES_COMMON_PERMISSION_DENIED;
600 return ERR_OK;
601 }
602 funcResult = innerCommonEventManager_->RemoveStickyCommonEvent(event, IPCSkeleton::GetCallingUid());
603 return ERR_OK;
604 }
605
SetStaticSubscriberState(bool enable,int32_t & funcResult)606 ErrCode CommonEventManagerService::SetStaticSubscriberState(bool enable, int32_t& funcResult)
607 {
608 if (!AccessTokenHelper::IsSystemApp()) {
609 EVENT_LOGE("Not system application");
610 funcResult = ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
611 return ERR_OK;
612 }
613 funcResult = innerCommonEventManager_->SetStaticSubscriberState(enable);
614 return ERR_OK;
615 }
616
SetStaticSubscriberStateByEvents(const std::vector<std::string> & events,bool enable,int32_t & funcResult)617 ErrCode CommonEventManagerService::SetStaticSubscriberStateByEvents(const std::vector<std::string>& events, bool enable,
618 int32_t& funcResult)
619 {
620 if (!AccessTokenHelper::IsSystemApp()) {
621 EVENT_LOGE("Not system application.");
622 funcResult = ERR_NOTIFICATION_CES_COMMON_NOT_SYSTEM_APP;
623 return ERR_OK;
624 }
625 funcResult = innerCommonEventManager_->SetStaticSubscriberState(events, enable);
626 return ERR_OK;
627 }
628
SetFreezeStatus(const std::set<int32_t> & pidList,bool isFreeze,bool & funcResult)629 ErrCode CommonEventManagerService::SetFreezeStatus(const std::set<int32_t>& pidList, bool isFreeze, bool& funcResult)
630 {
631 EVENT_LOGD("enter");
632 auto tokenId = IPCSkeleton::GetCallingTokenID();
633 if (!AccessTokenHelper::VerifyNativeToken(tokenId) ||
634 AccessTokenHelper::GetCallingProcessName(tokenId) != RESOURCE_MANAGER_PROCESS_NAME) {
635 EVENT_LOGE("Not subsystem request");
636 funcResult = false;
637 return ERR_OK;
638 }
639
640 if (!IsReady()) {
641 EVENT_LOGE("CommonEventManagerService not ready");
642 funcResult = false;
643 return ERR_OK;
644 }
645
646 std::weak_ptr<InnerCommonEventManager> wp = innerCommonEventManager_;
647 std::function<void()> setFreezeStatusFunc = [wp, pidList, isFreeze] () {
648 std::shared_ptr<InnerCommonEventManager> innerCommonEventManager = wp.lock();
649 if (innerCommonEventManager == nullptr) {
650 EVENT_LOGE("innerCommonEventManager not exist");
651 return;
652 }
653 innerCommonEventManager->SetFreezeStatus(pidList, isFreeze);
654 };
655
656 commonEventSrvQueue_->submit(setFreezeStatusFunc);
657 funcResult = true;
658 return ERR_OK;
659 }
660
CheckUserIdParams(const int32_t & userId)661 int32_t CommonEventManagerService::CheckUserIdParams(const int32_t &userId)
662 {
663 if (userId != ALL_USER && userId != CURRENT_USER && userId != UNDEFINED_USER
664 && !OsAccountManagerHelper::GetInstance()->CheckUserExists(userId)) {
665 EVENT_LOGE("UserId %{public}d is not exists in OsAccount", userId);
666 return ERR_NOTIFICATION_CES_USERID_INVALID;
667 }
668 return ERR_OK;
669 }
670 } // namespace EventFwk
671 } // namespace OHOS
672