Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
0880b18e | 2 | * Copyright (c) 1982, 1986 Regents of the University of California. |
da7c5cc6 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
0880b18e | 6 | * @(#)kern_resource.c 7.1 (Berkeley) %G% |
da7c5cc6 | 7 | */ |
1b64633a | 8 | |
94368568 JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "dir.h" | |
12 | #include "user.h" | |
13 | #include "inode.h" | |
14 | #include "proc.h" | |
15 | #include "seg.h" | |
16 | #include "fs.h" | |
17 | #include "uio.h" | |
18 | #include "vm.h" | |
19 | #include "kernel.h" | |
db1874da | 20 | |
93cc02ac BJ |
21 | /* |
22 | * Resource controls and accounting. | |
23 | */ | |
24 | ||
db1874da BJ |
25 | getpriority() |
26 | { | |
27 | register struct a { | |
28 | int which; | |
29 | int who; | |
30 | } *uap = (struct a *)u.u_ap; | |
31 | register struct proc *p; | |
3fa5efae | 32 | int low = PRIO_MAX + 1; |
db1874da | 33 | |
db1874da BJ |
34 | switch (uap->which) { |
35 | ||
36 | case PRIO_PROCESS: | |
37 | if (uap->who == 0) | |
38 | p = u.u_procp; | |
39 | else | |
40 | p = pfind(uap->who); | |
41 | if (p == 0) | |
957edfef | 42 | break; |
3fa5efae | 43 | low = p->p_nice; |
db1874da BJ |
44 | break; |
45 | ||
46 | case PRIO_PGRP: | |
47 | if (uap->who == 0) | |
48 | uap->who = u.u_procp->p_pgrp; | |
1d348849 | 49 | for (p = allproc; p != NULL; p = p->p_nxt) { |
db1874da | 50 | if (p->p_pgrp == uap->who && |
3fa5efae MK |
51 | p->p_nice < low) |
52 | low = p->p_nice; | |
93cc02ac BJ |
53 | } |
54 | break; | |
55 | ||
56 | case PRIO_USER: | |
57 | if (uap->who == 0) | |
58 | uap->who = u.u_uid; | |
1d348849 | 59 | for (p = allproc; p != NULL; p = p->p_nxt) { |
93cc02ac | 60 | if (p->p_uid == uap->who && |
3fa5efae MK |
61 | p->p_nice < low) |
62 | low = p->p_nice; | |
93cc02ac | 63 | } |
db1874da BJ |
64 | break; |
65 | ||
66 | default: | |
67 | u.u_error = EINVAL; | |
3fa5efae MK |
68 | return; |
69 | } | |
70 | if (low == PRIO_MAX + 1) { | |
71 | u.u_error = ESRCH; | |
72 | return; | |
db1874da | 73 | } |
3fa5efae | 74 | u.u_r.r_val1 = low; |
db1874da BJ |
75 | } |
76 | ||
77 | setpriority() | |
78 | { | |
79 | register struct a { | |
80 | int which; | |
81 | int who; | |
82 | int prio; | |
83 | } *uap = (struct a *)u.u_ap; | |
84 | register struct proc *p; | |
3fa5efae | 85 | int found = 0; |
db1874da | 86 | |
db1874da BJ |
87 | switch (uap->which) { |
88 | ||
89 | case PRIO_PROCESS: | |
93cc02ac BJ |
90 | if (uap->who == 0) |
91 | p = u.u_procp; | |
92 | else | |
93 | p = pfind(uap->who); | |
db1874da | 94 | if (p == 0) |
957edfef | 95 | break; |
db1874da | 96 | donice(p, uap->prio); |
3fa5efae | 97 | found++; |
db1874da BJ |
98 | break; |
99 | ||
100 | case PRIO_PGRP: | |
93cc02ac BJ |
101 | if (uap->who == 0) |
102 | uap->who = u.u_procp->p_pgrp; | |
1d348849 | 103 | for (p = allproc; p != NULL; p = p->p_nxt) |
3fa5efae | 104 | if (p->p_pgrp == uap->who) { |
db1874da | 105 | donice(p, uap->prio); |
3fa5efae MK |
106 | found++; |
107 | } | |
db1874da BJ |
108 | break; |
109 | ||
93cc02ac BJ |
110 | case PRIO_USER: |
111 | if (uap->who == 0) | |
112 | uap->who = u.u_uid; | |
1d348849 | 113 | for (p = allproc; p != NULL; p = p->p_nxt) |
3fa5efae | 114 | if (p->p_uid == uap->who) { |
93cc02ac | 115 | donice(p, uap->prio); |
3fa5efae MK |
116 | found++; |
117 | } | |
93cc02ac BJ |
118 | break; |
119 | ||
db1874da BJ |
120 | default: |
121 | u.u_error = EINVAL; | |
3fa5efae | 122 | return; |
db1874da | 123 | } |
3fa5efae MK |
124 | if (found == 0) |
125 | u.u_error = ESRCH; | |
db1874da BJ |
126 | } |
127 | ||
128 | donice(p, n) | |
129 | register struct proc *p; | |
130 | register int n; | |
131 | { | |
132 | ||
133 | if (u.u_uid && u.u_ruid && | |
134 | u.u_uid != p->p_uid && u.u_ruid != p->p_uid) { | |
6e6b3864 | 135 | u.u_error = EPERM; |
db1874da BJ |
136 | return; |
137 | } | |
3fa5efae MK |
138 | if (n > PRIO_MAX) |
139 | n = PRIO_MAX; | |
140 | if (n < PRIO_MIN) | |
141 | n = PRIO_MIN; | |
93cc02ac BJ |
142 | if (n < p->p_nice && !suser()) { |
143 | u.u_error = EACCES; | |
db1874da | 144 | return; |
93cc02ac | 145 | } |
db1874da BJ |
146 | p->p_nice = n; |
147 | (void) setpri(p); | |
db1874da BJ |
148 | } |
149 | ||
1edb1cf8 | 150 | setrlimit() |
db1874da BJ |
151 | { |
152 | register struct a { | |
153 | u_int which; | |
154 | struct rlimit *lim; | |
155 | } *uap = (struct a *)u.u_ap; | |
156 | struct rlimit alim; | |
157 | register struct rlimit *alimp; | |
63ce5236 | 158 | extern unsigned maxdmap; |
db1874da BJ |
159 | |
160 | if (uap->which >= RLIM_NLIMITS) { | |
161 | u.u_error = EINVAL; | |
162 | return; | |
163 | } | |
164 | alimp = &u.u_rlimit[uap->which]; | |
127f7d76 SL |
165 | u.u_error = copyin((caddr_t)uap->lim, (caddr_t)&alim, |
166 | sizeof (struct rlimit)); | |
167 | if (u.u_error) | |
db1874da | 168 | return; |
db1874da BJ |
169 | if (alim.rlim_cur > alimp->rlim_max || alim.rlim_max > alimp->rlim_max) |
170 | if (!suser()) | |
171 | return; | |
172 | switch (uap->which) { | |
173 | ||
174 | case RLIMIT_DATA: | |
d9937da6 KM |
175 | if (alim.rlim_cur > maxdmap) |
176 | alim.rlim_cur = maxdmap; | |
177 | if (alim.rlim_max > maxdmap) | |
178 | alim.rlim_max = maxdmap; | |
db1874da BJ |
179 | break; |
180 | ||
181 | case RLIMIT_STACK: | |
d9937da6 KM |
182 | if (alim.rlim_cur > maxdmap) |
183 | alim.rlim_cur = maxdmap; | |
184 | if (alim.rlim_max > maxdmap) | |
185 | alim.rlim_max = maxdmap; | |
db1874da BJ |
186 | break; |
187 | } | |
188 | *alimp = alim; | |
189 | if (uap->which == RLIMIT_RSS) | |
190 | u.u_procp->p_maxrss = alim.rlim_cur/NBPG; | |
191 | } | |
192 | ||
1edb1cf8 | 193 | getrlimit() |
db1874da BJ |
194 | { |
195 | register struct a { | |
196 | u_int which; | |
197 | struct rlimit *rlp; | |
198 | } *uap = (struct a *)u.u_ap; | |
199 | ||
200 | if (uap->which >= RLIM_NLIMITS) { | |
201 | u.u_error = EINVAL; | |
202 | return; | |
203 | } | |
127f7d76 SL |
204 | u.u_error = copyout((caddr_t)&u.u_rlimit[uap->which], (caddr_t)uap->rlp, |
205 | sizeof (struct rlimit)); | |
db1874da BJ |
206 | } |
207 | ||
208 | getrusage() | |
209 | { | |
210 | register struct a { | |
211 | int who; | |
212 | struct rusage *rusage; | |
213 | } *uap = (struct a *)u.u_ap; | |
214 | register struct rusage *rup; | |
215 | ||
216 | switch (uap->who) { | |
217 | ||
218 | case RUSAGE_SELF: | |
219 | rup = &u.u_ru; | |
220 | break; | |
221 | ||
222 | case RUSAGE_CHILDREN: | |
223 | rup = &u.u_cru; | |
224 | break; | |
225 | ||
226 | default: | |
227 | u.u_error = EINVAL; | |
228 | return; | |
229 | } | |
127f7d76 SL |
230 | u.u_error = copyout((caddr_t)rup, (caddr_t)uap->rusage, |
231 | sizeof (struct rusage)); | |
db1874da BJ |
232 | } |
233 | ||
234 | ruadd(ru, ru2) | |
235 | register struct rusage *ru, *ru2; | |
236 | { | |
3fd23f5c | 237 | register long *ip, *ip2; |
db1874da BJ |
238 | register int i; |
239 | ||
240 | timevaladd(&ru->ru_utime, &ru2->ru_utime); | |
241 | timevaladd(&ru->ru_stime, &ru2->ru_stime); | |
242 | if (ru->ru_maxrss < ru2->ru_maxrss) | |
243 | ru->ru_maxrss = ru2->ru_maxrss; | |
244 | ip = &ru->ru_first; ip2 = &ru2->ru_first; | |
245 | for (i = &ru->ru_last - &ru->ru_first; i > 0; i--) | |
246 | *ip++ += *ip2++; | |
247 | } |