# svn status M kern/kern_resource.c M sys/resourcevar.h M vm/vm_mmap.c Index: kern/kern_resource.c =================================================================== --- kern/kern_resource.c (revision 264283) +++ kern/kern_resource.c (working copy) @@ -39,6 +39,8 @@ __FBSDID("$FreeBSD$"); #include "opt_compat.h" +#define _RLIMIT_IDENT + #include #include #include @@ -58,6 +60,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include @@ -1228,6 +1231,67 @@ lim_rlimit(struct proc *p, int which, st p->p_sysent->sv_fixlimit(rlp, which); } +static int kern_loglimexceeded = 1; +SYSCTL_INT(_kern, OID_AUTO, loglimexceeded, CTLFLAG_RW, &kern_loglimexceeded, + 0, "Log processes exceeding setrlimit(2) limits to syslog(3)"); + +int +lim_exceeded(struct proc *p, int which, rlim_t newsize) +{ + rlim_t curlim; + const char *desc; + int limexceeded; + + PROC_LOCK_ASSERT(p, MA_OWNED); + + curlim = lim_cur(p, which); + limexceeded = newsize > curlim; + + if (kern_loglimexceeded == 0) + goto out; + + switch (which) { + case RLIMIT_CPU: + desc = "seconds"; + break; + case RLIMIT_FSIZE: + case RLIMIT_DATA: + case RLIMIT_STACK: + case RLIMIT_CORE: + case RLIMIT_RSS: + case RLIMIT_MEMLOCK: + case RLIMIT_VMEM: + case RLIMIT_SBSIZE: + case RLIMIT_SWAP: + desc = "bytes"; + break; + case RLIMIT_NPROC: + desc = "processes"; + break; + case RLIMIT_NOFILE: + desc = "files"; + break; + case RLIMIT_NPTS: + desc = "pseudo-terminals"; + break; + case RLIMIT_KQUEUES: + desc = "kqueues"; + break; + default: + desc = "resources"; + break; + } + + if (limexceeded) + logproc(LOG_INFO, p, __FUNCTION__, "attempted to allocate %ld " + "%s over %s limit of %ld\n", + (long)(newsize - curlim), desc, rlimit_ident[which], + (long)curlim); + +out: + return (limexceeded); +} + void uihashinit() { Index: sys/resourcevar.h =================================================================== --- sys/resourcevar.h (revision 264283) +++ sys/resourcevar.h (working copy) @@ -128,6 +128,7 @@ struct plimit *lim_alloc(void); void lim_copy(struct plimit *dst, struct plimit *src); rlim_t lim_cur(struct proc *p, int which); +int lim_exceeded(struct proc *p, int which, rlim_t newsize); void lim_fork(struct proc *p1, struct proc *p2); void lim_free(struct plimit *limp); struct plimit Index: vm/vm_mmap.c =================================================================== --- vm/vm_mmap.c (revision 264283) +++ vm/vm_mmap.c (working copy) @@ -1520,7 +1520,7 @@ vm_mmap(vm_map_t map, vm_offset_t *addr, if (map == &td->td_proc->p_vmspace->vm_map) { PROC_LOCK(td->td_proc); - if (map->size + size > lim_cur(td->td_proc, RLIMIT_VMEM)) { + if (lim_exceeded(td->td_proc, RLIMIT_VMEM, map->size + size)) { PROC_UNLOCK(td->td_proc); return (ENOMEM); }