Linux中守护进程无法停止

3
我使用了以下指南(http://peterlombardo.wikidot.com/linux-daemon-in-c),它运行得很好,但无法停止。

Main.cpp

//Global Directives
//#define DEBUG 1
#define DAE_NAME "dae"
#define DAE_PID "/var/run/dae.pid"

//Includes
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <syslog.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>

//Namespace (System)
using namespace std;

//Classes
#include "class.h"

//Structures
#include "struct/struct.h"

//Functions
#include "function/signal_handler.h"

void usage(int argc, char *argv[]) {
    if (argc >=1) {
        printf("Usage: %s -h -nn", argv[0]);
        printf("  Options:n");
        printf("      -ntDon't fork off as a daemon.n");
        printf("      -htShow this help screen.n");
        printf("n");
    }
}

int main(int argc, char *argv[]) {

#if defined(DEBUG)
    int daemonize = 0;
#else
    int daemonize = 1;
#endif

    // Setup signal handling before we start
    signal(SIGHUP, signal_handler);
    signal(SIGTERM, signal_handler);
    signal(SIGINT, signal_handler);
    signal(SIGQUIT, signal_handler);

    //Print Usage Information
    int c;
    while((c = getopt(argc, argv, "nh|help")) != -1) {
        switch(c){
            case 'h':
             usage(argc, argv);
                exit(0);
                break;
            case 'n':
                daemonize = 0;
                break;
            default:
             usage(argc, argv);
                exit(0);
                break;
        }
    }

    // Setup syslog logging
#if defined(DEBUG)
    setlogmask(LOG_UPTO(LOG_DEBUG));
    openlog(DAE_NAME, LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
#else
    setlogmask(LOG_UPTO(LOG_INFO));
    openlog(DAE_NAME, LOG_CONS, LOG_USER);
#endif

    //------------
    //Daemon Setup
    //------------
    pid_t pid, sid;

    if (daemonize) {
        syslog(LOG_INFO, "Starting %s", DAE_NAME);

        /* Fork off the parent process */
        pid = fork();
        if (pid < 0) {
            exit(EXIT_FAILURE);
        }
        /* If we got a good PID, then
           we can exit the parent process. */
        if (pid > 0) {
            exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);

        /* Create a new SID for the child process */
        sid = setsid();
        if (sid < 0) {
            /* Log the failure */
            exit(EXIT_FAILURE);
        }

        /* Change the current working directory */
        if ((chdir("/")) < 0) {
            /* Log the failure */
            exit(EXIT_FAILURE);
        }

        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }

    int count = 1;

    //-------
    //Process
    //-------
    while (1){
        process();

        syslog (LOG_INFO, "%s Processed(%s, %d)", DAE_NAME, getlogin(), count);

        count++;
    }

    syslog (LOG_INFO, "Exiting Daemon %s", DAE_NAME);

    closelog (); //Close the Connection

    exit(EXIT_SUCCESS);
}

singal_handler.h

#ifndef SIGNAL_HANDLER_H_
#define SIGNAL_HANDLER_H_

void signal_handler(int sig) {

    switch(sig) {
        case SIGHUP:
            syslog(LOG_WARNING, "Received SIGHUP signal.", DAE_NAME);
            break;
        case SIGTERM:
            syslog(LOG_WARNING, "Received SIGTERM signal.", DAE_NAME);
            break;
        default:
            syslog(LOG_WARNING, "Unhandled signal ", DAE_NAME);
            break;
    }
}

#endif

命令行

在命令行中,我运行以下命令:

./dae

然后我运行:

kill procid

日志

信息:开始 dae

信息:dae 处理((null), 1)

信息:dae 处理((null), 2)

信息:dae 处理((null), 3)

警告:收到 SIGTERM 信号。

信息:dae 处理((null), 4)

信息:dae 处理((null), 5)

这个该死的东西停不下来?有什么想法吗,谢谢。

1个回答

5

由于您正在处理 TERM 信号,因此其默认行为已被禁用。

您需要在信号处理程序中显式调用 exit() 才能终止程序。

以下是一种实现方式,它允许清理并且不使用 atexit()

signal_handler.h

#ifndef SIGNAL_HANDLER_H_
#define SIGNAL_HANDLER_H_

extern bool g_terminated;

void signal_handler(int sig) {

    switch(sig) {
        case SIGHUP:
            syslog(LOG_WARNING, "Received SIGHUP signal.", DAE_NAME);
            break;
        case SIGTERM:
            syslog(LOG_WARNING, "Received SIGTERM signal.", DAE_NAME);
            g_terminated = true;
            break;
        default:
            syslog(LOG_WARNING, "Unhandled signal ", DAE_NAME);
            break;
    }
}

#endif

main.cpp

bool g_terminated = false;

int main(int argc, char *argv[])
{
    .
    .
    .

    //-------
    //Process
    //-------
    while (!g_terminated) {
        process();

        syslog (LOG_INFO, "%s Processed(%s, %d)", DAE_NAME, getlogin(), count);

        count++;
    }

    // Cleanup...

    exit(EXIT_SUCCESS);
}

但是如果我想在 while 循环之后执行清理操作呢? - Angel.King.47
你可以使用 atexit() 注册一个退出函数。 - Frédéric Hamidi
如果我使用signal(..)函数来设置一个变量,我可以使用它来跳出while循环。 - Angel.King.47
添加了一个全局变量 int shutdown = 0,在 signal_handler 开关中断前将其设置为 shutdown = 1。这样主循环就会中断并在主循环下执行清理操作。感谢 Frederic,你帮我找到了这个解决方案。 :D - Angel.King.47
1
@Angel.King.47,刚刚用一个g_terminated布尔变量做了同样的事情 :) - Frédéric Hamidi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接