DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 17th August 2009
sbonar sbonar is offline
New User
 
Join Date: Aug 2009
Posts: 4
Default Newbie kernel question

What is the best way for kernel <-> user space communication? Examples?

Thx
Reply With Quote
  #2   (View Single Post)  
Old 17th August 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,975
Default

You don't specify anything, so this answer is extremely generic:

The POSIX standard specifies many different communications methods. While most are Inter-process communication techniques (IPCs) , there are some IPC methods that include kernel communication, e.g.: Signals and Timers.

However, the most common method for a userland application to contact the kernel is via a syscall. http://en.wikipedia.org/wiki/System_call

If you ask a more specific question, you'll get a more specific answer.
Reply With Quote
  #3   (View Single Post)  
Old 17th August 2009
sbonar sbonar is offline
New User
 
Join Date: Aug 2009
Posts: 4
Default

Sorry for being so generic.

To be more specific, I have a kernel driver that is collecting information. I also have a user-space app that wants to get that information. I know that a basic way for the user-space app to get the information is to use an IOCTL call, but I was wonder if there wasn't a more efficient way do this. For one thing, with the IOCTL, I have to poll every x secs to determine if there is any information to read, which is very inefficient.

Hope that helps
Reply With Quote
  #4   (View Single Post)  
Old 17th August 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,975
Default

It does help, thanks!

IIRC, ioctl(2) is an interface to special files (/dev entries). You might be able to continue using ioctl, as you have designed your program, but instead of polling continuously, add notification through kqueue(2)/kevent(2) instead. Or, perhaps you could abandon ioctl (since it was intended for device control, not information gathering) and consider using the suite of socket(2)-based syscalls.
Reply With Quote
  #5   (View Single Post)  
Old 17th August 2009
sbonar sbonar is offline
New User
 
Join Date: Aug 2009
Posts: 4
Default

Thanks. Any good examples on how a kernel and user-spaced app can use socket calls to communicate? I've used sockets to communicate between 2 apps in user-space on 2 different machines, i.e. client-server, but never from kernel to user-space.

Thanks again.
Reply With Quote
  #6   (View Single Post)  
Old 17th August 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,975
Default

I haven't either.

Poking through use-cases, it appears socket(2) is for userland-userland communcation. Sorry to have mislead you.

(There is the project at http://ksocket.sourceforge.org, dating from 2007, but that is Linux-specific. There was the ECalls project at http://www.cc.gatech.edu/systems/pro...ux/ecalls.html, but that appears to have been an academic research project from 2000-2001. It generated three papers but no apparent software.)

Other thoughts:

The tun(4) driver is a way for userland programs to access the network stack. This might be applicable, but I would think that would only be if your driver is a network driver, and only if the data of interest are incoming or outgoing packets. The bpf(4) device is probably better for that, anyway.

FreeBSD -- you don't mention your OS -- has a kenv(2) call that allows a userland program to inspect and change kernel environment variables. It is in support of the kenv(1) userland utility.
Reply With Quote
  #7   (View Single Post)  
Old 17th August 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,975
Default

One last thing: The only user->kernel communication tool that I use regularly is PF (I'm an OpenBSD user). It has a pseudo device driver, pf(4), which use ioctl(2) syscalls. The userland program is pfctl(8).

So ioctl appears to be the right tool for the job.

Reviewing the source for the driver and the userland program may be helpful.
Reply With Quote
  #8   (View Single Post)  
Old 17th August 2009
sbonar sbonar is offline
New User
 
Join Date: Aug 2009
Posts: 4
Default

The OS is NetBSD.
Reply With Quote
  #9   (View Single Post)  
Old 17th August 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,975
Default

pfctl(8)'s source code can be found here: http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/pfctl/

The pf(4) kernel driver's code can be found in here: http://www.openbsd.org/cgi-bin/cvsweb/src/sys/net/
Reply With Quote
Old 17th August 2009
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Banned
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Default

I can't think of any other solution besides the ones recommended by jggimi.. either way, you're still going to have to poll the kernel in some way.

Perhaps the Berkeley Packet Interface (.. bpf(4)) and http://www.monkey.org/~provos/libevent/?
Reply With Quote
Old 17th August 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 7,975
Default

I'd think kqueue/kevent would be easier to implement.
Reply With Quote
Old 17th August 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

A random thought while I wait for a *huge* rsync to finish:

use a pseudo device file for the driver to expose the information to the user space (assuming it actually does more then harvest information).

write a user-land program to query information through the device file (in terms of user interface: think ps, not top). I personally like a character device, and any kind you can read & write data from

Judging by your words, I assume your user-side program needs to be more like top, a program that regularly refreshes a display of the data; then like ps which just dumps it out ala batch job. If the data can be read from a device file related to your driver, the usual access control model can be used to control access to the data (through that file), and I would assume that you can use any of the available solutions to the "Hey, when there's something to read from this file, give me a shout!" problem. That should range from BSD kqueue()/kevent(), Linux inofity, and other OS-specifics, to the standard select()/poll(), and GOD knows how many libraries and packages; fam, gamin, their source codes, blah blah blah. Personally I would see if kqueue/kevent can be used with performance gains, then use it on systems that support the interface.



Thus, the problem would then be simplified from: how the hell do I send data from a 'kernel driver' to a user space app, down to 0/ implementing the device file and 1/ finding a suitable method of file system notification.




I'm sure the latter one has been done to death and back again; the former should have enough drivers available to examine how it can be done. If the kqueue interface is portable enough for your needs, take a look at the paper on it, check source code for various programs that have the same problem (monitoring a file for change), and take a look see at what options there are.




Call me a fool, a moron, or just a mad and insane geek - but I learned well at an early age, the value of breaking down complex problems into small simple to understand pieces. Living around computers has like wise made me appreciate simple, uniform, and easy to side-step interfaces....
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PF Configuration for newbie slakic OpenBSD Security 1 20th August 2009 02:35 PM
external drive partition question + fdisk question gosha OpenBSD General 15 15th June 2009 02:00 PM
Jails, ezjail, apache, very newbie question. neurosis FreeBSD General 15 23rd August 2008 01:38 PM
Newbie NAT problem TiN-MAN FreeBSD Installation and Upgrading 2 26th June 2008 06:42 AM
Newbie question about ajunta install Johnny2Bad Programming 3 8th June 2008 05:57 PM


All times are GMT. The time now is 05:25 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick