Re: sysctlbyname returns 1
Re: sysctlbyname returns 1
- Subject: Re: sysctlbyname returns 1
- From: Quinn <email@hidden>
- Date: Wed, 24 Nov 2004 14:02:40 +0000
At 12:23 -0600 8/11/04, Geoffrey Schmit wrote:
The following code, when made from within a kernel extension, results in
the error variable being set to 1 (EPERM?):
[...]
Any ideas? The same code works fine from a user-mode console
This is caused by a bug in the kernel. Specifically, the definition
of the name2oid sysctl entry in "xnu/bsd/kern/kern_newsysctl.c":
SYSCTL_PROC(_sysctl, 3, name2oid, CTLFLAG_RW|CTLFLAG_ANYBODY, 0, 0,
sysctl_sysctl_name2oid, "I", "");
should include the CTLFLAG_KERN flag to allow the entry to be used by
kernel code. The absence of this flag causes sysctlbyname to fail
because it can't translate your name to an OID. See sysctlnametomib
(in the same file) for background.
This bug has already been fixed in Tiger. Until then you can work
around it by using kernel_sysctl instead of sysctlbyname, as
illustrated by the code below.
S+E
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Technical Support * Networking, Communications, Hardware
kern_return_t sysctlbyname_start(kmod_info_t * ki, void * d)
{
int err;
int numberOfCPUs;
size_t size;
int oid[2];
oid[0] = CTL_HW;
oid[1] = HW_NCPU;
numberOfCPUs = -1;
size = sizeof(numberOfCPUs);
err = kernel_sysctl(
current_proc(),
oid,
sizeof(oid) / sizeof(*oid),
&numberOfCPUs,
&size,
NULL,
0
);
if (err == 0) {
assert(size == sizeof(numberOfCPUs));
printf("kernel_sysctl: numberOfCPUs = %d\n", numberOfCPUs);
} else {
printf("kernel_sysctl: failed with error %d\n", err);
}
return KERN_SUCCESS;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-kernel mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden