1*995cf491Schristos /* $NetBSD: procfs_limit.c,v 1.5 2024/05/12 17:22:29 christos Exp $ */
2dc2e740eSchristos
3dc2e740eSchristos /*-
4dc2e740eSchristos * Copyright (c) 2019 The NetBSD Foundation, Inc.
5dc2e740eSchristos * All rights reserved.
6dc2e740eSchristos *
7dc2e740eSchristos * This code is derived from software contributed to The NetBSD Foundation
8dc2e740eSchristos * by Christos Zoulas.
9dc2e740eSchristos *
10dc2e740eSchristos * Redistribution and use in source and binary forms, with or without
11dc2e740eSchristos * modification, are permitted provided that the following conditions
12dc2e740eSchristos * are met:
13dc2e740eSchristos * 1. Redistributions of source code must retain the above copyright
14dc2e740eSchristos * notice, this list of conditions and the following disclaimer.
15dc2e740eSchristos * 2. Redistributions in binary form must reproduce the above copyright
16dc2e740eSchristos * notice, this list of conditions and the following disclaimer in the
17dc2e740eSchristos * documentation and/or other materials provided with the distribution.
18dc2e740eSchristos *
19dc2e740eSchristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20dc2e740eSchristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21dc2e740eSchristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22dc2e740eSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23dc2e740eSchristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24dc2e740eSchristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25dc2e740eSchristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26dc2e740eSchristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27dc2e740eSchristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28dc2e740eSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29dc2e740eSchristos * POSSIBILITY OF SUCH DAMAGE.
30dc2e740eSchristos */
31dc2e740eSchristos
32dc2e740eSchristos #include <sys/cdefs.h>
33*995cf491Schristos __KERNEL_RCSID(0, "$NetBSD: procfs_limit.c,v 1.5 2024/05/12 17:22:29 christos Exp $");
34*995cf491Schristos
35*995cf491Schristos #if defined(_KERNEL_OPT)
36*995cf491Schristos #include "opt_sysv.h"
37*995cf491Schristos #endif
38dc2e740eSchristos
39dc2e740eSchristos #include <sys/param.h>
40dc2e740eSchristos #include <sys/systm.h>
41dc2e740eSchristos #include <sys/proc.h>
42dc2e740eSchristos #include <sys/malloc.h>
43dc2e740eSchristos #include <sys/resource.h>
44dc2e740eSchristos #include <miscfs/procfs/procfs.h>
45*995cf491Schristos #include <compat/linux/common/linux_misc.h>
46*995cf491Schristos #ifdef SYSVMSG
47*995cf491Schristos #include <sys/msg.h>
48*995cf491Schristos #endif
49dc2e740eSchristos
50*995cf491Schristos /* Taken from FreeBSD sys/compat/linprocfs/linprocfs.c */
51*995cf491Schristos static const struct linux_rlimit_ident {
52*995cf491Schristos const char *desc;
53*995cf491Schristos const char *unit;
54*995cf491Schristos unsigned int rlim_id;
55*995cf491Schristos } linux_rlimits_ident[] = {
56*995cf491Schristos { "Max cpu time", "seconds", RLIMIT_CPU },
57*995cf491Schristos { "Max file size", "bytes", RLIMIT_FSIZE },
58*995cf491Schristos { "Max data size", "bytes", RLIMIT_DATA },
59*995cf491Schristos { "Max stack size", "bytes", RLIMIT_STACK },
60*995cf491Schristos { "Max core file size", "bytes", RLIMIT_CORE },
61*995cf491Schristos { "Max resident set", "bytes", RLIMIT_RSS },
62*995cf491Schristos { "Max processes", "processes", RLIMIT_NPROC },
63*995cf491Schristos { "Max open files", "files", RLIMIT_NOFILE },
64*995cf491Schristos { "Max locked memory", "bytes", RLIMIT_MEMLOCK },
65*995cf491Schristos { "Max address space", "bytes", RLIMIT_AS },
66*995cf491Schristos { "Max file locks", "locks", LINUX_RLIMIT_LOCKS },
67*995cf491Schristos { "Max pending signals", "signals", LINUX_RLIMIT_SIGPENDING },
68*995cf491Schristos { "Max msgqueue size", "bytes", LINUX_RLIMIT_MSGQUEUE },
69*995cf491Schristos { "Max nice priority", "", LINUX_RLIMIT_NICE },
70*995cf491Schristos { "Max realtime priority", "", LINUX_RLIMIT_RTPRIO },
71*995cf491Schristos { "Max realtime timeout", "us", LINUX_RLIMIT_RTTIME },
72*995cf491Schristos { 0, 0, 0 }
73*995cf491Schristos };
74dc2e740eSchristos
75dc2e740eSchristos static size_t
prl(char * buf,size_t len,intmax_t lim,char sep)76dc2e740eSchristos prl(char *buf, size_t len, intmax_t lim, char sep)
77dc2e740eSchristos {
78dc2e740eSchristos if (lim == RLIM_INFINITY)
79dc2e740eSchristos return snprintf(buf, len, "%#20jx%c", lim, sep);
80dc2e740eSchristos else
81dc2e740eSchristos return snprintf(buf, len, "%20jd%c", lim, sep);
82dc2e740eSchristos }
83dc2e740eSchristos
84dc2e740eSchristos int
procfs_dolimit(struct lwp * curl,struct proc * p,struct pfsnode * pfs,struct uio * uio)85dc2e740eSchristos procfs_dolimit(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
86dc2e740eSchristos struct uio *uio)
87dc2e740eSchristos {
88dc2e740eSchristos static const char *label[] = RLIM_STRINGS;
89dc2e740eSchristos int error;
90dc2e740eSchristos char *buffer;
91dc2e740eSchristos size_t bufsize, pos, i;
92dc2e740eSchristos struct rlimit rl[RLIM_NLIMITS];
93dc2e740eSchristos
94dc2e740eSchristos if (uio->uio_rw != UIO_READ)
95dc2e740eSchristos return EOPNOTSUPP;
96dc2e740eSchristos
970eaaa024Sad mutex_enter(&proc_lock);
98dc2e740eSchristos mutex_enter(p->p_lock);
99dc2e740eSchristos memcpy(rl, p->p_rlimit, sizeof(rl));
100dc2e740eSchristos mutex_exit(p->p_lock);
1010eaaa024Sad mutex_exit(&proc_lock);
102dc2e740eSchristos
103dc2e740eSchristos error = 0;
104dc2e740eSchristos
105dc2e740eSchristos bufsize = (64 * 3) * __arraycount(rl);
106dc2e740eSchristos buffer = malloc(bufsize, M_TEMP, M_WAITOK);
107dc2e740eSchristos pos = 0;
108dc2e740eSchristos for (i = 0; i < __arraycount(rl); i++) {
109dc2e740eSchristos pos += snprintf(buffer + pos, bufsize - pos, "%20.20s ",
110dc2e740eSchristos label[i]);
111dc2e740eSchristos pos += prl(buffer + pos, bufsize - pos, rl[i].rlim_cur, ' ');
112dc2e740eSchristos pos += prl(buffer + pos, bufsize - pos, rl[i].rlim_max, '\n');
113dc2e740eSchristos }
114dc2e740eSchristos
11537c269d9Schristos if ((uintmax_t)uio->uio_offset < pos)
116dc2e740eSchristos error = uiomove(buffer + uio->uio_offset,
117dc2e740eSchristos pos - uio->uio_offset, uio);
118dc2e740eSchristos else
119dc2e740eSchristos error = 0;
120dc2e740eSchristos
121dc2e740eSchristos if (buffer != NULL)
122dc2e740eSchristos free(buffer, M_TEMP);
123dc2e740eSchristos
124dc2e740eSchristos return error;
125dc2e740eSchristos }
126*995cf491Schristos
127*995cf491Schristos int
procfs_dolimits(struct lwp * curl,struct proc * p,struct pfsnode * pfs,struct uio * uio)128*995cf491Schristos procfs_dolimits(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
129*995cf491Schristos struct uio *uio)
130*995cf491Schristos {
131*995cf491Schristos const struct linux_rlimit_ident *li;
132*995cf491Schristos int error;
133*995cf491Schristos char *buffer;
134*995cf491Schristos size_t bufsize, pos;
135*995cf491Schristos struct rlimit rl, rlimits[RLIM_NLIMITS];
136*995cf491Schristos
137*995cf491Schristos if (uio->uio_rw != UIO_READ)
138*995cf491Schristos return EOPNOTSUPP;
139*995cf491Schristos
140*995cf491Schristos mutex_enter(&proc_lock);
141*995cf491Schristos mutex_enter(p->p_lock);
142*995cf491Schristos memcpy(rlimits, p->p_rlimit, sizeof(rlimits));
143*995cf491Schristos mutex_exit(p->p_lock);
144*995cf491Schristos mutex_exit(&proc_lock);
145*995cf491Schristos
146*995cf491Schristos error = 0;
147*995cf491Schristos
148*995cf491Schristos bufsize = (64 * 3) * __arraycount(linux_rlimits_ident);
149*995cf491Schristos buffer = malloc(bufsize, M_TEMP, M_WAITOK);
150*995cf491Schristos pos = snprintf(buffer, bufsize, "%-26s%-21s%-21s%-21s\n",
151*995cf491Schristos "Limit", "Soft Limit", "Hard Limit", "Units");
152*995cf491Schristos for (li = linux_rlimits_ident; li->desc != NULL; ++li) {
153*995cf491Schristos switch (li->rlim_id)
154*995cf491Schristos {
155*995cf491Schristos case LINUX_RLIMIT_LOCKS:
156*995cf491Schristos case LINUX_RLIMIT_RTTIME:
157*995cf491Schristos case LINUX_RLIMIT_SIGPENDING:
158*995cf491Schristos rl.rlim_cur = RLIM_INFINITY;
159*995cf491Schristos break;
160*995cf491Schristos case LINUX_RLIMIT_MSGQUEUE:
161*995cf491Schristos #ifdef SYSVMSG
162*995cf491Schristos rl.rlim_cur = rl.rlim_max = msginfo.msgmnb;
163*995cf491Schristos break;
164*995cf491Schristos #endif
165*995cf491Schristos case LINUX_RLIMIT_NICE:
166*995cf491Schristos case LINUX_RLIMIT_RTPRIO:
167*995cf491Schristos rl.rlim_cur = rl.rlim_max = 0;
168*995cf491Schristos break;
169*995cf491Schristos default:
170*995cf491Schristos rl = rlimits[li->rlim_id];
171*995cf491Schristos break;
172*995cf491Schristos }
173*995cf491Schristos if (rl.rlim_cur == RLIM_INFINITY)
174*995cf491Schristos pos += snprintf(buffer + pos, bufsize - pos,
175*995cf491Schristos "%-26s%-21s%-21s%-10s\n",
176*995cf491Schristos li->desc, "unlimited", "unlimited", li->unit);
177*995cf491Schristos else
178*995cf491Schristos pos += snprintf(buffer + pos, bufsize - pos,
179*995cf491Schristos "%-26s%-21llu%-21llu%-10s\n",
180*995cf491Schristos li->desc, (unsigned long long)rl.rlim_cur,
181*995cf491Schristos (unsigned long long)rl.rlim_max, li->unit);
182*995cf491Schristos }
183*995cf491Schristos
184*995cf491Schristos if ((uintmax_t)uio->uio_offset < pos)
185*995cf491Schristos error = uiomove(buffer + uio->uio_offset,
186*995cf491Schristos pos - uio->uio_offset, uio);
187*995cf491Schristos else
188*995cf491Schristos error = 0;
189*995cf491Schristos
190*995cf491Schristos if (buffer != NULL)
191*995cf491Schristos free(buffer, M_TEMP);
192*995cf491Schristos
193*995cf491Schristos return error;
194*995cf491Schristos }
195