LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Communicate between kernel and user space (https://www.linuxquestions.org/questions/linux-newbie-8/communicate-between-kernel-and-user-space-943290/)

sindhu4sind 05-04-2012 11:39 AM

Communicate between kernel and user space
 
Hello Everyone,

I am newbie in Linux and also with c language, but i am working on a project. so i have to do it :(

I am unknown of making communication between kernel module and user space..

I have got a kernel module, and implemented some of my scenarios.
* If any network card fails then i get the notification (This is working for kernel module)
Here is a snapshot for kernel module where i am able to get the process id:
Quote:

struct task_struct *id;
...
...
...
...
void hbmRxDo (void){

int nRemoteId, nLocalHostId;
unsigned char nNodeAvailable, bNetIsSplitted = 1;

..
..

if (nNodeAvailable > 0)
{
// Update Data-Path from PORT_A to PORT_A
if (pHbmList[nRemoteId].pathAA <= 0) {
gNetInfo.pConn[nRemoteId] &= 0xFE;
printk ("\t[%d].PORT_A <--> [%d].PORT_A is not available\n", nLocalHostId, nRemoteId);
id = get_current();
printk("Kernel PID : %d", id);
...
..
}
Now I am not sure either this is proper way or not. but i am able to get pid for each time failure in communication..

Now i have to do some tasks and i have no idea about this:

1. I need to transfer that information (for which i got the process id) to Userspace
2. After receiving the information on userspace i will transmit that information on to the user space of the other NODE via TCP/IP or UDP/IP.

Any suggestions or any example resembling to this scenario will be very much help full for me..

Thank you for your time in advance..

Sindhu

pan64 05-05-2012 04:02 PM

So you are not familiar with linux, with c and with kernel.
Just a hint: the kernel cannot communicate itself with the userspace, but a userspace program can ask the kernel to provide some information. So you need to write a c program which will use this kernel module to get info.

sindhu4sind 05-06-2012 04:15 AM

Thank you pan64.. Not so much familiar with that kind of programming, can you please provide me some example?

sindhu4sind 05-06-2012 04:18 AM

i guess kill_proc() is the function which can share the pid

Quote:

kill_proc(id, SIGUSR1, 1);
If this function is carrying a signal to user space, then how to capture it on userspace? using ioctl!? but don't know the procedure!

---------- Post added 05-06-12 at 06:19 PM ----------

Any example will be really a good help if you can share..

pan64 05-06-2012 04:24 AM

here is an example: http://www.ibm.com/developerworks/li...-system-calls/
You have your own kernel module, you just need to look at the part related to syscall

sindhu4sind 05-06-2012 09:27 PM

Thank you pan6. It seems helpful.. :)

Aquarius_Girl 05-09-2012 08:03 AM

Quote:

Originally Posted by sindhu4sind (Post 4670453)
I am unknown of making communication between kernel module and user space..

There are functions named "copy_from_user" and
"copy_to_user" provided in the "kernel API documentation".

sindhu4sind 05-09-2012 09:22 AM

Quote:

Originally Posted by Anisha Kaul (Post 4674226)
There are functions named "copy_from_user" and
"copy_to_user" provided in the "kernel API documentation".

Thank you anisha kaul..
I actually developed further a userspace daemon also.
Here is my another post about that, with lots of confusion to communicate betweeen userspace and kernel using signals:
http://www.linuxquestions.org/questi...kernel-943991/


I am not a good programmer of C and also not so much familiar with Linux programming. everything there is new for me, i have just assigned a task by my professor which i am trying to complete.

Make me correct if i am creating wrong code also try to make it complete or give me some related example:
Quote:

/*Virtual Interface*/

int vif_ioctl (struct net_device* dev, struct ifreq *ifr, int cmd)
{
int ErrCode = 0;
struct pid_t pid;

struct host_info *pHostInfo, *uHostInfo;
struct net_info *pNetInfo, *uNetInfo;

switch (cmd) {
case SIOCSAFEDEVGETINFO:
/* some code */
break;
case SIOCSAFENETGETINFO:
/* some code */
case SIOCSAFEGETPIDINFO:
printk ("Get PID Info was callled. \n");
copy_from_user(pid, ?? , ??) //////////////////What i need to give arguments here in this function/////
break;
}
further in the kernel module following code is also implemented:
Quote:

..
static pid_t id;
int result;
..
int network_response()
{
...
...
if (x = 1)
{
printk("Failure of network.")
result = kill_proc(id, SIGUSR1,1);
printk("\nReporting failure detection %d\n", result);

}
if (x = 2)
{
printk("Failure of Node is detected.")
result = kill_proc(id, SIGUSR1,1);
printk("\nReporting failure detection %d\n", result);

}
}
I am confused that how kernel space will capture SIGUSR1 signals from userspace
May be my all discussion is not so much technical, or there you can find many mistakes in code. But pardon me I am not a technical person with this kind of stuff..
If any sample code or suggestions you have then I will be happy to hear that..

pan64 05-09-2012 10:00 AM

the signal handler is part of the user space code. The kernel only sends the signal to the process, but it is handled by the process itself.
kill_proc will send a signal and you need to implement a signal handler to catch it. see here: http://www.gnu.org/software/libc/man...-Handling.html (and defining handlers)


All times are GMT -5. The time now is 02:35 PM.