From nobody Fri Sep 27 15:03:27 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4XFYcv3v31z5Y5rD; Fri, 27 Sep 2024 15:03:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XFYcv2PSVz58y2; Fri, 27 Sep 2024 15:03:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727449407; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7TewG2kAFuzA6KZH+5oM2Q6EzSwr4NKlprxZDTQkfQQ=; b=XbzR6R5KaQ+gw68jjwZj/eAvNL/k0u8GnBvuhTYY6h7pLO8lfqaJuwEeCIQ4q07vzELouc r8a3fL50NlYlLQ4cU4VZkx8hscmkEwnNLzdnbp12LMJ7TTtagOH1lwjJsGX+GJuj6OinZy L3Ym+QHXoOpj8x5LuetqR9kvDVszdBCW02Y/McXy4poLg2aqcA8QWtzKapMbflDUmzaGGn ZB+Lx82qIkjg981H/Di/4UTU2J9Otrh991JYtZo9i6UdvY6Xbaz4cmW3NoXpP+elMmrZbf ft6kOvUed0PEq8VgxmZMtSTpVGCCrTtZ1kilALmel+NULnsFQbUyLtA7l9aCPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1727449407; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=7TewG2kAFuzA6KZH+5oM2Q6EzSwr4NKlprxZDTQkfQQ=; b=v58AOv4x02gqRjg8MvT5qWhIfjU5SAuv1hKaYweqfIXt+FXb+vmko0WB7eKRzCQ2bc5oJj Ul5bDpPevVWsWe+w1Qx8wlyN9BEoPEPV1XcR47WRsFCbF26bDI21sZyqH4/JI50Mi20+mt P25ZO65LRXcq852lVh+ceSqG+IuIpafoioXnitdUfdVcR85bKNKo/Lx4430d0NlASwYLZj PfyiTqIIO6ZSxhPFSYj9VXRnHn9KKkTMveX5FhY8okQ+TiP5nvOzwXOYVhjaqGbg8GMgNx /E+iwyK1NxR6heKHLERwO7DVt3mOcScKoOy6SPGJvX/vzZqtSxXtTwuRQqYFtQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1727449407; a=rsa-sha256; cv=none; b=WYmeBgBYQx7RACVWerpS5sX8NI6EWkiV4tUo+xL6zU+91ST6pRhUrHcZQDVsUorM/yZXcB no1Cbp4kyWD/5bnKuzZaxtAQNKtyV4Xmc4/RhNTH3wBtQ2fKKkBRynRDBAjotdjjHNFZdA EDx26t2wdU5lU+oK57wKf/WnKSAVOblBfqnTmvETMB0TBEfS9jIKrJfdkae1eTm2V+WOKs gXpOFvRZMxtmwzr1mx78XKMxlbONPEx53lEQfKIJFfzkpmh6JL3SbTxZtf9FFq7aLqbeN4 b65qu5jCIxKMZg8qkKv4yYclmRZCoV8qmNAB8pme5CuC8VK25+rVkBxeW6+gJA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4XFYcv1hXFzl68; Fri, 27 Sep 2024 15:03:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 48RF3Rh1009134; Fri, 27 Sep 2024 15:03:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 48RF3R1l009131; Fri, 27 Sep 2024 15:03:27 GMT (envelope-from git) Date: Fri, 27 Sep 2024 15:03:27 GMT Message-Id: <202409271503.48RF3R1l009131@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: f028f44ef3cb - main - Add getrlimitusage(2) List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: f028f44ef3cb10aafadf8b9f29954ed58bd88c70 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=f028f44ef3cb10aafadf8b9f29954ed58bd88c70 commit f028f44ef3cb10aafadf8b9f29954ed58bd88c70 Author: Konstantin Belousov AuthorDate: 2024-09-20 15:18:38 +0000 Commit: Konstantin Belousov 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 #include #include +#include #include #include #include @@ -60,6 +61,7 @@ #include #include #include +#include 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