LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-14-2012, 09:42 PM   #1
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Rep: Reputation: Disabled
Question Kernel Module and Userspace communication Signals - SIGUSR1


Hello Everybody,

I am a newbie and wrote this piece of code, I am expecting to receive a message on user space after inserting kernel Module.. But I am not able to receive that message, anybody can please make me correct?

KERNEL MODULE PROGRAM:
Quote:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/signal.h>

int init_module () {
int i=1, res = 4478;
char msg[80];
struct siginfo info;
int pid;
printk(KERN_ALERT"\nHello World..");

info.si_signo = -10;
info.si_errno = 0;
info.si_code = SI_USER;
info.si_pid = current->tgid;
// info.si_uid = current->uid;

send_sig_info(10, 4478, 1); /* 4478 is pid i am getting after running userspace program */
return 0;
}

void cleanup_module () {
printk(KERN_ALERT"\nGoodBye World");
}
USERSPACE PROGRAM:
Quote:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void fun1 (int no) {
putchar (10);
printf("Signal No. %d received.", no);
}

int main () {
int i=1 ;
signal(SIGUSR1, fun1);
printf("My PID is %d.\n",getpid());

while (i);
return 0;
}
Procedure to run:
Compiled userspace
Note down PID
Enter the PID into the kernel code and compile it using Makefile.

expected Output:
Whenever i insert module it must generate signal, and I must receive a message in the shell where i am running userspace program.

But I am not able to receive any message from kernel into userspace.

Can anyone please make me correct?

Sindhu..
 
Old 05-15-2012, 04:47 AM   #2
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932
Quote:
Originally Posted by sindhu4sind View Post
Code:
send_sig_info(10, 4478, 1);
Isn't this the signature of the send_sig_info function?
Code:
int send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
Is your kernel module compiling without any warnings?

Last edited by Aquarius_Girl; 05-15-2012 at 07:32 AM.
 
1 members found this post helpful.
Old 05-15-2012, 10:19 AM   #3
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Anisha Kaul View Post
Isn't this the signature of the send_sig_info function?
Code:
int send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
Is your kernel module compiling without any warnings?
Thank you Anisha..

I told that i am a newbie, yes I am receiving some warnings also:
Quote:
warning: passing argument 2 of ‘send_sig_info’ makes pointer from integer without a cast
expected ‘struct siginfo *’ but argument is of type ‘int’
warning: passing argument 3 of ‘send_sig_info’ makes pointer from integer without a cast
note: expected ‘struct task_struct *’ but argument is of type ‘int’
but i didn't noticed until it was compiling...

but after replacing:
Quote:
int send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
I am still not able to get required output..

Can you please help me to make this program workable? Please...
 
Old 05-15-2012, 10:23 AM   #4
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932
Show up the "exact" new code which you have currently,
then perhaps someone might be able to help.
 
1 members found this post helpful.
Old 05-15-2012, 11:45 AM   #5
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Anisha Kaul View Post
Show up the "exact" new code which you have currently,
then perhaps someone might be able to help.
Thankyou Anisha Kaul.
Here's my code after adopting changes you suggested:
Quote:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/signal.h>

int init_module () {

struct siginfo info;
struct task_struct *task;
int status;


printk(KERN_ALERT"\nHello World..");
int pid;// = 3594;
info.si_signo = SIGUSR1;
info.si_errno = 0;
info.si_code = SI_USER;
info.si_pid = current->tgid;
// info.si_uid = current->uid;

task = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);
status = send_sig_info(SIGUSR1, &info, task);
//send_sig_info(10, 4478, 1); /* 4478 is pid i am getting after running userspace program */

if (0 > status) {
printk("error sending signal\n");
return 0;
}

}

void cleanup_module () {
printk(KERN_ALERT"\nGoodBye World");
}
Still userspace code is same.

I am getting a WARNING again!!
Quote:
warning: ISO C90 forbids mixed declarations and code
Actually I never worked with C language and neither in kernel programing this is first time i just have a little piece of work in which i am stuck. I really need a help, because this is piece of work is hard for me.

actual code is a big Kernel Module implemented in for network communication implemented by someone else, it has multiple files and algorithms implemented. I just have to produce signals on 4 different places and to send those signals on userspace if some conditions occurs..

So thats why i am trying some codes for kernel and userspace communication. but unfortunately i didn't find any working code yet which can report to userspace in some conditions. If you can provide a fully working code for linux kernel and userspace communication Then it will be great help..

P.S: I wrote this kernel code by following steps in this old post: http://kerneltrap.org/node/5800
 
Old 05-15-2012, 11:33 PM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932
Quote:
Originally Posted by sindhu4sind View Post
Code:
task = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);
Would you not need to allocate memory to this pointer
before storing something in it?
Is this the correct way of assignment to the task_struct?
Did you try to insmod the module? What was the result?

Quote:
Originally Posted by sindhu4sind View Post
Actually I never worked with C language
The word never sounds horrible in this case.
You need to know C for any sort of Kernel programming.
 
1 members found this post helpful.
Old 05-16-2012, 12:42 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,237

Rep: Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836
what is going on with init_pid_ns? What is returned by find_pid_ns?
pid_task returns a valid pointer, we do not need to allocate memory.
 
2 members found this post helpful.
Old 05-17-2012, 12:53 AM   #8
sindhu4sind
Member
 
Registered: Apr 2012
Posts: 38

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Anisha Kaul View Post
Would you not need to allocate memory to this pointer
before storing something in it?
Is this the correct way of assignment to the task_struct?
Did you try to insmod the module? What was the result?


The word never sounds horrible in this case.
You need to know C for any sort of Kernel programming.
Quote:
Originally Posted by pan64 View Post
what is going on with init_pid_ns? What is returned by find_pid_ns?
pid_task returns a valid pointer, we do not need to allocate memory.

Thank you so much Anisha Kaul & pan64
I have achieved what i wanted to achieve, here is my cod:
KERNEL CODE
Quote:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
#include <linux/signal.h>
MODULE_LICENSE ("GPL");
MODULE_AUTHOR (“Raheel A. Memon<raheel@mju.ac.kr>”);
MODULE_DESCRIPTION (“Testing signals from kernel to userspace”);
struct siginfo sinfo;
pid_t pid;
struct task_struct *task;
int init_module () {
memset(&sinfo, 0, sizeof(struct siginfo));
sinfo.si_signo = SIGIO;
sinfo.si_code = SI_USER;
pid = 5218; // Everytime a new PID
//task = find_task_by_vpid(pid); // I am also working on new and old version of UBUNTU so thats why this is here
task = pid_task(find_vpid(pid), PIDTYPE_PID);
printk("%d .\n", task);
if(task == NULL) {
printk("Cannot find PID from user program\r\n");
return 0;
}
send_sig_info(SIGIO, &sinfo, task);
return 0;
}
void cleanup_module () {
printk(KERN_ALERT"\nGoodBye World\n\n");
}
Userspace Program:
Quote:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
void signal_handler (int signum){
if (signum == SIGIO) printf ("SIGIO\r\n"); return;
}
int main () {
int i=1 ;
signal(SIGIO, signal_handler);
printf("My PID is %d.\n",getpid());
while (i);
return 0;
}
This is working fine, in my condition what i have needed to do.
But i have 2 more questions:
1. What if I want to transfer a Message from kernel to userspace
2. here I am assigning everytime new PID to my program, what should i do that kernel can get PID from userspace by itself.

I am very thankful for your support.
I am looking for your response you both are really nice that supported me though i was not speaking so technically, neither my programs was so much technical.
 
Old 05-17-2012, 01:28 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,237

Rep: Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836Reputation: 6836
never mind, I'm glad you could make it.
in general a process can store its own pid in a file and you can identify the program by that (you can find such files for example in /var/run).

http://www.linuxquestions.org/questi...cation-592494/
http://people.ee.ethz.ch/~arkeller/l...ace_howto.html






Happy with solution ... mark as SOLVED
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
 
1 members found this post helpful.
Old 05-17-2012, 04:25 AM   #10
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932Reputation: 932
Quote:
Originally Posted by sindhu4sind View Post
What if I want to transfer a Message from kernel to userspace
There are functions named "copy_from_user"
and "copy_to_user" provided in the
kernel API documentation.

BTW, thanks for posting the full code.
It'll be helpful to those looking for
the same solutions.

I also request that in future put your
code in [CODE] tags rather than [QUOTE]
tags.

Last edited by Aquarius_Girl; 05-17-2012 at 04:28 AM.
 
  


Reply

Tags
kernel module, pid, signal, signal handler, userspace


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mmap kernel <-> userspace dasalam Linux - Kernel 2 08-08-2011 02:57 PM
Problem with reading (in userspace) from a kernel module AlexM25 Linux - Kernel 7 07-08-2010 11:17 AM
Userspace Executable are kernel independent? Harini1111 Linux - Kernel 2 12-05-2008 10:26 AM
Using kernel functions and macros in userspace karimasif Linux - General 1 01-16-2008 11:34 PM
inter module communication misbahuddin Programming 1 01-04-2006 01:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration