sigaction的处理程序没有关闭过程 [英] sigaction handler does not close the process

查看:186
本文介绍了sigaction的处理程序没有关闭过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的sigaction处理code

I have the following sigaction handler code

void signal_term_handler(int sig)
{
    int rc = async_lockf(pid_file, F_UNLCK);
    if(rc) {
        char piderr[] = "PID file unlock failed!\n";
        write(STDOUT_FILENO, piderr, (sizeof(piderr))-1);
    }
    close(pid_file);
    char exitmsg[] = "EXIT Daemon:TERM signal Received!\n";
    write(STDOUT_FILENO, exitmsg, (sizeof(exitmsg))-1);
    _exit(EXIT_SUCCESS); //async-signal-save exit
}

在上面的函数的所有函数调用是一个异步信号保存。即使是 async_lockf()是一个异步信号保存:

All the function calls in the above function are an async-signal-save. Even the async_lockf() is an async-signal-save:

/*
 * The code of async_lockf is copied from eglibc-2.11.3/io/lockf.c
 * The lockf.c is under the terms of the GNU Lesser General Public
 * Copyright (C) 1994,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
 * This file is part of the GNU C Library.
*/

int async_lockf(int fd, int cmd)
{
    struct flock fl = {0};

    /* async_lockf is always relative to the current file position.  */
    fl.l_whence = SEEK_CUR;
    fl.l_start = 0;
    fl.l_len = 0;

    switch (cmd)
    {
        case F_TEST:
            /* Test the async_lock: return 0 if FD is unlocked or locked by this process;
             return -1, set errno to EACCES, if another process holds the lock.  */
            fl.l_type = F_RDLCK;
            if (fcntl (fd, F_GETLK, &fl) < 0)
                return -1;
            if (fl.l_type == F_UNLCK || fl.l_pid == getpid ())
                return 0;
            errno = EACCES;
            return -1;

        case F_ULOCK:
            fl.l_type = F_UNLCK;
            cmd = F_SETLK;
            break;
        case F_LOCK:
            fl.l_type = F_WRLCK;
            cmd = F_SETLK;
            break;
        case F_TLOCK:
            fl.l_type = F_WRLCK;
            cmd = F_SETLK;
            break;

        default:
            errno = EINVAL;
            return -1;
    }

    /* async_lockf() is a cancellation point but so is fcntl() if F_SETLKW is
     used.  Therefore we don't have to care about cancellation here,
     the fcntl() function will take care of it.  */
    return fcntl (fd, cmd, &fl);
}

如果我执行杀-15 命令,但有些时候我得到的突运行,不会退出的sigaction的处理程序应关闭应用程序。这种情况很少见。例如,如果我启动应用程序,然后我停下来与杀-15 1000次,这种行为将只发生〜5倍。

the sigaction handler should close the application if I execute kill -15 command but some times I get the processus running and does not exit. this happens rarely. For example If I launch the application and then I stopped with kill -15 1000 times, this behaviour will happens only ~5 times

任何的解释这种奇怪的行为?为什么我的应用程序不存在?特别是我米使用异步信号保存功能( _exit())关闭突

Any explaination for this strange behaviour? Why my application does not exist? Especially that I m using async-signal-save function (_exit()) to close the processus

推荐答案

要看到发生了什么,尝试连接 strace的 GDB 来的过程,并看到它卡住了。我最好的猜测是,你有code,它的屏蔽信号( sigprocmask )在执行阻塞操作,从而$ ​​P $运行pventing信号处理程序。

To see what's happening, try attaching strace or gdb to the process and to see where it's stuck. My best guess is that you have code that's masking signals (sigprocmask) while performing a blocking operation, thus preventing the signal handler from running.

这篇关于sigaction的处理程序没有关闭过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆