From owner-freebsd-bugs@FreeBSD.ORG Sat Feb 23 15:50:00 2013 Return-Path: Delivered-To: freebsd-bugs@smarthost.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 95291569 for ; Sat, 23 Feb 2013 15:50:00 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:1900:2254:206c::16:87]) by mx1.freebsd.org (Postfix) with ESMTP id 78BE1260; Sat, 23 Feb 2013 15:50:00 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.6/8.14.6) with ESMTP id r1NFo0gF034445; Sat, 23 Feb 2013 15:50:00 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.6/8.14.6/Submit) id r1NFo0NG034444; Sat, 23 Feb 2013 15:50:00 GMT (envelope-from gnats) Resent-Date: Sat, 23 Feb 2013 15:50:00 GMT Resent-Message-Id: <201302231550.r1NFo0NG034444@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Cc: Marius Strobl Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Christoph Mallon Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 4B7BA4A2 for ; Sat, 23 Feb 2013 15:44:20 +0000 (UTC) (envelope-from christoph.mallon@gmx.de) Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by mx1.freebsd.org (Postfix) with ESMTP id E4E06237 for ; Sat, 23 Feb 2013 15:44:19 +0000 (UTC) Received: from mailout-de.gmx.net ([10.1.76.30]) by mrigmx.server.lan (mrigmx001) with ESMTP (Nemesis) id 0M8LTo-1V4pdJ0eDC-00vyoa for ; Sat, 23 Feb 2013 16:44:19 +0100 Received: (qmail invoked by alias); 23 Feb 2013 15:44:18 -0000 Received: from p5B132CB8.dip.t-dialin.net (EHLO rotluchs.lokal) [91.19.44.184] by mail.gmx.net (mp030) with SMTP; 23 Feb 2013 16:44:18 +0100 Received: from tron by rotluchs.lokal with local (Exim 4.80.1 (FreeBSD)) (envelope-from ) id 1U9HGr-000Dez-I3 for FreeBSD-gnats-submit@freebsd.org; Sat, 23 Feb 2013 16:44:17 +0100 Message-Id: Date: Sat, 23 Feb 2013 16:44:17 +0100 From: Christoph Mallon To: FreeBSD-gnats-submit@freebsd.org X-Send-Pr-Version: 3.114 X-GNATS-Notify: Marius Strobl Subject: kern/176373: [PATCH] Simplify and clean up kern_sysctl X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: Christoph Mallon List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 23 Feb 2013 15:50:00 -0000 >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 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 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 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 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: