LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-22-2010, 05:19 AM   #1
haha
LQ Newbie
 
Registered: Apr 2010
Posts: 1

Rep: Reputation: 0
How to send a signal from a module to a process


Hi,
I am writing a kernel module (2.6.31-20). I want it to send a signal to a user-space process. I have a PID of that process. The code is like this:

Code:
struct task_struct *ts;
ts = find_vpid(process_pid);
force_sig(2, ts);
I have inserted debugging printks after each line of the code. The first two lines are executed, but the last one is not and I get a "Killed" message.

Since I am a newbie to kernel module programming I would appreciate some hint of what could be wrong and what I have to do to make it work.

Thanks.
 
Old 04-22-2010, 02:55 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
You can use kill_proc_info function to send signal from kernel to user space.
 
Old 04-22-2010, 11:41 PM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Have a look at below link :
http://people.ee.ethz.ch/~arkeller/l..._howto.html#s6
 
Old 04-23-2010, 11:46 AM   #4
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
You can use send_sig()

send_sig(int sig, struct task_struct *p, int priv)
 
Old 04-26-2010, 09:39 AM   #5
akuehn
LQ Newbie
 
Registered: Apr 2010
Distribution: debian, ubuntu, buildroot
Posts: 5

Rep: Reputation: 0
Hi!

I'm currently stuck at the same problem with kernel 2.6.32 and I followed the suggested links and here is the resulting procedure in my module.
The required process id is provided via ioctl from the associated_process.

But, I can't get the thing compiled! Actually, the find_task_by_pid_ns call is the problem. Obviously, some link is missing but which?

WARNING: "find_task_by_pid_ns" [mypath/and/modulename.ko] undefined!



static void sendSignal(void)
{
struct siginfo info;
struct task_struct *task;
int status;

/* is there a process we can send to? */
if (-1 == associated_process)
return;

task = find_task_by_pid_ns(associated_process, &init_pid_ns);
if (!task) {
printk("error finding task\n");
return;
}
info.si_signo = SIGPWR;
info.si_code = SI_QUEUE;
info.si_errno = 0; /* no recovery */

status = send_sig_info(SIGPWR, &info, task); //send the signal
if (0 > status) {
printk("error sending signal\n");
return;
}
}


Any other mistakes are not considered by now ;-)
 
Old 04-26-2010, 11:32 PM   #6
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Hi akuehn,

Have a look at the following link:
http://tomoyo.sourceforge.jp/cgi-bin...task_by_pid_ns

Here see the kernel version (top left) for which the function is defined
Version: linux-2.6.34

Last edited by Aquarius_Girl; 04-27-2010 at 01:35 AM.
 
Old 04-27-2010, 05:49 AM   #7
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
As I have said, use send_sig(), more something like this

in your kernel space driver:

struct task_struct *tsk;


your_ioctl()
{
...
case INFORM_SIG:
tsk = current;
...
}

your_send_sig_func()
{
send_sig(SIGPWR, tsk, 0);
}

Just check the signal that you want to send has the same signal number in user space, because some signal numbers are differently mapped in kernel space and user space.

Regards,
archieval
 
Old 04-27-2010, 09:04 AM   #8
akuehn
LQ Newbie
 
Registered: Apr 2010
Distribution: debian, ubuntu, buildroot
Posts: 5

Rep: Reputation: 0
Question

Hi!

Thanks for the replies...

Anyway, I don't understand how to get the task_struct for my user process which has to receive the signal?
Addressing send_sig to the current task sends the signal to the module thread itself. Or do I have to walk through the whole thread structure, looking for the right pid?

Cheers
akuehn
 
Old 04-27-2010, 02:22 PM   #9
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
In the ioctl code, the "current" variable, predefined global variable in kernel space, is your user process.

your_ioctl()
{
...
case INFORM_SIG:
tsk = current;
...
}
 
Old 04-28-2010, 01:08 AM   #10
archieval
Member
 
Registered: Apr 2007
Location: Philippines
Distribution: Kubuntu, Ubuntu, CentOS
Posts: 289

Rep: Reputation: 41
The "current" task will be the currently executing task "at that time" when you registered or called the ioctl command before you send signal to it. You save it to a variable so that it will not refer to the "current" task that will send the signal.

Regards,
archieval
 
Old 04-28-2010, 02:48 AM   #11
akuehn
LQ Newbie
 
Registered: Apr 2010
Distribution: debian, ubuntu, buildroot
Posts: 5

Rep: Reputation: 0
Thumbs up

Hey! That is it!

I didn't know that the macro "current" returns the task that is currently calling the ioctl. Thus, it is not necessary to actually provide the pid of the calling process via ioctl. It is simply the call itself.

And now it works as intended.

Thanks to all of you :-)
And have a nice day

akuehn
 
  


Reply



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
how to send events from kernel module to user space process yugandhar Linux - Kernel 2 02-21-2011 10:39 AM
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
Not able to send the signal through browser pandy047 Linux - Software 1 12-15-2009 12:18 PM
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 03:10 AM
trying to send signal from linux module andreiij Programming 1 05-02-2005 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 11:45 AM.

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