triggering user space with kernel

前端 未结 3 706
感动是毒
感动是毒 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:35

    An example I used in the past was to send signal to user space from hardware interrupt in kernel space.

    KERNEL SPACE

    You have to prepare siginfo and task_struct before sending a signal:

    struct siginfo info;
    struct task_struct *t;
    
    info.si_signo = SIG_TEST;
    info.si_code = SI_QUEUE;
    info.si_int = 1234; // Any value you want to send
    rcu_read_lock();
    

    And also find the task with user space application PID. You have to send it from user-space to kernel-space through write or ioctl operations.

    t = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);
    

    Then you can send the signal.

    rcu_read_unlock();      
    send_sig_info(SIG_TEST, &info, t);
    

    I omitted here, but you must check the result of every operation.

    The previous code prepare the signal structure and send it. Bear in mind that you need the application's PID. In my case the application from user space send its PID through ioctl driver procedure.

    USER SPACE

    You have to define and implement the callback function:

    void signalFunction(int n, siginfo_t *info, void *unused) {
          .....
          .....  
    }
    

    In main procedure:

    struct sigaction sig;
    
    sig.sa_sigaction = signalFunction; // Callback function
    sig.sa_flags = SA_SIGINFO;
    sigaction(SIG_TEST, &sig, NULL);
    

    I hope it helps.

提交回复
热议问题