[Nagios-devel] [PATCH] core: Avoid lock file removal race condition

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
Guest

[Nagios-devel] [PATCH] core: Avoid lock file removal race condition

Post by Guest »

--047d7b33dd70bb676904d9ee5b98
Content-Type: text/plain; charset=ISO-8859-1

This patch avoids a race condition in removal of lock files. The race
condition happens when:

1. we receive a SIGHUP, breaking us out of the event execution loop, and
2. receive a SIGTERM - or any other "shutdown" signal - AFTER checking for
the sigshutdown state but BEFORE the do-while loop terminates. I.e, during
execution of cleanup() or close_debug_log() towards the end of the loop.

Signed-off-by: Anton Lofgren
---
base/nagios.c | 15 +++++++++++----
base/utils.c | 1 -
2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/base/nagios.c b/base/nagios.c
index 6b834b2..806d355 100644
--- a/base/nagios.c
+++ b/base/nagios.c
@@ -539,6 +539,11 @@ int main(int argc, char **argv) {

/* keep monitoring things until we get a shutdown command */
do {
+ if (daemon_mode == TRUE && sigrestart == TRUE) {
+ /* if we've restarted in daemon mode, we don't want to
leak the
+ * lock file */
+ my_free(lock_file);
+ }

/* reset program variables */
reset_variables();
@@ -827,10 +832,6 @@ int main(int argc, char **argv) {
iobroker_destroy(nagios_iobs, IOBROKER_CLOSE_SOCKETS);
nagios_iobs = NULL;

- /* make sure lock file has been removed - it may not have
been if we received a shutdown command */
- if(daemon_mode == TRUE)
- unlink(lock_file);
-
/* log a shutdown message */
logit(NSLOG_PROCESS_INFO, TRUE, "Successfully shutdown...
(PID=%d)\n", (int)getpid());
}
@@ -844,6 +845,12 @@ int main(int argc, char **argv) {
}
while(sigrestart == TRUE && sigshutdown == FALSE);

+ /* make sure lock file has been removed - it may not have been if
we received a shutdown command */
+ if(daemon_mode == TRUE) {
+ unlink(lock_file);
+ my_free(lock_file);
+ }
+
/* free misc memory */
my_free(config_file);
}
diff --git a/base/utils.c b/base/utils.c
index af95663..1ff1e7b 100644
--- a/base/utils.c
+++ b/base/utils.c
@@ -3157,7 +3157,6 @@ void free_memory(nagios_macros *mac) {
my_free(temp_path);
my_free(check_result_path);
my_free(command_file);
- my_free(lock_file);
my_free(log_archive_path);

return;
--
1.8.2

--047d7b33dd70bb676904d9ee5b98
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

This patch avoids a=A0 race condition in removal of lock f=
iles. The racecondition happens when:1. we receive a SIGHUP, br=
eaking us out of the event execution loop, and2. receive a SIGTERM - or=
any other "shutdown" signal - AFTER checking for
the sigshutdown state but BEFORE the do-while loop terminates. I.e, duringexecution of cleanup() or close_debug_log() towards the end of the loop.=
Signed-off-by: Anton Lofgren <alofgren@op5.com>
---=A0base/nagios.c | 15 +++++++++++----=A0base/utils.c=A0 |=A0 1 -=
=A02 files changed, 11 insertions(+), 5 deletions(-)diff --git =
a/base/nagios.c b/base/nagios.cindex 6b834b2..806d355 100644--- a/b=
ase/nagios.c
+++ b/base/nagios.c@@ -539,6 +539,11 @@ int main(int argc, char **argv)=
{=A0=A0=A0=A0=A0 =A0=A0=A0 /* keep monitoring things until we get =
a shutdown command */=A0=A0=A0=A0 =A0=A0=A0 do {+=A0=A0=A0 =A0=A0=
=A0 =A0=A0=A0 if (daemon_mode =3D=3D TRUE && sigrestart =3D=3D TRUE=
) {
+=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 /* if we've restarted in daemo=
n mode, we don't want to leak the+=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=
=A0=A0 =A0* lock file */+=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0 my_fre=
e(lock_file);+=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 }=A0=A0=A0=A0=A0 =
=A0=A0=A0 =A0=A0=A0 /* reset program variables */
=A0=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 reset_variables();@@ -827,10 +832,6 @@=
int main(int argc, char **argv) {=A0=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=
=A0=A0 iobroker_destroy(nagios_iobs, IOBROKER_CLOSE_SOCKETS);=

...[email truncated]...


This post was automatically imported from historical nagios-devel mailing list archives
Original poster: alofgren@op5.com
Locked