📄 signalimp.c
字号:
/** The central clearinghouse for JavaSignals. Copyright 1998 Kevin Hester, kevinh@acm.org Commerical use prohibited - non commercial distribution is OK. Contact the author for further information. I just added some fixes from bablokb@gmx.net 3/29/99 - his comments below:I am just writing an article in the German Linux-Magazin aboutRMI and I thought I would use JavaSignals to make my serverbehave nicely (mainly react to SIGHUP and SIGTERM), and alsoto spread the news about a great Java package. But JavaSignalsbreaks RMI (the method Naming.rebind(...) never returned onceSignalManager.add() was called).The reason is simple: the rmiregistry uses signals tocommunicate with the java program and JavaSignals resets allsignals to SIG_DFL, exept those I'm listening for.I changed the code to only call sigaction for these signals, butnow sleep(...) would also return on signals I don't care about.So some additional changes were necessary. In fact, the code isstill not clean, it would need some sort of semaphore, but itshould do for normal cases. I included the new SignalImp.c.One additional change is necessary in SignalManagerThread.java:you must add a "setPriority(4);" before calling start();. Thisis one piece of mystery: neither green nor native threads have apriority model implemented, but if a thread uses the console andanother thread has the same priority, the threads block eachother. Note: setting the priority below 3 will actually disablethe thread (at least on my Linux systems, NT works fine, but NThas a native thread model with priorities).Bernhard*/#include <assert.h>#include "kh_signal_SignalManager.h"#include <stdio.h>#include <signal.h>static unsigned oldMask = 0;static int caughtSig = 0, isOurSig = 0;static struct sigaction oldactions[_NSIG];/** This is our master signal handler*/static void sigHandler(int signal){ // printf("Caught a signal %d\n", signal); caughtSig = signal; isOurSig = 1;}static struct sigaction newaction = { sigHandler, 0, 0, 0 };static unsigned getMask(JNIEnv *env, jclass _class){ jfieldID id = (*env)->GetStaticFieldID(env, _class, "interestMask", "I"); assert(id); return (*env)->GetStaticIntField(env, _class, id);}JNIEXPORT jint JNICALL Java_kh_signal_SignalManager_waitForSignal (JNIEnv *env, jclass _class){ while (1) { // Wait up to a year - we are really waiting for a signal int numleft = sleep(60 * 60 * 24 * 365); // printf("Sleep expired\n"); if(numleft == 0) printf("This shouldn't happen - timer expired\n"); else if (isOurSig) { isOurSig = 0; return caughtSig; } }}JNIEXPORT void JNICALL Java_kh_signal_SignalManager_setInterestMask (JNIEnv *env, jclass _class){ unsigned newMask = getMask(env, _class); unsigned changed = newMask ^ oldMask; int bit, result, mask, installUs; // printf("SignalImp: setting mask to 0x%x, old mask 0x%x\n", newMask, oldMask); for(bit = 0; bit < 32; bit++) { mask = (1 << bit); if(changed & mask) { installUs = newMask & mask; // FIXME - we should only save the old action the FIRST time // we do an install if (installUs) { // printf("Setting signal %d\n", bit); result = sigaction(bit,&newaction,(void *) NULL); } if(result < 0) printf("Error setting signal %d\n", bit); } } // printf("SignalImp: mask set\n"); oldMask = newMask;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -