1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "errno.h"
33 #include "sysinfo.h"
34 #include "sys/reboot.h"
35 #include "sys/resource.h"
36 #include "sys/times.h"
37 #include "sys/utsname.h"
38 #include "capability_type.h"
39 #include "capability_api.h"
40 #include "los_process_pri.h"
41 #include "los_strncpy_from_user.h"
42 #ifdef LOSCFG_SHELL
43 #include "shcmd.h"
44 #include "shmsg.h"
45 #endif
46 #include "user_copy.h"
47 #include "unistd.h"
48
49 #ifdef LOSCFG_UTS_CONTAINER
50 #define HOST_NAME_MAX_LEN 65
SysSetHostName(const char * name,size_t len)51 int SysSetHostName(const char *name, size_t len)
52 {
53 int ret;
54 char tmpName[HOST_NAME_MAX_LEN];
55 unsigned int intSave;
56
57 if (name == NULL) {
58 return -EFAULT;
59 }
60
61 if ((len < 0) || (len > HOST_NAME_MAX_LEN)) {
62 return -EINVAL;
63 }
64
65 ret = LOS_ArchCopyFromUser(&tmpName, name, len);
66 if (ret != 0) {
67 return -EFAULT;
68 }
69
70 SCHEDULER_LOCK(intSave);
71 struct utsname *currentUtsName = OsGetCurrUtsName();
72 if (currentUtsName == NULL) {
73 SCHEDULER_UNLOCK(intSave);
74 return -EFAULT;
75 }
76
77 (VOID)memset_s(currentUtsName->nodename, sizeof(currentUtsName->nodename), 0, sizeof(currentUtsName->nodename));
78 ret = memcpy_s(currentUtsName->nodename, sizeof(currentUtsName->nodename), tmpName, len);
79 if (ret != EOK) {
80 SCHEDULER_UNLOCK(intSave);
81 return -EFAULT;
82 }
83 SCHEDULER_UNLOCK(intSave);
84 return 0;
85 }
86 #endif
87
SysUname(struct utsname * name)88 int SysUname(struct utsname *name)
89 {
90 int ret;
91 struct utsname tmpName;
92 ret = LOS_ArchCopyFromUser(&tmpName, name, sizeof(struct utsname));
93 if (ret != 0) {
94 return -EFAULT;
95 }
96 ret = uname(&tmpName);
97 if (ret < 0) {
98 return ret;
99 }
100 ret = LOS_ArchCopyToUser(name, &tmpName, sizeof(struct utsname));
101 if (ret != 0) {
102 return -EFAULT;
103 }
104 return ret;
105 }
106
SysInfo(struct sysinfo * info)107 int SysInfo(struct sysinfo *info)
108 {
109 int ret;
110 struct sysinfo tmpInfo = { 0 };
111
112 tmpInfo.totalram = LOS_MemPoolSizeGet(m_aucSysMem1);
113 tmpInfo.freeram = LOS_MemPoolSizeGet(m_aucSysMem1) - LOS_MemTotalUsedGet(m_aucSysMem1);
114 tmpInfo.sharedram = 0;
115 tmpInfo.bufferram = 0;
116 tmpInfo.totalswap = 0;
117 tmpInfo.freeswap = 0;
118
119 ret = LOS_ArchCopyToUser(info, &tmpInfo, sizeof(struct sysinfo));
120 if (ret != 0) {
121 return -EFAULT;
122 }
123 return 0;
124 }
125
SysReboot(int magic,int magic2,int type)126 int SysReboot(int magic, int magic2, int type)
127 {
128 (void)magic;
129 (void)magic2;
130 if (!IsCapPermit(CAP_REBOOT)) {
131 return -EPERM;
132 }
133 SystemRebootFunc rebootHook = OsGetRebootHook();
134 if ((type == RB_AUTOBOOT) && (rebootHook != NULL)) {
135 rebootHook();
136 return 0;
137 }
138 return -EFAULT;
139 }
140
141 #ifdef LOSCFG_SHELL
SysShellExec(const char * msgName,const char * cmdString)142 int SysShellExec(const char *msgName, const char *cmdString)
143 {
144 int ret;
145 unsigned int uintRet;
146 errno_t err;
147 CmdParsed cmdParsed;
148 char msgNameDup[CMD_KEY_LEN + 1] = { 0 };
149 char cmdStringDup[CMD_MAX_LEN + 1] = { 0 };
150
151 if (!IsCapPermit(CAP_SHELL_EXEC)) {
152 return -EPERM;
153 }
154
155 ret = LOS_StrncpyFromUser(msgNameDup, msgName, CMD_KEY_LEN + 1);
156 if (ret < 0) {
157 return -EFAULT;
158 } else if (ret > CMD_KEY_LEN) {
159 return -ENAMETOOLONG;
160 }
161
162 ret = LOS_StrncpyFromUser(cmdStringDup, cmdString, CMD_MAX_LEN + 1);
163 if (ret < 0) {
164 return -EFAULT;
165 } else if (ret > CMD_MAX_LEN) {
166 return -ENAMETOOLONG;
167 }
168
169 err = memset_s(&cmdParsed, sizeof(CmdParsed), 0, sizeof(CmdParsed));
170 if (err != EOK) {
171 return -EFAULT;
172 }
173
174 uintRet = ShellMsgTypeGet(&cmdParsed, msgNameDup);
175 if (uintRet != LOS_OK) {
176 PRINTK("%s:command not found\n", msgNameDup);
177 return -EFAULT;
178 } else {
179 (void)OsCmdExec(&cmdParsed, (char *)cmdStringDup);
180 }
181
182 return 0;
183 }
184 #endif
185
186 #define USEC_PER_SEC 1000000
187
ConvertClocks(struct timeval * time,clock_t clk)188 static void ConvertClocks(struct timeval *time, clock_t clk)
189 {
190 time->tv_usec = (clk % CLOCKS_PER_SEC) * USEC_PER_SEC / CLOCKS_PER_SEC;
191 time->tv_sec = (clk) / CLOCKS_PER_SEC;
192 }
193
SysGetrusage(int what,struct rusage * ru)194 int SysGetrusage(int what, struct rusage *ru)
195 {
196 int ret;
197 struct tms time;
198 clock_t usec, sec;
199 struct rusage kru;
200
201 ret = LOS_ArchCopyFromUser(&kru, ru, sizeof(struct rusage));
202 if (ret != 0) {
203 return -EFAULT;
204 }
205
206 if (times(&time) == -1) {
207 return -EFAULT;
208 }
209
210 switch (what) {
211 case RUSAGE_SELF: {
212 usec = time.tms_utime;
213 sec = time.tms_stime;
214 break;
215 }
216 case RUSAGE_CHILDREN: {
217 usec = time.tms_cutime;
218 sec = time.tms_cstime;
219 break;
220 }
221 default:
222 return -EINVAL;
223 }
224 ConvertClocks(&kru.ru_utime, usec);
225 ConvertClocks(&kru.ru_stime, sec);
226
227 ret = LOS_ArchCopyToUser(ru, &kru, sizeof(struct rusage));
228 if (ret != 0) {
229 return -EFAULT;
230 }
231 return 0;
232 }
233
SysSysconf(int name)234 long SysSysconf(int name)
235 {
236 long ret;
237
238 ret = sysconf(name);
239 if (ret == -1) {
240 return -get_errno();
241 }
242
243 return ret;
244 }
245
SysUgetrlimit(int resource,unsigned long long k_rlim[2])246 int SysUgetrlimit(int resource, unsigned long long k_rlim[2])
247 {
248 int ret;
249 struct rlimit lim = { 0 };
250
251 ret = getrlimit(resource, &lim);
252 if (ret < 0) {
253 return ret;
254 }
255
256 ret = LOS_ArchCopyToUser(k_rlim, &lim, sizeof(struct rlimit));
257 if (ret != 0) {
258 return -EFAULT;
259 }
260
261 return ret;
262 }
263
SysSetrlimit(int resource,unsigned long long k_rlim[2])264 int SysSetrlimit(int resource, unsigned long long k_rlim[2])
265 {
266 int ret;
267 struct rlimit lim;
268
269 if(!IsCapPermit(CAP_CAPSET)) {
270 return -EPERM;
271 }
272
273 ret = LOS_ArchCopyFromUser(&lim, k_rlim, sizeof(struct rlimit));
274 if (ret != 0) {
275 return -EFAULT;
276 }
277 ret = setrlimit(resource, &lim);
278
279 return ret;
280 }
281