• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "utility.h"
17 
18 #include <grp.h>
19 #include <pwd.h>
20 #include <unistd.h>
21 
22 #include <cerrno>
23 #include <limits>
24 #include <regex>
25 #include <sstream>
26 
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 
30 #include "securec.h"
31 
32 #include "devicestatus_define.h"
33 
34 namespace OHOS {
35 namespace Msdp {
36 namespace DeviceStatus {
37 namespace {
38 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "Utility" };
39 } // namespace
40 
CopyNulstr(char * dest,size_t size,const char * src)41 size_t Utility::CopyNulstr(char *dest, size_t size, const char *src)
42 {
43     CHKPR(dest, 0);
44     CHKPR(src, 0);
45 
46     size_t len = strlen(src);
47     if (len >= size) {
48         if (size > 1) {
49             len = size - 1;
50         } else {
51             len = 0;
52         }
53     }
54     if (len > 0) {
55         errno_t ret = memcpy_s(dest, size, src, len);
56         if (ret != EOK) {
57             FI_HILOGW("memcpy_s:bounds checking failed");
58         }
59     }
60     if (size > 0) {
61         dest[len] = '\0';
62     }
63     return len;
64 }
65 
StartWith(const char * str,const char * prefix)66 bool Utility::StartWith(const char *str, const char *prefix)
67 {
68     size_t prefixlen = strlen(prefix);
69     return (prefixlen > 0 ? (strncmp(str, prefix, strlen(prefix)) == 0) : false);
70 }
71 
StartWith(const std::string & str,const std::string & prefix)72 bool Utility::StartWith(const std::string &str, const std::string &prefix)
73 {
74     if (str.size() < prefix.size()) {
75         return false;
76     }
77     return (str.compare(0, prefix.size(), prefix) == 0);
78 }
79 
RemoveTrailingChars(char c,char * path)80 void Utility::RemoveTrailingChars(char c, char *path)
81 {
82     CHKPV(path);
83     size_t len = strlen(path);
84     while (len > 0 && path[len-1] == c) {
85         path[--len] = '\0';
86     }
87 }
88 
RemoveTrailingChars(const std::string & toRemoved,std::string & path)89 void Utility::RemoveTrailingChars(const std::string &toRemoved, std::string &path)
90 {
91     while (!path.empty() && (toRemoved.find(path.back()) != std::string::npos)) {
92         path.pop_back();
93     }
94 }
95 
RemoveSpace(std::string & str)96 void Utility::RemoveSpace(std::string &str)
97 {
98     str.erase(remove_if(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c);}), str.end());
99 }
100 
IsInteger(const std::string & target)101 bool Utility::IsInteger(const std::string &target)
102 {
103     std::regex pattern("^\\s*-?(0|([1-9]\\d*))\\s*$");
104     return std::regex_match(target, pattern);
105 }
106 
DoesFileExist(const char * path)107 bool Utility::DoesFileExist(const char *path)
108 {
109     return (access(path, F_OK) == 0);
110 }
111 
GetFileSize(const char * path)112 ssize_t Utility::GetFileSize(const char *path)
113 {
114     struct stat buf {};
115     ssize_t sz { 0 };
116 
117     if (stat(path, &buf) == 0) {
118         if (S_ISREG(buf.st_mode)) {
119             sz = buf.st_size;
120         } else {
121             FI_HILOGE("Not regular file:\'%{public}s\'", path);
122         }
123     } else {
124         FI_HILOGE("stat(\'%{public}s\') failed:%{public}s", path, strerror(errno));
125     }
126     return sz;
127 }
128 
ShowFileAttributes(const char * path)129 void Utility::ShowFileAttributes(const char *path)
130 {
131     CALL_DEBUG_ENTER;
132     FI_HILOGD("======================= File Attributes ========================");
133     FI_HILOGD("%{public}20s:%{public}s", "FILE NAME", path);
134 
135     struct stat buf {};
136     if (stat(path, &buf) != 0) {
137         FI_HILOGE("stat(\'%{public}s\') failed:%{public}s", path, strerror(errno));
138         return;
139     }
140     if (S_ISDIR(buf.st_mode)) {
141         FI_HILOGD("%{public}20s: directory", "TYPE");
142     } else if (S_ISCHR(buf.st_mode)) {
143         FI_HILOGD("%{public}20s: character special file", "TYPE");
144     } else if (S_ISREG(buf.st_mode)) {
145         FI_HILOGD("%{public}20s: regular file", "TYPE");
146     }
147 
148     std::ostringstream ss;
149     if (buf.st_mode & S_IRUSR) {
150         ss << "U+R ";
151     }
152     if (buf.st_mode & S_IWUSR) {
153         ss << "U+W ";
154     }
155     if (buf.st_mode & S_IXUSR) {
156         ss << "U+X ";
157     }
158     if (buf.st_mode & S_IRGRP) {
159         ss << "G+R ";
160     }
161     if (buf.st_mode & S_IWGRP) {
162         ss << "G+W ";
163     }
164     if (buf.st_mode & S_IXGRP) {
165         ss << "G+X ";
166     }
167     if (buf.st_mode & S_IROTH) {
168         ss << "O+R ";
169     }
170     if (buf.st_mode & S_IWOTH) {
171         ss << "O+W ";
172     }
173     if (buf.st_mode & S_IXOTH) {
174         ss << "O+X ";
175     }
176     FI_HILOGD("%{public}20s:%{public}s", "PERMISSIONS", ss.str().c_str());
177 }
178 
ShowUserAndGroup()179 void Utility::ShowUserAndGroup()
180 {
181     CALL_DEBUG_ENTER;
182     static constexpr size_t BUFSIZE { 1024 };
183     char buffer[BUFSIZE];
184     struct passwd buf;
185     struct passwd *pbuf = nullptr;
186     struct group grp;
187     struct group *pgrp = nullptr;
188 
189     FI_HILOGD("======================= Users and Groups =======================");
190     uid_t uid = getuid();
191     if (getpwuid_r(uid, &buf, buffer, sizeof(buffer), &pbuf) != 0) {
192         FI_HILOGE("getpwuid_r failed:%{public}s", strerror(errno));
193     } else {
194         FI_HILOGD("%{public}20s:%{public}10u%{public}20s", "USER", uid, buf.pw_name);
195     }
196 
197     gid_t gid = getgid();
198     if (getgrgid_r(gid, &grp, buffer, sizeof(buffer), &pgrp) != 0) {
199         FI_HILOGE("getgrgid_r failed:%{public}s", strerror(errno));
200     } else {
201         FI_HILOGD("%{public}20s:%{public}10u%{public}20s", "GROUP", gid, grp.gr_name);
202     }
203 
204     uid = geteuid();
205     if (getpwuid_r(uid, &buf, buffer, sizeof(buffer), &pbuf) != 0) {
206         FI_HILOGE("getpwuid_r failed:%{public}s", strerror(errno));
207     } else {
208         FI_HILOGD("%{public}20s:%{public}10u%{public}20s", "EFFECTIVE USER", uid, buf.pw_name);
209     }
210 
211     gid = getegid();
212     if (getgrgid_r(gid, &grp, buffer, sizeof(buffer), &pgrp) != 0) {
213         FI_HILOGE("getgrgid_r failed:%{public}s", strerror(errno));
214     } else {
215         FI_HILOGD("%{public}20s:%{public}10u%{public}20s", "EFFECTIVE GROUP", gid, grp.gr_name);
216     }
217 
218     gid_t groups[NGROUPS_MAX + 1];
219     int32_t ngrps = getgroups(sizeof(groups), groups);
220     for (int32_t i = 0; i < ngrps; ++i) {
221         if (getgrgid_r(groups[i], &grp, buffer, sizeof(buffer), &pgrp) != 0) {
222             FI_HILOGE("getgrgid_r failed:%{public}s", strerror(errno));
223         } else {
224             FI_HILOGD("%{public}20s:%{public}10u%{public}20s", "SUPPLEMENTARY GROUP", groups[i], grp.gr_name);
225         }
226     }
227 }
228 } // namespace DeviceStatus
229 } // namespace Msdp
230 } // namespace OHOS
231