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
SysUname(struct utsname * name)50 int SysUname(struct utsname *name)
51 {
52 int ret;
53 struct utsname tmpName;
54 ret = LOS_ArchCopyFromUser(&tmpName, name, sizeof(struct utsname));
55 if (ret != 0) {
56 return -EFAULT;
57 }
58 ret = uname(&tmpName);
59 if (ret < 0) {
60 return ret;
61 }
62 ret = LOS_ArchCopyToUser(name, &tmpName, sizeof(struct utsname));
63 if (ret != 0) {
64 return -EFAULT;
65 }
66 return ret;
67 }
68
SysInfo(struct sysinfo * info)69 int SysInfo(struct sysinfo *info)
70 {
71 int ret;
72 struct sysinfo tmpInfo = { 0 };
73
74 tmpInfo.totalram = LOS_MemPoolSizeGet(m_aucSysMem1);
75 tmpInfo.freeram = LOS_MemPoolSizeGet(m_aucSysMem1) - LOS_MemTotalUsedGet(m_aucSysMem1);
76 tmpInfo.sharedram = 0;
77 tmpInfo.bufferram = 0;
78 tmpInfo.totalswap = 0;
79 tmpInfo.freeswap = 0;
80
81 ret = LOS_ArchCopyToUser(info, &tmpInfo, sizeof(struct sysinfo));
82 if (ret != 0) {
83 return -EFAULT;
84 }
85 return 0;
86 }
87
SysReboot(int magic,int magic2,int type)88 int SysReboot(int magic, int magic2, int type)
89 {
90 (void)magic;
91 (void)magic2;
92 if (!IsCapPermit(CAP_REBOOT)) {
93 return -EPERM;
94 }
95 SystemRebootFunc rebootHook = OsGetRebootHook();
96 if ((type == RB_AUTOBOOT) && (rebootHook != NULL)) {
97 rebootHook();
98 return 0;
99 }
100 return -EFAULT;
101 }
102
103 #ifdef LOSCFG_SHELL
SysShellExec(const char * msgName,const char * cmdString)104 int SysShellExec(const char *msgName, const char *cmdString)
105 {
106 int ret;
107 unsigned int uintRet;
108 errno_t err;
109 CmdParsed cmdParsed;
110 char msgNameDup[CMD_KEY_LEN + 1] = { 0 };
111 char cmdStringDup[CMD_MAX_LEN + 1] = { 0 };
112
113 if (!IsCapPermit(CAP_SHELL_EXEC)) {
114 return -EPERM;
115 }
116
117 ret = LOS_StrncpyFromUser(msgNameDup, msgName, CMD_KEY_LEN + 1);
118 if (ret < 0) {
119 return -EFAULT;
120 } else if (ret > CMD_KEY_LEN) {
121 return -ENAMETOOLONG;
122 }
123
124 ret = LOS_StrncpyFromUser(cmdStringDup, cmdString, CMD_MAX_LEN + 1);
125 if (ret < 0) {
126 return -EFAULT;
127 } else if (ret > CMD_MAX_LEN) {
128 return -ENAMETOOLONG;
129 }
130
131 err = memset_s(&cmdParsed, sizeof(CmdParsed), 0, sizeof(CmdParsed));
132 if (err != EOK) {
133 return -EFAULT;
134 }
135
136 uintRet = ShellMsgTypeGet(&cmdParsed, msgNameDup);
137 if (uintRet != LOS_OK) {
138 PRINTK("%s:command not found\n", msgNameDup);
139 return -EFAULT;
140 } else {
141 (void)OsCmdExec(&cmdParsed, (char *)cmdStringDup);
142 }
143
144 return 0;
145 }
146 #endif
147
148 #define USEC_PER_SEC 1000000
149
ConvertClocks(struct timeval * time,clock_t clk)150 static void ConvertClocks(struct timeval *time, clock_t clk)
151 {
152 time->tv_usec = (clk % CLOCKS_PER_SEC) * USEC_PER_SEC / CLOCKS_PER_SEC;
153 time->tv_sec = (clk) / CLOCKS_PER_SEC;
154 }
155
SysGetrusage(int what,struct rusage * ru)156 int SysGetrusage(int what, struct rusage *ru)
157 {
158 int ret;
159 struct tms time;
160 clock_t usec, sec;
161 struct rusage kru;
162
163 ret = LOS_ArchCopyFromUser(&kru, ru, sizeof(struct rusage));
164 if (ret != 0) {
165 return -EFAULT;
166 }
167
168 if (times(&time) == -1) {
169 return -EFAULT;
170 }
171
172 switch (what) {
173 case RUSAGE_SELF: {
174 usec = time.tms_utime;
175 sec = time.tms_stime;
176 break;
177 }
178 case RUSAGE_CHILDREN: {
179 usec = time.tms_cutime;
180 sec = time.tms_cstime;
181 break;
182 }
183 default:
184 return -EINVAL;
185 }
186 ConvertClocks(&kru.ru_utime, usec);
187 ConvertClocks(&kru.ru_stime, sec);
188
189 ret = LOS_ArchCopyToUser(ru, &kru, sizeof(struct rusage));
190 if (ret != 0) {
191 return -EFAULT;
192 }
193 return 0;
194 }
195
SysSysconf(int name)196 long SysSysconf(int name)
197 {
198 long ret;
199
200 ret = sysconf(name);
201 if (ret == -1) {
202 return -get_errno();
203 }
204
205 return ret;
206 }
207
SysUgetrlimit(int resource,unsigned long long k_rlim[2])208 int SysUgetrlimit(int resource, unsigned long long k_rlim[2])
209 {
210 int ret;
211 struct rlimit lim;
212
213 ret = getrlimit(resource, &lim);
214 if (ret < 0) {
215 return ret;
216 }
217
218 ret = LOS_ArchCopyToUser(k_rlim, &lim, sizeof(struct rlimit));
219 if (ret != 0) {
220 return -EFAULT;
221 }
222
223 return ret;
224 }
225
SysSetrlimit(int resource,unsigned long long k_rlim[2])226 int SysSetrlimit(int resource, unsigned long long k_rlim[2])
227 {
228 int ret;
229 struct rlimit lim;
230
231 if(!IsCapPermit(CAP_CAPSET)) {
232 return -EPERM;
233 }
234
235 ret = LOS_ArchCopyFromUser(&lim, k_rlim, sizeof(struct rlimit));
236 if (ret != 0) {
237 return -EFAULT;
238 }
239 ret = setrlimit(resource, &lim);
240
241 return ret;
242 }
243