Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PID file management with forks > 0 #113

Open
moracca opened this issue Aug 19, 2024 · 1 comment
Open

PID file management with forks > 0 #113

moracca opened this issue Aug 19, 2024 · 1 comment

Comments

@moracca
Copy link

moracca commented Aug 19, 2024

When running MRTG with a config file that specifies the Forks option, I've noticed that MRTG no longer creates the pid file specified on the command line with --pid-file

Or rather, it does create the pid file, but it rapidly gets deleted once MRTG forks.

How can we accurately keep track of the master PID file when using multiple forks in order to be able to send TERM signals etc?

From my recollection this used to work properly, but all the systems I've checked recently are not keeping the PID file when Forks is configured.

Thanks!

@afengler
Copy link

I've also run into this issue. The problem is in the END block in the main program, presumably also affecting the signal handlers.
Currently, when the program exits, it cleans up $main::Cleanfile3 (and 2 and 1), which is the pidfile. The END block also gets called when one of the forks exits. I've made a simple fix to sort out whether or not it's the parent, pretty crude but is doing the job for me.

diff:

--- mrtg.old	2024-12-20 14:15:31.852830000 -0500
+++ mrtg.new	2024-12-20 14:15:24.263401000 -0500
@@ -106,6 +106,7 @@
 $main::STARTTIME = time;
 
 %main::verified_rrd = ();
+$main::is_parent = 1;
     
 if ($MRTG_lib::OS eq 'OS2') {
 # in daemon mode we will pause 3 seconds to be sure that parent died
@@ -114,23 +115,27 @@
 }
 
 if ($MRTG_lib::OS eq 'UNIX') {
-   $SIG{INT} = $SIG{TERM} = 
-           sub {   unlink ${main::Cleanfile} 
-                       if defined $main::Cleanfile;
-                   unlink ${main::Cleanfile2}
-                       if defined $main::Cleanfile2;
-                   unlink ${main::Cleanfile3}
-                       if defined $main::Cleanfile3;
+   $SIG{INT} = $SIG{TERM} = sub {   
+	  			   if ($main::is_parent) {
+					   unlink ${main::Cleanfile} 
+						   if defined $main::Cleanfile;
+					   unlink ${main::Cleanfile2}
+						   if defined $main::Cleanfile2;
+					   unlink ${main::Cleanfile3}
+						   if defined $main::Cleanfile3;
+				   }
                    warn "$NOW: ERROR: Bailout after SIG $_[0]\n";
                    exit 1;
                 };
   $SIG{HUP} = sub {
-                   unlink ${main::Cleanfile} 
-                       if defined $main::Cleanfile;
-                   unlink ${main::Cleanfile2}
-                       if defined $main::Cleanfile2;
-                   unlink ${main::Cleanfile3}
-                       if defined $main::Cleanfile3;
+	  			   if ($main::is_parent) {
+					   unlink ${main::Cleanfile} 
+						   if defined $main::Cleanfile;
+					   unlink ${main::Cleanfile2}
+						   if defined $main::Cleanfile2;
+					   unlink ${main::Cleanfile3}
+						   if defined $main::Cleanfile3;
+				   }
                    die "$NOW: ERROR: Bailout after SIG $_[0]\n";
                 };
 }
@@ -138,9 +143,11 @@
 
 END {
     local($?, $!);
-    unlink ${main::Cleanfile} if defined $main::Cleanfile;
-    unlink ${main::Cleanfile2} if defined $main::Cleanfile2;
-    unlink ${main::Cleanfile3} if defined $main::Cleanfile3;
+    if ($main::is_parent) {
+		unlink ${main::Cleanfile} if defined $main::Cleanfile;
+		unlink ${main::Cleanfile2} if defined $main::Cleanfile2;
+		unlink ${main::Cleanfile3} if defined $main::Cleanfile3;
+	}
 }
 
 &main;
@@ -2627,6 +2634,7 @@
      		$hand[$i] = *D; # funky file handle magic ... 
 		debug ('fork',"Parent $$ after fork of child $i");
 	    } else {  # child
+		$main::is_parent = 0;
 		debug ('fork',"Child $i ($$) after fork");
 		my $res = "";
                 my %deadhost;

It's a little janky, but should be straightforward, set $main::is_parent, unset it in the child after forking, check for it before cleaning files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants