全部博文(15)
分类: BSD
2012-04-12 04:45:44
In this tutorial, you learn how to create a generic kernel extension (kext) for Mac OS X. You create a simple kext that prints messages when loading and unloading. This tutorial does not cover the process for loading or debugging your kext—see “Debugging a Kernel Extension With GDB” after you have completed this tutorial for information on loading and debugging.
If you are unfamiliar with Xcode, first read A Tour of Xcode.
Road MapThese are the four major steps you follow:
This tutorial assumes that you are logged in as an administrator of your machine, which is necessary for using the sudo command.
Create a New ProjectCreating a kext project in Xcode is as simple as selecting the appropriate project template and providing a name.
Launch Xcode.
Choose File > New > New Project. The New Project panel appears.
In the New Project panel, select System Plug-in from the list of project categories on the left. Select Generic Kernel Extension from the list of templates on the right. Click Next.
In the screen that appears, enter MyKext for the product name, enter a company identifier, and click Next.
Choose a location for the project, and click Create.
Xcode creates a new project and displays its project window. You should see something like this:The new project contains several files, including a source file, MyKext.c, that contains templates for the kext’s start and stop functions.
6. Make sure the kext is building for the correct architectures.(If you don’t see the screen above, select MyKext under Targets. Select the Build Settings tab. Click the disclosure triangle next to Architectures.)
Next to Build Active Architecture Only make sure to select No—this is especially important if you are running a 32-bit kernel on a 64-bit machine.
Now that you’ve created the project, it’s time to make your kext do something when it gets loaded and unloaded. You’ll do so by adding code to your kext’s start and stop functions, which are called when your kext is loaded and unloaded.
Implement the Start and Stop FunctionsOpen MyKext.c to edit the start and stop functions.
Figure 1 shows the unedited file.
Figure 1 Viewing source code in XcodeThe default start and stop functions do nothing but return a successful status. A real kext’s start and stop functions typically register and unregister callbacks with kernel runtime systems, but for this tutorial, your kext simply prints messages so that you can confirm when your kext has been started and stopped.
2. Edit MyKext.c to match the code in Listing 1.
Listing 1 MyKext.c file contents
点击(此处)折叠或打开
Notice that MyKext.c includes two header files,
Save your changes by choosing File > Save.
Like all bundles, a kext contains an information property list, which describes the kext. The default Info.plist file created by Xcode contains template values that you must edit to describe your kext.
A kext’s Info.plist file is in XML format. Whenever possible, you should view and edit the file from within Xcode or within the Property List Editor application. In this way, you help ensure that you don’t add elements (such as comments) that cannot be parsed by the kernel during early boot.
Click Info.plist in the Xcode project window.
Xcode displays the Info.plist file in the editor pane. You should see the elements of the property list file, as shown in Figure 2.
Figure 2 MyKext Info.plistBy default, Xcode’s property list editor masks the actual keys and values of a property list. To see the actual keys and values, Control-click anywhere in the property list editor and choose Show Raw Keys/Values from the contextual menu.
Change the value of the CFBundleIdentifier property to use your unique namespace prefix.
On the line for CFBundleIdentifier, double-click in the Value column to edit it. Select com.yourcompany and change it to com.MyCompany (or your company’s DNS domain in reverse). The value should now be com.MyCompany.kext.${PRODUCT_NAME:rfc1034identifier}.
Bundles in Mac OS X typically use a reverse-DNS naming convention to avoid namespace collisions. This is particularly important for kexts because all loaded kexts share a single namespace for bundle identifiers.
The last portion of the default bundle identifier, ${PRODUCT_NAME:rfc1034identifier}, is replaced with the Product Name build setting for the kext target when you build your project.
Save your changes by choosing File > Save.
Now you’re ready to configure your build settings and build your kext to make sure the source code compiles. First, configure your build settings to build the kext for every architecture, to make sure your kext will load regardless of the architecture of the kernel.
Click the disclosure triangle next to Targets in the Groups and Files pane.
Select the MyKext target.
Choose File > Get Info. The Target “MyKext” Info window opens.
In the list of settings, find Build Active Architecture Only and make sure the checkbox is unchecked.
Close the Target MyKext Info window.
Now that your kext is building against every architecture, choose Build > Build to build your project. If the build fails, correct all indicated errors and rebuild before proceeding.
Add Library DeclarationsBecause kexts are linked at load time, a kext must list its libraries in its information property list with the OSBundleLibraries property. At this stage of creating your kext, you need to find out what those libraries are. The best way to do so is to run the kextlibs tool on your built kext and copy its output into your kext’s Info.plist file.
Run kextlibs on the Kernel Extensionkextlibs is a command-line program that you run with the Terminal application. Its purpose is to identify libraries that your kext needs to link against.
Note: This tutorial uses the dollar sign ($) prompt when it shows the commands you type in the Terminal application. This is the default prompt of the bash shell, which is the default shell in Mac OS X. If you’re using a different shell, you may see a different prompt (the percent symbol (%) is another common prompt).
Start the Terminal application, located in /Applications/Utilities.
In the Terminal window, move to the directory that contains your kext.
Xcode stores your kext in the Debug folder of the build
folder of your project (unless you’ve chosen a different build
configuration or set a different location for build products using
Xcode’s Preferences dialog).
点击(此处)折叠或打开
This directory contains your kext. It should have the name MyKext.kext. This name is formed from the Product Name as set in your target’s build settings, and a suffix, in this case .kext.
Run kextlibs on your kernel extension with the -xml command-line flag.
This command looks for all unresolved symbols in your kernel extension’s executable among the installed library extensions (in /System/Library/Extensions/) and prints an XML fragment suitable for pasting into an Info.plist file. For example:
点击(此处)折叠或打开
点击(此处)折叠或打开
If kextlibs prints any errors or exits with a nonzero status, it may have been unable to locate some symbols. For this tutorial, the libraries are known, but in general usage you should use the kextfind tool to find libraries for any symbols that kextlibs cannot locate. See “Locate Kexts with kextfind” for information on kextfind.
Select the XML output of kextlibs and choose Edit > Copy.
Earlier, you edited the information property list with Xcode’s graphical property list editor. For this operation, however, you need to edit the information property list as text.
Control-click Info.plist in the Xcode project window, then choose Open As > Source Code File from the contextual menu.
Xcode displays the Info.plist file in the editor pane. You should see the XML contents of the property list file, as shown in Figure 3. Note that dictionary keys and values are listed sequentially.
Figure 3 MyKext Info.plist as textFigure 3 MyKext Info.plist as text2. Select all the lines defining the empty OSBundleLibraries dictionary:
点击(此处)折叠或打开
3. Paste text into the info dictionary.
If kextlibs ran successfully, choose Edit > Paste to paste the text you copied from Terminal.
If kextlibs didn’t run successfully, type or paste this text into the info dictionary:
点击(此处)折叠或打开
4. Save your changes by choosing File > Save.
5. Choose Build > Build a final time to rebuild your kext with the new information property list.
Now you are ready to prepare your kext for loading. You do this with the kextutil tool, which can examine a kext and determine whether it is able to be loaded. kextutil can also load a kext for development purposes, but that functionality is not covered in this tutorial.
Note: This tutorial does not cover actually loading a kext. For safety reasons, you should not load your kext on your development machine. For information on loading and debugging a kext with a two-machine setup, see “Debugging a Kernel Extension With GDB.”
Kexts have strict permissions requirements (see “Kernel Extensions Have Strict Security Requirements” for details). The easiest way to set these permissions is to create a copy of your kext as the root user. Type the following into Terminal from the proper directory and provide your password when prompted:
点击(此处)折叠或打开
Now that the permissions of the kext’s temporary copy are correct, you are ready to run kextutil.
Run kextutilType the following into Terminal:
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
zhanleewo2012-04-13 15:51:55