kern/176373: [PATCH] Simplify and clean up kern_sysctl

Christoph Mallon christoph.mallon at gmx.de
Sat Feb 23 15:50:00 UTC 2013


>Number:         176373
>Category:       kern
>Synopsis:       [PATCH] Simplify and clean up kern_sysctl
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Sat Feb 23 15:50:00 UTC 2013
>Closed-Date:
>Last-Modified:
>Originator:     Christoph Mallon
>Release:        
>Organization:
>Environment:


	
>Description:
This series of patches performs some simplifications and clean ups in kern_sysctl:
- Use strdup() instead of reimplementing it.
- Use __DECONST instead of strange casts.
- Remove pointless #ifndef _SYS_SYSPROTO_H_.
- Reduce code duplication and simplify name2oid().
>How-To-Repeat:
	
>Fix:
Please apply these patches.

--- 0001-kern_sysctl-Use-strdup-instead-of-reimplementing-it.patch begins here ---
>From c2614b9cffabd2e796e979dfefaaa28931506c13 Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon at gmx.de>
Date: Sat, 23 Feb 2013 12:46:06 +0100
Subject: [PATCH 1/4] kern_sysctl: Use strdup() instead of reimplementing it.

---
 sys/kern/kern_sysctl.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 33296d3..6a7b58c 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -462,8 +462,6 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 	int (*handler)(SYSCTL_HANDLER_ARGS), const char *fmt, const char *descr)
 {
 	struct sysctl_oid *oidp;
-	ssize_t len;
-	char *newname;
 
 	/* You have to hook up somewhere.. */
 	if (parent == NULL)
@@ -490,11 +488,7 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 	SLIST_NEXT(oidp, oid_link) = NULL;
 	oidp->oid_number = number;
 	oidp->oid_refcnt = 1;
-	len = strlen(name);
-	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
-	bcopy(name, newname, len + 1);
-	newname[len] = '\0';
-	oidp->oid_name = newname;
+	oidp->oid_name = strdup(name, M_SYSCTLOID);
 	oidp->oid_handler = handler;
 	oidp->oid_kind = CTLFLAG_DYN | kind;
 	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
@@ -508,12 +502,8 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 		oidp->oid_arg2 = arg2;
 	}
 	oidp->oid_fmt = fmt;
-	if (descr) {
-		int len = strlen(descr) + 1;
-		oidp->oid_descr = malloc(len, M_SYSCTLOID, M_WAITOK);
-		if (oidp->oid_descr)
-			strcpy((char *)(uintptr_t)(const void *)oidp->oid_descr, descr);
-	}
+	if (descr)
+		oidp->oid_descr = strdup(descr, M_SYSCTLOID);
 	/* Update the context, if used */
 	if (clist != NULL)
 		sysctl_ctx_entry_add(clist, oidp);
@@ -529,14 +519,10 @@ sysctl_add_oid(struct sysctl_ctx_list *clist, struct sysctl_oid_list *parent,
 void
 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
 {
-	ssize_t len;
 	char *newname;
 	void *oldname;
 
-	len = strlen(name);
-	newname = malloc(len + 1, M_SYSCTLOID, M_WAITOK);
-	bcopy(name, newname, len + 1);
-	newname[len] = '\0';
+	newname = strdup(name, M_SYSCTLOID);
 	SYSCTL_XLOCK();
 	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
 	oidp->oid_name = newname;
-- 
1.8.1.3
--- 0001-kern_sysctl-Use-strdup-instead-of-reimplementing-it.patch ends here ---

--- dummy1 begins here ---
dummy file, because GNATS damages every other file
--- dummy1 ends here ---

--- 0002-kern_sysctl-Use-__DECONST-instead-of-strange-casts.patch begins here ---
>From 1dfbd4970ca555fe1eb7a291dd04c3863e50297b Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon at gmx.de>
Date: Sat, 23 Feb 2013 12:48:43 +0100
Subject: [PATCH 2/4] kern_sysctl: Use __DECONST instead of strange casts.

---
 sys/kern/kern_sysctl.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 6a7b58c..b1d33cb 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -444,9 +444,8 @@ sysctl_remove_oid_locked(struct sysctl_oid *oidp, int del, int recurse)
 				SYSCTL_SLEEP(&oidp->oid_running, "oidrm", 0);
 			}
 			if (oidp->oid_descr)
-				free((void *)(uintptr_t)(const void *)oidp->oid_descr, M_SYSCTLOID);
-			free((void *)(uintptr_t)(const void *)oidp->oid_name,
-			     M_SYSCTLOID);
+				free(__DECONST(char *, oidp->oid_descr), M_SYSCTLOID);
+			free(__DECONST(char *, oidp->oid_name, M_SYSCTLOID));
 			free(oidp, M_SYSCTLOID);
 		}
 	}
@@ -520,11 +519,11 @@ void
 sysctl_rename_oid(struct sysctl_oid *oidp, const char *name)
 {
 	char *newname;
-	void *oldname;
+	char *oldname;
 
 	newname = strdup(name, M_SYSCTLOID);
 	SYSCTL_XLOCK();
-	oldname = (void *)(uintptr_t)(const void *)oidp->oid_name;
+	oldname = __DECONST(char *, oidp->oid_name);
 	oidp->oid_name = newname;
 	SYSCTL_XUNLOCK();
 	free(oldname, M_SYSCTLOID);
-- 
1.8.1.3
--- 0002-kern_sysctl-Use-__DECONST-instead-of-strange-casts.patch ends here ---

--- dummy2 begins here ---
dummy file, because GNATS damages every other file
--- dummy2 ends here ---

--- 0003-kern_sysctl-Remove-pointless-ifndef-_SYS_SYSPROTO_H_.patch begins here ---
>From e8423de48e8864ea8db1e6cb8655288804a985bc Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon at gmx.de>
Date: Sat, 23 Feb 2013 12:53:57 +0100
Subject: [PATCH 3/4] kern_sysctl: Remove pointless #ifndef _SYS_SYSPROTO_H_.

The first thing, which the included file sys/sysproto.h does, is setting _SYS_SYSPROTO_H_.
---
 sys/kern/kern_sysctl.c | 10 ----------
 1 file changed, 10 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index b1d33cb..74e4137 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -1522,16 +1522,6 @@ sysctl_root(SYSCTL_HANDLER_ARGS)
 	return (error);
 }
 
-#ifndef _SYS_SYSPROTO_H_
-struct sysctl_args {
-	int	*name;
-	u_int	namelen;
-	void	*old;
-	size_t	*oldlenp;
-	void	*new;
-	size_t	newlen;
-};
-#endif
 int
 sys___sysctl(struct thread *td, struct sysctl_args *uap)
 {
-- 
1.8.1.3
--- 0003-kern_sysctl-Remove-pointless-ifndef-_SYS_SYSPROTO_H_.patch ends here ---

--- dummy3 begins here ---
dummy file, because GNATS damages every other file
--- dummy3 ends here ---

--- 0004-kern_sysctl-Reduce-code-duplication-and-simplify-nam.patch begins here ---
>From 1123715a295de35502afc6e59ae280c16d598236 Mon Sep 17 00:00:00 2001
From: Christoph Mallon <christoph.mallon at gmx.de>
Date: Sat, 23 Feb 2013 16:04:53 +0100
Subject: [PATCH 4/4] kern_sysctl: Reduce code duplication and simplify
 name2oid().

---
 sys/kern/kern_sysctl.c | 36 ++++++++----------------------------
 1 file changed, 8 insertions(+), 28 deletions(-)

diff --git a/sys/kern/kern_sysctl.c b/sys/kern/kern_sysctl.c
index 74e4137..687d07a 100644
--- a/sys/kern/kern_sysctl.c
+++ b/sys/kern/kern_sysctl.c
@@ -808,39 +808,26 @@ static SYSCTL_NODE(_sysctl, 2, next, CTLFLAG_RD | CTLFLAG_CAPRD,
 static int
 name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
 {
-	int i;
 	struct sysctl_oid *oidp;
 	struct sysctl_oid_list *lsp = &sysctl__children;
 	char *p;
 
 	SYSCTL_ASSERT_XLOCKED();
 
-	if (!*name)
-		return (ENOENT);
-
-	p = name + strlen(name) - 1 ;
-	if (*p == '.')
-		*p = '\0';
-
-	*len = 0;
-
-	for (p = name; *p && *p != '.'; p++) 
-		;
-	i = *p;
-	if (i == '.')
-		*p = '\0';
+	for (*len = 0; *len < CTL_MAXNAME;) {
+		p = strsep(&name, ".");
 
-	oidp = SLIST_FIRST(lsp);
-
-	while (oidp && *len < CTL_MAXNAME) {
-		if (strcmp(name, oidp->oid_name)) {
-			oidp = SLIST_NEXT(oidp, oid_link);
-			continue;
+		oidp = SLIST_FIRST(lsp);
+		for (;; oidp = SLIST_NEXT(oidp, oid_link)) {
+			if (oidp == NULL)
+				return (ENOENT);
+			if (strcmp(p, oidp->oid_name) == 0)
+				break;
 		}
 		*oid++ = oidp->oid_number;
 		(*len)++;
 
-		if (!i) {
+		if (name == NULL || *name == '\0') {
 			if (oidpp)
 				*oidpp = oidp;
 			return (0);
@@ -853,13 +840,6 @@ name2oid(char *name, int *oid, int *len, struct sysctl_oid **oidpp)
 			break;
 
 		lsp = SYSCTL_CHILDREN(oidp);
-		oidp = SLIST_FIRST(lsp);
-		name = p+1;
-		for (p = name; *p && *p != '.'; p++) 
-				;
-		i = *p;
-		if (i == '.')
-			*p = '\0';
 	}
 	return (ENOENT);
 }
-- 
1.8.1.3
--- 0004-kern_sysctl-Reduce-code-duplication-and-simplify-nam.patch ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list