This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 71889ff22e64904a940945525855b5ef660eb80a Author: guoshichao <[email protected]> AuthorDate: Tue Jul 22 13:44:11 2025 +0800 ulimit: add ulimit implementation implement the ulimit() based on getrlimit/setrlimit function. the ulimit() function is requiredd by vsx testset Signed-off-by: guoshichao <[email protected]> --- include/ulimit.h | 52 ++++++++++++++++++++++++++++ libs/libc/unistd/CMakeLists.txt | 3 +- libs/libc/unistd/Make.defs | 2 +- libs/libc/unistd/lib_ulimit.c | 75 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) diff --git a/include/ulimit.h b/include/ulimit.h new file mode 100644 index 00000000000..2260cd69b7a --- /dev/null +++ b/include/ulimit.h @@ -0,0 +1,52 @@ +/**************************************************************************** + * include/ulimit.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __INCLUDE_ULIMIT_H +#define __INCLUDE_ULIMIT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <sys/resource.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Get limit on the size of a file, in units of 512 bytes */ + +#define UL_GETFSIZE 1 + +/* Set limit on the size of a file */ + +#define UL_SETFSIZE 2 + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +long int ulimit(int cmd, long newlimit); + +#endif /* __INCLUDE_ULIMIT_H */ diff --git a/libs/libc/unistd/CMakeLists.txt b/libs/libc/unistd/CMakeLists.txt index 792c1ac4a74..0cef734cc7a 100644 --- a/libs/libc/unistd/CMakeLists.txt +++ b/libs/libc/unistd/CMakeLists.txt @@ -69,7 +69,8 @@ set(SRCS lib_getpass.c lib_chdir.c lib_fchdir.c - lib_confstr.c) + lib_confstr.c + lib_ulimit.c) if(NOT CONFIG_SCHED_USER_IDENTITY) list( diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs index 1ce751bd868..e5d8d248f60 100644 --- a/libs/libc/unistd/Make.defs +++ b/libs/libc/unistd/Make.defs @@ -33,7 +33,7 @@ CSRCS += lib_futimes.c lib_lutimes.c lib_gethostname.c lib_sethostname.c CSRCS += lib_fchownat.c lib_linkat.c lib_readlinkat.c lib_symlinkat.c CSRCS += lib_unlinkat.c lib_usleep.c lib_getpgrp.c lib_getpgid.c CSRCS += lib_lockf.c lib_flock.c lib_getpass.c -CSRCS += lib_chdir.c lib_fchdir.c lib_confstr.c +CSRCS += lib_chdir.c lib_fchdir.c lib_confstr.c lib_ulimit.c ifneq ($(CONFIG_SCHED_USER_IDENTITY),y) CSRCS += lib_setuid.c lib_setgid.c lib_getuid.c lib_getgid.c diff --git a/libs/libc/unistd/lib_ulimit.c b/libs/libc/unistd/lib_ulimit.c new file mode 100644 index 00000000000..776414c6b0e --- /dev/null +++ b/libs/libc/unistd/lib_ulimit.c @@ -0,0 +1,75 @@ +/**************************************************************************** + * libs/libc/unistd/lib_ulimit.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <errno.h> +#include <ulimit.h> + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ulimit + * + * Description: + * The ulimit will make calls to getrlimit() and setrlimit() to perform + * get and set resource limits respectively. + * + * Returned value: + * On success, return a non-negative value. + * On failure, return -1 and set the errno value. + * + ****************************************************************************/ + +long ulimit(int cmd, long newlimit) +{ + long ret = ERROR; + + switch (cmd) + { + case UL_GETFSIZE: + { + struct rlimit rlp; + getrlimit(RLIMIT_FSIZE, &rlp); + ret = rlp.rlim_cur / 512UL; + } + break; + case UL_SETFSIZE: + { + struct rlimit rlp; + rlp.rlim_cur = newlimit * 512UL; + ret = setrlimit(RLIMIT_FSIZE, &rlp); + } + break; + default: + set_errno(EINVAL); + break; + } + + return ret; +}
