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 <cerrno>
17 #include <climits>
18 #include <fstream>
19 #include <sys/stat.h>
20 #include "time_zone_info.h"
21 #include "ipc_skeleton.h"
22 #include "time_file_utils.h"
23
24 namespace OHOS {
25 namespace MiscServices {
26 namespace {
27 constexpr const char *TIMEZONE_KEY = "persist.time.timezone";
28 constexpr const char *TIMEZONE_LIST_CONFIG_PATH = "/system/etc/zoneinfo/timezone_list.cfg";
29 constexpr const char *DISTRO_TIMEZONE_LIST_CONFIG = "/system/etc/tzdata_distro/timezone_list.cfg";
30 const int TIMEZONE_OK = 0;
31 const int CONFIG_LEN = 35;
32 const int HOUR_TO_MIN = 60;
33 } // namespace
34
GetInstance()35 TimeZoneInfo &TimeZoneInfo::GetInstance()
36 {
37 static TimeZoneInfo instance;
38 return instance;
39 }
40
Init()41 void TimeZoneInfo::Init()
42 {
43 TIME_HILOGD(TIME_MODULE_SERVICE, "Start.");
44 char value[CONFIG_LEN] = "Asia/Shanghai";
45 if (GetParameter(TIMEZONE_KEY, "", value, CONFIG_LEN) < TIMEZONE_OK) {
46 TIME_HILOGW(TIME_MODULE_SERVICE, "No found timezone from system parameter.");
47 }
48 if (!SetTimezone(value)) {
49 TIME_HILOGE(TIME_MODULE_SERVICE, "Init Set kernel failed.");
50 }
51 curTimezoneId_ = value;
52 TIME_HILOGD(TIME_MODULE_SERVICE, "Timezone value: %{public}s", value);
53 }
54
SetTimezone(const std::string & timezoneId)55 bool TimeZoneInfo::SetTimezone(const std::string &timezoneId)
56 {
57 std::lock_guard<std::mutex> lock(timezoneMutex_);
58 if (curTimezoneId_ == timezoneId) {
59 TIME_HILOGI(TIME_MODULE_SERVICE, "Same Timezone has been set.");
60 return true;
61 }
62 TIME_HILOGI(TIME_MODULE_SERVICE, "Set timezone : %{public}s, Current timezone : %{public}s, uid: %{public}d",
63 timezoneId.c_str(), curTimezoneId_.c_str(), IPCSkeleton::GetCallingUid());
64 std::set<std::string> availableTimeZoneIDs = GetTimeZoneAvailableIDs();
65 if (availableTimeZoneIDs.find(timezoneId) == availableTimeZoneIDs.end()) {
66 TIME_HILOGE(TIME_MODULE_SERVICE, "Invalid timezone");
67 return false;
68 }
69 setenv("TZ", timezoneId.c_str(), 1);
70 tzset();
71 if (!SetTimezoneToKernel()) {
72 TIME_HILOGE(TIME_MODULE_SERVICE, "SetTimezone Set kernel failed.");
73 return false;
74 }
75 auto errNo = SetParameter(TIMEZONE_KEY, timezoneId.c_str());
76 if (errNo > TIMEZONE_OK) {
77 TIME_HILOGE(TIME_MODULE_SERVICE, "SetTimezone timezoneId: %{public}d: %{public}s", errNo, timezoneId.c_str());
78 return false;
79 }
80 curTimezoneId_ = timezoneId;
81 return true;
82 }
83
GetTimeZoneAvailableIDs()84 std::set<std::string> TimeZoneInfo::GetTimeZoneAvailableIDs()
85 {
86 std::set<std::string> availableTimeZoneIDs;
87 struct stat s;
88 const char *tzIdConfigPath = stat(DISTRO_TIMEZONE_LIST_CONFIG, &s) == 0 ?
89 DISTRO_TIMEZONE_LIST_CONFIG : TIMEZONE_LIST_CONFIG_PATH;
90 std::unique_ptr<char[]> resolvedPath = std::make_unique<char[]>(PATH_MAX + 1);
91 if (realpath(tzIdConfigPath, resolvedPath.get()) == nullptr) {
92 TIME_HILOGE(TIME_MODULE_SERVICE, "Get realpath failed, errno: %{public}d.", errno);
93 return availableTimeZoneIDs;
94 }
95 std::ifstream file(resolvedPath.get());
96 if (!file.good()) {
97 TIME_HILOGE(TIME_MODULE_SERVICE, "Open timezone list config file failed.");
98 return availableTimeZoneIDs;
99 }
100 std::string line;
101 while (std::getline(file, line)) {
102 if (line.length() == 0) {
103 break;
104 }
105 line.resize(line.find_last_not_of("\r\n") + 1);
106 availableTimeZoneIDs.insert(line);
107 }
108 file.close();
109 return availableTimeZoneIDs;
110 }
111
GetTimezone(std::string & timezoneId)112 bool TimeZoneInfo::GetTimezone(std::string &timezoneId)
113 {
114 timezoneId = curTimezoneId_;
115 return true;
116 }
117
SetTimezoneToKernel()118 bool TimeZoneInfo::SetTimezoneToKernel()
119 {
120 time_t t = time(nullptr);
121 struct tm *localTime = localtime(&t);
122 struct timezone tz {};
123 if (localTime == nullptr) {
124 TIME_HILOGE(TIME_MODULE_SERVICE, "localtime is nullptr errornum: %{public}s.", strerror(errno));
125 return false;
126 }
127 tz.tz_minuteswest = -localTime->tm_gmtoff / HOUR_TO_MIN;
128 tz.tz_dsttime = 0;
129 int result = settimeofday(nullptr, &tz);
130 if (result < 0) {
131 TIME_HILOGE(TIME_MODULE_SERVICE, "Settimeofday timezone fail: %{public}d.", result);
132 return false;
133 }
134 TIME_HILOGD(TIME_MODULE_SERVICE, "Settimeofday timezone success ");
135 return true;
136 }
137 } // namespace MiscServices
138 } // namespace OHOS