10d163575Sopenharmony_ci/* 20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. 30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. 40d163575Sopenharmony_ci * 50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 60d163575Sopenharmony_ci * are permitted provided that the following conditions are met: 70d163575Sopenharmony_ci * 80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 90d163575Sopenharmony_ci * conditions and the following disclaimer. 100d163575Sopenharmony_ci * 110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 120d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 130d163575Sopenharmony_ci * provided with the distribution. 140d163575Sopenharmony_ci * 150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 160d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 170d163575Sopenharmony_ci * permission. 180d163575Sopenharmony_ci * 190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 300d163575Sopenharmony_ci */ 310d163575Sopenharmony_ci 320d163575Sopenharmony_ci#include "sys/types.h" 330d163575Sopenharmony_ci#include "sys/resource.h" 340d163575Sopenharmony_ci#include "unistd.h" 350d163575Sopenharmony_ci#include "stdio.h" 360d163575Sopenharmony_ci#include "pthread.h" 370d163575Sopenharmony_ci#include "sys/utsname.h" 380d163575Sopenharmony_ci#include "mqueue.h" 390d163575Sopenharmony_ci#include "semaphore.h" 400d163575Sopenharmony_ci#include "los_process_pri.h" 410d163575Sopenharmony_ci#include "los_hw.h" 420d163575Sopenharmony_ci 430d163575Sopenharmony_ci/* 440d163575Sopenharmony_ci * Supply some suitable values for constants that may not be present 450d163575Sopenharmony_ci * in all configurations. 460d163575Sopenharmony_ci */ 470d163575Sopenharmony_ci#define SC_ENABLE 1 480d163575Sopenharmony_ci#define SC_DISABLE (-1) 490d163575Sopenharmony_ci 500d163575Sopenharmony_ci#define CONF_CASE_RETURN(name, val) \ 510d163575Sopenharmony_ci case (name): \ 520d163575Sopenharmony_ci return (val) 530d163575Sopenharmony_ci 540d163575Sopenharmony_ciint uname(struct utsname *name) 550d163575Sopenharmony_ci{ 560d163575Sopenharmony_ci if (name == NULL) { 570d163575Sopenharmony_ci return -EFAULT; 580d163575Sopenharmony_ci } 590d163575Sopenharmony_ci 600d163575Sopenharmony_ci#ifdef LOSCFG_UTS_CONTAINER 610d163575Sopenharmony_ci struct utsname *currentUtsName = OsGetCurrUtsName(); 620d163575Sopenharmony_ci if (currentUtsName == NULL) { 630d163575Sopenharmony_ci return -EFAULT; 640d163575Sopenharmony_ci } 650d163575Sopenharmony_ci (VOID)memcpy_s(name, sizeof(struct utsname), currentUtsName, sizeof(struct utsname)); 660d163575Sopenharmony_ci#else 670d163575Sopenharmony_ci 680d163575Sopenharmony_ci (VOID)strcpy_s(name->sysname, sizeof(name->sysname), KERNEL_NAME); 690d163575Sopenharmony_ci (VOID)strcpy_s(name->nodename, sizeof(name->nodename), KERNEL_NODE_NAME); 700d163575Sopenharmony_ci INT32 ret = sprintf_s(name->version, sizeof(name->version), "%s %u.%u.%u.%u %s %s", 710d163575Sopenharmony_ci KERNEL_NAME, KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE, __DATE__, __TIME__); 720d163575Sopenharmony_ci if (ret < 0) { 730d163575Sopenharmony_ci return -EIO; 740d163575Sopenharmony_ci } 750d163575Sopenharmony_ci 760d163575Sopenharmony_ci const char *cpuInfo = LOS_CpuInfo(); 770d163575Sopenharmony_ci (VOID)strcpy_s(name->machine, sizeof(name->machine), cpuInfo); 780d163575Sopenharmony_ci ret = sprintf_s(name->release, sizeof(name->release), "%u.%u.%u.%u", 790d163575Sopenharmony_ci KERNEL_MAJOR, KERNEL_MINOR, KERNEL_PATCH, KERNEL_ITRE); 800d163575Sopenharmony_ci if (ret < 0) { 810d163575Sopenharmony_ci return -EIO; 820d163575Sopenharmony_ci } 830d163575Sopenharmony_ci 840d163575Sopenharmony_ci name->domainname[0] = '\0'; 850d163575Sopenharmony_ci#endif 860d163575Sopenharmony_ci return 0; 870d163575Sopenharmony_ci} 880d163575Sopenharmony_ci 890d163575Sopenharmony_cilong sysconf(int name) 900d163575Sopenharmony_ci{ 910d163575Sopenharmony_ci switch (name) { 920d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_AIO_LISTIO_MAX, SC_DISABLE); 930d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_AIO_MAX, SC_DISABLE); 940d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_AIO_PRIO_DELTA_MAX, SC_DISABLE); 950d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_ARG_MAX, ARG_MAX); 960d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_ASYNCHRONOUS_IO, SC_DISABLE); 970d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_CHILD_MAX, CHILD_MAX); 980d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_CLK_TCK, SYS_CLK_TCK); 990d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_DELAYTIMER_MAX, DELAYTIMER_MAX); 1000d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_FSYNC, SC_DISABLE); 1010d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_GETGR_R_SIZE_MAX, GETGR_R_SIZE_MAX); 1020d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_GETPW_R_SIZE_MAX, GETPW_R_SIZE_MAX); 1030d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_JOB_CONTROL, SC_DISABLE); 1040d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_LOGIN_NAME_MAX, LOGIN_NAME_MAX); 1050d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MAPPED_FILES, SC_DISABLE); 1060d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MEMLOCK, SC_DISABLE); 1070d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MEMLOCK_RANGE, SC_DISABLE); 1080d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MEMORY_PROTECTION, SC_DISABLE); 1090d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MESSAGE_PASSING, SC_DISABLE); 1100d163575Sopenharmony_ci#ifdef LOSCFG_BASE_IPC_QUEUE 1110d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MQ_OPEN_MAX, MQ_OPEN_MAX); 1120d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_MQ_PRIO_MAX, MQ_PRIO_MAX); 1130d163575Sopenharmony_ci#endif 1140d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_NGROUPS_MAX, NGROUPS_MAX); 1150d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_OPEN_MAX, OPEN_MAX); 1160d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_PAGESIZE, 0x1000); 1170d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_PRIORITIZED_IO, SC_DISABLE); 1180d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_PRIORITY_SCHEDULING, SC_DISABLE); 1190d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_REALTIME_SIGNALS, SC_DISABLE); 1200d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_RTSIG_MAX, RTSIG_MAX); 1210d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SAVED_IDS, SC_DISABLE); 1220d163575Sopenharmony_ci 1230d163575Sopenharmony_ci#ifdef LOSCFG_BASE_IPC_SEM 1240d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SEMAPHORES, SC_ENABLE); 1250d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SEM_NSEMS_MAX, SEM_NSEMS_MAX); 1260d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SEM_VALUE_MAX, SEM_VALUE_MAX); 1270d163575Sopenharmony_ci#endif 1280d163575Sopenharmony_ci 1290d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SHARED_MEMORY_OBJECTS, SC_DISABLE); 1300d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SIGQUEUE_MAX, SIGQUEUE_MAX); 1310d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_STREAM_MAX, STREAM_MAX); 1320d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_SYNCHRONIZED_IO, SC_DISABLE); 1330d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREADS, SC_ENABLE); 1340d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_ATTR_STACKADDR, SC_ENABLE); 1350d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_ATTR_STACKSIZE, PTHREAD_ATTR_STACKSIZE); 1360d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_DESTRUCTOR_ITERATIONS, PTHREAD_DESTRUCTOR_ITERATIONS); 1370d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_KEYS_MAX, PTHREAD_KEYS_MAX); 1380d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_PRIO_INHERIT, PTHREAD_PRIO_INHERIT); 1390d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_PRIO_PROTECT, PTHREAD_PRIO_PROTECT); 1400d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_PRIORITY_SCHEDULING, PTHREAD_PRIORITY_SCHEDULING); 1410d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_PROCESS_SHARED, PTHREAD_PROCESS_SHARED); 1420d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_SAFE_FUNCTIONS, SC_DISABLE); 1430d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_STACK_MIN, PTHREAD_STACK_MIN); 1440d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_THREAD_THREADS_MAX, PTHREAD_THREADS_MAX); 1450d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_TIMERS, TIMERS); 1460d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_TIMER_MAX, TIMER_MAX); 1470d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_TTY_NAME_MAX, TTY_NAME_MAX); 1480d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_TZNAME_MAX, TZNAME_MAX); 1490d163575Sopenharmony_ci CONF_CASE_RETURN(_SC_VERSION, POSIX_VERSION); 1500d163575Sopenharmony_ci 1510d163575Sopenharmony_ci default: 1520d163575Sopenharmony_ci set_errno(EINVAL); 1530d163575Sopenharmony_ci return -1; 1540d163575Sopenharmony_ci } 1550d163575Sopenharmony_ci} 1560d163575Sopenharmony_ci 1570d163575Sopenharmony_cipid_t getpid(void) 1580d163575Sopenharmony_ci{ 1590d163575Sopenharmony_ci return ((LosTaskCB *)(OsCurrTaskGet()))->taskID; 1600d163575Sopenharmony_ci} 1610d163575Sopenharmony_ci 1620d163575Sopenharmony_ciint getrlimit(int resource, struct rlimit *rlim) 1630d163575Sopenharmony_ci{ 1640d163575Sopenharmony_ci unsigned int intSave; 1650d163575Sopenharmony_ci LosProcessCB *pcb = OsCurrProcessGet(); 1660d163575Sopenharmony_ci struct rlimit *resourceLimit = pcb->resourceLimit; 1670d163575Sopenharmony_ci 1680d163575Sopenharmony_ci switch (resource) { 1690d163575Sopenharmony_ci case RLIMIT_NOFILE: 1700d163575Sopenharmony_ci case RLIMIT_FSIZE: 1710d163575Sopenharmony_ci break; 1720d163575Sopenharmony_ci default: 1730d163575Sopenharmony_ci return -EINVAL; 1740d163575Sopenharmony_ci } 1750d163575Sopenharmony_ci 1760d163575Sopenharmony_ci if (resourceLimit == NULL) { 1770d163575Sopenharmony_ci rlim->rlim_cur = 0; 1780d163575Sopenharmony_ci rlim->rlim_max = 0; 1790d163575Sopenharmony_ci 1800d163575Sopenharmony_ci return 0; 1810d163575Sopenharmony_ci } 1820d163575Sopenharmony_ci 1830d163575Sopenharmony_ci SCHEDULER_LOCK(intSave); 1840d163575Sopenharmony_ci rlim->rlim_cur = resourceLimit[resource].rlim_cur; 1850d163575Sopenharmony_ci rlim->rlim_max = resourceLimit[resource].rlim_max; 1860d163575Sopenharmony_ci SCHEDULER_UNLOCK(intSave); 1870d163575Sopenharmony_ci 1880d163575Sopenharmony_ci return 0; 1890d163575Sopenharmony_ci} 1900d163575Sopenharmony_ci 1910d163575Sopenharmony_ci#define FSIZE_RLIMIT 0XFFFFFFFF 1920d163575Sopenharmony_ci#ifndef NR_OPEN_DEFAULT 1930d163575Sopenharmony_ci#define NR_OPEN_DEFAULT 1024 1940d163575Sopenharmony_ci#endif 1950d163575Sopenharmony_ciint setrlimit(int resource, const struct rlimit *rlim) 1960d163575Sopenharmony_ci{ 1970d163575Sopenharmony_ci unsigned int intSave; 1980d163575Sopenharmony_ci struct rlimit *resourceLimit = NULL; 1990d163575Sopenharmony_ci LosProcessCB *pcb = OsCurrProcessGet(); 2000d163575Sopenharmony_ci 2010d163575Sopenharmony_ci if (rlim->rlim_cur > rlim->rlim_max) { 2020d163575Sopenharmony_ci return -EINVAL; 2030d163575Sopenharmony_ci } 2040d163575Sopenharmony_ci switch (resource) { 2050d163575Sopenharmony_ci case RLIMIT_NOFILE: 2060d163575Sopenharmony_ci if (rlim->rlim_max > NR_OPEN_DEFAULT) { 2070d163575Sopenharmony_ci return -EPERM; 2080d163575Sopenharmony_ci } 2090d163575Sopenharmony_ci break; 2100d163575Sopenharmony_ci case RLIMIT_FSIZE: 2110d163575Sopenharmony_ci if (rlim->rlim_max > FSIZE_RLIMIT) { 2120d163575Sopenharmony_ci return -EPERM; 2130d163575Sopenharmony_ci } 2140d163575Sopenharmony_ci break; 2150d163575Sopenharmony_ci default: 2160d163575Sopenharmony_ci return -EINVAL; 2170d163575Sopenharmony_ci } 2180d163575Sopenharmony_ci 2190d163575Sopenharmony_ci if (pcb->resourceLimit == NULL) { 2200d163575Sopenharmony_ci resourceLimit = LOS_MemAlloc((VOID *)m_aucSysMem0, RLIM_NLIMITS * sizeof(struct rlimit)); 2210d163575Sopenharmony_ci if (resourceLimit == NULL) { 2220d163575Sopenharmony_ci return -EINVAL; 2230d163575Sopenharmony_ci } 2240d163575Sopenharmony_ci } 2250d163575Sopenharmony_ci 2260d163575Sopenharmony_ci SCHEDULER_LOCK(intSave); 2270d163575Sopenharmony_ci if (pcb->resourceLimit == NULL) { 2280d163575Sopenharmony_ci pcb->resourceLimit = resourceLimit; 2290d163575Sopenharmony_ci resourceLimit = NULL; 2300d163575Sopenharmony_ci } 2310d163575Sopenharmony_ci pcb->resourceLimit[resource].rlim_cur = rlim->rlim_cur; 2320d163575Sopenharmony_ci pcb->resourceLimit[resource].rlim_max = rlim->rlim_max; 2330d163575Sopenharmony_ci SCHEDULER_UNLOCK(intSave); 2340d163575Sopenharmony_ci 2350d163575Sopenharmony_ci (VOID)LOS_MemFree((VOID *)m_aucSysMem0, resourceLimit); 2360d163575Sopenharmony_ci return 0; 2370d163575Sopenharmony_ci} 238