xref: /freebsd/sys/fs/procfs/procfs_rlimit.c (revision c84d8db0ab3d02f65422e8dcdc1f911cc15a04ec)
1d167cf6fSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4d37ed5a0SPoul-Henning Kamp  * Copyright (c) 1999 Adrian Chadd
5d37ed5a0SPoul-Henning Kamp  * Copyright (c) 1993
6d37ed5a0SPoul-Henning Kamp  *	The Regents of the University of California.  All rights reserved.
7d37ed5a0SPoul-Henning Kamp  *
8d37ed5a0SPoul-Henning Kamp  * This code is derived from software contributed to Berkeley by
9d37ed5a0SPoul-Henning Kamp  * Jan-Simon Pendry.
10d37ed5a0SPoul-Henning Kamp  *
11d37ed5a0SPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
12d37ed5a0SPoul-Henning Kamp  * modification, are permitted provided that the following conditions
13d37ed5a0SPoul-Henning Kamp  * are met:
14d37ed5a0SPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
15d37ed5a0SPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
16d37ed5a0SPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
17d37ed5a0SPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
18d37ed5a0SPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
19d37ed5a0SPoul-Henning Kamp  * 3. All advertising materials mentioning features or use of this software
20d37ed5a0SPoul-Henning Kamp  *    must display the following acknowledgement:
21d37ed5a0SPoul-Henning Kamp  *	This product includes software developed by the University of
22d37ed5a0SPoul-Henning Kamp  *	California, Berkeley and its contributors.
23d37ed5a0SPoul-Henning Kamp  * 4. Neither the name of the University nor the names of its contributors
24d37ed5a0SPoul-Henning Kamp  *    may be used to endorse or promote products derived from this software
25d37ed5a0SPoul-Henning Kamp  *    without specific prior written permission.
26d37ed5a0SPoul-Henning Kamp  *
27d37ed5a0SPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28d37ed5a0SPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29d37ed5a0SPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30d37ed5a0SPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31d37ed5a0SPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32d37ed5a0SPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33d37ed5a0SPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34d37ed5a0SPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35d37ed5a0SPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36d37ed5a0SPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37d37ed5a0SPoul-Henning Kamp  * SUCH DAMAGE.
38d37ed5a0SPoul-Henning Kamp  */
39d37ed5a0SPoul-Henning Kamp 
40d37ed5a0SPoul-Henning Kamp /*
41d37ed5a0SPoul-Henning Kamp  * To get resource.h to include our rlimit_ident[] array of rlimit identifiers
42d37ed5a0SPoul-Henning Kamp  */
43d37ed5a0SPoul-Henning Kamp 
44d37ed5a0SPoul-Henning Kamp #define _RLIMIT_IDENT
45d37ed5a0SPoul-Henning Kamp 
46d37ed5a0SPoul-Henning Kamp #include <sys/param.h>
4791d5354aSJohn Baldwin #include <sys/lock.h>
4891d5354aSJohn Baldwin #include <sys/mutex.h>
49d37ed5a0SPoul-Henning Kamp #include <sys/systm.h>
50d37ed5a0SPoul-Henning Kamp #include <sys/proc.h>
51d37ed5a0SPoul-Henning Kamp #include <sys/resourcevar.h>
52d37ed5a0SPoul-Henning Kamp #include <sys/resource.h>
533a669c52SDag-Erling Smørgrav #include <sys/sbuf.h>
5491d5354aSJohn Baldwin #include <sys/types.h>
5591d5354aSJohn Baldwin #include <sys/malloc.h>
563a669c52SDag-Erling Smørgrav 
573a669c52SDag-Erling Smørgrav #include <fs/pseudofs/pseudofs.h>
5899d300a1SRuslan Ermilov #include <fs/procfs/procfs.h>
59d37ed5a0SPoul-Henning Kamp 
60*c84d8db0SKonstantin Belousov _Static_assert(nitems(rlimit_ident) == RLIM_NLIMITS,
61*c84d8db0SKonstantin Belousov     "resource.h RLIMIT_IDENT needs update");
62*c84d8db0SKonstantin Belousov 
63d37ed5a0SPoul-Henning Kamp int
procfs_doprocrlimit(PFS_FILL_ARGS)643a669c52SDag-Erling Smørgrav procfs_doprocrlimit(PFS_FILL_ARGS)
65d37ed5a0SPoul-Henning Kamp {
6691d5354aSJohn Baldwin 	struct plimit *limp;
67d37ed5a0SPoul-Henning Kamp 	int i;
68d37ed5a0SPoul-Henning Kamp 
6991d5354aSJohn Baldwin 	/*
7091d5354aSJohn Baldwin 	 * Obtain a private reference to resource limits
7191d5354aSJohn Baldwin 	 */
7291d5354aSJohn Baldwin 
7391d5354aSJohn Baldwin 	PROC_LOCK(p);
7491d5354aSJohn Baldwin 	limp = lim_hold(p->p_limit);
7591d5354aSJohn Baldwin 	PROC_UNLOCK(p);
7691d5354aSJohn Baldwin 
77d37ed5a0SPoul-Henning Kamp 	for (i = 0; i < RLIM_NLIMITS; i++) {
78d37ed5a0SPoul-Henning Kamp 		/*
79d37ed5a0SPoul-Henning Kamp 		 * Add the rlimit ident
80d37ed5a0SPoul-Henning Kamp 		 */
81d37ed5a0SPoul-Henning Kamp 
823a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%s ", rlimit_ident[i]);
83d37ed5a0SPoul-Henning Kamp 
84d37ed5a0SPoul-Henning Kamp 		/*
85d37ed5a0SPoul-Henning Kamp 		 * Replace RLIM_INFINITY with -1 in the string
86d37ed5a0SPoul-Henning Kamp 		 */
87d37ed5a0SPoul-Henning Kamp 
88d37ed5a0SPoul-Henning Kamp 		/*
89d37ed5a0SPoul-Henning Kamp 		 * current limit
90d37ed5a0SPoul-Henning Kamp 		 */
91d37ed5a0SPoul-Henning Kamp 
9291d5354aSJohn Baldwin 		if (limp->pl_rlimit[i].rlim_cur == RLIM_INFINITY) {
933a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "-1 ");
94d37ed5a0SPoul-Henning Kamp 		} else {
953a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%llu ",
9691d5354aSJohn Baldwin 			    (unsigned long long)limp->pl_rlimit[i].rlim_cur);
97d37ed5a0SPoul-Henning Kamp 		}
98d37ed5a0SPoul-Henning Kamp 
99d37ed5a0SPoul-Henning Kamp 		/*
100d37ed5a0SPoul-Henning Kamp 		 * maximum limit
101d37ed5a0SPoul-Henning Kamp 		 */
102d37ed5a0SPoul-Henning Kamp 
10391d5354aSJohn Baldwin 		if (limp->pl_rlimit[i].rlim_max == RLIM_INFINITY) {
1043a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "-1\n");
105d37ed5a0SPoul-Henning Kamp 		} else {
1063a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%llu\n",
10791d5354aSJohn Baldwin 			    (unsigned long long)limp->pl_rlimit[i].rlim_max);
108d37ed5a0SPoul-Henning Kamp 		}
109d37ed5a0SPoul-Henning Kamp 	}
110d37ed5a0SPoul-Henning Kamp 
11191d5354aSJohn Baldwin 	lim_free(limp);
1123a669c52SDag-Erling Smørgrav 	return (0);
113d37ed5a0SPoul-Henning Kamp }
114