xref: /freebsd/sys/fs/procfs/procfs_rlimit.c (revision 29363fb4)
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 
60d37ed5a0SPoul-Henning Kamp int
procfs_doprocrlimit(PFS_FILL_ARGS)613a669c52SDag-Erling Smørgrav procfs_doprocrlimit(PFS_FILL_ARGS)
62d37ed5a0SPoul-Henning Kamp {
6391d5354aSJohn Baldwin 	struct plimit *limp;
64d37ed5a0SPoul-Henning Kamp 	int i;
65d37ed5a0SPoul-Henning Kamp 
6691d5354aSJohn Baldwin 	/*
6791d5354aSJohn Baldwin 	 * Obtain a private reference to resource limits
6891d5354aSJohn Baldwin 	 */
6991d5354aSJohn Baldwin 
7091d5354aSJohn Baldwin 	PROC_LOCK(p);
7191d5354aSJohn Baldwin 	limp = lim_hold(p->p_limit);
7291d5354aSJohn Baldwin 	PROC_UNLOCK(p);
7391d5354aSJohn Baldwin 
74d37ed5a0SPoul-Henning Kamp 	for (i = 0; i < RLIM_NLIMITS; i++) {
75d37ed5a0SPoul-Henning Kamp 		/*
76d37ed5a0SPoul-Henning Kamp 		 * Add the rlimit ident
77d37ed5a0SPoul-Henning Kamp 		 */
78d37ed5a0SPoul-Henning Kamp 
793a669c52SDag-Erling Smørgrav 		sbuf_printf(sb, "%s ", rlimit_ident[i]);
80d37ed5a0SPoul-Henning Kamp 
81d37ed5a0SPoul-Henning Kamp 		/*
82d37ed5a0SPoul-Henning Kamp 		 * Replace RLIM_INFINITY with -1 in the string
83d37ed5a0SPoul-Henning Kamp 		 */
84d37ed5a0SPoul-Henning Kamp 
85d37ed5a0SPoul-Henning Kamp 		/*
86d37ed5a0SPoul-Henning Kamp 		 * current limit
87d37ed5a0SPoul-Henning Kamp 		 */
88d37ed5a0SPoul-Henning Kamp 
8991d5354aSJohn Baldwin 		if (limp->pl_rlimit[i].rlim_cur == RLIM_INFINITY) {
903a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "-1 ");
91d37ed5a0SPoul-Henning Kamp 		} else {
923a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%llu ",
9391d5354aSJohn Baldwin 			    (unsigned long long)limp->pl_rlimit[i].rlim_cur);
94d37ed5a0SPoul-Henning Kamp 		}
95d37ed5a0SPoul-Henning Kamp 
96d37ed5a0SPoul-Henning Kamp 		/*
97d37ed5a0SPoul-Henning Kamp 		 * maximum limit
98d37ed5a0SPoul-Henning Kamp 		 */
99d37ed5a0SPoul-Henning Kamp 
10091d5354aSJohn Baldwin 		if (limp->pl_rlimit[i].rlim_max == RLIM_INFINITY) {
1013a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "-1\n");
102d37ed5a0SPoul-Henning Kamp 		} else {
1033a669c52SDag-Erling Smørgrav 			sbuf_printf(sb, "%llu\n",
10491d5354aSJohn Baldwin 			    (unsigned long long)limp->pl_rlimit[i].rlim_max);
105d37ed5a0SPoul-Henning Kamp 		}
106d37ed5a0SPoul-Henning Kamp 	}
107d37ed5a0SPoul-Henning Kamp 
10891d5354aSJohn Baldwin 	lim_free(limp);
1093a669c52SDag-Erling Smørgrav 	return (0);
110d37ed5a0SPoul-Henning Kamp }
111