git: f028f44ef3cb - main - Add getrlimitusage(2)
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 27 Sep 2024 15:03:27 UTC
The branch main has been updated by kib:
URL: https://cgit.FreeBSD.org/src/commit/?id=f028f44ef3cb10aafadf8b9f29954ed58bd88c70
commit f028f44ef3cb10aafadf8b9f29954ed58bd88c70
Author: Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-09-20 15:18:38 +0000
Commit: Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-09-27 15:02:04 +0000
Add getrlimitusage(2)
Reviewed by: markj, olce
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D46747
---
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 58723289af21..db5cce833012 100644
--- a/sys/kern/kern_resource.c
+++ b/sys/kern/kern_resource.c
@@ -38,6 +38,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>
@@ -60,6 +61,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");
@@ -804,6 +806,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 fac1c2e1e96f..849e5ee2f13b 100644
--- a/sys/kern/syscalls.master
+++ b/sys/kern/syscalls.master
@@ -3333,5 +3333,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 81346028f1ed..d2803cccf380 100644
--- a/sys/sys/resource.h
+++ b/sys/sys/resource.h
@@ -171,6 +171,9 @@ struct loadavg {
#define CP_IDLE 4
#define CPUSTATES 5
+/* getrlimitusage flags */
+#define GETRLIMITUSAGE_EUID 0x0001
+
#endif /* __BSD_VISIBLE */
#ifdef _KERNEL