1 /*
2 * Copyright (C) 2021 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 "simple_timer_info.h"
17 #include "time_common.h"
18 #include "time_xcollie.h"
19 #include "time_service_stub.h"
20 #include "ntp_update_time.h"
21 #include "ntp_trusted_time.h"
22
23 namespace OHOS {
24 namespace MiscServices {
25 using namespace OHOS::HiviewDFX;
26 namespace {
27 static const uint32_t MAX_EXEMPTION_SIZE = 1000;
28 static const int MAX_PID_LIST_SIZE = 1024;
29 }
30
31
TimeServiceStub()32 TimeServiceStub::TimeServiceStub()
33 {
34 memberFuncMap_ = {
35 { TimeServiceIpcInterfaceCode::SET_TIME,
36 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTime(data, reply); } },
37 { TimeServiceIpcInterfaceCode::SET_TIME_ZONE,
38 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTimeZone(data, reply); } },
39 { TimeServiceIpcInterfaceCode::GET_TIME_ZONE,
40 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetTimeZone(data, reply); } },
41 { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI,
42 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeMs(data, reply); } },
43 { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO,
44 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeNs(data, reply); } },
45 { TimeServiceIpcInterfaceCode::CREATE_TIMER,
46 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnCreateTimer(data, reply); } },
47 { TimeServiceIpcInterfaceCode::START_TIMER,
48 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStartTimer(data, reply); } },
49 { TimeServiceIpcInterfaceCode::STOP_TIMER,
50 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStopTimer(data, reply); } },
51 { TimeServiceIpcInterfaceCode::DESTROY_TIMER,
52 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnDestroyTimer(data, reply); } },
53 { TimeServiceIpcInterfaceCode::PROXY_TIMER,
54 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnTimerProxy(data, reply); } },
55 { TimeServiceIpcInterfaceCode::PID_PROXY_TIMER,
56 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnPidTimerProxy(data, reply); } },
57 { TimeServiceIpcInterfaceCode::ADJUST_TIMER,
58 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAdjustTimer(data, reply); } },
59 { TimeServiceIpcInterfaceCode::SET_TIMER_EXEMPTION,
60 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t {
61 return OnSetTimerExemption(data, reply); } },
62 { TimeServiceIpcInterfaceCode::RESET_ALL_PROXY,
63 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAllProxyReset(data, reply); } },
64 { TimeServiceIpcInterfaceCode::GET_NTP_TIME_MILLI,
65 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetNtpTimeMs(data, reply); } },
66 { TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI,
67 [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetRealTimeMs(data, reply); } },
68 };
69 }
70
~TimeServiceStub()71 TimeServiceStub::~TimeServiceStub()
72 {
73 memberFuncMap_.clear();
74 }
75
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
77 MessageOption &option)
78 {
79 TIME_HILOGD(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
80 std::u16string descriptor = TimeServiceStub::GetDescriptor();
81 std::u16string remoteDescriptor = data.ReadInterfaceToken();
82 if (descriptor != remoteDescriptor) {
83 TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail");
84 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
85 }
86 pid_t p = IPCSkeleton::GetCallingPid();
87 pid_t p1 = IPCSkeleton::GetCallingUid();
88 TIME_HILOGD(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1,
89 code);
90 if (code >= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME) &&
91 code <= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI)) {
92 auto itFunc = memberFuncMap_.find(static_cast<TimeServiceIpcInterfaceCode>(code));
93 if (itFunc != memberFuncMap_.end()) {
94 auto memberFunc = itFunc->second;
95 if (memberFunc != nullptr) {
96 return memberFunc(data, reply);
97 }
98 }
99 }
100 int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
101 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
102 return ret;
103 }
104
OnSetTime(MessageParcel & data,MessageParcel & reply)105 int32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply)
106 {
107 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
108 TimeXCollie timeXCollie("TimeService::SetTime");
109 int64_t time = data.ReadInt64();
110 auto apiVersion = data.ReadInt8();
111 if (apiVersion == APIVersion::API_VERSION_9) {
112 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
113 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
114 return E_TIME_NOT_SYSTEM_APP;
115 }
116 }
117 if (!TimePermission::CheckCallingPermission(TimePermission::setTime)) {
118 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
119 return E_TIME_NO_PERMISSION;
120 }
121 int32_t ret = SetTime(time);
122 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
123 return ret;
124 }
125
OnSetTimeZone(MessageParcel & data,MessageParcel & reply)126 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
127 {
128 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
129 TimeXCollie timeXCollie("TimeService::SetTimeZone");
130 std::string timeZoneId = data.ReadString();
131 auto apiVersion = data.ReadInt8();
132 if (apiVersion == APIVersion::API_VERSION_9) {
133 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
134 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
135 return E_TIME_NOT_SYSTEM_APP;
136 }
137 }
138 if (!TimePermission::CheckCallingPermission(TimePermission::setTimeZone)) {
139 TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
140 return E_TIME_NO_PERMISSION;
141 }
142 int32_t ret = SetTimeZone(timeZoneId);
143 TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
144 return ret;
145 }
146
OnGetTimeZone(MessageParcel & data,MessageParcel & reply)147 int32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply)
148 {
149 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
150 TimeXCollie timeXCollie("TimeService::GetTimeZone");
151 std::string timeZoneId;
152 int32_t ret = GetTimeZone(timeZoneId);
153 if (ret != ERR_OK) {
154 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
155 return ret;
156 }
157 reply.WriteString(timeZoneId);
158 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
159 return ret;
160 }
161
OnGetThreadTimeMs(MessageParcel & data,MessageParcel & reply)162 int32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)
163 {
164 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
165 TimeXCollie timeXCollie("TimeService::GetThreadTimeMs");
166 int64_t time;
167 int32_t ret = GetThreadTimeMs(time);
168 if (ret != ERR_OK) {
169 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
170 return ret;
171 }
172 reply.WriteInt64(time);
173 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
174 return ret;
175 }
176
OnGetThreadTimeNs(MessageParcel & data,MessageParcel & reply)177 int32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)
178 {
179 TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
180 TimeXCollie timeXCollie("TimeService::GetThreadTimeNs");
181 int64_t time;
182 int32_t ret = GetThreadTimeNs(time);
183 if (ret != ERR_OK) {
184 TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
185 return ret;
186 }
187 reply.WriteInt64(time);
188 TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
189 return ret;
190 }
191
OnCreateTimer(MessageParcel & data,MessageParcel & reply)192 int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply)
193 {
194 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
195 TimeXCollie timeXCollie("TimeService::CreateTimer");
196 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
197 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
198 return E_TIME_NOT_SYSTEM_APP;
199 }
200 std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent{ nullptr };
201 auto name = data.ReadString();
202 auto type = data.ReadInt32();
203 auto repeat = data.ReadBool();
204 auto disposable = data.ReadBool();
205 auto autoRestore = data.ReadBool();
206 auto interval = data.ReadUint64();
207 if (data.ReadBool()) {
208 wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>(
209 data.ReadParcelable<OHOS::AbilityRuntime::WantAgent::WantAgent>());
210 if (!wantAgent) {
211 TIME_HILOGE(TIME_MODULE_SERVICE, "Input wantagent nullptr");
212 return E_TIME_NULLPTR;
213 }
214 }
215 sptr<IRemoteObject> obj = data.ReadRemoteObject();
216 if (obj == nullptr) {
217 TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr");
218 return E_TIME_NULLPTR;
219 }
220 auto timerOptions = std::make_shared<SimpleTimerInfo>();
221 timerOptions->name = name;
222 timerOptions->type = type;
223 timerOptions->repeat = repeat;
224 timerOptions->interval = interval;
225 timerOptions->disposable = disposable;
226 timerOptions->autoRestore = autoRestore;
227 timerOptions->wantAgent = wantAgent;
228 uint64_t timerId = data.ReadUint64();
229 auto errCode = CreateTimer(timerOptions, obj, timerId);
230 if (errCode != E_TIME_OK) {
231 TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed");
232 return E_TIME_DEAL_FAILED;
233 }
234 if (!reply.WriteUint64(timerId)) {
235 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId");
236 return E_TIME_WRITE_PARCEL_ERROR;
237 }
238 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
239 return ERR_OK;
240 }
241
OnStartTimer(MessageParcel & data,MessageParcel & reply)242 int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply)
243 {
244 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
245 TimeXCollie timeXCollie("TimeService::StartTimer");
246 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
247 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
248 return E_TIME_NOT_SYSTEM_APP;
249 }
250 auto timerId = data.ReadUint64();
251 auto triggerTime = data.ReadUint64();
252 if (StartTimer(timerId, triggerTime) != E_TIME_OK) {
253 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer");
254 return E_TIME_DEAL_FAILED;
255 }
256 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
257 return ERR_OK;
258 }
259
OnStopTimer(MessageParcel & data,MessageParcel & reply)260 int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply)
261 {
262 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
263 TimeXCollie timeXCollie("TimeService::StopTimer");
264 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
265 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
266 return E_TIME_NOT_SYSTEM_APP;
267 }
268 auto timerId = data.ReadUint64();
269 if (StopTimer(timerId) != E_TIME_OK) {
270 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer");
271 return E_TIME_DEAL_FAILED;
272 }
273 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
274 return ERR_OK;
275 }
276
OnDestroyTimer(MessageParcel & data,MessageParcel & reply)277 int32_t TimeServiceStub::OnDestroyTimer(MessageParcel &data, MessageParcel &reply)
278 {
279 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
280 TimeXCollie timeXCollie("TimeService::DestroyTimer");
281 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
282 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
283 return E_TIME_NOT_SYSTEM_APP;
284 }
285 auto timerId = data.ReadUint64();
286 if (DestroyTimer(timerId, false) != E_TIME_OK) {
287 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer");
288 return E_TIME_DEAL_FAILED;
289 }
290 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
291 return ERR_OK;
292 }
293
OnTimerProxy(MessageParcel & data,MessageParcel & reply)294 int32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply)
295 {
296 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
297 TimeXCollie timeXCollie("TimeService::TimerProxy");
298 auto uid = data.ReadInt32();
299 if (uid == 0) {
300 TIME_HILOGE(TIME_MODULE_SERVICE, "Error param uid.");
301 return E_TIME_READ_PARCEL_ERROR;
302 }
303 auto isProxy = data.ReadBool();
304 auto needRetrigger = data.ReadBool();
305 if (!ProxyTimer(uid, isProxy, needRetrigger)) {
306 return E_TIME_DEAL_FAILED;
307 }
308 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
309 return ERR_OK;
310 }
311
OnPidTimerProxy(MessageParcel & data,MessageParcel & reply)312 int32_t TimeServiceStub::OnPidTimerProxy(MessageParcel &data, MessageParcel &reply)
313 {
314 TimeXCollie timeXCollie("TimeService::PidTimerProxy");
315 auto uid = data.ReadInt32();
316 auto pidListSize = data.ReadInt32();
317 std::set<int> pidList;
318 if (pidListSize == 0 || pidListSize > MAX_PID_LIST_SIZE) {
319 TIME_HILOGE(TIME_MODULE_SERVICE, "Error pid list size.");
320 return E_TIME_READ_PARCEL_ERROR;
321 }
322 for (int i = 0; i < pidListSize; i++) {
323 auto pid = data.ReadInt32();
324 if (pid != 0) {
325 pidList.insert(pid);
326 }
327 }
328 if (pidList.size() == 0) {
329 TIME_HILOGE(TIME_MODULE_SERVICE, "Error pidList.");
330 return E_TIME_READ_PARCEL_ERROR;
331 }
332 auto isProxy = data.ReadBool();
333 auto needRetrigger = data.ReadBool();
334 if (!ProxyTimer(uid, pidList, isProxy, needRetrigger)) {
335 return E_TIME_DEAL_FAILED;
336 }
337 return ERR_OK;
338 }
339
OnAdjustTimer(MessageParcel & data,MessageParcel & reply)340 int32_t TimeServiceStub::OnAdjustTimer(MessageParcel &data, MessageParcel &reply)
341 {
342 TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust start.");
343 TimeXCollie timeXCollie("TimeService::AdjustTimer");
344 if (!TimePermission::CheckProxyCallingPermission()) {
345 TIME_HILOGE(TIME_MODULE_SERVICE, "Adjust Timer permission check failed");
346 return E_TIME_NO_PERMISSION;
347 }
348 bool isAdjust = false;
349 uint32_t interval = 0;
350 if (!data.ReadBool(isAdjust)) {
351 return E_TIME_READ_PARCEL_ERROR;
352 }
353 if (!data.ReadUint32(interval)) {
354 return E_TIME_READ_PARCEL_ERROR;
355 }
356 if (isAdjust && interval == 0) {
357 TIME_HILOGE(TIME_MODULE_SERVICE, "invalid parameter: interval");
358 return E_TIME_READ_PARCEL_ERROR;
359 }
360 if (AdjustTimer(isAdjust, interval) != E_TIME_OK) {
361 TIME_HILOGE(TIME_MODULE_SERVICE, "Error adjust timer.");
362 return E_TIME_DEAL_FAILED;
363 }
364 TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust end.");
365 return ERR_OK;
366 }
367
OnSetTimerExemption(MessageParcel & data,MessageParcel & reply)368 int32_t TimeServiceStub::OnSetTimerExemption(MessageParcel &data, MessageParcel &reply)
369 {
370 TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption start.");
371 TimeXCollie timeXCollie("TimeService::SetTimerExemption");
372 if (!TimePermission::CheckProxyCallingPermission()) {
373 TIME_HILOGE(TIME_MODULE_SERVICE, "Set Timer Exemption permission check failed");
374 return E_TIME_NO_PERMISSION;
375 }
376 std::unordered_set<std::string> nameArr;
377 uint32_t size;
378 bool isExemption;
379 if (!data.ReadUint32(size)) {
380 return E_TIME_READ_PARCEL_ERROR;
381 }
382 if (size > MAX_EXEMPTION_SIZE) {
383 return E_TIME_PARAMETERS_INVALID;
384 }
385 for (uint32_t i = 0; i < size; ++i) {
386 std::string name;
387 if (!data.ReadString(name)) {
388 return E_TIME_READ_PARCEL_ERROR;
389 }
390 nameArr.insert(name);
391 }
392
393 if (!data.ReadBool(isExemption)) {
394 return E_TIME_READ_PARCEL_ERROR;
395 }
396 SetTimerExemption(nameArr, isExemption);
397 TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption end.");
398 return ERR_OK;
399 }
400
OnAllProxyReset(MessageParcel & data,MessageParcel & reply)401 int32_t TimeServiceStub::OnAllProxyReset(MessageParcel &data, MessageParcel &reply)
402 {
403 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
404 TimeXCollie timeXCollie("TimeService::AllProxyReset");
405 if (!ResetAllProxy()) {
406 return E_TIME_DEAL_FAILED;
407 }
408 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
409 return ERR_OK;
410 }
411
OnGetNtpTimeMs(MessageParcel & data,MessageParcel & reply)412 int32_t TimeServiceStub::OnGetNtpTimeMs(MessageParcel &data, MessageParcel &reply)
413 {
414 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
415 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
416 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
417 return E_TIME_NOT_SYSTEM_APP;
418 }
419 int64_t time = 0;
420 auto ret = GetNtpTimeMs(time);
421 if (ret != E_TIME_OK) {
422 return ret;
423 }
424 if (!reply.WriteUint64(time)) {
425 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time");
426 return E_TIME_WRITE_PARCEL_ERROR;
427 }
428 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
429 return ERR_OK;
430 }
431
OnGetRealTimeMs(MessageParcel & data,MessageParcel & reply)432 int32_t TimeServiceStub::OnGetRealTimeMs(MessageParcel &data, MessageParcel &reply)
433 {
434 TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
435 if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
436 TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
437 return E_TIME_NOT_SYSTEM_APP;
438 }
439 int64_t time = 0;
440 auto ret = GetRealTimeMs(time);
441 if (ret != E_TIME_OK) {
442 return ret;
443 }
444 if (!reply.WriteUint64(time)) {
445 TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time");
446 return E_TIME_WRITE_PARCEL_ERROR;
447 }
448 TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
449 return ERR_OK;
450 }
451 } // namespace MiscServices
452 } // namespace OHOS