Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 21 additions & 33 deletions procServ.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ void writeInfoFile(const std::string& infofile);
void ttySetCharNoEcho(bool save);

// Signal handlers
static void OnSigPipe(int);
static void OnSigTerm(int);
static void OnSigHup(int);

// Flags used for communication between sig handler and main()
static volatile sig_atomic_t sigPipeSet;
static volatile sig_atomic_t sigTermSet;
static volatile sig_atomic_t sigHupSet;

Expand Down Expand Up @@ -475,38 +473,41 @@ int main(int argc,char * argv[])
memset(&sig, 0, sizeof(sig));

PRINTF("Installing signal handlers\n");
// SIGPIPE, SIGTERM and SIGHUP will be handled in the main loop

// SIGTERM and SIGHUP will be handled in the main loop
// with the assistance of pselect. This means that we have them
// blocked outside of pselect call, but unblocked atomically
// within pselect. Each time pselect returns, we safely check if
// any of the signals were received.

// Block the signals that we bill be handling in the main loop.

// Block the signals that we will be handling in the main loop
// and re-enable them during pselect.
// At the same time, retrieve the original signal mask before
// blocking, to be passed to pselect.
// blocking, to be passed to the child processes.

sigset_t sigset_block;
sigset_t sigset_child;
sigset_t sigset_pselect;
sigemptyset(&sigset_block);
sigaddset(&sigset_block, SIGPIPE);
sigaddset(&sigset_block, SIGTERM);
sigaddset(&sigset_block, SIGHUP);
sigprocmask(SIG_BLOCK, &sigset_block, &sigset_pselect);

sig.sa_handler = &OnSigPipe; // sigaction() needed for Solaris
sigaction(SIGPIPE, &sig, NULL);
sigaddset(&sigset_block, SIGXFSZ);
if (inFgMode) {
sigaddset(&sigset_block, SIGINT);
sigaddset(&sigset_block, SIGQUIT);
}
sigprocmask(SIG_BLOCK, &sigset_block, &sigset_child);

// enable SIGTERM and SIGHUP during pselect
sigprocmask(0, NULL, &sigset_pselect);
sigdelset(&sigset_pselect, SIGTERM);
sigdelset(&sigset_pselect, SIGHUP);

sig.sa_handler = &OnSigTerm;
sigaction(SIGTERM, &sig, NULL);
sig.sa_handler = &OnSigHup;
sigaction(SIGHUP, &sig, NULL);
sig.sa_handler = SIG_IGN;
sigaction(SIGXFSZ, &sig, NULL);
if (inFgMode) {
sig.sa_handler = SIG_IGN;
sigaction(SIGINT, &sig, NULL);
sig.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &sig, NULL);
}

// Make an accept item to listen for control connections
PRINTF("Creating control listener\n");
Expand Down Expand Up @@ -598,8 +599,6 @@ int main(int argc,char * argv[])
// Run here until something makes it die
while ( ! shutdownServer )
{
const size_t BUFLEN = 100;
char buf[BUFLEN];
connectionItem * p;
fd_set fdset; // FD stuff for select()
int fd, nFd;
Expand All @@ -625,12 +624,6 @@ int main(int argc,char * argv[])

// Handle signals for which signal handlers were called while in pselect.

if (sigPipeSet) {
sigPipeSet = 0;
sprintf( buf, "@@@ Got a sigPipe signal: Did the child close its tty?" NL);
SendToAll( buf, strlen(buf), NULL );
}

if (sigTermSet) {
sigTermSet = 0;
PRINTF("SigTerm received\n");
Expand All @@ -657,7 +650,7 @@ int main(int argc,char * argv[])
PRINTF("Option oneshot is set... exiting\n");
shutdownServer = true;
} else {
npi= processFactory(childExec, childArgv);
npi= processFactory(childExec, childArgv, &sigset_child);
if (npi) AddConnection(npi);
if (firstRun) {
firstRun = false;
Expand Down Expand Up @@ -848,11 +841,6 @@ void DeleteConnection(connectionItem *ci)
assert(connectionNo>=0);
}

static void OnSigPipe(int)
{
sigPipeSet = 1;
}

static void OnSigTerm(int)
{
sigTermSet = 1;
Expand Down
2 changes: 1 addition & 1 deletion procServ.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void DeleteConnection(connectionItem *ci);
// constructors are public:

// processFactory creates the process that we are managing
connectionItem * processFactory(char *exe, char *argv[]);
connectionItem * processFactory(char *exe, char *argv[], sigset_t *sigset);
bool processFactoryNeedsRestart(); // Call to test status of the server process
void processFactorySendSignal(int signal);

Expand Down
4 changes: 2 additions & 2 deletions processClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

class processClass : public connectionItem
{
friend connectionItem * processFactory(char *exe, char *argv[]);
friend connectionItem * processFactory(char *exe, char *argv[], sigset_t *sigset);
friend bool processFactoryNeedsRestart();
friend void processFactorySendSignal(int signal);
public:
processClass(char *exe, char *argv[]);
processClass(char *exe, char *argv[], sigset_t *sigset);
void readFromFd(void);
int Send(const char *,int);
void markDeadIfChildIs(pid_t pid) { if (pid==_pid) _markedForDeletion=true; }
Expand Down
7 changes: 4 additions & 3 deletions processFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool processFactoryNeedsRestart()
return true;
}

connectionItem * processFactory(char *exe, char *argv[])
connectionItem * processFactory(char *exe, char *argv[], sigset_t *sigset)
{
const size_t BUFLEN = 512;
char buf[BUFLEN];
Expand All @@ -71,7 +71,7 @@ connectionItem * processFactory(char *exe, char *argv[])
SendToAll( buf, strlen(buf), 0 );
}

connectionItem *ci = new processClass(exe, argv);
connectionItem *ci = new processClass(exe, argv, sigset);
PRINTF("Created new child connection (processClass %p)\n", ci);
return ci;
}
Expand Down Expand Up @@ -126,7 +126,7 @@ processClass::~processClass()
// parent: sets the minimum time for the next restart
// child: sets the coresize, becomes a process group leader,
// and does an execvp() with the command
processClass::processClass(char *exe, char *argv[])
processClass::processClass(char *exe, char *argv[], sigset_t *sigset)
{
_runningItem=this;
struct rlimit corelimit;
Expand Down Expand Up @@ -208,6 +208,7 @@ processClass::processClass(char *exe, char *argv[])
corelimit.rlim_cur = coreSize;
setrlimit( RLIMIT_CORE, &corelimit );
}
sigprocmask(SIG_SETMASK, sigset, NULL);
if ( chDir && chdir( chDir ) ) {
fprintf( stderr, "%s: child could not chdir to %s, %s\n",
procservName, chDir, strerror(errno) );
Expand Down