简体   繁体   中英

'kill -9 process ID' signal handler

I develop a program which use tcp socket in the http server for communication with clients. I use the following code to create the http server:

void http_server_init(void)
{
    struct sockaddr_in server;
    int cr_port;

    for(;;) {
        cr_port = conf.port;
        int i = (DEFAULT_PORT == cr_port)? 1 : 0;
        //Create socket
        cr_socket_desc = socket(AF_INET , SOCK_STREAM , 0);
        if (cr_socket_desc == -1)
        {
            LOG (ERROR,"Could not open server socket, Error no is : %d, Error description is : %s", errno, strerror(errno));
            sleep(1);
            continue;
        }

        /* enable SO_REUSEADDR */
        int reusaddr = 1;
        if (setsockopt(cr_socket_desc, SOL_SOCKET, SO_REUSEADDR, &reusaddr, sizeof(int)) < 0) {
            LOG (WARNING,"setsockopt(SO_REUSEADDR) failed");
        }

        //Prepare the sockaddr_in structure
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        for(;;i++) {
            server.sin_port = htons(cr_port);
            //Bind
            if( bind(cr_socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
            {
                //print the error message
                LOG (ERROR,"Could not bind server socket on the port %d, Error no is : %d, Error description is : %s", cr_port, errno, strerror(errno));
                cr_port = DEFAULT_PORT + i;
                LOG (INFO,"Trying to use another port: %d", cr_port);
                continue;
            }
            break;
        }
        break;
    }
    LOG (INFO,"server initiated with the port: %d", cr_port);
}

And then I close the socket if the program exit.

I use the socket close function if my program is stopped normally (/etc/init.d/myprog stop)

close(cr_socket_desc);

but when stopped my program with kill -9 PID of my program I use the following code :

    void signal_handler(int signal_num)
    {
        close(cr_socket_desc);
        _exit;
    }
    void main()
    {

     ....

    struct sigaction act;
    memset(&act, 0, sizeof(act));
    act.sa_handler = signal_handler;
    sigaction(SIGINT,  &act, 0);
    sigaction(SIGTERM, &act, 0);
    sigaction(SIGKILL,  &act, 0);
    sigaction(SIGSTOP, &act, 0);

   ......

   }

But the socket is not closed since it was taken by another service.

The question is how handler the kill -9 signal and how catch it ?

Kill -9 sends a SIGKILL, which you cannot catch or ignore. (See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html ).

You also cannot block SIGKILL using sigprocmask(). Any attempt to do so will be ignored.

The fact that you cannot catch these SIGKILL means your program will never have a chance to run any code at all when SIGKILL is received. SIGKILL is a way for the OS to kill your process with extreme prejudice.

You do not say why you are using kill -9 to kill your process. If that is something you are doing on your own, you should try sending a different signal (SIGTERM would be fine, which is what kill sends by default, without any argument specifying a different signal number). That will invoke your signal handler and allow you to close your socket cleanly.

If your program is really stuck and you really do need to use kill -9 to get rid of it, there is simply no way to perform any clean shutdown.

You cannot catch or ignore these signals,

SIGKILL and SIGSTOP

So for this you can block that signals using the sigprocmask() .

int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM