1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#ifndef WINDOWS
#include "sigint.h" // def.h
#include <sys/types.h>
#include <signal.h>
#include "btcontent.h"
#include "tracker.h"
#include "peerlist.h"
#include "btconfig.h"
#include "console.h"
extern "C" {
RETSIGTYPE sig_catch(int sig_no)
{
if(SIGINT == sig_no || SIGTERM == sig_no){
Tracker.ClearRestart();
Tracker.SetStoped();
signal(sig_no,sig_catch2);
}
}
static RETSIGTYPE sig_catch2(int sig_no)
{
if(SIGINT == sig_no || SIGTERM == sig_no){
if( cfg_cache_size ) BTCONTENT.FlushCache();
BTCONTENT.SaveBitfield();
WORLD.CloseAll();
signal(sig_no,SIG_DFL);
raise(sig_no);
}
}
// Handler for other signals
RETSIGTYPE signals(int sig_no)
{
return CONSOLE.Signal(sig_no);
}
} // extern "C"
#endif
void sig_setup()
{
#ifndef WINDOWS
signal(SIGPIPE,SIG_IGN);
signal(SIGINT,sig_catch);
signal(SIGTERM,sig_catch);
// Don't let printf restart a write after SIGTTOU, we will hard-loop!
struct sigaction handler;
handler.sa_handler = signals;
sigemptyset(&(handler.sa_mask));
handler.sa_flags = 0; // SA_RESTART is not set
sigaction(SIGTTOU, &handler, (struct sigaction *)0);
// Likewise with input after SIGTTIN
sigaction(SIGTTIN, &handler, (struct sigaction *)0);
signal(SIGCONT,signals);
signal(SIGTSTP,signals);
#endif
}
|