信号在android源码/external/pppd 源码项目中的应用解读分析

来源:互联网 发布:java 读取apk版本号 编辑:程序博客网 时间:2024/06/06 01:25

pppoe里面也有信号的处理的方式,

直接上源码:

static voidsetup_signals(){    struct sigaction sa;    /*     * Compute mask of all interesting signals and install signal handlers     * for each.  Only one signal handler may be active at a time.  Therefore,     * all other signals should be masked when any handler is executing.     */    sigemptyset(&signals_handled);    sigaddset(&signals_handled, SIGHUP);    sigaddset(&signals_handled, SIGINT);    sigaddset(&signals_handled, SIGTERM);    sigaddset(&signals_handled, SIGCHLD);    sigaddset(&signals_handled, SIGUSR2);    #define SIGNAL(s, handler)do { \sa.sa_handler = handler; \if (sigaction(s, &sa, NULL) < 0) \    fatal("Couldn't establish signal handler (%d): %m", s); \    } while (0)    sa.sa_mask = signals_handled;    sa.sa_flags = 0;    SIGNAL(SIGHUP, hup);/* Hangup */    SIGNAL(SIGINT, term);/* Interrupt */    SIGNAL(SIGTERM, term);/* Terminate */    SIGNAL(SIGCHLD, chld);    SIGNAL(SIGUSR1, toggle_debug);/* Toggle debug flag */    SIGNAL(SIGUSR2, open_ccp);/* Reopen CCP */    /*     * Install a handler for other signals which would otherwise     * cause pppd to exit without cleaning up.     */    SIGNAL(SIGABRT, bad_signal);    SIGNAL(SIGALRM, bad_signal);    SIGNAL(SIGFPE, bad_signal);    SIGNAL(SIGILL, bad_signal);    SIGNAL(SIGPIPE, bad_signal);    SIGNAL(SIGQUIT, bad_signal);    SIGNAL(SIGSEGV, bad_signal);    /*     * Apparently we can get a SIGPIPE when we call syslog, if     * syslogd has died and been restarted.  Ignoring it seems     * be sufficient.     */    signal(SIGPIPE, SIG_IGN);}

这里面和之前的dhcp以及dnsmasq不一样,单纯信号函数接口和对应的hangler处理

上面对每一种有单独的处理方式,分别来看。

/* * hup - Catch SIGHUP signal. * * Indicates that the physical layer has been disconnected. * We don't rely on this indication; if the user has sent this * signal, we just take the link down. */static voidhup(sig)    int sig;{    /* can't log a message here, it can deadlock */    got_sighup = 1;    if (conn_running)/* Send the signal to the [dis]connector process(es) also */kill_my_pg(sig);    notify(sigreceived, sig);    if (waiting)siglongjmp(sigjmp, 1);}
这里面由一个重要的知识点,siglongjump,需要后面补充下!

着重讨论下面的case:用户主动关闭这个进程的操作,和前面是一样的。

/* * term - Catch SIGTERM signal and SIGINT signal (^C/del). * * Indicates that we should initiate a graceful disconnect and exit. *//*ARGSUSED*/static voidterm(sig)    int sig;{    /* can't log a message here, it can deadlock */    got_sigterm = sig;    if (conn_running)/* Send the signal to the [dis]connector process(es) also */kill_my_pg(sig);    notify(sigreceived, sig);    if (waiting)siglongjmp(sigjmp, 1);}
下面主要为了保持同步而接收的信号,

/* * chld - Catch SIGCHLD signal. * Sets a flag so we will call reap_kids in the mainline. */static voidchld(sig)    int sig;{    got_sigchld = 1;    if (waiting)siglongjmp(sigjmp, 1);}

加一个标志位,表示子进程已经退出,需要主进程做相应的动作。


原创粉丝点击