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 "sys/types.h"
33 #include "sys/resource.h"
34 #include "unistd.h"
35 #include "stdio.h"
36 #include "pthread.h"
37 #include "sys/utsname.h"
38 #include "mqueue.h"
39 #include "semaphore.h"
40 #include "los_process_pri.h"
41 #include "los_hw.h"
42
43 /*
44 * Supply some suitable values for constants that may not be present
45 * in all configurations.
46 */
47 #define SC_ENABLE 1
48 #define SC_DISABLE (-1)
49
50 #define CONF_CASE_RETURN(name, val) \
51 case (name): \
52 return (val)
53
uname(struct utsname * name)54 int uname(struct utsname *name)
55 {
56 INT32 ret;
57 const char *cpuInfo = NULL;
58
59 if (name == NULL) {
60 return -EFAULT;
61 }
62 (VOID)strcpy_s(name->sysname, sizeof(name->sysname), KERNEL_NAME);
63 (VOID)strcpy_s(name->nodename, sizeof(name->nodename), "hisilicon");
64 ret = sprintf_s(name->version, sizeof(name->version), "%s %u.%u.%u.%u %s %s",
65 KERNEL_NAME, KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE, __DATE__, __TIME__);
66 if (ret < 0) {
67 return -EIO;
68 }
69
70 cpuInfo = LOS_CpuInfo();
71 (VOID)strcpy_s(name->machine, sizeof(name->machine), cpuInfo);
72 ret = sprintf_s(name->release, sizeof(name->release), "%u.%u.%u.%u",
73 KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE);
74 if (ret < 0) {
75 return -EIO;
76 }
77
78 name->domainname[0] = '\0';
79 return 0;
80 }
81
sysconf(int name)82 long sysconf(int name)
83 {
84 switch (name) {
85 CONF_CASE_RETURN(_SC_AIO_LISTIO_MAX, SC_DISABLE);
86 CONF_CASE_RETURN(_SC_AIO_MAX, SC_DISABLE);
87 CONF_CASE_RETURN(_SC_AIO_PRIO_DELTA_MAX, SC_DISABLE);
88 CONF_CASE_RETURN(_SC_ARG_MAX, ARG_MAX);
89 CONF_CASE_RETURN(_SC_ASYNCHRONOUS_IO, SC_DISABLE);
90 CONF_CASE_RETURN(_SC_CHILD_MAX, CHILD_MAX);
91 CONF_CASE_RETURN(_SC_CLK_TCK, SYS_CLK_TCK);
92 CONF_CASE_RETURN(_SC_DELAYTIMER_MAX, DELAYTIMER_MAX);
93 CONF_CASE_RETURN(_SC_FSYNC, SC_DISABLE);
94 CONF_CASE_RETURN(_SC_GETGR_R_SIZE_MAX, GETGR_R_SIZE_MAX);
95 CONF_CASE_RETURN(_SC_GETPW_R_SIZE_MAX, GETPW_R_SIZE_MAX);
96 CONF_CASE_RETURN(_SC_JOB_CONTROL, SC_DISABLE);
97 CONF_CASE_RETURN(_SC_LOGIN_NAME_MAX, LOGIN_NAME_MAX);
98 CONF_CASE_RETURN(_SC_MAPPED_FILES, SC_DISABLE);
99 CONF_CASE_RETURN(_SC_MEMLOCK, SC_DISABLE);
100 CONF_CASE_RETURN(_SC_MEMLOCK_RANGE, SC_DISABLE);
101 CONF_CASE_RETURN(_SC_MEMORY_PROTECTION, SC_DISABLE);
102 CONF_CASE_RETURN(_SC_MESSAGE_PASSING, SC_DISABLE);
103 #ifdef LOSCFG_BASE_IPC_QUEUE
104 CONF_CASE_RETURN(_SC_MQ_OPEN_MAX, MQ_OPEN_MAX);
105 CONF_CASE_RETURN(_SC_MQ_PRIO_MAX, MQ_PRIO_MAX);
106 #endif
107 CONF_CASE_RETURN(_SC_NGROUPS_MAX, NGROUPS_MAX);
108 CONF_CASE_RETURN(_SC_OPEN_MAX, OPEN_MAX);
109 CONF_CASE_RETURN(_SC_PAGESIZE, 0x1000);
110 CONF_CASE_RETURN(_SC_PRIORITIZED_IO, SC_DISABLE);
111 CONF_CASE_RETURN(_SC_PRIORITY_SCHEDULING, SC_DISABLE);
112 CONF_CASE_RETURN(_SC_REALTIME_SIGNALS, SC_DISABLE);
113 CONF_CASE_RETURN(_SC_RTSIG_MAX, RTSIG_MAX);
114 CONF_CASE_RETURN(_SC_SAVED_IDS, SC_DISABLE);
115
116 #ifdef LOSCFG_BASE_IPC_SEM
117 CONF_CASE_RETURN(_SC_SEMAPHORES, SC_ENABLE);
118 CONF_CASE_RETURN(_SC_SEM_NSEMS_MAX, SEM_NSEMS_MAX);
119 CONF_CASE_RETURN(_SC_SEM_VALUE_MAX, SEM_VALUE_MAX);
120 #endif
121
122 CONF_CASE_RETURN(_SC_SHARED_MEMORY_OBJECTS, SC_DISABLE);
123 CONF_CASE_RETURN(_SC_SIGQUEUE_MAX, SIGQUEUE_MAX);
124 CONF_CASE_RETURN(_SC_STREAM_MAX, STREAM_MAX);
125 CONF_CASE_RETURN(_SC_SYNCHRONIZED_IO, SC_DISABLE);
126 CONF_CASE_RETURN(_SC_THREADS, SC_ENABLE);
127 CONF_CASE_RETURN(_SC_THREAD_ATTR_STACKADDR, SC_ENABLE);
128 CONF_CASE_RETURN(_SC_THREAD_ATTR_STACKSIZE, PTHREAD_ATTR_STACKSIZE);
129 CONF_CASE_RETURN(_SC_THREAD_DESTRUCTOR_ITERATIONS, PTHREAD_DESTRUCTOR_ITERATIONS);
130 CONF_CASE_RETURN(_SC_THREAD_KEYS_MAX, PTHREAD_KEYS_MAX);
131 CONF_CASE_RETURN(_SC_THREAD_PRIO_INHERIT, PTHREAD_PRIO_INHERIT);
132 CONF_CASE_RETURN(_SC_THREAD_PRIO_PROTECT, PTHREAD_PRIO_PROTECT);
133 CONF_CASE_RETURN(_SC_THREAD_PRIORITY_SCHEDULING, PTHREAD_PRIORITY_SCHEDULING);
134 CONF_CASE_RETURN(_SC_THREAD_PROCESS_SHARED, PTHREAD_PROCESS_SHARED);
135 CONF_CASE_RETURN(_SC_THREAD_SAFE_FUNCTIONS, SC_DISABLE);
136 CONF_CASE_RETURN(_SC_THREAD_STACK_MIN, PTHREAD_STACK_MIN);
137 CONF_CASE_RETURN(_SC_THREAD_THREADS_MAX, PTHREAD_THREADS_MAX);
138 CONF_CASE_RETURN(_SC_TIMERS, TIMERS);
139 CONF_CASE_RETURN(_SC_TIMER_MAX, TIMER_MAX);
140 CONF_CASE_RETURN(_SC_TTY_NAME_MAX, TTY_NAME_MAX);
141 CONF_CASE_RETURN(_SC_TZNAME_MAX, TZNAME_MAX);
142 CONF_CASE_RETURN(_SC_VERSION, POSIX_VERSION);
143
144 default:
145 set_errno(EINVAL);
146 return -1;
147 }
148 }
149
getpid(void)150 pid_t getpid(void)
151 {
152 return ((LosTaskCB *)(OsCurrTaskGet()))->taskID;
153 }
154
getrlimit(int resource,struct rlimit * rlim)155 int getrlimit(int resource, struct rlimit *rlim)
156 {
157 unsigned int intSave;
158 LosProcessCB *pcb = OsCurrProcessGet();
159 struct rlimit *resourceLimit = pcb->resourceLimit;
160
161 switch (resource) {
162 case RLIMIT_NOFILE:
163 case RLIMIT_FSIZE:
164 break;
165 default:
166 return -EINVAL;
167 }
168
169 if (resourceLimit == NULL) {
170 rlim->rlim_cur = 0;
171 rlim->rlim_max = 0;
172
173 return 0;
174 }
175
176 SCHEDULER_LOCK(intSave);
177 rlim->rlim_cur = resourceLimit[resource].rlim_cur;
178 rlim->rlim_max = resourceLimit[resource].rlim_max;
179 SCHEDULER_UNLOCK(intSave);
180
181 return 0;
182 }
183
184 #define FSIZE_RLIMIT 0XFFFFFFFF
185 #ifndef NR_OPEN_DEFAULT
186 #define NR_OPEN_DEFAULT 1024
187 #endif
setrlimit(int resource,const struct rlimit * rlim)188 int setrlimit(int resource, const struct rlimit *rlim)
189 {
190 unsigned int intSave;
191 struct rlimit *resourceLimit = NULL;
192 LosProcessCB *pcb = OsCurrProcessGet();
193
194 if (rlim->rlim_cur > rlim->rlim_max) {
195 return -EINVAL;
196 }
197 switch (resource) {
198 case RLIMIT_NOFILE:
199 if (rlim->rlim_max > NR_OPEN_DEFAULT) {
200 return -EPERM;
201 }
202 break;
203 case RLIMIT_FSIZE:
204 if (rlim->rlim_max > FSIZE_RLIMIT) {
205 return -EPERM;
206 }
207 break;
208 default:
209 return -EINVAL;
210 }
211
212 if (pcb->resourceLimit == NULL) {
213 resourceLimit = LOS_MemAlloc((VOID *)m_aucSysMem0, RLIM_NLIMITS * sizeof(struct rlimit));
214 if (resourceLimit == NULL) {
215 return -EINVAL;
216 }
217 }
218
219 SCHEDULER_LOCK(intSave);
220 if (pcb->resourceLimit == NULL) {
221 pcb->resourceLimit = resourceLimit;
222 resourceLimit = NULL;
223 }
224 pcb->resourceLimit[resource].rlim_cur = rlim->rlim_cur;
225 pcb->resourceLimit[resource].rlim_max = rlim->rlim_max;
226 SCHEDULER_UNLOCK(intSave);
227
228 (VOID)LOS_MemFree((VOID *)m_aucSysMem0, resourceLimit);
229 return 0;
230 }
231