Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bsd sockets in a kext



Stephane --

Thank you for the tip. This seems to be the way to go but I still have a lot
of trouble and I'm confused by the comments in the headers.

Which comments? What headers? Please be specific so that everyone has a reference...

Do you have an howto or some source code example, that would help a lot.

Well, someone else has been asking a similar question on the storage side. An example would be that in user space, you find your object. Say your object is named net_odul_driver_stephanekeyboard. You have to call registerService() in your start() routine in order for IOKit to see your object from user space. In user space, you register for notifications:

(This code might look familiar to someone lurking)

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <mach/mach_port.h>

void ServiceMatched ( void *refcon, io_iterator_t iterator );
void ServiceInterest ( void *refcon, io_service_t service, natural_t type, void *arg );

IONotificationPortRef gNotifyPort = NULL;

int
main ( int argc, const char *argv[] )
{
mach_port_t masterPort;
io_iterator_t iterator;

IOMasterPort ( bootstrap_port, &masterPort );
gNotifyPort = IONotificationPortCreate ( masterPort );
CFRunLoopAddSource ( CFRunLoopGetCurrent ( ), IONotificationPortGetRunLoopSource ( gNotifyPort ), kCFRunLoopCommonModes );
IOServiceAddMatchingNotification ( gNotifyPort,
kIOFirstMatchNotification,
IOServiceMatching("net_odul_driver_stephanekeyboard"),
&ServiceMatched,
NULL,
&iterator );

// The iterator might have objects here. Clear it out.
ServiceMatched ( NULL, iterator );
CFRunLoopRun ( );

if ( iterator != 0 )
{

IOObjectRelease ( iterator );
iterator = 0;

}

CFRunLoopRemoveSource ( CFRunLoopGetCurrent ( ), IONotificationPortGetRunLoopSource ( gNotifyPort ), kCFRunLoopCommonModes );

if ( gNotifyPort != 0 )
{
mach_port_deallocate ( gNotifyPort );
}

if ( masterPort != 0 )
{
mach_port_deallocate ( masterPort );
}

return 0;

}

void
ServiceMatched ( void *refcon, io_iterator_t iterator )
{
io_service_t service;
io_iterator_t ignored;

while( ( service = IOIteratorNext ( iterator ) ) != ( io_service_t ) NULL )
{
IOServiceAddInterestNotification ( gNotifyPort, service, kIOGeneralInterest, ServiceInterest, NULL, &ignored );
}
}

void
ServiceInterest ( void *refcon, io_service_t service, natural_t type, void *arg )
{
printf ( "interest: %08lX, type = %08lX, arg = %08lX\n", ( UInt32 ) service, ( UInt32 ) type, ( UInt32 ) arg );
}



The first part of this code attempts to find objects of type net_odul_driver_stephanekeyboard. The second part adds a general interest notification for the object. It is all runloop based. I think we are encouraging people who have to write a daemon to use CFRunLoop instead of rolling their own runloop using mach ports (Jim might be able to clarify). The function ServiceInterest will get called whenever your net_odul_driver_stephanekeyboard object calls messageClients().

There's no guarantee that this code will compile. Also, you should not take this code as an exact replica of what you want. It is just an example. There might be errors, leaks, etc.

-- Chris

------------------
6 Infinite Loop
M/S 306-2MS
Cupertino CA 95014
phone: (408) 974-4033
fax: (408) 862-7577
email: email@hidden
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Bsd sockets in a kext (From: Stephane ODUL <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.