Search code examples
macoskerneldevice-driverkernel-extension

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.)


Solution

  • 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.