How can I get the OS X version number in a kext?

I have a kext that needs to know what version of OS X it is running on. CocoaDev has an article which describes how to get the OS X version info using Gestalt(), but the code requires Cocoa.

Can I call Gestalt() from a kext? If so, what #include do I use to define it? If not, are there any other solutions?


Background:

I'd like to use the same kexts in on all versions of OS X from 10.4 through 10.7.

BUT: The kexts call cdevsw_add, which was changed in Lion in a non-backward-compatible way. Along with (apparently) changes to some kernel programs that call it, the changes mean — per the comment before the routine — that cdevsw_add should be called with a different first argument on 10.7 than on OS X 10.0 through 10.6. (-12 on Lion, -1 on earlier versions.)

If the kexts can determine which version of OS X they are running on, it's easy. (If not, it will be a pain to do — maybe a horrible kludge like building two different versions of the kexts and having the kext-loading code pick which one to load.)

Answers


Kernel.framework provides <libkern/version.h>. There are declared some extern variables like version_major, version_minor etc. AFAIK those are exported from the libkern.kpi.

Hope it helps.


You can use sysctl to get the kernel version (scroll down to method 3). It allegedly works when you develop kernel modules.

Here's an example of the method, in case the site ever goes down.

#include <sys/param.h>
#include <sys/sysctl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int mib[] = {CTL_KERN, KERN_OSRELEASE};
    size_t len;
    sysctl(mib, sizeof mib / sizeof(int), NULL, &len, NULL, 0);

    char* kernelVersion = malloc(len);
    sysctl(mib, sizeof mib / sizeof(int), kernelVersion, &len, NULL, 0);

    printf("Kernel version is %s\n", kernelVersion);
    free(kernelVersion);
}

Of course, you'll need to figure out the kernel versions of Snow Leopard and Lion, but that shouldn't be very hard. (I can testify that the kernel version of the current Lion release is 11.0.0.)


Need Your Help

SignalR with multiple event handlers per event

javascript signalr

In C# i would hook many handlers to an event like so:

Stop flickering in simple Java animation

java swing timer paint japplet

I have a simple applet that animates a rectangle along the x-axis of the canvas. The problem is that it flickers. I have tried to Google this problem, but I didn't come up with anything useful or