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
36 changes: 21 additions & 15 deletions procServ.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,38 +475,44 @@ 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
// 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);

sigaddset(&sigset_block, SIGXFSZ);
if (inFgMode) {
sigaddset(&sigset_block, SIGINT);
sigaddset(&sigset_block, SIGQUIT);
}
Comment on lines +497 to +501

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unblocks some more signals from the child, because the SIG_IGN disposition used in sigaction previously would still apply to the child processes, even if the sigmask was restored.

Would be nice if the commit message called out that nuance.

sigprocmask(SIG_BLOCK, &sigset_block, &sigset_child);

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

sig.sa_handler = &OnSigPipe; // sigaction() needed for Solaris
sigaction(SIGPIPE, &sig, NULL);
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);
}

@dirk-zimoch dirk-zimoch Apr 15, 2021

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? Ignore CTRL-C when running in the foreground?


// Make an accept item to listen for control connections
PRINTF("Creating control listener\n");
Expand Down Expand Up @@ -657,7 +663,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
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be const 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);
Comment on lines +18 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for these

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