Date: Sat, 5 Oct 2024 07:11:24 GMT From: Konstantin Belousov <kib@FreeBSD.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Subject: git: 7c41d08320a1 - stable/14 - Add getrlimitusage(2) Message-ID: <202410050711.4957BO82001429@gitrepo.freebsd.org>
next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=7c41d08320a18634a32bba727dc5f9f78086e746 commit 7c41d08320a18634a32bba727dc5f9f78086e746 Author: Konstantin Belousov <kib@FreeBSD.org> AuthorDate: 2024-09-20 15:18:38 +0000 Commit: Konstantin Belousov <kib@FreeBSD.org> CommitDate: 2024-10-05 07:08:55 +0000 Add getrlimitusage(2) (cherry picked from commit f028f44ef3cb10aafadf8b9f29954ed58bd88c70) --- sys/kern/kern_resource.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++ sys/kern/syscalls.master | 7 +++ sys/sys/resource.h | 3 ++ 3 files changed, 125 insertions(+) diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 6b9930962859..b4548b38f633 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -41,6 +41,7 @@ #include <sys/systm.h> #include <sys/sysproto.h> #include <sys/file.h> +#include <sys/filedesc.h> #include <sys/kernel.h> #include <sys/lock.h> #include <sys/malloc.h> @@ -63,6 +64,7 @@ #include <vm/vm_param.h> #include <vm/pmap.h> #include <vm/vm_map.h> +#include <vm/vm_extern.h> static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures"); static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); @@ -807,6 +809,119 @@ sys_getrlimit(struct thread *td, struct getrlimit_args *uap) return (error); } +static int +getrlimitusage_one(struct proc *p, u_int which, int flags, rlim_t *res) +{ + struct thread *td; + struct uidinfo *ui; + struct vmspace *vm; + uid_t uid; + int error; + + error = 0; + PROC_LOCK(p); + uid = (flags & GETRLIMITUSAGE_EUID) == 0 ? p->p_ucred->cr_ruid : + p->p_ucred->cr_uid; + PROC_UNLOCK(p); + + ui = uifind(uid); + vm = vmspace_acquire_ref(p); + + switch (which) { + case RLIMIT_CPU: + PROC_LOCK(p); + PROC_STATLOCK(p); + FOREACH_THREAD_IN_PROC(p, td) + ruxagg(p, td); + *res = p->p_rux.rux_runtime; + PROC_STATUNLOCK(p); + PROC_UNLOCK(p); + *res /= cpu_tickrate(); + break; + case RLIMIT_FSIZE: + error = ENXIO; + break; + case RLIMIT_DATA: + if (vm == NULL) + error = ENXIO; + else + *res = vm->vm_dsize * PAGE_SIZE; + break; + case RLIMIT_STACK: + if (vm == NULL) + error = ENXIO; + else + *res = vm->vm_ssize * PAGE_SIZE; + break; + case RLIMIT_CORE: + error = ENXIO; + break; + case RLIMIT_RSS: + if (vm == NULL) + error = ENXIO; + else + *res = vmspace_resident_count(vm) * PAGE_SIZE; + break; + case RLIMIT_MEMLOCK: + if (vm == NULL) + error = ENXIO; + else + *res = pmap_wired_count(vmspace_pmap(vm)) * PAGE_SIZE; + break; + case RLIMIT_NPROC: + *res = ui->ui_proccnt; + break; + case RLIMIT_NOFILE: + *res = proc_nfiles(p); + break; + case RLIMIT_SBSIZE: + *res = ui->ui_sbsize; + break; + case RLIMIT_VMEM: + if (vm == NULL) + error = ENXIO; + else + *res = vm->vm_map.size; + break; + case RLIMIT_NPTS: + *res = ui->ui_ptscnt; + break; + case RLIMIT_SWAP: + *res = ui->ui_vmsize; + break; + case RLIMIT_KQUEUES: + *res = ui->ui_kqcnt; + break; + case RLIMIT_UMTXP: + *res = ui->ui_umtxcnt; + break; + case RLIMIT_PIPEBUF: + *res = ui->ui_pipecnt; + break; + default: + error = EINVAL; + break; + } + + vmspace_free(vm); + uifree(ui); + return (error); +} + +int +sys_getrlimitusage(struct thread *td, struct getrlimitusage_args *uap) +{ + rlim_t res; + int error; + + if ((uap->flags & ~(GETRLIMITUSAGE_EUID)) != 0) + return (EINVAL); + error = getrlimitusage_one(curproc, uap->which, uap->flags, &res); + if (error == 0) + error = copyout(&res, uap->res, sizeof(res)); + return (error); +} + /* * Transform the running time and tick information for children of proc p * into user and system time usage. diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index 47b67e4dbaa7..825ab1a4536c 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -3347,5 +3347,12 @@ uintptr_t idx2 ); } +589 AUE_NULL STD|CAPENABLED { + int getrlimitusage( + u_int which, + int flags, + _Out_ rlim_t *res + ); + } ; vim: syntax=off diff --git a/sys/sys/resource.h b/sys/sys/resource.h index 0c6b34f7ca0f..77119a1e5302 100644 --- a/sys/sys/resource.h +++ b/sys/sys/resource.h @@ -173,6 +173,9 @@ struct loadavg { #define CP_IDLE 4 #define CPUSTATES 5 +/* getrlimitusage flags */ +#define GETRLIMITUSAGE_EUID 0x0001 + #endif /* __BSD_VISIBLE */ #ifdef _KERNEL
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202410050711.4957BO82001429>