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