内核:使用sigqueue函数

时间:2016-04-09 18:28:59

标签: c linux-kernel signals linux-device-driver kernel-module

我尝试实现从可加载内核模块到用户应用程序的信号调用。该应用程序使用sigaction来处理信号调用,之后它将使用sival_int来处理switchcase的不同操作。

void signalHandler_function (int signum,
                             siginfo_t *siginfo,
                             void *ucontext)
{
   printf("signum: %i\n", signum);
   printf("sigval: %d\n", siginfo->si_value.sival_int);
}

int main(){
   int ret;
   int pid = getpid();
   char pidc[4];
   struct sigaction sig;

   memcpy(pidc,&pid,4);

   // configure signal handler
   sig.sa_flags = SA_SIGINFO;
   sig.sa_sigaction = signalHandler_function;
   sigaction(SIGIO, &sig, NULL);

   g_fdCharDev = open(EXPDEV_DEVPATHNAME, O_RDWR);

   printf("My process ID : %d\n", pid);
   ret = write(g_fdCharDev, &pid, 1);
   ...
}

在内核方面,我尝试使用send_sigqueue()(与sigqueue_alloc()sigqueue_free()一起使用)。这在include/linux/sched.h, line 2320中声明为extern并在kernel/signal.c, line 1560中定义。但是链接器说这个函数是未定义的:

make -C /lib/modules/3.19.0-58-generic/build/ M=/home/alex/git/Kernel3/SignalHandling modules
make[1]: Verzeichnis »/usr/src/linux-headers-3.19.0-58-generic« wird betreten
  CC [M]  /home/alex/git/Kernel3/SignalHandling/ExpDev.o
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "sigqueue_free" [/home/alex/git/Kernel3/SignalHandling/ExpDev.ko] undefined!
WARNING: "sigqueue_alloc" [/home/alex/git/Kernel3/SignalHandling/ExpDev.ko] undefined!
WARNING: "send_sigqueue" [/home/alex/git/Kernel3/SignalHandling/ExpDev.ko] undefined!
  CC      /home/alex/git/Kernel3/SignalHandling/ExpDev.mod.o
  LD [M]  /home/alex/git/Kernel3/SignalHandling/ExpDev.ko
make[1]: Verzeichnis »/usr/src/linux-headers-3.19.0-58-generic« wird verlassen
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

以下是LKM的缩短来源:

#include <linux/init.h>   /// Macros used to mark up functions e.g. __init __exit
#include <linux/module.h> /// Core header for loading LKMs into the kernel
#include <linux/device.h> /// Header to support the kernel Driver Model
#include <linux/kernel.h> /// Contains types, macros, functions for the kernel
#include <linux/fs.h>     /// Header for the Linux file system support
#include <asm/uaccess.h>  /// Required for the copy to user function
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/rcupdate.h>
#include <linux/slab.h>

struct siginfo *g_sig_info;
static struct sigqueue *g_sigqueue;
int g_usp_pid;
struct task_struct *g_sig_task;

static int __init expdev_init(void)
{
  ...
  // setup signal
  g_sigqueue = sigqueue_alloc();
  g_sigqueue->info.si_signo = SIGIO;
  g_sigqueue->info.si_signo = SI_QUEUE;
  g_sigqueue->info.si_errno = 0;
  ...
}

static void __exit expdev_exit(void)
{
  sigqueue_free(g_sigqueue);
  ...
}

static ssize_t dev_write(struct file *filep,
                         const char *buffer,
                         size_t len,
                         loff_t *offset)
{
  ...
  memcpy(&g_usp_pid,buffer,4); // we know the PID comes with the buffer

  // find task to the given PID
  rcu_read_lock();
  g_sig_task = pid_task(find_pid_ns(g_usp_pid, &init_pid_ns),
                        PIDTYPE_PID);

  //send signal to user land
  g_sigqueue->info.si_value.sival_int = 33;
  ret = send_sigqueue(g_sigqueue, g_sig_task, 0);
  ...
}

为什么它不可链接?我的做法错了吗?

1 个答案:

答案 0 :(得分:0)

为内核模块导出的任何有用的list_head或函数都可以在System.map文件中找到。

$ sudo grep sigqueue /boot/System.map-3.19.0-58-generic
ffffffff81084880 T sigqueue_alloc
ffffffff810848b0 T sigqueue_free
ffffffff81084920 T send_sigqueue

此函数指针地址可用于本地函数:

#include <linux/init.h>   /// Macros used to mark up functions e.g. __init __exit
#include <linux/module.h> /// Core header for loading LKMs into the kernel
#include <linux/device.h> /// Header to support the kernel Driver Model
#include <linux/kernel.h> /// Contains types, macros, functions for the kernel
#include <linux/fs.h>     /// Header for the Linux file system support
#include <asm/uaccess.h>  /// Required for the copy to user function
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/rcupdate.h>
#include <linux/slab.h>

struct siginfo *g_sig_info;
static struct sigqueue *g_sigqueue;
int g_usp_pid;
struct task_struct *g_sig_task;

// ffffffff81084920 T send_sigqueue
int (*send_sigqueue_ptr)( struct sigqueue *,
                          struct task_struct *,
                          int group) = (void*)0xffffffff81084920;
// ffffffff81084880 T sigqueue_alloc
struct sigqueue *(*sigqueue_alloc_ptr)(void) = (void*)0xffffffff81084880;
// ffffffff810848b0 T sigqueue_free
void (*sigqueue_free_ptr)(struct sigqueue *) = (void*)0xffffffff810848b0;

static int __init expdev_init(void)
{
  ...
  // setup signal
  g_sigqueue = sigqueue_alloc_ptr();
  g_sigqueue->info.si_signo = SIGIO;
  g_sigqueue->info.si_code  = SI_QUEUE;
  g_sigqueue->info.si_errno = 0;
  ...
}

static void __exit expdev_exit(void)
{
  sigqueue_free_ptr(g_sigqueue);
  ...
}

static ssize_t dev_write(struct file *filep,
                         const char *buffer,
                         size_t len,
                         loff_t *offset)
{
  ...
  memcpy(&g_usp_pid,buffer,4); // we know the PID comes with the buffer

  // find task to the given PID
  rcu_read_lock();
  g_sig_task = pid_task(find_pid_ns(g_usp_pid, &init_pid_ns),
                        PIDTYPE_PID);

  //send signal to user land
  g_sigqueue->info.si_value.sival_int = 33;
  ret = send_sigqueue_ptr(g_sigqueue, g_sig_task, 0);
  ...
}

这仅适用于此特殊内核版本,并且不是非常便携。如果有任何宏或其他解决方法确实获得此地址或功能?这是编译kernel/signal.c的好方法吗?

如果有人知道改善这种情况的方法。请随时评论和编辑。