Add proper const keywords to sysctl(3) parameters.

The `name' and `newp' arguments can be marked const, because the buffers
they refer to are never changed. While there, perform some other
cleanups:

- Remove K&R from sysctl.c.
- Implement sysctlbyname() using sysctlnametomib() to prevent
  duplication of an undocumented kernel interface.
- Fix some whitespace nits.

It seems the prototypes are now in sync with NetBSD as well.
This commit is contained in:
ed 2010-02-21 13:57:02 +00:00
parent 24f8e6df25
commit 1fdea921e1
5 changed files with 17 additions and 27 deletions

View File

@ -28,7 +28,7 @@
.\" @(#)sysctl.3 8.4 (Berkeley) 5/9/95
.\" $FreeBSD$
.\"
.Dd January 28, 2009
.Dd February 21, 2010
.Dt SYSCTL 3
.Os
.Sh NAME
@ -42,9 +42,9 @@
.In sys/types.h
.In sys/sysctl.h
.Ft int
.Fn sysctl "int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen"
.Fn sysctl "const int *name" "u_int namelen" "void *oldp" "size_t *oldlenp" "const void *newp" "size_t newlen"
.Ft int
.Fn sysctlbyname "const char *name" "void *oldp" "size_t *oldlenp" "void *newp" "size_t newlen"
.Fn sysctlbyname "const char *name" "void *oldp" "size_t *oldlenp" "const void *newp" "size_t newlen"
.Ft int
.Fn sysctlnametomib "const char *name" "int *mibp" "size_t *sizep"
.Sh DESCRIPTION

View File

@ -43,15 +43,12 @@ __FBSDID("$FreeBSD$");
#include <unistd.h>
#include <string.h>
extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen);
extern int __sysctl(const int *name, u_int namelen, void *oldp,
size_t *oldlenp, const void *newp, size_t newlen);
int
sysctl(name, namelen, oldp, oldlenp, newp, newlen)
int *name;
u_int namelen;
void *oldp, *newp;
size_t *oldlenp, newlen;
sysctl(const int *name, u_int namelen, void *oldp, size_t *oldlenp,
const void *newp, size_t newlen)
{
if (name[0] != CTL_USER)
return (__sysctl(name, namelen, oldp, oldlenp, newp, newlen));

View File

@ -13,26 +13,19 @@ __FBSDID("$FreeBSD$");
#include <sys/types.h>
#include <sys/sysctl.h>
#include <string.h>
int
sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
size_t newlen)
sysctlbyname(const char *name, void *oldp, size_t *oldlenp,
const void *newp, size_t newlen)
{
int name2oid_oid[2];
int real_oid[CTL_MAXNAME+2];
int error;
size_t oidlen;
name2oid_oid[0] = 0; /* This is magic & undocumented! */
name2oid_oid[1] = 3;
oidlen = sizeof(real_oid);
error = sysctl(name2oid_oid, 2, real_oid, &oidlen, (void *)name,
strlen(name));
oidlen = sizeof(real_oid) / sizeof(int);
error = sysctlnametomib(name, real_oid, &oidlen);
if (error < 0)
return error;
oidlen /= sizeof (int);
return (error);
error = sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen);
return (error);
}

View File

@ -48,8 +48,8 @@ sysctlnametomib(const char *name, int *mibp, size_t *sizep)
oid[0] = 0;
oid[1] = 3;
*sizep *= sizeof (int);
error = sysctl(oid, 2, mibp, sizep, (void *)name, strlen(name));
*sizep /= sizeof (int);
*sizep *= sizeof(int);
error = sysctl(oid, 2, mibp, sizep, name, strlen(name));
*sizep /= sizeof(int);
return (error);
}

View File

@ -714,8 +714,8 @@ int sysctl_wire_old_buffer(struct sysctl_req *req, size_t len);
#include <sys/cdefs.h>
__BEGIN_DECLS
int sysctl(int *, u_int, void *, size_t *, void *, size_t);
int sysctlbyname(const char *, void *, size_t *, void *, size_t);
int sysctl(const int *, u_int, void *, size_t *, const void *, size_t);
int sysctlbyname(const char *, void *, size_t *, const void *, size_t);
int sysctlnametomib(const char *, int *, size_t *);
__END_DECLS
#endif /* _KERNEL */