除去通常的netlink機制之外,本文介紹兩種內核主動發起與用戶層的交互機制。類似ioctl、proc文件操作或者socket類等用戶層發起的與內核交互操作不在此討論。
內核調用用戶層應用程序,使用函數call_usermodehelper實現,其參數爲用戶層應用程序的目錄path,需要傳遞的參數argv、環境參數envp和參數wait。最後一個參數wait可指定是否需要等待應用程序執行完成,設置爲UMH_NO_WAIT(0)表示不等待。
int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
{
struct subprocess_info *info;
gfp_t gfp_mask = (wait == UMH_NO_WAIT) ? GFP_ATOMIC : GFP_KERNEL;
info = call_usermodehelper_setup(path, argv, envp, gfp_mask,
NULL, NULL, NULL);
return call_usermodehelper_exec(info, wait);
}
另外一種是內核通過send_sig函數發送指定的信號給應用程序。這個使用是有前提的,因爲其第二個參數task_struct爲要給其發送信號的應用程序的task結構體,如果知道應用程序運行時的進程ID號,在內核中可通過函數find_task_by_vpid獲取到其對應的task結構。
struct task_struct *find_task_by_vpid(pid_t vnr)
{
return find_task_by_pid_ns(vnr, task_active_pid_ns(current));
}
int send_sig(int sig, struct task_struct *p, int priv)
{
return send_sig_info(sig, __si_special(priv), p);
}