• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# samgr<a name="EN-US_TOPIC_0000001162068341"></a>
2
3-   [Introduction](#section11660541593)
4-   [Directory Structure](#section161941989596)
5-   [Usage](#section1312121216216)
6-   [Repositories Involved](#section1371113476307)
7
8## Introduction<a name="section11660541593"></a>
9
10The  **samgr**  module is a core module of OpenHarmony. It provides functions related to system abilities \(also called system services\), including system ability \(SA\) startup, registration, and query.
11
12![](figures/en-us_image_0000001115820566.png)
13
14## Directory Structure<a name="section161941989596"></a>
15
16```
17/foundation/distributedschedule/samgr/services/samgr/
18├── native
19│   ├── BUILD.gn  # Compilation script
20│   ├── include   # Header files
21│   ├── samgr.rc  # RC file for starting samgr
22│   ├── source    # Source code
23│   ├── test      # Test code
24```
25
26## Usage<a name="section1312121216216"></a>
27
281.  After receiving the registration message from the SA framework, the samgr service saves information about the particular SA in the local cache.
29
30    ```
31    int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sptr<IRemoteObject>& ability,
32        const SAExtraProp& extraProp)
33    {
34        if (!CheckInputSysAbilityId(systemAbilityId) || ability == nullptr || (!CheckCapability(extraProp.capability))) {
35            HILOGE("AddSystemAbilityExtra input params is invalid.");
36            return ERR_INVALID_VALUE;
37        }
38        {
39            unique_lock<shared_mutex> writeLock(abilityMapLock_);
40            auto saSize = abilityMap_.size();
41            if (saSize >= MAX_SERVICES) {
42                HILOGE("map size error, (Has been greater than %zu)", saSize);
43                return ERR_INVALID_VALUE;
44            }
45            SAInfo saInfo;
46            saInfo.remoteObj = ability;
47            saInfo.isDistributed = extraProp.isDistributed;
48            saInfo.capability = extraProp.capability;
49            saInfo.permission = Str16ToStr8(extraProp.permission);
50            abilityMap_[systemAbilityId] = std::move(saInfo);
51            HILOGI("insert %{public}d. size : %zu,", systemAbilityId, abilityMap_.size());
52        }
53        if (abilityDeath_ != nullptr) {
54            ability->AddDeathRecipient(abilityDeath_);
55        }
56        FindSystemAbilityManagerNotify(systemAbilityId, ADD_SYSTEM_ABILITY_TRANSACTION);
57        u16string strName = Str8ToStr16(to_string(systemAbilityId));
58        if (extraProp.isDistributed && dBinderService_ != nullptr) {
59            dBinderService_->RegisterRemoteProxy(strName, ability);
60            HILOGD("AddSystemAbility RegisterRemoteProxy, serviceId is %{public}d", systemAbilityId);
61        }
62        if (systemAbilityDataStorage_ == nullptr) {
63            HILOGE("AddSystemAbility systemAbilityDataStorage not init!");
64            return ERR_NO_INIT;
65        }
66        if (extraProp.isDistributed) {
67            systemAbilityDataStorage_->ClearSyncRecords();
68            DoInsertSaData(strName, ability, extraProp);
69        }
70    }
71    ```
72
732.  If the SA requested by the SA framework is a local SA, the samgr service searches for the proxy object of the SA based on the SA ID and then returns the proxy object to the SA framework.
74
75    ```
76    sptr<IRemoteObject> SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId)
77    {
78        if (!CheckInputSysAbilityId(systemAbilityId)) {
79            return nullptr;
80        }
81
82        std::string selectedDeviceId;
83        if (CheckRemoteSa(to_string(systemAbilityId), selectedDeviceId)) {
84           return CheckSystemAbility(systemAbilityId, selectedDeviceId);
85        }
86
87        shared_lock<shared_mutex> readLock(abilityMapLock_);
88        auto iter = abilityMap_.find(systemAbilityId);
89        if (iter != abilityMap_.end()) {
90            auto callingUid = IPCSkeleton::GetCallingUid();
91            if (IsSystemApp(callingUid) || CheckPermission(iter->second.permission)) {
92                 return iter->second.remoteObj;
93            }
94            return nullptr;
95        }
96        return nullptr;
97    }
98    ```
99
100
101## Repositories Involved<a name="section1371113476307"></a>
102
103Distributed Scheduler subsystem
104
105distributedschedule\_dms\_fwk
106
107distributedschedule\_safwk
108
109**distributedschedule\_samgr**
110
111distributedschedule\_safwk\_lite
112
113hdistributedschedule\_samgr\_lite
114
115distributedschedule\_dms\_fwk\_lite
116
117