Module Name:    src
Committed By:   rmind
Date:           Sat May 14 17:57:05 UTC 2011

Modified Files:
        src/sys/kern: kern_resource.c
        src/sys/sys: resource.h resourcevar.h

Log Message:
- Sprinkle __read_mostly, consitify maxdmap and maxsmap.
- Prevent sys/resourcevar.h from inclusion in userland.
- sys_{set,get}priority: use id_t for 'who', not int.
- Make donice() routine static.
- Remove trailing spaces, KNF.


To generate a diff of this commit:
cvs rdiff -u -r1.163 -r1.164 src/sys/kern/kern_resource.c
cvs rdiff -u -r1.31 -r1.32 src/sys/sys/resource.h
cvs rdiff -u -r1.51 -r1.52 src/sys/sys/resourcevar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_resource.c
diff -u src/sys/kern/kern_resource.c:1.163 src/sys/kern/kern_resource.c:1.164
--- src/sys/kern/kern_resource.c:1.163	Sat May 14 17:12:28 2011
+++ src/sys/kern/kern_resource.c	Sat May 14 17:57:05 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_resource.c,v 1.163 2011/05/14 17:12:28 rmind Exp $	*/
+/*	$NetBSD: kern_resource.c,v 1.164 2011/05/14 17:57:05 rmind Exp $	*/
 
 /*-
  * Copyright (c) 1982, 1986, 1991, 1993
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.163 2011/05/14 17:12:28 rmind Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_resource.c,v 1.164 2011/05/14 17:57:05 rmind Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -62,15 +62,17 @@
  * Maximum process data and stack limits.
  * They are variables so they are patchable.
  */
-rlim_t maxdmap = MAXDSIZ;
-rlim_t maxsmap = MAXSSIZ;
+const rlim_t		maxdmap = MAXDSIZ;
+const rlim_t		maxsmap = MAXSSIZ;
 
-static pool_cache_t	plimit_cache;
-static pool_cache_t	pstats_cache;
+static pool_cache_t	plimit_cache	__read_mostly;
+static pool_cache_t	pstats_cache	__read_mostly;
 
 static kauth_listener_t	resource_listener;
+static struct sysctllog	*proc_sysctllog;
 
-static void sysctl_proc_setup(void);
+static int	donice(struct lwp *, struct proc *, int);
+static void	sysctl_proc_setup(void);
 
 static int
 resource_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
@@ -85,12 +87,12 @@
 	switch (action) {
 	case KAUTH_PROCESS_NICE:
 		if (kauth_cred_geteuid(cred) != kauth_cred_geteuid(p->p_cred) &&
-                    kauth_cred_getuid(cred) != kauth_cred_geteuid(p->p_cred)) {
-                        break;
-                }
+		    kauth_cred_getuid(cred) != kauth_cred_geteuid(p->p_cred)) {
+			break;
+		}
 
-                if ((u_long)arg1 >= p->p_nice)
-                        result = KAUTH_RESULT_ALLOW;
+		if ((u_long)arg1 >= p->p_nice)
+			result = KAUTH_RESULT_ALLOW;
 
 		break;
 
@@ -163,8 +165,8 @@
 		syscallarg(id_t) who;
 	} */
 	struct proc *curp = l->l_proc, *p;
+	id_t who = SCARG(uap, who);
 	int low = NZERO + PRIO_MAX + 1;
-	int who = SCARG(uap, who);
 
 	mutex_enter(proc_lock);
 	switch (SCARG(uap, which)) {
@@ -202,17 +204,17 @@
 
 	default:
 		mutex_exit(proc_lock);
-		return (EINVAL);
+		return EINVAL;
 	}
 	mutex_exit(proc_lock);
 
-	if (low == NZERO + PRIO_MAX + 1)
-		return (ESRCH);
+	if (low == NZERO + PRIO_MAX + 1) {
+		return ESRCH;
+	}
 	*retval = low - NZERO;
-	return (0);
+	return 0;
 }
 
-/* ARGSUSED */
 int
 sys_setpriority(struct lwp *l, const struct sys_setpriority_args *uap,
     register_t *retval)
@@ -223,8 +225,8 @@
 		syscallarg(int) prio;
 	} */
 	struct proc *curp = l->l_proc, *p;
+	id_t who = SCARG(uap, who);
 	int found = 0, error = 0;
-	int who = SCARG(uap, who);
 
 	mutex_enter(proc_lock);
 	switch (SCARG(uap, which)) {
@@ -277,9 +279,8 @@
 		return EINVAL;
 	}
 	mutex_exit(proc_lock);
-	if (found == 0)
-		return ESRCH;
-	return error;
+
+	return (found == 0) ? ESRCH : error;
 }
 
 /*
@@ -287,7 +288,7 @@
  *
  * Call with the target process' credentials locked.
  */
-int
+static int
 donice(struct lwp *l, struct proc *chgp, int n)
 {
 	kauth_cred_t cred = l->l_cred;
@@ -297,21 +298,25 @@
 	if (kauth_cred_geteuid(cred) && kauth_cred_getuid(cred) &&
 	    kauth_cred_geteuid(cred) != kauth_cred_geteuid(chgp->p_cred) &&
 	    kauth_cred_getuid(cred) != kauth_cred_geteuid(chgp->p_cred))
-		return (EPERM);
+		return EPERM;
 
-	if (n > PRIO_MAX)
+	if (n > PRIO_MAX) {
 		n = PRIO_MAX;
-	if (n < PRIO_MIN)
+	}
+	if (n < PRIO_MIN) {
 		n = PRIO_MIN;
+	}
 	n += NZERO;
+
 	if (kauth_authorize_process(cred, KAUTH_PROCESS_NICE, chgp,
-	    KAUTH_ARG(n), NULL, NULL))
-		return (EACCES);
+	    KAUTH_ARG(n), NULL, NULL)) {
+		return EACCES;
+	}
+
 	sched_nice(chgp, n);
-	return (0);
+	return 0;
 }
 
-/* ARGSUSED */
 int
 sys_setrlimit(struct lwp *l, const struct sys_setrlimit_args *uap,
     register_t *retval)
@@ -320,14 +325,14 @@
 		syscallarg(int) which;
 		syscallarg(const struct rlimit *) rlp;
 	} */
-	int which = SCARG(uap, which);
+	int error, which = SCARG(uap, which);
 	struct rlimit alim;
-	int error;
 
 	error = copyin(SCARG(uap, rlp), &alim, sizeof(struct rlimit));
-	if (error)
-		return (error);
-	return (dosetrlimit(l, l->l_proc, which, &alim));
+	if (error) {
+		return error;
+	}
+	return dosetrlimit(l, l->l_proc, which, &alim);
 }
 
 int
@@ -337,14 +342,14 @@
 	int error;
 
 	if ((u_int)which >= RLIM_NLIMITS)
-		return (EINVAL);
+		return EINVAL;
 
 	if (limp->rlim_cur > limp->rlim_max) {
 		/*
 		 * This is programming error. According to SUSv2, we should
 		 * return error in this case.
 		 */
-		return (EINVAL);
+		return EINVAL;
 	}
 
 	alimp = &p->p_rlimit[which];
@@ -356,7 +361,7 @@
 	error = kauth_authorize_process(l->l_cred, KAUTH_PROCESS_RLIMIT,
 	    p, KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_SET), limp, KAUTH_ARG(which));
 	if (error)
-		return (error);
+		return error;
 
 	lim_privatise(p);
 	/* p->p_limit is now unchangeable */
@@ -383,9 +388,9 @@
 		 * moment it would try to access anything on it's current stack.
 		 * This conforms to SUSv2.
 		 */
-		if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE
-		    || limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
-			return (EINVAL);
+		if (limp->rlim_cur < p->p_vmspace->vm_ssize * PAGE_SIZE ||
+		    limp->rlim_max < p->p_vmspace->vm_ssize * PAGE_SIZE) {
+			return EINVAL;
 		}
 
 		/*
@@ -439,10 +444,9 @@
 	mutex_enter(&p->p_limit->pl_lock);
 	*alimp = *limp;
 	mutex_exit(&p->p_limit->pl_lock);
-	return (0);
+	return 0;
 }
 
-/* ARGSUSED */
 int
 sys_getrlimit(struct lwp *l, const struct sys_getrlimit_args *uap,
     register_t *retval)
@@ -456,7 +460,7 @@
 	struct rlimit rl;
 
 	if ((u_int)which >= RLIM_NLIMITS)
-		return (EINVAL);
+		return EINVAL;
 
 	mutex_enter(p->p_lock);
 	memcpy(&rl, &p->p_rlimit[which], sizeof(rl));
@@ -537,7 +541,6 @@
 	}
 }
 
-/* ARGSUSED */
 int
 sys___getrusage50(struct lwp *l, const struct sys___getrusage50_args *uap,
     register_t *retval)
@@ -738,20 +741,18 @@
 struct pstats *
 pstatscopy(struct pstats *ps)
 {
+	struct pstats *nps;
+	size_t len;
 
-	struct pstats *newps;
-
-	newps = pool_cache_get(pstats_cache, PR_WAITOK);
+	nps = pool_cache_get(pstats_cache, PR_WAITOK);
 
-	memset(&newps->pstat_startzero, 0,
-	(unsigned) ((char *)&newps->pstat_endzero -
-		    (char *)&newps->pstat_startzero));
-	memcpy(&newps->pstat_startcopy, &ps->pstat_startcopy,
-	((char *)&newps->pstat_endcopy -
-	 (char *)&newps->pstat_startcopy));
+	len = (char *)&nps->pstat_endzero - (char *)&nps->pstat_startzero;
+	memset(&nps->pstat_startzero, 0, len);
 
-	return (newps);
+	len = (char *)&nps->pstat_endcopy - (char *)&nps->pstat_startcopy;
+	memcpy(&nps->pstat_startcopy, &ps->pstat_startcopy, len);
 
+	return nps;
 }
 
 void
@@ -1015,13 +1016,11 @@
 	return error;
 }
 
-static struct sysctllog *proc_sysctllog;
-
 /*
- * and finally, the actually glue that sticks it to the tree
+ * Setup sysctl nodes.
  */
 static void
-sysctl_proc_setup()
+sysctl_proc_setup(void)
 {
 
 	sysctl_createv(&proc_sysctllog, 0, NULL, NULL,

Index: src/sys/sys/resource.h
diff -u src/sys/sys/resource.h:1.31 src/sys/sys/resource.h:1.32
--- src/sys/sys/resource.h:1.31	Sun Mar 29 01:02:50 2009
+++ src/sys/sys/resource.h	Sat May 14 17:57:05 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: resource.h,v 1.31 2009/03/29 01:02:50 mrg Exp $	*/
+/*	$NetBSD: resource.h,v 1.32 2011/05/14 17:57:05 rmind Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -124,8 +124,6 @@
 extern struct loadavg averunnable;
 struct pcred;
 int	dosetrlimit(struct lwp *, struct proc *, int, struct rlimit *);
-int	donice(struct lwp *, struct proc *, int);
-
 #else
 #include <sys/cdefs.h>
 

Index: src/sys/sys/resourcevar.h
diff -u src/sys/sys/resourcevar.h:1.51 src/sys/sys/resourcevar.h:1.52
--- src/sys/sys/resourcevar.h:1.51	Sun May  1 01:15:18 2011
+++ src/sys/sys/resourcevar.h	Sat May 14 17:57:05 2011
@@ -1,4 +1,4 @@
-/*	$NetBSD: resourcevar.h,v 1.51 2011/05/01 01:15:18 rmind Exp $	*/
+/*	$NetBSD: resourcevar.h,v 1.52 2011/05/14 17:57:05 rmind Exp $	*/
 
 /*
  * Copyright (c) 1991, 1993
@@ -34,6 +34,10 @@
 #ifndef	_SYS_RESOURCEVAR_H_
 #define	_SYS_RESOURCEVAR_H_
 
+#if !defined(_KERNEL) && !defined(_KMEMUSER)
+#error "not supposed to be exposed to userland"
+#endif
+
 #include <sys/mutex.h>
 
 /*
@@ -112,9 +116,9 @@
 void	ruadd(struct rusage *, struct rusage *);
 void	rulwps(proc_t *, struct rusage *);
 struct	pstats *pstatscopy(struct pstats *);
-void 	pstatsfree(struct pstats *);
-extern rlim_t maxdmap;
-extern rlim_t maxsmap;
+void	pstatsfree(struct pstats *);
+extern const rlim_t maxdmap;
+extern const rlim_t maxsmap;
 
 #endif
 

Reply via email to