triggering user space with kernel

前端 未结 3 744
感动是毒
感动是毒 2021-01-20 18:25

I need to send a string from kernel to a user space function without asking for it in particular from the user space, sort of triggering a function or application in the use

3条回答
  •  感动是毒
    2021-01-20 18:47

    Here's how my process works, I would be interested in any suggestions for improvements as well:

    1. Start the kernel module
    2. Start user space application, which sends a custom command to the kernel module to register the user space PID for kernel module signals. In my case this was via a write to /dev/mymodule. The kernel module registers the PID:

      ...
      printk("registering a new process id to receive signals: %d\n", current->pid);
      signal_pid = current->pid;
      ...
      

      The user space application also registers a handler with the kernel for certain types of signals.

      void local_sig_handler(int signum) {
              printf("received a signal from my module\n");
              fflush(stdout); }
      ...
      signal(SIGIO, local_sig_handler);
      
    3. Kernel module generates a signal

      ...
      struct siginfo info;
      struct task_struct *t;
      info.si_signo=SIGIO;
      info.si_int=1;
      info.si_code = SI_QUEUE;        
      
      printk("<1>IRQ received: %d\n", irq);
      printk("<1>searching for task id: %d\n", signal_pid);
      
      t= pid_task(find_vpid(signal_pid),PIDTYPE_PID);//user_pid has been fetched successfully
      
      if(t == NULL){
              printk("<1>no such pid, cannot send signal\n");
      } else {
              printk("<1>found the task, sending signal\n");
              send_sig_info(SIGIO, &info, t);
      }
      
    4. Kernel relays the signal to the application's handler

提交回复
热议问题