示例#1
0
os_result
os_signalHandlerSetHandler(
    os_signal signal,
    os_actionHandler handler)
{
    os_sigset mask;
    os_result result;
    os_sigaction action;
    int r;

    /* block all signals during handling of a signal */
    result = os_sigsetFill(&mask);
    if (result != os_resultSuccess)
    {
        OS_REPORT_2(OS_ERROR,
                    "os_signalHandlerInit", 0,
                    "os_sigsetFill(0x%x) failed, result = %d",
                    &action.sa_mask, result);
    } else
    {
        action = os_sigactionNew(handler, &mask, SA_SIGINFO);
    }
    if (result == os_resultSuccess)
    {
        r = os_sigsetDel(&action.sa_mask, signal);
        if (r < 0)
        {
            OS_REPORT_3(OS_ERROR,
                        "os_signalHandlerInit", 0,
                        "os_sigsetDel(0x%x, %d) failed, result = %d",
                        &action, signal, r);
            result = os_resultFail;
            assert(OS_FALSE);
        }
    }
    if (result == os_resultSuccess)
    {
        r = os_sigactionSet(signal, &action, &old_signalHandler[signal]);
        if (r < 0) {
            OS_REPORT_4(OS_ERROR,
                        "os_signalHandlerInit", 0,
                        "os_sigactionSet(%d, 0x%x, 0x%x) failed, result = %d",
                        signal, &action, &old_signalHandler[signal], r);
            result = os_resultFail;
            assert(OS_FALSE);
        }
    }
    return result;
}
示例#2
0
static os_result
os_signalHandlerInit(
    os_signalHandler _this)
{
    os_result result = os_resultSuccess;
    os_sigset block_all_sigset, old_sigset;
    unsigned i;
    int r;
    os_threadAttr thrAttr;

    assert(_this);

    _this->handleExceptions = OS_FALSE;

    if(os__signalHandlerCallbackInit(&_this->callbackInfo) != os_resultSuccess) {
        goto err_callbackInfoInit;
    }

    if(!isSignallingSafe(1)){
        return os_resultSuccess;
    }

    /* Initialise the exceptionsMask */
    sigemptyset(&exceptionsMask);
    for(i = 0; i < lengthof(exceptions); i++){
        sigaddset(&exceptionsMask, exceptions[i]);
    }

    /* Initialise the quitsMask */
    sigemptyset(&quitsMask);
    for(i = 0; i < quits_len; i++){
        sigaddset(&quitsMask, quits[i]);
    }

    /* create signal handling pipes */
    r = pipe(_this->pipeIn);
    if (r < 0) {
        OS_REPORT(OS_ERROR, "os_signalHandlerInit", 0, "pipe(_this->pipeIn) failed, result = %d", r);
        goto err_pipeIn;
    }

    r = fcntl(_this->pipeIn[0], F_SETFD, 1);
    if (r < 0) {
        OS_REPORT(OS_WARNING, "os_signalHandlerInit", 0, "fcntl(_this->pipeIn[0]) failed, result = %d", r);
        goto err_pipeInFcntl;
    }

    r = fcntl(_this->pipeIn[1], F_SETFD, 1);
    if (r < 0) {
        OS_REPORT(OS_WARNING, "os_signalHandlerInit", 0, "fcntl(_this->pipeIn[1]) failed, result = %d", r);
        goto err_pipeInFcntl;
    }

    r = pipe(_this->pipeOut);
    if (r < 0) {
        OS_REPORT(OS_ERROR, "os_signalHandlerInit", 1, "pipe(_this->pipeOut) failed, result = %d", r);
        goto err_pipeOut;
    }

    r = fcntl(_this->pipeOut[0], F_SETFD, 1);
    if (r < 0) {
        OS_REPORT(OS_WARNING, "os_signalHandlerInit", 0, "fcntl(_this->pipeOut[0]) failed, result = %d", r);
        goto err_pipeOutFcntl;
    }

    r = fcntl(_this->pipeOut[1], F_SETFD, 1);
    if (r < 0) {
        OS_REPORT(OS_WARNING, "os_signalHandlerInit", 0, "fcntl(_this->pipeOut[1]) failed, result = %d", r);
        goto err_pipeOutFcntl;
    }

    /* Block all signals in order to start the signalHandlerThread with all
     * signals blocked. */
    result = os_sigsetFill(&block_all_sigset);
    if (result != os_resultSuccess) {
        OS_REPORT(OS_ERROR, "os_signalHandlerInit", 0,
                "os_sigsetFill(&block_all_sigset) failed: %s", os_resultImage(result));
        goto err_sigsetMask;
    }

    /* Remove signals that cannot be blocked. */
    for (i = 0; i < lengthof(excludes); i++) {
        const int sig = excludes[i];
        if (os_sigsetDel(&block_all_sigset, sig) != 0) {
            OS_REPORT(OS_ERROR, "os_signalHandlerInit", 0, "os_sigsetDel(0x%"PA_PRIxADDR", %d) failed, result = %d",
                    (os_address)&_this->action, sig, r);
            goto err_sigsetMask;
        }
    }

    result = os_sigThreadSetMask(&block_all_sigset, &old_sigset);
    if (result != os_resultSuccess) {
        OS_REPORT(OS_ERROR, "os_signalHandlerInit", 0, "os_sigThreadSetMask(0x%"PA_PRIxADDR", 0x%"PA_PRIxADDR") failed: %s",
                    (os_address)&block_all_sigset, (os_address)&old_sigset, os_resultImage(result));
        goto err_sigsetMask;
    }

    /* Setup signal handler thread. */
    os_threadAttrInit(&thrAttr);
    thrAttr.stackSize = 4*1024*1024; /* 4 MB */
    result = os_threadCreate(&_this->threadId,
                             "signalHandler",
                             &thrAttr,
                             signalHandlerThread,
                             (void*)_this);

    if (result != os_resultSuccess) {
        OS_REPORT(OS_ERROR, "os_signalHandlerInit", 0,
                    "os_threadCreate(0x%"PA_PRIxADDR", 0x%"PA_PRIxADDR",0x%"PA_PRIxADDR",0) failed: %s",
                    (os_address)&_this->threadId,
                    (os_address)&thrAttr,
                    (os_address)signalHandlerThread,
                    os_resultImage(result));
        goto err_threadCreate;
    }

    /* Reset signal mask to original value. */
    result = os_sigThreadSetMask(&old_sigset, NULL);
    if (result != os_resultSuccess) {
        OS_REPORT(OS_ERROR, "os_signalHandlerInit", 0,
                    "os_sigThreadSetMask(0x%"PA_PRIxADDR", NULL) failed: %s",
                    (os_address)&old_sigset, os_resultImage(result));
        goto err_sigResetMask;
    }

    /* install signal handlers */
    _this->action = os_sigactionNew(signalHandler, &block_all_sigset, SA_SIGINFO);

    if (os_signalHandlerEnableExitSignals() != os_resultSuccess) {
        goto err_enableExitHandling;
    }

    return os_resultSuccess;

/* Error handling */
err_enableExitHandling:
err_sigResetMask:
    os__signalHandlerThreadStop(_this);
err_threadCreate:
    (void) os_sigThreadSetMask(&old_sigset, NULL);
err_sigsetMask:
    /* No undo needed for fcntl's/sigsetFill */
err_pipeOutFcntl:
    (void) close(_this->pipeOut[0]);
    (void) close(_this->pipeOut[1]);
err_pipeOut:
    /* No undo needed for fcntl's */
err_pipeInFcntl:
    (void) close(_this->pipeIn[0]);
    (void) close(_this->pipeIn[1]);
err_pipeIn:
    os__signalHandlerCallbackDeinit(&_this->callbackInfo);
err_callbackInfoInit:
    return os_resultFail;
}
示例#3
0
static os_result
os_signalHandlerInit(
    os_signalHandler _this)
{
    os_result result = os_resultSuccess;
    os_sigaction action;
    os_sigset block_all_sigset, old_sigset;
    int i, r;

    if (_this == NULL) {
        result = os_resultFail;
    }
    _this->exitRequestCallback = (os_signalHandlerExitRequestCallback)0;
    _this->exceptionCallback = (os_signalHandlerExceptionCallback)0;


    if(isSignallingSafe(1)) {
        /* Initialise the exceptionsMask */
        sigemptyset(&exceptionsMask);
        for(i = 0; i < lengthof(exceptions); i++) {
            sigaddset(&exceptionsMask, exceptions[i]);
        }

        /* Initialise the quitsMask */
        sigemptyset(&quitsMask);
        for(i = 0; i < lengthof(quits); i++) {
            sigaddset(&quitsMask, quits[i]);
        }

        /* create signal handling pipes */
        if (result == os_resultSuccess) {
            r = pipe(_this->pipeIn);
            if (r<0) {
                OS_REPORT_1(OS_ERROR,
                            "os_signalHandlerInit", 0,
                            "pipe(_this->pipeIn) failed, result = %d",
                            r);
                result = os_resultFail;
            } else {
                r = fcntl(_this->pipeIn[0], F_SETFD, 1);
                if (r<0) {
                    OS_REPORT_1(OS_WARNING,
                                "os_signalHandlerInit", 0,
                                "fcntl(_this->pipeIn[0]) failed, result = %d", r);
                    assert(OS_FALSE);
                }
                r = fcntl(_this->pipeIn[1], F_SETFD, 1);
                if (r<0) {
                    OS_REPORT_1(OS_WARNING,
                                "os_signalHandlerInit", 0,
                                "fcntl(_this->pipeIn[1]) failed, result = %d", r);
                    assert(OS_FALSE);
                }
            }
        }
        if (result == os_resultSuccess) {
            r = pipe(_this->pipeOut);
            if (r<0) {
                OS_REPORT_1(OS_ERROR,
                            "os_signalHandlerInit", 1,
                            "pipe(_this->pipeOut) failed, result = %d",
                            r);
                result = os_resultFail;
            } else {
                r = fcntl(_this->pipeOut[0], F_SETFD, 1);
                if (r<0) {
                    OS_REPORT_1(OS_WARNING,
                                "os_signalHandlerInit", 0,
                                "fcntl(_this->pipeOut[0]) failed, result = %d",
                                r);
                    assert(OS_FALSE);
                }
                r = fcntl(_this->pipeOut[1], F_SETFD, 1);
                if (r<0) {
                    OS_REPORT_1(OS_WARNING,
                                "os_signalHandlerInit", 0,
                                "fcntl(_this->pipeOut[1]) failed, result = %d",
                                r);
                    assert(OS_FALSE);
                }
            }
        }
        /* Block all signals */
        if (result == os_resultSuccess) {
            result = os_sigsetFill(&block_all_sigset);
            if (result != os_resultSuccess) {
                OS_REPORT_1(OS_ERROR,
                            "os_signalHandlerInit", 0,
                            "os_sigsetFill(&block_all_sigset) failed, result = %d",
                            r);
                assert(OS_FALSE);
            } else {
                result = os_sigThreadSetMask(&block_all_sigset, &old_sigset);
                if (result != os_resultSuccess) {
                    OS_REPORT_3(OS_ERROR,
                                "os_signalHandlerInit", 0,
                                "os_sigThreadSetMask(0x%x, 0x%x) failed, result = %d",
                                &block_all_sigset, &old_sigset, r);
                    assert(OS_FALSE);
                }
            }
        }
        /* Setup signal handler thread. */
        if (result == os_resultSuccess) {
            os_threadAttr thrAttr;

            result = os_threadAttrInit(&thrAttr);
            if (result != os_resultSuccess) {
                OS_REPORT_2(OS_ERROR,
                            "os_signalHandlerInit", 0,
                            "pthread_attr_init(0x%x) failed, result = %d",
                            &thrAttr, r);
                assert(OS_FALSE);
            } else {
                thrAttr.stackSize = 4*1024*1024; /* 4 MB */
                result = os_threadCreate(&_this->threadId,
                                         "signalHandler",
                                         &thrAttr,
                                         signalHandlerThread,
                                         (void*)_this);

                if (result != os_resultSuccess) {
                    OS_REPORT_4(OS_ERROR,
                                "os_signalHandlerInit", 0,
                                "os_threadCreate(0x%x, 0x%x,0x%x,0) failed, result = %d",
                                &_this->threadId,
                                &thrAttr,
                                signalHandlerThread,
                                result);
                    assert(OS_FALSE);
                }
            }
        }
        /* Reset signal mask to original value. */
        if (result == os_resultSuccess) {
            result = os_sigThreadSetMask(&old_sigset, NULL);
            if (result != os_resultSuccess) {
                OS_REPORT_2(OS_ERROR,
                            "os_signalHandlerInit", 0,
                            "os_sigThreadSetMask(0x%x, NULL) failed, result = %d",
                            &old_sigset, r);
                result = os_resultFail;
                assert(OS_FALSE);
            }
        }
        /* install signal handlers */
        if (result == os_resultSuccess) {
            os_sigset mask;
            /* block all signals during handling of a signal */
            result = os_sigsetFill(&mask);
            if (result != os_resultSuccess) {
                OS_REPORT_2(OS_ERROR,
                            "os_signalHandlerInit", 0,
                            "os_sigsetFill(0x%x) failed, result = %d",
                            &action.sa_mask, result);
            } else {
                action = os_sigactionNew(signalHandler, &mask, SA_SIGINFO);
            }
            if (result == os_resultSuccess) {
                for (i=0; i<lengthof(exceptions); i++) {
                    const int sig = exceptions[i];
                    r = os_sigsetDel(&action.sa_mask, sig);
                    if (r<0) {
                        OS_REPORT_3(OS_ERROR,
                                    "os_signalHandlerInit", 0,
                                    "os_sigsetDel(0x%x, %d) failed, result = %d",
                                    &action, sig, r);
                        result = os_resultFail;
                        assert(OS_FALSE);
                    }
                }
            }
            if (result == os_resultSuccess) {
                for (i=0; i<lengthof(exceptions); i++) {
                    const int sig = exceptions[i];
                    /* For exceptions we always set our own signal handler, since
                     * applications that cause an exception are not in a position
                     * to ignore it. However, we will chain the old handler to our
                     * own.
                     */
                    r = os_sigactionSet(sig, &action, &old_signalHandler[sig]);
                    if (r < 0) {
                        OS_REPORT_4(OS_ERROR,
                                    "os_signalHandlerInit", 0,
                                    "os_sigactionSet(%d, 0x%x, 0x%x) failed, result = %d",
                                    sig, &action, &old_signalHandler[sig], r);
                        result = os_resultFail;
                        assert(OS_FALSE);
                    }
                }
            }
            if (result == os_resultSuccess) {
                for (i=0; i<lengthof(quits); i++) {
                    const int sig = quits[i];
                    /* By passing NULL we only retrieve the currently set handler. If
                     * the signal should be ignored, we don't do anything. Otherwise we
                     * chain the old handler to our own.
                     * man-page of sigaction only states behaviour when new
                     * action is non-NULL, but all known implementations act as
                     * expected. That is: return the currently set signal-hand-
                     * ler (and not the previous, as the man-pages state).
                     * NOTE: Not MT-safe */
                    r = os_sigactionSet(sig, NULL, &old_signalHandler[sig]);
                    if (r == 0) {
                        if(old_signalHandler[sig].sa_handler != SIG_IGN) {
                            /* We don't know if the oldHandler has been modified in the mean
                             * time, since there is no way to make the signal handler reentrant.
                             * It doesn't make sense to look for modifications now, since a
                             * new modification could be on its way while we are processing
                             * the current modification.
                             * For that reason we will ignore any intermediate modifications
                             * and continue setting our own handler. Processes should therefore
                             * refrain from modifying the signal handler in multiple threads.
                             */
                            r = os_sigactionSet(sig, &action, NULL);
                            if (r != 0) {
                                OS_REPORT_4(OS_ERROR,
                                            "os_signalHandlerInit", 0,
                                            "os_sigactionSet(%d, 0x%x, 0x%x) failed, result = %d",
                                            sig, &action, &old_signalHandler[sig], r);
                                result = os_resultFail;
                                assert(OS_FALSE);
                            }
                        } else {
                            OS_REPORT_1(OS_INFO,
                                        "os_signalHandlerThread", 0,
                                        "Not installing a signal handler for the already ignored signal %d.",
                                        sig);
                        }
                    } else {
                        OS_REPORT_1(OS_ERROR, "os_signalHandlerInit", 0, "Could not retrieve currently set signal-handler for signal %d", sig);
                        result = os_resultFail;
                        assert(OS_FALSE);
                    }
                }
            }
        }
    } else {
        result = os_resultSuccess;
    }
    return result;
}