diff --git a/compat/posix/src/misc.c b/compat/posix/src/misc.c index 39337571cc96dd021f5f966cba0f790d5745f05c..6f2685b58658b6a2ac982b0db367c3b612277e05 100644 --- a/compat/posix/src/misc.c +++ b/compat/posix/src/misc.c @@ -30,12 +30,14 @@ */ #include "sys/types.h" +#include "sys/resource.h" #include "unistd.h" #include "stdio.h" #include "pthread.h" #include "sys/utsname.h" #include "mqueue.h" #include "semaphore.h" +#include "los_process_pri.h" #include "los_hw.h" /* @@ -150,3 +152,47 @@ pid_t getpid(void) return ((LosTaskCB *)(OsCurrTaskGet()))->taskID; } +int getrlimit(int resource, struct rlimit *rlim) +{ + LosProcessCB *pcb = OsCurrProcessGet(); + + switch (resource) { + case RLIMIT_NOFILE: + case RLIMIT_FSIZE: + break; + default: + return -EINVAL; + } + rlim->rlim_cur = pcb->pl_rlimit[resource].rlim_cur; + rlim->rlim_max = pcb->pl_rlimit[resource].rlim_max; + + return 0; +} + +#define FSIZE_RLIMIT 0XFFFFFFFF +int setrlimit(int resource, const struct rlimit *rlim) +{ + LosProcessCB *pcb = OsCurrProcessGet(); + + if (rlim->rlim_cur > rlim->rlim_max) { + return -EINVAL; + } + switch (resource) { + case RLIMIT_NOFILE: + if (rlim->rlim_max > NR_OPEN_DEFAULT) { + return -EPERM; + } + break; + case RLIMIT_FSIZE: + if (rlim->rlim_max > FSIZE_RLIMIT) { + return -EPERM; + } + break; + default: + return -EINVAL; + } + pcb->pl_rlimit[resource].rlim_cur = rlim->rlim_cur; + pcb->pl_rlimit[resource].rlim_max = rlim->rlim_max; + + return 0; +} \ No newline at end of file diff --git a/kernel/base/include/los_process_pri.h b/kernel/base/include/los_process_pri.h index 27e80cf46679ae20fe659f75f90b7ef68e47d332..d21d7115070401da95246cd432772f57046699e9 100644 --- a/kernel/base/include/los_process_pri.h +++ b/kernel/base/include/los_process_pri.h @@ -45,6 +45,7 @@ #ifdef LOSCFG_SECURITY_VID #include "vid_type.h" #endif +#include "sys/resource.h" #ifdef __cplusplus #if __cplusplus @@ -126,6 +127,7 @@ typedef struct ProcessCB { #ifdef LOSCFG_KERNEL_CPUP OsCpupBase processCpup; /**< Process cpu usage */ #endif + struct rlimit pl_rlimit[RLIM_NLIMITS]; } LosProcessCB; #define CLONE_VM 0x00000100 diff --git a/security/cap/capability_type.h b/security/cap/capability_type.h index b5163325740ca2f7980464fb361d626049280457..dc239e343d6ca4d7ea13ad805c985c87ffb9ce6f 100644 --- a/security/cap/capability_type.h +++ b/security/cap/capability_type.h @@ -66,4 +66,4 @@ #define CAP_REBOOT 18 // self deined privileged syscalls #define CAP_SHELL_EXEC 19 -#endif \ No newline at end of file +#endif diff --git a/syscall/los_syscall.h b/syscall/los_syscall.h index 67df685ea02002c6f35cde3775baa2fb11c3c580..20de4aeccca17b6b089672f775ccd2dcba8c6a3f 100644 --- a/syscall/los_syscall.h +++ b/syscall/los_syscall.h @@ -296,5 +296,8 @@ extern int SysUmask(int mask); extern int SysShellExec(const char *msgName, const char *cmdString); extern int SysReboot(int magic, int magic2, int type); extern int SysGetrusage(int what, struct rusage *ru); +extern long SysSysconf(int name); +extern int SysUgetrlimit(int resource, unsigned long long k_rlim[2]); +extern int SysSetrlimit(int resource, unsigned long long k_rlim[2]); #endif #endif /* _LOS_SYSCALL_H */ diff --git a/syscall/misc_syscall.c b/syscall/misc_syscall.c index 936d48cc82c2e63c9fce8f203c63cea8fed5d907..d92649ff92eb49206b39b096ec43844a2eec6601 100644 --- a/syscall/misc_syscall.c +++ b/syscall/misc_syscall.c @@ -44,6 +44,10 @@ #include "shmsg.h" #endif #include "user_copy.h" +#include "los_strncpy_from_user.h" +#include "capability_type.h" +#include "capability_api.h" +#include "unistd.h" int SysUname(struct utsname *name) @@ -192,3 +196,50 @@ int SysGetrusage(int what, struct rusage *ru) return 0; } +long SysSysconf(int name) +{ + long ret; + + ret = sysconf(name); + if (ret == -1) { + return -get_errno(); + } + + return ret; +} + +int SysUgetrlimit(int resource, unsigned long long k_rlim[2]) +{ + int ret; + struct rlimit lim; + + ret = getrlimit(resource, &lim); + if (ret < 0) { + return ret; + } + + ret = LOS_ArchCopyToUser(k_rlim, &lim, sizeof(struct rlimit)); + if (ret != 0) { + return -EFAULT; + } + + return ret; +} + +int SysSetrlimit(int resource, unsigned long long k_rlim[2]) +{ + int ret; + struct rlimit lim; + + if(!IsCapPermit(CAP_CAPSET)) { + return -EPERM; + } + + ret = LOS_ArchCopyFromUser(&lim, k_rlim, sizeof(struct rlimit)); + if (ret != 0) { + return -EFAULT; + } + ret = setrlimit(resource, &lim); + + return ret; +} diff --git a/syscall/syscall_lookup.h b/syscall/syscall_lookup.h index 4b714a32440491d0b9e2f978a2891b8e9f654775..1439e48568fc2bd08b78b6ed3aeabf84a7f10431 100644 --- a/syscall/syscall_lookup.h +++ b/syscall/syscall_lookup.h @@ -253,3 +253,6 @@ SYSCALL_HAND_DEF(__NR_pthread_join, SysThreadJoin, int, ARG_NUM_1) SYSCALL_HAND_DEF(__NR_pthread_deatch, SysUserThreadDetach, int, ARG_NUM_1) SYSCALL_HAND_DEF(__NR_creat_user_thread, SysCreateUserThread, unsigned int, ARG_NUM_3) SYSCALL_HAND_DEF(__NR_getrusage, SysGetrusage, int, ARG_NUM_2) +SYSCALL_HAND_DEF(__NR_sysconf, SysSysconf, long, ARG_NUM_1) +SYSCALL_HAND_DEF(__NR_ugetrlimit, SysUgetrlimit, int, ARG_NUM_2) +SYSCALL_HAND_DEF(__NR_setrlimit, SysSetrlimit, int, ARG_NUM_2) \ No newline at end of file diff --git a/testsuites/unittest/misc/BUILD.gn b/testsuites/unittest/misc/BUILD.gn index ca9be5e18c4e181b73942e27111438fe98cadab0..96b1af962f363aa961706149e68c1d900e1d8382 100644 --- a/testsuites/unittest/misc/BUILD.gn +++ b/testsuites/unittest/misc/BUILD.gn @@ -44,6 +44,8 @@ sources_entry = [ sources_full = [ "full/misc_test_006.cpp", "full/misc_test_007.cpp", + "full/misc_test_008.cpp", + "full/misc_test_009.cpp", "full/misc_test_010.cpp", "full/misc_test_011.cpp", "full/misc_test_012.cpp", @@ -56,8 +58,6 @@ sources_smoke = [ "smoke/misc_test_003.cpp", "smoke/misc_test_004.cpp", "smoke/misc_test_005.cpp", - "smoke/misc_test_008.cpp", - "smoke/misc_test_009.cpp", "smoke/misc_test_014.cpp", ] @@ -86,4 +86,4 @@ if (LOSCFG_USER_TEST_LEVEL >= TEST_LEVEL_MIDDLE) { configs = [ "..:public_config_for_all" ] deps = [ "//third_party/bounds_checking_function:libsec_shared" ] } -} \ No newline at end of file +} diff --git a/testsuites/unittest/misc/smoke/misc_test_008.cpp b/testsuites/unittest/misc/full/misc_test_008.cpp similarity index 100% rename from testsuites/unittest/misc/smoke/misc_test_008.cpp rename to testsuites/unittest/misc/full/misc_test_008.cpp diff --git a/testsuites/unittest/misc/smoke/misc_test_009.cpp b/testsuites/unittest/misc/full/misc_test_009.cpp similarity index 100% rename from testsuites/unittest/misc/smoke/misc_test_009.cpp rename to testsuites/unittest/misc/full/misc_test_009.cpp diff --git a/testsuites/unittest/misc/misc_test.cpp b/testsuites/unittest/misc/misc_test.cpp index 2bcc66e19000e53b42ee2d0d24544bb0e0e53471..bd49e9e1ab8b3cc57886e42449326ea930d7736b 100644 --- a/testsuites/unittest/misc/misc_test.cpp +++ b/testsuites/unittest/misc/misc_test.cpp @@ -128,10 +128,32 @@ HWTEST_F(MiscTest, ItTestMisc006, TestSize.Level0) * @tc.type: FUNC * @tc.require: AR000EEMQ9 */ -/*HWTEST_F(MiscTest, ItTestMisc007, TestSize.Level0) +HWTEST_F(MiscTest, ItTestMisc007, TestSize.Level0) { ItTestMisc007(); -}*/ +} + +/* * + * @tc.name: IT_TEST_MISC_008 + * @tc.desc: function for MiscTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(MiscTest, ItTestMisc008, TestSize.Level0) +{ + ItTestMisc008(); +} + +/* * + * @tc.name: IT_TEST_MISC_009 + * @tc.desc: function for MiscTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(MiscTest, ItTestMisc009, TestSize.Level0) +{ + ItTestMisc009(); +} /* * * @tc.name: IT_TEST_MISC_010 @@ -155,6 +177,17 @@ HWTEST_F(MiscTest, ItTestMisc006, TestSize.Level0) ItTestMisc011(); }*/ +/* * + * @tc.name: IT_TEST_MISC_012 + * @tc.desc: function for MiscTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(MiscTest, ItTestMisc012, TestSize.Level0) +{ + ItTestMisc012(); +} + /* * * @tc.name: IT_TEST_MISC_013 * @tc.desc: function for MiscTest