kern_resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /* $OpenBSD: kern_resource.c,v 1.54 2015/02/09 09:39:09 miod Exp $ */
  2. /* $NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $ */
  3. /*-
  4. * Copyright (c) 1982, 1986, 1991, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * (c) UNIX System Laboratories, Inc.
  7. * All or some portions of this file are derived from material licensed
  8. * to the University of California by American Telephone and Telegraph
  9. * Co. or Unix System Laboratories, Inc. and are reproduced herein with
  10. * the permission of UNIX System Laboratories, Inc.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. *
  36. * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94
  37. */
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/kernel.h>
  41. #include <sys/file.h>
  42. #include <sys/resourcevar.h>
  43. #include <sys/pool.h>
  44. #include <sys/proc.h>
  45. #include <sys/ktrace.h>
  46. #include <sys/sched.h>
  47. #include <sys/mount.h>
  48. #include <sys/syscallargs.h>
  49. #include <uvm/uvm_extern.h>
  50. void tuagg_sub(struct tusage *, struct proc *);
  51. /*
  52. * Patchable maximum data and stack limits.
  53. */
  54. rlim_t maxdmap = MAXDSIZ;
  55. rlim_t maxsmap = MAXSSIZ;
  56. /*
  57. * Resource controls and accounting.
  58. */
  59. int
  60. sys_getpriority(struct proc *curp, void *v, register_t *retval)
  61. {
  62. struct sys_getpriority_args /* {
  63. syscallarg(int) which;
  64. syscallarg(id_t) who;
  65. } */ *uap = v;
  66. struct process *pr;
  67. int low = NZERO + PRIO_MAX + 1;
  68. switch (SCARG(uap, which)) {
  69. case PRIO_PROCESS:
  70. if (SCARG(uap, who) == 0)
  71. pr = curp->p_p;
  72. else
  73. pr = prfind(SCARG(uap, who));
  74. if (pr == NULL)
  75. break;
  76. if (pr->ps_nice < low)
  77. low = pr->ps_nice;
  78. break;
  79. case PRIO_PGRP: {
  80. struct pgrp *pg;
  81. if (SCARG(uap, who) == 0)
  82. pg = curp->p_p->ps_pgrp;
  83. else if ((pg = pgfind(SCARG(uap, who))) == NULL)
  84. break;
  85. LIST_FOREACH(pr, &pg->pg_members, ps_pglist)
  86. if (pr->ps_nice < low)
  87. low = pr->ps_nice;
  88. break;
  89. }
  90. case PRIO_USER:
  91. if (SCARG(uap, who) == 0)
  92. SCARG(uap, who) = curp->p_ucred->cr_uid;
  93. LIST_FOREACH(pr, &allprocess, ps_list)
  94. if (pr->ps_ucred->cr_uid == SCARG(uap, who) &&
  95. pr->ps_nice < low)
  96. low = pr->ps_nice;
  97. break;
  98. default:
  99. return (EINVAL);
  100. }
  101. if (low == NZERO + PRIO_MAX + 1)
  102. return (ESRCH);
  103. *retval = low - NZERO;
  104. return (0);
  105. }
  106. /* ARGSUSED */
  107. int
  108. sys_setpriority(struct proc *curp, void *v, register_t *retval)
  109. {
  110. struct sys_setpriority_args /* {
  111. syscallarg(int) which;
  112. syscallarg(id_t) who;
  113. syscallarg(int) prio;
  114. } */ *uap = v;
  115. struct process *pr;
  116. int found = 0, error = 0;
  117. switch (SCARG(uap, which)) {
  118. case PRIO_PROCESS:
  119. if (SCARG(uap, who) == 0)
  120. pr = curp->p_p;
  121. else
  122. pr = prfind(SCARG(uap, who));
  123. if (pr == NULL)
  124. break;
  125. error = donice(curp, pr, SCARG(uap, prio));
  126. found++;
  127. break;
  128. case PRIO_PGRP: {
  129. struct pgrp *pg;
  130. if (SCARG(uap, who) == 0)
  131. pg = curp->p_p->ps_pgrp;
  132. else if ((pg = pgfind(SCARG(uap, who))) == NULL)
  133. break;
  134. LIST_FOREACH(pr, &pg->pg_members, ps_pglist) {
  135. error = donice(curp, pr, SCARG(uap, prio));
  136. found++;
  137. }
  138. break;
  139. }
  140. case PRIO_USER:
  141. if (SCARG(uap, who) == 0)
  142. SCARG(uap, who) = curp->p_ucred->cr_uid;
  143. LIST_FOREACH(pr, &allprocess, ps_list)
  144. if (pr->ps_ucred->cr_uid == SCARG(uap, who)) {
  145. error = donice(curp, pr, SCARG(uap, prio));
  146. found++;
  147. }
  148. break;
  149. default:
  150. return (EINVAL);
  151. }
  152. if (found == 0)
  153. return (ESRCH);
  154. return (error);
  155. }
  156. int
  157. donice(struct proc *curp, struct process *chgpr, int n)
  158. {
  159. struct ucred *ucred = curp->p_ucred;
  160. struct proc *p;
  161. int s;
  162. if (ucred->cr_uid != 0 && ucred->cr_ruid != 0 &&
  163. ucred->cr_uid != chgpr->ps_ucred->cr_uid &&
  164. ucred->cr_ruid != chgpr->ps_ucred->cr_uid)
  165. return (EPERM);
  166. if (n > PRIO_MAX)
  167. n = PRIO_MAX;
  168. if (n < PRIO_MIN)
  169. n = PRIO_MIN;
  170. n += NZERO;
  171. if (n < chgpr->ps_nice && suser(curp, 0))
  172. return (EACCES);
  173. chgpr->ps_nice = n;
  174. SCHED_LOCK(s);
  175. TAILQ_FOREACH(p, &chgpr->ps_threads, p_thr_link)
  176. (void)resetpriority(p);
  177. SCHED_UNLOCK(s);
  178. return (0);
  179. }
  180. /* ARGSUSED */
  181. int
  182. sys_setrlimit(struct proc *p, void *v, register_t *retval)
  183. {
  184. struct sys_setrlimit_args /* {
  185. syscallarg(int) which;
  186. syscallarg(const struct rlimit *) rlp;
  187. } */ *uap = v;
  188. struct rlimit alim;
  189. int error;
  190. error = copyin((caddr_t)SCARG(uap, rlp), (caddr_t)&alim,
  191. sizeof (struct rlimit));
  192. if (error)
  193. return (error);
  194. #ifdef KTRACE
  195. if (KTRPOINT(p, KTR_STRUCT))
  196. ktrrlimit(p, &alim);
  197. #endif
  198. return (dosetrlimit(p, SCARG(uap, which), &alim));
  199. }
  200. int
  201. dosetrlimit(struct proc *p, u_int which, struct rlimit *limp)
  202. {
  203. struct rlimit *alimp;
  204. rlim_t maxlim;
  205. int error;
  206. if (which >= RLIM_NLIMITS || limp->rlim_cur > limp->rlim_max)
  207. return (EINVAL);
  208. alimp = &p->p_rlimit[which];
  209. if (limp->rlim_max > alimp->rlim_max)
  210. if ((error = suser(p, 0)) != 0)
  211. return (error);
  212. if (p->p_p->ps_limit->p_refcnt > 1) {
  213. struct plimit *l = p->p_p->ps_limit;
  214. /* limcopy() can sleep, so copy before decrementing refcnt */
  215. p->p_p->ps_limit = limcopy(l);
  216. limfree(l);
  217. alimp = &p->p_rlimit[which];
  218. }
  219. switch (which) {
  220. case RLIMIT_DATA:
  221. maxlim = maxdmap;
  222. break;
  223. case RLIMIT_STACK:
  224. maxlim = maxsmap;
  225. break;
  226. case RLIMIT_NOFILE:
  227. maxlim = maxfiles;
  228. break;
  229. case RLIMIT_NPROC:
  230. maxlim = maxprocess;
  231. break;
  232. default:
  233. maxlim = RLIM_INFINITY;
  234. break;
  235. }
  236. if (limp->rlim_max > maxlim)
  237. limp->rlim_max = maxlim;
  238. if (limp->rlim_cur > limp->rlim_max)
  239. limp->rlim_cur = limp->rlim_max;
  240. if (which == RLIMIT_STACK) {
  241. /*
  242. * Stack is allocated to the max at exec time with only
  243. * "rlim_cur" bytes accessible. If stack limit is going
  244. * up make more accessible, if going down make inaccessible.
  245. */
  246. if (limp->rlim_cur != alimp->rlim_cur) {
  247. vaddr_t addr;
  248. vsize_t size;
  249. vm_prot_t prot;
  250. struct vmspace *vm = p->p_vmspace;
  251. if (limp->rlim_cur > alimp->rlim_cur) {
  252. prot = PROT_READ | PROT_WRITE;
  253. size = limp->rlim_cur - alimp->rlim_cur;
  254. #ifdef MACHINE_STACK_GROWS_UP
  255. addr = (vaddr_t)vm->vm_maxsaddr +
  256. alimp->rlim_cur;
  257. #else
  258. addr = (vaddr_t)vm->vm_minsaddr -
  259. limp->rlim_cur;
  260. #endif
  261. } else {
  262. prot = PROT_NONE;
  263. size = alimp->rlim_cur - limp->rlim_cur;
  264. #ifdef MACHINE_STACK_GROWS_UP
  265. addr = (vaddr_t)vm->vm_maxsaddr +
  266. limp->rlim_cur;
  267. #else
  268. addr = (vaddr_t)vm->vm_minsaddr -
  269. alimp->rlim_cur;
  270. #endif
  271. }
  272. addr = trunc_page(addr);
  273. size = round_page(size);
  274. (void) uvm_map_protect(&vm->vm_map,
  275. addr, addr+size, prot, FALSE);
  276. }
  277. }
  278. *alimp = *limp;
  279. return (0);
  280. }
  281. /* ARGSUSED */
  282. int
  283. sys_getrlimit(struct proc *p, void *v, register_t *retval)
  284. {
  285. struct sys_getrlimit_args /* {
  286. syscallarg(int) which;
  287. syscallarg(struct rlimit *) rlp;
  288. } */ *uap = v;
  289. struct rlimit *alimp;
  290. int error;
  291. if (SCARG(uap, which) < 0 || SCARG(uap, which) >= RLIM_NLIMITS)
  292. return (EINVAL);
  293. alimp = &p->p_rlimit[SCARG(uap, which)];
  294. error = copyout(alimp, SCARG(uap, rlp), sizeof(struct rlimit));
  295. #ifdef KTRACE
  296. if (error == 0 && KTRPOINT(p, KTR_STRUCT))
  297. ktrrlimit(p, alimp);
  298. #endif
  299. return (error);
  300. }
  301. void
  302. tuagg_sub(struct tusage *tup, struct proc *p)
  303. {
  304. timespecadd(&tup->tu_runtime, &p->p_rtime, &tup->tu_runtime);
  305. tup->tu_uticks += p->p_uticks;
  306. tup->tu_sticks += p->p_sticks;
  307. tup->tu_iticks += p->p_iticks;
  308. }
  309. /*
  310. * Aggregate a single thread's immediate time counts into the running
  311. * totals for the thread and process
  312. */
  313. void
  314. tuagg_unlocked(struct process *pr, struct proc *p)
  315. {
  316. tuagg_sub(&pr->ps_tu, p);
  317. tuagg_sub(&p->p_tu, p);
  318. timespecclear(&p->p_rtime);
  319. p->p_uticks = 0;
  320. p->p_sticks = 0;
  321. p->p_iticks = 0;
  322. }
  323. void
  324. tuagg(struct process *pr, struct proc *p)
  325. {
  326. int s;
  327. SCHED_LOCK(s);
  328. tuagg_unlocked(pr, p);
  329. SCHED_UNLOCK(s);
  330. }
  331. /*
  332. * Transform the running time and tick information in a struct tusage
  333. * into user, system, and interrupt time usage.
  334. */
  335. void
  336. calctsru(struct tusage *tup, struct timespec *up, struct timespec *sp,
  337. struct timespec *ip)
  338. {
  339. u_quad_t st, ut, it;
  340. int freq;
  341. st = tup->tu_sticks;
  342. ut = tup->tu_uticks;
  343. it = tup->tu_iticks;
  344. if (st + ut + it == 0) {
  345. timespecclear(up);
  346. timespecclear(sp);
  347. if (ip != NULL)
  348. timespecclear(ip);
  349. return;
  350. }
  351. freq = stathz ? stathz : hz;
  352. st = st * 1000000000 / freq;
  353. sp->tv_sec = st / 1000000000;
  354. sp->tv_nsec = st % 1000000000;
  355. ut = ut * 1000000000 / freq;
  356. up->tv_sec = ut / 1000000000;
  357. up->tv_nsec = ut % 1000000000;
  358. if (ip != NULL) {
  359. it = it * 1000000000 / freq;
  360. ip->tv_sec = it / 1000000000;
  361. ip->tv_nsec = it % 1000000000;
  362. }
  363. }
  364. void
  365. calcru(struct tusage *tup, struct timeval *up, struct timeval *sp,
  366. struct timeval *ip)
  367. {
  368. struct timespec u, s, i;
  369. calctsru(tup, &u, &s, ip != NULL ? &i : NULL);
  370. TIMESPEC_TO_TIMEVAL(up, &u);
  371. TIMESPEC_TO_TIMEVAL(sp, &s);
  372. if (ip != NULL)
  373. TIMESPEC_TO_TIMEVAL(ip, &i);
  374. }
  375. /* ARGSUSED */
  376. int
  377. sys_getrusage(struct proc *p, void *v, register_t *retval)
  378. {
  379. struct sys_getrusage_args /* {
  380. syscallarg(int) who;
  381. syscallarg(struct rusage *) rusage;
  382. } */ *uap = v;
  383. struct rusage ru;
  384. int error;
  385. error = dogetrusage(p, SCARG(uap, who), &ru);
  386. if (error == 0) {
  387. error = copyout(&ru, SCARG(uap, rusage), sizeof(ru));
  388. #ifdef KTRACE
  389. if (error == 0 && KTRPOINT(p, KTR_STRUCT))
  390. ktrrusage(p, &ru);
  391. #endif
  392. }
  393. return (error);
  394. }
  395. int
  396. dogetrusage(struct proc *p, int who, struct rusage *rup)
  397. {
  398. struct process *pr = p->p_p;
  399. struct proc *q;
  400. switch (who) {
  401. case RUSAGE_SELF:
  402. /* start with the sum of dead threads, if any */
  403. if (pr->ps_ru != NULL)
  404. *rup = *pr->ps_ru;
  405. else
  406. memset(rup, 0, sizeof(*rup));
  407. /* add on all living threads */
  408. TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
  409. ruadd(rup, &q->p_ru);
  410. tuagg(pr, q);
  411. }
  412. calcru(&pr->ps_tu, &rup->ru_utime, &rup->ru_stime, NULL);
  413. break;
  414. case RUSAGE_THREAD:
  415. *rup = p->p_ru;
  416. calcru(&p->p_tu, &rup->ru_utime, &rup->ru_stime, NULL);
  417. break;
  418. case RUSAGE_CHILDREN:
  419. *rup = pr->ps_cru;
  420. break;
  421. default:
  422. return (EINVAL);
  423. }
  424. return (0);
  425. }
  426. void
  427. ruadd(struct rusage *ru, struct rusage *ru2)
  428. {
  429. long *ip, *ip2;
  430. int i;
  431. timeradd(&ru->ru_utime, &ru2->ru_utime, &ru->ru_utime);
  432. timeradd(&ru->ru_stime, &ru2->ru_stime, &ru->ru_stime);
  433. if (ru->ru_maxrss < ru2->ru_maxrss)
  434. ru->ru_maxrss = ru2->ru_maxrss;
  435. ip = &ru->ru_first; ip2 = &ru2->ru_first;
  436. for (i = &ru->ru_last - &ru->ru_first; i >= 0; i--)
  437. *ip++ += *ip2++;
  438. }
  439. struct pool plimit_pool;
  440. /*
  441. * Make a copy of the plimit structure.
  442. * We share these structures copy-on-write after fork,
  443. * and copy when a limit is changed.
  444. */
  445. struct plimit *
  446. limcopy(struct plimit *lim)
  447. {
  448. struct plimit *newlim;
  449. static int initialized;
  450. if (!initialized) {
  451. pool_init(&plimit_pool, sizeof(struct plimit), 0, 0, PR_WAITOK,
  452. "plimitpl", NULL);
  453. initialized = 1;
  454. }
  455. newlim = pool_get(&plimit_pool, PR_WAITOK);
  456. memcpy(newlim->pl_rlimit, lim->pl_rlimit,
  457. sizeof(struct rlimit) * RLIM_NLIMITS);
  458. newlim->p_refcnt = 1;
  459. return (newlim);
  460. }
  461. void
  462. limfree(struct plimit *lim)
  463. {
  464. if (--lim->p_refcnt > 0)
  465. return;
  466. pool_put(&plimit_pool, lim);
  467. }