分享

linux内核中的信号机制

 lchjczw 2013-04-14

linux内核中的信号机制--信号发送

Kernel version:2.6.14

CPU architecture:ARM920T

Author:ce123(http://blog.csdn.net/ce123)


应用程序发送信号时,主要通过kill进行。注意:不要被“kill”迷惑,它并不是发送SIGKILL信号专用函数。这个函数主要通过系统调用sys_kill()进入内核,它接收两个参数:

第一个参数为目标进程id,kill()可以向进程(或进程组),线程(轻权线程)发送信号,因此pid有以下几种情况:

  • pid>0:目标进程(可能是轻权进程)由pid指定。
  • pid=0:信号被发送到当前进程组中的每一个进程。
  • pid=-1:信号被发送到任何一个进程,init进程(PID=1)和以及当前进程无法发送信号的进程除外。
  • pid<-1:信号被发送到目标进程组,其id由参数中的pid的绝对值指定。
第二个参数为需要发送的信号。

由于sys_kill处理的情况比较多,分析起来比较复杂,我们从tkill()函数入手,这个函数把信号发送到由参数指定pid指定的线程(轻权进程)中。tkill的内核入口是sys_tkill(kernel/signal.c),其定义如下:

  1. /*  
  2.  *  Send a signal to only one task, even if it's a CLONE_THREAD task.  
  3.  */  
  4. asmlinkage long  
  5. sys_tkill(int pid, int sig)  
  6. {  
  7.     struct siginfo info;  
  8.     int error;  
  9.     struct task_struct *p;  
  10.   
  11.     /* This is only valid for single tasks */  
  12.     if (pid <= 0)//对参数pid进行检查  
  13.         return -EINVAL;  
  14.   
  15.     info.si_signo = sig; //根据参数初始化一个siginfo结构  
  16.     info.si_errno = 0;  
  17.     info.si_code = SI_TKILL;  
  18.     info.si_pid = current->tgid;  
  19.     info.si_uid = current->uid;  
  20.   
  21.     read_lock(&tasklist_lock);  
  22.     p = find_task_by_pid(pid);//获取由pid指定的线程的task_struct结构  
  23.     error = -ESRCH;  
  24.     if (p) {  
  25.         error = check_kill_permission(sig, &info, p);//权限检查  
  26.         /*  
  27.          * The null signal is a permissions and process existence  
  28.          * probe.  No signal is actually delivered.  
  29.          */  
  30.         if (!error && sig && p->sighand) {  
  31.             spin_lock_irq(&p->sighand->siglock);  
  32.             handle_stop_signal(sig, p);  
  33.             //对某些特殊信号进程处理,例如当收到SIGSTOP时,需要把信号队列中的SIGCONT全部删除  
  34.             error = specific_send_sig_info(sig, &info, p);//把信号加入到信号队列  
  35.             spin_unlock_irq(&p->sighand->siglock);  
  36.         }  
  37.     }  
  38.     read_unlock(&tasklist_lock);  
  39.     return error;  
  40. }  

sys_tkill函数主要是通过pecific_send_sig_info()函数实现的,下面我们看一下pecific_send_sig_info()(kernel/signal.c)的定义:

  1. static int  
  2. specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)  
  3. {  
  4.     int ret = 0;  
  5.   
  6.     if (!irqs_disabled())  
  7.         BUG();  
  8.     assert_spin_locked(&t->sighand->siglock);  
  9.   
  10.     if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))  
  11.         /*  
  12.          * Set up a return to indicate that we dropped the signal.  
  13.          */  
  14.         ret = info->si_sys_private;  
  15.     /*信号被忽略*/  
  16.     /* Short-circuit ignored signals.  */  
  17.     if (sig_ignored(t, sig))  
  18.         goto out;  
  19.   
  20.     /* Support queueing exactly one non-rt signal, so that we  
  21.        can get more detailed information about the cause of  
  22.        the signal. */  
  23.     if (LEGACY_QUEUE(&t->pending, sig))  
  24.         goto out;  
  25.   
  26.     ret = send_signal(sig, info, t, &t->pending);//实际的发送工作  
  27.     if (!ret && !sigismember(&t->blocked, sig))  
  28.         signal_wake_up(t, sig == SIGKILL);  
  29. out:  
  30.     return ret;  
  31. }  
首先调用sig_ignored检查信号是否被忽略,然后检查发送的信号是不是普通信号,如果是普通信号,就需要根据信号位图来检查当前信号队列中是否已经存在该信号,如果已经存在,对于普通信号不需要做任何处理。然后调用send_signal来完成实际的发送工作,send_signal()是信号发送的重点,除sys_tkill之外的函数,最终都是通过send_signal()来完成信号的发送工作的。

这里注意到想send_signal()传递的参数时t->pending,也就是连接Private Signal Queue的那条链。最后,如果发送成功就调用signal_wake_up()来唤醒目标进程,这样可以保证该进程进入就绪状态,从而有机会被调度执行信号处理函数。

现在我们来看看send_signal()(kernel/signal.c)函数,这个函数的主要工作就是分配并初始化一个sigqueue结构,然后把它添加到信号队列中。

  1. static int send_signal(int sig, struct siginfo *info, struct task_struct *t,  
  2.             struct sigpending *signals)  
  3. {  
  4.     struct sigqueue * q = NULL;  
  5.     int ret = 0;  
  6.   
  7.     /*  
  8.      * fast-pathed signals for kernel-internal things like SIGSTOP  
  9.      * or SIGKILL.  
  10.      */  
  11.     if ((unsigned long)info == 2)  
  12.         goto out_set;  
  13.   
  14.     /* Real-time signals must be queued if sent by sigqueue, or  
  15.        some other real-time mechanism.  It is implementation  
  16.        defined whether kill() does so.  We attempt to do so, on  
  17.        the principle of least surprise, but since kill is not  
  18.        allowed to fail with EAGAIN when low on memory we just  
  19.        make sure at least one signal gets delivered and don't  
  20.        pass on the info struct.  */  
  21.   
  22.     q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&  
  23.                          ((unsigned long) info < 2 ||  
  24.                           info->si_code >= 0)));//分配sigqueue结构  
  25.     if (q) {//如果成功分配到sigqueue结构,就把它添加到队列中,并对其初始化  
  26.         list_add_tail(&q->list, &signals->list);  
  27.         switch ((unsigned long) info) {  
  28.         case 0:  
  29.             q->info.si_signo = sig;  
  30.             q->info.si_errno = 0;  
  31.             q->info.si_code = SI_USER;  
  32.             q->info.si_pid = current->pid;  
  33.             q->info.si_uid = current->uid;  
  34.             break;  
  35.         case 1:  
  36.             q->info.si_signo = sig;  
  37.             q->info.si_errno = 0;  
  38.             q->info.si_code = SI_KERNEL;  
  39.             q->info.si_pid = 0;  
  40.             q->info.si_uid = 0;  
  41.             break;  
  42.         default:  
  43.             copy_siginfo(&q->info, info);//拷贝sigqueue结构  
  44.             break;  
  45.         }  
  46.     } else {  
  47.         if (sig >= SIGRTMIN && info && (unsigned long)info != 1  
  48.            && info->si_code != SI_USER)  
  49.         /*  
  50.          * Queue overflow, abort.  We may abort if the signal was rt  
  51.          * and sent by user using something other than kill().  
  52.          */  
  53.             return -EAGAIN;  
  54.         if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))  
  55.             /*  
  56.              * Set up a return to indicate that we dropped   
  57.              * the signal.  
  58.              */  
  59.             ret = info->si_sys_private;  
  60.     }  
  61.   
  62. out_set:  
  63.     sigaddset(&signals->signal, sig);//设置信号位图  
  64.     return ret;  
  65. }  
从上面的分析可以看出,我们看到信号被添加到信号队列之后,会调用signal_wake_up()唤醒这个进程,signal_wake_up()(kernel/signal.c)的定义如下:

  1. /*  
  2.  * Tell a process that it has a new active signal..  
  3.  *  
  4.  * NOTE! we rely on the previous spin_lock to  
  5.  * lock interrupts for us! We can only be called with  
  6.  * "siglock" held, and the local interrupt must  
  7.  * have been disabled when that got acquired!  
  8.  *  
  9.  * No need to set need_resched since signal event passing  
  10.  * goes through ->blocked  
  11.  */  
  12. void signal_wake_up(struct task_struct *t, int resume)  
  13. {  
  14.     unsigned int mask;  
  15.   
  16.     set_tsk_thread_flag(t, TIF_SIGPENDING);//为进程设置TIF_SIGPENDING标志  
  17.   
  18.     /*  
  19.      * For SIGKILL, we want to wake it up in the stopped/traced case.  
  20.      * We don't check t->state here because there is a race with it  
  21.      * executing another processor and just now entering stopped state.  
  22.      * By using wake_up_state, we ensure the process will wake up and  
  23.      * handle its death signal.  
  24.      */  
  25.     mask = TASK_INTERRUPTIBLE;  
  26.     if (resume)  
  27.         mask |= TASK_STOPPED | TASK_TRACED;  
  28.     if (!wake_up_state(t, mask))  
  29.         kick_process(t);  
  30. }  
signal_wake_up()首先为进程设置TIF_SIGPENDING标志,说明该进程有延迟的信号要等待处理。然后再调用wake_up_state()唤醒目标进程,如果目标进程在其他的CPU上运行,wake_up_state()将返回0,此时调用kick_process()向该CPU发送一个处理器间中断。当中断返回前戏,会为当前进程处理延迟的信号。

此后当该进程被调度时,在进程返回用户空间前,会调用do_notify_resume()处理该进程的信号。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多