How to send a signal from inside the kernel?

Aziz KEZZOU opensource.enthousiat at gmail.com
Thu Mar 17 11:55:13 PST 2005


> Aziz KEZZOU wrote this message on Thu, Mar 17, 2005 at 12:34 -0500:
> > Hi all,
> > I would like to send a signal (e.g SIGUSR1) to a user process from
> > inside the kernel (kld module).
> > Can any one tell me how to do it ?
> > I tried the following code inspired from sys/kern/kern_sig.c :
> > ==============================================================
> > #include <sys/types.h>
> > #include <sys/signal.h>
> >
> > int process_pid;
> > struct kill_args {
> >       int     pid;
> >       int     signum;
> > };
> >
> > void send_SIGUSR1() {
> >   struct kill_args uap;
> >   uap.pid = process_pid;
> >   uap.signum = SIGUSR1;
> >   kill((struct thread *)0, &uap);
> > }
> > ===============================================================
> >
> > but that causes a page fault in kernel mode (ie. Kernel panic :-)
> >
> > Any help is appreciated, thanks.
> 
> Take a look at psignal(9)...  You'll need to look up the struct proc
> for psignal with pfind(9)...   and then PROC_UNLOCK the struct proc
> after you've used psignal...
> 
> so:
> struct proc *p;
> 
> p = pfind(pid);
> if (p != NULL) {
>         psignal(p, SIGUSR1);
>         PROC_UNLOCK(p);
> }
> 
> I haven't tried the code above, but that should do what you want...

It works, thanks a lot !!
Here are the headers needed in case someone reads this thread:

#include <unistd.h> /*needed only for NULL, can be removed*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/proc.h> 

Have fun,
Aziz


More information about the freebsd-hackers mailing list