How to restart C daemon program in Linux after receiving SIGHUP signal

前端 未结 4 1912
故里飘歌
故里飘歌 2021-01-12 10:57

Can anybody please post some example code on how I can reread a configuration file and restart my daemon after the daemon receives a SIGHUP signal. The daemon is a user spac

4条回答
  •  误落风尘
    2021-01-12 11:28

    I found this page as I was looking for an example myself to make sure I'm doing it correctly. Since there is no example for this I'll post my attempt and let others comment on it:

    volatile sig_atomic_t g_eflag = 0;
    volatile sig_atomic_t g_hupflag = 1;
    
    static void signal_handler(int sig)
    {
        switch(sig)
        {
        case SIGHUP:
            g_hupflag = 1;
            break;
        case SIGINT:
        case SIGTERM:
            g_eflag = 1;
            break;
        }
    }
    
    int main(int argc, char **argv)
    {
        signal(SIGINT, signal_handler);
        signal(SIGTERM, signal_handler);
        signal(SIGHUP, signal_handler);
        signal(SIGPIPE, SIG_IGN);
    
        while(!g_eflag)
        {
            if(g_hupflag)
            {
                g_hupflag = 0;
                load_config();
            }
    
            // ... do daemon work ...
        }
    
        return 0;
    }
    

提交回复
热议问题