os_result os_signalHandlerFinishExitRequest( os_callbackArg arg) { os_result r = os_resultSuccess; int sig = (int)(os_address)arg.sig; os_sigaction * xo; /* This is a request from the application to round up an (asynchronous) * exit-request. */ if (sig < 1 || sig >= OS_NSIG){ OS_REPORT(OS_WARNING, "os_signalHandlerFinishExitRequest", 0, "Callback-arg contains invalid signal, value out of range 1-%d: arg = %d", OS_NSIG, sig); r = os_resultInvalid; } else if (sigismember(&quitsMask, sig) == 0){ #if OS_NSIG >= 100 #error "Worst-case allocation assumes max. signal of 99, which apparently is not correct" #endif /* We know which signal-number exist, all take at most 2 digits + ", ", * so allocate worst-case 4 * quits_len */ char *expected = os_malloc(quits_len * 4 + 1); if(expected){ unsigned i; int pos; assert(quits_len > 0); pos = sprintf(expected, "%d", quits[0]); for(i = 1; i < quits_len; i++){ pos += sprintf(expected + pos, ", %d", quits[i]); } } OS_REPORT(OS_WARNING, "os_signalHandlerFinishExitRequest", 0, "Unexpected Signal, value out of range: signal = %d. Expected one of %s.", sig, expected ? expected : "the asynchronous exit request signals"); os_free(expected); r = os_resultInvalid; } if(r == os_resultSuccess){ /* We need to restore the original signal-handler and than re-raise the * original signal. */ xo = &old_signalHandler[sig]; if(os_sigactionSet(sig, xo, NULL) != 0){ OS_REPORT(OS_WARNING, "os_signalHandlerFinishExitRequest", 0, "Resetting original signal handler for signal %d failed, sigaction did not return 0.",sig); abort(); /* We were unable to reset the original handler, which is pretty serious. */ } else { os_sigset current_sigset, old_sigset; /* Determine the current mask, and make sure that the signal to be * raised is not blocked (this code is typically executed in the * signalHandlerThread (if callback is implemented synchronously), * which blocks all signals). */ os_sigThreadSetMask(NULL, ¤t_sigset); os_sigsetDel(¤t_sigset, sig); raise(sig); /* Set mask temporarily, this should raise the pending signal set above. */ os_sigThreadSetMask(¤t_sigset, &old_sigset); /* Reset the mask to the original state. If this is the signal- * HandlerThread this is essential (if sig is handled), otherwise just * the proper thing to do. */ os_sigThreadSetMask(&old_sigset, NULL); } } return r; }
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; }
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; }