Advanced Search
Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page >

Commonly Used Functions

One of the most common problems faced when programming in the kernel is use of “standard” functions—things like printf or bcopy. Many commonly used standard C library functions are implemented in the kernel. In order to use them, however, you need to include the appropriate prototypes, which may be different from the user space prototypes for those functions, and which generally have different names when included from kernel code.

In general, any non–I/O Kit header that you can safely include in the kernel is located in xnu/bsd/sys or xnu/osfmk/mach, although there are a few specialized headers in other places like libkern and libsa. Normal headers (those in /usr/include) cannot be used in the kernel.

Important: If you are writing an I/O Kit KEXT, most of these functions are not what you are looking for. The I/O Kit provides its own APIs for these features, including IOLog, IOMemoryDescriptor, and IOLock. While using the lower-level functionality is not expressly forbidden, it is generally discouraged (though printf is always fine). For more information about APIs available to I/O Kit KEXTs, see Kernel Framework Reference.

Table 6-1 lists some commonly used C functions, variables, and types, and gives the location of their prototypes.

Table 6-1  Commonly used C functions

Function name

Header path

printf

<sys/systm.h>

Buffer cache functions (bread, bwrite, and brelse)

<sys/buf.h>

Directory entries

<sys/dirent.h>

Error numbers

<sys/errno.h>

Kernel special variables

<sys/kernel.h>

Spinlocks

<sys/lock.h>

malloc

<sys/malloc.h>

Queues

<sys/queue.h>

Random number generator

<sys/rand.h>

bzero, bcopy, copyin, and copyout

<sys/systm.h>

timeout and untimeout

<sys/system.h>

Various time functions

<sys/time.h>

Standard type declarations

<sys/types.h>

<mach/mach_types.h>

User credentials

<sys/ucred.h>

OS and system information

<sys/utsname.h>

If the standard C function you are trying to use is not in one of these files, chances are the function is not supported for use within the kernel, and you need to implement your code in another way.

The symbols in these header files are divided among multiple symbol sets, depending on the technology area where they were designed to be used. To use these, you may have to declare dependencies on any of the following:

Where possible, you should specify a dependency on the KPI version of these symbols. However, these symbols are only available in v10.4 and later. For the I/O Kit and libkern, this should make little difference. For other areas, such as network kernel extensions or file system KEXTs, you must use the KPI versions if you want your extension to load in Mac OS X v10.4 and later.

For a complete list of symbols in any of these dependencies, run nm(1) on the binaries in /System/Library/Extensions/System.kext/PlugIns.



< Previous PageNext Page >


Last updated: 2006-11-07




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice